summaryrefslogtreecommitdiff
path: root/noncore/apps/opie-gutenbrowser/openetext.cpp
Unidiff
Diffstat (limited to 'noncore/apps/opie-gutenbrowser/openetext.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/apps/opie-gutenbrowser/openetext.cpp517
1 files changed, 517 insertions, 0 deletions
diff --git a/noncore/apps/opie-gutenbrowser/openetext.cpp b/noncore/apps/opie-gutenbrowser/openetext.cpp
new file mode 100644
index 0000000..40f35d6
--- a/dev/null
+++ b/noncore/apps/opie-gutenbrowser/openetext.cpp
@@ -0,0 +1,517 @@
1/***************************************************************************
2 openetext.cpp - description
3 begin : Sat Dec 4 1999
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
15#include "gutenbrowser.h"
16#include "openetext.h"
17#include "editTitle.h"
18
19#include <qfileinfo.h>
20#include <qpe/applnk.h>
21#include <qpe/qpeapplication.h>
22
23#include <stdlib.h>
24#include <qtextstream.h>
25#include <qpe/config.h>
26
27#include <opie/ofiledialog.h>
28
29#if defined(_WS_WIN_)
30#include <windows.h>
31#else
32#include <unistd.h>
33#include "sys/stat.h"
34#endif
35
36OpenEtext::OpenEtext(QWidget *parent, QString name) : QDialog(parent,name,true)
37{
38 local_library = (QDir::homeDirPath ()) +"/Applications/gutenbrowser/";
39 local_index=local_library+"GUTINDEX.ALL";
40 initDialog();
41 connect( QListBox_1, SIGNAL(selected ( int)),this, SLOT(select_title(int)) );
42
43 getTitles();
44}
45
46OpenEtext::~OpenEtext()
47{
48}
49
50void OpenEtext::getTitles()
51{
52 QListBox_1->clear();
53 Config config("Gutenbrowser");
54 config.setGroup( "Files" );
55 s_numofFiles = config.readEntry("NumberOfFiles", "0");
56 QLabel_1->setText( tr(
57 s_numofFiles+" etexts in your library."));
58 int i_numofFiles = s_numofFiles.toInt();
59 for (int i = 1; i <= i_numofFiles+1; i++) {
60 QString temp;
61 temp.setNum(i);
62 config.setGroup( "Files" );
63 QString ramble = config.readEntry(temp, "" );
64 config.setGroup( "Titles" );
65 title = config.readEntry(ramble, "");
66 if( !title.isEmpty()) {
67 QListBox_1->insertItem ( QPixmap( QPEApplication::qpeDir()+"pics/gutenbrowser/gutenbrowser_sm.png"), title, -1);
68 }
69 }
70}
71
72/*
73 opens selected title */
74void OpenEtext::OpenTitle()
75{
76 if( QListBox_1->currentItem() != -1)
77 select_title( QListBox_1->currentItem() );
78 else {
79 QString sMsg;
80 sMsg = "You must select an Etext to open";
81 QMessageBox::message( "Error",sMsg);
82 }
83}
84
85void OpenEtext::select_title(int index )
86{
87 OpenButton->setDown(TRUE);
88 QString temp;
89 temp.setNum( index + 1);
90 const char * title;
91 title = QListBox_1->text( index );
92 openFileTitle = title;
93 Config config("Gutenbrowser");
94 config.setGroup( "Files" );
95 file = config.readEntry(temp, "");
96 OpenEtext::accept();
97}
98
99/*
100 This function imports already existing etexts into the local library list*/
101void OpenEtext::open()
102{
103OpenFileButton->setDown(TRUE);
104 title = "";
105 QString currDir="";
106 QString filer;
107 QString name_file;
108 QString local ;
109 local = (QDir::homeDirPath () +"Applications/gutenbrowser");
110 QDir library( local);
111
112// fileBrowser *browseForFiles;
113// browseForFiles=new fileBrowser(this,"Browse for File", TRUE, 0, "guten/plain;text/plain");
114// // browseForFiles=new fileBrowser(this,"fileBrowser",TRUE,0, "*");
115// browseForFiles->setFileView( 0);
116// browseForFiles->showMaximized();
117// browseForFiles->exec();
118// QString selFile= browseForFiles->selectedFileName;
119// fileList=browseForFiles->fileList;
120 Config cfg("Gutenbrowser");
121 cfg. setGroup ( "View" );
122 QString dir = cfg.readEntry("LastOpenDirectory", QPEApplication::documentDir());
123 QMap<QString, QStringList> map;
124 map.insert(tr("All"), QStringList() );
125 QStringList text;
126 text << "text/*";
127 map.insert(tr("Text"), text );
128 text << "*";
129 map.insert(tr("All"), text );
130
131 QString str = OFileDialog::getOpenFileName( 2, dir , QString::null, map);
132
133 if( !str.isEmpty() && QFile(str).exists() && !QFileInfo(str).isDir() ) {
134 filer = str;
135
136 qDebug("Open file: "+str);
137
138 // QStringList::ConstIterator f;
139// QString fileTemp;
140// for ( f = fileList.begin(); f != fileList.end(); f++ ) {
141// fileTemp = *f;
142// fileTemp.right( fileTemp.length()-5);
143// fileName = fileTemp;
144// if( !fileName.isEmpty() ){
145// filer = fileName;
146// } else { //filename is empty
147// // QString sMsg;
148// // sMsg = "Error opening library filelist "+fileName;
149// }
150
151 if( filer.right(4) == ".txt"
152 || filer.right(4) == ".TXT"
153 || filer.right(4) == ".etx"
154 || filer.right(4) == ".ETX"
155 || filer.right(4) == ".etx"
156 || filer.right(4) == ".ETX"
157 || filer.right(4) == ".zip"
158 || filer.right(4) == ".ZIP" ) {
159 QFileInfo zipFile( filer);
160 QString s_fileName = zipFile.fileName();
161 QString cmd;
162
163 if( s_fileName.right(4) == ".zip") { // unzip that sucker....
164 s_fileName = s_fileName.left( s_fileName.length() - 4);
165 if( chdir((const char*)local_library.latin1())!=0)
166 qDebug("chdir failed.");// QString cmd = "gunzip -d " + filer + " -d " + local_library;
167 cmd = "gunzip -S .zip " + filer;
168 fileName = local_library + s_fileName + ".txt";
169 system( cmd);
170 }
171// this renames the .txt to .etx!!
172 else /*if( s_fileName.right(4) == ".txt" || if( s_fileName.right(4) == ".TXT"))*/ {
173// qDebug("Filename is "+fileName);
174 s_fileName = fileName;
175 s_fileName.replace( s_fileName.length()-3,3,"gtn");// s_fileName.replace( s_fileName.length()-3,3,"etx");
176 rename(fileName.latin1(),s_fileName.latin1());
177 fileName = s_fileName;
178// qDebug("Filename is now "+fileName);
179 }
180 } else
181 fileName = str;
182
183 FindTitle(fileName);
184
185 QFileInfo fi( fileName);
186 name_file = fi.fileName();
187 name_file = name_file.left(name_file.length() - 4);
188
189 qDebug("Setting doclink");
190 DocLnk lnk;
191 qDebug("name is " + name_file);
192 lnk.setName(name_file); //sets file name
193 qDebug("Title is "+title);
194 lnk.setComment(title);
195
196 QListBox_1->clear();
197 getTitles();
198 qDebug("Filename is "+fileName);
199 lnk.setFile(fileName); //sets File property
200 lnk.setType("guten/plain");// hey is this a REGISTERED mime type?!?!? ;D
201 lnk.setExec(fileName);
202 lnk.setIcon("gutenbrowser/Gutenbrowser");
203 if(!lnk.writeLink())
204 qDebug("Writing doclink did not work");
205
206 } // end of for each file name....
207 OpenFileButton->setDown(FALSE);
208}
209
210/*
211find the title in the config file */
212bool OpenEtext::FindTitle( QString filename)
213{
214 Config config("Gutenbrowser");
215 config.setGroup( "Files" );
216 QString s_numofFiles = config.readEntry("NumberOfFiles", "0");
217 int i_numofFiles = s_numofFiles.toInt();
218
219 QFileInfo fi(filename);
220 name = fi.fileName();
221 qDebug("filename to open is " + name);
222 QFile indexLib( filename);
223 bool findCheck = FALSE;
224 // int Titlenumber=0;
225
226 if ( indexLib.open( IO_ReadOnly) ) {
227 qDebug("file opened successfully");
228 QTextStream indexStream( &indexLib );
229 QString target = "Project Gutenberg Etext of";
230 QString target2 = "Project Gutenberg Etext";
231 QString target3 = "Project Gutenberg's Etext of";
232 QString target4 = "Project Gutenberg's Etext";
233 QString indexLine;
234 while ( !indexStream.eof() ) {
235 // until end of file..
236 indexLine = indexStream.readLine();
237 if( indexLine.find(target, 0, TRUE) > -1 && !findCheck) {findCheck = TRUE;
238 title = indexLine.mid( indexLine.find(target, 0, TRUE) + (target.length()) , indexLine.find("\r", 0, TRUE));
239 title = title.left( title.find( "*",0, TRUE));
240 title = title.stripWhiteSpace ();
241// qDebug("Found the title 1 and it is %s", title.latin1());
242// QListBox_1->insertItem ( title);
243 }
244 if( indexLine.find( target2, 0, TRUE) > -1 && !findCheck) {
245 findCheck = TRUE;
246 title = indexLine.mid( indexLine.find( target2, 0, TRUE ) + ( target2.length()) , indexLine.find("\r", 0, TRUE) );
247 title = title.left( title.find( "*",0, TRUE));
248 title = title.stripWhiteSpace ();
249// qDebug("Found the title 2 and it is %s", title.latin1());
250// QListBox_1->insertItem ( title);
251 }
252 if( indexLine.find( target3, 0, TRUE) > -1 && !findCheck) {
253 findCheck = TRUE;
254 title = indexLine.mid( indexLine.find( target3, 0, TRUE) + ( target3.length()) , indexLine.find("\r", 0, TRUE));
255 title = title.left( title.find( "*",0, TRUE));
256 title = title.stripWhiteSpace ();
257// qDebug("Found the title 3 and it is %s", title.latin1());
258 }
259 if( indexLine.find( target4, 0, TRUE) > -1 && !findCheck) {
260 findCheck = TRUE;
261 title = indexLine.mid( indexLine.find( target4, 0, TRUE) + ( target4.length()) , indexLine.find("\r", 0, TRUE));
262 title = title.left( title.find( "*",0, TRUE));
263 title = title.stripWhiteSpace ();
264// qDebug("Found the title 4 and it is %s", title.latin1());
265 }
266 } //endof file
267 indexLib.close();
268
269 if( !findCheck || title.length() < 2) {
270 qDebug("Trying hard to find title from GUTINDEX.ALL");
271 title = titleFromLibrary( filename);
272 findCheck = TRUE;
273 }
274
275 if ( checkConf() == false && findCheck == TRUE) {
276
277 config.setGroup( "Files");
278 config.writeEntry("NumberOfFiles",i_numofFiles + 1 );
279 QString interger;
280 interger.setNum( i_numofFiles +1);
281 config.writeEntry(interger, filename);
282 config.setGroup( "Titles" );
283 for (int i = 1; i <= i_numofFiles; i++) { //find dup titles in config file
284 QString temp;
285 temp.setNum(i);
286 config.setGroup( "Files" );
287 QString ramble = config.readEntry(temp, "" );
288 config.setGroup( "Titles" );
289 QString title1 = config.readEntry(ramble, "");
290 if(title1==title)
291 title+="1";
292 i= i_numofFiles+1;
293 }
294 if(title.length()<3) {
295// qDebug("title is empty");
296 title="Unknown";
297 }
298 config.writeEntry( filename,title);
299 }
300 } else {
301 QString sMsg;
302 sMsg = "Error opening library file: "+filename;
303 printf( sMsg+"\n");
304 }
305 return true;
306}
307
308QString OpenEtext::titleFromLibrary( QString fileName)
309{
310 QFile indexLib( local_index);
311 QString target;
312 int find1 = fileName.findRev("/",-1,TRUE) + 1;
313 int find2 = fileName.findRev(".gtn",-1,TRUE) - find1;
314 if(find2==-1-find1)
315 int find2 = fileName.findRev(".etx",-1,TRUE) - find1;
316 else if(find2==-1-find1)
317 int find2 = fileName.findRev(".txt",-1,TRUE) - find1;
318
319 target = fileName.mid( find1, find2 );
320 QString checker = target.right(1);
321 bool ok;
322 checker.toInt( &ok,10);
323 if( ok) {
324 target = target.left( target.length()-1);
325 checker = target.right(1);
326 ok = FALSE;
327 checker.toInt( &ok,10);
328 if( ok) {
329 target = target.left( target.length()-1);
330 }
331 }
332 if ( indexLib.open( IO_ReadOnly) ) {
333 // file opened successfully
334 QTextStream indexStream( &indexLib );
335 QString indexLine;
336 bool findCheck = FALSE;
337 while ( !indexStream.atEnd() ) { // until end of file..
338 indexLine = indexStream.readLine();
339
340 if( indexLine.find( target,0,FALSE) > 0 ) {
341 findCheck = TRUE;
342 title = indexLine.mid( 9, 50);
343// title = indexLine.mid( 26, indexLine.length() );
344 title = title.stripWhiteSpace ();
345// qDebug("Finally Found the title and it is\n %s", title.latin1());
346// QListBox_1->insertItem ( title);
347 }
348 } //end while loop
349 }
350 else
351 qDebug("Error opening library index "+ local_index);
352 return title;
353}
354
355bool OpenEtext::checkConf()
356{
357 QString file = fileName;
358 Config config("Gutenbrowser");
359 config.setGroup( "Files" );
360 QString s_numofFiles = config.readEntry("NumberOfFiles", "0" );
361 int i_numofFiles = s_numofFiles.toInt();
362
363 for (int i = 1; i <= i_numofFiles; i++) {
364 QString temp;
365 temp.setNum(i);
366 config.setGroup( "Files");
367 QString ramble = config.readEntry(temp, "" );
368
369 if(ramble == file ) {
370 return true;
371 }
372 }
373 return false;
374}
375
376void OpenEtext::remove()
377{
378 title_text = QListBox_1->text( QListBox_1->currentItem() );
379 title_text=title_text.stripWhiteSpace();
380 QString msg ="<p>Do you really want to REMOVE\n" +title_text +"?\nThis will not delete the file.</P>";
381 switch( QMessageBox::information( this, (tr("Remove Etext")),
382 (tr(msg)),
383 (tr("&Yes")), (tr("&Cancel")), 0 )){
384 case 0: // Yes clicked,
385 removeSelection();
386 QListBox_1->clear();
387 getTitles();
388 break;
389 case 1: // Cancel
390 break;
391 };
392}
393
394/*
395 this removes selected title entry*/
396void OpenEtext::removeSelection()
397{
398 Config config("Gutenbrowser");
399 config.setGroup( "Files" );
400 s_numofFiles = config.readEntry("NumberOfFiles", "0");
401 int rem=0;
402 int i_numofFiles = s_numofFiles.toInt();
403 QString fileNum2;
404 QString fileNum;
405
406 for (int i = 1; i <= i_numofFiles; i++) {
407 fileNum.setNum(i);
408 config.setGroup( "Files" );
409
410 QString s_filename = config.readEntry(fileNum, "" );
411 config.setGroup( "Titles" );
412
413 QString file_title = config.readEntry( s_filename, "");
414 if(title_text == file_title) {
415 rem=i;
416//qDebug("file title to remove is "+file_title);
417 selFile = s_filename;
418 config.removeEntry( s_filename); //removes file=title
419 }
420 }
421 config.setGroup( "Files" );
422
423 for(int fg = rem; fg < i_numofFiles ; fg++ ) { //this rewrites Files number entry
424 fileNum.setNum(fg);
425 fileNum2.setNum( fg + 1);
426
427 QString s_filename2 = config.readEntry(fileNum2, "" );
428
429 if (!s_filename2.isEmpty()) {
430 config.writeEntry(fileNum, s_filename2 );
431 }
432 }
433 config.writeEntry("NumberOfFiles", i_numofFiles - 1 );
434 config.removeEntry(fileNum2);
435// remFile();
436}
437
438/*
439 removes file title name from list and config file*/
440void OpenEtext::remFile()
441{
442 Config config("Gutenbrowser");
443 config.setGroup( "Files" );
444 QString remFile;
445 s_numofFiles = config.readEntry("NumberOfFiles", "0");
446 int i_numofFiles = s_numofFiles.toInt();
447 for (int i = 1; i <= i_numofFiles; i++) {
448 QString fileNum;
449 fileNum.setNum(i);
450 QString s_filename = config.readEntry(fileNum, "" );
451
452 if( s_filename == selFile) {
453 config.removeEntry(selFile);
454
455 for(int fg = i_numofFiles - i; fg < i_numofFiles ; fg++ ) { //this rewrites Files number entry
456 QString fileNum2;
457 fileNum2.setNum( fg + 1);
458 QString s_filename2 = config.readEntry(fileNum2, "" );
459
460 if (!s_filename2.isEmpty()) {
461 config.writeEntry(fileNum, s_filename2 );
462 }
463 }
464 }
465 }
466 config.writeEntry("NumberOfFiles", i_numofFiles - 1 );
467 config.removeEntry(s_numofFiles);
468
469 QListBox_1->triggerUpdate(true);
470}
471
472/*
473sorts the list*/
474void OpenEtext::scan() {
475
476 QListBox_1->sort(TRUE);
477 QListBox_1->triggerUpdate(true);
478}
479
480void OpenEtext::editTitle() {
481
482 int currentItem=QListBox_1->currentItem();
483 QString title_text = QListBox_1->text( currentItem);
484//qDebug("Selected "+title_text);
485
486 Config config("Gutenbrowser");
487 config.setGroup( "Files" );
488 s_numofFiles = config.readEntry("NumberOfFiles", "0");
489 int i_numofFiles = s_numofFiles.toInt();
490 QString fileNum;
491 for (int i = 1; i <= i_numofFiles+1; i++) {
492
493 fileNum.setNum(i);
494 config.setGroup( "Files" );
495 QString s_filename = config.readEntry(fileNum, "" );
496 config.setGroup( "Titles" );
497 QString file_title = config.readEntry( s_filename, "");
498//qDebug("file_title is "+file_title);
499 if(title_text == file_title ) {
500 selFile = s_filename;
501//qDebug("Edit: "+ file_title );
502 i=i_numofFiles+1;
503 Edit_Title *titleEdit;
504 titleEdit = new Edit_Title(this,file_title ,TRUE);
505 if(titleEdit->exec() !=0) {
506//qDebug(titleEdit->newTitle);
507 config.writeEntry( s_filename, titleEdit->newTitle);
508 QListBox_1->removeItem(currentItem);
509 QListBox_1->insertItem ( QPixmap( QPEApplication::qpeDir()+"pics/gutenbrowser/gutenbrowser_sm.png"), titleEdit->newTitle, currentItem);
510 }
511 }
512 }
513
514// getTitles();
515 QListBox_1->triggerUpdate(true);
516}
517