/*************************************************************************** * Copyright (C) 2004 by Johan Maes * * on4qz@telenet.be * * * * 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; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include "ftp.h" #include #include #include #include #include "configdialog.h" #include "utils.h" ftpInterface::ftpInterface() { ftp=NULL; init(); } ftpInterface::~ftpInterface() { destroy(); if(ftp) delete ftp; } void ftpInterface::init() { if(ftp) { destroy(); delete ftp; } ftp = new QFtp( 0); connect( ftp, SIGNAL(commandStarted(int)),SLOT(ftp_commandStarted()) ); connect( ftp, SIGNAL(commandFinished(int,bool)),SLOT(ftp_commandFinished()) ); connect( ftp, SIGNAL(done(bool)),SLOT(ftp_done(bool)) ); connect( ftp, SIGNAL(stateChanged(int)),SLOT(ftp_stateChanged(int)) ); connect( ftp, SIGNAL(listInfo(const QUrlInfo &)),SLOT(ftp_listInfo(const QUrlInfo &)) ); connect( ftp, SIGNAL(rawCommandReply(int, const QString &)),SLOT(ftp_rawCommandReply(int, const QString &)) ); } void ftpInterface::destroy() { if ( ftp->state() != QFtp::Unconnected ) { logfile.add("FTP destroy"); ftp->close(); } } void ftpInterface::uploadFile(QString fileName,QString fixFilename,bool deleteSource) { delSrc=deleteSource; if(!isUnconnected()) return; if ( fileName.isNull() ) return; sourceFn=new QFile(fileName); if ( !sourceFn->open( IO_ReadOnly ) ) { QMessageBox::critical( 0, tr("Upload error"), tr("Can't open file '%1' for reading.").arg(fileName) ); delete sourceFn; sourceFn=NULL; return; } QProgressDialog progress( tr("Uploading file..."),tr("Cancel"), 0, 0,"upload progress dialog", TRUE ); connect( ftp, SIGNAL(dataTransferProgress(int,int)),&progress, SLOT(setProgress(int,int)) ); connect( ftp, SIGNAL(commandFinished(int,bool)), &progress, SLOT(reset()) ); connect( &progress, SIGNAL(cancelled()),ftp, SLOT(abort()) ); QFileInfo fi( fileName ); QFileInfo fin(fixFilename); connectToHost(); ftp->put( sourceFn, fi.fileName()); if(!fixFilename.isEmpty()) { ftp->rename(fi.fileName(),fin.fileName()); } progress.exec(); // ### takes a lot of time!!! } void ftpInterface::connectToHost() { destroy(); ftp->connectToHost(ftpRemoteHost,ftpPort); ftp->login( ftpLogin, ftpPassword ); changePath(ftpRemoteDirectory); } // This slot is connected to the QComboBox::activated() signal of the // remotePath. void ftpInterface::changePath( const QString &newPath ) { ftp->cd( newPath ); } /**************************************************************************** ** ** Slots connected to signals of the QFtp class ** *****************************************************************************/ void ftpInterface::ftp_commandStarted() { if ( ftp->currentCommand() == QFtp::List ) { } } void ftpInterface::ftp_commandFinished() { logfile.add("FTP commandFinished"); if (delSrc) sourceFn->remove(); delete ftp->currentDevice(); } void ftpInterface::ftp_done( bool error ) { if ( error ) { QMessageBox::critical( 0, tr("FTP Error"), ftp->errorString() ); // If we are connected, but not logged in, it is not meaningful to stay // connected to the server since the error is a really fatal one (login // failed). } destroy(); } bool ftpInterface::isUnconnected() { return ftp->state() == QFtp::Unconnected; } void ftpInterface::ftp_stateChanged( int state ) { switch ( (QFtp::State)state ) { case QFtp::Unconnected: logfile.add("FTP Unconnected"); break; case QFtp::HostLookup: logfile.add("FTP Host lookup"); break; case QFtp::Connecting: logfile.add("FTP Connecting"); break; case QFtp::Connected: logfile.add("FTP Connected"); break; case QFtp::LoggedIn: logfile.add("FTP Logged In"); break; case QFtp::Closing: logfile.add("FTP Closing"); break; } } void ftpInterface::ftp_listInfo( const QUrlInfo &) { } void ftpInterface::ftp_rawCommandReply( int code, const QString &text ) { logfile.add("FTP Raw Command Reply: code=%d , %s",code,(const char *) text); }