File indexing completed on 2024-05-12 16:46:38

0001 /***************************************************************************
0002     Copyright (C) 2009 Robby Stephenson <robby@periapsis.org>
0003  ***************************************************************************/
0004 
0005 /***************************************************************************
0006  *                                                                         *
0007  *   This program is free software; you can redistribute it and/or         *
0008  *   modify it under the terms of the GNU General Public License as        *
0009  *   published by the Free Software Foundation; either version 2 of        *
0010  *   the License or (at your option) version 3 or any later version        *
0011  *   accepted by the membership of KDE e.V. (or its successor approved     *
0012  *   by the membership of KDE e.V.), which shall act as a proxy            *
0013  *   defined in Section 14 of version 3 of the license.                    *
0014  *                                                                         *
0015  *   This program is distributed in the hope that it will be useful,       *
0016  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0018  *   GNU General Public License for more details.                          *
0019  *                                                                         *
0020  *   You should have received a copy of the GNU General Public License     *
0021  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0022  *                                                                         *
0023  ***************************************************************************/
0024 
0025 #include "wallet.h"
0026 #include "guiproxy.h"
0027 
0028 #include <KWallet>
0029 
0030 #include <QWidget>
0031 
0032 using Tellico::Wallet;
0033 
0034 Tellico::Wallet* Wallet::self() {
0035   static Wallet wallet;
0036   return &wallet;
0037 }
0038 
0039 Wallet::Wallet() : m_wallet(nullptr) {
0040 }
0041 
0042 bool Wallet::prepareWallet() {
0043   if(GUI::Proxy::widget() && (!m_wallet || !m_wallet->isOpen())) {
0044     delete m_wallet;
0045     m_wallet = KWallet::Wallet::openWallet(KWallet::Wallet::NetworkWallet(), GUI::Proxy::widget()->effectiveWinId());
0046   }
0047   if(!m_wallet || !m_wallet->isOpen()) {
0048     delete m_wallet;
0049     m_wallet = nullptr;
0050     return false;
0051   }
0052 
0053   if(!m_wallet->hasFolder(KWallet::Wallet::PasswordFolder()) &&
0054      !m_wallet->createFolder(KWallet::Wallet::PasswordFolder())) {
0055     return false;
0056   }
0057 
0058   return m_wallet->setFolder(KWallet::Wallet::PasswordFolder());
0059 }
0060 
0061 QByteArray Wallet::readWalletEntry(const QString& key_) {
0062   QByteArray value;
0063 
0064   if(!prepareWallet()) {
0065     return value;
0066   }
0067 
0068   if(m_wallet->readEntry(key_, value) != 0) {
0069     return QByteArray();
0070   }
0071 
0072   return value;
0073 }
0074 
0075 QMap<QString, QString> Wallet::readWalletMap(const QString& key_) {
0076   QMap<QString, QString> map;
0077 
0078   if(!prepareWallet()) {
0079     return map;
0080   }
0081 
0082   if(m_wallet->readMap(key_, map) != 0) {
0083     return QMap<QString, QString>();
0084   }
0085 
0086   return map;
0087 }