| 1 | // |
| 2 | // C++ Implementation: Wallet |
| 3 | // |
| 4 | // Description: |
| 5 | // |
| 6 | // |
| 7 | // Author: hooey <[email protected]>, (C) 2009 |
| 8 | // |
| 9 | // Copyright: See COPYING file that comes with this distribution |
| 10 | // |
| 11 | // |
| 12 | |
| 13 | #include "wallet.h" |
| 14 | #include "qterm.h" |
| 15 | #include <QtDBus> |
| 16 | #include <QtDebug> |
| 17 | #include <QtGui/QWidget> |
| 18 | namespace QTerm { |
| 19 | Wallet::Wallet(QObject * parent) |
| 20 | :m_walletName(),m_handle(0) |
| 21 | { |
| 22 | m_wallet = NULL; |
| 23 | } |
| 24 | |
| 25 | Wallet::~Wallet() |
| 26 | { |
| 27 | close(); |
| 28 | } |
| 29 | |
| 30 | bool Wallet::isWalletAvailable() |
| 31 | { |
| 32 | QDBusConnectionInterface* interface = QDBusConnection::sessionBus().interface(); |
| 33 | return interface && interface->isServiceRegistered("org.kde.kwalletd"); |
| 34 | } |
| 35 | |
| 36 | void Wallet::open() |
| 37 | { |
| 38 | if (m_wallet != NULL) { |
| 39 | if (m_wallet->isOpen(m_handle)) { |
| 40 | return; |
| 41 | } else { |
| 42 | emit error("Failed to open the wallet"); |
| 43 | return; |
| 44 | } |
| 45 | } |
| 46 | m_wallet = new OrgKdeKWalletInterface("org.kde.kwalletd","/modules/kwalletd",QDBusConnection::sessionBus(),this); |
| 47 | m_walletName = m_wallet->localWallet(); |
| 48 | // FIXME: how to get win id? |
| 49 | //WId wid = static_cast<QWidget *> (parent())->winId(); |
| 50 | m_handle = m_wallet->open(m_walletName, 0, "QTerm"); |
| 51 | connect(m_wallet,SIGNAL(walletOpened(const QString &)), this, SLOT(walletOpened(const QString &))); |
| 52 | connect(m_wallet,SIGNAL(walletClosed(int)), this, SLOT(walletClosed(int))); |
| 53 | } |
| 54 | |
| 55 | void Wallet::close() |
| 56 | { |
| 57 | if (m_wallet == NULL) { |
| 58 | return; |
| 59 | } |
| 60 | m_wallet->close(m_handle,false,"QTerm"); |
| 61 | delete m_wallet; |
| 62 | m_wallet = NULL; |
| 63 | m_handle = 0; |
| 64 | } |
| 65 | |
| 66 | void Wallet::walletOpened(const QString & walletName) |
| 67 | { |
| 68 | if (!m_wallet->isOpen(m_handle)) { |
| 69 | qDebug() << "Failed to open the wallet"; |
| 70 | emit error("Failed to open the wallet"); |
| 71 | return; |
| 72 | } |
| 73 | if (!m_wallet->hasFolder(m_handle, "QTerm", "QTerm")) { |
| 74 | m_wallet->createFolder(m_handle, "QTerm", "QTerm"); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | void Wallet::walletClosed(int handle) |
| 79 | { |
| 80 | if (handle == m_handle) { |
| 81 | delete m_wallet; |
| 82 | m_wallet = NULL; |
| 83 | m_handle = 0; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | QString Wallet::readPassword(const QString & site, const QString & username) |
| 88 | { |
| 89 | if (!m_wallet->isOpen(m_handle)) { |
| 90 | return QString(); |
| 91 | } |
| 92 | |
| 93 | QString key = site+"_"+username; |
| 94 | QString password=m_wallet->readPassword(m_handle, "QTerm", key, "QTerm"); |
| 95 | return password; |
| 96 | } |
| 97 | |
| 98 | bool Wallet::writePassword(const QString & site, const QString & username, const QString & password) |
| 99 | { |
| 100 | if (!m_wallet->isOpen(m_handle)) { |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | QString key = site+"_"+username; |
| 105 | if (m_wallet->writePassword(m_handle, "QTerm", key, password, "QTerm")==0) { |
| 106 | return true; |
| 107 | } |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | bool Wallet::removePassword(const QString & site, const QString & username) |
| 112 | { |
| 113 | if (!m_wallet->isOpen(m_handle)) { |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | QString key = site+"_"+username; |
| 118 | if (m_wallet->removeEntry(m_handle, "QTerm", key, "QTerm")==0) { |
| 119 | return true; |
| 120 | } |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | } |
| 125 | |
| 126 | #include <moc_wallet.cpp> |