kpilot

kpalmdoc_dlg.cc

Go to the documentation of this file.
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., 51 Franklin Street, Fifth Floor, Boston,
00022 ** MA 02110-1301, USA.
00023 */
00024 
00025 /*
00026 ** Bug reports and questions can be sent to kde-pim@kde.org
00027 */
00028 #include "options.h"
00029 
00030 #include <stdlib.h>
00031 #include <stdio.h>
00032 #include <iostream>
00033 
00034 #include <qtabwidget.h>
00035 #include <qcheckbox.h>
00036 #include <qradiobutton.h>
00037 #include <qbuttongroup.h>
00038 #include <qlabel.h>
00039 #include <qcombobox.h>
00040 
00041 #include <klocale.h>
00042 #include <kconfig.h>
00043 #include <kaboutapplication.h>
00044 #include <kapplication.h>
00045 #include <kurlrequester.h>
00046 #include <kmessagebox.h>
00047 #include <kcharsets.h>
00048 
00049 #include <pilotLocalDatabase.h>
00050 
00051 #include "kpalmdoc_dlg.h"
00052 #include "kpalmdoc_dlgbase.h"
00053 #include "DOC-converter.h"
00054 #include "kpalmdocSettings.h"
00055 
00056 
00057 ConverterDlg::ConverterDlg( QWidget *parent, const QString& caption)
00058    : KDialogBase( parent, "converterdialog", false, caption, KDialogBase::Close|KDialogBase::Help|KDialogBase::User1,
00059        KDialogBase::Close, true, i18n("&About"))
00060 {
00061     QWidget *page = makeHBoxMainWidget();
00062     dlg=new ConverterDlgBase(page);
00063     QStringList l = KGlobal::charsets()->descriptiveEncodingNames();
00064     for ( QStringList::Iterator it = l.begin(); it != l.end(); ++it)
00065     {
00066         dlg->fEncoding->insertItem(*it);
00067     }
00068 
00069     readSettings();
00070 
00071     connect(dlg->fDirectories, SIGNAL(toggled(bool)),
00072         this, SLOT(slotDirectories(bool)));
00073     connect(dlg->fTextToPDB, SIGNAL(clicked()), this, SLOT(slotToPDB()));
00074     connect(dlg->fPDBToText, SIGNAL(clicked()), this, SLOT(slotToText()));
00075 
00076     resize(minimumSize());
00077 }
00078 
00079 ConverterDlg::~ConverterDlg()
00080 {
00081     // no need to delete child widgets, Qt does it all for us
00082 }
00083 void ConverterDlg::writeSettings()
00084 {
00085     // General page
00086     KPalmDocSettings::setTXTFolder( dlg->fTXTDir->url() );
00087     KPalmDocSettings::setPDBFolder( dlg->fPDBDir->url() );
00088     KPalmDocSettings::setSyncFolders( dlg->fDirectories->isChecked() );
00089     KPalmDocSettings::setAskOverwrite( dlg->fAskOverwrite->isChecked() );
00090     KPalmDocSettings::setVerboseMessages( dlg->fVerbose->isChecked() );
00091     KPalmDocSettings::setEncoding( dlg->fEncoding->currentText() );
00092 
00093     // PC->Handheld page
00094     KPalmDocSettings::setCompress( dlg->fCompress->isChecked() );
00095     KPalmDocSettings::setConvertBookmarks( dlg->fConvertBookmarks->isChecked() );
00096     KPalmDocSettings::setBookmarksInline( dlg->fBookmarksInline->isChecked() );
00097     KPalmDocSettings::setBookmarksEndtags( dlg->fBookmarksEndtags->isChecked() );
00098     KPalmDocSettings::setBookmarksBmk( dlg->fBookmarksBmk->isChecked() );
00099 
00100     // Handheld->PC page
00101     KPalmDocSettings::setBookmarksToPC( dlg->fPCBookmarks->id(dlg->fPCBookmarks->selected()) );
00102 
00103     KPalmDocSettings::self()->writeConfig();
00104 }
00105 
00106 void ConverterDlg::readSettings()
00107 {
00108     FUNCTIONSETUP;
00109 
00110     KPalmDocSettings::self()->readConfig();
00111 
00112     // General Page:
00113     dlg->fTXTDir->setURL(KPalmDocSettings::tXTFolder());
00114     dlg->fPDBDir->setURL(KPalmDocSettings::pDBFolder());
00115     bool dir=KPalmDocSettings::syncFolders();
00116     dlg->fDirectories->setChecked(dir);
00117     slotDirectories(dir);
00118     dlg->fAskOverwrite->setChecked( KPalmDocSettings::askOverwrite() );
00119     dlg->fVerbose->setChecked( KPalmDocSettings::verboseMessages() );
00120     QString encoding = KPalmDocSettings::encoding();
00121 #ifdef DEBUG
00122     DEBUGKPILOT << fname << ": Encoding=" << encoding << endl;
00123 #endif
00124     dlg->fEncoding->setCurrentText( KPalmDocSettings::encoding() );
00125 
00126     // PC->Handheld page
00127     dlg->fCompress->setChecked(KPalmDocSettings::compress() );
00128     dlg->fConvertBookmarks->setChecked(KPalmDocSettings::convertBookmarks());
00129     dlg->fBookmarksInline->setChecked(KPalmDocSettings::bookmarksInline());
00130     dlg->fBookmarksEndtags->setChecked(KPalmDocSettings::bookmarksEndtags());
00131     dlg->fBookmarksBmk->setChecked(KPalmDocSettings::bookmarksBmk());
00132 
00133     // Handheld->PC page
00134     dlg->fPCBookmarks->setButton(KPalmDocSettings::bookmarksToPC() );
00135 }
00136 
00137 void ConverterDlg::slotClose()
00138 {
00139   writeSettings();
00140   kapp->quit();
00141   delete this;
00142 }
00143 
00144 void ConverterDlg::slotToText()
00145 {
00146     FUNCTIONSETUP;
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)), QString::null, i18n("Use Folder"), KStdGuiItem::cancel());
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)), QString::null, i18n("Use Folder"), KStdGuiItem::cancel());
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         DEBUGKPILOT<<"Pdbinfo.dir="<<pdbinfo.dir().absPath()<<endl;
00237         DEBUGKPILOT<<"txtinfo.dir="<<txtinfo.dir().absPath()<<endl;
00238         QStringList pdbfiles(pdbinfo.dir().entryList(CSL1("*.pdb")));
00239         QStringList converted_Files;
00240 
00241         DEBUGKPILOT<<"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)+CSL1(".txt");
00245             DEBUGKPILOT<<"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     FUNCTIONSETUP;
00293     // First, get the settings from the controls and initialize
00294     // the converter object
00295     bool compress=dlg->fCompress->isChecked();
00296     int bmks=0;
00297     if (dlg->fConvertBookmarks->isChecked())
00298     {
00299         if (dlg->fBookmarksInline->isChecked()) bmks|=DOCConverter::eBmkInline;
00300         if (dlg->fBookmarksEndtags->isChecked()) bmks|=DOCConverter::eBmkEndtags;
00301         if(dlg->fBookmarksBmk->isChecked()) bmks|=DOCConverter::eBmkDefaultBmkFile;
00302     }
00303     DOCConverter conv;
00304     conv.setBookmarkTypes(bmks);
00305     conv.setCompress(compress);
00306     conv.setSort(DOCConverter::eSortName);
00307 
00308 
00309     askOverwrite=dlg->fAskOverwrite->isChecked();
00310     verbose=dlg->fVerbose->isChecked();
00311 
00312 
00313     bool dir=dlg->fDirectories->isChecked();
00314     QString txturl=dlg->fTXTDir->url();
00315     QString pdburl=dlg->fPDBDir->url();
00316 
00317     QFileInfo txtinfo(txturl);
00318     QFileInfo pdbinfo(pdburl);
00319 
00320     if (dir)
00321     {
00322         if (txtinfo.isFile())
00323         {
00324             int res=KMessageBox::questionYesNo(this,
00325                 i18n("<qt>You selected to sync folders, "
00326                 "but gave a filename instead (<em>%1</em>)."
00327                 "<br>Use folder <em>%2</em> instead?</qt>").arg(txturl)
00328                 .arg(txtinfo.dirPath(true)), QString::null, i18n("Use Folder"), KStdGuiItem::cancel());
00329             if (res==KMessageBox::Yes)
00330             {
00331                 txturl=txtinfo.dirPath(true);
00332                 txtinfo.setFile(txturl);
00333             }
00334             else return;
00335         }
00336 
00337         if (!txtinfo.isDir() || !txtinfo.exists())
00338         {
00339             KMessageBox::sorry(this,
00340                 i18n("<qt>The folder <em>%1</em> for "
00341                 "the text files is not a valid folder.</qt>").arg(txturl));
00342             return;
00343         }
00344 
00345 
00346         // Now check the to directory:
00347         if (pdbinfo.isFile())
00348         {
00349             int res=KMessageBox::questionYesNo(this,
00350                 i18n("<qt>You selected to sync folders, "
00351                 "but gave a filename instead (<em>%1</em>)."
00352                 "<br>Use folder <em>%2</em> instead?</qt>")
00353                 .arg(pdburl)
00354                 .arg(pdbinfo.dirPath(true)), QString::null, i18n("Use Folder"), KStdGuiItem::cancel());
00355             if (res==KMessageBox::Yes) {
00356                 pdburl=pdbinfo.dirPath(true);
00357                 pdbinfo.setFile(pdburl);
00358             }
00359             else return;
00360         }
00361 
00362         // Now that we have a directory path, try to create it:
00363         if (!pdbinfo.isDir()) {
00364             pdbinfo.dir().mkdir(pdburl, true);
00365         }
00366         if (!pdbinfo.isDir()) {
00367             KMessageBox::sorry(this, i18n("<qt>The folder <em>%1</em> for "
00368                 "the PalmDOC files could not be created.</qt>").arg(pdburl));
00369             return;
00370         }
00371 
00372 
00373         // Now that we have both directories, create the converter object
00374         DEBUGKPILOT<<"Pdbinfo.dir="<<pdbinfo.dir().absPath()<<endl;
00375         DEBUGKPILOT<<"txtinfo.dir="<<txtinfo.dir().absPath()<<endl;
00376         QStringList txtfiles(txtinfo.dir().entryList(CSL1("*.txt")));
00377         QStringList converted_Files;
00378 
00379         DEBUGKPILOT<<"Length of filename list: "<<txtfiles.size()<<endl;
00380         for ( QStringList::Iterator it = txtfiles.begin(); it != txtfiles.end(); ++it )
00381         {
00382             QString pdbfile=QFileInfo(*it).baseName(true)+CSL1(".pdb");
00383             DEBUGKPILOT<<"pdbfile="<<pdbfile<<", pdbdir="<<pdburl<<", txtfile="<<*it<<", txtdir="<<txturl<<endl;
00384             if (convertTXTtoPDB(txturl, *it, pdburl, pdbfile, &conv))
00385             {
00386                 converted_Files.append(*it);
00387             }
00388         }
00389         if (converted_Files.size()>0) {
00390             KMessageBox::informationList(this, i18n("The following texts were "
00391                     "successfully converted:"), converted_Files, i18n("Conversion Successful"));
00392         }
00393         else
00394         {
00395             KMessageBox::sorry(this, i18n("No text files were converted correctly"));
00396         }
00397 
00398 
00399     } else { // no dir
00400 
00401 
00402         // Check the from file
00403         if (!txtinfo.isFile() || !txtinfo.exists())
00404         {
00405             KMessageBox::sorry(this, i18n("<qt>The file <em>%1</em> does not "
00406                 "exist.</qt>").arg(txturl));
00407             return;
00408         }
00409 
00410         if (convertTXTtoPDB(txtinfo.dirPath(true), txtinfo.fileName(),
00411                 pdbinfo.dirPath(true), pdbinfo.fileName(), &conv) )
00412         {
00413             KMessageBox::information(this, i18n("Conversion of file %1 successful.").arg(txturl));
00414         }
00415 
00416     }
00417 
00418 }
00419 
00420 
00421 void ConverterDlg::slotUser1()
00422 {
00423     KAboutApplication ab(KGlobal::instance()->aboutData(), this);
00424     ab.show();
00425     ab.exec();
00426     return;
00427 }
00428 
00429 void ConverterDlg::slotDirectories(bool dir)
00430 {
00431     FUNCTIONSETUP;
00432     DEBUGKPILOT<<"Slot Directories: "<<dir<<endl;
00433     if (dir)
00434     {
00435         dlg->fTextLabel->setText(i18n("&Text folder:"));
00436         dlg->fPdbLabel->setText(i18n("&PalmDOC folder:"));
00437         dlg->fTXTDir->setMode(KFile::LocalOnly | KFile::Directory);
00438         dlg->fPDBDir->setMode(KFile::LocalOnly | KFile::Directory);
00439     } else {
00440         dlg->fTextLabel->setText(i18n("&Text file:"));
00441         dlg->fPdbLabel->setText(i18n("&DOC file:"));
00442         dlg->fTXTDir->setMode(KFile::LocalOnly | KFile::File);
00443         dlg->fPDBDir->setMode(KFile::LocalOnly | KFile::File);
00444     }
00445 }
00446 
00447 bool ConverterDlg::convertTXTtoPDB(QString txtdir, QString txtfile,
00448         QString pdbdir, QString pdbfile, DOCConverter*conv)
00449 {
00450     FUNCTIONSETUP;
00451     bool res=false;
00452     QFileInfo dbfileinfo(pdbdir, pdbfile);
00453     DEBUGKPILOT<<"Working  on file "<<pdbfile<<endl;
00454     if (!dbfileinfo.exists() || !askOverwrite ||
00455             (KMessageBox::Yes==KMessageBox::questionYesNo(this,
00456             i18n("<qt>The database file <em>%1</em> already exists. Overwrite it?</qt>")
00457             .arg(dbfileinfo.filePath()), QString::null, i18n("Overwrite"), KStdGuiItem::cancel() ) ))
00458     {
00459         PilotLocalDatabase*pdbdb=new PilotLocalDatabase(pdbdir, QFileInfo(pdbfile).baseName(), false);
00460         if (pdbdb)
00461         {
00462             if (!pdbdb->isOpen())
00463             {
00464 #ifdef DEBUG
00465                 DEBUGKPILOT<<pdbfile<<" does not yet exist. Creating it"<<endl;
00466 #endif
00467                 if (!pdbdb->createDatabase(get_long("REAd"), get_long("TEXt")) ) {
00468                 }
00469             }
00470 
00471             if (pdbdb->isOpen())
00472             {
00473                 conv->setPDB(pdbdb);
00474                 conv->setTXTpath(txtdir, txtfile);
00475                 DEBUGKPILOT<<"Converting "<<txtfile<<" (dir "<<txtdir<<") to "<<dbfileinfo.filePath()<<endl;
00476                 if (conv->convertTXTtoPDB()) res=true;
00477             }
00478             delete pdbdb;
00479         }
00480         if ( !res && verbose )
00481         {
00482             KMessageBox::sorry(this, i18n("<qt>Error while converting the text %1.</qt>").arg(txtfile));
00483         }
00484     }
00485     else
00486     {
00487         DEBUGKPILOT<<"Ignoring the file "<<txtfile<<endl;
00488     }
00489     return res;
00490 }
00491 
00492 bool ConverterDlg::convertPDBtoTXT(QString pdbdir, QString pdbfile,
00493         QString txtdir, QString txtfile, DOCConverter*conv)
00494 {
00495     FUNCTIONSETUP;
00496     bool res=false;
00497     QFileInfo txtfileinfo(txtdir, txtfile);
00498     DEBUGKPILOT<<"Working  on file "<<txtfile<<endl;
00499     if (!txtfileinfo.exists() || !askOverwrite ||
00500             (KMessageBox::Yes==KMessageBox::questionYesNo(this,
00501             i18n("<qt>The text file <em>%1</em> already exists. Overwrite it?</qt>")
00502             .arg(txtfileinfo.filePath()), QString::null, i18n("Overwrite"), KStdGuiItem::cancel() ) ))
00503     {
00504         PilotLocalDatabase*pdbdb=new PilotLocalDatabase(pdbdir, QFileInfo(pdbfile).baseName(), false);
00505         if (pdbdb)
00506         {
00507             if (pdbdb->isOpen())
00508             {
00509                 conv->setPDB(pdbdb);
00510                 conv->setTXTpath(txtdir, txtfile);
00511                 DEBUGKPILOT<<"Converting "<<txtfile<<" (dir "<<txtdir<<") from "<<pdbfile<<" (dir "<<pdbdir<<")"<<endl;
00512                 if (conv->convertPDBtoTXT()) res=true;
00513             }
00514             delete pdbdb;
00515         }
00516         if ( !res && verbose )
00517         {
00518             KMessageBox::sorry(this, i18n("<qt>Error while converting the text %1.</qt>").arg(pdbfile));
00519         }
00520     }
00521     else
00522     {
00523         DEBUGKPILOT<<"Ignoring the file "<<pdbfile<<endl;
00524     }
00525     return res;
00526 
00527 }
00528 
00529 #include "kpalmdoc_dlg.moc"