add more spacing
[personal-kdebase.git] / runtime / knetattach / knetattach.cpp
blobcc3dade7d69c1d2f7a52273a268d84aaa330d53b
1 /*
2 Copyright (C) 2004 George Staikos <staikos@kde.org>
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; see the file COPYING. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
21 #include "knetattach.h"
23 #include <QtCore/QVariant>
24 #include <kio/netaccess.h>
25 #include <kmessagebox.h>
26 #include <kiconloader.h>
27 #include <klocale.h>
28 #include <kglobalsettings.h>
29 #include <kconfig.h>
30 #include <kapplication.h>
31 #include <kstandarddirs.h>
32 #include <kdirnotify.h>
33 #include <kcharsets.h>
35 KNetAttach::KNetAttach( QWidget* parent )
36 : Q3Wizard( parent ), Ui_KNetAttach()
38 setupUi( this );
40 connect(_recent, SIGNAL(toggled(bool)), _recentConnectionName, SLOT(setEnabled(bool)));
41 connect(_connectionName, SIGNAL(textChanged(const QString&)), this, SLOT(updateParametersPageStatus()));
42 connect(_user, SIGNAL(textChanged(const QString&)), this, SLOT(updateParametersPageStatus()));
43 connect(_host, SIGNAL(textChanged(const QString&)), this, SLOT(updateParametersPageStatus()));
44 connect(_path, SIGNAL(textChanged(const QString&)), this, SLOT(updateParametersPageStatus()));
45 connect(_useEncryption, SIGNAL(toggled(bool)), this, SLOT(updatePort(bool)));
46 connect(_createIcon, SIGNAL(toggled(bool)), this, SLOT(updateFinishButtonText(bool)));
47 connect( this, SIGNAL(helpClicked() ), this, SLOT( slotHelpClicked() ) );
48 setWindowIcon(KIcon("knetattach"));
49 disconnect(finishButton(), SIGNAL(clicked()), (QDialog*)this, SLOT(accept()));
50 connect(finishButton(), SIGNAL(clicked()), this, SLOT(finished()));
51 finishButton()->setText(i18n("Save && C&onnect"));
52 //setResizeMode(Fixed); FIXME: make the wizard fixed-geometry
53 setFinishEnabled(_folderParameters, false);
54 KConfig crecent( "krecentconnections", KConfig::NoGlobals );
55 KConfigGroup recent(&crecent, "General");
56 QStringList idx = recent.readEntry("Index",QStringList());
57 if (idx.isEmpty()) {
58 _recent->setEnabled(false);
59 if (_recent->isChecked()) {
60 _webfolder->setChecked(true);
62 } else {
63 _recent->setEnabled(true);
64 _recentConnectionName->addItems(idx);
66 _encoding->clear();
67 _encoding->addItems(KGlobal::charsets()->descriptiveEncodingNames());
68 _encoding->setCurrentIndex(0);
71 void KNetAttach::slotHelpClicked()
73 KToolInvocation::invokeHelp(QString(), "knetattach" );
76 void KNetAttach::setInformationText( const QString &type )
78 QString text;
80 if (type=="WebFolder") {
81 text = i18n("Enter a name for this <i>WebFolder</i> as well as a server address, port and folder path to use and press the <b>Save & Connect</b> button.");
82 } else if (type=="Fish") {
83 text = i18n("Enter a name for this <i>Secure shell connection</i> as well as a server address, port and folder path to use and press the <b>Save & Connect</b> button.");
84 } else if (type=="FTP") {
85 text = i18n("Enter a name for this <i>File Transfer Protocol connection</i> as well as a server address and folder path to use and press the <b>Save & Connect</b> button.");
86 } else if (type=="SMB") {
87 text = i18n("Enter a name for this <i>Microsoft Windows network drive</i> as well as a server address and folder path to use and press the <b>Save & Connect</b> button.");
90 _informationText->setText(text);
93 void KNetAttach::showPage( QWidget *page )
95 if (page == _folderType) {
96 } else if (page == _folderParameters) {
97 _host->setFocus();
98 _connectionName->setFocus();
100 if (_webfolder->isChecked()) {
101 setInformationText("WebFolder");
102 updateForProtocol("WebFolder");
103 _port->setValue(80);
104 } else if (_fish->isChecked()) {
105 setInformationText("Fish");
106 updateForProtocol("Fish");
107 _port->setValue(22);
108 } else if (_ftp->isChecked()) {
109 setInformationText("FTP");
110 updateForProtocol("FTP");
111 _port->setValue(21);
112 if (_path->text().isEmpty()) {
113 _path->setText("/");
115 } else if (_smb->isChecked()) {
116 setInformationText("SMB");
117 updateForProtocol("SMB");
118 } else { //if (_recent->isChecked()) {
119 KConfig recent( "krecentconnections", KConfig::NoGlobals );
120 if (!recent.hasGroup(_recentConnectionName->currentText())) {
121 KConfigGroup group = recent.group("General");
122 QStringList idx = group.readEntry("Index",QStringList());
123 if (idx.isEmpty()) {
124 _recent->setEnabled(false);
125 if (_recent->isChecked()) {
126 _webfolder->setChecked(true);
128 } else {
129 _recent->setEnabled(true);
130 _recentConnectionName->addItems(idx);
132 showPage(_folderType);
133 return;
135 KConfigGroup group = recent.group(_recentConnectionName->currentText());
136 _type = group.readEntry("Type");
137 setInformationText(_type);
138 if (!updateForProtocol(_type)) {
139 // FIXME: handle error
141 KUrl u(group.readEntry("URL"));
142 _host->setText(u.host());
143 _user->setText(u.user());
144 _path->setText(u.path());
145 if (group.hasKey("Port")) {
146 _port->setValue(group.readEntry("Port",0));
147 } else {
148 _port->setValue(u.port());
150 _connectionName->setText(_recentConnectionName->currentText());
151 _createIcon->setChecked(false);
153 updateParametersPageStatus();
156 Q3Wizard::showPage(page);
160 void KNetAttach::updateParametersPageStatus()
162 setFinishEnabled(_folderParameters,
163 !_host->text().trimmed().isEmpty() &&
164 !_path->text().trimmed().isEmpty() &&
165 !_connectionName->text().trimmed().isEmpty());
168 void KNetAttach::finished()
170 setBackEnabled(_folderParameters,false);
171 setFinishEnabled(_folderParameters, false);
172 KUrl url;
173 if (_type == "WebFolder") {
174 if (_useEncryption->isChecked()) {
175 url.setProtocol("webdavs");
176 } else {
177 url.setProtocol("webdav");
179 url.setPort(_port->value());
180 } else if (_type == "Fish") {
181 KConfig config("kio_fishrc");
182 KConfigGroup cg(&config, _host->text().trimmed());
183 cg.writeEntry("Charset", KGlobal::charsets()->encodingForName(_encoding->currentText()));
184 url.setProtocol("fish");
185 url.setPort(_port->value());
186 } else if (_type == "FTP") {
187 url.setProtocol("ftp");
188 url.setPort(_port->value());
189 KConfig config("kio_ftprc");
190 KConfigGroup cg(&config, _host->text().trimmed());
191 cg.writeEntry("Charset", KGlobal::charsets()->encodingForName(_encoding->currentText()));
192 config.sync();
193 } else if (_type == "SMB") {
194 url.setProtocol("smb");
195 } else { // recent
198 url.setHost(_host->text().trimmed());
199 url.setUser(_user->text().trimmed());
200 QString path = _path->text().trimmed();
201 #ifndef Q_WS_WIN
202 // could a relative path really be made absolute by simply prepending a '/' ?
203 if (!path.startsWith('/')) {
204 path = QString("/") + path;
206 #endif
207 url.setPath(path);
208 _folderParameters->setEnabled(false);
209 bool success = doConnectionTest(url);
210 _folderParameters->setEnabled(true);
211 if (!success) {
212 KMessageBox::sorry(this, i18n("Unable to connect to server. Please check your settings and try again."));
213 showPage(_folderParameters);
214 setBackEnabled(_folderParameters, true);
215 return;
218 KToolInvocation::invokeBrowser(url.url());
220 QString name = _connectionName->text().trimmed();
222 if (_createIcon->isChecked()) {
223 KGlobal::dirs()->addResourceType("remote_entries", "data", "remoteview");
225 QString path = KGlobal::dirs()->saveLocation("remote_entries");
226 path += name + ".desktop";
227 KConfig _desktopFile( path, KConfig::SimpleConfig );
228 KConfigGroup desktopFile(&_desktopFile, "Desktop Entry");
229 desktopFile.writeEntry("Icon", "folder-remote");
230 desktopFile.writeEntry("Name", name);
231 desktopFile.writeEntry("Type", "Link");
232 desktopFile.writeEntry("URL", url.prettyUrl());
233 desktopFile.writeEntry("Charset", url.fileEncoding());
234 desktopFile.sync();
235 org::kde::KDirNotify::emitFilesAdded( "remote:/" );
238 if (!name.isEmpty()) {
239 KConfig _recent("krecentconnections", KConfig::NoGlobals);
240 KConfigGroup recent(&_recent, "General");
241 QStringList idx = recent.readEntry("Index",QStringList());
242 _recent.deleteGroup(name); // erase anything stale
243 if (idx.contains(name)) {
244 idx.removeAll(name);
245 idx.prepend(name);
246 recent.writeEntry("Index", idx);
247 } else {
248 QString last;
249 if (!idx.isEmpty()) {
250 last = idx.last();
251 idx.pop_back();
253 idx.prepend(name);
254 _recent.deleteGroup(last);
255 recent.writeEntry("Index", idx);
257 recent = KConfigGroup(&_recent,name);
258 recent.writeEntry("URL", url.prettyUrl());
259 if (_type == "WebFolder" || _type == "Fish" || _type == "FTP") {
260 recent.writeEntry("Port", _port->value());
262 recent.writeEntry("Type", _type);
263 recent.sync();
266 QDialog::accept();
270 void KNetAttach::updatePort(bool encryption)
272 if (_webfolder->isChecked()) {
273 if (encryption) {
274 _port->setValue(443);
275 } else {
276 _port->setValue(80);
282 bool KNetAttach::doConnectionTest(const KUrl& url)
284 KIO::UDSEntry entry;
285 if (KIO::NetAccess::stat(url, entry, this)) {
286 // Anything to test here?
287 return true;
289 return false;
293 bool KNetAttach::updateForProtocol(const QString& protocol)
295 _type = protocol;
296 if (protocol == "WebFolder") {
297 _useEncryption->show();
298 _portText->show();
299 _port->show();
300 _userText->show();
301 _user->show();
302 _encodingText->hide();
303 _encoding->hide();
304 } else if (protocol == "Fish") {
305 _useEncryption->hide();
306 _portText->show();
307 _port->show();
308 _userText->show();
309 _user->show();
310 } else if (protocol == "FTP") {
311 _useEncryption->hide();
312 _portText->show();
313 _port->show();
314 _userText->show();
315 _user->show();
316 _encodingText->show();
317 _encoding->show();
318 } else if (protocol == "SMB") {
319 _useEncryption->hide();
320 _portText->hide();
321 _port->hide();
322 _userText->hide();
323 _user->hide();
324 _encodingText->hide();
325 _encoding->hide();
326 } else {
327 _type = "";
328 return false;
330 return true;
334 void KNetAttach::updateFinishButtonText(bool save)
336 if (save) {
337 finishButton()->setText(i18n("Save && C&onnect"));
338 } else {
339 finishButton()->setText(i18n("C&onnect"));
343 // vim: ts=8 sw=4 noet
344 #include "knetattach.moc"