-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
uploadprofiledlg.cpp
156 lines (132 loc) · 5.04 KB
/
uploadprofiledlg.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/***************************************************************************
* Copyright 2007 Niko Sams <[email protected]> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "uploadprofiledlg.h"
#include <QListWidgetItem>
#include <KLocalizedString>
#include <QFileDialog>
#include <QDialog>
#include <QDialogButtonBox>
#include <QPushButton>
#include <qglobal.h>
#include <kprotocolmanager.h>
#include <kprotocolinfo.h>
#include <kio/statjob.h>
#include <KJobWidgets>
#include <kmessagebox.h>
#include "ui_uploadprofiledlg.h"
#include "uploadprofileitem.h"
UploadProfileDlg::UploadProfileDlg(QWidget *parent)
: QDialog (parent)
{
setWindowTitle(i18n("Upload Profile"));
QVBoxLayout *mainLayout = new QVBoxLayout;
setLayout(mainLayout);
QWidget* widget = new QWidget(this);
m_ui = new Ui::UploadProfileDlg();
m_ui->setupUi(widget);
m_ui->browseButtonLocal->setIcon(QIcon::fromTheme("document-open"));
connect(m_ui->browseButtonLocal, SIGNAL(clicked()), this, SLOT(browseLocal()));
m_ui->browseButton->setIcon(QIcon::fromTheme("document-open"));
connect(m_ui->browseButton, SIGNAL(clicked()), this, SLOT(browse()));
QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
okButton->setDefault(true);
okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
connect(buttonBox, SIGNAL(accepted()), this, SLOT(slotAcceptButtonClicked()));
connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
mainLayout->addWidget(widget);
mainLayout->addWidget(buttonBox);
QStringList protocols = KProtocolInfo::protocols();
protocols.sort();
Q_FOREACH (QString p, protocols) {
QUrl u;
u.setScheme(p);
if (KProtocolManager::supportsWriting(u) && KProtocolManager::supportsMakeDir(u)
&& KProtocolManager::supportsDeleting(u)) {
m_ui->comboProtocol->addItem(p);
}
}
}
UploadProfileDlg::~UploadProfileDlg()
{
delete m_ui;
}
int UploadProfileDlg::editProfile(UploadProfileItem* item)
{
m_ui->lineProfileName->setText(item->text());
m_ui->defaultProfile->setChecked(item->isDefault());
m_ui->lineLocalPath->setText(item->localUrl().toString());
updateUrl(item->url());
int result = exec();
if (result == QDialog::Accepted) {
item->setText(m_ui->lineProfileName->text());
item->setUrl(currentUrl());
QUrl localUrl = QUrl(m_ui->lineLocalPath->text());
item->setLocalUrl(localUrl);
item->setDefault(m_ui->defaultProfile->checkState() == Qt::Checked);
}
return result;
}
QUrl UploadProfileDlg::currentUrl()
{
QUrl url;
url.setHost(m_ui->lineHost->text());
url.setUserName(m_ui->lineUser->text());
url.setPath(m_ui->linePath->text());
if (m_ui->port->text().toInt() > 0) url.setPort(m_ui->port->text().toInt());
url.setScheme(m_ui->comboProtocol->currentText());
return url;
}
void UploadProfileDlg::updateUrl(const QUrl& url)
{
m_ui->lineHost->setText(url.host());
m_ui->lineUser->setText(url.userName());
m_ui->linePath->setText(url.path());
if (url.port() > 0) {
m_ui->port->setText(QString::number(url.port()));
} else {
m_ui->port->setText("");
}
int index = m_ui->comboProtocol->findData(url.scheme(), Qt::DisplayRole);
m_ui->comboProtocol->setCurrentIndex(index);
}
void UploadProfileDlg::browse()
{
QUrl chosenDir = QFileDialog::getExistingDirectoryUrl(this, QString(), currentUrl());
if(chosenDir.isValid()) {
updateUrl(chosenDir);
}
}
void UploadProfileDlg::browseLocal()
{
QUrl chosenDir = QFileDialog::getExistingDirectoryUrl(this, QString(), m_ui->lineLocalPath->text());
if(chosenDir.isValid()) {
m_ui->lineLocalPath->setText(chosenDir.path());
}
}
void UploadProfileDlg::slotAcceptButtonClicked()
{
KIO::StatJob* statJob = KIO::stat(currentUrl());
statJob->setSide(KIO::StatJob::DestinationSide);
KJobWidgets::setWindow(statJob, this);
bool dirExists = statJob->exec();
if (!dirExists) {
KMessageBox::error(this, i18n("The specified URL does not exist."));
return;
}
//TODO: check if local dir is subpath of project dir
QString selectedLocalPath = m_ui->lineLocalPath->text();
if(!QDir(selectedLocalPath).exists()) {
KMessageBox::error(this, i18n("The specified local directory does not exist."));
return;
}
QDialog::accept();
}
// kate: space-indent on; indent-width 4; tab-width 4; replace-tabs on