00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "gutenbrowser.h"
00016 #include "openetext.h"
00017 #include "editTitle.h"
00018
00019
00020 #include <opie2/ofiledialog.h>
00021 #include <opie2/odebug.h>
00022 #include <qpe/applnk.h>
00023 #include <qpe/config.h>
00024 #include <qpe/qpeapplication.h>
00025 using namespace Opie::Ui;
00026
00027
00028 #include <qtextstream.h>
00029 #include <qfileinfo.h>
00030
00031
00032 #if defined(_WS_WIN_)
00033 #include <windows.h>
00034 #else
00035 #include <unistd.h>
00036 #include <stdlib.h>
00037 #include <sys/stat.h>
00038 #endif
00039
00040 OpenEtext::OpenEtext(QWidget *parent, QString name) : QDialog(parent,name,true)
00041 {
00042 local_library = (QDir::homeDirPath ()) +"/Applications/gutenbrowser/";
00043 local_index=local_library+"GUTINDEX.ALL";
00044 initDialog();
00045 connect( QListBox_1, SIGNAL(selected ( int)),this, SLOT(select_title(int)) );
00046
00047 getTitles();
00048 }
00049
00050 OpenEtext::~OpenEtext()
00051 {
00052 }
00053
00054 void OpenEtext::getTitles()
00055 {
00056 QListBox_1->clear();
00057 Config config("Gutenbrowser");
00058 config.setGroup( "Files" );
00059 s_numofFiles = config.readEntry("NumberOfFiles", "0");
00060 QLabel_1->setText( tr(
00061 s_numofFiles+" etexts in your library."));
00062 int i_numofFiles = s_numofFiles.toInt();
00063 for (int i = 1; i <= i_numofFiles+1; i++) {
00064 QString temp;
00065 temp.setNum(i);
00066 config.setGroup( "Files" );
00067 QString ramble = config.readEntry(temp, "" );
00068 config.setGroup( "Titles" );
00069 title = config.readEntry(ramble, "");
00070 if( !title.isEmpty()) {
00071 QListBox_1->insertItem ( QPixmap( QPEApplication::qpeDir()+"pics/gutenbrowser/gutenbrowser_sm.png"), title, -1);
00072 }
00073 }
00074 }
00075
00076
00077
00078 void OpenEtext::OpenTitle()
00079 {
00080 if( QListBox_1->currentItem() != -1)
00081 select_title( QListBox_1->currentItem() );
00082 else {
00083 QString sMsg;
00084 sMsg = "You must select an Etext to open";
00085 QMessageBox::message( "Error",sMsg);
00086 }
00087 }
00088
00089 void OpenEtext::select_title(int index )
00090 {
00091 OpenButton->setDown(TRUE);
00092 QString temp;
00093 temp.setNum( index + 1);
00094 const char * title;
00095 title = QListBox_1->text( index );
00096 openFileTitle = title;
00097 Config config("Gutenbrowser");
00098 config.setGroup( "Files" );
00099 file = config.readEntry(temp, "");
00100 OpenEtext::accept();
00101 }
00102
00103
00104
00105 void OpenEtext::open()
00106 {
00107 OpenFileButton->setDown(TRUE);
00108 title = "";
00109 QString currDir="";
00110 QString filer;
00111 QString name_file;
00112 QString local ;
00113 local = (QDir::homeDirPath () +"Applications/gutenbrowser");
00114 QDir library( local);
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124 Config cfg("Gutenbrowser");
00125 cfg. setGroup ( "View" );
00126 QString dir = cfg.readEntry("LastOpenDirectory", QPEApplication::documentDir());
00127 QMap<QString, QStringList> map;
00128 map.insert(tr("All"), QStringList() );
00129 QStringList text;
00130 text << "text/*";
00131 map.insert(tr("Text"), text );
00132 text << "*";
00133 map.insert(tr("All"), text );
00134
00135 QString str = OFileDialog::getOpenFileName( 2, dir , QString::null, map);
00136
00137 if( !str.isEmpty() && QFile(str).exists() && !QFileInfo(str).isDir() ) {
00138 filer = str;
00139
00140 odebug << "Open file: "+str << oendl;
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155 if( filer.right(4) == ".txt"
00156 || filer.right(4) == ".TXT"
00157 || filer.right(4) == ".etx"
00158 || filer.right(4) == ".ETX"
00159 || filer.right(4) == ".etx"
00160 || filer.right(4) == ".ETX"
00161 || filer.right(4) == ".zip"
00162 || filer.right(4) == ".ZIP" ) {
00163 QFileInfo zipFile( filer);
00164 QString s_fileName = zipFile.fileName();
00165 QString cmd;
00166
00167 if( s_fileName.right(4) == ".zip") {
00168 s_fileName = s_fileName.left( s_fileName.length() - 4);
00169 if( chdir((const char*)local_library.latin1())!=0)
00170 odebug << "chdir failed." << oendl;
00171 cmd = "gunzip -S .zip " + filer;
00172 fileName = local_library + s_fileName + ".txt";
00173 system( cmd);
00174 }
00175
00176 else {
00177
00178 s_fileName = fileName;
00179 s_fileName.replace( s_fileName.length()-3,3,"gtn");
00180 rename(fileName.latin1(),s_fileName.latin1());
00181 fileName = s_fileName;
00182
00183 }
00184 } else
00185 fileName = str;
00186
00187 FindTitle(fileName);
00188
00189 QFileInfo fi( fileName);
00190 name_file = fi.fileName();
00191 name_file = name_file.left(name_file.length() - 4);
00192
00193 odebug << "Setting doclink" << oendl;
00194 DocLnk lnk;
00195 odebug << "name is " + name_file << oendl;
00196 lnk.setName(name_file);
00197 odebug << "Title is "+title << oendl;
00198 lnk.setComment(title);
00199
00200 QListBox_1->clear();
00201 getTitles();
00202 odebug << "Filename is "+fileName << oendl;
00203 lnk.setFile(fileName);
00204 lnk.setType("guten/plain");
00205 lnk.setExec(fileName);
00206 lnk.setIcon("gutenbrowser/Gutenbrowser");
00207 if(!lnk.writeLink())
00208 odebug << "Writing doclink did not work" << oendl;
00209
00210 }
00211 OpenFileButton->setDown(FALSE);
00212 }
00213
00214
00215
00216 bool OpenEtext::FindTitle( QString filename)
00217 {
00218 Config config("Gutenbrowser");
00219 config.setGroup( "Files" );
00220 QString s_numofFiles = config.readEntry("NumberOfFiles", "0");
00221 int i_numofFiles = s_numofFiles.toInt();
00222
00223 QFileInfo fi(filename);
00224 name = fi.fileName();
00225 odebug << "filename to open is " + name << oendl;
00226 QFile indexLib( filename);
00227 bool findCheck = FALSE;
00228
00229
00230 if ( indexLib.open( IO_ReadOnly) ) {
00231 odebug << "file opened successfully" << oendl;
00232 QTextStream indexStream( &indexLib );
00233 QString target = "Project Gutenberg Etext of";
00234 QString target2 = "Project Gutenberg Etext";
00235 QString target3 = "Project Gutenberg's Etext of";
00236 QString target4 = "Project Gutenberg's Etext";
00237 QString indexLine;
00238 while ( !indexStream.eof() ) {
00239
00240 indexLine = indexStream.readLine();
00241 if( indexLine.find(target, 0, TRUE) > -1 && !findCheck) {findCheck = TRUE;
00242 title = indexLine.mid( indexLine.find(target, 0, TRUE) + (target.length()) , indexLine.find("\r", 0, TRUE));
00243 title = title.left( title.find( "*",0, TRUE));
00244 title = title.stripWhiteSpace ();
00245
00246
00247 }
00248 if( indexLine.find( target2, 0, TRUE) > -1 && !findCheck) {
00249 findCheck = TRUE;
00250 title = indexLine.mid( indexLine.find( target2, 0, TRUE ) + ( target2.length()) , indexLine.find("\r", 0, TRUE) );
00251 title = title.left( title.find( "*",0, TRUE));
00252 title = title.stripWhiteSpace ();
00253
00254
00255 }
00256 if( indexLine.find( target3, 0, TRUE) > -1 && !findCheck) {
00257 findCheck = TRUE;
00258 title = indexLine.mid( indexLine.find( target3, 0, TRUE) + ( target3.length()) , indexLine.find("\r", 0, TRUE));
00259 title = title.left( title.find( "*",0, TRUE));
00260 title = title.stripWhiteSpace ();
00261
00262 }
00263 if( indexLine.find( target4, 0, TRUE) > -1 && !findCheck) {
00264 findCheck = TRUE;
00265 title = indexLine.mid( indexLine.find( target4, 0, TRUE) + ( target4.length()) , indexLine.find("\r", 0, TRUE));
00266 title = title.left( title.find( "*",0, TRUE));
00267 title = title.stripWhiteSpace ();
00268
00269 }
00270 }
00271 indexLib.close();
00272
00273 if( !findCheck || title.length() < 2) {
00274 odebug << "Trying hard to find title from GUTINDEX.ALL" << oendl;
00275 title = titleFromLibrary( filename);
00276 findCheck = TRUE;
00277 }
00278
00279 if ( checkConf() == false && findCheck == TRUE) {
00280
00281 config.setGroup( "Files");
00282 config.writeEntry("NumberOfFiles",i_numofFiles + 1 );
00283 QString interger;
00284 interger.setNum( i_numofFiles +1);
00285 config.writeEntry(interger, filename);
00286 config.setGroup( "Titles" );
00287 for (int i = 1; i <= i_numofFiles; i++) {
00288 QString temp;
00289 temp.setNum(i);
00290 config.setGroup( "Files" );
00291 QString ramble = config.readEntry(temp, "" );
00292 config.setGroup( "Titles" );
00293 QString title1 = config.readEntry(ramble, "");
00294 if(title1==title)
00295 title+="1";
00296 i= i_numofFiles+1;
00297 }
00298 if(title.length()<3) {
00299
00300 title="Unknown";
00301 }
00302 config.writeEntry( filename,title);
00303 }
00304 } else {
00305 QString sMsg;
00306 sMsg = "Error opening library file: "+filename;
00307 printf( sMsg+"\n");
00308 }
00309 return true;
00310 }
00311
00312 QString OpenEtext::titleFromLibrary( QString fileName)
00313 {
00314 QFile indexLib( local_index);
00315 QString target;
00316 int find1 = fileName.findRev("/",-1,TRUE) + 1;
00317 int find2 = fileName.findRev(".gtn",-1,TRUE) - find1;
00318 if(find2==-1-find1)
00319 int find2 = fileName.findRev(".etx",-1,TRUE) - find1;
00320 else if(find2==-1-find1)
00321 int find2 = fileName.findRev(".txt",-1,TRUE) - find1;
00322
00323 target = fileName.mid( find1, find2 );
00324 QString checker = target.right(1);
00325 bool ok;
00326 checker.toInt( &ok,10);
00327 if( ok) {
00328 target = target.left( target.length()-1);
00329 checker = target.right(1);
00330 ok = FALSE;
00331 checker.toInt( &ok,10);
00332 if( ok) {
00333 target = target.left( target.length()-1);
00334 }
00335 }
00336 if ( indexLib.open( IO_ReadOnly) ) {
00337
00338 QTextStream indexStream( &indexLib );
00339 QString indexLine;
00340 bool findCheck = FALSE;
00341 while ( !indexStream.atEnd() ) {
00342 indexLine = indexStream.readLine();
00343
00344 if( indexLine.find( target,0,FALSE) > 0 ) {
00345 findCheck = TRUE;
00346 title = indexLine.mid( 9, 50);
00347
00348 title = title.stripWhiteSpace ();
00349
00350
00351 }
00352 }
00353 }
00354 else
00355 odebug << "Error opening library index "+ local_index << oendl;
00356 return title;
00357 }
00358
00359 bool OpenEtext::checkConf()
00360 {
00361 QString file = fileName;
00362 Config config("Gutenbrowser");
00363 config.setGroup( "Files" );
00364 QString s_numofFiles = config.readEntry("NumberOfFiles", "0" );
00365 int i_numofFiles = s_numofFiles.toInt();
00366
00367 for (int i = 1; i <= i_numofFiles; i++) {
00368 QString temp;
00369 temp.setNum(i);
00370 config.setGroup( "Files");
00371 QString ramble = config.readEntry(temp, "" );
00372
00373 if(ramble == file ) {
00374 return true;
00375 }
00376 }
00377 return false;
00378 }
00379
00380 void OpenEtext::remove()
00381 {
00382 title_text = QListBox_1->text( QListBox_1->currentItem() );
00383 title_text=title_text.stripWhiteSpace();
00384 QString msg ="<p>Do you really want to REMOVE\n" +title_text +"?\nThis will not delete the file.</P>";
00385 switch( QMessageBox::information( this, (tr("Remove Etext")),
00386 (tr(msg)),
00387 (tr("&Yes")), (tr("&Cancel")), 0 )){
00388 case 0:
00389 removeSelection();
00390 QListBox_1->clear();
00391 getTitles();
00392 break;
00393 case 1:
00394 break;
00395 };
00396 }
00397
00398
00399
00400 void OpenEtext::removeSelection()
00401 {
00402 Config config("Gutenbrowser");
00403 config.setGroup( "Files" );
00404 s_numofFiles = config.readEntry("NumberOfFiles", "0");
00405 int rem=0;
00406 int i_numofFiles = s_numofFiles.toInt();
00407 QString fileNum2;
00408 QString fileNum;
00409
00410 for (int i = 1; i <= i_numofFiles; i++) {
00411 fileNum.setNum(i);
00412 config.setGroup( "Files" );
00413
00414 QString s_filename = config.readEntry(fileNum, "" );
00415 config.setGroup( "Titles" );
00416
00417 QString file_title = config.readEntry( s_filename, "");
00418 if(title_text == file_title) {
00419 rem=i;
00420
00421 selFile = s_filename;
00422 config.removeEntry( s_filename);
00423 }
00424 }
00425 config.setGroup( "Files" );
00426
00427 for(int fg = rem; fg < i_numofFiles ; fg++ ) {
00428 fileNum.setNum(fg);
00429 fileNum2.setNum( fg + 1);
00430
00431 QString s_filename2 = config.readEntry(fileNum2, "" );
00432
00433 if (!s_filename2.isEmpty()) {
00434 config.writeEntry(fileNum, s_filename2 );
00435 }
00436 }
00437 config.writeEntry("NumberOfFiles", i_numofFiles - 1 );
00438 config.removeEntry(fileNum2);
00439
00440 }
00441
00442
00443
00444 void OpenEtext::remFile()
00445 {
00446 Config config("Gutenbrowser");
00447 config.setGroup( "Files" );
00448 QString remFile;
00449 s_numofFiles = config.readEntry("NumberOfFiles", "0");
00450 int i_numofFiles = s_numofFiles.toInt();
00451 for (int i = 1; i <= i_numofFiles; i++) {
00452 QString fileNum;
00453 fileNum.setNum(i);
00454 QString s_filename = config.readEntry(fileNum, "" );
00455
00456 if( s_filename == selFile) {
00457 config.removeEntry(selFile);
00458
00459 for(int fg = i_numofFiles - i; fg < i_numofFiles ; fg++ ) {
00460 QString fileNum2;
00461 fileNum2.setNum( fg + 1);
00462 QString s_filename2 = config.readEntry(fileNum2, "" );
00463
00464 if (!s_filename2.isEmpty()) {
00465 config.writeEntry(fileNum, s_filename2 );
00466 }
00467 }
00468 }
00469 }
00470 config.writeEntry("NumberOfFiles", i_numofFiles - 1 );
00471 config.removeEntry(s_numofFiles);
00472
00473 QListBox_1->triggerUpdate(true);
00474 }
00475
00476
00477
00478 void OpenEtext::scan() {
00479
00480 QListBox_1->sort(TRUE);
00481 QListBox_1->triggerUpdate(true);
00482 }
00483
00484 void OpenEtext::editTitle() {
00485
00486 int currentItem=QListBox_1->currentItem();
00487 QString title_text = QListBox_1->text( currentItem);
00488
00489
00490 Config config("Gutenbrowser");
00491 config.setGroup( "Files" );
00492 s_numofFiles = config.readEntry("NumberOfFiles", "0");
00493 int i_numofFiles = s_numofFiles.toInt();
00494 QString fileNum;
00495 for (int i = 1; i <= i_numofFiles+1; i++) {
00496
00497 fileNum.setNum(i);
00498 config.setGroup( "Files" );
00499 QString s_filename = config.readEntry(fileNum, "" );
00500 config.setGroup( "Titles" );
00501 QString file_title = config.readEntry( s_filename, "");
00502
00503 if(title_text == file_title ) {
00504 selFile = s_filename;
00505
00506 i=i_numofFiles+1;
00507 Edit_Title *titleEdit;
00508 titleEdit = new Edit_Title(this,file_title ,TRUE);
00509 if(titleEdit->exec() !=0) {
00510
00511 config.writeEntry( s_filename, titleEdit->newTitle);
00512 QListBox_1->removeItem(currentItem);
00513 QListBox_1->insertItem ( QPixmap( QPEApplication::qpeDir()+"pics/gutenbrowser/gutenbrowser_sm.png"), titleEdit->newTitle, currentItem);
00514 }
00515 }
00516 }
00517
00518
00519 QListBox_1->triggerUpdate(true);
00520 }
00521