summaryrefslogtreecommitdiff
path: root/noncore/tools/pimconverter/converter.cpp
blob: e8bd4753647b0eb804f6ad6de1ddebd2c4767c43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include "converter.h"

#include <stdlib.h> // For "system()" command

/* OPIE */
#include <opie2/oapplicationfactory.h>
#include <opie2/odebug.h>
#include <opie2/opimglobal.h>
#include <qpe/global.h>
// Include SQL related header files
#define __USE_SQL
#include <opie2/opimaccessfactory.h>

/* QT */
#include <qdatetime.h>
#include <qprogressbar.h>
#include <qcombobox.h>
#include <qcheckbox.h>
#include <qmessagebox.h>


OPIE_EXPORT_APP( Opie::Core::OApplicationFactory<Converter> )

using namespace Opie;
using namespace Pim;

Converter::Converter(QWidget *p, const char* n,  WFlags fl):
    converter_base( p, n, fl ),
    m_selectedDatabase( ADDRESSBOOK ),
    m_selectedSourceFormat( XML ),
    m_selectedDestFormat( SQL ),
    m_criticalState( false )
{
    m_dataBaseSelector -> setCurrentItem( m_selectedDatabase );
    m_sourceFormatSelector -> setCurrentItem( m_selectedSourceFormat );
    m_destFormatSelector -> setCurrentItem( m_selectedDestFormat );
    m_eraseDB -> setChecked( true );  // Default erase on copy
}

void Converter::selectedDatabase( int num )
{
    m_selectedDatabase = num;
}

void Converter::selectedDestFormat( int num )
{
    m_selectedDestFormat = num;
}

void Converter::selectedSourceFormat( int num )
{
    m_selectedSourceFormat = num;
}

