summaryrefslogtreecommitdiff
path: root/noncore/apps/opie-gutenbrowser/ftpsitedlg.cpp
blob: 44ade8dabd74e427da7482891c5d0efb7a9e36ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/***************************************************************************
                          ftpsitedlg.cpp  -  description
                             -------------------
    begin                : Tue Jul 25 2000
    copyright            : (C) 2000 -2004 by llornkcor
    email                : ljp@llornkcor.com
 ***************************************************************************/
/***************************************************************************
 *   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.                                   *
 ***************************************************************************/

#include "optionsDialog.h"
#include "gutenbrowser.h"
//#include "NetworkDialog.h"
#include "output.h"

#include <qpe/config.h>

/* QT */
#include <qprogressbar.h>
#include <qurloperator.h>
#include <qlistbox.h>

/* STD */
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>

// :)~
void optionsDialog::ftpSiteDlg( )
{
//printf( "ftpSiteDlg: ListFile is "+ ListFile +"\n" );
//  initDialog();
    local_library = (QDir::homeDirPath ()) +"/Applications/gutenbrowser/";
//    ListFile = local_library + "ftpList";
    ListFile = QPEApplication::qpeDir() + "etc/gutenbrowser";
    QDir dir(ListFile);
    if( !dir.exists())
        dir.mkdir(ListFile,true);
    ListFile+="/ftpList";
    qDebug( "opening "+ListFile );
    if ( QFile(ListFile).exists() ) {
        openSiteList();
    } else {
           switch( QMessageBox::warning( this, "Gutenbrowser",
                                          "Do you want to  download \nan ftp site list?",
                                          QMessageBox::Yes,   QMessageBox::No)) {
                    case   QMessageBox::Yes: // yes
                        getSite();
                    break;
                    case   QMessageBox::No: // No
                      // exit
                    break;
            }

    }
}


/*
// get ftp list from web- parse it. */
void optionsDialog::getSite()
{
    QString file_name;
    QString ftp_listFileURL;
    QString outputFile;
//    outputFile = local_library+ "list.html";
    outputFile = ListFile + "list.html";
    QString networkUrl="http://www.gutenberg.net/catalog/world/selectpermanentmirror?fk_books=12962";
//    QString networkUrl= "http://www.gutenberg.org/www/mirror.sites.html";
//http://www.gutenberg.org/index.html";
//       QString networkUrl= "http://llornkcor.com/index.shtml";
//    //  "http://www.gutenberg.org/index.html"
//
//Qhttp stops working at times.... :(
//    NetworkDialog *NetworkDlg;
//    NetworkDlg = new NetworkDialog( this,"Network Protocol Dialog",TRUE,0,networkUrl,outputFile);
//    if( NetworkDlg->exec() != 0 )
//    { // use new, improved, *INSTANT* network-dialog-file-getterer
//        qDebug( "gitcha!" );
//    }
//    delete NetworkDlg;
//#ifdef Q_WS_QWS

// TODO qprocess here

    QString cmd="wget -T 15 -O " +outputFile + " " + networkUrl;// + " 2>&1" ;
 qDebug( "Issuing the command "+cmd );
    Output *outDlg;
    outDlg = new Output( 0, tr("Downloading ftp sites...."),TRUE);
    outDlg->showMaximized();
    outDlg->show();
    qApp->processEvents();
    FILE *fp;
    char line[130];
    outDlg->OutputEdit->append( tr("Running wget") );
    sleep(1);
    fp = popen(  (const char *) cmd, "r");
    while ( fgets( line, sizeof line, fp)) {
        outDlg->OutputEdit->append(line);
        outDlg->OutputEdit->setCursorPosition(outDlg->OutputEdit->numLines() + 1,0,FALSE);
    }
    pclose(fp);
    outDlg->close();
    if(outDlg)
        delete outDlg;

//    outputFile=ListFile;
    ftp_QListBox_1->clear();
    parseFtpList( outputFile); // got the html list, now parse it so we can use it
}

