kwalleteditor.cpp 46.5 KB
Newer Older
1
/*
2
3
    SPDX-FileCopyrightText: 2003-2005 George Staikos <staikos@kde.org>
    SPDX-FileCopyrightText: 2005 Isaac Clerencia <isaac@warp.es>
4

5
    SPDX-License-Identifier: GPL-2.0-or-later
6
7
8
*/

#include "kwalleteditor.h"
9
#include "kbetterthankdialogbase.h"
10
#include "kwmapeditor.h"
11
#include "allyourbase.h"
12

Laurent Montel's avatar
Laurent Montel committed
13
14
15
#include <QAction>
#include <QDialog>
#include <QInputDialog>
16
17
#include <KIO/StoredTransferJob>
#include <KJobWidgets>
18
#include <KActionCollection>
19
#include <KCodecs>
20
#include <KMessageBox>
Laurent Montel's avatar
Laurent Montel committed
21
#include <QMenu>
22
23
#include <KSqueezedTextLabel>
#include <KStandardAction>
Laurent Montel's avatar
Laurent Montel committed
24
#include <KConfigGroup>
David Faure's avatar
Ported    
David Faure committed
25

Laurent Montel's avatar
Laurent Montel committed
26
#include <QTemporaryFile>
27
#include <KXMLGUIFactory>
Laurent Montel's avatar
Laurent Montel committed
28
#include <KSharedConfig>
Teo Mrnjavac's avatar
Teo Mrnjavac committed
29

30
#include <KToolBar>
Laurent Montel's avatar
Laurent Montel committed
31
#include <KLocalizedString>
Laurent Montel's avatar
Laurent Montel committed
32
#include <QIcon>
Teo Mrnjavac's avatar
Teo Mrnjavac committed
33
34
#include <KTreeWidgetSearchLine>

35
#include <QCheckBox>
36
#include <QClipboard>
Teo Mrnjavac's avatar
Teo Mrnjavac committed
37
38
39
40
41
#include <QDomDocument>
#include <QDomElement>
#include <QDomNode>
#include <QFileDialog>
#include <QList>
42
#include <QPushButton>
Teo Mrnjavac's avatar
Teo Mrnjavac committed
43
#include <QStack>
44
#include <QSet>
Teo Mrnjavac's avatar
Teo Mrnjavac committed
45
#include <QTimer>
46
#include <QToolButton>
Teo Mrnjavac's avatar
Teo Mrnjavac committed
47
48
#include <QVBoxLayout>
#include <QXmlStreamWriter>
49

50
#include <assert.h>
Teo Mrnjavac's avatar
Teo Mrnjavac committed
51
#include <stdlib.h>
52

Laurent Montel's avatar
Laurent Montel committed
53
54
55
56
57
58
59
60
61
62
63
QAction *KWalletEditor::_newFolderAction = nullptr;
QAction *KWalletEditor::_deleteFolderAction = nullptr;
QAction *KWalletEditor::_exportAction = nullptr;
QAction *KWalletEditor::_mergeAction = nullptr;
QAction *KWalletEditor::_importAction = nullptr;
QAction *KWalletEditor::_newEntryAction = nullptr;
QAction *KWalletEditor::_renameEntryAction = nullptr;
QAction *KWalletEditor::_deleteEntryAction = nullptr;
QAction *KWalletEditor::_copyPassAction = nullptr;
QAction *KWalletEditor::_alwaysShowContentsAction = nullptr;
QAction *KWalletEditor::_alwaysHideContentsAction = nullptr;
64
65
66

RegisterCreateActionsMethod KWalletEditor::_registerCreateActionMethod(&KWalletEditor::createActions);

Laurent Montel's avatar
nullptr    
Laurent Montel committed
67
KWalletEditor::KWalletEditor(QWidget *parent, const QString &name)
68
    : QWidget(parent), _displayedItem(nullptr), _actionCollection(nullptr), _alwaysShowContents(false)
Teo Mrnjavac's avatar
Teo Mrnjavac committed
69
70
{
    setupUi(this);
Laurent Montel's avatar
nullptr    
Laurent Montel committed
71
    setObjectName(name);
Teo Mrnjavac's avatar
Teo Mrnjavac committed
72
73
74
    _newWallet = false;
    _splitter->setStretchFactor(0, 1);
    _splitter->setStretchFactor(1, 2);
Laurent Montel's avatar
Laurent Montel committed
75
    _contextMenu = new QMenu(this);
Teo Mrnjavac's avatar
Teo Mrnjavac committed
76

Laurent Montel's avatar
Laurent Montel committed
77
78
    _undoChanges->setIcon(QIcon::fromTheme(QStringLiteral("edit-undo")));
    _saveChanges->setIcon(QIcon::fromTheme(QStringLiteral("document-save")));
79
    _hasUnsavedChanges = false;
Teo Mrnjavac's avatar
Teo Mrnjavac committed
80
81

    QVBoxLayout *box = new QVBoxLayout(_entryListFrame);
Laurent Montel's avatar
Laurent Montel committed
82
    box->setContentsMargins(0, 0, 0, 0);
83
    _entryList = new KWalletEntryList(_entryListFrame, QStringLiteral("Wallet Entry List"));
Teo Mrnjavac's avatar
Teo Mrnjavac committed
84
    _entryList->setContextMenuPolicy(Qt::CustomContextMenu);
85
    _searchLine = new KTreeWidgetSearchLine(_entryListFrame, _entryList);
86
    _searchLine->setPlaceholderText(i18n("Search"));
Laurent Montel's avatar
Laurent Montel committed
87
    connect(_searchLine, &KTreeWidgetSearchLine::textChanged, this, &KWalletEditor::onSearchTextChanged);
Teo Mrnjavac's avatar
Teo Mrnjavac committed
88
89
90
91
92
93
    box->addWidget(_searchLine);
    box->addWidget(_entryList);

    _entryStack->setEnabled(true);

    box = new QVBoxLayout(_entryStack->widget(2));
Laurent Montel's avatar
Laurent Montel committed
94
    box->setContentsMargins(0, 0, 0, 0);
Teo Mrnjavac's avatar
Teo Mrnjavac committed
95
    _mapEditorShowHide = new QCheckBox(i18n("&Show values"), _entryStack->widget(2));
Laurent Montel's avatar
Laurent Montel committed
96
    connect(_mapEditorShowHide, &QCheckBox::toggled, this, &KWalletEditor::showHideMapEditorValue);
Teo Mrnjavac's avatar
Teo Mrnjavac committed
97
98
99
100
101
    _mapEditor = new KWMapEditor(_currentMap, _entryStack->widget(2));
    box->addWidget(_mapEditorShowHide);
    box->addWidget(_mapEditor);

    // load splitter size
Teo Mrnjavac's avatar
Teo Mrnjavac committed
102
    KConfigGroup cg(KSharedConfig::openConfig(), "WalletEditor");
Teo Mrnjavac's avatar
Teo Mrnjavac committed
103
104
105
    QList<int> splitterSize = cg.readEntry("SplitterSize", QList<int>());
    if (splitterSize.size() != 2) {
        splitterSize.clear();
Teo Mrnjavac's avatar
Teo Mrnjavac committed
106
107
        splitterSize.append(_splitter->width() / 2);
        splitterSize.append(_splitter->width() / 2);
Teo Mrnjavac's avatar
Teo Mrnjavac committed
108
109
    }
    _splitter->setSizes(splitterSize);
110
    _alwaysShowContents = cg.readEntry("AlwaysShowContents", false);
111

112
113
    _searchLine->setFocus();

Laurent Montel's avatar
Laurent Montel committed
114
115
116
117
118
119
120
121
122
123
124
125
    connect(_entryList, &KWalletEntryList::currentItemChanged, this, &KWalletEditor::entrySelectionChanged);
    connect(_entryList, &KWalletEntryList::customContextMenuRequested, this, &KWalletEditor::listContextMenuRequested);
    connect(_entryList, &KWalletEntryList::itemChanged, this, &KWalletEditor::listItemChanged);

    connect(_passwordValue, &QTextEdit::textChanged, this, &KWalletEditor::entryEditted);
    connect(_mapEditor, &KWMapEditor::dirty, this, &KWalletEditor::entryEditted);

    connect(_undoChanges, &QPushButton::clicked, this, &KWalletEditor::restoreEntry);
    connect(_saveChanges, &QPushButton::clicked, this, &KWalletEditor::saveEntry);

    connect(_showContents, &QToolButton::clicked, this, &KWalletEditor::showPasswordContents);
    connect(_hideContents, &QToolButton::clicked, this, &KWalletEditor::hidePasswordContents);
Teo Mrnjavac's avatar
Teo Mrnjavac committed
126

127
128
    _passwordValue->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));

