Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

converter.cpp

Go to the documentation of this file.
00001 #include "converter.h"
00002 
00003 #include <stdlib.h> // For "system()" command
00004 
00005 /* OPIE */
00006 #include <opie2/oapplicationfactory.h>
00007 #include <opie2/odebug.h>
00008 #include <opie2/opimglobal.h>
00009 #include <qpe/global.h>
00010 // Include SQL related header files
00011 #define __USE_SQL
00012 #include <opie2/opimaccessfactory.h>
00013 
00014 /* QT */
00015 #include <qdatetime.h>
00016 #include <qprogressbar.h>
00017 #include <qcombobox.h>
00018 #include <qcheckbox.h>
00019 #include <qmessagebox.h>
00020 
00021 
00022 OPIE_EXPORT_APP( Opie::Core::OApplicationFactory<Converter> )
00023 
00024 using namespace Opie;
00025 using namespace Pim;
00026 
00027 Converter::Converter(QWidget *p, const char* n,  WFlags fl):
00028     converter_base( p, n, fl ),
00029     m_selectedDatabase( ADDRESSBOOK ),
00030     m_selectedSourceFormat( XML ),
00031     m_selectedDestFormat( SQL ),
00032     m_criticalState( false )
00033 {
00034     m_dataBaseSelector -> setCurrentItem( m_selectedDatabase );
00035     m_sourceFormatSelector -> setCurrentItem( m_selectedSourceFormat );
00036     m_destFormatSelector -> setCurrentItem( m_selectedDestFormat );
00037     m_eraseDB -> setChecked( true );  // Default erase on copy
00038 }
00039 
00040 void Converter::selectedDatabase( int num )
00041 {
00042     m_selectedDatabase = num;
00043 }
00044 
00045 void Converter::selectedDestFormat( int num )
00046 {
00047     m_selectedDestFormat = num;
00048 }
00049 
00050 void Converter::selectedSourceFormat( int num )
00051 {
00052     m_selectedSourceFormat = num;
00053 }
00054 
00055 void Converter::start_conversion(){
00056 
00057     // Creating backends to the requested databases..
00058     OPimBase* sourceDB;
00059     OPimBase* destDB;
00060 
00061     odebug << "SourceFormat: " <<  m_selectedSourceFormat << oendl;
00062     odebug << "DestFormat: " << m_selectedDestFormat << oendl;
00063     if ( m_selectedSourceFormat == m_selectedDestFormat ){
00064 
00065             QMessageBox::warning( this, tr("PimConverter"),
00066                                   tr( "<qt>It is not a good idea to use"
00067                                       "the same source and destformat !</qt>"));
00068             return;
00069     }
00070 
00071     switch( m_selectedSourceFormat ){
00072     case XML:
00073         odebug << "XMLSourceDB = " << m_selectedDatabase << "" << oendl;
00074         switch( m_selectedDatabase ){
00075         case ADDRESSBOOK:{
00076             sourceDB = OPimAccessFactory<OPimContactAccess>::create( OPimGlobal::CONTACTLIST, OPimGlobal::XML, "converter" );
00077         }
00078             break;
00079         case TODOLIST:{
00080             sourceDB = OPimAccessFactory<OPimTodoAccess>::create( OPimGlobal::TODOLIST, OPimGlobal::XML, "converter" );
00081             }break;
00082         case DATEBOOK:{
00083             sourceDB = OPimAccessFactory<ODateBookAccess>::create( OPimGlobal::DATEBOOK, OPimGlobal::XML, "converter" );
00084                 }
00085             break;
00086         default:
00087             owarn << "Unknown database selected (" << m_selectedDatabase << ")" << oendl;
00088             return;
00089         }
00090         break;
00091     case SQL:
00092         odebug << "SQLSourceDB = " << m_selectedDatabase << "" << oendl;
00093         switch( m_selectedDatabase ){
00094         case ADDRESSBOOK:{
00095             sourceDB = OPimAccessFactory<OPimContactAccess>::create( OPimGlobal::CONTACTLIST, OPimGlobal::SQL, "converter" );
00096                 }
00097             break;
00098         case TODOLIST:{
00099             sourceDB = OPimAccessFactory<OPimTodoAccess>::create( OPimGlobal::TODOLIST, OPimGlobal::SQL, "converter" );
00100             }break;
00101         case DATEBOOK:{
00102             sourceDB = OPimAccessFactory<ODateBookAccess>::create( OPimGlobal::DATEBOOK, OPimGlobal::SQL, "converter" );
00103                 }
00104             break;
00105         default:
00106             owarn << "Unknown database selected (" << m_selectedDatabase << ")" << oendl;
00107             return;
00108         }
00109         break;
00110     default:
00111         owarn << "Unknown source format selected (" << m_selectedSourceFormat << ") !!" << oendl;
00112         return;
00113     }
00114 
00115     switch ( m_selectedDestFormat ){
00116     case XML:
00117         odebug << "XMLDestDB = " << m_selectedDatabase << "" << oendl;
00118         switch( m_selectedDatabase ){
00119         case ADDRESSBOOK:{
00120             destDB = OPimAccessFactory<OPimContactAccess>::create( OPimGlobal::CONTACTLIST, OPimGlobal::XML, "converter" );
00121                 }
00122             break;
00123         case TODOLIST:{
00124             destDB = OPimAccessFactory<OPimTodoAccess>::create( OPimGlobal::TODOLIST, OPimGlobal::XML, "converter" );
00125             }break;
00126         case DATEBOOK:{
00127             destDB = OPimAccessFactory<ODateBookAccess>::create( OPimGlobal::DATEBOOK, OPimGlobal::XML, "converter" );
00128                 }
00129             break;
00130         default:
00131             owarn << "Unknown database selected (" << m_selectedDatabase << ")" << oendl;
00132             return;
00133         }
00134         break;
00135     case SQL:
00136         odebug << "SQLDestDB = " << m_selectedDatabase << "" << oendl;
00137         switch( m_selectedDatabase ){
00138         case ADDRESSBOOK:{
00139             destDB = OPimAccessFactory<OPimContactAccess>::create( OPimGlobal::CONTACTLIST, OPimGlobal::SQL, "converter" );
00140                 }
00141             break;
00142         case TODOLIST:{
00143             destDB = OPimAccessFactory<OPimTodoAccess>::create( OPimGlobal::TODOLIST, OPimGlobal::SQL, "converter" );
00144             }break;
00145         case DATEBOOK:{
00146             destDB = OPimAccessFactory<ODateBookAccess>::create( OPimGlobal::DATEBOOK, OPimGlobal::SQL, "converter" );
00147                 }
00148             break;
00149         default:
00150             owarn << "Unknown database selected (" << m_selectedDatabase << ")" << oendl;
00151             return;
00152         }
00153         break;
00154     default:
00155         owarn << "Unknown destination format selected (" << m_selectedDestFormat << ")!!" << oendl;
00156         return;
00157     }
00158 
00159     if ( !sourceDB || !destDB )
00160         return;
00161 
00162     m_criticalState = true;
00163 
00164     sourceDB -> load();
00165     destDB -> load();
00166 
00167     QTime t;
00168     t.start();
00169 
00170     // Clean the dest-database if requested (isChecked)
00171     if ( m_eraseDB -> isChecked() ){
00172         odebug << "Clearing destination database!" << oendl;
00173         destDB -> clear();
00174     }
00175 
00176     // Now transmit every pim-item from the source database to the destination -database
00177     QArray<int> uidList = sourceDB->records();
00178     odebug << "Try to move data for addressbook.. (" << uidList.count() << " items) " << oendl;
00179     m_progressBar->setTotalSteps( uidList.count() );
00180     int count = 0;
00181     for ( uint i = 0; i < uidList.count(); ++i ){
00182         odebug << "Adding uid: " << uidList[i] << "" << oendl;
00183         OPimRecord* rec = sourceDB -> record( uidList[i] );
00184         destDB -> add( rec );
00185         m_progressBar->setProgress( ++count );
00186     }
00187 
00188     // Now commit data..
00189     destDB -> save();
00190 
00191     m_criticalState = false;
00192 
00193     // Delete the frontends. Backends will be deleted automatically, too !
00194     // We have to cast them back to delete them properly !
00195     switch( m_selectedDatabase ){
00196     case ADDRESSBOOK:
00197         delete static_cast<OPimContactAccess*> (sourceDB);
00198         delete static_cast<OPimContactAccess*> (destDB);
00199         break;
00200     case TODOLIST:
00201         delete static_cast<OPimTodoAccess*> (sourceDB);
00202         delete static_cast<OPimTodoAccess*> (destDB);
00203         break;
00204     case DATEBOOK:
00205         delete static_cast<ODateBookAccess*> (sourceDB);
00206         delete static_cast<ODateBookAccess*> (destDB);
00207         break;
00208     default:
00209         owarn << "Unknown database selected (" << m_selectedDatabase << ")" << oendl;
00210         return;
00211     }
00212 
00213 
00214     owarn << "Conversion is finished and needed " << t.elapsed() << " ms !" << oendl;
00215 }
00216 
00217 void Converter::closeEvent( QCloseEvent *e )
00218 {
00219 
00220         /* Due to the fact that we don't have multitasking here, this
00221          * critical handling don't make sense, but the future..
00222          */
00223         if ( m_criticalState ){
00224                 e->ignore();
00225                 return;
00226         }
00227         e->accept();
00228 }
00229 
00230 bool Converter::sqliteMoveAndConvert( const QString& name, const QString& src, const QString &dest )
00231 {
00232 
00233         QMessageBox::information( this, tr( "Pim-Converter" ),
00234                                   tr( "<qt>Starting to convert the database for %1</qt>" ).arg( name ) );
00235 
00236 
00237     bool error = false;
00238     QString cmd;
00239     if (!QFile::exists( src ) ) {
00240         cmd = tr( "No SQLite2 database could be found!" );
00241         error = true;
00242     }
00243 
00244     if( QFile::exists( dest ) ) {
00245         cmd = tr( "The database is already converted!" );
00246         error = true;
00247     }
00248 
00249 
00250     if ( error ){
00251                 QMessageBox::critical( this, tr("Pim-Converter"),
00252                                        tr("<qt>Conversion not possible: <br>"
00253                                           "Problem: %1</qt>").arg(cmd) );
00254                 return error;
00255     }
00256             
00257 
00258     /*
00259      * Move it over
00260      */
00261     cmd = "mv " + Global::shellQuote(src) + " " + Global::shellQuote(dest);
00262     if( ::system( cmd ) != 0 ){
00263             error = true;
00264     }
00265 
00266 
00267     /*
00268      * Convert it
00269      */
00270     if ( !error ){
00271             cmd = "sqlite " + Global::shellQuote(dest) + " .dump | sqlite3 " + Global::shellQuote(src);
00272             if ( ::system( cmd ) != 0 ){
00273                     error = true;
00274             }
00275     }
00276 
00277 
00278     /*
00279      * Check whether conversion really worked. If not, move old database back to recover it
00280      */
00281     if ( !QFile::exists( src ) ){
00282             cmd = "mv " + Global::shellQuote(dest) + " " + Global::shellQuote(src);
00283             if ( ::system( cmd ) != 0 ){
00284             }
00285             error = true;
00286             cmd = "Database-Format is not V2!?";            
00287     }
00288 
00289     if ( error ){
00290             QMessageBox::critical( this, tr("Pim-Converter"),
00291                                    tr("<qt>An internal error occurred: <br>"
00292                                       "Converting the database was impossible! <br>"
00293                                       "Command/Reason: '%1' </qt>").arg(cmd) );
00294             
00295     }
00296 
00297 
00298 }
00299 
00300 
00301 
00302 void Converter::start_upgrade()
00303 {
00304         odebug << "Start upgrading" << oendl;
00305         switch( QMessageBox::warning( this, tr("Pim-Converter"),
00306                                       tr("<qt>Are you really sure that you "
00307                                       "want to convert your database from "
00308                                       "sqlite V2 to sqlite V3?</qt>"),
00309                                       QMessageBox::Ok | QMessageBox::Default,
00310                                       QMessageBox::Abort | QMessageBox::Escape )) {
00311 
00312         case QMessageBox::Abort: // Abort clicked or Escape pressed
00313                 // abort
00314                 return;
00315                 break;
00316         }
00317         odebug << "Checking whether sqlite is installed" << oendl;
00318         if ( system( "which sqlite" ) != 0 ){
00319                 QMessageBox::critical( this, tr("Pim-Converter"),
00320                                        tr("<qt>An internal error occurred: "
00321                                        "sqlite was not accessible! "
00322                                        "Please correct the PATH or install "
00323                                        "this packages!</qt>") );
00324                 return;
00325         }
00326         if ( system( "which sqlite3" ) != 0 ){
00327                 QMessageBox::critical( this, tr("Pim-Converter"),
00328                                        tr("<qt>An internal error occurred: "
00329                                           "sqlite3 was not accessible! "
00330                                           "Please correct the PATH or install "
00331                                           "this packages!</qt>") );
00332                 return;
00333         }
00334 
00335         QString src, dest;
00336         bool error = false;
00337 
00338         src  = Global::applicationFileName("addressbook", "addressbook.db"    );
00339         dest = Global::applicationFileName("addressbook", "addressbook.db_v2" );
00340         error = sqliteMoveAndConvert( "Addressbook", src, dest );
00341 
00342         src  = Global::applicationFileName("datebook", "datebook.db"    );
00343         dest = Global::applicationFileName("datebook", "datebook.db_v2" );
00344         error = sqliteMoveAndConvert( "Datebook", src, dest );
00345         
00346 
00347         src  = Global::applicationFileName("todolist", "todolist.db"    );
00348         dest = Global::applicationFileName("todolist", "todolist.db_v2" );
00349         error = sqliteMoveAndConvert( "TodoList", src, dest );
00350 
00351         if ( !error ){
00352                 QMessageBox::information( this, tr("Pim-Converter"),
00353                                           tr("Conversion is finished!"),
00354                                           QMessageBox::Ok );
00355         }
00356 }

Generated on Sat Nov 5 16:18:02 2005 for OPIE by  doxygen 1.4.2