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>
28 #include <kglobalsettings.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()
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());
58 _recent
->setEnabled(false);
59 if (_recent
->isChecked()) {
60 _webfolder
->setChecked(true);
63 _recent
->setEnabled(true);
64 _recentConnectionName
->addItems(idx
);
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
)
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
) {
98 _connectionName
->setFocus();
100 if (_webfolder
->isChecked()) {
101 setInformationText("WebFolder");
102 updateForProtocol("WebFolder");
104 } else if (_fish
->isChecked()) {
105 setInformationText("Fish");
106 updateForProtocol("Fish");
108 } else if (_ftp
->isChecked()) {
109 setInformationText("FTP");
110 updateForProtocol("FTP");
112 if (_path
->text().isEmpty()) {
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());
124 _recent
->setEnabled(false);
125 if (_recent
->isChecked()) {
126 _webfolder
->setChecked(true);
129 _recent
->setEnabled(true);
130 _recentConnectionName
->addItems(idx
);
132 showPage(_folderType
);
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));
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);
173 if (_type
== "WebFolder") {
174 if (_useEncryption
->isChecked()) {
175 url
.setProtocol("webdavs");
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()));
193 } else if (_type
== "SMB") {
194 url
.setProtocol("smb");
198 url
.setHost(_host
->text().trimmed());
199 url
.setUser(_user
->text().trimmed());
200 QString path
= _path
->text().trimmed();
202 // could a relative path really be made absolute by simply prepending a '/' ?
203 if (!path
.startsWith('/')) {
204 path
= QString("/") + path
;
208 _folderParameters
->setEnabled(false);
209 bool success
= doConnectionTest(url
);
210 _folderParameters
->setEnabled(true);
212 KMessageBox::sorry(this, i18n("Unable to connect to server. Please check your settings and try again."));
213 showPage(_folderParameters
);
214 setBackEnabled(_folderParameters
, true);
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());
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
)) {
246 recent
.writeEntry("Index", idx
);
249 if (!idx
.isEmpty()) {
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
);
270 void KNetAttach::updatePort(bool encryption
)
272 if (_webfolder
->isChecked()) {
274 _port
->setValue(443);
282 bool KNetAttach::doConnectionTest(const KUrl
& url
)
285 if (KIO::NetAccess::stat(url
, entry
, this)) {
286 // Anything to test here?
293 bool KNetAttach::updateForProtocol(const QString
& protocol
)
296 if (protocol
== "WebFolder") {
297 _useEncryption
->show();
302 _encodingText
->hide();
304 } else if (protocol
== "Fish") {
305 _useEncryption
->hide();
310 } else if (protocol
== "FTP") {
311 _useEncryption
->hide();
316 _encodingText
->show();
318 } else if (protocol
== "SMB") {
319 _useEncryption
->hide();
324 _encodingText
->hide();
334 void KNetAttach::updateFinishButtonText(bool save
)
337 finishButton()->setText(i18n("Save && C&onnect"));
339 finishButton()->setText(i18n("C&onnect"));
343 // vim: ts=8 sw=4 noet
344 #include "knetattach.moc"