summaryrefslogtreecommitdiff
path: root/noncore/apps/opie-gutenbrowser/ftpsitedlg.cpp
Unidiff
Diffstat (limited to 'noncore/apps/opie-gutenbrowser/ftpsitedlg.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/apps/opie-gutenbrowser/ftpsitedlg.cpp247
1 files changed, 247 insertions, 0 deletions
diff --git a/noncore/apps/opie-gutenbrowser/ftpsitedlg.cpp b/noncore/apps/opie-gutenbrowser/ftpsitedlg.cpp
new file mode 100644
index 0000000..ad770b5
--- a/dev/null
+++ b/noncore/apps/opie-gutenbrowser/ftpsitedlg.cpp
@@ -0,0 +1,247 @@
1/***************************************************************************
2 ftpsitedlg.cpp - description
3 -------------------
4 begin : Tue Jul 25 2000
5 copyright : (C) 2000 -2004 by llornkcor
6 email : ljp@llornkcor.com
7 ***************************************************************************/
8/***************************************************************************
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 ***************************************************************************/
14#include "optionsDialog.h"
15#include "gutenbrowser.h"
16//#include "NetworkDialog.h"
17#include "output.h"
18
19#include <qprogressbar.h>
20#include <stdlib.h>
21#include <qurloperator.h>
22#include <qpe/config.h>
23#include <qlistbox.h>
24
25#include <unistd.h>
26#include <stdio.h>
27#include <stdlib.h>
28
29// :)~
30void optionsDialog::ftpSiteDlg( )
31{
32//printf( "ftpSiteDlg: ListFile is "+ ListFile +"\n" );
33// initDialog();
34 local_library = (QDir::homeDirPath ()) +"/Applications/gutenbrowser/";
35// ListFile = local_library + "ftpList";
36 ListFile = QPEApplication::qpeDir() + "/etc/gutenbrowser";
37 QDir dir(ListFile);
38 if( !dir.exists())
39 dir.mkdir(ListFile,true);
40 ListFile+="/ftpList";
41 qDebug("opening "+ListFile);
42 if ( QFile(ListFile).exists() ) {
43 openSiteList();
44 } else {
45 switch( QMessageBox::warning( this, "Gutenbrowser",
46 "Do you want to download \nan ftp site list?",
47 QMessageBox::Yes, QMessageBox::No)) {
48 case QMessageBox::Yes: // yes
49 getSite();
50 break;
51 case QMessageBox::No: // No
52 // exit
53 break;
54 }
55
56 }
57}
58
59
60/*
61// get ftp list from web- parse it. */
62void optionsDialog::getSite()
63{
64 QString file_name;
65 QString ftp_listFileURL;
66 QString outputFile;
67// outputFile = local_library+ "list.html";
68 outputFile = ListFile + "list.html";
69 QString networkUrl= "http://www.gutenberg.org/www/mirror.sites.html";
70//http://www.gutenberg.org/index.html";
71// QString networkUrl= "http://llornkcor.com/index.shtml";
72// // "http://www.gutenberg.org/index.html"
73//
74//Qhttp stops working at times.... :(
75// NetworkDialog *NetworkDlg;
76// NetworkDlg = new NetworkDialog( this,"Network Protocol Dialog",TRUE,0,networkUrl,outputFile);
77// if( NetworkDlg->exec() != 0 )
78// { // use new, improved, *INSTANT* network-dialog-file-getterer
79// qDebug("gitcha!");
80// }
81// delete NetworkDlg;
82//#ifdef Q_WS_QWS
83
84// TODO qprocess here
85
86 QString cmd="wget -T 15 -O " +outputFile + " " + networkUrl + " 2>&1" ;
87 qDebug("Issuing the command "+cmd);
88 Output *outDlg;
89 outDlg = new Output( 0, tr("Downloading ftp sites...."),TRUE);
90 outDlg->showMaximized();
91 outDlg->show();
92 qApp->processEvents();
93 FILE *fp;
94 char line[130];
95 outDlg->OutputEdit->append( tr("Running wget") );
96 sleep(1);
97 fp = popen( (const char *) cmd, "r");
98 while ( fgets( line, sizeof line, fp)) {
99 outDlg->OutputEdit->append(line);
100 outDlg->OutputEdit->setCursorPosition(outDlg->OutputEdit->numLines() + 1,0,FALSE);
101 }
102 pclose(fp);
103 outDlg->close();
104 if(outDlg)
105 delete outDlg;
106
107// outputFile=ListFile;
108 ftp_QListBox_1->clear();
109 parseFtpList( outputFile); // got the html list, now parse it so we can use it
110}
111
112bool optionsDialog::parseFtpList( QString outputFile)
113{
114// parse ftplist html and extract just the machine names/directories
115// TODO: add locations!!
116 qDebug("parse ftplist "+outputFile);
117 QString ftpList, s_location;
118 QFile f( outputFile );
119
120 if( f.open( IO_ReadWrite )) {
121 QTextStream t( &f);
122 QString countryName;
123 bool b_gotchTest=false;
124
125 while ( !t.eof() ) {
126 QString s = t.readLine();
127 int start;
128 int end;
129 if( s.find( "FTP mirror sites for Project Gutenberg:", 0, TRUE) !=-1) { //lower end of this file
130 b_gotchTest = true;
131 }
132 if( b_gotchTest) {
133 if(( start = s.find( "ftp://", 0, TRUE))!=-1 ) {
134 end = s.find( "/\"", 0, TRUE);// ==-1)) {
135 if( end == -1) {
136 end = s.find( "\">");
137 }
138 ftpSite = s.mid( start, (end - start) );
139 if(ftpSite.right(1) != "/") {
140 // ftpSite += "/";
141 }
142 ftpSite=ftpSite.right( ftpSite.length()-6);
143 if( ftpSite.find("\n", 0, TRUE) )
144 ftpSite.remove( ftpSite.find("\n", 0, TRUE), 1);
145
146 if( ftpSite.find("\"", 0, TRUE) )
147 ftpSite.remove( ftpSite.find("\"", 0, TRUE), 1);
148 if( ftpSite.find("a>", 0, TRUE) )
149 ftpSite.remove( ftpSite.find("a>", 0, TRUE) -2, 4);
150
151
152 s = t.readLine();
153 s = t.readLine();
154 if(( start=s.find("<BR>(", 0, TRUE) ) != -1) {
155// qDebug("%s",s.latin1());
156 end = s.find( ")", 0, TRUE)+1;
157 s_location= s.mid( start+4, (end - start) );
158
159// qDebug("%s", s_location.latin1());
160// ftpList += ftpSite + "\n";
161// ftp_QListBox_1->sort( TRUE );
162 ftpList += s_location+ " "+ftpSite+"\n";
163
164 ftp_QListBox_1->sort( TRUE );
165 QString winbug=" ";
166 ftp_QListBox_1->insertItem ( s_location.latin1() +winbug+ftpSite);
167 // ftp_QListBox_1->insertItem ( ftpSite+" "+s_location.latin1());
168 }
169
170 // ftp_QListBox_1->insertItem ( ftpSite);
171 }
172 } // end find ftp://
173
174 } // end while loop
175 QFile f2( ListFile);
176 if(!f2.open( IO_ReadWrite | IO_Truncate))
177 QMessageBox::message( (tr("Note")), (tr("File not opened sucessfully.\n"+ListFile )) );
178 f2.writeBlock( ftpList, ftpList.length() );
179 f.close();
180 f2.close();
181 if( f.exists() )
182 f.remove();
183 } else {
184 QMessageBox::message( (tr("ftpSiteDlg")), (tr("List File not opened sucessfully." )) );
185 return false;
186 }
187 setActiveWindow();
188 return true;
189}
190
191void optionsDialog::openSiteList() {
192
193 qDebug(" just opens the ftp site list");
194// ListFile = ( QDir::homeDirPath ()) +"/.gutenbrowser/ftpList";
195 QFile f( ListFile);
196 if(!f.open( IO_ReadWrite )) {
197 QMessageBox::message( (tr("Note")), (tr("File not opened sucessfully." )) );
198 } else {
199 QTextStream t( &f);
200 while ( !t.atEnd() ) {
201 QString ftpSite = t.readLine();
202 ftp_QListBox_1->sort( TRUE );
203 ftp_QListBox_1->insertItem ( ftpSite);
204
205 } // end while loop
206 f.close();
207 }
208
209 QListBoxItem *itemSel=0;
210 if( (itemSel=ftp_QListBox_1->findItem( ftp_host)) )
211 ftp_QListBox_1->setSelected( itemSel, true);
212}
213
214/*
215List box clicked */
216void optionsDialog::getSelection( QListBoxItem *item)
217{
218 QString selctionStr;
219 selctionStr = item->text();
220// selctionStr = ftp_QListBox_1->currentText();
221// printf( selctionStr+"\n" );
222 select_site( selctionStr );
223}
224
225void optionsDialog::select_site( const char *index )
226{
227// ftp://ftp.datacanyon.com/pub/gutenberg/
228
229 s_site = index;
230 QString s_site2;
231// if(s_site.find("(",0,TRUE))
232 s_site2=s_site.right( s_site.length()-(s_site.find(" ",0,TRUE)+4) );
233qDebug("Selected ftp site is "+ s_site2);
234 int i_ftp = s_site2.find("/", 0, FALSE);
235 ftp_host = s_site2.left(i_ftp );
236 ftp_base_dir = s_site2.right( s_site2.length() - i_ftp);
237
238// config->read();
239 Config config("Gutenbrowser");
240 config.setGroup( "FTPsite" );
241 config.writeEntry("SiteName",ftp_host);
242 config.writeEntry("base",ftp_base_dir);
243// config->write();
244 TextLabel3->setText( "Current ftp server:\n"+ftp_host /*+ ftp_base_dir*/ );
245// optionsDialog::accept();
246}
247