27 #include <QDomElement>
29 #include <QDomDocument>
30 #include <QXmlStreamWriter>
34 #include <kfiledialog.h>
35 #include <kinputdialog.h>
36 #include <kio/netaccess.h>
37 #include <kactioncollection.h>
40 #include <kmessagebox.h>
42 #include <ksqueezedtextlabel.h>
43 #include <kstandarddirs.h>
44 #include <kstandardaction.h>
46 #include <ktemporaryfile.h>
47 #include <kxmlguifactory.h>
50 #include <QPushButton>
53 #include <QTextStream>
55 #include <QVBoxLayout>
57 #include <QToolButton>
63 #include <KTreeWidgetSearchLine>
65 QAction *KWalletEditor::_newFolderAction =0;
66 QAction *KWalletEditor::_deleteFolderAction =0;
67 QAction *KWalletEditor::_exportAction =0;
68 QAction *KWalletEditor::_saveAsAction =0;
69 QAction *KWalletEditor::_mergeAction =0;
70 QAction *KWalletEditor::_importAction =0;
71 KAction *KWalletEditor::_newEntryAction =0;
72 KAction *KWalletEditor::_renameEntryAction =0;
73 KAction *KWalletEditor::_deleteEntryAction =0;
74 KAction *KWalletEditor::_copyPassAction =0;
75 QAction *KWalletEditor::_alwaysShowContentsAction =0;
76 QAction *KWalletEditor::_alwaysHideContentsAction =0;
82 :
QWidget(parent), _displayedItem(0), _actionCollection(0), _alwaysShowContents(false) {
84 setObjectName( QLatin1String( name ) );
86 _splitter->setStretchFactor(0, 1);
87 _splitter->setStretchFactor(1, 2);
88 _contextMenu =
new KMenu(
this);
90 _undoChanges->setIcon(KIcon( QLatin1String(
"edit-undo" )));
91 _saveChanges->setIcon(KIcon( QLatin1String(
"document-save" )));
93 QVBoxLayout *box =
new QVBoxLayout(_entryListFrame);
96 _entryList->setContextMenuPolicy(Qt::CustomContextMenu);
97 _searchLine =
new KTreeWidgetSearchLine(_entryListFrame, _entryList);
98 _searchLine->setClickMessage(i18n(
"Search"));
99 connect(_searchLine, SIGNAL(textChanged(QString)),
this, SLOT(onSearchTextChanged(QString)));
100 box->addWidget(_searchLine);
101 box->addWidget(_entryList);
103 _entryStack->setEnabled(
true);
105 box =
new QVBoxLayout(_entryStack->widget(2));
107 _mapEditorShowHide =
new QCheckBox(i18n(
"&Show values"), _entryStack->widget(2));
108 connect(_mapEditorShowHide, SIGNAL(toggled(
bool)),
this, SLOT(showHideMapEditorValue(
bool)));
109 _mapEditor =
new KWMapEditor(_currentMap, _entryStack->widget(2));
110 box->addWidget(_mapEditorShowHide);
111 box->addWidget(_mapEditor);
114 KConfigGroup cg(KGlobal::config(),
"WalletEditor");
115 QList<int> splitterSize = cg.readEntry(
"SplitterSize", QList<int>());
116 if (splitterSize.size() != 2) {
117 splitterSize.clear();
118 splitterSize.append(_splitter->width()/2);
119 splitterSize.append(_splitter->width()/2);
121 _splitter->setSizes(splitterSize);
122 _alwaysShowContents = cg.readEntry(
"AlwaysShowContents",
false);
124 _searchLine->setFocus();
129 SIGNAL(customContextMenuRequested(QPoint)),
131 SLOT(listContextMenuRequested(QPoint)));
137 connect(_passwordValue, SIGNAL(textChanged()),
138 this, SLOT(entryEditted()));
139 connect(_mapEditor, SIGNAL(dirty()),
140 this, SLOT(entryEditted()));
142 connect(_undoChanges, SIGNAL(clicked()),
143 this, SLOT(restoreEntry()));
144 connect(_saveChanges, SIGNAL(clicked()),
145 this, SLOT(saveEntry()));
147 connect(_showContents, SIGNAL(clicked()),
148 this, SLOT(showPasswordContents()));
149 connect(_hideContents, SIGNAL(clicked()),
150 this, SLOT(hidePasswordContents()));
161 KConfigGroup cg(KGlobal::config(),
"WalletEditor");
162 cg.writeEntry(
"SplitterSize", _splitter->sizes());
163 cg.writeEntry(
"AlwaysShowContents", _alwaysShowContents);
177 Q_ASSERT(wallet != 0);
183 connect(_w, SIGNAL(walletOpened(
bool)),
this, SLOT(walletOpened(
bool)));
185 connect(_w, SIGNAL(folderUpdated(QString)),
this, SLOT(updateEntries(QString)));
186 connect(_w, SIGNAL(folderListUpdated()),
this, SLOT(updateFolderList()));
193 _mapEditorShowHide->setChecked(
false);
194 showHideMapEditorValue(
false);
197 _searchLine->setFocus();
200 KActionCollection* KWalletEditor::actionCollection()
202 if (_actionCollection == 0) {
203 _actionCollection =
new KActionCollection(
this);
205 return _actionCollection;
208 void KWalletEditor::createActions(KActionCollection* actionCollection) {
209 _newFolderAction = actionCollection->addAction( QLatin1String(
"create_folder" ));
210 _newFolderAction->setText( i18n(
"&New Folder...") );
211 _newFolderAction->setIcon( KIcon( QLatin1String(
"folder-new" )) );
213 _deleteFolderAction = actionCollection->addAction( QLatin1String(
"delete_folder" ) );
214 _deleteFolderAction->setText( i18n(
"&Delete Folder") );
216 _mergeAction = actionCollection->addAction( QLatin1String(
"wallet_merge" ));
217 _mergeAction->setText( i18n(
"&Import Wallet...") );
219 _importAction= actionCollection->addAction( QLatin1String(
"wallet_import" ) );
220 _importAction->setText( i18n(
"&Import XML...") );
222 _exportAction = actionCollection->addAction( QLatin1String(
"wallet_export" ) );
223 _exportAction->setText( i18n(
"&Export as XML...") );
225 _saveAsAction = KStandardAction::saveAs(0, 0, actionCollection);
227 _copyPassAction = actionCollection->addAction( QLatin1String(
"copy_action" ) );
228 _copyPassAction->setText( i18n(
"&Copy") );
229 _copyPassAction->setShortcut( Qt::Key_C + Qt::CTRL );
230 _copyPassAction->setEnabled(
false);
232 _newEntryAction = actionCollection->addAction( QLatin1String(
"new_entry" ) );
233 _newEntryAction->setText( i18n(
"&New..." ) );
234 _newEntryAction->setShortcut( Qt::Key_Insert );
235 _newEntryAction->setEnabled(
false);
237 _renameEntryAction = actionCollection->addAction( QLatin1String(
"rename_entry" ) );
238 _renameEntryAction->setText( i18n(
"&Rename" ) );
239 _renameEntryAction->setShortcut( Qt::Key_F2 );
240 _renameEntryAction->setEnabled(
false);
242 _deleteEntryAction = actionCollection->addAction( QLatin1String(
"delete_entry" ) );
243 _deleteEntryAction->setText( i18n(
"&Delete" ) );
244 _deleteEntryAction->setShortcut( Qt::Key_Delete );
245 _deleteEntryAction->setEnabled(
false);
247 _alwaysShowContentsAction = actionCollection->addAction( QLatin1String(
"always_show_contents" ));
248 _alwaysShowContentsAction->setText( i18n(
"Always show contents") );
249 _alwaysShowContentsAction->setCheckable(
true);
251 _alwaysHideContentsAction = actionCollection->addAction( QLatin1String(
"always_hide_contents") );
252 _alwaysHideContentsAction->setText( i18n(
"Always hide contents") );
253 _alwaysHideContentsAction->setCheckable(
true);
256 void KWalletEditor::connectActions()
258 connect(_newFolderAction, SIGNAL(triggered(
bool)), SLOT(
createFolder()));
259 connect(
this, SIGNAL(
enableFolderActions(
bool)), _newFolderAction, SLOT(setEnabled(
bool)));
261 connect(_deleteFolderAction, SIGNAL(triggered(
bool)), SLOT(
deleteFolder()));
263 connect(
this, SIGNAL(
enableFolderActions(
bool)), _deleteFolderAction, SLOT(setEnabled(
bool)));
265 connect(_mergeAction, SIGNAL(triggered(
bool)), SLOT(importWallet()));
268 connect(_importAction, SIGNAL(triggered(
bool)), SLOT(importXML()));
271 connect(_exportAction, SIGNAL(triggered(
bool)), SLOT(exportXML()));
274 connect(_saveAsAction, SIGNAL(triggered(
bool)), SLOT(saveAs()));
277 connect(_newEntryAction, SIGNAL(triggered(
bool)), SLOT(newEntry()));
279 connect(_renameEntryAction, SIGNAL(triggered(
bool)), SLOT(renameEntry()));
281 connect(_deleteEntryAction, SIGNAL(triggered(
bool)), SLOT(deleteEntry()));
283 connect(_copyPassAction, SIGNAL(triggered(
bool)), SLOT(copyPassword()));
286 _showContents->addAction(_alwaysShowContentsAction);
287 _alwaysShowContentsAction->setChecked(_alwaysShowContents);
288 connect(_alwaysShowContentsAction, SIGNAL(triggered(
bool)), SLOT(onAlwaysShowContents(
bool)));
290 _hideContents->addAction(_alwaysHideContentsAction);
291 _alwaysHideContentsAction->setChecked(!_alwaysShowContents);
292 connect(_alwaysHideContentsAction, SIGNAL(triggered(
bool)), SLOT(onAlwaysHideContents(
bool)));
295 void KWalletEditor::disconnectActions()
297 disconnect(_newFolderAction, SIGNAL(triggered(
bool)),
this, SLOT(
createFolder()));
298 disconnect(
this, SIGNAL(
enableFolderActions(
bool)), _newFolderAction, SLOT(setEnabled(
bool)));
300 disconnect(_deleteFolderAction, SIGNAL(triggered(
bool)),
this, SLOT(
deleteFolder()));
302 disconnect(
this, SIGNAL(
enableFolderActions(
bool)), _deleteFolderAction, SLOT(setEnabled(
bool)));
304 disconnect(_mergeAction, SIGNAL(triggered(
bool)),
this, SLOT(importWallet()));
307 disconnect(_importAction, SIGNAL(triggered(
bool)),
this, SLOT(importXML()));
308 disconnect(
this, SIGNAL(
enableWalletActions(
bool)), _importAction, SLOT(setEnabled(
bool)));
310 disconnect(_exportAction, SIGNAL(triggered(
bool)),
this, SLOT(exportXML()));
311 disconnect(
this, SIGNAL(
enableWalletActions(
bool)), _exportAction, SLOT(setEnabled(
bool)));
313 disconnect(_saveAsAction, SIGNAL(triggered(
bool)),
this, SLOT(saveAs()));
314 disconnect(
this, SIGNAL(
enableWalletActions(
bool)), _saveAsAction, SLOT(setEnabled(
bool)));
316 disconnect(_newEntryAction, SIGNAL(triggered(
bool)),
this, SLOT(newEntry()));
318 disconnect(_renameEntryAction, SIGNAL(triggered(
bool)),
this, SLOT(renameEntry()));
320 disconnect(_deleteEntryAction, SIGNAL(triggered(
bool)),
this, SLOT(deleteEntry()));
322 disconnect(_copyPassAction, SIGNAL(triggered(
bool)),
this, SLOT(copyPassword()));
323 disconnect(
this, SIGNAL(
enableWalletActions(
bool)), _copyPassAction, SLOT(setEnabled(
bool)));
325 disconnect(_alwaysShowContentsAction, SIGNAL(triggered(
bool)),
this, SLOT(onAlwaysShowContents(
bool)));
326 disconnect(_alwaysHideContentsAction, SIGNAL(triggered(
bool)),
this, SLOT(onAlwaysHideContents(
bool)));
337 void KWalletEditor::updateFolderList(
bool checkEntries) {
338 const QStringList fl = _w->folderList();
339 QStack<QTreeWidgetItem*> trash;
341 for (
int i = 0; i < _entryList->topLevelItemCount(); ++i) {
346 if (!fl.contains(fi->
name())) {
354 for (QStringList::const_iterator i = fl.begin(); i != fl.end(); ++i) {
363 const QStringList entries = _w->entryList();
371 for (QStringList::const_iterator j = entries.begin(); j != entries.end(); ++j) {
372 switch (_w->entryType(*j)) {
373 case KWallet::Wallet::Password:
376 case KWallet::Wallet::Stream:
379 case KWallet::Wallet::Map:
382 case KWallet::Wallet::Unknown:
388 _entryList->setEnabled(
true);
392 if (!fl.contains(_currentFolder)) {
393 _currentFolder.clear();
394 _entryTitle->clear();
408 int rc = KMessageBox::warningContinueCancel(
this, i18n(
"Are you sure you wish to delete the folder '%1' from the wallet?", fi->
name()),QString(),KStandardGuiItem::del());
409 if (rc == KMessageBox::Continue) {
410 bool rc = _w->removeFolder(fi->
name());
412 KMessageBox::sorry(
this, i18n(
"Error deleting folder."));
415 _currentFolder.clear();
416 _entryTitle->clear();
431 n = KInputDialog::getText(i18n(
"New Folder"),
432 i18n(
"Please choose a name for the new folder:"),
442 int rc = KMessageBox::questionYesNo(
this, i18n(
"Sorry, that folder name is in use. Try again?"), QString(), KGuiItem(i18n(
"Try Again")), KGuiItem(i18n(
"Do Not Try")));
456 void KWalletEditor::saveEntry() {
459 _saveChanges->setEnabled(
false);
460 _undoChanges->setEnabled(
false);
462 if (item && _w && item->parent()) {
465 if (ci->
entryType() == KWallet::Wallet::Password) {
466 rc = _w->writePassword(item->text(0), _passwordValue->toPlainText());
467 }
else if (ci->
entryType() == KWallet::Wallet::Map) {
469 rc = _w->writeMap(item->text(0), _currentMap);
480 KMessageBox::sorry(
this, i18n(
"Error saving entry. Error code: %1", rc));
484 void KWalletEditor::restoreEntry() {
485 entrySelectionChanged(_entryList->currentItem());
489 void KWalletEditor::entryEditted() {
490 _saveChanges->setEnabled(
true);
491 _undoChanges->setEnabled(
true);
497 if ( _saveChanges->isEnabled() && _displayedItem && (_displayedItem != item) ){
499 i18n(
"The contents of the current item has changed.\nDo you want to save changes?"))) {
502 _saveChanges->setEnabled(
false);
503 _undoChanges->setEnabled(
false);
510 _contextMenu->clear();
511 _contextMenu->setEnabled(
true);
513 _newEntryAction->setEnabled(
false);
514 _renameEntryAction->setEnabled(
false);
515 _deleteEntryAction->setEnabled(
false);
520 _contextMenu->addTitle(_contextMenu->fontMetrics().elidedText(
521 item->text(0), Qt::ElideMiddle, 200 ) );
524 switch (item->type()) {
534 _w->setFolder(fi->
name());
535 _deleteFolderAction->setEnabled(
false);
538 _contextMenu->addAction( _newEntryAction );
539 _contextMenu->addAction( _renameEntryAction );
540 _contextMenu->addAction( _deleteEntryAction );
541 _newEntryAction->setEnabled(
true);
542 _renameEntryAction->setEnabled(
true);
543 _deleteEntryAction->setEnabled(
true);
545 if (ci->
entryType() == KWallet::Wallet::Password) {
547 if (_w->readPassword(item->text(0), pass) == 0) {
548 _entryStack->setCurrentIndex(4);
549 _entryName->setText(i18n(
"Password: %1",
551 _passwordValue->setText(pass);
552 _saveChanges->setEnabled(
false);
553 _undoChanges->setEnabled(
false);
556 _contextMenu->addSeparator();
557 _contextMenu->addAction( _copyPassAction );
558 if(_alwaysShowContents) {
559 QTimer::singleShot(0,
this, SLOT(showPasswordContents()));
561 }
else if (ci->
entryType() == KWallet::Wallet::Map) {
562 _entryStack->setCurrentIndex(2);
563 if (_w->readMap(item->text(0), _currentMap) == 0) {
565 _entryName->setText(i18n(
"Name-Value Map: %1", item->text(0)));
566 _saveChanges->setEnabled(
false);
567 _undoChanges->setEnabled(
false);
568 showHideMapEditorValue(_mapEditorShowHide->isChecked());
570 }
else if (ci->
entryType() == KWallet::Wallet::Stream) {
571 _entryStack->setCurrentIndex(3);
573 if (_w->readEntry(item->text(0), ba) == 0) {
574 _entryName->setText(i18n(
"Binary Data: %1",
576 _saveChanges->setEnabled(
false);
577 _undoChanges->setEnabled(
false);
587 if (ci->
entryType() == KWallet::Wallet::Unknown) {
589 _contextMenu->setEnabled(
false);
592 _contextMenu->addAction( _newEntryAction );
593 _newEntryAction->setEnabled(
true);
600 _w->setFolder(fi->
name());
601 _deleteFolderAction->setEnabled(
false);
603 _entryStack->setCurrentIndex(0);
608 _contextMenu->addAction( _newFolderAction );
609 _contextMenu->addAction( _deleteFolderAction );
615 _w->setFolder(fi->
name());
616 _deleteFolderAction->setEnabled(
true);
618 _entryStack->setCurrentIndex(0);
624 _contextMenu->addAction( _deleteEntryAction );
625 _deleteEntryAction->setEnabled(
true);
630 _contextMenu->addAction( _newFolderAction );
634 _currentFolder = fi->
name();
635 _entryTitle->setText(QString::fromLatin1(
"<font size=\"+1\">%1</font>").arg(fi->text(0)));
636 _iconTitle->setPixmap(fi->
getFolderIcon(KIconLoader::Toolbar));
639 _displayedItem = item;
642 void KWalletEditor::updateEntries(
const QString& folder) {
643 QStack<QTreeWidgetItem*> trash;
645 _w->setFolder(folder);
646 const QStringList entries = _w->entryList();
660 for (
int i = 0; i < pi->childCount(); ++i) {
662 if (!entries.contains(twi->text(0))) {
663 if (twi == _entryList->currentItem()) {
664 entrySelectionChanged(0L);
670 for (
int i = 0; i < mi->childCount(); ++i) {
672 if (!entries.contains(twi->text(0))) {
673 if (twi == _entryList->currentItem()) {
674 entrySelectionChanged(0L);
680 for (
int i = 0; i < bi->childCount(); ++i) {
682 if (!entries.contains(twi->text(0))) {
683 if (twi == _entryList->currentItem()) {
684 entrySelectionChanged(0L);
690 for (
int i = 0; i < ui->childCount(); ++i) {
692 if (!entries.contains(twi->text(0))) {
693 if (twi == _entryList->currentItem()) {
694 entrySelectionChanged(0L);
704 for (QStringList::const_iterator i = entries.begin(); i != entries.end(); ++i) {
709 switch (_w->entryType(*i)) {
710 case KWallet::Wallet::Password:
713 case KWallet::Wallet::Stream:
716 case KWallet::Wallet::Map:
719 case KWallet::Wallet::Unknown:
726 if (fi->
name() == _currentFolder) {
727 _entryTitle->setText(QString::fromLatin1(
"<font size=\"+1\">%1</font>").arg(fi->text(0)));
729 if (!_entryList->currentItem()) {
731 _entryStack->setCurrentIndex(0);
735 void KWalletEditor::listContextMenuRequested(
const QPoint& pos) {
736 if (!_contextMenu->isEnabled()) {
740 _contextMenu->popup(_entryList->mapToGlobal(pos));
744 void KWalletEditor::copyPassword() {
748 if (_w->readPassword(item->text(0), pass) == 0) {
749 QApplication::clipboard()->setText(pass);
755 void KWalletEditor::newEntry() {
772 _w->setFolder(fi->
name());
778 n = KInputDialog::getText(i18n(
"New Entry"),
779 i18n(
"Please choose a name for the new entry:"),
790 int rc = KMessageBox::questionYesNo(
this, i18n(
"Sorry, that entry already exists. Try again?"), QString(), KGuiItem(i18n(
"Try Again")), KGuiItem(i18n(
"Do Not Try")));
799 if (_w && item && !n.isEmpty()) {
807 KMessageBox::error(
this, i18n(
"An unexpected error occurred trying to add the new entry"));
810 _w->setFolder(fi->
name());
816 KMessageBox::error(
this, i18n(
"An unexpected error occurred trying to add the new entry"));
820 if (ci->
entryType() == KWallet::Wallet::Password) {
821 _w->writePassword(n, QString());
822 }
else if (ci->
entryType() == KWallet::Wallet::Map) {
823 _w->writeMap(n, QMap<QString,QString>());
824 }
else if (ci->
entryType() == KWallet::Wallet::Stream) {
825 _w->writeEntry(n, QByteArray());
830 _entryList->setCurrentItem(ni);
831 _entryList->scrollToItem(ni);
834 _entryTitle->setText(QString::fromLatin1(
"<font size=\"+1\">%1</font>").arg(fi->text(0)));
839 void KWalletEditor::renameEntry() {
842 _entryList->editItem(item, 0);
848 void KWalletEditor::listItemChanged(
QTreeWidgetItem* item,
int column) {
849 if (item && column == 0) {
855 const QString t = item->text(0);
856 if (t == i->
name()) {
859 if (!_w || t.isEmpty()) {
864 if (_w->renameEntry(i->
name(), t) == 0) {
868 KMessageBox::error(
this, i18n(
"An unexpected error occurred trying to rename the entry"));
871 if (ci->
entryType() == KWallet::Wallet::Password) {
872 _entryName->setText(i18n(
"Password: %1", item->text(0)));
873 }
else if (ci->
entryType() == KWallet::Wallet::Map) {
874 _entryName->setText(i18n(
"Name-Value Map: %1", item->text(0)));
875 }
else if (ci->
entryType() == KWallet::Wallet::Stream) {
876 _entryName->setText(i18n(
"Binary Data: %1", item->text(0)));
885 void KWalletEditor::deleteEntry() {
888 int rc = KMessageBox::warningContinueCancel(
this, i18n(
"Are you sure you wish to delete the item '%1'?", item->text(0)),QString(),KStandardGuiItem::del());
889 if (rc == KMessageBox::Continue) {
892 KMessageBox::error(
this, i18n(
"An unexpected error occurred trying to delete the entry"));
896 _w->removeEntry(item->text(0));
898 entrySelectionChanged(_entryList->currentItem());
900 _entryTitle->setText(QString::fromLatin1(
"<font size=\"+1\">%1</font>").arg(fi->text(0)));
905 void KWalletEditor::changePassword() {
906 KWallet::Wallet::changePassword(
_walletName, winId());
910 void KWalletEditor::walletOpened(
bool success) {
919 KMessageBox::sorry(
this, i18n(
"Unable to open the requested wallet."));
925 void KWalletEditor::hidePasswordContents() {
926 _entryStack->setCurrentIndex(4);
930 void KWalletEditor::showPasswordContents() {
931 _entryStack->setCurrentIndex(1);
935 void KWalletEditor::showHideMapEditorValue(
bool show) {
937 _mapEditor->showColumn(2);
939 _mapEditor->hideColumn(2);
946 void KWalletEditor::importWallet() {
947 KUrl url = KFileDialog::getOpenUrl(KUrl(), QLatin1String(
"*.kwl" ),
this);
953 if (!KIO::NetAccess::download(url, tmpFile,
this)) {
954 KMessageBox::sorry(
this, i18n(
"Unable to access wallet '<b>%1</b>'.", url.prettyUrl()));
958 KWallet::Wallet *w = KWallet::Wallet::openWallet(tmpFile, winId(), KWallet::Wallet::Path);
959 if (w && w->isOpen()) {
961 QStringList fl = w->folderList();
962 for (QStringList::ConstIterator f = fl.constBegin(); f != fl.constEnd(); ++f) {
963 if (!w->setFolder(*f)) {
967 if (!_w->hasFolder(*f)) {
968 _w->createFolder(*f);
973 QMap<QString, QMap<QString, QString> > map;
974 QSet<QString> mergedkeys;
976 rc = w->readMapList(QLatin1String(
"*" ), map);
978 QMap<QString, QMap<QString, QString> >::ConstIterator me;
979 for (me = map.constBegin(); me != map.constEnd(); ++me) {
980 bool hasEntry = _w->hasEntry(me.key());
981 if (hasEntry && mp ==
Prompt) {
984 bd->
setLabel(i18n(
"Folder '<b>%1</b>' already contains an entry '<b>%2</b>'. Do you wish to replace it?", Qt::escape(*f), Qt::escape(me.key())));
991 if (mp ==
Yes || mp ==
No) {
998 }
else if (hasEntry && mp ==
Never) {
1001 _w->writeMap(me.key(), me.value());
1002 mergedkeys.insert(me.key());
1006 QMap<QString, QString> pwd;
1007 rc = w->readPasswordList(QLatin1String(
"*" ), pwd);
1009 QMap<QString, QString>::ConstIterator pe;
1010 for (pe = pwd.constBegin(); pe != pwd.constEnd(); ++pe) {
1011 bool hasEntry = _w->hasEntry(pe.key());
1012 if (hasEntry && mp ==
Prompt) {
1015 bd->
setLabel(i18n(
"Folder '<b>%1</b>' already contains an entry '<b>%2</b>'. Do you wish to replace it?", Qt::escape(*f), Qt::escape(pe.key())));
1022 if (mp ==
Yes || mp ==
No) {
1029 }
else if (hasEntry && mp ==
Never) {
1032 _w->writePassword(pe.key(), pe.value());
1033 mergedkeys.insert(pe.key());
1037 QMap<QString, QByteArray> ent;
1038 rc = w->readEntryList(QLatin1String(
"*" ), ent);
1040 QMap<QString, QByteArray>::ConstIterator ee;
1041 for (ee = ent.constBegin(); ee != ent.constEnd(); ++ee) {
1043 if (mergedkeys.contains(ee.key())) {
1046 bool hasEntry = _w->hasEntry(ee.key());
1047 if (hasEntry && mp ==
Prompt) {
1050 bd->
setLabel(i18n(
"Folder '<b>%1</b>' already contains an entry '<b>%2</b>'. Do you wish to replace it?", Qt::escape(*f), Qt::escape(ee.key())));
1057 if (mp ==
Yes || mp ==
No) {
1064 }
else if (hasEntry && mp ==
Never) {
1067 _w->writeEntry(ee.key(), ee.value());
1075 KIO::NetAccess::removeTempFile(tmpFile);
1076 updateFolderList(
true);
1081 void KWalletEditor::importXML() {
1082 KUrl url = KFileDialog::getOpenUrl( KUrl(), QLatin1String(
"*.xml" ),
this);
1083 if (url.isEmpty()) {
1088 if (!KIO::NetAccess::download(url, tmpFile,
this)) {
1089 KMessageBox::sorry(
this, i18n(
"Unable to access XML file '<b>%1</b>'.", url.prettyUrl()));
1094 if (!qf.open(QIODevice::ReadOnly)) {
1095 KMessageBox::sorry(
this, i18n(
"Error opening XML file '<b>%1</b>' for input.", url.prettyUrl()));
1096 KIO::NetAccess::removeTempFile(tmpFile);
1100 QDomDocument doc(tmpFile);
1101 if (!doc.setContent(&qf)) {
1102 KMessageBox::sorry(
this, i18n(
"Error reading XML file '<b>%1</b>' for input.", url.prettyUrl()));
1103 KIO::NetAccess::removeTempFile(tmpFile);
1107 QDomElement top = doc.documentElement();
1108 if (top.tagName().toLower() != QLatin1String(
"wallet" )) {
1109 KMessageBox::sorry(
this, i18n(
"Error: XML file does not contain a wallet."));
1110 KIO::NetAccess::removeTempFile(tmpFile);
1114 QDomNode n = top.firstChild();
1116 while (!n.isNull()) {
1117 QDomElement e = n.toElement();
1118 if (e.tagName().toLower() != QLatin1String(
"folder" )) {
1119 n = n.nextSibling();
1123 QString fname = e.attribute(QLatin1String(
"name" ));
1124 if (fname.isEmpty()) {
1125 n = n.nextSibling();
1128 if (!_w->hasFolder(fname)) {
1129 _w->createFolder(fname);
1131 _w->setFolder(fname);
1132 QDomNode enode = e.firstChild();
1133 while (!enode.isNull()) {
1134 e = enode.toElement();
1135 QString type = e.tagName().toLower();
1136 QString ename = e.attribute(QLatin1String(
"name" ));
1137 bool hasEntry = _w->hasEntry(ename);
1138 if (hasEntry && mp ==
Prompt) {
1141 bd->
setLabel(i18n(
"Folder '<b>%1</b>' already contains an entry '<b>%2</b>'. Do you wish to replace it?", Qt::escape(fname), Qt::escape(ename)));
1148 if (mp ==
Yes || mp ==
No) {
1152 enode = enode.nextSibling();
1155 }
else if (hasEntry && mp ==
Never) {
1156 enode = enode.nextSibling();
1160 if (type == QLatin1String(
"password" )) {
1161 _w->writePassword(ename, e.text());
1162 }
else if (type == QLatin1String(
"stream" )) {
1163 _w->writeEntry(ename, KCodecs::base64Decode(e.text().toLatin1()));
1164 }
else if (type == QLatin1String(
"map" )) {
1165 QMap<QString,QString> map;
1166 QDomNode mapNode = e.firstChild();
1167 while (!mapNode.isNull()) {
1168 QDomElement mape = mapNode.toElement();
1169 if (mape.tagName().toLower() == QLatin1String(
"mapentry" )) {
1170 map[mape.attribute(QLatin1String(
"name" ))] = mape.text();
1172 mapNode = mapNode.nextSibling();
1174 _w->writeMap(ename, map);
1176 enode = enode.nextSibling();
1178 n = n.nextSibling();
1181 KIO::NetAccess::removeTempFile(tmpFile);
1182 updateFolderList(
true);
1187 void KWalletEditor::exportXML() {
1190 QXmlStreamWriter xml(&tf);
1191 xml.setAutoFormatting(
true);
1192 xml.writeStartDocument();
1193 const QStringList fl = _w->folderList();
1195 xml.writeStartElement(QLatin1String(
"wallet" ));
1196 xml.writeAttribute(QLatin1String(
"name" ),
_walletName);
1197 for (QStringList::const_iterator i = fl.constBegin(); i != fl.constEnd(); ++i) {
1198 xml.writeStartElement(QLatin1String(
"folder" ));
1199 xml.writeAttribute(QLatin1String(
"name" ), *i);
1201 QStringList entries = _w->entryList();
1202 for (QStringList::const_iterator j = entries.constBegin(); j != entries.constEnd(); ++j) {
1203 switch (_w->entryType(*j)) {
1204 case KWallet::Wallet::Password:
1207 if (_w->readPassword(*j, pass) == 0) {
1208 xml.writeStartElement(QLatin1String(
"password" ));
1209 xml.writeAttribute(QLatin1String(
"name" ), *j);
1210 xml.writeCharacters(pass);
1211 xml.writeEndElement();
1215 case KWallet::Wallet::Stream:
1218 if (_w->readEntry(*j, ba) == 0) {
1219 xml.writeStartElement(QLatin1String(
"stream" ));
1220 xml.writeAttribute(QLatin1String(
"name" ), *j);
1221 xml.writeCharacters(QLatin1String( KCodecs::base64Encode(ba) ));
1222 xml.writeEndElement();
1226 case KWallet::Wallet::Map:
1228 QMap<QString,QString> map;
1229 if (_w->readMap(*j, map) == 0) {
1230 xml.writeStartElement(QLatin1String(
"map" ));
1231 xml.writeAttribute(QLatin1String(
"name" ), *j);
1232 for (QMap<QString,QString>::ConstIterator k = map.constBegin(); k != map.constEnd(); ++k) {
1233 xml.writeStartElement(QLatin1String(
"mapentry" ));
1234 xml.writeAttribute(QLatin1String(
"name" ), k.key());
1235 xml.writeCharacters(k.value());
1236 xml.writeEndElement();
1238 xml.writeEndElement();
1242 case KWallet::Wallet::Unknown:
1247 xml.writeEndElement();
1250 xml.writeEndElement();
1251 xml.writeEndDocument();
1254 KUrl url = KFileDialog::getSaveUrl(KUrl(), QLatin1String(
"*.xml" ),
this);
1256 if (!url.isEmpty()) {
1257 KIO::NetAccess::dircopy(KUrl::fromPath(tf.fileName()), url,
this);
1267 void KWalletEditor::saveAs() {
1268 KUrl url = KFileDialog::getSaveUrl(KUrl(), QLatin1String(
"*.kwl" ),
this);
1269 if (!url.isEmpty()) {
1274 QString path = KGlobal::dirs()->saveLocation(
"kwallet") + QLatin1Char(
'/' ) +
_walletName + QLatin1String(
".kwl" );
1275 KIO::NetAccess::dircopy(KUrl::fromPath(path), url,
this);
1285 disconnectActions();
1286 _saveAsAction->setEnabled(
false);
1297 void KWalletEditor::onSearchTextChanged(
const QString& text)
1299 static bool treeIsExpanded =
false;
1300 if (text.isEmpty()) {
1301 if (treeIsExpanded) {
1302 _entryList->setCurrentItem(NULL);
1305 QTimer::singleShot(300, _entryList, SLOT(collapseAll()));
1306 treeIsExpanded =
false;
1309 if (!treeIsExpanded) {
1310 _entryList->expandAll();
1311 treeIsExpanded =
true;
1315 QTimer::singleShot(300, _entryList, SLOT(selectFirstVisible()));
1318 QTimer::singleShot(300, _entryList, SLOT(refreshItemsCount()));
1321 void KWalletEditor::onAlwaysShowContents(
bool checked)
1323 _alwaysShowContents = checked;
1324 _alwaysHideContentsAction->setChecked(!_alwaysShowContents);
1325 showPasswordContents();
1328 void KWalletEditor::onAlwaysHideContents(
bool checked)
1330 _alwaysShowContents = !checked;
1331 _alwaysShowContentsAction->setChecked(_alwaysShowContents);
1333 hidePasswordContents();
1337 #include "kwalleteditor.moc"
This class is intended to avoid coupling between the main window and it's contained widgets that hand...
void enableContextFolderActions(bool enable)
KWalletEditor(QWidget *parent, const char *name=0)
virtual void hideEvent(QHideEvent *)
KWalletFolderItem * getFolder(const QString &name)
QPixmap getFolderIcon(KIconLoader::Group group)
void enableFolderActions(bool enable)
void setWallet(KWallet::Wallet *wallet, bool isPath=false)
KWalletContainerItem * getContainer(KWallet::Wallet::EntryType type)
void enableWalletActions(bool enable)
void setWallet(KWallet::Wallet *w)
virtual void showEvent(QShowEvent *)
const QString & name() const
void setNewWallet(bool newWallet)
KWallet::Wallet::EntryType entryType()
bool contains(const QString &itemKey)
virtual void setLabel(const QString &label)
bool existsFolder(const QString &name)
void setName(const QString &n)