00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "NetworkDialog.h"
00016 #include "gutenbrowser.h"
00017
00018
00019 #include <opie2/odebug.h>
00020
00021
00022 #include <qprogressbar.h>
00023 #include <qstringlist.h>
00024 #include <qpe/config.h>
00025 #include <qvaluelist.h>
00026 #include <qapplication.h>
00027 #include <qfile.h>
00028 #include <qstringlist.h>
00029 #include <qlabel.h>
00030 #include <qpushbutton.h>
00031 #include <qlayout.h>
00032
00033
00034 #include <unistd.h>
00035 extern "C" {
00036 #include <ftplib.h>
00037 }
00038
00039 QProgressBar* ProgressBar1;
00040 QPushButton* buttonCancel;
00041 static netbuf *conn = NULL;
00042
00043 static int log_progress(netbuf *, int xfered, void *arg) {
00044 int fsz = *(int *)arg;
00045 int pct = (xfered * 100) / fsz;
00046 printf("%3d%%\r", pct);
00047 fflush(stdout);
00048 ProgressBar1->setProgress(xfered);
00049 qApp->processEvents();
00050
00051 return 1;
00052 }
00053
00054 NetworkDialog::NetworkDialog( QWidget* parent, const char* name, bool modal, WFlags fl, const QStringList netL)
00055 : QDialog( parent, name, modal, fl )
00056 {
00057
00058 ftp_host = netL[0];
00059 networkUrl = strUrl = netL[0];
00060
00061 dir = ftp_base_dir = netL[1];
00062 localFileName = netL[2];
00063 s_partialFileName = netL[3];
00064
00065 resize(240,120);
00066
00067 local_library = (QDir::homeDirPath ()) +"/Applications/gutenbrowser/";
00068
00069
00070
00071
00072
00073
00074
00075
00076 if ( !name )
00077 setName( "FTP NetworkDialog" );
00078 setCaption(tr("FTP Download"));
00079
00080 initDialog();
00081 }
00082
00083 NetworkDialog::~NetworkDialog() {
00084 }
00085
00086 void NetworkDialog::initDialog() {
00087
00088 QGridLayout *layout = new QGridLayout( this );
00089 totalBytesDownloaded=0;
00090 warnLabel = new QLabel( this, "TextLabel" );
00091 warnLabel ->setText( tr( "Download should start shortly" ) );
00092 TextLabel3 = new QLabel( this, "TextLabel3" );
00093 TextLabel3->setText( "");
00094 layout->addMultiCellWidget( warnLabel, 0, 0, 0, 5 );
00095 layout->addMultiCellWidget( TextLabel3, 1, 1, 0, 5 );
00096
00097 ProgressBar1 = new QProgressBar( this, "ProgressBar1" );
00098 ProgressBar1->setProgress(0);
00099 layout->addMultiCellWidget( ProgressBar1, 3, 3, 0, 4 );
00100
00101
00102 buttonCancel = new QPushButton( this, "buttonCancel" );
00103 buttonCancel->setText( tr( "&Cancel" ) );
00104 buttonCancel->setAutoDefault( TRUE );
00105 buttonCancel->setDefault( TRUE );
00106 layout->addMultiCellWidget( buttonCancel, 3, 3, 5, 5 );
00107
00108 connect(buttonCancel,SIGNAL(clicked()),this,SLOT(reject()));
00109 QTimer::singleShot( 1000, this, SLOT( doOk() ));
00110
00111 }
00112
00113
00114
00115 bool NetworkDialog::downloadFile( QString networkUrl )
00116 {
00117 int fsz;
00118 warnLabel ->setText( "");
00119 qApp->processEvents();
00120 odebug << "Downloading: " << networkUrl << "" << oendl;
00121 odebug << "Into: " << localFileName << "" << oendl;
00122 if( networkUrl.length() > 5) {
00123 QString ftp_user = "anonymous";
00124 QString ftp_pass = "zaurus@gutenbrowser.com";
00125
00126 if(ftp_host.length() < 2) {
00127 odebug << "Please select an ftp host" << oendl;
00128 successDownload=false;
00129 QMessageBox::message("Note","You need to select an ftp host");
00130 return false;
00131 }
00132 QString msg;
00133 odebug << ftp_host << oendl;
00134 odebug << "Opening ftp connection." << oendl;
00135 warnLabel->setText("connecting to: "+ftp_host );
00136 qApp->processEvents();
00138 if (!FtpConnect( ftp_host.latin1(), &conn)) {
00139 i=0;
00140 successDownload=false;
00141 QMessageBox::message("Note","Unable to connect to\n"+ftp_host);
00142 return false;
00143 }
00144 TextLabel3->setText("Opening ftp connection.");
00145 qApp->processEvents();
00146 if (!FtpLogin( ftp_user.latin1(), ftp_pass.latin1(),conn )) {
00147 FtpQuit(conn);
00148 successDownload=false;
00149 msg.sprintf("Unable to log in\n%s",FtpLastResponse(conn));
00150 QMessageBox::message("Note",msg);
00151 return false;
00152 }
00153 odebug << "Changing directories." << oendl;
00154 TextLabel3->setText("Changing directories.");
00155 qApp->processEvents();
00156 if (!FtpChdir( dir.latin1(), conn )) {
00157 successDownload=false;
00158 msg.sprintf("Unable to change directories "+dir+"\n%s",FtpLastResponse(conn));
00159 QMessageBox::message("Note",msg);
00160 FtpQuit(conn);
00161 return false;
00162 }
00163
00164
00165
00166
00167
00168
00169
00170 QString File_Name;
00171
00172 if( s_partialFileName.right(4) != ".txt") {
00173 TextLabel3->setText("Getting directory list.");
00174 qApp->processEvents();
00175 if (!FtpDir( "./.guten_temp", dir.latin1(), conn) ) {
00176 msg.sprintf("Unable to list the directory\n"+dir+"\n%s",FtpLastResponse(conn));
00177 QMessageBox::message("Note",msg);
00178 successDownload = false;
00179 FtpQuit(conn);
00180 return false;
00181 }
00182 QFile tmp("./.guten_temp");
00183 if (tmp.open(IO_ReadOnly)) {
00184 QTextStream t( &tmp );
00185
00186 QString s;
00187
00188 while ( !t.eof()) {
00189 s = t.readLine();
00190
00191 if (s.contains(s_partialFileName, FALSE)) {
00192 QString str = s.right( (s.length()) - (s.find(s_partialFileName, FALSE)) );
00193
00194 if (str.contains(".txt")) {
00195 File_Name = str;
00196
00197 break;
00198 }
00199 }
00200 }
00201 tmp.close();
00202
00203 }
00204 } else {
00205 File_Name = s_partialFileName;
00206 qDebug("new dir " + File_Name);
00207 }
00208
00209
00210 Config cfg("Gutenbrowser");
00211 cfg.setGroup("General");
00212 QString temp=cfg.readEntry("DownloadDirectory",local_library);
00213
00214 localFileName = temp+File_Name;
00215 odebug << "Requesting file "+ File_Name << oendl;
00216 odebug << "Saving as "+localFileName << oendl;
00217 msg="Requesting file "+ File_Name;
00218 TextLabel3->setText(msg);
00219 qApp->processEvents();
00220 if( File_Name.length()>3) {
00221 if (!FtpSize( File_Name.latin1(), &fsz, FTPLIB_ASCII, conn))
00222 fsz = 0;
00223 QString temp;
00224 temp.sprintf( File_Name+" "+" %dkb", fsz);
00225 TextLabel3->setText(temp);
00226
00227 ProgressBar1->setTotalSteps(fsz);
00228 FtpOptions(FTPLIB_CALLBACK, (long) log_progress, conn);
00229 FtpOptions(FTPLIB_IDLETIME, (long) 1000, conn);
00230 FtpOptions(FTPLIB_CALLBACKARG, (long) &fsz, conn);
00231 FtpOptions(FTPLIB_CALLBACKBYTES, (long) fsz/10, conn);
00232
00233 if (!FtpGet( localFileName.latin1(), File_Name.latin1(), FTPLIB_ASCII, conn)) {
00234 msg.sprintf("Could not download "+ File_Name+"\n%s",FtpLastResponse(conn));
00235 successDownload=false;
00236 QMessageBox::message("Note",msg);
00237 update();
00238 }
00239 } else {
00240 QMessageBox::message("Note","Could not download file "+ File_Name);
00241 successDownload=false;
00242 FtpQuit(conn);
00243 return false;
00244 }
00245 odebug << "Ftp session successful" << oendl;
00246 successDownload=TRUE;
00247 FtpQuit(conn);
00248 return true;
00249 }
00250 return false;
00251 }
00252
00253 void NetworkDialog::doOk() {
00254 owarn << "Do OK" << oendl;
00255 QString loginStr;
00256 loginStr = "gutenbrowser";
00257 if ( !ftp_host.isEmpty() ) {
00258 if( ftp_host.find( "/", ftp_host.length() - 1,TRUE) != -1 && ftp_host.find("ftp://",0, TRUE) != -1) {
00259 TextLabel3->setText( tr( "List remote dir:\n" + ftp_host) );
00260
00261
00262 if( downloadFile(ftp_host))
00263 successDownload = true;
00264 else {
00265 successDownload = false;
00266 reject();
00267 }
00268 } else {
00269 if(downloadFile(ftp_host))
00270 successDownload = true;
00271 else {
00272 successDownload = false;
00273 reject();
00274 }
00275 }
00276 }
00277 accept();
00278 }