Rafał Rzepecki's avatar
Rafał Rzepecki committed
129
    _binaryViewShow->setChecked(_alwaysShowContents);
Teo Mrnjavac's avatar
Teo Mrnjavac committed
130
//    createActions();
131
    // TODO: remove kwalleteditor.rc file
132
}
133

Teo Mrnjavac's avatar
Teo Mrnjavac committed
134
135
KWalletEditor::~KWalletEditor()
{
136
137
138
    emit enableFolderActions(false);
    emit enableWalletActions(false);
    emit enableContextFolderActions(false);
Teo Mrnjavac's avatar
Teo Mrnjavac committed
139
    // save splitter size
Teo Mrnjavac's avatar
Teo Mrnjavac committed
140
    KConfigGroup cg(KSharedConfig::openConfig(), "WalletEditor");
Teo Mrnjavac's avatar
Teo Mrnjavac committed
141
    cg.writeEntry("SplitterSize", _splitter->sizes());
142
    cg.writeEntry("AlwaysShowContents", _alwaysShowContents);
Teo Mrnjavac's avatar
Teo Mrnjavac committed
143
144
145
    cg.sync();

    delete _w;
Laurent Montel's avatar
Laurent Montel committed
146
    _w = nullptr;
Teo Mrnjavac's avatar
Teo Mrnjavac committed
147
148
149
150
    if (_nonLocal) {
        KWallet::Wallet::closeWallet(_walletName, true);
    }
    delete _contextMenu;
Laurent Montel's avatar
Laurent Montel committed
151
    _contextMenu = nullptr;
152
153
}

Teo Mrnjavac's avatar
Teo Mrnjavac committed
154
void KWalletEditor::setWallet(KWallet::Wallet *wallet, bool isPath)
Valentin Rusu's avatar
Valentin Rusu committed
155
{
Laurent Montel's avatar
Laurent Montel committed
156
    Q_ASSERT(wallet != nullptr);
Valentin Rusu's avatar
Valentin Rusu committed
157
158
159
160
    _walletName = wallet->walletName();
    _nonLocal = isPath;

    _w = wallet;
161
    _entryList->setWallet(_w);
Laurent Montel's avatar
Laurent Montel committed
162
163
164
    connect(_w, &KWallet::Wallet::walletOpened, this, &KWalletEditor::walletOpened);
    connect(_w, &KWallet::Wallet::walletClosed, this, &KWalletEditor::walletClosed);
    connect(_w, &KWallet::Wallet::folderUpdated, this, &KWalletEditor::updateEntries);
Valentin Rusu's avatar
Valentin Rusu committed
165
166
    connect(_w, SIGNAL(folderListUpdated()), this, SLOT(updateFolderList()));
    updateFolderList();
167
168
169
170

    emit enableFolderActions(true);
    emit enableWalletActions(true);
    emit enableContextFolderActions(true);
171

Valentin Rusu's avatar
Valentin Rusu committed
172
173
174
    _mapEditorShowHide->setChecked(false);
    showHideMapEditorValue(false);

175
176
    setFocus();
    _searchLine->setFocus();
Valentin Rusu's avatar
Valentin Rusu committed
177
178
}

Laurent Montel's avatar
Laurent Montel committed
179
180
181
182
183
bool KWalletEditor::isOpen() const
{
    return _w != nullptr;
}

Teo Mrnjavac's avatar
Teo Mrnjavac committed
184
KActionCollection *KWalletEditor::actionCollection()
185
{
186
    if (!_actionCollection) {
187
188
189
190
191
        _actionCollection = new KActionCollection(this);
    }
    return _actionCollection;
}

Teo Mrnjavac's avatar
Teo Mrnjavac committed
192
193
void KWalletEditor::createActions(KActionCollection *actionCollection)
{
Laurent Montel's avatar
Laurent Montel committed
194
    _newFolderAction = actionCollection->addAction(QStringLiteral("create_folder"));
Teo Mrnjavac's avatar
Teo Mrnjavac committed
195
    _newFolderAction->setText(i18n("&New Folder..."));
Laurent Montel's avatar
Laurent Montel committed
196
    _newFolderAction->setIcon(QIcon::fromTheme(QStringLiteral("folder-new")));
197

Laurent Montel's avatar
Laurent Montel committed
198
    _deleteFolderAction = actionCollection->addAction(QStringLiteral("delete_folder"));
Teo Mrnjavac's avatar
Teo Mrnjavac committed
199
    _deleteFolderAction->setText(i18n("&Delete Folder"));
200

Laurent Montel's avatar
Laurent Montel committed
201
    _mergeAction = actionCollection->addAction(QStringLiteral("wallet_merge"));
202
    _mergeAction->setText(i18n("&Import a wallet..."));
203

Laurent Montel's avatar
Laurent Montel committed
204
    _importAction = actionCollection->addAction(QStringLiteral("wallet_import"));
Teo Mrnjavac's avatar
Teo Mrnjavac committed
205
    _importAction->setText(i18n("&Import XML..."));
206

Laurent Montel's avatar
Laurent Montel committed
207
    _exportAction = actionCollection->addAction(QStringLiteral("wallet_export"));
Teo Mrnjavac's avatar
Teo Mrnjavac committed
208
    _exportAction->setText(i18n("&Export as XML..."));
209

Laurent Montel's avatar
Laurent Montel committed
210
    _copyPassAction = actionCollection->addAction(QStringLiteral("copy_action"));
Teo Mrnjavac's avatar
Teo Mrnjavac committed
211
    _copyPassAction->setText(i18n("&Copy"));
212
    actionCollection->setDefaultShortcut(_copyPassAction, Qt::Key_C | Qt::CTRL);
213
    _copyPassAction->setEnabled(false);
214

Laurent Montel's avatar
Laurent Montel committed
215
    _newEntryAction = actionCollection->addAction(QStringLiteral("new_entry"));
Teo Mrnjavac's avatar
Teo Mrnjavac committed
216
    _newEntryAction->setText(i18n("&New..."));
Laurent Montel's avatar
Laurent Montel committed
217
    actionCollection->setDefaultShortcut(_newEntryAction, Qt::Key_Insert);
Teo Mrnjavac's avatar
Teo Mrnjavac committed
218
    _newEntryAction->setEnabled(false);
219

Laurent Montel's avatar
Laurent Montel committed
220
    _renameEntryAction = actionCollection->addAction(QStringLiteral("rename_entry"));
Teo Mrnjavac's avatar
Teo Mrnjavac committed
221
    _renameEntryAction->setText(i18n("&Rename"));
Laurent Montel's avatar
Laurent Montel committed
222
    actionCollection->setDefaultShortcut(_renameEntryAction, Qt::Key_F2);
Teo Mrnjavac's avatar
Teo Mrnjavac committed
223
    _renameEntryAction->setEnabled(false);
224

Laurent Montel's avatar
Laurent Montel committed
225
    _deleteEntryAction = actionCollection->addAction(QStringLiteral("delete_entry"));
Teo Mrnjavac's avatar
Teo Mrnjavac committed
226
    _deleteEntryAction->setText(i18n("&Delete"));
Laurent Montel's avatar
Laurent Montel committed
227
    actionCollection->setDefaultShortcut(_deleteEntryAction, Qt::Key_Delete);
Teo Mrnjavac's avatar
Teo Mrnjavac committed
228
    _deleteEntryAction->setEnabled(false);
229

Laurent Montel's avatar
Laurent Montel committed
230
    _alwaysShowContentsAction = actionCollection->addAction(QStringLiteral("always_show_contents"));
Teo Mrnjavac's avatar
Teo Mrnjavac committed
231
    _alwaysShowContentsAction->setText(i18n("Always show contents"));
232
233
    _alwaysShowContentsAction->setCheckable(true);

Laurent Montel's avatar
Laurent Montel committed
234
    _alwaysHideContentsAction = actionCollection->addAction(QStringLiteral("always_hide_contents"));
Teo Mrnjavac's avatar
Teo Mrnjavac committed
235
    _alwaysHideContentsAction->setText(i18n("Always hide contents"));
236
    _alwaysHideContentsAction->setCheckable(true);
237
}
238

