~ubuntu-branches/ubuntu/lucid/skrooge/lucid

« back to all changes in this revision

Viewing changes to skrooge_importexport/skgimportexportplugin.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Mercatante
  • Date: 2009-05-02 06:47:15 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20090502064715-b7l4eciitw3asos3
Tags: 0.2.7-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
        SKGTRACEIN(10, "SKGImportExportPlugin::~SKGImportExportPlugin");
47
47
        parent=NULL;
48
48
        currentBankDocument=NULL;
 
49
 
 
50
        importQifAction=NULL;
 
51
        importOfxAction=NULL;
 
52
        importCsvAction=NULL;
 
53
        importCsvUnitAction=NULL;
 
54
        exportCsvAction=NULL;
 
55
        exportQifAction=NULL;
 
56
        processingFoundTransfert=NULL;
 
57
        processingBankPerfect=NULL;
 
58
        processingBank=NULL;
49
59
}
50
60
 
51
61
void SKGImportExportPlugin::setupActions(SKGMainPanel* iParent, SKGDocument* iDocument, const QStringList& iArgument)
69
79
        overlayofx.push_back("skrooge");
70
80
 
71
81
        importOfxAction = new KAction(KIcon("document-import", NULL, overlayofx), i18n("Import &OFX..."), this);
72
 
        connect(importOfxAction, SIGNAL(triggered(bool)), SLOT(importOfx()));
 
82
        connect(importOfxAction, SIGNAL(triggered(bool)), SLOT(import()));
73
83
        actionCollection()->addAction( QLatin1String("import_ofx"), importOfxAction );
74
84
        imports->addAction(importOfxAction);
75
85
 
76
 
        iParent->registedGlobalAction("import_ofx", importOfxAction);
 
86
        if (parent) parent->registedGlobalAction("import_ofx", importOfxAction);
77
87
 
78
88
        //Import QIF
79
89
        QStringList overlaycsv;
80
90
        overlaycsv.push_back("text-csv");
81
91
 
82
92
        importQifAction = new KAction(KIcon("document-import"), i18n("Import &QIF..."), this);
83
 
        connect(importQifAction, SIGNAL(triggered(bool)), SLOT(importQif()));
 
93
        connect(importQifAction, SIGNAL(triggered(bool)), SLOT(import()));
84
94
        actionCollection()->addAction( QLatin1String("import_qif"), importQifAction );
85
95
        imports->addAction(importQifAction);
86
96
 
87
 
        iParent->registedGlobalAction("import_qif", importQifAction);
 
97
        if (parent) parent->registedGlobalAction("import_qif", importQifAction);
88
98
 
89
99
        //Import CSV
90
100
        importCsvAction = new KAction(KIcon("document-import", NULL, overlaycsv), i18n("Import &CSV..."), this);
91
 
        connect(importCsvAction, SIGNAL(triggered(bool)), SLOT(importCsv()));
 
101
        connect(importCsvAction, SIGNAL(triggered(bool)), SLOT(import()));
92
102
        actionCollection()->addAction( QLatin1String("import_csv"), importCsvAction );
93
103
        imports->addAction(importCsvAction);
94
104
 
95
 
        iParent->registedGlobalAction("import_csv", importCsvAction);
 
105
        if (parent) parent->registedGlobalAction("import_csv", importCsvAction);
96
106
 
97
107
        //Import CSV Unit
98
108
        importCsvUnitAction = new KAction(KIcon("document-import", NULL, overlaycsv), i18n("Import CSV &Unit..."), this);
99
 
        connect(importCsvUnitAction, SIGNAL(triggered(bool)), SLOT(importCsvUnit()));
 
109
        connect(importCsvUnitAction, SIGNAL(triggered(bool)), SLOT(import()));
100
110
        actionCollection()->addAction( QLatin1String("import_csv_unit"), importCsvUnitAction );
101
111
        imports->addAction(importCsvUnitAction);
102
112
 
103
 
        iParent->registedGlobalAction("import_csv_unit", importCsvUnitAction);
 
113
        if (parent) parent->registedGlobalAction("import_csv_unit", importCsvUnitAction);
104
114
 
105
115
        //Exports
106
116
        KActionMenu* exports=new  KActionMenu(KIcon("document-export"),i18n("Export"), this);
112
122
        actionCollection()->addAction( QLatin1String("export_qif"), exportQifAction );
113
123
        exports->addAction(exportQifAction);
114
124
 
115
 
        iParent->registedGlobalAction("export_qif", exportQifAction);
 
