2014-11-15 04:16:00 +02:00
/*
Copyright ( C ) 2004 George Staikos < staikos @ kde . org >
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 .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
General Public License for more details .
You should have received a copy of the GNU General Public License
along with this program ; see the file COPYING . If not , write to
the Free Software Foundation , Inc . , 51 Franklin Street , Fifth Floor ,
Boston , MA 02110 - 1301 , USA .
*/
# include "knetattach.h"
# include <QtCore/QVariant>
2015-08-12 21:07:13 +03:00
# include <QtCore/qtextcodec.h>
2014-11-15 04:16:00 +02:00
# include <KIO/NetAccess>
# include <KMessageBox>
# include <KIcon>
# include <KLocale>
# include <KGlobalSettings>
2015-09-01 04:37:19 +03:00
# include <KConfigGroup>
2014-11-15 04:16:00 +02:00
# include <KStandardDirs>
# include <KDirNotify>
# include <KCharsets>
# include <KDebug>
# include <KToolInvocation>
2022-02-16 14:35:33 +02:00
# include <KProtocolInfo>
2014-11-15 04:16:00 +02:00
KNetAttach : : KNetAttach ( QWidget * parent )
: QWizard ( parent ) , Ui_KNetAttach ( )
{
setupUi ( this ) ;
2022-02-16 14:35:33 +02:00
_ftp - > setEnabled ( KProtocolInfo : : isKnownProtocol ( " ftp " ) ) ;
_sftp - > setEnabled ( KProtocolInfo : : isKnownProtocol ( " sftp " ) ) ;
2014-11-15 04:16:00 +02:00
connect ( _recent , SIGNAL ( toggled ( bool ) ) , _recentConnectionName , SLOT ( setEnabled ( bool ) ) ) ;
connect ( _connectionName , SIGNAL ( textChanged ( QString ) ) , this , SLOT ( updateParametersPageStatus ( ) ) ) ;
connect ( _user , SIGNAL ( textChanged ( QString ) ) , this , SLOT ( updateParametersPageStatus ( ) ) ) ;
2023-06-05 14:58:27 +03:00
connect ( _pass , SIGNAL ( textChanged ( QString ) ) , this , SLOT ( updateParametersPageStatus ( ) ) ) ;
2014-11-15 04:16:00 +02:00
connect ( _host , SIGNAL ( textChanged ( QString ) ) , this , SLOT ( updateParametersPageStatus ( ) ) ) ;
connect ( _path , SIGNAL ( textChanged ( QString ) ) , this , SLOT ( updateParametersPageStatus ( ) ) ) ;
connect ( _createIcon , SIGNAL ( toggled ( bool ) ) , this , SLOT ( updateFinishButtonText ( bool ) ) ) ;
2023-06-12 14:13:41 +03:00
connect ( _savePass , SIGNAL ( toggled ( bool ) ) , this , SLOT ( updateSavePasswordBox ( bool ) ) ) ;
2014-11-15 04:16:00 +02:00
connect ( this , SIGNAL ( helpRequested ( ) ) , this , SLOT ( slotHelpClicked ( ) ) ) ;
connect ( this , SIGNAL ( currentIdChanged ( int ) ) , this , SLOT ( slotPageChanged ( int ) ) ) ;
setWindowIcon ( KIcon ( " knetattach " ) ) ;
setOption ( HaveHelpButton , true ) ;
2022-02-16 15:46:26 +02:00
// setResizeMode(Fixed); FIXME: make the wizard fixed-geometry
2014-11-15 04:16:00 +02:00
button ( FinishButton ) - > setEnabled ( false ) ;
KConfig crecent ( " krecentconnections " , KConfig : : NoGlobals ) ;
KConfigGroup recent ( & crecent , " General " ) ;
QStringList idx = recent . readEntry ( " Index " , QStringList ( ) ) ;
if ( idx . isEmpty ( ) ) {
2022-02-16 14:35:33 +02:00
_recent - > setEnabled ( false ) ;
2014-11-15 04:16:00 +02:00
} else {
2022-02-16 14:35:33 +02:00
_recent - > setEnabled ( true ) ;
_recentConnectionName - > addItems ( idx ) ;
2014-11-15 04:16:00 +02:00
}
_encoding - > clear ( ) ;
_encoding - > addItems ( KGlobal : : charsets ( ) - > descriptiveEncodingNames ( ) ) ;
const int codecForLocaleIdx = _encoding - > findText ( QTextCodec : : codecForLocale ( ) - > name ( ) , Qt : : MatchContains ) ;
_encoding - > setCurrentIndex ( codecForLocaleIdx ! = - 1 ? codecForLocaleIdx : 0 ) ;
}
2022-02-16 15:46:26 +02:00
void KNetAttach : : slotPageChanged ( int )
2014-11-15 04:16:00 +02:00
{
2023-06-13 02:35:04 +03:00
// force emision of the toggled() signal
_createIcon - > setChecked ( false ) ;
// and the check state
2023-06-05 20:00:54 +03:00
_createIcon - > setChecked ( true ) ;
2014-11-15 04:16:00 +02:00
}
void KNetAttach : : slotHelpClicked ( )
{
2024-05-07 10:46:00 +03:00
KToolInvocation : : self ( ) - > invokeHelp ( QString ( ) , " knetattach " ) ;
2014-11-15 04:16:00 +02:00
}
2022-02-16 15:46:26 +02:00
void KNetAttach : : setInformationText ( const QString & type )
2014-11-15 04:16:00 +02:00
{
QString text ;
2022-02-16 16:21:45 +02:00
if ( type = = " FTP " ) {
2014-11-15 04:16:00 +02:00
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. " ) ;
2022-02-16 16:21:45 +02:00
} else if ( type = = " SFTP " ) {
text = i18n ( " Enter a name for this <i>SSH File Transfer Protocol</i> as well as a server address, port and folder path to use and press the <b>Save & Connect</b> button. " ) ;
2014-11-15 04:16:00 +02:00
}
_informationText - > setText ( text ) ;
}
void KNetAttach : : updateParametersPageStatus ( )
{
button ( FinishButton ) - > setEnabled (
2022-02-16 14:35:33 +02:00
! _host - > text ( ) . trimmed ( ) . isEmpty ( ) & &
! _path - > text ( ) . trimmed ( ) . isEmpty ( ) & &
! _connectionName - > text ( ) . trimmed ( ) . isEmpty ( )
) ;
2023-06-05 20:00:54 +03:00
updateFinishButtonText ( _createIcon - > isChecked ( ) ) ;
2014-11-15 04:16:00 +02:00
}
bool KNetAttach : : validateCurrentPage ( )
{
if ( currentPage ( ) = = _folderType ) {
2022-02-16 14:35:33 +02:00
_host - > setFocus ( ) ;
_connectionName - > setFocus ( ) ;
2022-02-16 16:21:45 +02:00
if ( _ftp - > isChecked ( ) ) {
2022-02-16 14:35:33 +02:00
setInformationText ( " FTP " ) ;
updateForProtocol ( " FTP " ) ;
_port - > setValue ( 21 ) ;
if ( _path - > text ( ) . isEmpty ( ) ) {
_path - > setText ( " / " ) ;
}
2022-02-16 16:21:45 +02:00
} else if ( _sftp - > isChecked ( ) ) {
setInformationText ( " SFTP " ) ;
updateForProtocol ( " SFTP " ) ;
_port - > setValue ( 22 ) ;
if ( _path - > text ( ) . isEmpty ( ) ) {
_path - > setText ( " / " ) ;
}
2022-02-16 14:35:33 +02:00
} else { //if (_recent->isChecked()) {
KConfig recent ( " krecentconnections " , KConfig : : NoGlobals ) ;
if ( ! recent . hasGroup ( _recentConnectionName - > currentText ( ) ) ) {
KConfigGroup group = recent . group ( " General " ) ;
QStringList idx = group . readEntry ( " Index " , QStringList ( ) ) ;
if ( idx . isEmpty ( ) ) {
_recent - > setEnabled ( false ) ;
} else {
_recent - > setEnabled ( true ) ;
_recentConnectionName - > addItems ( idx ) ;
}
return false ;
}
KConfigGroup group = recent . group ( _recentConnectionName - > currentText ( ) ) ;
_type = group . readEntry ( " Type " ) ;
setInformationText ( _type ) ;
if ( ! updateForProtocol ( _type ) ) {
// FIXME: handle error
}
KUrl u ( group . readEntry ( " URL " ) ) ;
_host - > setText ( u . host ( ) ) ;
2023-06-23 16:55:47 +03:00
_user - > setText ( u . userName ( ) ) ;
2023-06-05 14:58:27 +03:00
_pass - > setText ( u . password ( ) ) ;
2022-02-16 14:35:33 +02:00
_path - > setText ( u . path ( ) ) ;
if ( group . hasKey ( " Port " ) ) {
_port - > setValue ( group . readEntry ( " Port " , 0 ) ) ;
} else {
_port - > setValue ( u . port ( ) ) ;
}
_connectionName - > setText ( _recentConnectionName - > currentText ( ) ) ;
_createIcon - > setChecked ( false ) ;
}
updateParametersPageStatus ( ) ;
} else {
button ( BackButton ) - > setEnabled ( false ) ;
button ( FinishButton ) - > setEnabled ( false ) ;
KUrl url ;
2022-02-16 16:21:45 +02:00
if ( _type = = " FTP " ) {
2023-06-23 16:55:47 +03:00
url . setScheme ( " ftp " ) ;
2022-02-16 14:35:33 +02:00
url . setPort ( _port - > value ( ) ) ;
KConfig config ( " kio_ftprc " ) ;
KConfigGroup cg ( & config , _host - > text ( ) . trimmed ( ) ) ;
cg . writeEntry ( " Charset " , KGlobal : : charsets ( ) - > encodingForName ( _encoding - > currentText ( ) ) ) ;
config . sync ( ) ;
2022-02-16 16:21:45 +02:00
} else if ( _type = = " SFTP " ) {
2023-06-23 16:55:47 +03:00
url . setScheme ( " sftp " ) ;
2022-02-16 16:21:45 +02:00
url . setPort ( _port - > value ( ) ) ;
2022-02-16 14:35:33 +02:00
} else { // recent
}
url . setHost ( _host - > text ( ) . trimmed ( ) ) ;
2023-06-23 16:55:47 +03:00
url . setUserName ( _user - > text ( ) . trimmed ( ) ) ;
2023-06-05 14:58:27 +03:00
url . setPassword ( _pass - > text ( ) . trimmed ( ) ) ;
2022-02-16 14:35:33 +02:00
QString path = _path - > text ( ) . trimmed ( ) ;
// could a relative path really be made absolute by simply prepending a '/' ?
if ( ! path . startsWith ( ' / ' ) ) {
path = QString ( " / " ) + path ;
}
url . setPath ( path ) ;
_folderParameters - > setEnabled ( false ) ;
bool success = doConnectionTest ( url ) ;
_folderParameters - > setEnabled ( true ) ;
if ( ! success ) {
KMessageBox : : sorry ( this , i18n ( " Unable to connect to server. Please check your settings and try again. " ) ) ;
button ( BackButton ) - > setEnabled ( true ) ;
return false ;
}
2024-05-12 03:22:02 +03:00
KToolInvocation : : self ( ) - > startServiceForUrl ( url . url ( ) , this ) ;
2022-02-16 14:35:33 +02:00
QString name = _connectionName - > text ( ) . trimmed ( ) ;
2023-06-12 14:13:41 +03:00
// SECURITY: plain password may be stored
const QString remoteUrl = ( _savePass - > isChecked ( ) ? url . url ( ) : url . prettyUrl ( ) ) ;
2022-02-16 14:35:33 +02:00
if ( _createIcon - > isChecked ( ) ) {
KGlobal : : dirs ( ) - > addResourceType ( " remote_entries " , " data " , " remoteview " ) ;
QString path = KGlobal : : dirs ( ) - > saveLocation ( " remote_entries " ) ;
path + = name + " .desktop " ;
KConfig _desktopFile ( path , KConfig : : SimpleConfig ) ;
KConfigGroup desktopFile ( & _desktopFile , " Desktop Entry " ) ;
desktopFile . writeEntry ( " Icon " , " folder-remote " ) ;
desktopFile . writeEntry ( " Name " , name ) ;
desktopFile . writeEntry ( " Type " , " Link " ) ;
2023-06-12 14:13:41 +03:00
desktopFile . writeEntry ( " URL " , remoteUrl ) ;
2023-06-25 03:40:18 +03:00
desktopFile . writeEntry ( " Charset " , url . queryItemValue ( " charset " ) ) ;
2022-02-16 14:35:33 +02:00
desktopFile . sync ( ) ;
org : : kde : : KDirNotify : : emitFilesAdded ( " remote:/ " ) ;
}
if ( ! name . isEmpty ( ) ) {
KConfig _recent ( " krecentconnections " , KConfig : : NoGlobals ) ;
KConfigGroup recent ( & _recent , " General " ) ;
QStringList idx = recent . readEntry ( " Index " , QStringList ( ) ) ;
_recent . deleteGroup ( name ) ; // erase anything stale
if ( idx . contains ( name ) ) {
idx . removeAll ( name ) ;
idx . prepend ( name ) ;
recent . writeEntry ( " Index " , idx ) ;
} else {
QString last ;
if ( ! idx . isEmpty ( ) ) {
last = idx . last ( ) ;
idx . pop_back ( ) ;
}
idx . prepend ( name ) ;
_recent . deleteGroup ( last ) ;
recent . writeEntry ( " Index " , idx ) ;
}
recent = KConfigGroup ( & _recent , name ) ;
2023-06-12 14:13:41 +03:00
recent . writeEntry ( " URL " , remoteUrl ) ;
2022-06-03 02:03:34 +03:00
if ( _type = = " FTP " | | _type = = " SFTP " ) {
2022-02-16 14:35:33 +02:00
recent . writeEntry ( " Port " , _port - > value ( ) ) ;
}
recent . writeEntry ( " Type " , _type ) ;
recent . sync ( ) ;
}
2014-11-15 04:16:00 +02:00
}
return true ;
}
bool KNetAttach : : doConnectionTest ( const KUrl & url )
{
KIO : : UDSEntry entry ;
if ( KIO : : NetAccess : : stat ( url , entry , this ) ) {
2022-02-16 14:35:33 +02:00
// Anything to test here?
return true ;
2014-11-15 04:16:00 +02:00
}
return false ;
}
bool KNetAttach : : updateForProtocol ( const QString & protocol )
{
_type = protocol ;
2022-02-16 16:21:45 +02:00
if ( protocol = = " FTP " ) {
_portText - > show ( ) ;
_port - > show ( ) ;
_userText - > show ( ) ;
_user - > show ( ) ;
2023-06-05 14:58:27 +03:00
_passText - > show ( ) ;
_pass - > show ( ) ;
2022-02-16 16:21:45 +02:00
_encodingText - > show ( ) ;
_encoding - > show ( ) ;
} else if ( protocol = = " SFTP " ) {
2022-02-16 14:35:33 +02:00
_portText - > show ( ) ;
_port - > show ( ) ;
_userText - > show ( ) ;
_user - > show ( ) ;
2023-06-05 14:58:27 +03:00
_passText - > show ( ) ;
_pass - > show ( ) ;
2022-02-16 14:35:33 +02:00
_encodingText - > hide ( ) ;
_encoding - > hide ( ) ;
2014-11-15 04:16:00 +02:00
} else {
2022-02-16 14:35:33 +02:00
_type = " " ;
return false ;
2014-11-15 04:16:00 +02:00
}
return true ;
}
void KNetAttach : : updateFinishButtonText ( bool save )
{
if ( save ) {
2022-02-16 14:35:33 +02:00
button ( FinishButton ) - > setText ( i18n ( " Save && C&onnect " ) ) ;
2014-11-15 04:16:00 +02:00
} else {
2022-02-16 14:35:33 +02:00
button ( FinishButton ) - > setText ( i18n ( " C&onnect " ) ) ;
2014-11-15 04:16:00 +02:00
}
2023-06-12 14:13:41 +03:00
updateSavePasswordBox ( _savePass - > isChecked ( ) ) ;
}
void KNetAttach : : updateSavePasswordBox ( bool save )
{
2023-06-05 20:00:54 +03:00
if ( save & & ! _pass - > text ( ) . trimmed ( ) . isEmpty ( ) ) {
2023-06-12 14:13:41 +03:00
_savePass - > setIcon ( KIcon ( " dialog-warning " ) ) ;
_savePass - > setToolTip ( i18n ( " The plain password will be stored " ) ) ;
2023-06-05 20:00:54 +03:00
} else {
2023-06-12 14:13:41 +03:00
_savePass - > setIcon ( QIcon ( ) ) ;
_savePass - > setToolTip ( QString ( ) ) ;
2023-06-05 20:00:54 +03:00
}
2014-11-15 04:16:00 +02:00
}
// vim: ts=8 sw=4 noet
2015-02-27 09:28:46 +00:00
# include "moc_knetattach.cpp"