-rw-r--r-- | pwmanager/pwmanager/pwm.cpp | 85 |
1 files changed, 83 insertions, 2 deletions
diff --git a/pwmanager/pwmanager/pwm.cpp b/pwmanager/pwmanager/pwm.cpp index 66d26d6..ac0c978 100644 --- a/pwmanager/pwmanager/pwm.cpp +++ b/pwmanager/pwmanager/pwm.cpp @@ -53,12 +53,13 @@ #include "pwm.h" #include "pwminit.h" #include "pwmprint.h" #include "addentrywndimpl.h" #include "globalstuff.h" #include "findwndimpl.h" +#include "csv.h" #ifdef CONFIG_KWALLETIF # include "kwalletif.h" # include "kwalletemu.h" #endif #ifdef CONFIG_KEYCARD @@ -110,21 +111,23 @@ enum { enum { BUTTON_POPUP_OPTIONS_CONFIG = 0 }; // Button IDs for "export" popup menu (in "file" popup menu) enum { BUTTON_POPUP_EXPORT_TEXT = 0, - BUTTON_POPUP_EXPORT_GPASMAN + BUTTON_POPUP_EXPORT_GPASMAN, + BUTTON_POPUP_EXPORT_CSV #ifdef CONFIG_KWALLETIF ,BUTTON_POPUP_EXPORT_KWALLET #endif }; // Button IDs for "import" popup menu (in "file" popup menu) enum { BUTTON_POPUP_IMPORT_TEXT = 0, - BUTTON_POPUP_IMPORT_GPASMAN + BUTTON_POPUP_IMPORT_GPASMAN, + BUTTON_POPUP_IMPORT_CSV #ifdef CONFIG_KWALLETIF ,BUTTON_POPUP_IMPORT_KWALLET #endif }; #ifdef PWM_EMBEDDED @@ -178,16 +181,18 @@ PwM::PwM(PwMInit *_init, PwMDoc *doc, updateCaption(); showStatMsg(i18n("Ready.")); } PwM::~PwM() { + //qDebug("PwM::~PwM()"); disconnect(curDoc(), SIGNAL(docClosed(PwMDoc *)), this, SLOT(docClosed(PwMDoc *))); conf()->confWndMainWndSize(size()); emit closed(this); + //qDebug("PwM::~PwM() emited closed(this)"); delete view; } void PwM::initMenubar() { KIconLoader* picons; @@ -238,24 +243,28 @@ void PwM::initMenubar() filePopup->insertSeparator(); // "file/export" popup menu exportPopup->insertItem(i18n("&Text-file..."), this, SLOT(exportToText()), 0, BUTTON_POPUP_EXPORT_TEXT); exportPopup->insertItem(i18n("&Gpasman / Kpasman ..."), this, SLOT(exportToGpasman()), 0, BUTTON_POPUP_EXPORT_GPASMAN); + exportPopup->insertItem(i18n("&CSV (Comma Separated Value) ..."), this, + SLOT(exportToCsv()), 0, BUTTON_POPUP_EXPORT_CSV); #ifdef CONFIG_KWALLETIF exportPopup->insertItem(i18n("&KWallet..."), this, SLOT(exportToKWallet()), 0, BUTTON_POPUP_EXPORT_KWALLET); #endif filePopup->insertItem(QIconSet(picons->loadIcon("fileexport", KIcon::Small)), i18n("E&xport"), exportPopup, BUTTON_POPUP_FILE_EXPORT); // "file/import" popup menu importPopup->insertItem(i18n("&Text-file..."), this, SLOT(importFromText()), 0, BUTTON_POPUP_IMPORT_TEXT); importPopup->insertItem(i18n("&Gpasman / Kpasman ..."), this, SLOT(importFromGpasman()), 0, BUTTON_POPUP_IMPORT_GPASMAN); + importPopup->insertItem(i18n("&CSV (Comma Separated Value) ..."), this, + SLOT(importCsv()), 0, BUTTON_POPUP_IMPORT_CSV); #ifdef CONFIG_KWALLETIF importPopup->insertItem(i18n("&KWallet..."), this, SLOT(importKWallet()), 0, BUTTON_POPUP_IMPORT_KWALLET); #endif filePopup->insertItem(QIconSet(picons->loadIcon("fileimport", KIcon::Small)), i18n("I&mport"), importPopup, @@ -1013,12 +1022,84 @@ void PwM::exportToGpasman() i18n("error while writing")); } else showStatMsg(i18n("Successfully exported data.")); curDoc()->timer()->putLock(DocTimer::id_autoLockTimer); } + + +void PwM::exportToCsv() +{ + PWM_ASSERT(curDoc()); + if (curDoc()->isDocEmpty()) { + KMessageBox::information(this, + i18n + ("Sorry, there is nothing to export;\n" + "please add some passwords first."), + i18n("Nothing to Do")); + return; + } + + curDoc()->timer()->getLock(DocTimer::id_autoLockTimer); + QString fn(KFileDialog::getSaveFileName("*.csv", i18n("*|CSV Text File"), this)); + if (fn.isEmpty()) { + curDoc()->timer()->putLock(DocTimer::id_autoLockTimer); + return; + } + + Csv csv(this); + if (!csv.exportData(fn, curDoc())) { + curDoc()->timer()->putLock(DocTimer::id_autoLockTimer); + showStatMsg(i18n("CSV file export failed.")); + return; + } + showStatMsg(i18n("Successfully exported data.")); + curDoc()->timer()->putLock(DocTimer::id_autoLockTimer); +} + +bool PwM::importCsv() +{ + Csv csv(this); + if (!isVirgin()) { + if (KMessageBox::questionYesNo(this, + i18n("Do you want to import the data\n" + "into the current document? (If you\n" + "select \"no\", a new document will be\n" + "opened.)"), + i18n("Import into This Document?")) + == KMessageBox::No) { + // import the data to a new window. + PwM *newInstance = init->createMainWnd(); + bool ok = newInstance->importCsv(); + if (!ok) { + newInstance->setForceQuit(true); + delete_and_null(newInstance); + } + return ok; + } + } + + QString filename = KFileDialog::getOpenFileName("*.csv", i18n("*|CSV Text File"), this); + if (filename.isEmpty()) + return false; + curDoc()->timer()->getLock(DocTimer::id_autoLockTimer); + if (!csv.importData(filename, curDoc())) { + curDoc()->timer()->putLock(DocTimer::id_autoLockTimer); + showStatMsg(i18n("CSV file import failed.")); + return false; + } + curDoc()->timer()->putLock(DocTimer::id_autoLockTimer); + KMessageBox::information(this, + i18n("Successfully imported the CSV data\n" + "into the current document."), i18n("Successfully Imported")); + showStatMsg(i18n("Successfully imported")); + setVirgin(false); + return true; +} + + void PwM::exportToKWallet() { #ifdef CONFIG_KWALLETIF if (!checkAndAskForKWalletEmu()) return; PWM_ASSERT(curDoc()); |