239
240
void KWalletEditor::connectActions()
{
Laurent Montel's avatar
Laurent Montel committed
241
242
    connect(_newFolderAction, &QAction::triggered, this, &KWalletEditor::createFolder);
    connect(this, &KWalletEditor::enableFolderActions, _newFolderAction, &QAction::setEnabled);
243

Laurent Montel's avatar
Laurent Montel committed
244
    connect(_deleteFolderAction, &QAction::triggered, this, &KWalletEditor::deleteFolder);
Laurent Montel's avatar
Laurent Montel committed
245
246
    connect(this, &KWalletEditor::enableContextFolderActions, _deleteFolderAction, &QAction::setEnabled);
    connect(this, &KWalletEditor::enableFolderActions, _deleteFolderAction, &QAction::setEnabled);
247

Laurent Montel's avatar
Laurent Montel committed
248
249
    connect(_mergeAction, &QAction::triggered, this, &KWalletEditor::importWallet);
    connect(this, &KWalletEditor::enableWalletActions, _mergeAction, &QAction::setEnabled);
250

Laurent Montel's avatar
Laurent Montel committed
251
252
    connect(_importAction, &QAction::triggered, this, &KWalletEditor::importXML);
    connect(this, &KWalletEditor::enableWalletActions, _importAction, &QAction::setEnabled);
253

Laurent Montel's avatar
Laurent Montel committed
254
255
    connect(_exportAction, &QAction::triggered, this, &KWalletEditor::exportXML);
    connect(this, &KWalletEditor::enableWalletActions, _exportAction, &QAction::setEnabled);
256

Laurent Montel's avatar
Laurent Montel committed
257
    connect(_newEntryAction, &QAction::triggered, this, &KWalletEditor::newEntry);
258

Laurent Montel's avatar
Laurent Montel committed
259
    connect(_renameEntryAction, &QAction::triggered, this, &KWalletEditor::renameEntry);
260

Laurent Montel's avatar
Laurent Montel committed
261
    connect(_deleteEntryAction, &QAction::triggered, this, &KWalletEditor::deleteEntry);
262

Laurent Montel's avatar
Laurent Montel committed
263
264
    connect(_copyPassAction, &QAction::triggered, this, &KWalletEditor::copyPassword);
    connect(this, &KWalletEditor::enableWalletActions, _copyPassAction, &QAction::setEnabled);
265
266
267

    _showContents->addAction(_alwaysShowContentsAction);
    _alwaysShowContentsAction->setChecked(_alwaysShowContents);
Laurent Montel's avatar
Laurent Montel committed
268
    connect(_alwaysShowContentsAction, &QAction::triggered, this, &KWalletEditor::onAlwaysShowContents);
269
270
271

    _hideContents->addAction(_alwaysHideContentsAction);
    _alwaysHideContentsAction->setChecked(!_alwaysShowContents);
Laurent Montel's avatar
Laurent Montel committed
272
    connect(_alwaysHideContentsAction, &QAction::triggered, this, &KWalletEditor::onAlwaysHideContents);
273
274
}

275
276
void KWalletEditor::disconnectActions()
{
Laurent Montel's avatar
Laurent Montel committed
277
278
    disconnect(_newFolderAction, &QAction::triggered, this, &KWalletEditor::createFolder);
    disconnect(this, &KWalletEditor::enableFolderActions, _newFolderAction, &QAction::setEnabled);
279

Laurent Montel's avatar
Laurent Montel committed
280
281
282
    disconnect(_deleteFolderAction, &QAction::triggered, this, &KWalletEditor::deleteFolder);
    disconnect(this, &KWalletEditor::enableContextFolderActions, _deleteFolderAction, &QAction::setEnabled);
    disconnect(this, &KWalletEditor::enableFolderActions, _deleteFolderAction, &QAction::setEnabled);
283

Laurent Montel's avatar
Laurent Montel committed
284
285
    disconnect(_mergeAction, &QAction::triggered, this, &KWalletEditor::importWallet);
    disconnect(this, &KWalletEditor::enableWalletActions, _mergeAction, &QAction::setEnabled);
286

Laurent Montel's avatar
Laurent Montel committed
287
288
    disconnect(_importAction, &QAction::triggered, this, &KWalletEditor::importXML);
    disconnect(this, &KWalletEditor::enableWalletActions, _importAction, &QAction::setEnabled);
289

Laurent Montel's avatar
Laurent Montel committed
290
291
    disconnect(_exportAction, &QAction::triggered, this, &KWalletEditor::exportXML);
    disconnect(this, &KWalletEditor::enableWalletActions, _exportAction, &QAction::setEnabled);
292

Laurent Montel's avatar
Laurent Montel committed
293
    disconnect(_newEntryAction, &QAction::triggered, this, &KWalletEditor::newEntry);
294

Laurent Montel's avatar
Laurent Montel committed
295
    disconnect(_renameEntryAction, &QAction::triggered, this, &KWalletEditor::renameEntry);
296

Laurent Montel's avatar
Laurent Montel committed
297
    disconnect(_deleteEntryAction, &QAction::triggered, this, &KWalletEditor::deleteEntry);
298

Laurent Montel's avatar
Laurent Montel committed
299
300
    disconnect(_copyPassAction, &QAction::triggered, this, &KWalletEditor::copyPassword);
    disconnect(this, &KWalletEditor::enableWalletActions, _copyPassAction, &QAction::setEnabled);
301

Laurent Montel's avatar
Laurent Montel committed
302
303
    disconnect(_alwaysShowContentsAction, &QAction::triggered, this, &KWalletEditor::onAlwaysShowContents);
    disconnect(_alwaysHideContentsAction, &QAction::triggered, this, &KWalletEditor::onAlwaysHideContents);
304
}
305

Teo Mrnjavac's avatar
Teo Mrnjavac committed
306
307
void KWalletEditor::walletClosed()
{
Laurent Montel's avatar
Laurent Montel committed
308
    _w = nullptr;
Teo Mrnjavac's avatar
Teo Mrnjavac committed
309
310
311
    setEnabled(false);
    emit enableWalletActions(false);
    emit enableFolderActions(false);
312
313
}

