File indexing completed on 2024-04-21 04:56:59

0001 /* This file is part of the KDE project
0002 
0003    Copyright (C) 2008 Urs Wolfer <uwolfer @ kde.org>
0004 
0005    This program is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 */
0010 
0011 #include "dlgwebinterface.h"
0012 
0013 #include "settings.h"
0014 
0015 #include <KLocalizedString>
0016 #include <KMessageBox>
0017 #include <kwallet.h>
0018 
0019 DlgWebinterface::DlgWebinterface(QDialog *parent)
0020     : QWidget(parent)
0021 {
0022     setupUi(this);
0023 
0024     readConfig();
0025 
0026     connect(parent, &QDialog::accepted, this, &DlgWebinterface::saveSettings);
0027     connect(webinterfacePwd, &KPasswordLineEdit::passwordChanged, this, &DlgWebinterface::changed);
0028 }
0029 
0030 DlgWebinterface::~DlgWebinterface()
0031 {
0032     delete m_wallet;
0033 }
0034 
0035 void DlgWebinterface::readConfig()
0036 {
0037     if (Settings::webinterfaceEnabled()) {
0038         m_wallet = KWallet::Wallet::openWallet(KWallet::Wallet::LocalWallet(),
0039                                                winId(), /// Use MainWindow?
0040                                                KWallet::Wallet::Asynchronous);
0041         if (m_wallet) {
0042             connect(m_wallet, &KWallet::Wallet::walletOpened, this, &DlgWebinterface::walletOpened);
0043         } else {
0044             KMessageBox::error(nullptr, i18n("Could not open KWallet"));
0045         }
0046     }
0047 }
0048 
0049 void DlgWebinterface::walletOpened(bool opened)
0050 {
0051     if (opened && (m_wallet->hasFolder("KGet") || m_wallet->createFolder("KGet")) && m_wallet->setFolder("KGet")) {
0052         QString pwd;
0053         m_wallet->readPassword("Webinterface", pwd);
0054         webinterfacePwd->setPassword(pwd);
0055     } else {
0056         KMessageBox::error(nullptr, i18n("Could not open KWallet"));
0057     }
0058 }
0059 
0060 void DlgWebinterface::saveSettings()
0061 {
0062     if (m_wallet) {
0063         m_wallet->writePassword("Webinterface", webinterfacePwd->password());
0064     }
0065     Q_EMIT saved();
0066 }
0067 
0068 #include "moc_dlgwebinterface.cpp"