125
        if (parent) parent->registedGlobalAction("export_qif", exportQifAction);
116
126
 
117
127
        //Export CSV
118
128
        exportCsvAction = new KAction(KIcon("document-export", NULL, overlaycsv), i18n("Export &CSV..."), this);
120
130
        actionCollection()->addAction( QLatin1String("export_csv"), exportCsvAction );
121
131
        exports->addAction(exportCsvAction);
122
132
 
123
 
        iParent->registedGlobalAction("export_csv", exportCsvAction);
 
133
        if (parent) parent->registedGlobalAction("export_csv", exportCsvAction);
124
134
 
125
135
        //Processing
126
136
        KActionMenu* processing=new  KActionMenu(KIcon("tools-wizard"),i18n("Processing"), this);
135
145
        actionCollection()->addAction( QLatin1String("process_foundtransfer"), processingFoundTransfert );
136
146
        processing->addAction(processingFoundTransfert);
137
147
 
138
 
        iParent->registedGlobalAction("process_foundtransfer", processingFoundTransfert);
 
148
        if (parent) parent->registedGlobalAction("process_foundtransfer", processingFoundTransfert);
139
149
 
140
150
        //Processing bankperfect
141
151
        processingBankPerfect = new KAction(KIcon("tools-wizard"), i18n("Clean BankPerfect's imports"), this);
143
153
        actionCollection()->addAction( QLatin1String("process_bankperfect"), processingBankPerfect );
144
154
        processing->addAction(processingBankPerfect);
145
155
 
146
 
        iParent->registedGlobalAction("process_bankperfect", processingBankPerfect);
 
156
        if (parent) parent->registedGlobalAction("process_bankperfect", processingBankPerfect);
147
157
 
148
158
        //Processing banks
149
159
        processingBank = new KAction(KIcon("tools-wizard"), i18n("Clean bank's imports"), this);
151
161
        actionCollection()->addAction( QLatin1String("process_banks"), processingBank );
152
162
        processing->addAction(processingBank);
153
163
 
154
 
        iParent->registedGlobalAction("process_banks", processingBank);
 
164
        if (parent) parent->registedGlobalAction("process_banks", processingBank);
155
165
 
156
166
}
157
167
 
193
203
{
194
204
        SKGTRACEIN(10, "SKGImportExportPlugin::refresh");
195
205
 
196
 
        bool test=(currentBankDocument->getDatabase()!=NULL);
197
 
        importQifAction->setEnabled(test);
198
 
        importOfxAction->setEnabled(test);
199
 
        importCsvAction->setEnabled(test);
200
 
        exportQifAction->setEnabled(test);
201
 
        exportCsvAction->setEnabled(test);
202
 
        processingFoundTransfert->setEnabled(test);
203
 
        processingBankPerfect->setEnabled(test);
204
 
        processingBank->setEnabled(test);
 
206
        if (currentBankDocument) {
 
207
                bool test=(currentBankDocument->getDatabase()!=NULL);
 
208
                if (importQifAction) importQifAction->setEnabled(test);
 
209
                if (importOfxAction) importOfxAction->setEnabled(test);
 
210
                if (importCsvAction) importCsvAction->setEnabled(test);
 
211
                if (exportQifAction) exportQifAction->setEnabled(test);
 
212
                if (exportCsvAction) exportCsvAction->setEnabled(test);
 
213
                if (importCsvUnitAction) importCsvUnitAction->setEnabled(test);
 
214
                if (processingFoundTransfert) processingFoundTransfert->setEnabled(test);
 
215
                if (processingBankPerfect) processingBankPerfect->setEnabled(test);
 
216
                if (processingBank) processingBank->setEnabled(test);
 
217
        }
205
218
}
206
219
 
207
220
void SKGImportExportPlugin::close()
209
222
        SKGTRACEIN(10, "SKGImportExportPlugin::close");
210
223
}
211
224
 
212
 
void SKGImportExportPlugin::importOfx()
213
 
{
214
 
        SKGError err;
215
 
        SKGTRACEINRC(10, "SKGImportExportPlugin::importOfx",err);
216
 
        import(SKGImportExportManager::OFX);
217
 
}
218
 
 
219
 
void SKGImportExportPlugin::importQif()
220
 
{
221
 
        SKGError err;
222
 
        SKGTRACEINRC(10, "SKGImportExportPlugin::importQif",err);
223
 
        import(SKGImportExportManager::QIF);
224
 
}
225
 
 
226
 
void SKGImportExportPlugin::importCsv()
227
 
