1 | /* |
2 | Copyright (C) 2004 George Staikos <staikos@kde.org> |
3 | |
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. |
8 | |
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. |
13 | |
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. |
18 | */ |
19 | |
20 | |
21 | #include "knetattach.h" |
22 | |
23 | #include <QtCore/QVariant> |
24 | |
25 | #include <KIO/NetAccess> |
26 | #include <KMessageBox> |
27 | #include <KIcon> |
28 | #include <KLocale> |
29 | #include <KGlobalSettings> |
30 | #include <KConfig> |
31 | #include <KStandardDirs> |
32 | #include <KDirNotify> |
33 | #include <KCharsets> |
34 | #include <KDebug> |
35 | #include <KRun> |
36 | #include <KToolInvocation> |
37 | |
38 | KNetAttach::KNetAttach( QWidget* parent ) |
39 | : QWizard( parent ), Ui_KNetAttach() |
40 | { |
41 | setupUi( this ); |
42 | |
43 | connect(_recent, SIGNAL(toggled(bool)), _recentConnectionName, SLOT(setEnabled(bool))); |
44 | connect(_connectionName, SIGNAL(textChanged(QString)), this, SLOT(updateParametersPageStatus())); |
45 | connect(_user, SIGNAL(textChanged(QString)), this, SLOT(updateParametersPageStatus())); |
46 | connect(_host, SIGNAL(textChanged(QString)), this, SLOT(updateParametersPageStatus())); |
47 | connect(_path, SIGNAL(textChanged(QString)), this, SLOT(updateParametersPageStatus())); |
48 | connect(_useEncryption, SIGNAL(toggled(bool)), this, SLOT(updatePort(bool))); |
49 | connect(_createIcon, SIGNAL(toggled(bool)), this, SLOT(updateFinishButtonText(bool))); |
50 | connect( this, SIGNAL(helpRequested()), this, SLOT(slotHelpClicked()) ); |
51 | connect( this, SIGNAL(currentIdChanged(int)), this, SLOT(slotPageChanged(int)) ); |
52 | setWindowIcon(KIcon("knetattach" )); |
53 | setOption(HaveHelpButton, true); |
54 | //setResizeMode(Fixed); FIXME: make the wizard fixed-geometry |
55 | button(FinishButton)->setEnabled(false); |
56 | KConfig crecent( "krecentconnections" , KConfig::NoGlobals ); |
57 | KConfigGroup recent(&crecent, "General" ); |
58 | QStringList idx = recent.readEntry("Index" ,QStringList()); |
59 | if (idx.isEmpty()) { |
60 | _recent->setEnabled(false); |
61 | if (_recent->isChecked()) { |
62 | _webfolder->setChecked(true); |
63 | } |
64 | } else { |
65 | _recent->setEnabled(true); |
66 | _recentConnectionName->addItems(idx); |
67 | } |
68 | _encoding->clear(); |
69 | _encoding->addItems(KGlobal::charsets()->descriptiveEncodingNames()); |
70 | const int codecForLocaleIdx = _encoding->findText(QTextCodec::codecForLocale()->name(), Qt::MatchContains); |
71 | _encoding->setCurrentIndex(codecForLocaleIdx != -1 ? codecForLocaleIdx : 0); |
72 | } |
73 | |
74 | void KNetAttach::slotPageChanged( int ) |
75 | { |
76 | updateFinishButtonText(true); |
77 | } |
78 | |
79 | void KNetAttach::slotHelpClicked() |
80 | { |
81 | KToolInvocation::invokeHelp(QString(), "knetattach" ); |
82 | } |
83 | |
84 | void KNetAttach::setInformationText( const QString &type ) |
85 | { |
86 | QString text; |
87 | |
88 | if (type=="WebFolder" ) { |
89 | 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." ); |
90 | } else if (type=="Fish" ) { |
91 | 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." ); |
92 | } else if (type=="FTP" ) { |
93 | 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." ); |
94 | } else if (type=="SMB" ) { |
95 | 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." ); |
96 | } |
97 | |
98 | _informationText->setText(text); |
99 | } |
100 | |
101 | void KNetAttach::updateParametersPageStatus() |
102 | { |
103 | button(FinishButton)->setEnabled( |
104 | !_host->text().trimmed().isEmpty() && |
105 | !_path->text().trimmed().isEmpty() && |
106 | !_connectionName->text().trimmed().isEmpty()); |
107 | } |
108 | |
109 | bool KNetAttach::validateCurrentPage() |
110 | { |
111 | if (currentPage() == _folderType){ |
112 | _host->setFocus(); |
113 | _connectionName->setFocus(); |
114 | |
115 | if (_webfolder->isChecked()) { |
116 | setInformationText("WebFolder" ); |
117 | updateForProtocol("WebFolder" ); |
118 | _port->setValue(80); |
119 | } else if (_fish->isChecked()) { |
120 | setInformationText("Fish" ); |
121 | updateForProtocol("Fish" ); |
122 | _port->setValue(22); |
123 | } else if (_ftp->isChecked()) { |
124 | setInformationText("FTP" ); |
125 | updateForProtocol("FTP" ); |
126 | _port->setValue(21); |
127 | if (_path->text().isEmpty()) { |
128 | _path->setText("/" ); |
129 | } |
130 | } else if (_smb->isChecked()) { |
131 | setInformationText("SMB" ); |
132 | updateForProtocol("SMB" ); |
133 | } else { //if (_recent->isChecked()) { |
134 | KConfig recent( "krecentconnections" , KConfig::NoGlobals ); |
135 | if (!recent.hasGroup(_recentConnectionName->currentText())) { |
136 | KConfigGroup group = recent.group("General" ); |
137 | QStringList idx = group.readEntry("Index" ,QStringList()); |
138 | if (idx.isEmpty()) { |
139 | _recent->setEnabled(false); |
140 | if (_recent->isChecked()) { |
141 | _webfolder->setChecked(true); |
142 | } |
143 | } else { |
144 | _recent->setEnabled(true); |
145 | _recentConnectionName->addItems(idx); |
146 | } |
147 | return false; |
148 | } |
149 | KConfigGroup group = recent.group(_recentConnectionName->currentText()); |
150 | _type = group.readEntry("Type" ); |
151 | setInformationText(_type); |
152 | if (!updateForProtocol(_type)) { |
153 | // FIXME: handle error |
154 | } |
155 | KUrl u(group.readEntry("URL" )); |
156 | _host->setText(u.host()); |
157 | _user->setText(u.user()); |
158 | _path->setText(u.path()); |
159 | if (group.hasKey("Port" )) { |
160 | _port->setValue(group.readEntry("Port" ,0)); |
161 | } else { |
162 | _port->setValue(u.port()); |
163 | } |
164 | _connectionName->setText(_recentConnectionName->currentText()); |
165 | _createIcon->setChecked(false); |
166 | } |
167 | updateParametersPageStatus(); |
168 | |
169 | }else{ |
170 | button(BackButton)->setEnabled(false); |
171 | button(FinishButton)->setEnabled(false); |
172 | KUrl url; |
173 | if (_type == "WebFolder" ) { |
174 | if (_useEncryption->isChecked()) { |
175 | url.setProtocol("webdavs" ); |
176 | } else { |
177 | url.setProtocol("webdav" ); |
178 | } |
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(_protocolText->currentText()); |
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 |
196 | } |
197 | |
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; |
205 | } |
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 | button(BackButton)->setEnabled(true); |
214 | return false; |
215 | } |
216 | |
217 | KRun::runUrl(url, "inode/directory" , this); |
218 | |
219 | QString name = _connectionName->text().trimmed(); |
220 | |
221 | if (_createIcon->isChecked()) { |
222 | KGlobal::dirs()->addResourceType("remote_entries" , "data" , "remoteview" ); |
223 | |
224 | QString path = KGlobal::dirs()->saveLocation("remote_entries" ); |
225 | path += name + ".desktop" ; |
226 | KConfig _desktopFile( path, KConfig::SimpleConfig ); |
227 | KConfigGroup desktopFile(&_desktopFile, "Desktop Entry" ); |
228 | desktopFile.writeEntry("Icon" , "folder-remote" ); |
229 | desktopFile.writeEntry("Name" , name); |
230 | desktopFile.writeEntry("Type" , "Link" ); |
231 | desktopFile.writeEntry("URL" , url.prettyUrl()); |
232 | desktopFile.writeEntry("Charset" , url.fileEncoding()); |
233 | desktopFile.sync(); |
234 | org::kde::KDirNotify::emitFilesAdded( "remote:/" ); |
235 | } |
236 | |
237 | if (!name.isEmpty()) { |
238 | KConfig _recent("krecentconnections" , KConfig::NoGlobals); |
239 | KConfigGroup recent(&_recent, "General" ); |
240 | QStringList idx = recent.readEntry("Index" ,QStringList()); |
241 | _recent.deleteGroup(name); // erase anything stale |
242 | if (idx.contains(name)) { |
243 | idx.removeAll(name); |
244 | idx.prepend(name); |
245 | recent.writeEntry("Index" , idx); |
246 | } else { |
247 | QString last; |
248 | if (!idx.isEmpty()) { |
249 | last = idx.last(); |
250 | idx.pop_back(); |
251 | } |
252 | idx.prepend(name); |
253 | _recent.deleteGroup(last); |
254 | recent.writeEntry("Index" , idx); |
255 | } |
256 | recent = KConfigGroup(&_recent,name); |
257 | recent.writeEntry("URL" , url.prettyUrl()); |
258 | if (_type == "WebFolder" || _type == "Fish" || _type == "FTP" ) { |
259 | recent.writeEntry("Port" , _port->value()); |
260 | } |
261 | recent.writeEntry("Type" , _type); |
262 | recent.sync(); |
263 | } |
264 | } |
265 | return true; |
266 | } |
267 | |
268 | |
269 | void KNetAttach::updatePort(bool encryption) |
270 | { |
271 | if (_webfolder->isChecked()) { |
272 | if (encryption) { |
273 | _port->setValue(443); |
274 | } else { |
275 | _port->setValue(80); |
276 | } |
277 | } |
278 | } |
279 | |
280 | |
281 | bool KNetAttach::doConnectionTest(const KUrl& url) |
282 | { |
283 | KIO::UDSEntry entry; |
284 | if (KIO::NetAccess::stat(url, entry, this)) { |
285 | // Anything to test here? |
286 | return true; |
287 | } |
288 | return false; |
289 | } |
290 | |
291 | |
292 | bool KNetAttach::updateForProtocol(const QString& protocol) |
293 | { |
294 | _type = protocol; |
295 | if (protocol == "WebFolder" ) { |
296 | _useEncryption->show(); |
297 | _portText->show(); |
298 | _port->show(); |
299 | _protocol->hide(); |
300 | _protocolText->hide(); |
301 | _userText->show(); |
302 | _user->show(); |
303 | _encodingText->hide(); |
304 | _encoding->hide(); |
305 | } else if (protocol == "Fish" ) { |
306 | _useEncryption->hide(); |
307 | _portText->show(); |
308 | _port->show(); |
309 | _protocol->show(); |
310 | _protocolText->show(); |
311 | _userText->show(); |
312 | _user->show(); |
313 | _encodingText->show(); |
314 | _encoding->show(); |
315 | } else if (protocol == "FTP" ) { |
316 | _useEncryption->hide(); |
317 | _portText->show(); |
318 | _port->show(); |
319 | _protocol->hide(); |
320 | _protocolText->hide(); |
321 | _userText->show(); |
322 | _user->show(); |
323 | _encodingText->show(); |
324 | _encoding->show(); |
325 | } else if (protocol == "SMB" ) { |
326 | _useEncryption->hide(); |
327 | _portText->hide(); |
328 | _port->hide(); |
329 | _protocol->hide(); |
330 | _protocolText->hide(); |
331 | _userText->hide(); |
332 | _user->hide(); |
333 | _encodingText->hide(); |
334 | _encoding->hide(); |
335 | } else { |
336 | _type = "" ; |
337 | return false; |
338 | } |
339 | return true; |
340 | } |
341 | |
342 | |
343 | void KNetAttach::updateFinishButtonText(bool save) |
344 | { |
345 | if (save) { |
346 | button(FinishButton)->setText(i18n("Save && C&onnect" )); |
347 | } else { |
348 | button(FinishButton)->setText(i18n("C&onnect" )); |
349 | } |
350 | } |
351 | |
352 | // vim: ts=8 sw=4 noet |
353 | #include "knetattach.moc" |
354 | |