summaryrefslogtreecommitdiff
path: root/noncore/tools/pimconverter/converter.cpp
blob: e9de3c3a00aef984928aaad1eb70b49f471c5e95 (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
#include "converter.h"

/* OPIE */
#include <opie2/oapplicationfactory.h>
#include <opie2/odebug.h>
#include <opie2/opimglobal.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, "PimConverter",
				  tr( "It is not a good idea to use\n" )
				  +tr( "the same source and destformat !" ),
				  tr( "Ok" ) );
	    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();
}