Teo Mrnjavac's avatar
Teo Mrnjavac committed
314
315
void KWalletEditor::updateFolderList(bool checkEntries)
{
Teo Mrnjavac's avatar
Teo Mrnjavac committed
316
    const QStringList fl = _w->folderList();
Teo Mrnjavac's avatar
Teo Mrnjavac committed
317
    QStack<QTreeWidgetItem *> trash;
Teo Mrnjavac's avatar
Teo Mrnjavac committed
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341

    for (int i = 0; i < _entryList->topLevelItemCount(); ++i) {
        KWalletFolderItem *fi = dynamic_cast<KWalletFolderItem *>(_entryList->topLevelItem(i));
        if (!fi) {
            continue;
        }
        if (!fl.contains(fi->name())) {
            trash.push(fi);
        }
    }

    qDeleteAll(trash);
    trash.clear();

    for (QStringList::const_iterator i = fl.begin(); i != fl.end(); ++i) {
        if (_entryList->existsFolder(*i)) {
            if (checkEntries) {
                updateEntries(*i);
            }
            continue;
        }

        _w->setFolder(*i);
        const QStringList entries = _w->entryList();
Teo Mrnjavac's avatar
Teo Mrnjavac committed
342
        KWalletFolderItem *item = new KWalletFolderItem(_w, _entryList, *i, entries.count());
Teo Mrnjavac's avatar
Teo Mrnjavac committed
343

Teo Mrnjavac's avatar
Teo Mrnjavac committed
344
345
346
347
        KWalletContainerItem *pi = new KWalletContainerItem(item, i18n("Passwords"), KWallet::Wallet::Password);
        KWalletContainerItem *mi = new KWalletContainerItem(item, i18n("Maps"), KWallet::Wallet::Map);
        KWalletContainerItem *bi = new KWalletContainerItem(item, i18n("Binary Data"), KWallet::Wallet::Stream);
        KWalletContainerItem *ui = new KWalletContainerItem(item, i18n("Unknown"), KWallet::Wallet::Unknown);
Teo Mrnjavac's avatar
Teo Mrnjavac committed
348
349
350

        for (QStringList::const_iterator j = entries.begin(); j != entries.end(); ++j) {
            switch (_w->entryType(*j)) {
Teo Mrnjavac's avatar
Teo Mrnjavac committed
351
352
353
354
355
356
357
358
359
360
361
362
363
            case KWallet::Wallet::Password:
                new KWalletEntryItem(_w, pi, *j);
                break;
            case KWallet::Wallet::Stream:
                new KWalletEntryItem(_w, bi, *j);
                break;
            case KWallet::Wallet::Map:
                new KWalletEntryItem(_w, mi, *j);
                break;
            case KWallet::Wallet::Unknown:
            default:
                new QTreeWidgetItem(ui, QStringList() << *j);
                break;
Teo Mrnjavac's avatar
Teo Mrnjavac committed
364
365
366
367
368
369
370
371
372
373
374
            }
        }
        _entryList->setEnabled(true);
    }

    //check if the current folder has been removed
    if (!fl.contains(_currentFolder)) {
        _currentFolder.clear();
        _entryTitle->clear();
        _iconTitle->clear();
    }
375
}
376

Teo Mrnjavac's avatar
Teo Mrnjavac committed
377
378
void KWalletEditor::deleteFolder()
{
Teo Mrnjavac's avatar
Teo Mrnjavac committed
379
380
381
382
383
384
385
386
    if (_w) {
        QTreeWidgetItem *i = _entryList->currentItem();
        if (i) {
            KWalletFolderItem *fi = dynamic_cast<KWalletFolderItem *>(i);
            if (!fi) {
                return;
            }

Teo Mrnjavac's avatar
Teo Mrnjavac committed
387
            int rc = KMessageBox::warningContinueCancel(this, i18n("Are you sure you wish to delete the folder '%1' from the wallet?", fi->name()), QString(), KStandardGuiItem::del());
Teo Mrnjavac's avatar
Teo Mrnjavac committed
388
389
390
391
392
393
394
395
396
397
398
399
400
            if (rc == KMessageBox::Continue) {
                bool rc = _w->removeFolder(fi->name());
                if (!rc) {
                    KMessageBox::sorry(this, i18n("Error deleting folder."));
                    return;
                }
                _currentFolder.clear();
                _entryTitle->clear();
                _iconTitle->clear();
                updateFolderList();
            }
        }
    }
401
402
}

Teo Mrnjavac's avatar
Teo Mrnjavac committed
403
404
void KWalletEditor::createFolder()
{
Teo Mrnjavac's avatar
Teo Mrnjavac committed
405
406
407
408
409
    if (_w) {
        QString n;
        bool ok;

        do {
Laurent Montel's avatar
Laurent Montel committed
410
            n = QInputDialog::getText(this, i18n("New Folder"),
Teo Mrnjavac's avatar
Teo Mrnjavac committed
411
                                      i18n("Please choose a name for the new folder:"),
Laurent Montel's avatar
Laurent Montel committed
412
413
                                      QLineEdit::Normal, QString(),
                                      &ok);
Teo Mrnjavac's avatar
Teo Mrnjavac committed
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431

            if (!ok) {
                return;
            }

            if (_entryList->existsFolder(n)) {
                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")));
                if (rc == KMessageBox::Yes) {
                    continue;
                }
                n.clear();
            }
            break;
        } while (true);

        _w->createFolder(n);
        updateFolderList();
    }
432
433
}

Teo Mrnjavac's avatar
Teo Mrnjavac committed
434
435
void KWalletEditor::saveEntry()
{
Teo Mrnjavac's avatar
Teo Mrnjavac committed
436
437
438
439
    int rc = 1;
    QTreeWidgetItem *item = _displayedItem; //  _entryList->currentItem();
    _saveChanges->setEnabled(false);
    _undoChanges->setEnabled(false);
440
    _hasUnsavedChanges = false;
Teo Mrnjavac's avatar
Teo Mrnjavac committed
441
    if (item && _w && item->parent()) {
Teo Mrnjavac's avatar
Teo Mrnjavac committed
442
        KWalletContainerItem *ci = dynamic_cast<KWalletContainerItem *>(item->parent());
Teo Mrnjavac's avatar
Teo Mrnjavac committed
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
        if (ci) {
            if (ci->entryType() == KWallet::Wallet::Password) {
                rc = _w->writePassword(item->text(0), _passwordValue->toPlainText());
            } else if (ci->entryType() == KWallet::Wallet::Map) {
                _mapEditor->saveMap();
                rc = _w->writeMap(item->text(0), _currentMap);
            } else {
                return;
            }

            if (rc == 0) {
                return;
            }
        }
    }

    KMessageBox::sorry(this, i18n("Error saving entry. Error code: %1", rc));
460
461
}

Teo Mrnjavac's avatar
Teo Mrnjavac committed
462
463
void KWalletEditor::restoreEntry()
{
Teo Mrnjavac's avatar
Teo Mrnjavac committed
464
    entrySelectionChanged(_entryList->currentItem());
465
    _hasUnsavedChanges = false;
466
467
}

Teo Mrnjavac's avatar
Teo Mrnjavac committed
468
469
void KWalletEditor::entryEditted()
{
Teo Mrnjavac's avatar
Teo Mrnjavac committed
470
471
    _saveChanges->setEnabled(true);
    _undoChanges->setEnabled(true);
472
    _hasUnsavedChanges = true;
473
474
}