void Converter::start_conversion(){

    // Creating backends to the requested databases..
    OPimBase* sourceDB;
    OPimBase* destDB;

    odebug << "SourceFormat: " <<  m_selectedSourceFormat << oendl;
    odebug << "DestFormat: " << m_selectedDestFormat << oendl;
    if ( m_selectedSourceFormat == m_selectedDestFormat ){

	    QMessageBox::warning( this, tr("PimConverter"),
				  tr( "<qt>It is not a good idea to use"
				      "the same source and destformat !</qt>"));
	    return;
    }

    switch( m_selectedSourceFormat ){
    case XML:
        odebug << "XMLSourceDB = " << m_selectedDatabase << "" << oendl;
        switch( m_selectedDatabase ){
        case ADDRESSBOOK:{
            sourceDB = OPimAccessFactory<OPimContactAccess>::create( OPimGlobal::CONTACTLIST, OPimGlobal::XML, "converter" );
	}
            break;
        case TODOLIST:{
            sourceDB = OPimAccessFactory<OPimTodoAccess>::create( OPimGlobal::TODOLIST, OPimGlobal::XML, "converter" );
            }break;
        case DATEBOOK:{
            sourceDB = OPimAccessFactory<ODateBookAccess>::create( OPimGlobal::DATEBOOK, OPimGlobal::XML, "converter" );
                }
            break;
        default:
            owarn << "Unknown database selected (" << m_selectedDatabase << ")" << oendl;
            return;
        }
        break;
    case SQL:
        odebug << "SQLSourceDB = " << m_selectedDatabase << "" << oendl;
        switch( m_selectedDatabase ){
        case ADDRESSBOOK:{
            sourceDB = OPimAccessFactory<OPimContactAccess>::create( OPimGlobal::CONTACTLIST, OPimGlobal::SQL, "converter" );
                }
            break;
        case TODOLIST:{
            sourceDB = OPimAccessFactory<OPimTodoAccess>::create( OPimGlobal::TODOLIST, OPimGlobal::SQL, "converter" );
            }break;
        case DATEBOOK:{
            sourceDB = OPimAccessFactory<ODateBookAccess>::create( OPimGlobal::DATEBOOK, OPimGlobal::SQL, "converter" );
                }
            break;
        default:
            owarn << "Unknown database selected (" << m_selectedDatabase << ")" << oendl;
            return;
        }
        break;
    default:
        owarn << "Unknown source format selected (" << m_selectedSourceFormat << ") !!" << oendl;
        return;
    }

    switch ( m_selectedDestFormat ){
    case XML:
        odebug << "XMLDestDB = " << m_selectedDatabase << "" << oendl;
        switch( m_selectedDatabase ){
        case ADDRESSBOOK:{
            destDB = OPimAccessFactory<OPimContactAccess>::create( OPimGlobal::CONTACTLIST, OPimGlobal::XML, "converter" );
                }
            break;
        case TODOLIST:{
            destDB = OPimAccessFactory<OPimTodoAccess>::create( OPimGlobal::TODOLIST, OPimGlobal::XML, "converter" );
            }break;
        case DATEBOOK:{
            destDB = OPimAccessFactory<ODateBookAccess>::create( OPimGlobal::DATEBOOK, OPimGlobal::XML, "converter" );
                }
            break;
        default:
            owarn << "Unknown database selected (" << m_selectedDatabase << ")" << oendl;
            return;
        }
        break;
    case SQL:
        odebug << "SQLDestDB = " << m_selectedDatabase << "" << oendl;
        switch( m_selectedDatabase ){
        case ADDRESSBOOK:{
            destDB = OPimAccessFactory<OPimContactAccess>::create( OPimGlobal::CONTACTLIST, OPimGlobal::SQL, "converter" );
                }
            break;
        case TODOLIST:{
            destDB = OPimAccessFactory<OPimTodoAccess>::create( OPimGlobal::TODOLIST, OPimGlobal::SQL, "converter" );
            }break;
        case DATEBOOK:{
            destDB = OPimAccessFactory<ODateBookAccess>::create( OPimGlobal::DATEBOOK, OPimGlobal::SQL, "converter" );
                }
            break;
        default:
            owarn << "Unknown database selected (" << m_selectedDatabase << ")" << oendl;
            return;
        }
        break;
    default:
        owarn << "Unknown destination format selected (" << m_selectedDestFormat << ")!!" << oendl;
        return;
    }

    if ( !sourceDB || !destDB )
        return;

    m_criticalState = true;

    sourceDB -> load();
    destDB -> load();

    QTime t;
    t.start();

    // Clean the dest-database if requested (isChecked)
    if ( m_eraseDB -> isChecked() ){
        odebug << "Clearing destination database!" << oendl;
        destDB -> clear();
    }

    // Now transmit every pim-item from the source database to the destination -database
    QArray<int> uidList = sourceDB->records();
    odebug << "Try to move data for addressbook.. (" << uidList.count() << " items) " << oendl;
    m_progressBar->setTotalSteps( uidList.count() );
    int count = 0;
    for ( uint i = 0; i < uidList.count(); ++i ){
        odebug << "Adding uid: " << uidList[i] << "" << oendl;
        OPimRecord* rec = sourceDB -> record( uidList[i] );
        destDB -> add( rec );
        m_progressBar->setProgress( ++count );
    }

    // Now commit data..
    destDB -> save();

    m_criticalState = false;

    // Delete the frontends. Backends will be deleted automatically, too !
    // We have to cast them back to delete them properly !
    switch( m_selectedDatabase ){
    case ADDRESSBOOK:
        delete static_cast<OPimContactAccess*> (sourceDB);
        delete static_cast<OPimContactAccess*> (destDB);
        break;
    case TODOLIST:
        delete static_cast<OPimTodoAccess*> (sourceDB);
        delete static_cast<OPimTodoAccess*> (destDB);
        break;
    case DATEBOOK:
        delete static_cast<ODateBookAccess*> (sourceDB);
        delete static_cast<ODateBookAccess*> (destDB);
        break;
    default:
        owarn << "Unknown database selected (" << m_selectedDatabase << ")" << oendl;
        return;
    }


    owarn << "Conversion is finished and needed " << t.elapsed() << " ms !" << oendl;
}

