File indexing completed on 2024-05-19 05:55:47

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2013 Valentin Rusu <kde@rusu.info>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "kwalletmanagerwidget.h"
0009 #include "kwalletmanagerwidgetitem.h"
0010 #include "kwallet_interface.h"
0011 #include "kwalletmanager_debug.h"
0012 
0013 #include <KLocalizedString>
0014 #include <KMessageBox>
0015 #include <KWallet>
0016 #include <QDragEnterEvent>
0017 #include <QUrl>
0018 #include <QVBoxLayout>
0019 
0020 KWalletManagerWidget::KWalletManagerWidget(QWidget *parent, Qt::WindowFlags /*flags*/):
0021     KPageWidget(parent)
0022 {
0023     setFaceType(Auto);
0024     setAcceptDrops(true);
0025 
0026     connect(this, &KWalletManagerWidget::currentPageChanged, this, &KWalletManagerWidget::onCurrentPageChanged);
0027 }
0028 
0029 KWalletManagerWidget::~KWalletManagerWidget() = default;
0030 
0031 void KWalletManagerWidget::onCurrentPageChanged(KPageWidgetItem */*current*/, KPageWidgetItem */*before*/)
0032 {
0033 
0034 }
0035 
0036 void KWalletManagerWidget::updateWalletDisplay(const QString &selectWallet /* = QString() */)
0037 {
0038     // NOTE: this method is called upon several kwalletd events
0039     static bool alreadyUpdating = false;
0040     if (alreadyUpdating) {
0041         return;
0042     }
0043 
0044     alreadyUpdating = true;
0045     // find out pages corresponding to deleted wallets
0046     const QStringList wl = KWallet::Wallet::walletList();
0047     WalletPagesHash::iterator p = _walletPages.begin();
0048     while (p != _walletPages.end()) {
0049         if (!wl.contains(p.key())) {
0050             // remove the page corresponding to the missing wallet
0051             removePage(p.value());
0052             p = _walletPages.erase(p);
0053         } else {
0054             ++p;
0055         }
0056     }
0057 
0058     // add new wallets
0059     for (QStringList::const_iterator i = wl.begin(); i != wl.end(); ++i) {
0060         const QString &name = *i;
0061         if (!_walletPages.contains(name)) {
0062             auto wi = new KWalletManagerWidgetItem(this, name);
0063             addPage(wi);
0064             _walletPages.insert(*i, wi);
0065         }
0066     }
0067 
0068     // update existing wallets display, e.g. icon
0069     WalletPagesHash::const_iterator cp = _walletPages.constBegin();
0070     WalletPagesHash::const_iterator cend = _walletPages.constEnd();
0071     for (; cp != cend; cp++) {
0072         cp.value()->updateWalletDisplay();
0073     }
0074 
0075     if (!selectWallet.isEmpty()) {
0076         setCurrentPage(_walletPages[selectWallet]);
0077     }
0078     alreadyUpdating = false;
0079 }
0080 
0081 bool KWalletManagerWidget::hasWallet(const QString &name) const
0082 {
0083     return _walletPages.contains(name);
0084 }
0085 
0086 bool KWalletManagerWidget::openWalletFile(const QString &/*path*/)
0087 {
0088     Q_ASSERT(0);
0089     // TODO: implement this method: add a new tab with an editor centered on a file
0090     return false;
0091 }
0092 
0093 bool KWalletManagerWidget::openWallet(const QString &name)
0094 {
0095     bool result = false;
0096     if (_walletPages.contains(name)) {
0097         KWalletManagerWidgetItem *wi = _walletPages[name];
0098         setCurrentPage(wi);
0099         result = wi->openWallet();
0100     }
0101     return result;
0102 }
0103 
0104 QString KWalletManagerWidget::activeWalletName() const
0105 {
0106     auto page = qobject_cast<KWalletManagerWidgetItem *>(currentPage());
0107     if (page) {
0108         return page->walletName();
0109     } else {
0110         return {};
0111     }
0112 }
0113 
0114 void KWalletManagerWidget::dragEnterEvent(QDragEnterEvent *e)
0115 {
0116     if (e->mimeData()->hasFormat(QStringLiteral("application/x-kwallet-wallet"))) {
0117         e->accept();
0118     } else {
0119         e->ignore();
0120     }
0121 }
0122 
0123 void KWalletManagerWidget::dragMoveEvent(QDragMoveEvent */*e*/)
0124 {
0125     qCDebug(KWALLETMANAGER_LOG) << "KWalletManagerWidget::dragMoveEvent";
0126 //     KUrl dummy;
0127 //     QListWidgetItem *dummy2;
0128 //     if (shouldIgnoreDropEvent(e, &dummy, &dummy2)) {
0129 //         e->ignore();
0130 //     } else {
0131 //         e->accept();
0132 //     }
0133 }
0134 
0135 void KWalletManagerWidget::dropEvent(QDropEvent */*e*/)
0136 {
0137     qCDebug(KWALLETMANAGER_LOG) << "KWalletManagerWidget::dropEvent";
0138 //     KUrl u;
0139 //     QListWidgetItem *item;
0140 //     if (shouldIgnoreDropEvent(e, &u, &item)) {
0141 //         e->ignore();
0142 //         return;
0143 //     }
0144 //
0145 //     if (!item) {
0146 //         // Not dropped over an item thus it is a wallet
0147 //         const QString dest = KGlobal::dirs()->saveLocation("kwallet") + u.fileName();
0148 //         if (QFile::exists(dest)) {
0149 //             KMessageBox::error(viewport(), i18n("That wallet file already exists.  You cannot overwrite wallets."));
0150 //             e->ignore();
0151 //             return;
0152 //         }
0153 //
0154 //         // FIXME: verify that it is a real wallet file first
0155 //         KIO::NetAccess::file_copy(u, KUrl(dest));
0156 //         e->accept();
0157 //     } else {
0158 //         // Dropped over an item thus it is a folder
0159 //         KWalletItem *kwi = dynamic_cast<KWalletItem *>(item);
0160 //         Q_ASSERT(kwi);
0161 //         if (kwi) {
0162 //             kwi->processDropEvent(e);
0163 //         }
0164 //     }
0165 }
0166 
0167 bool KWalletManagerWidget::shouldIgnoreDropEvent(const QDropEvent */*e*/, QUrl */*u*/) const
0168 {
0169     return false;
0170 //     if (e->source() == viewport()) {
0171 //         return true;
0172 //     }
0173 //
0174 //     if (!e->provides("application/x-kwallet-folder") &&
0175 //         !e->provides("application/x-kwallet-wallet") &&
0176 //         !e->provides("text/uri-list")) {
0177 //         return true;
0178 //     }
0179 //
0180 //     // Over wallets folders, over nothing wallets
0181 //     *item = itemAt(e->pos());
0182 //     const QByteArray edata = e->encodedData(item ? "application/x-kwallet-folder" : "application/x-kwallet-wallet");
0183 //     *u = decodeUrl(edata);
0184 //     if (*u == KUrl()) {
0185 //         *u = decodeUrl(e->encodedData("text/uri-list"));
0186 //     }
0187 //
0188 //     return *u == KUrl();
0189 }
0190 
0191 bool KWalletManagerWidget::hasUnsavedChanges(const QString &name) const
0192 {
0193     if (name.isEmpty()) {
0194         WalletPagesHash::const_iterator cp   = _walletPages.constBegin();
0195         WalletPagesHash::const_iterator cend = _walletPages.constEnd();
0196         for (; cp != cend; ++cp) {
0197             if (cp.value()->hasUnsavedChanges()) {
0198                 return true;
0199             }
0200         }
0201         return false;
0202     } else {
0203         WalletPagesHash::const_iterator it = _walletPages.find(name);
0204         if (it == _walletPages.constEnd()) {
0205                 return false;
0206         }
0207         return it.value()->hasUnsavedChanges();
0208     }
0209 }
0210 
0211 void KWalletManagerWidget::setErrorMessage(const QString &message) {
0212     if (!_walletPages.isEmpty()) {
0213         return;
0214     }
0215 
0216     if (_errorItem == nullptr) {
0217         auto placeholderWidget = new QWidget();
0218 
0219         auto layout = new QVBoxLayout();
0220         placeholderWidget->setLayout(layout);
0221 
0222         _errorLabel =
0223             new QLabel(xi18nc("@info:status",
0224                 "An error occurred when connecting to the KWallet service:<nl/>%1",
0225                 message));
0226         _errorLabel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
0227         layout->addWidget(_errorLabel);
0228 
0229         _errorItem = new KPageWidgetItem(placeholderWidget);
0230     }
0231 
0232     addPage(_errorItem);
0233     setCurrentPage(_errorItem);
0234 }
0235 
0236 #include "moc_kwalletmanagerwidget.cpp"