Teo Mrnjavac's avatar
Teo Mrnjavac committed
475
476
void KWalletEditor::entrySelectionChanged(QTreeWidgetItem *item)
{
477
    // do not forget to save changes
Teo Mrnjavac's avatar
Teo Mrnjavac committed
478
479
480
    if (_saveChanges->isEnabled() && _displayedItem && (_displayedItem != item)) {
        if (KMessageBox::Yes ==  KMessageBox::questionYesNo(this,
                i18n("The contents of the current item has changed.\nDo you want to save changes?"))) {
481
            saveEntry();
482
        } else {
483
484
            _saveChanges->setEnabled(false);
            _undoChanges->setEnabled(false);
485
            _hasUnsavedChanges = false;
486
487
        }
    }
488

Laurent Montel's avatar
Laurent Montel committed
489
490
    KWalletContainerItem *ci = nullptr;
    KWalletFolderItem *fi = nullptr;
Teo Mrnjavac's avatar
Teo Mrnjavac committed
491
492
493
494
495
496
497
498
499

    // clear the context menu
    _contextMenu->clear();
    _contextMenu->setEnabled(true);
    // disable the entry actions (reenable them on adding)
    _newEntryAction->setEnabled(false);
    _renameEntryAction->setEnabled(false);
    _deleteEntryAction->setEnabled(false);

Teo Mrnjavac's avatar
Teo Mrnjavac committed
500
    if (item) {
Teo Mrnjavac's avatar
Teo Mrnjavac committed
501
        // set the context menu's title
Laurent Montel's avatar
Laurent Montel committed
502
        _contextMenu->addSection(_contextMenu->fontMetrics().elidedText(
Teo Mrnjavac's avatar
Teo Mrnjavac committed
503
                                   item->text(0), Qt::ElideMiddle, 200));
Teo Mrnjavac's avatar
Teo Mrnjavac committed
504
505
506

        // TODO rtti
        switch (item->type()) {
Teo Mrnjavac's avatar
Teo Mrnjavac committed
507
508
509
510
511
512
513
514
515
516
517
        case KWalletEntryItemClass:
            ci = dynamic_cast<KWalletContainerItem *>(item->parent());
            if (!ci) {
                return;
            }
            fi = dynamic_cast<KWalletFolderItem *>(ci->parent());
            if (!fi) {
                return;
            }
            _w->setFolder(fi->name());
            _deleteFolderAction->setEnabled(false);
Teo Mrnjavac's avatar
Teo Mrnjavac committed
518

Teo Mrnjavac's avatar
Teo Mrnjavac committed
519
520
521
522
523
524
525
            // add standard menu items
            _contextMenu->addAction(_newEntryAction);
            _contextMenu->addAction(_renameEntryAction);
            _contextMenu->addAction(_deleteEntryAction);
            _newEntryAction->setEnabled(true);
            _renameEntryAction->setEnabled(true);
            _deleteEntryAction->setEnabled(true);
Teo Mrnjavac's avatar
Teo Mrnjavac committed
526

Teo Mrnjavac's avatar
Teo Mrnjavac committed
527
528
529
530
531
532
533
534
535
            if (ci->entryType() == KWallet::Wallet::Password) {
                QString pass;
                if (_w->readPassword(item->text(0), pass) == 0) {
                    _entryStack->setCurrentIndex(4);
                    _entryName->setText(i18n("Password: %1",
                                             item->text(0)));
                    _passwordValue->setText(pass);
                    _saveChanges->setEnabled(false);
                    _undoChanges->setEnabled(false);
536
                    _hasUnsavedChanges = false;
Teo Mrnjavac's avatar
Teo Mrnjavac committed
537
                }
Teo Mrnjavac's avatar
Teo Mrnjavac committed
538
539
540
541
                // add a context-menu action for copying passwords
                _contextMenu->addSeparator();
                _contextMenu->addAction(_copyPassAction);
                if (_alwaysShowContents) {
542
                    QTimer::singleShot(0, this, &KWalletEditor::showPasswordContents);
Teo Mrnjavac's avatar
Teo Mrnjavac committed
543
                }
Teo Mrnjavac's avatar
Teo Mrnjavac committed
544
545
546
547
548
549
550
            } else if (ci->entryType() == KWallet::Wallet::Map) {
                _entryStack->setCurrentIndex(2);
                if (_w->readMap(item->text(0), _currentMap) == 0) {
                    _mapEditor->reload();
                    _entryName->setText(i18n("Name-Value Map: %1", item->text(0)));
                    _saveChanges->setEnabled(false);
                    _undoChanges->setEnabled(false);
551
                    _hasUnsavedChanges = false;
Teo Mrnjavac's avatar
Teo Mrnjavac committed
552
                    showHideMapEditorValue(_mapEditorShowHide->isChecked());
Teo Mrnjavac's avatar
Teo Mrnjavac committed
553
                }
Teo Mrnjavac's avatar
Teo Mrnjavac committed
554
555
556
557
558
559
560
561
            } else if (ci->entryType() == KWallet::Wallet::Stream) {
                _entryStack->setCurrentIndex(3);
                QByteArray ba;
                if (_w->readEntry(item->text(0), ba) == 0) {
                    _entryName->setText(i18n("Binary Data: %1",
                                             item->text(0)));
                    _saveChanges->setEnabled(false);
                    _undoChanges->setEnabled(false);
562
                    _hasUnsavedChanges = false;
Rafał Rzepecki's avatar
Rafał Rzepecki committed
563
                    _binaryView->setData(ba);
Teo Mrnjavac's avatar
Teo Mrnjavac committed
564
565
566
                }
            }
            break;
Teo Mrnjavac's avatar
Teo Mrnjavac committed
567

Teo Mrnjavac's avatar
Teo Mrnjavac committed
568
569
570
571
572
573
574
575
576
577
578
579
580
        case KWalletContainerItemClass:
            ci = dynamic_cast<KWalletContainerItem *>(item);
            if (!ci) {
                return;
            }
            if (ci->entryType() == KWallet::Wallet::Unknown) {
                // disable context menu on unknown items
                _contextMenu->setEnabled(false);
            } else {
                // add the context menu action
                _contextMenu->addAction(_newEntryAction);
                _newEntryAction->setEnabled(true);
            }
Teo Mrnjavac's avatar
Teo Mrnjavac committed
581

Teo Mrnjavac's avatar
Teo Mrnjavac committed
582
583
584
585
586
587
588
589
590
            fi = dynamic_cast<KWalletFolderItem *>(item->parent());
            if (!fi) {
                return;
            }
            _w->setFolder(fi->name());
            _deleteFolderAction->setEnabled(false);
            _entryName->clear();
            _entryStack->setCurrentIndex(0);
            break;
Teo Mrnjavac's avatar
Teo Mrnjavac committed
591

Teo Mrnjavac's avatar
Teo Mrnjavac committed
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
        case KWalletFolderItemClass:
            // add the context menu actions
            _contextMenu->addAction(_newFolderAction);
            _contextMenu->addAction(_deleteFolderAction);

            fi = dynamic_cast<KWalletFolderItem *>(item);
            if (!fi) {
                return;
            }
            _w->setFolder(fi->name());
            _deleteFolderAction->setEnabled(true);
            _entryName->clear();
            _entryStack->setCurrentIndex(0);
            break;

        default:
            // all items but Unknown entries return have their
            // rtti set. Unknown items can only be deleted.
            _contextMenu->addAction(_deleteEntryAction);
            _deleteEntryAction->setEnabled(true);
            break;
Teo Mrnjavac's avatar
Teo Mrnjavac committed
613
614
615
        }
    } else {
        // no item selected. add the "new folder" action to the context menu
Teo Mrnjavac's avatar
Teo Mrnjavac committed
616
        _contextMenu->addAction(_newFolderAction);
Teo Mrnjavac's avatar
Teo Mrnjavac committed
617
618
619
620
    }

    if (fi) {
        _currentFolder = fi->name();
Laurent Montel's avatar
Laurent Montel committed
621
        _entryTitle->setText(QStringLiteral("<font size=\"+1\">%1</font>").arg(fi->text(0)));
622
        _iconTitle->setPixmap(fi->getFolderIcon().pixmap(KIconLoader::global()->currentSize(KIconLoader::Toolbar)));
Teo Mrnjavac's avatar
Teo Mrnjavac committed
623
    }
Teo Mrnjavac's avatar
Teo Mrnjavac committed
624

Teo Mrnjavac's avatar
Teo Mrnjavac committed
625
    _displayedItem = item;
626
627
}