{
228
 
        SKGError err;
229
 
        SKGTRACEINRC(10, "SKGImportExportPlugin::importCsv",err);
230
 
        import(SKGImportExportManager::CSV);
231
 
}
232
 
 
233
 
void SKGImportExportPlugin::importCsvUnit()
234
 
{
235
 
        SKGError err;
236
 
        SKGTRACEINRC(10, "SKGImportExportPlugin::importCsvUnit",err);
237
 
        import(SKGImportExportManager::CSVUNIT);
238
 
}
239
 
 
240
 
void SKGImportExportPlugin::import(SKGImportExportManager::ImportExportMode iMode)
 
225
void SKGImportExportPlugin::import()
241
226
{
242
227
        SKGError err;
243
228
        SKGTRACEINRC(10, "SKGImportExportPlugin::import",err);
244
 
        QString lastCodecUsed=currentBankDocument->getParameter("SKG_LAST_CODEC_USED_FOR_IMPORT");
245
 
        if (lastCodecUsed.length()==0) lastCodecUsed=QTextCodec::codecForLocale()->name();
246
 
 
247
 
        KEncodingFileDialog::Result result=KEncodingFileDialog::getOpenFileNamesAndEncoding(lastCodecUsed,
248
 
                                           "kfiledialog:///IMPEXP",
249
 
                                           iMode==SKGImportExportManager::CSV || iMode==SKGImportExportManager::CSVUNIT ?
250
 
                                           "*.csv|"+i18n("CSV Files") :
251
 
                                           iMode==SKGImportExportManager::OFX ? "*.ofx|"+i18n("OFX Files") : "*.qif|"+i18n("QIF Files") ,
252
 
                                           parent);
253
 
        QSet<QString> fileNames=result.fileNames.toSet();
254
 
        QString codec=result.encoding;
255
 
 
256
 
        int nbFiles=fileNames.count();
257
 
        if (nbFiles) {
258
 
 
259
 
                QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
260
 
                {
261
 
                        SKGBEGINPROGRESSTRANSACTION(*currentBankDocument, i18n("Import %1 with codec %2",
262
 
                                                    (iMode==SKGImportExportManager::CSV || iMode==SKGImportExportManager::CSVUNIT ? i18n("CSV") :
263
 
                                                     iMode==SKGImportExportManager::OFX ? i18n("OFX") : i18n("QIF")), codec), err, nbFiles);
264
 
 
265
 
                        if (err.isSucceeded()) err=currentBankDocument->setParameter("SKG_LAST_CODEC_USED_FOR_IMPORT", codec);
266
 
 
267
 
                        QSetIterator<QString> f(fileNames);
268
 
                        int i=1;
269
 
                        while (err.isSucceeded() && f.hasNext()) {
270
 
                                QString fileName=f.next();
271
 
                                SKGImportExportManager imp1((SKGDocumentBank*) currentBankDocument, iMode, fileName);
272
 
                                imp1.setCodec(codec);
273
 
                                err=imp1.importFile();
274
 
                                if (err.isFailed()) {
275
 
                                        err.addError(ERR_FAIL, i18n("Import of [%1] failed", fileName));
276
 
                                } else {
277
 
                                        err=SKGError(0, i18n("File [%1] successfully imported.", fileName));
 
229
        if (currentBankDocument) {
 
230
                QAction* sender=static_cast<QAction*> ( this->sender() );
 
231
                SKGImportExportManager::ImportExportMode iMode=SKGImportExportManager::OFX;
 
232
 
 
233
                if (sender==importQifAction) iMode=SKGImportExportManager::QIF;
 
234
                else if (sender==importCsvAction) iMode=SKGImportExportManager::CSV;
 
235
                else if (sender==importCsvUnitAction) iMode=SKGImportExportManager::CSVUNIT;
 
236
 
 
237
                QString lastCodecUsed=currentBankDocument->getParameter("SKG_LAST_CODEC_USED_FOR_IMPORT");
 
238
                if (lastCodecUsed.length()==0) lastCodecUsed=QTextCodec::codecForLocale()->name();
 
239
 
 
240
                KEncodingFileDialog::Result result=KEncodingFileDialog::getOpenFileNamesAndEncoding(lastCodecUsed,
 
241
                                                   "kfiledialog:///IMPEXP",
 
242
                                                   iMode==SKGImportExportManager::CSV || iMode==SKGImportExportManager::CSVUNIT ?
 
243
                                                   "*.csv|"+i18n("CSV Files") :
 
244
                                                   iMode==SKGImportExportManager::OFX ? "*.ofx|"+i18n("OFX Files") : "*.qif|"+i18n("QIF Files") ,
 
245
                                                   parent);
 
246
                QSet<QString> fileNames=result.fileNames.toSet();
 
247
                QString codec=result.encoding;
 
248
 
 
249
                int nbFiles=fileNames.count();
 
250
                if (nbFiles) {
 
251
 
 
252
                        QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
 
253
                        {
 
254
                                SKGBEGINPROGRESSTRANSACTION(*currentBankDocument, i18n("Import %1 with codec %2",
 
255
                                                            (iMode==SKGImportExportManager::CSV || iMode==SKGImportExportManager::CSVUNIT ? i18n("CSV") :
 
256
                                                             iMode==SKGImportExportManager::OFX ? i18n("OFX") : i18n("QIF")), codec), err, nbFiles);
 
257
 
 
258
                                if (err.isSucceeded()) err=currentBankDocument->setParameter("SKG_LAST_CODEC_USED_FOR_IMPORT", codec);
 
259
 
 
260
                                QSetIterator<QString> f(fileNames);
 
261
                                int i=1;
 
262
                                while (err.isSucceeded() && f.hasNext()) {
 
263
                                        QString fileName=f.next();
 
264
                                        SKGImportExportManager imp1((SKGDocumentBank*) currentBankDocument, iMode, fileName);
 
265
                                        imp1.setCodec(codec);
 
266
                                        err=imp1.importFile();
 
267
                                        if (err.isFailed()) {
 
268
                                                err.addError(ERR_FAIL, i18n("Import of [%1] failed", fileName));
 
269
                                        } else {
 
270
                                                err=SKGError(0, i18n("File [%1] successfully imported.", fileName));
 
271
                                        }
 
272
 
 
273
                                        if (err.isSucceeded()) err=currentBankDocument->stepForward(i);
 
274
                                        i++;
278
275
                                }
279
 
 
280
 
                                if (err.isSucceeded()) err=currentBankDocument->stepForward(i);
281
 
                                i++;
282
276
                        }
283
 
                }
284
 
                QApplication::restoreOverrideCursor();
285
 
        }
286
 
 
287
 
        //status bar
288
 
        if (err.isSucceeded() && nbFiles>1) {
289
 
                err=SKGError(0, i18n("%1 files successfully imported.", nbFiles));
290
 
        }
291
 
 
292
 
        //Display error
293
 
        parent->displayErrorMessage(err);
 
277
                        QApplication::restoreOverrideCursor();
 
278
                }
 
279
 
 
280
                //status bar
 
281
                if (err.isSucceeded() && nbFiles>1) {
 
282
                        err=SKGError(0, i18n("%1 files successfully imported.", nbFiles));
 
283
                }
 
284
 
 
285
                //Display error
 
286
                if (parent) parent->displayErrorMessage(err);
 
287
        }
294
288
}
295
289
 
296
290
void SKGImportExportPlugin::exportCsv()
298
292
        SKGError err;
299
293
        SKGTRACEINRC(10, "SKGImportExportPlugin::exportCsv",err);
300
294
        QString fileName=SKGMainPanel::getSaveFileName(KUrl("kfiledialog:///IMPEXP"), "*.csv|"+i18n("CSV Files") , parent);
301
 
        if (fileName.isEmpty()) return;
 
295
        if (fileName.isEmpty() || !currentBankDocument) return;
302
296
 
303
297
        QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
304
298
        {
315
309
        else err.addError(ERR_FAIL, i18n("Export of [%1] failed", fileName));
316
310
 
317
311
        //Display error
318
 
        parent->displayErrorMessage(err);
 
312
        if (parent) parent->displayErrorMessage(err);
319
313
}
320
314
 
321
315
void SKGImportExportPlugin::exportQif()
323
317
        SKGError err;
324
318
        SKGTRACEINRC(10, "SKGImportExportPlugin::exportQif",err);
325
319
        QString fileName=SKGMainPanel::getSaveFileName(KUrl("kfiledialog:///IMPEXP"), "*.qif|"+i18n("QIF Files") , parent);
326
 
        if (fileName.isEmpty()) return;
 
320
        if (fileName.isEmpty() || !currentBankDocument) return;
327
321
 
328
322
        QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
329
323
        {
340
334
        else err.addError(ERR_FAIL, i18n("Export of [%1] failed", fileName));
341
335
 
342
336
        //Display error
343
 
        parent->displayErrorMessage(err);
 
337
        if (parent) parent->displayErrorMessage(err);
344
338
}
345
339
 
346
340
void SKGImportExportPlugin::foundTransfer()
347
341
{
348
342
        SKGError err;
349
343
        SKGTRACEINRC(10, "SKGImportExportPlugin::foundTransfer",err);
350
 
        QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
351
 
        int NbOperationsMerged=0;
352
 
        {
353
 
                SKGBEGINTRANSACTION(*currentBankDocument, i18n("Find and group transfers"), err);
354
 
                if (err.isSucceeded()) {
355
 
                        SKGImportExportManager imp1((SKGDocumentBank*) currentBankDocument, SKGImportExportManager::QIF, "");
356
 
                        err=imp1.findAndGroupTransfers(NbOperationsMerged);
 
344
        if (currentBankDocument) {
 
345
                QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
 
346
                int NbOperationsMerged=0;
 
347
                {
 
348
                        SKGBEGINTRANSACTION(*currentBankDocument, i18n("Find and group transfers"), err);
 
349
                        if (err.isSucceeded()) {
 
350
                                SKGImportExportManager imp1((SKGDocumentBank*) currentBankDocument, SKGImportExportManager::QIF, "");
 
351
                                err=imp1.findAndGroupTransfers(NbOperationsMerged);
 
352
                        }
357
353
                }
 
354
                QApplication::restoreOverrideCursor();
 
355
 
 
356
                //status bar
 
357
                if (err.isSucceeded()) err=SKGError(0, i18n("Document successfully processed. %1 operations merged.", NbOperationsMerged));
 
358
                else err.addError(ERR_FAIL, i18n("Processing failed."));
 
359
 
 
360
                //Display error
 
361
                if (parent) parent->displayErrorMessage(err);
358
362
        }
359
 
        QApplication::restoreOverrideCursor();
360
 
 
361
 
        //status bar
362
 
        if (err.isSucceeded()) err=SKGError(0, i18n("Document successfully processed. %1 operations merged.", NbOperationsMerged));
363
 
        else err.addError(ERR_FAIL, i18n("Processing failed."));
364
 
 
365
 
        //Display error
366
 
        parent->displayErrorMessage(err);
367
363
}
368
364
 
369
365
void SKGImportExportPlugin::cleanBankPerfect()
370
366
{
371
367
        SKGError err;
372
368
        SKGTRACEINRC(10, "SKGImportExportPlugin::cleanBankPerfect",err);
373
 
        QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
374
 
        {
375
 
                SKGBEGINTRANSACTION(*currentBankDocument, i18n("Clean BankPerfect's imports"), err);
376
 
                if (err.isSucceeded()) {
377
 
                        SKGImportExportManager imp1((SKGDocumentBank*) currentBankDocument, SKGImportExportManager::QIF, "");
378
 
                        err=imp1.cleanBankPerfectImport();
 
369
        if (currentBankDocument) {
 
370
                QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
 
371
                {
 
372
                        SKGBEGINTRANSACTION(*currentBankDocument, i18n("Clean BankPerfect's imports"), err);
 
373
                        if (err.isSucceeded()) {
 
374
                                SKGImportExportManager imp1((SKGDocumentBank*) currentBankDocument, SKGImportExportManager::QIF, "");
 
375
                                err=imp1.cleanBankPerfectImport();
 
376
                        }
379
377
                }
 
378
                QApplication::restoreOverrideCursor();
 
379
 
 
380
                //status bar
 
381
                if (err.isSucceeded()) err=SKGError(0, i18n("Document successfully cleaned."));
 
382
                else err.addError(ERR_FAIL, i18n("Clean failed."));
 
383
 
 
384
                //Display error
 
385
                if (parent) parent->displayErrorMessage(err);
380
386
        }
381
 
        QApplication::restoreOverrideCursor();
382
 
 
383
 
        //status bar
384
 
        if (err.isSucceeded()) err=SKGError(0, i18n("Document successfully cleaned."));
385
 
        else err.addError(ERR_FAIL, i18n("Clean failed."));
386
 
 
387
 
        //Display error
388
 
        parent->displayErrorMessage(err);
389
387
}
390
388
 
391
389
void SKGImportExportPlugin::cleanBanks()
407
405
        else err.addError(ERR_FAIL, i18n("Clean failed."));
408
406
 
409
407
        //Display error
410
 
        parent->displayErrorMessage(err);
 
408
        if (parent) parent->displayErrorMessage(err);
411
409
}
412
410
#include "skgimportexportplugin.moc"