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

0001 /*
0002     SPDX-FileCopyrightText: 2013 Valentin Rusu <kde@rusu.info>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "walletcontrolwidget.h"
0008 #include "kwalleteditor.h"
0009 #include "applicationsmanager.h"
0010 #include "kwalletmanager_debug.h"
0011 
0012 #include <KLocalizedString>
0013 #include <KMessageBox>
0014 #include <QMenu>
0015 #include <KWallet>
0016 
0017 
0018 #include <QTimer>
0019 #include <kwidgetsaddons_version.h>
0020 
0021 WalletControlWidget::WalletControlWidget(QWidget *parent, const QString &walletName):
0022     QWidget(parent),
0023     _walletName(walletName)
0024 {
0025     setupUi(this);
0026     onSetupWidget();
0027 
0028     QTimer::singleShot(1, this, &WalletControlWidget::onSetupWidget);
0029 }
0030 
0031 bool WalletControlWidget::openWallet()
0032 {
0033     bool result = false;
0034     if (_wallet && _wallet->isOpen()) {
0035         result = true; // already opened
0036     } else {
0037         _wallet = KWallet::Wallet::openWallet(_walletName, effectiveWinId());
0038         result = _wallet != nullptr;
0039         onSetupWidget();
0040     }
0041     return result;
0042 }
0043 
0044 void WalletControlWidget::onSetupWidget()
0045 {
0046     if (KWallet::Wallet::isOpen(_walletName)) {
0047         if (!_wallet) {
0048             _wallet = KWallet::Wallet::openWallet(_walletName, effectiveWinId());
0049             if (!_wallet) {
0050                 qCDebug(KWALLETMANAGER_LOG) << "Weird situation: wallet could not be opened when setting-up the widget.";
0051             }
0052         }
0053     }
0054 
0055     if (_wallet) {
0056         connect(_wallet, &KWallet::Wallet::walletClosed, this, &WalletControlWidget::onWalletClosed);
0057         _openClose->setText(i18n("&Close"));
0058 
0059         if (!_walletEditor) {
0060             _walletEditor = new KWalletEditor(_editorFrame);
0061             _editorFrameLayout->addWidget(_walletEditor);
0062             _walletEditor->setVisible(true);
0063         }
0064         _walletEditor->setWallet(_wallet);
0065 
0066         if (!_applicationsManager) {
0067             _applicationsManager = new ApplicationsManager(_applicationsFrame);
0068             _applicationsFrameLayout->addWidget(_applicationsManager);
0069             _applicationsManager->setVisible(true);
0070         }
0071         _applicationsManager->setWallet(_wallet);
0072 
0073         _changePassword->setEnabled(true);
0074         _stateLabel->setText(i18nc("the 'kdewallet' is currently open (e.g. %1 will be replaced with current wallet name)", "The '%1' wallet is currently open.", _walletName));
0075         _tabs->setTabIcon(0, QIcon::fromTheme(QLatin1String("wallet-open")).pixmap(16));
0076     } else {
0077         _openClose->setText(i18n("&Open..."));
0078 
0079         if (_walletEditor) {
0080             _walletEditor->setVisible(false);
0081             delete _walletEditor;
0082             _walletEditor = nullptr;
0083         }
0084 
0085         if (_applicationsManager) {
0086             _applicationsManager->setVisible(false);
0087             delete _applicationsManager;
0088             _applicationsManager = nullptr;
0089         }
0090         _changePassword->setEnabled(false);
0091         _stateLabel->setText(i18n("The wallet is currently closed."));
0092         _tabs->setTabIcon(0, QIcon::fromTheme(QStringLiteral("wallet-closed")).pixmap(16));
0093     }
0094 }
0095 
0096 void WalletControlWidget::onOpenClose()
0097 {
0098     // TODO create some fancy animation here to make _walletEditor appear or dissapear in a fancy way
0099     if (_wallet) {
0100         if (hasUnsavedChanges()) {
0101             int choice = KMessageBox::warningTwoActions(this, i18n("Ignore unsaved changes?"), {}, KGuiItem(i18n("Ignore")), KStandardGuiItem::cancel());
0102             if (choice == KMessageBox::ButtonCode::SecondaryAction) {
0103                 return;
0104             }
0105         }
0106         // Wallet is open, attempt close it
0107         int rc = KWallet::Wallet::closeWallet(_walletName, false);
0108         if (rc != 0) {
0109             rc = KMessageBox::warningTwoActions(
0110                 this,
0111                 i18n("Unable to close wallet cleanly. It is probably in use by other applications. Do you wish to force it closed?"),
0112                 QString(),
0113                 KGuiItem(i18n("Force Closure")),
0114                 KGuiItem(i18n("Do Not Force")));
0115             if (rc == KMessageBox::ButtonCode::PrimaryAction) {
0116                 rc = KWallet::Wallet::closeWallet(_walletName, true);
0117                 if (rc != 0) {
0118                     KMessageBox::error(this, i18n("Unable to force the wallet closed. Error code was %1.", rc));
0119                 } else {
0120                     _wallet = nullptr;
0121                 }
0122             }
0123         } else {
0124             _wallet = nullptr;
0125         }
0126     } else {
0127         _wallet = KWallet::Wallet::openWallet(_walletName, window()->winId());
0128     }
0129     onSetupWidget();
0130 }
0131 
0132 void WalletControlWidget::onWalletClosed()
0133 {
0134     _wallet = nullptr;
0135     onSetupWidget();
0136 }
0137 
0138 void WalletControlWidget::updateWalletDisplay()
0139 {
0140 //     QList<QAction*> existingActions = _disconnect->actions();
0141 //     QList<QAction*>::const_iterator i = existingActions.constBegin();
0142 //     QList<QAction*>::const_iterator ie = existingActions.constEnd();
0143 //     for ( ; i != ie; i++ ) {
0144 //         _disconnect->removeAction(*i);
0145 //     }
0146 //
0147 }
0148 
0149 void WalletControlWidget::onDisconnectApplication()
0150 {
0151     auto a = qobject_cast<QAction *>(sender());
0152     Q_ASSERT(a);
0153     if (a)  {
0154         KWallet::Wallet::disconnectApplication(_walletName, a->data().toString());
0155     }
0156 }
0157 
0158 void WalletControlWidget::onChangePassword()
0159 {
0160     KWallet::Wallet::changePassword(_walletName, effectiveWinId());
0161 }
0162 
0163 bool WalletControlWidget::hasUnsavedChanges() const
0164 {
0165     return (_walletEditor ? _walletEditor->hasUnsavedChanges() : false);
0166 }
0167 
0168 void WalletControlWidget::hideEvent(QHideEvent *)
0169 {
0170 }
0171 
0172 void WalletControlWidget::showEvent(QShowEvent *)
0173 {
0174 }
0175 
0176 #include "moc_walletcontrolwidget.cpp"