Teo Mrnjavac's avatar
Teo Mrnjavac committed
628
629
630
void KWalletEditor::updateEntries(const QString &folder)
{
    QStack<QTreeWidgetItem *> trash;
Teo Mrnjavac's avatar
Teo Mrnjavac committed
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650

    _w->setFolder(folder);
    const QStringList entries = _w->entryList();

    KWalletFolderItem *fi = _entryList->getFolder(folder);

    if (!fi) {
        return;
    }

    KWalletContainerItem *pi = fi->getContainer(KWallet::Wallet::Password);
    KWalletContainerItem *mi = fi->getContainer(KWallet::Wallet::Map);
    KWalletContainerItem *bi = fi->getContainer(KWallet::Wallet::Stream);
    KWalletContainerItem *ui = fi->getContainer(KWallet::Wallet::Unknown);

    // Remove deleted entries
    for (int i = 0; i < pi->childCount(); ++i) {
        QTreeWidgetItem *twi = pi->child(i);
        if (!entries.contains(twi->text(0))) {
            if (twi == _entryList->currentItem()) {
Laurent Montel's avatar
Laurent Montel committed
651
                entrySelectionChanged(nullptr);
Teo Mrnjavac's avatar
Teo Mrnjavac committed
652
653
654
655
656
657
658
659
660
            }
            trash.push(twi);
        }
    }

    for (int i = 0; i < mi->childCount(); ++i) {
        QTreeWidgetItem *twi = mi->child(i);
        if (!entries.contains(twi->text(0))) {
            if (twi == _entryList->currentItem()) {
Laurent Montel's avatar
Laurent Montel committed
661
                entrySelectionChanged(nullptr);
Teo Mrnjavac's avatar
Teo Mrnjavac committed
662
663
664
665
666
667
668
669
670
            }
            trash.push(twi);
        }
    }

    for (int i = 0; i < bi->childCount(); ++i) {
        QTreeWidgetItem *twi = bi->child(i);
        if (!entries.contains(twi->text(0))) {
            if (twi == _entryList->currentItem()) {
Laurent Montel's avatar
Laurent Montel committed
671
                entrySelectionChanged(nullptr);
Teo Mrnjavac's avatar
Teo Mrnjavac committed
672
673
674
675
676
677
678
679
680
            }
            trash.push(twi);
        }
    }

    for (int i = 0; i < ui->childCount(); ++i) {
        QTreeWidgetItem *twi = ui->child(i);
        if (!entries.contains(twi->text(0))) {
            if (twi == _entryList->currentItem()) {
Laurent Montel's avatar
Laurent Montel committed
681
                entrySelectionChanged(nullptr);
Teo Mrnjavac's avatar
Teo Mrnjavac committed
682
683
684
685
686
687
688
689
690
            }
            trash.push(twi);
        }
    }

    qDeleteAll(trash);
    trash.clear();

    // Add new entries
691
    for (QStringList::const_iterator i = entries.begin(), end = entries.end(); i != end; ++i) {
Teo Mrnjavac's avatar
Teo Mrnjavac committed
692
        if (fi->contains(*i)) {
Teo Mrnjavac's avatar
Teo Mrnjavac committed
693
694
695
696
            continue;
        }

        switch (_w->entryType(*i)) {
Teo Mrnjavac's avatar
Teo Mrnjavac committed
697
698
699
700
701
702
703
704
705
706
707
708
709
        case KWallet::Wallet::Password:
            new KWalletEntryItem(_w, pi, *i);
            break;
        case KWallet::Wallet::Stream:
            new KWalletEntryItem(_w, bi, *i);
            break;
        case KWallet::Wallet::Map:
            new KWalletEntryItem(_w, mi, *i);
            break;
        case KWallet::Wallet::Unknown:
        default:
            new QTreeWidgetItem(ui, QStringList() << *i);
            break;
Teo Mrnjavac's avatar
Teo Mrnjavac committed
710
711
712
713
        }
    }
    fi->refresh();
    if (fi->name() == _currentFolder) {
Laurent Montel's avatar
Laurent Montel committed
714
        _entryTitle->setText(QStringLiteral("<font size=\"+1\">%1</font>").arg(fi->text(0)));
Teo Mrnjavac's avatar
Teo Mrnjavac committed
715
716
717
718
719
    }
    if (!_entryList->currentItem()) {
        _entryName->clear();
        _entryStack->setCurrentIndex(0);
    }
720
721
}

Teo Mrnjavac's avatar
Teo Mrnjavac committed
722
723
void KWalletEditor::listContextMenuRequested(const QPoint &pos)
{
Teo Mrnjavac's avatar
Teo Mrnjavac committed
724
725
726
    if (!_contextMenu->isEnabled()) {
        return;
    }
727

Teo Mrnjavac's avatar
Teo Mrnjavac committed
728
    _contextMenu->popup(_entryList->mapToGlobal(pos));
729
730
}

Teo Mrnjavac's avatar
Teo Mrnjavac committed
731
732
void KWalletEditor::copyPassword()
{
Teo Mrnjavac's avatar
Teo Mrnjavac committed
733
734
735
736
737
738
739
    QTreeWidgetItem *item = _entryList->currentItem();
    if (_w && item) {
        QString pass;
        if (_w->readPassword(item->text(0), pass) == 0) {
            QApplication::clipboard()->setText(pass);
        }
    }
740
741
}

