Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

ftpsitedlg.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                           ftpsitedlg.cpp  -  description
00003                              -------------------
00004     begin                : Tue Jul 25 2000
00005     copyright            : (C) 2000 -2004 by llornkcor
00006     email                : ljp@llornkcor.com
00007  ***************************************************************************/
00008 /***************************************************************************
00009  *   This program is free software; you can redistribute it and/or modify  *
00010  *   it under the terms of the GNU General Public License as published by  *
00011  *   the Free Software Foundation; either version 2 of the License, or     *
00012  *   (at your option) any later version.                                   *
00013  ***************************************************************************/
00014 
00015 #include "optionsDialog.h"
00016 #include "gutenbrowser.h"
00017 //#include "NetworkDialog.h"
00018 #include "output.h"
00019 
00020 #include <qpe/config.h>
00021 
00022 /* QT */
00023 #include <qprogressbar.h>
00024 #include <qurloperator.h>
00025 #include <qlistbox.h>
00026 
00027 /* STD */
00028 #include <stdlib.h>
00029 #include <unistd.h>
00030 #include <stdio.h>
00031 
00032 // :)~
00033 void optionsDialog::ftpSiteDlg( )
00034 {
00035 //printf( "ftpSiteDlg: ListFile is "+ ListFile +"\n" );
00036 //  initDialog();
00037     local_library = (QDir::homeDirPath ()) +"/Applications/gutenbrowser/";
00038 //    ListFile = local_library + "ftpList";
00039     ListFile = QPEApplication::qpeDir() + "etc/gutenbrowser";
00040     QDir dir(ListFile);
00041     if( !dir.exists())
00042         dir.mkdir(ListFile,true);
00043     ListFile+="/ftpList";
00044     qDebug( "opening "+ListFile );
00045     if ( QFile(ListFile).exists() ) {
00046         openSiteList();
00047     } else {
00048            switch( QMessageBox::warning( this, "Gutenbrowser",
00049                                           "Do you want to  download \nan ftp site list?",
00050                                           QMessageBox::Yes,   QMessageBox::No)) {
00051                     case   QMessageBox::Yes: // yes
00052                         getSite();
00053                     break;
00054                     case   QMessageBox::No: // No
00055                       // exit
00056                     break;
00057             }
00058 
00059     }
00060 }
00061 
00062 
00063 /*
00064 // get ftp list from web- parse it. */
00065 void optionsDialog::getSite()
00066 {
00067     QString file_name;
00068     QString ftp_listFileURL;
00069     QString outputFile;
00070 //    outputFile = local_library+ "list.html";
00071     outputFile = ListFile + "list.html";
00072     QString networkUrl="http://www.gutenberg.net/catalog/world/selectpermanentmirror?fk_books=12962";
00073 //    QString networkUrl= "http://www.gutenberg.org/www/mirror.sites.html";
00074 //http://www.gutenberg.org/index.html";
00075 //       QString networkUrl= "http://llornkcor.com/index.shtml";
00076 //    //  "http://www.gutenberg.org/index.html"
00077 //
00078 //Qhttp stops working at times.... :(
00079 //    NetworkDialog *NetworkDlg;
00080 //    NetworkDlg = new NetworkDialog( this,"Network Protocol Dialog",TRUE,0,networkUrl,outputFile);
00081 //    if( NetworkDlg->exec() != 0 )
00082 //    { // use new, improved, *INSTANT* network-dialog-file-getterer
00083 //        qDebug( "gitcha!" );
00084 //    }
00085 //    delete NetworkDlg;
00086 //#ifdef Q_WS_QWS
00087 
00088 // TODO qprocess here
00089 
00090     QString cmd="wget -T 15 -O " +outputFile + " " + networkUrl;// + " 2>&1" ;
00091  qDebug( "Issuing the command "+cmd );
00092     Output *outDlg;
00093     outDlg = new Output( 0, tr("Downloading ftp sites...."),TRUE);
00094     outDlg->showMaximized();
00095     outDlg->show();
00096     qApp->processEvents();
00097     FILE *fp;
00098     char line[130];
00099     outDlg->OutputEdit->append( tr("Running wget") );
00100     sleep(1);
00101     fp = popen(  (const char *) cmd, "r");
00102     while ( fgets( line, sizeof line, fp)) {
00103         outDlg->OutputEdit->append(line);
00104         outDlg->OutputEdit->setCursorPosition(outDlg->OutputEdit->numLines() + 1,0,FALSE);
00105     }
00106     pclose(fp);
00107     outDlg->close();
00108     if(outDlg)
00109         delete outDlg;
00110 
00111 //    outputFile=ListFile;
00112     ftp_QListBox_1->clear();
00113     parseFtpList( outputFile); // got the html list, now parse it so we can use it
00114 }
00115 
00116 bool optionsDialog::parseFtpList( QString outputFile)
00117 {
00118 // parse ftplist html and extract just the machine names/directories
00119 // TODO: add locations!!
00120     qDebug( "parse ftplist "+outputFile );
00121     QString ftpList, s_location;
00122     QFile f( outputFile );
00123 
00124     if( f.open( IO_ReadWrite )) {
00125         QTextStream t( &f);
00126         QString countryName;
00127         bool b_gotchTest=false;
00128 
00129         while ( !t.eof() ) {
00130             QString s = t.readLine();
00131             int start;
00132             int end;
00133             if( s.find( "Select a Default Download Site - Project Gutenberg", 0, TRUE) !=-1) { //lower end of this file
00134                 b_gotchTest = true;
00135             }
00136             if( b_gotchTest) {
00137                 if(( start = s.find( "ftp://", 0, TRUE))!=-1 ) {
00138 //                    qDebug(s);
00139 //                    qDebug("%d", start);
00140                     end = s.find( "</td></tr>", 0, TRUE);// ==-1)) {
00141                     if( end == -1) {
00142                         end = s.find( "\">");
00143                     }
00144 //                    qDebug("end %d", end);
00145                     start =start + 6;
00146                     ftpSite =  s.mid( start , (end - start)  );
00147                     if(ftpSite.right(1) != "/") {
00148                           //    ftpSite += "/";
00149                     }
00150 //                    qDebug("ftpsite " + ftpSite);
00151 
00152                     for (int j = 0 ; j<3;j++) {
00153                         s = t.readLine();
00154                         QString finder="</td><td rowspan=\"3\">";
00155                         if(( start = s.find( finder, 0, TRUE) ) != -1) {
00156                             //                          qDebug( "%d" + s, start  );
00157                             end = s.find( finder, start + 2, TRUE) ;
00158 //                            qDebug("end %d",end);
00159                             s_location = s.mid( start + finder.length() , (end - start)-finder.length() );
00160 //
00161 //                            qDebug("Location "+s_location  );
00162 
00163                             ftpList += s_location+ "    "+ftpSite+"\n";
00164                             ftp_QListBox_1->sort( TRUE );
00165                             QString winbug="    ";
00166                             ftp_QListBox_1->insertItem ( s_location.latin1() + winbug + ftpSite);
00167                             j=3;
00168                     // ftp_QListBox_1->insertItem ( ftpSite+"  "+s_location.latin1());
00169                         }
00170                     }
00171                     //  ftp_QListBox_1->insertItem ( ftpSite);
00172                 }
00173             } // end find ftp://
00174 
00175         } // end while loop
00176         QFile f2( ListFile);
00177     if(!f2.open( IO_ReadWrite | IO_Truncate))
00178         QMessageBox::message( (tr("Note")), (tr("File not opened sucessfully.\n"+ListFile )) );
00179         f2.writeBlock( ftpList, ftpList.length() );
00180         f.close();
00181         f2.close();
00182        if( f.exists() )
00183         f.remove();
00184     } else {
00185         QMessageBox::message( (tr("ftpSiteDlg")), (tr("List File not opened sucessfully." )) );
00186         return false;
00187     }
00188     setActiveWindow();
00189     return true;
00190 }
00191 
00192 void optionsDialog::openSiteList() {
00193 
00194     qDebug( " just opens the ftp site list" );
00195 //  ListFile = ( QDir::homeDirPath ()) +"/.gutenbrowser/ftpList";
00196     QFile f( ListFile);
00197     if(!f.open( IO_ReadWrite )) {
00198         QMessageBox::message( (tr("Note")), (tr("File not opened sucessfully." )) );
00199     } else {
00200         QTextStream t( &f);
00201         while ( !t.atEnd() ) {
00202             QString ftpSite = t.readLine();
00203             ftp_QListBox_1->sort( TRUE );
00204             ftp_QListBox_1->insertItem ( ftpSite);
00205 
00206         } // end while loop
00207         f.close();
00208     }
00209 
00210     QListBoxItem *itemSel=0;
00211     if( (itemSel=ftp_QListBox_1->findItem( ftp_host)) )
00212         ftp_QListBox_1->setSelected( itemSel, true);
00213 }
00214 
00215 /*
00216 List box clicked */
00217 void optionsDialog::getSelection(  QListBoxItem *item)
00218 {
00219     QString selctionStr;
00220     selctionStr = item->text();
00221 //  selctionStr = ftp_QListBox_1->currentText();
00222 //  printf( selctionStr+"\n" );
00223     select_site( selctionStr );
00224 }
00225 
00226 void optionsDialog::select_site( const char *index )
00227 {
00228 //   ftp://ftp.datacanyon.com/pub/gutenberg/
00229 
00230     s_site = index;
00231     QString s_site2;
00232 //    if(s_site.find("(",0,TRUE))
00233     s_site2=s_site.right( s_site.length()-(s_site.find("    ",0,TRUE)+4)  );
00234 
00235     qDebug( "Selected ftp site is "+ s_site2 );
00236 
00237     int i_ftp = s_site2.find("/", 0, FALSE);
00238     ftp_host = s_site2.left(i_ftp );
00239     ftp_base_dir = s_site2.right( s_site2.length() - i_ftp);
00240 
00241 //    config->read();
00242     Config config("Gutenbrowser");
00243     config.setGroup( "FTPsite" );
00244     config.writeEntry("SiteName",ftp_host);
00245     config.writeEntry("base",ftp_base_dir);
00246 //    config->write();
00247     TextLabel3->setText( "Current ftp server:\n" + ftp_host /*+ ftp_base_dir*/ );
00248 //    optionsDialog::accept();
00249 }
00250 

Generated on Sat Nov 5 16:16:51 2005 for OPIE by  doxygen 1.4.2