00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "optionsDialog.h"
00016 #include "gutenbrowser.h"
00017
00018 #include "output.h"
00019
00020 #include <qpe/config.h>
00021
00022
00023 #include <qprogressbar.h>
00024 #include <qurloperator.h>
00025 #include <qlistbox.h>
00026
00027
00028 #include <stdlib.h>
00029 #include <unistd.h>
00030 #include <stdio.h>
00031
00032
00033 void optionsDialog::ftpSiteDlg( )
00034 {
00035
00036
00037 local_library = (QDir::homeDirPath ()) +"/Applications/gutenbrowser/";
00038
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:
00052 getSite();
00053 break;
00054 case QMessageBox::No:
00055
00056 break;
00057 }
00058
00059 }
00060 }
00061
00062
00063
00064
00065 void optionsDialog::getSite()
00066 {
00067 QString file_name;
00068 QString ftp_listFileURL;
00069 QString outputFile;
00070
00071 outputFile = ListFile + "list.html";
00072 QString networkUrl="http://www.gutenberg.net/catalog/world/selectpermanentmirror?fk_books=12962";
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090 QString cmd="wget -T 15 -O " +outputFile + " " + networkUrl;
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
00112 ftp_QListBox_1->clear();
00113 parseFtpList( outputFile);
00114 }
00115
00116 bool optionsDialog::parseFtpList( QString outputFile)
00117 {
00118
00119
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) {
00134 b_gotchTest = true;
00135 }
00136 if( b_gotchTest) {
00137 if(( start = s.find( "ftp://", 0, TRUE))!=-1 ) {
00138
00139
00140 end = s.find( "</td></tr>", 0, TRUE);
00141 if( end == -1) {
00142 end = s.find( "\">");
00143 }
00144
00145 start =start + 6;
00146 ftpSite = s.mid( start , (end - start) );
00147 if(ftpSite.right(1) != "/") {
00148
00149 }
00150
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
00157 end = s.find( finder, start + 2, TRUE) ;
00158
00159 s_location = s.mid( start + finder.length() , (end - start)-finder.length() );
00160
00161
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
00169 }
00170 }
00171
00172 }
00173 }
00174
00175 }
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
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 }
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
00217 void optionsDialog::getSelection( QListBoxItem *item)
00218 {
00219 QString selctionStr;
00220 selctionStr = item->text();
00221
00222
00223 select_site( selctionStr );
00224 }
00225
00226 void optionsDialog::select_site( const char *index )
00227 {
00228
00229
00230 s_site = index;
00231 QString s_site2;
00232
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
00242 Config config("Gutenbrowser");
00243 config.setGroup( "FTPsite" );
00244 config.writeEntry("SiteName",ftp_host);
00245 config.writeEntry("base",ftp_base_dir);
00246
00247 TextLabel3->setText( "Current ftp server:\n" + ftp_host );
00248
00249 }
00250