File indexing completed on 2022-08-05 15:23:20

0001 /***************************************************************************
0002 *   Copyright 2007 Niko Sams <niko.sams@gmail.com>                        *
0003 *                                                                         *
0004 *   This program is free software; you can redistribute it and/or modify  *
0005 *   it under the terms of the GNU General Public License as published by  *
0006 *   the Free Software Foundation; either version 2 of the License, or     *
0007 *   (at your option) any later version.                                   *
0008 *                                                                         *
0009 ***************************************************************************/
0010 #include "uploadprofiledlg.h"
0011 
0012 #include <QListWidgetItem>
0013 #include <KLocalizedString>
0014 #include <QFileDialog>
0015 #include <QDialog>
0016 #include <QDialogButtonBox>
0017 #include <QPushButton>
0018 #include <qglobal.h>
0019 
0020 #include <kprotocolmanager.h>
0021 #include <kprotocolinfo.h>
0022 #include <kio/statjob.h>
0023 #include <KJobWidgets>
0024 #include <kmessagebox.h>
0025 
0026 #include "ui_uploadprofiledlg.h"
0027 #include "uploadprofileitem.h"
0028 
0029 UploadProfileDlg::UploadProfileDlg(QWidget *parent)
0030     : QDialog (parent)
0031 {
0032     setWindowTitle(i18n("Upload Profile"));
0033 
0034     QVBoxLayout *mainLayout = new QVBoxLayout;
0035     setLayout(mainLayout);
0036 
0037     QWidget* widget = new QWidget(this);
0038     m_ui = new Ui::UploadProfileDlg();
0039     m_ui->setupUi(widget);
0040 
0041     m_ui->browseButtonLocal->setIcon(QIcon::fromTheme("document-open"));
0042     connect(m_ui->browseButtonLocal, SIGNAL(clicked()), this, SLOT(browseLocal()));
0043     m_ui->browseButton->setIcon(QIcon::fromTheme("document-open"));
0044     connect(m_ui->browseButton, SIGNAL(clicked()), this, SLOT(browse()));
0045 
0046     QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
0047     QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
0048     okButton->setDefault(true);
0049     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0050 
0051     connect(buttonBox, SIGNAL(accepted()), this, SLOT(slotAcceptButtonClicked()));
0052     connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
0053 
0054     mainLayout->addWidget(widget);
0055     mainLayout->addWidget(buttonBox);
0056 
0057     QStringList protocols = KProtocolInfo::protocols();
0058     protocols.sort();
0059     Q_FOREACH (QString p, protocols) {
0060         QUrl u;
0061         u.setScheme(p);
0062         if (KProtocolManager::supportsWriting(u) && KProtocolManager::supportsMakeDir(u)
0063             && KProtocolManager::supportsDeleting(u)) {
0064             m_ui->comboProtocol->addItem(p);
0065         }
0066     }
0067 }
0068 
0069 UploadProfileDlg::~UploadProfileDlg()
0070 {
0071     delete m_ui;
0072 }
0073 
0074 int UploadProfileDlg::editProfile(UploadProfileItem* item)
0075 {
0076     m_ui->lineProfileName->setText(item->text());
0077     m_ui->defaultProfile->setChecked(item->isDefault());
0078     m_ui->lineLocalPath->setText(item->localUrl().toString());
0079     updateUrl(item->url());
0080 
0081     int result = exec();
0082     if (result == QDialog::Accepted) {
0083         item->setText(m_ui->lineProfileName->text());
0084         item->setUrl(currentUrl());
0085         QUrl localUrl = QUrl(m_ui->lineLocalPath->text());
0086         item->setLocalUrl(localUrl);
0087         item->setDefault(m_ui->defaultProfile->checkState() == Qt::Checked);
0088     }
0089     return result;
0090 }
0091 
0092 QUrl UploadProfileDlg::currentUrl()
0093 {
0094     QUrl url;
0095     url.setHost(m_ui->lineHost->text());
0096     url.setUserName(m_ui->lineUser->text());
0097     url.setPath(m_ui->linePath->text());
0098     if (m_ui->port->text().toInt() > 0) url.setPort(m_ui->port->text().toInt());
0099     url.setScheme(m_ui->comboProtocol->currentText());
0100     return url;
0101 }
0102 
0103 void UploadProfileDlg::updateUrl(const QUrl& url)
0104 {
0105     m_ui->lineHost->setText(url.host());
0106     m_ui->lineUser->setText(url.userName());
0107     m_ui->linePath->setText(url.path());
0108     if (url.port() > 0) {
0109         m_ui->port->setText(QString::number(url.port()));
0110     } else {
0111         m_ui->port->setText("");
0112     }
0113     int index = m_ui->comboProtocol->findData(url.scheme(), Qt::DisplayRole);
0114     m_ui->comboProtocol->setCurrentIndex(index);
0115 }
0116 
0117 void UploadProfileDlg::browse()
0118 {
0119     QUrl chosenDir = QFileDialog::getExistingDirectoryUrl(this, QString(), currentUrl());
0120 
0121     if(chosenDir.isValid()) {
0122         updateUrl(chosenDir);
0123     }
0124 }
0125 
0126 void UploadProfileDlg::browseLocal()
0127 {
0128     QUrl chosenDir = QFileDialog::getExistingDirectoryUrl(this, QString(), m_ui->lineLocalPath->text());
0129 
0130     if(chosenDir.isValid()) {
0131         m_ui->lineLocalPath->setText(chosenDir.path());
0132     }
0133 }
0134 
0135 void UploadProfileDlg::slotAcceptButtonClicked()
0136 {
0137     KIO::StatJob* statJob = KIO::stat(currentUrl());
0138     statJob->setSide(KIO::StatJob::DestinationSide);
0139     KJobWidgets::setWindow(statJob, this);
0140     bool dirExists = statJob->exec();
0141     if (!dirExists) {
0142         KMessageBox::error(this, i18n("The specified URL does not exist."));
0143         return;
0144     }
0145     
0146     //TODO: check if local dir is subpath of project dir
0147     QString selectedLocalPath = m_ui->lineLocalPath->text();
0148     if(!QDir(selectedLocalPath).exists()) {
0149         KMessageBox::error(this, i18n("The specified local directory does not exist."));
0150         return;
0151     }
0152     
0153     QDialog::accept();
0154 }
0155 
0156 // kate: space-indent on; indent-width 4; tab-width 4; replace-tabs on