summaryrefslogtreecommitdiff
path: root/noncore/apps/opie-gutenbrowser/NetworkDialog.cpp
blob: ba583ad5f6f76ca1a3dcd46d475ef56e298d0eb0 (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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// /***************************************************************************
//                           NetworkDialog.cpp  -  description
//     begin                : Sun Aug 27 2000
//     copyright            : (C) 2000 - 2004 by L.J. Potter
//     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.                                   *
//  ***************************************************************************/
// // half-assed attempt at providing a network dialog.
// /* Created: Sun Aug 27 15:24:52 2000*/


#include "NetworkDialog.h"
#include "gutenbrowser.h"

/* OPIE */
#include <opie2/odebug.h>

/* QT */
#include <qprogressbar.h>
#include <qstringlist.h>
#include <qpe/config.h>
#include <qvaluelist.h>
#include <qapplication.h>
#include <qfile.h>
#include <qstringlist.h>
#include <qlabel.h>
#include <qpushbutton.h>
#include <qlayout.h>

/* STD */
#include <unistd.h>
extern "C" {
#include <ftplib.h>
}

QProgressBar* ProgressBar1;
QPushButton* buttonCancel;
static netbuf *conn = NULL;

static int log_progress(netbuf *, int xfered, void *arg) {
	int fsz = *(int *)arg;
	int pct = (xfered * 100) / fsz;
	printf("%3d%%\r", pct);
	fflush(stdout);
	ProgressBar1->setProgress(xfered);
	qApp->processEvents();

	return 1;
}

NetworkDialog::NetworkDialog( QWidget* parent,  const char* name, bool modal, WFlags fl, const QStringList netL)
		: QDialog( parent, name, modal, fl )
{

	ftp_host = netL[0];
	networkUrl = strUrl =  netL[0];

	dir = ftp_base_dir = netL[1];
	localFileName = netL[2];
	s_partialFileName = netL[3];

	resize(240,120);

	local_library = (QDir::homeDirPath ()) +"/Applications/gutenbrowser/";
//      autoOk = autoDownload;
//      if( networkUrl.find("ftp",0,false)== -1 ) {
//         if ( !name )
//              setName( "HTTP NetworkDialog" );
//          setCaption( tr( "HTTP Download  ) );
//      qInitNetworkProtocols();  // registers ftp protocol // for now
//      QNetworkProtocol::registerNetworkProtocol( "http", new QNetworkProtocolFactory<Http> );
//      } else {
	if ( !name )
		setName( "FTP NetworkDialog" );
	setCaption(tr("FTP Download"));
//    }
	initDialog(); //opens file to be written
}

NetworkDialog::~NetworkDialog() {
}

void NetworkDialog::initDialog() {

	QGridLayout *layout = new QGridLayout( this );
	totalBytesDownloaded=0;
	warnLabel = new QLabel( this, "TextLabel" );
	warnLabel ->setText( tr( "Download should start shortly" ) );
	TextLabel3 = new QLabel( this, "TextLabel3" );
	TextLabel3->setText( "");
	layout->addMultiCellWidget( warnLabel, 0, 0, 0, 5 );
	layout->addMultiCellWidget( TextLabel3, 1, 1, 0, 5 );

	ProgressBar1 = new QProgressBar(  this, "ProgressBar1" );
	ProgressBar1->setProgress(0);
	layout->addMultiCellWidget( ProgressBar1, 3, 3, 0, 4 );

		//  hbox->addStretch(1);
	buttonCancel = new QPushButton( this, "buttonCancel" );
	buttonCancel->setText( tr( "&Cancel"  ) );
	buttonCancel->setAutoDefault( TRUE );
	buttonCancel->setDefault( TRUE );
	layout->addMultiCellWidget( buttonCancel, 3, 3, 5, 5 );

	connect(buttonCancel,SIGNAL(clicked()),this,SLOT(reject()));
	QTimer::singleShot( 1000, this, SLOT(  doOk()  ));

}

/*
	downloads the file networkUrl */
bool NetworkDialog::downloadFile( QString networkUrl )
{
	int fsz;
	warnLabel ->setText( "");
	qApp->processEvents();
	odebug << "Downloading: " << networkUrl << "" << oendl;
	odebug << "Into: " << localFileName << "" << oendl;
	if( networkUrl.length() > 5) {
		QString ftp_user = "anonymous";
		QString ftp_pass = "zaurus@gutenbrowser.com";
//    ftp_host= networkUrl.mid(networkUrl.find("ftp://",0, TRUE),
		if(ftp_host.length() < 2) {
			odebug << "Please select an ftp host" << oendl;
			successDownload=false;
			QMessageBox::message("Note","You need to select an ftp host");
			return false;
		}
		QString msg;
		odebug << ftp_host << oendl;
		odebug << "Opening ftp connection." << oendl;
		warnLabel->setText("connecting to: "+ftp_host );
		qApp->processEvents();
			/////////// Open FTP connection
			if (!FtpConnect( ftp_host.latin1(), &conn)) {
				i=0;
				successDownload=false;
				QMessageBox::message("Note","Unable to connect to\n"+ftp_host);
				return false;
			}
			TextLabel3->setText("Opening ftp connection.");
			qApp->processEvents();
			if (!FtpLogin( ftp_user.latin1(), ftp_pass.latin1(),conn )) {
				FtpQuit(conn);
				successDownload=false;
				msg.sprintf("Unable to log in\n%s",FtpLastResponse(conn));
				QMessageBox::message("Note",msg);
				return  false;
			}
			odebug << "Changing directories." << oendl;
			TextLabel3->setText("Changing directories.");
			qApp->processEvents();
			if (!FtpChdir( dir.latin1(), conn )) {
				successDownload=false;
				msg.sprintf("Unable to change directories "+dir+"\n%s",FtpLastResponse(conn));
				QMessageBox::message("Note",msg);
				FtpQuit(conn);
				return false;
			}
//      if (!FtpNlst( "./.guten_temp", dir, conn)) {
//              successDownload=false;
//              msg.sprintf("Unable to change directories "+dir+"\n%s",FtpLastResponse(conn));
//              QMessageBox::message("Note",msg);
//              FtpQuit(conn);
//              return false;
//      }
			QString File_Name;

			if( s_partialFileName.right(4) != ".txt") {
					TextLabel3->setText("Getting directory list.");
					qApp->processEvents();
					if (!FtpDir( "./.guten_temp", dir.latin1(), conn) ) {
							msg.sprintf("Unable to list the directory\n"+dir+"\n%s",FtpLastResponse(conn));
							QMessageBox::message("Note",msg);
							successDownload = false;
							FtpQuit(conn);
							return false;
					}
					QFile tmp("./.guten_temp");
					if (tmp.open(IO_ReadOnly)) {
							QTextStream t( &tmp );   // use a text stream
								//odebug << "Finding partial filename "+s_partialFileName << oendl;
							QString s;

							while ( !t.eof()) {
									s = t.readLine();

									if (s.contains(s_partialFileName, FALSE)) {
											QString str = s.right( (s.length()) - (s.find(s_partialFileName, FALSE)) );

											if (str.contains(".txt")) {
													File_Name = str;
														//odebug << "Found file_name "+ File_Name << oendl;
													break;
											}
									}
							}  //end of while loop
							tmp.close();
//        tmp.remove(); ///TODO this is for release version Zaurus
					}
			} else {
					File_Name =  s_partialFileName;
					qDebug("new dir " + File_Name);
			}
			

			Config cfg("Gutenbrowser");
			cfg.setGroup("General");
			QString temp=cfg.readEntry("DownloadDirectory",local_library);

			localFileName = temp+File_Name;
			odebug << "Requesting file "+ File_Name << oendl;
			odebug << "Saving as "+localFileName << oendl;
			msg="Requesting file "+ File_Name;
			TextLabel3->setText(msg);
			qApp->processEvents();
			if( File_Name.length()>3) {
				if (!FtpSize( File_Name.latin1(), &fsz, FTPLIB_ASCII, conn))
					fsz = 0;
				QString temp;
				temp.sprintf( File_Name+" "+" %dkb", fsz);
				TextLabel3->setText(temp);

				ProgressBar1->setTotalSteps(fsz);
				FtpOptions(FTPLIB_CALLBACK, (long) log_progress, conn);
				FtpOptions(FTPLIB_IDLETIME, (long) 1000, conn);
				FtpOptions(FTPLIB_CALLBACKARG, (long) &fsz, conn);
				FtpOptions(FTPLIB_CALLBACKBYTES, (long) fsz/10, conn);

				if (!FtpGet( localFileName.latin1(), File_Name.latin1(), FTPLIB_ASCII, conn)) {
					msg.sprintf("Could not download "+ File_Name+"\n%s",FtpLastResponse(conn));
					successDownload=false;
					QMessageBox::message("Note",msg);
					update();
				}
			} else {
				QMessageBox::message("Note","Could not download file "+ File_Name);
				successDownload=false;
				FtpQuit(conn);
				return false;
			}
			odebug << "Ftp session successful" << oendl;
			successDownload=TRUE;
			FtpQuit(conn);
			return true;
	} //no network url
	return false;
}

void NetworkDialog::doOk() {
	owarn << "Do OK" << oendl;
	QString loginStr;
	loginStr = "gutenbrowser";
	if ( !ftp_host.isEmpty() )   {
		if( ftp_host.find( "/", ftp_host.length() - 1,TRUE) != -1  &&  ftp_host.find("ftp://",0, TRUE) != -1) {
			TextLabel3->setText( tr( "List remote dir:\n" + ftp_host) );
//    TextLabel2->setText( tr( "local file to download into: " +localFileName ) );

			if( downloadFile(ftp_host))
				successDownload = true;
			else {
				successDownload = false;
				reject();
			}
		} else {
			if(downloadFile(ftp_host))
				successDownload = true;
			else {
				successDownload = false;
				reject();
			}
		}
	}
	accept();
}