kpilot Library API Documentation

kpalmdoc_dlg.cc

00001 /* kpalmdoc_dlg.cpp
00002 **
00003 ** Copyright (C) 2003 by Reinhold Kainhofer
00004 **
00005 ** This is the main dialog of the KDE PalmDOC converter.
00006 */
00007 
00008 /*
00009 ** This program is free software; you can redistribute it and/or modify
00010 ** it under the terms of the GNU General Public License as published by
00011 ** the Free Software Foundation; either version 2 of the License, or
00012 ** (at your option) any later version.
00013 **
00014 ** This program is distributed in the hope that it will be useful,
00015 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00017 ** GNU General Public License for more details.
00018 **
00019 ** You should have received a copy of the GNU General Public License
00020 ** along with this program in a file called COPYING; if not, write to
00021 ** the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00022 ** MA 02111-1307, USA.
00023 */
00024 
00025 /*
00026 ** Bug reports and questions can be sent to kde-pim@kde.org
00027 */
00028 #include "kpalmdoc_dlg.h"
00029 #include "kpalmdoc_dlgbase.h"
00030 
00031 #include <qtabwidget.h>
00032 #include <kapplication.h>
00033 #include <stdlib.h>
00034 #include <stdio.h>
00035 #include <iostream>
00036 
00037 #include <kurlrequester.h>
00038 #include <qcheckbox.h>
00039 #include <qradiobutton.h>
00040 #include <kconfig.h>
00041 #include <qbuttongroup.h>
00042 #include <klocale.h>
00043 #include <qlabel.h>
00044 #include <kaboutapplication.h>
00045 #include <kmessagebox.h>
00046 
00047 #include <pilotLocalDatabase.h>
00048 
00049 #include "options.h"
00050 #include "DOC-converter.h"
00051 
00052 
00053 ConverterDlg::ConverterDlg( QWidget *parent, const QString& caption)
00054    : KDialogBase( parent, "converterdialog", false, caption, KDialogBase::Close|KDialogBase::Help|KDialogBase::User1,
00055        KDialogBase::Close, true, i18n("&About"))
00056 {
00057     QWidget *page = makeHBoxMainWidget();
00058     dlg=new ConverterDlgBase(page);
00059     readSettings();
00060 //  setMainWidget(dlg->tabWidget);
00061 
00062     // TODO: Connect the signals and slots from the various widgets!
00063     // e.g.
00064     connect(dlg->fDirectories, SIGNAL(toggled(bool)),
00065         this, SLOT(slotDirectories(bool)));
00066     connect(dlg->fTextToPDB, SIGNAL(clicked()), this, SLOT(slotToPDB()));
00067     connect(dlg->fPDBToText, SIGNAL(clicked()), this, SLOT(slotToText()));
00068 
00069     resize(minimumSize());
00070 }
00071 
00072 ConverterDlg::~ConverterDlg()
00073 {
00074     // no need to delete child widgets, Qt does it all for us
00075 }
00076 void ConverterDlg::writeSettings()
00077 {
00078     KConfig* fConfig = kapp->config();
00079     if (!fConfig) return;
00080 //  config->setGroup("GeneralData");
00081 
00082     // General page
00083     fConfig->writeEntry("TXT folder", dlg->fTXTDir->url());
00084     fConfig->writeEntry("PDB folder", dlg->fPDBDir->url());
00085     fConfig->writeEntry("Sync folders", dlg->fDirectories->isChecked());
00086     fConfig->writeEntry("Ask before overwriting files", dlg->fAskOverwrite->isChecked());
00087     fConfig->writeEntry("Verbose messages", dlg->fVerbose->isChecked());
00088 
00089     // PC->Handheld page
00090     fConfig->writeEntry("Compress", dlg->fCompress->isChecked());
00091     fConfig->writeEntry("Convert bookmarks", dlg->fConvertBookmarks->isChecked());
00092     fConfig->writeEntry("Bookmarks inline", dlg->fBookmarksInline->isChecked());
00093     fConfig->writeEntry("Bookmarks endtags", dlg->fBookmarksEndtags->isChecked());
00094     fConfig->writeEntry("Bookmarks bmk", dlg->fBookmarksBmk->isChecked());
00095 
00096     // Handheld->PC page
00097     fConfig->writeEntry("Bookmarks to PC",
00098         dlg->fPCBookmarks->id(dlg->fPCBookmarks->selected()));
00099 
00100     fConfig->sync();
00101 }
00102 void ConverterDlg::readSettings()
00103 {
00104     KConfig* fConfig = kapp->config();
00105     if (!fConfig) return;
00106 
00107     // General Page:
00108     dlg->fTXTDir->setURL(fConfig->
00109         readEntry("TXT directory"));
00110     dlg->fPDBDir->setURL(fConfig->
00111         readEntry("PDB directory"));
00112     bool dir=fConfig->
00113         readBoolEntry("Sync directories", false);
00114     dlg->fDirectories->setChecked(dir);
00115     slotDirectories(dir);
00116     askOverwrite=fConfig->readBoolEntry("Ask before overwriting files", true);
00117     dlg->fAskOverwrite->setChecked(askOverwrite);
00118     verbose=fConfig->readBoolEntry("Verbose messages", true);
00119     dlg->fVerbose->setChecked(verbose);
00120 
00121     // PC->Handheld page
00122     dlg->fCompress->setChecked(fConfig->
00123         readBoolEntry("Compress", true));
00124     dlg->fConvertBookmarks->setChecked(fConfig->
00125         readBoolEntry("Convert bookmarks", true));
00126     dlg->fBookmarksInline->setChecked(fConfig->
00127         readBoolEntry("Bookmarks inline", true));
00128     dlg->fBookmarksEndtags->setChecked(fConfig->
00129         readBoolEntry("Bookmarks endtags", true));
00130     dlg->fBookmarksBmk->setChecked(fConfig->
00131         readBoolEntry("Bookmarks bmk", true));
00132 
00133     // Handheld->PC page
00134     dlg->fPCBookmarks->setButton(fConfig->
00135         readNumEntry("Bookmarks to PC", 0));
00136 }
00137 
00138 void ConverterDlg::slotClose()
00139 {
00140   writeSettings();
00141   kapp->quit();
00142   delete this;
00143 }
00144 
00145 void ConverterDlg::slotToText()
00146 {
00147     // First, get the settings from the controls and initialize
00148     // the converter object
00149     int bmks=dlg->fPCBookmarks->id(dlg->fPCBookmarks->selected());
00150     DOCConverter conv;
00151     switch(bmks) {
00152         case 0: conv.setBookmarkTypes(DOCConverter::eBmkNone); break;
00153         case 1: conv.setBookmarkTypes(DOCConverter::eBmkInline); break;
00154         case 2: conv.setBookmarkTypes(DOCConverter::eBmkEndtags); break;
00155         case 3: conv.setBookmarkTypes(DOCConverter::eBmkDefaultBmkFile); break;
00156         default:
00157             break;
00158     }
00159 
00160     askOverwrite=dlg->fAskOverwrite->isChecked();
00161     verbose=dlg->fVerbose->isChecked();
00162 
00163 
00164     bool dir=dlg->fDirectories->isChecked();
00165     QString txturl=dlg->fTXTDir->url();
00166     QString pdburl=dlg->fPDBDir->url();
00167 
00168     QFileInfo txtinfo(txturl);
00169     QFileInfo pdbinfo(pdburl);
00170 
00171     if (dir)
00172     {
00173         if (pdbinfo.isFile())
00174         {
00175             int res=KMessageBox::questionYesNo(this, 
00176                 i18n("<qt>You selected to sync folders, "
00177                 "but gave a filename instead (<em>%1</em>)."
00178                 "<br>Use folder <em>%2</em> instead?</qt>").arg(pdburl)
00179                 .arg(pdbinfo.dirPath(true)));
00180             if (res==KMessageBox::Yes)
00181             {
00182                 pdburl=pdbinfo.dirPath(true);
00183                 pdbinfo.setFile(pdburl);
00184             }
00185             else return;
00186         }
00187 
00188         if (!pdbinfo.isDir())
00189         {
00190             // no directory, so error message and return
00191             KMessageBox::sorry(this, 
00192                 i18n("<qt>The folder <em>%1</em> for "
00193                 "the handheld database files is not a valid "
00194                 "folder.</qt>").arg(pdburl));
00195             return;
00196         }
00197 
00198         if (!pdbinfo.exists())
00199         {
00200             KMessageBox::sorry(this, 
00201                 i18n("<qt>The folder <em>%1</em> for "
00202                 "the handheld database files is not a "
00203                 "valid directory.</qt>").arg(pdburl));
00204             return;
00205         }
00206 
00207 
00208         // Now check the to directory:
00209         if (txtinfo.isFile())
00210         {
00211             int res=KMessageBox::questionYesNo(this, 
00212                 i18n("<qt>You selected to sync folders, "
00213                 "but gave a filename instead (<em>%1</em>)."
00214                 "<br>Use folder <em>%2</em> instead?</qt>").arg(txturl)
00215                 .arg(txtinfo.dirPath(true)));
00216             if (res==KMessageBox::Yes) {
00217                 txturl=txtinfo.dirPath(true);
00218                 txtinfo.setFile(txturl);
00219             }
00220             else return;
00221         }
00222 
00223         // Now that we have a directory path, try to create it:
00224         if (!txtinfo.isDir()) {
00225             txtinfo.dir().mkdir(txturl, true);
00226         }
00227         if (!txtinfo.isDir()) {
00228             KMessageBox::sorry(this, 
00229                 i18n("<qt>The folder <em>%1</em> for "
00230                 "the text files could not be created.</qt>").arg(txturl));
00231             return;
00232         }
00233 
00234 
00235         // Now that we have both directories, create the converter object
00236 DEBUGCONDUIT<<"Pdbinfo.dir="<<pdbinfo.dir().absPath()<<endl;
00237 DEBUGCONDUIT<<"txtinfo.dir="<<txtinfo.dir().absPath()<<endl;
00238         QStringList pdbfiles(pdbinfo.dir().entryList("*.pdb"));
00239         QStringList converted_Files;
00240 
00241 DEBUGCONDUIT<<"Length of filename list: "<<pdbfiles.size()<<endl;
00242         for ( QStringList::Iterator it = pdbfiles.begin(); it != pdbfiles.end(); ++it )
00243         {
00244             QString txtfile=QFileInfo(*it).baseName(true)+".txt";
00245 DEBUGCONDUIT<<"pdbfile="<<*it<<", pdbdir="<<pdburl<<", txtfile="<<txtfile<<", txtdir="<<txturl<<endl;
00246             if (convertPDBtoTXT(pdburl, *it, txturl, txtfile, &conv))
00247             {
00248                 converted_Files.append(*it);
00249             }
00250         }
00251         if (converted_Files.size()>0) {
00252             KMessageBox::informationList(this, i18n("The following texts were "
00253                     "successfully converted:"), converted_Files, i18n("Conversion Successful"));
00254         }
00255         else
00256         {
00257             KMessageBox::sorry(this, i18n("No text files were converted correctly"));
00258         }
00259 
00260 
00261     } else { // no dir
00262 
00263 
00264         // Check the from file
00265         if (!pdbinfo.isFile() || !pdbinfo.exists())
00266         {
00267             KMessageBox::sorry(this, i18n("<qt>The file <em>%1</em> does not "
00268                 "exist.</qt>").arg(pdburl));
00269             return;
00270         }
00271 
00272         // Now check the to file
00273 /*      // I can't check if a given filename is a valid filename
00274         if (!txtinfo.isFile())
00275         {
00276             KMessageBox::sorry(this, i18n("<qt>The filename <em>%1</em> for the "
00277                 "text is not a valid filename.</qt>").arg(txturl));
00278             return;
00279         }*/
00280         if (convertPDBtoTXT(pdbinfo.dirPath(true), pdbinfo.fileName(),
00281                 txtinfo.dirPath(true), txtinfo.fileName(), &conv) )
00282         {
00283             KMessageBox::information(this, i18n("Conversion of file %1 successful.").arg(pdburl));
00284         }
00285 
00286     }
00287 
00288 }
00289 
00290 void ConverterDlg::slotToPDB()
00291 {
00292     // First, get the settings from the controls and initialize
00293     // the converter object
00294     bool compress=dlg->fCompress->isChecked();
00295     int bmks=0;
00296     if (dlg->fConvertBookmarks->isChecked())
00297     {
00298         if (dlg->fBookmarksInline->isChecked()) bmks|=DOCConverter::eBmkInline;
00299         if (dlg->fBookmarksEndtags->isChecked()) bmks|=DOCConverter::eBmkEndtags;
00300         if(dlg->fBookmarksBmk->isChecked()) bmks|=DOCConverter::eBmkDefaultBmkFile;
00301     }
00302     DOCConverter conv;
00303     conv.setBookmarkTypes(bmks);
00304     conv.setCompress(compress);
00305     conv.setSort(DOCConverter::eSortName);
00306 
00307 
00308     askOverwrite=dlg->fAskOverwrite->isChecked();
00309     verbose=dlg->fVerbose->isChecked();
00310 
00311 
00312     bool dir=dlg->fDirectories->isChecked();
00313     QString txturl=dlg->fTXTDir->url();
00314     QString pdburl=dlg->fPDBDir->url();
00315 
00316     QFileInfo txtinfo(txturl);
00317     QFileInfo pdbinfo(pdburl);
00318 
00319     if (dir)
00320     {
00321         if (txtinfo.isFile())
00322         {
00323             int res=KMessageBox::questionYesNo(this, 
00324                 i18n("<qt>You selected to sync folders, "
00325                 "but gave a filename instead (<em>%1</em>)."
00326                 "<br>Use folder <em>%2</em> instead?</qt>").arg(txturl)
00327                 .arg(txtinfo.dirPath(true)));
00328             if (res==KMessageBox::Yes)
00329             {
00330                 txturl=txtinfo.dirPath(true);
00331                 txtinfo.setFile(txturl);
00332             }
00333             else return;
00334         }
00335 
00336         if (!txtinfo.isDir() || !txtinfo.exists())
00337         {
00338             KMessageBox::sorry(this, 
00339                 i18n("<qt>The folder <em>%1</em> for "
00340                 "the text files is not a valid folder.</qt>").arg(txturl));
00341             return;
00342         }
00343 
00344 
00345         // Now check the to directory:
00346         if (pdbinfo.isFile())
00347         {
00348             int res=KMessageBox::questionYesNo(this, 
00349                 i18n("<qt>You selected to sync folders, "
00350                 "but gave a filename instead (<em>%1</em>)."
00351                 "<br>Use folder <em>%2</em> instead?</qt>")
00352                 .arg(pdburl)
00353                 .arg(pdbinfo.dirPath(true)));
00354             if (res==KMessageBox::Yes) {
00355                 pdburl=pdbinfo.dirPath(true);
00356                 pdbinfo.setFile(pdburl);
00357             }
00358             else return;
00359         }
00360 
00361         // Now that we have a directory path, try to create it:
00362         if (!pdbinfo.isDir()) {
00363             pdbinfo.dir().mkdir(pdburl, true);
00364         }
00365         if (!pdbinfo.isDir()) {
00366             KMessageBox::sorry(this, i18n("<qt>The folder <em>%1</em> for "
00367                 "the PalmDOC files could not be created.</qt>").arg(pdburl));
00368             return;
00369         }
00370 
00371 
00372         // Now that we have both directories, create the converter object
00373 DEBUGCONDUIT<<"Pdbinfo.dir="<<pdbinfo.dir().absPath()<<endl;
00374 DEBUGCONDUIT<<"txtinfo.dir="<<txtinfo.dir().absPath()<<endl;
00375         QStringList txtfiles(txtinfo.dir().entryList("*.txt"));
00376         QStringList converted_Files;
00377 
00378 DEBUGCONDUIT<<"Length of filename list: "<<txtfiles.size()<<endl;
00379         for ( QStringList::Iterator it = txtfiles.begin(); it != txtfiles.end(); ++it )
00380         {
00381             QString pdbfile=QFileInfo(*it).baseName(true)+".pdb";
00382 DEBUGCONDUIT<<"pdbfile="<<pdbfile<<", pdbdir="<<pdburl<<", txtfile="<<*it<<", txtdir="<<txturl<<endl;
00383             if (convertTXTtoPDB(txturl, *it, pdburl, pdbfile, &conv))
00384             {
00385                 converted_Files.append(*it);
00386             }
00387         }
00388         if (converted_Files.size()>0) {
00389             KMessageBox::informationList(this, i18n("The following texts were "
00390                     "successfully converted:"), converted_Files, i18n("Conversion Successful"));
00391         }
00392         else
00393         {
00394             KMessageBox::sorry(this, i18n("No text files were converted correctly"));
00395         }
00396 
00397 
00398     } else { // no dir
00399 
00400 
00401         // Check the from file
00402         if (!txtinfo.isFile() || !txtinfo.exists())
00403         {
00404             KMessageBox::sorry(this, i18n("<qt>The file <em>%1</em> does not "
00405                 "exist.</qt>").arg(txturl));
00406             return;
00407         }
00408 
00409         if (convertTXTtoPDB(txtinfo.dirPath(true), txtinfo.fileName(),
00410                 pdbinfo.dirPath(true), pdbinfo.fileName(), &conv) )
00411         {
00412             KMessageBox::information(this, i18n("Conversion of file %1 successful.").arg(txturl));
00413         }
00414 
00415     }
00416 
00417 }
00418 
00419 
00420 void ConverterDlg::slotUser1()
00421 {
00422     KAboutApplication ab(KGlobal::instance()->aboutData(), this);
00423     ab.show();
00424     ab.exec();
00425     return;
00426 }
00427 
00428 void ConverterDlg::slotDirectories(bool dir)
00429 {
00430     DEBUGCONDUIT<<"Slot Directories: "<<dir<<endl;
00431     if (dir)
00432     {
00433         dlg->fTextLabel->setText(i18n("&Text folder:"));
00434         dlg->fPdbLabel->setText(i18n("&PalmDOC folder:"));
00435         dlg->fTXTDir->setMode(KFile::LocalOnly | KFile::Directory);
00436         dlg->fPDBDir->setMode(KFile::LocalOnly | KFile::Directory);
00437     } else {
00438         dlg->fTextLabel->setText(i18n("&Text file:"));
00439         dlg->fPdbLabel->setText(i18n("&DOC file:"));
00440         dlg->fTXTDir->setMode(KFile::LocalOnly | KFile::File);
00441         dlg->fPDBDir->setMode(KFile::LocalOnly | KFile::File);
00442     }
00443 }
00444 
00445 bool ConverterDlg::convertTXTtoPDB(QString txtdir, QString txtfile,
00446         QString pdbdir, QString pdbfile, DOCConverter*conv)
00447 {
00448     bool res=false;
00449     QFileInfo dbfileinfo(pdbdir, pdbfile);
00450 DEBUGCONDUIT<<"Working  on file "<<pdbfile<<endl;
00451     if (!dbfileinfo.exists() || !askOverwrite ||
00452             (KMessageBox::Yes==KMessageBox::questionYesNo(this,
00453             i18n("<qt>The database file already <em>%1</em> exists.  Overwrite it?</qt>")
00454             .arg(dbfileinfo.filePath()) ) ))
00455     {
00456         PilotLocalDatabase*pdbdb=new PilotLocalDatabase(pdbdir, QFileInfo(pdbfile).baseName(), false);
00457         if (pdbdb)
00458         {
00459             if (!pdbdb->isDBOpen())
00460             {
00461 #ifdef DEBUG
00462                 DEBUGCONDUIT<<pdbfile<<" does not yet exist. Creating it"<<endl;
00463 #endif
00464                 if (!pdbdb->createDatabase(get_long("REAd"), get_long("TEXt")) ) {
00465                 }
00466             }
00467 
00468             if (pdbdb->isDBOpen())
00469             {
00470                 conv->setPDB(pdbdb);
00471                 conv->setTXTpath(txtdir, txtfile);
00472                 DEBUGCONDUIT<<"Converting "<<txtfile<<" (dir "<<txtdir<<") to "<<dbfileinfo.filePath()<<endl;
00473                 if (conv->convertTXTtoPDB()) res=true;
00474             }
00475             delete pdbdb;
00476         }
00477         if ( !res && verbose )
00478         {
00479             KMessageBox::sorry(this, i18n("<qt>Error while converting the text %1.</qt>").arg(txtfile));
00480         }
00481     }
00482     else
00483     {
00484         DEBUGCONDUIT<<"Ignoring the file "<<txtfile<<endl;
00485     }
00486     return res;
00487 }
00488 
00489 bool ConverterDlg::convertPDBtoTXT(QString pdbdir, QString pdbfile,
00490         QString txtdir, QString txtfile, DOCConverter*conv)
00491 {
00492     bool res=false;
00493     QFileInfo txtfileinfo(txtdir, txtfile);
00494 DEBUGCONDUIT<<"Working  on file "<<txtfile<<endl;
00495     if (!txtfileinfo.exists() || !askOverwrite ||
00496             (KMessageBox::Yes==KMessageBox::questionYesNo(this,
00497             i18n("<qt>The text file already <em>%1</em> exists.  Overwrite it?</qt>")
00498             .arg(txtfileinfo.filePath()) ) ))
00499     {
00500         PilotLocalDatabase*pdbdb=new PilotLocalDatabase(pdbdir, QFileInfo(pdbfile).baseName(), false);
00501         if (pdbdb)
00502         {
00503             if (pdbdb->isDBOpen())
00504             {
00505                 conv->setPDB(pdbdb);
00506                 conv->setTXTpath(txtdir, txtfile);
00507                 DEBUGCONDUIT<<"Converting "<<txtfile<<" (dir "<<txtdir<<") from "<<pdbfile<<" (dir "<<pdbdir<<")"<<endl;
00508                 if (conv->convertPDBtoTXT()) res=true;
00509             }
00510             delete pdbdb;
00511         }
00512         if ( !res && verbose )
00513         {
00514             KMessageBox::sorry(this, i18n("<qt>Error while converting the text %1.</qt>").arg(pdbfile));
00515         }
00516     }
00517     else
00518     {
00519         DEBUGCONDUIT<<"Ignoring the file "<<pdbfile<<endl;
00520     }
00521     return res;
00522 
00523 }
00524 
00525 #include "kpalmdoc_dlg.moc"
KDE Logo
This file is part of the documentation for kpilot Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:36:47 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003