bool optionsDialog::parseFtpList( QString outputFile)
{
// parse ftplist html and extract just the machine names/directories
// TODO: add locations!!
    qDebug( "parse ftplist "+outputFile );
    QString ftpList, s_location;
    QFile f( outputFile );

    if( f.open( IO_ReadWrite )) {
        QTextStream t( &f);
        QString countryName;
        bool b_gotchTest=false;

        while ( !t.eof() ) {
            QString s = t.readLine();
            int start;
            int end;
            if( s.find( "Select a Default Download Site - Project Gutenberg", 0, TRUE) !=-1) { //lower end of this file
                b_gotchTest = true;
            }
            if( b_gotchTest) {
                if(( start = s.find( "ftp://", 0, TRUE))!=-1 ) {
//                    qDebug(s);
//                    qDebug("%d", start);
                    end = s.find( "</td></tr>", 0, TRUE);// ==-1)) {
                    if( end == -1) {
                        end = s.find( "\">");
                    }
//                    qDebug("end %d", end);
                    start =start + 6;
                    ftpSite =  s.mid( start , (end - start)  );
                    if(ftpSite.right(1) != "/") {
                          //    ftpSite += "/";
                    }
//                    qDebug("ftpsite " + ftpSite);

                    for (int j = 0 ; j<3;j++) {
                        s = t.readLine();
                        QString finder="</td><td rowspan=\"3\">";
                        if(( start = s.find( finder, 0, TRUE) ) != -1) {
                            //                          qDebug( "%d" + s, start  );
                            end = s.find( finder, start + 2, TRUE) ;
//                            qDebug("end %d",end);
                            s_location = s.mid( start + finder.length() , (end - start)-finder.length() );
//
//                            qDebug("Location "+s_location  );

                            ftpList += s_location+ "    "+ftpSite+"\n";
                            ftp_QListBox_1->sort( TRUE );
                            QString winbug="    ";
                            ftp_QListBox_1->insertItem ( s_location.latin1() + winbug + ftpSite);
                            j=3;
                    // ftp_QListBox_1->insertItem ( ftpSite+"  "+s_location.latin1());
                        }
                    }
                    //  ftp_QListBox_1->insertItem ( ftpSite);
                }
            } // end find ftp://

        } // end while loop
        QFile f2( ListFile);
    if(!f2.open( IO_ReadWrite | IO_Truncate))
        QMessageBox::message( (tr("Note")), (tr("File not opened sucessfully.\n"+ListFile )) );
        f2.writeBlock( ftpList, ftpList.length() );
        f.close();
        f2.close();
       if( f.exists() )
        f.remove();
    } else {
        QMessageBox::message( (tr("ftpSiteDlg")), (tr("List File not opened sucessfully." )) );
        return false;
    }
    setActiveWindow();
    return true;
}

void optionsDialog::openSiteList() {

    qDebug( " just opens the ftp site list" );
//  ListFile = ( QDir::homeDirPath ()) +"/.gutenbrowser/ftpList";
    QFile f( ListFile);
    if(!f.open( IO_ReadWrite )) {
        QMessageBox::message( (tr("Note")), (tr("File not opened sucessfully." )) );
    } else {
        QTextStream t( &f);
        while ( !t.atEnd() ) {
            QString ftpSite = t.readLine();
            ftp_QListBox_1->sort( TRUE );
            ftp_QListBox_1->insertItem ( ftpSite);

        } // end while loop
        f.close();
    }

    QListBoxItem *itemSel=0;
    if( (itemSel=ftp_QListBox_1->findItem( ftp_host)) )
        ftp_QListBox_1->setSelected( itemSel, true);
}

/*
List box clicked */
void optionsDialog::getSelection(  QListBoxItem *item)
{
    QString selctionStr;
    selctionStr = item->text();
//  selctionStr = ftp_QListBox_1->currentText();
//  printf( selctionStr+"\n" );
    select_site( selctionStr );
}

void optionsDialog::select_site( const char *index )
{
//   ftp://ftp.datacanyon.com/pub/gutenberg/

    s_site = index;
    QString s_site2;
//    if(s_site.find("(",0,TRUE))
    s_site2=s_site.right( s_site.length()-(s_site.find("    ",0,TRUE)+4)  );

    qDebug( "Selected ftp site is "+ s_site2 );

    int i_ftp = s_site2.find("/", 0, FALSE);
    ftp_host = s_site2.left(i_ftp );
    ftp_base_dir = s_site2.right( s_site2.length() - i_ftp);

//    config->read();
    Config config("Gutenbrowser");
    config.setGroup( "FTPsite" );
    config.writeEntry("SiteName",ftp_host);
    config.writeEntry("base",ftp_base_dir);
//    config->write();
    TextLabel3->setText( "Current ftp server:\n" + ftp_host /*+ ftp_base_dir*/ );
//    optionsDialog::accept();
}