Teo Mrnjavac's avatar
Teo Mrnjavac committed
742
743
void KWalletEditor::newEntry()
{
Teo Mrnjavac's avatar
Teo Mrnjavac committed
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
    QTreeWidgetItem *item = _entryList->currentItem();
    QString n;
    bool ok;

    KWalletFolderItem *fi;

    //set the folder where we're trying to create the new entry
    if (_w && item) {
        QTreeWidgetItem *p = item;
        if (p->type() == KWalletEntryItemClass) {
            p = item->parent();
        }
        fi = dynamic_cast<KWalletFolderItem *>(p->parent());
        if (!fi) {
            return;
        }
        _w->setFolder(fi->name());
    } else {
        return;
    }

    do {
Laurent Montel's avatar
Laurent Montel committed
766
        n = QInputDialog::getText(this, i18n("New Entry"),
Teo Mrnjavac's avatar
Teo Mrnjavac committed
767
                                  i18n("Please choose a name for the new entry:"),
Laurent Montel's avatar
Laurent Montel committed
768
769
                                  QLineEdit::Normal, QString(),
                                  &ok);
Teo Mrnjavac's avatar
Teo Mrnjavac committed
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800

        if (!ok) {
            return;
        }

        // FIXME: prohibits the use of the subheadings
        if (fi->contains(n)) {
            int rc = KMessageBox::questionYesNo(this, i18n("Sorry, that entry already exists. Try again?"), QString(), KGuiItem(i18n("Try Again")), KGuiItem(i18n("Do Not Try")));
            if (rc == KMessageBox::Yes) {
                continue;
            }
            n.clear();
        }
        break;
    } while (true);

    if (_w && item && !n.isEmpty()) {
        QTreeWidgetItem *p = item;
        if (p->type() == KWalletEntryItemClass) {
            p = item->parent();
        }

        KWalletFolderItem *fi = dynamic_cast<KWalletFolderItem *>(p->parent());
        if (!fi) {
            KMessageBox::error(this, i18n("An unexpected error occurred trying to add the new entry"));
            return;
        }
        _w->setFolder(fi->name());

        KWalletEntryItem *ni = new KWalletEntryItem(_w, p, n);

Teo Mrnjavac's avatar
Teo Mrnjavac committed
801
        KWalletContainerItem *ci = dynamic_cast<KWalletContainerItem *>(p);
Teo Mrnjavac's avatar
Teo Mrnjavac committed
802
803
804
805
806
807
808
809
        if (!ci) {
            KMessageBox::error(this, i18n("An unexpected error occurred trying to add the new entry"));
            delete ni;
            return;
        }
        if (ci->entryType() == KWallet::Wallet::Password) {
            _w->writePassword(n, QString());
        } else if (ci->entryType() == KWallet::Wallet::Map) {
Teo Mrnjavac's avatar
Teo Mrnjavac committed
810
            _w->writeMap(n, QMap<QString, QString>());
Teo Mrnjavac's avatar
Teo Mrnjavac committed
811
812
813
814
815
816
817
818
819
820
        } else if (ci->entryType() == KWallet::Wallet::Stream) {
            _w->writeEntry(n, QByteArray());
        } else {
            abort();
        }

        _entryList->setCurrentItem(ni);
        _entryList->scrollToItem(ni);

        fi->refresh();
Laurent Montel's avatar
Laurent Montel committed
821
        _entryTitle->setText(QStringLiteral("<font size=\"+1\">%1</font>").arg(fi->text(0)));
Teo Mrnjavac's avatar
Teo Mrnjavac committed
822
    }
823
824
}

Teo Mrnjavac's avatar
Teo Mrnjavac committed
825
826
void KWalletEditor::renameEntry()
{
Teo Mrnjavac's avatar
Teo Mrnjavac committed
827
828
829
830
    QTreeWidgetItem *item = _entryList->currentItem();
    if (_w && item) {
        _entryList->editItem(item, 0);
    }
831
832
833
}

// Only supports renaming of KWalletEntryItem derived classes.
Teo Mrnjavac's avatar
Teo Mrnjavac committed
834
835
void KWalletEditor::listItemChanged(QTreeWidgetItem *item, int column)
{
Teo Mrnjavac's avatar
Teo Mrnjavac committed
836
    if (item && column == 0) {
Teo Mrnjavac's avatar
Teo Mrnjavac committed
837
        KWalletEntryItem *i = dynamic_cast<KWalletEntryItem *>(item);
Teo Mrnjavac's avatar
Teo Mrnjavac committed
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
        if (!i) {
            return;
        }

        const QString t = item->text(0);
        if (t == i->name()) {
            return;
        }
        if (!_w || t.isEmpty()) {
            i->restoreName();
            return;
        }

        if (_w->renameEntry(i->name(), t) == 0) {
            i->setName(t);
Teo Mrnjavac's avatar
Teo Mrnjavac committed
853
            KWalletContainerItem *ci = dynamic_cast<KWalletContainerItem *>(item->parent());
Teo Mrnjavac's avatar
Teo Mrnjavac committed
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
            if (!ci) {
                KMessageBox::error(this, i18n("An unexpected error occurred trying to rename the entry"));
                return;
            }
            if (ci->entryType() == KWallet::Wallet::Password) {
                _entryName->setText(i18n("Password: %1", item->text(0)));
            } else if (ci->entryType() == KWallet::Wallet::Map) {
                _entryName->setText(i18n("Name-Value Map: %1", item->text(0)));
            } else if (ci->entryType() == KWallet::Wallet::Stream) {
                _entryName->setText(i18n("Binary Data: %1", item->text(0)));
            }
        } else {
            i->restoreName();
        }
    }
869
870
}

Teo Mrnjavac's avatar
Teo Mrnjavac committed
871
872
void KWalletEditor::deleteEntry()
{
Teo Mrnjavac's avatar
Teo Mrnjavac committed
873
874
    QTreeWidgetItem *item = _entryList->currentItem();
    if (_w && item) {
Teo Mrnjavac's avatar
Teo Mrnjavac committed
875
        int rc = KMessageBox::warningContinueCancel(this, i18n("Are you sure you wish to delete the item '%1'?", item->text(0)), QString(), KStandardGuiItem::del());
Teo Mrnjavac's avatar
Teo Mrnjavac committed
876
877
878
879
880
881
        if (rc == KMessageBox::Continue) {
            KWalletFolderItem *fi = dynamic_cast<KWalletFolderItem *>(item->parent()->parent());
            if (!fi) {
                KMessageBox::error(this, i18n("An unexpected error occurred trying to delete the entry"));
                return;
            }
Laurent Montel's avatar
Laurent Montel committed
882
            _displayedItem = nullptr;
Teo Mrnjavac's avatar
Teo Mrnjavac committed
883
884
885
886
            _w->removeEntry(item->text(0));
            delete item;
            entrySelectionChanged(_entryList->currentItem());
            fi->refresh();
Laurent Montel's avatar
Laurent Montel committed
887
            _entryTitle->setText(QStringLiteral("<font size=\"+1\">%1</font>").arg(fi->text(0)));
Teo Mrnjavac's avatar
Teo Mrnjavac committed
888
889
        }
    }
890
891
}

Teo Mrnjavac's avatar
Teo Mrnjavac committed
892
893
void KWalletEditor::changePassword()
{
Teo Mrnjavac's avatar
Teo Mrnjavac committed
894
    KWallet::Wallet::changePassword(_walletName, effectiveWinId());
George Staikos's avatar
George Staikos committed
895
896
}

Teo Mrnjavac's avatar
Teo Mrnjavac committed
897
898
void KWalletEditor::walletOpened(bool success)
{
Teo Mrnjavac's avatar
Teo Mrnjavac committed
899
900
901
902
903
904
905
906
907
908
909
    if (success) {
        emit enableFolderActions(true);
        emit enableContextFolderActions(false);
        emit enableWalletActions(true);
        updateFolderList();
        _entryList->setWallet(_w);
    } else {
        if (!_newWallet) {
            KMessageBox::sorry(this, i18n("Unable to open the requested wallet."));
        }
    }
910
911
}

Teo Mrnjavac's avatar
Teo Mrnjavac committed
912
913
void KWalletEditor::hidePasswordContents()
{
Teo Mrnjavac's avatar
Teo Mrnjavac committed
914
    _entryStack->setCurrentIndex(4);
915
916
}

Teo Mrnjavac's avatar
Teo Mrnjavac committed
917
918
void KWalletEditor::showPasswordContents()
{
Teo Mrnjavac's avatar
Teo Mrnjavac committed
919
    _entryStack->setCurrentIndex(1);
920
921
}

Teo Mrnjavac's avatar
Teo Mrnjavac committed
922
923
void KWalletEditor::showHideMapEditorValue(bool show)
{
Teo Mrnjavac's avatar
Teo Mrnjavac committed
924
925
926
927
928
    if (show) {
        _mapEditor->showColumn(2);
    } else {
        _mapEditor->hideColumn(2);
    }
George Staikos's avatar
George Staikos committed
929
930
}

931
932
enum MergePlan { Prompt = 0, Always = 1, Never = 2, Yes = 3, No = 4 };