void Converter::closeEvent( QCloseEvent *e )
{

	/* Due to the fact that we don't have multitasking here, this
	 * critical handling don't make sense, but the future..
	 */
	if ( m_criticalState ){
		e->ignore();
		return;
	}
	e->accept();
}

bool Converter::sqliteMoveAndConvert( const QString& name, const QString& src, const QString &dest )
{

	QMessageBox::information( this, tr( "Pim-Converter" ),
				  tr( "<qt>Starting to convert the database for %1</qt>" ).arg( name ) );


    bool error = false;
    QString cmd;
    if (!QFile::exists( src ) ) {
        cmd = tr( "No SQLite2 database could be found!" );
        error = true;
    }

    if( QFile::exists( dest ) ) {
        cmd = tr( "The database is already converted!" );
        error = true;
    }


    if ( error ){
		QMessageBox::critical( this, tr("Pim-Converter"),
                                       tr("<qt>Conversion not possible: \n"
                                          "Problem: %1</qt>").arg(cmd) );
		return error;
    }
	    

    /*
     * Move it over
     */
    cmd = "mv " + Global::shellQuote(src) + " " + Global::shellQuote(dest);
    if( ::system( cmd ) != 0 ){
	    error = true;
    }


    /*
     * Convert it
     */
    if ( !error ){
	    cmd = "sqlite " + Global::shellQuote(dest) + " .dump | sqlite3 " + Global::shellQuote(src);
	    if ( ::system( cmd ) != 0 ){
		    error = true;
	    }
    }

    if ( error ){
	    QMessageBox::critical( this, tr("Pim-Converter"),
				   tr("<qt>An internal error occurred: "
				      "Converting the database was impossible! "
				      "Command: '%1' </qt>").arg(cmd) );
	    
    }
}



void Converter::start_upgrade()
{
	odebug << "Start upgrading" << oendl;
	switch( QMessageBox::warning( this, tr("Pim-Converter"),
				      tr("<qt>Are you really sure that you "
				      "want to convert your database from "
				      "sqlite V2 to sqlite V3?</qt>"),
				      QMessageBox::Ok | QMessageBox::Default,
				      QMessageBox::Abort | QMessageBox::Escape )) {

	case QMessageBox::Abort: // Abort clicked or Escape pressed
		// abort
		return;
		break;
	}
	odebug << "Checking whether sqlite is installed" << oendl;
	if ( system( "which sqlite" ) != 0 ){
		QMessageBox::critical( this, tr("Pim-Converter"),
				       tr("<qt>An internal error occurred: "
				       "sqlite was not accessible! "
				       "Please correct the PATH or install "
				       "this packages!</qt>") );
		return;
	}
	if ( system( "which sqlite3" ) != 0 ){
                QMessageBox::critical( this, tr("Pim-Converter"),
                                       tr("<qt>An internal error occurred: "
                                          "sqlite3 was not accessible! "
                                          "Please correct the PATH or install "
                                          "this packages!</qt>") );
		return;
        }

        QString src, dest;
	bool error = false;

        src  = Global::applicationFileName("addressbook", "addressbook.db"    );
        dest = Global::applicationFileName("addressbook", "addressbook.db_v2" );
	error = sqliteMoveAndConvert( "Addressbook", src, dest );

        src  = Global::applicationFileName("datebook", "datebook.db"    );
        dest = Global::applicationFileName("datebook", "datebook.db_v2" );
	error = sqliteMoveAndConvert( "Datebook", src, dest );
	

        src  = Global::applicationFileName("todolist", "todolist.db"    );
        dest = Global::applicationFileName("todolist", "todolist.db_v2" );
	error = sqliteMoveAndConvert( "TodoList", src, dest );

	if ( !error ){
		QMessageBox::information( this, tr("Pim-Converter"),
					  tr("Conversion is finished!"),
					  QMessageBox::Ok );
	}
}