Teo Mrnjavac's avatar
Teo Mrnjavac committed
933
934
void KWalletEditor::importWallet()
{
Laurent Montel's avatar
Laurent Montel committed
935
    QUrl url = QFileDialog::getOpenFileUrl(this, QString(), QUrl(), QStringLiteral("*.kwl"));
936

Teo Mrnjavac's avatar
Teo Mrnjavac committed
937
938
939
    if (url.isEmpty()) {
        return;
    }
940

941
942
943
944
945
946
947
948
949
    QTemporaryFile tmpFile;
    if (!tmpFile.open()) {
        KMessageBox::sorry(this, i18n("Unable to create temporary file for downloading '<b>%1</b>'.", url.toDisplayString()));
        return;
    }

    KIO::StoredTransferJob *job = KIO::storedGet(url);
    KJobWidgets::setWindow(job, this);
    if (!job->exec()) {
950
        KMessageBox::sorry(this, i18n("Unable to access wallet '<b>%1</b>'.", url.toDisplayString()));
Teo Mrnjavac's avatar
Teo Mrnjavac committed
951
952
        return;
    }
953
954
    tmpFile.write(job->data());
    tmpFile.flush();
Teo Mrnjavac's avatar
Teo Mrnjavac committed
955

956
    KWallet::Wallet *w = KWallet::Wallet::openWallet(tmpFile.fileName(), effectiveWinId(), KWallet::Wallet::Path);
Teo Mrnjavac's avatar
Teo Mrnjavac committed
957
958
959
960
961
962
963
964
965
966
967
968
969
970
    if (w && w->isOpen()) {
        MergePlan mp = Prompt;
        QStringList fl = w->folderList();
        for (QStringList::ConstIterator f = fl.constBegin(); f != fl.constEnd(); ++f) {
            if (!w->setFolder(*f)) {
                continue;
            }

            if (!_w->hasFolder(*f)) {
                _w->createFolder(*f);
            }

            _w->setFolder(*f);

971
972
            bool readMap = false;
            QMap<QString, QMap<QString, QString>> map = w->mapList(&readMap);
Teo Mrnjavac's avatar
Teo Mrnjavac committed
973
            QSet<QString> mergedkeys; // prevents re-merging already merged entries.
974
            if (readMap) {
Teo Mrnjavac's avatar
Teo Mrnjavac committed
975
976
977
978
979
980
                QMap<QString, QMap<QString, QString> >::ConstIterator me;
                for (me = map.constBegin(); me != map.constEnd(); ++me) {
                    bool hasEntry = _w->hasEntry(me.key());
                    if (hasEntry && mp == Prompt) {
                        KBetterThanKDialogBase *bd;
                        bd = new KBetterThanKDialogBase(this);
981
                        bd->setLabel(i18n("Folder '<b>%1</b>' already contains an entry '<b>%2</b>'.  Do you wish to replace it?", f->toHtmlEscaped(), me.key().toHtmlEscaped()));
Laurent Montel's avatar
Laurent Montel committed
982
                        mp = static_cast<MergePlan>(bd->exec());
Teo Mrnjavac's avatar
Teo Mrnjavac committed
983
984
985
986
987
988
                        delete bd;
                        bool ok = false;
                        if (mp == Always || mp == Yes) {
                            ok = true;
                        }
                        if (mp == Yes || mp == No) {
Teo Mrnjavac's avatar
Teo Mrnjavac committed
989
                            // reset mp
Teo Mrnjavac's avatar
Teo Mrnjavac committed
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
                            mp = Prompt;
                        }
                        if (!ok) {
                            continue;
                        }
                    } else if (hasEntry && mp == Never) {
                        continue;
                    }
                    _w->writeMap(me.key(), me.value());
                    mergedkeys.insert(me.key()); // remember this key has been merged
                }
            }

1003
1004
1005
            bool readPassList = false;
            QMap<QString, QString> pwd = w->passwordList(&readPassList);
            if (readPassList) {
Teo Mrnjavac's avatar
Teo Mrnjavac committed
1006
1007
1008
1009
                QMap<QString, QString>::ConstIterator pe;
                for (pe = pwd.constBegin(); pe != pwd.constEnd(); ++pe) {
                    bool hasEntry = _w->hasEntry(pe.key());
                    if (hasEntry && mp == Prompt) {
1010
                        KBetterThanKDialogBase *bd = new KBetterThanKDialogBase(this);
1011
                        bd->setLabel(i18n("Folder '<b>%1</b>' already contains an entry '<b>%2</b>'.  Do you wish to replace it?", f->toHtmlEscaped(), pe.key().toHtmlEscaped()));
Laurent Montel's avatar
Laurent Montel committed
1012
                        mp = static_cast<MergePlan>(bd->exec());
Teo Mrnjavac's avatar
Teo Mrnjavac committed
1013
1014
1015
1016
1017
1018
                        delete bd;
                        bool ok = false;
                        if (mp == Always || mp == Yes) {
                            ok = true;
                        }
                        if (mp == Yes || mp == No) {
Teo Mrnjavac's avatar
Teo Mrnjavac committed
1019
                            // reset mp
Teo Mrnjavac's avatar
Teo Mrnjavac committed
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
                            mp = Prompt;
                        }
                        if (!ok) {
                            continue;
                        }
                    } else if (hasEntry && mp == Never) {
                        continue;
                    }
                    _w->writePassword(pe.key(), pe.value());
                    mergedkeys.insert(pe.key()); // remember this key has been merged
                }
            }

1033
1034
1035
            bool readEntries = false;
            QMap<QString, QByteArray> ent = w->entriesList(&readEntries);
            if (readEntries) {
Teo Mrnjavac's avatar
Teo Mrnjavac committed
1036
1037
1038
1039
1040
1041
1042
1043
                QMap<QString, QByteArray>::ConstIterator ee;
                for (ee = ent.constBegin(); ee != ent.constEnd(); ++ee) {
                    // prevent re-merging already merged entries.
                    if (mergedkeys.contains(ee.key())) {
                        continue;
                    }
                    bool hasEntry = _w->hasEntry(ee.key());
                    if (hasEntry && mp == Prompt) {
1044
                        KBetterThanKDialogBase *bd = new KBetterThanKDialogBase(this);
1045
                        bd->setLabel(i18n("Folder '<b>%1</b>' already contains an entry '<b>%2</b>'.  Do you wish to replace it?", f->toHtmlEscaped(), ee.key().toHtmlEscaped()));
Laurent Montel's avatar
Laurent Montel committed
1046
                        mp = static_cast<MergePlan>(bd->exec());
Teo Mrnjavac's avatar
Teo Mrnjavac committed
1047
1048
1049
1050
1051
1052
                        delete bd;
                        bool ok = false;
                        if (mp == Always || mp == Yes) {
                            ok = true;
                        }
                        if (mp == Yes || mp == No) {
Teo Mrnjavac's avatar
Teo Mrnjavac committed
1053
                            // reset mp
Teo Mrnjavac's avatar
Teo Mrnjavac committed
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
                            mp = Prompt;
                        }
                        if (!ok) {
                            continue;
                        }
                    } else if (hasEntry && mp == Never) {
                        continue;
                    }
                    _w->writeEntry(ee.key(), ee.value());
                }
            }
        }
    }

    delete w;

    updateFolderList(true);
    restoreEntry();
1072
1073
}

Teo Mrnjavac's avatar
Teo Mrnjavac committed
1074
1075
void KWalletEditor::importXML()
{
1076
    const QUrl url = QFileDialog::getOpenFileUrl(this, QString(), QUrl(), QStringLiteral("*.xml"));
1077
1078

    if (url.isEmpty()) {
Teo Mrnjavac's avatar
Teo Mrnjavac committed
1079
1080
        return;
    }
1081

1082
1083
1084
    KIO::StoredTransferJob *job = KIO::storedGet(url);
    KJobWidgets::setWindow(job,