kwallet.cc
/* This file is part of the KDE project
*
* Copyright (C) 2002-2004 George Staikos <staikos@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "kwallettypes.h"
#include "kwallet.h"
#include <kconfig.h>
#include <kdebug.h>
#include <kdeversion.h>
#include <dcopclient.h>
#include <dcopref.h>
#include <qpopupmenu.h>
#include <qapplication.h>
#include <assert.h>
using namespace KWallet;
const QString Wallet::LocalWallet() {
KConfig cfg("kwalletrc", true);
cfg.setGroup("Wallet");
if (!cfg.readBoolEntry("Use One Wallet", true)) {
QString tmp = cfg.readEntry("Local Wallet", "localwallet");
if (tmp.isEmpty()) {
return "localwallet";
}
return tmp;
}
QString tmp = cfg.readEntry("Default Wallet", "kdewallet");
if (tmp.isEmpty()) {
return "kdewallet";
}
return tmp;
}
const QString Wallet::NetworkWallet() {
KConfig cfg("kwalletrc", true);
cfg.setGroup("Wallet");
QString tmp = cfg.readEntry("Default Wallet", "kdewallet");
if (tmp.isEmpty()) {
return "kdewallet";
}
return tmp;
}
const QString Wallet::PasswordFolder() {
return "Passwords";
}
const QString Wallet::FormDataFolder() {
return "Form Data";
}
Wallet::Wallet(int handle, const QString& name)
: QObject(0L), DCOPObject(), d(0L), _name(name), _handle(handle) {
_dcopRef = new DCOPRef("kded", "kwalletd");
_dcopRef->dcopClient()->setNotifications(true);
connect(_dcopRef->dcopClient(),
SIGNAL(applicationRemoved(const QCString&)),
this,
SLOT(slotAppUnregistered(const QCString&)));
connectDCOPSignal(_dcopRef->app(), _dcopRef->obj(), "walletClosed(int)", "slotWalletClosed(int)", false);
connectDCOPSignal(_dcopRef->app(), _dcopRef->obj(), "folderListUpdated(QString)", "slotFolderListUpdated(QString)", false);
connectDCOPSignal(_dcopRef->app(), _dcopRef->obj(), "folderUpdated(QString, QString)", "slotFolderUpdated(QString, QString)", false);
connectDCOPSignal(_dcopRef->app(), _dcopRef->obj(), "applicationDisconnected(QString, QCString)", "slotApplicationDisconnected(QString, QCString)", false);
// Verify that the wallet is still open
if (_handle != -1) {
DCOPReply r = _dcopRef->call("isOpen", _handle);
if (r.isValid()) {
bool rc = false;
r.get(rc);
if (!rc) {
_handle = -1;
_name = QString::null;
}
}
}
}
Wallet::~Wallet() {
if (_handle != -1) {
_dcopRef->call("close", _handle, false);
_handle = -1;
_folder = QString::null;
_name = QString::null;
}
delete _dcopRef;
_dcopRef = 0L;
}
QStringList Wallet::walletList() {
DCOPReply r = DCOPRef("kded", "kwalletd").call("wallets");
QStringList rc;
if (r.isValid()) {
r.get(rc);
}
return rc;
}
void Wallet::changePassword(const QString& name, WId w) {
DCOPRef("kded", "kwalletd").send("changePassword", name, uint(w));
}
bool Wallet::isEnabled() {
DCOPReply r = DCOPRef("kded", "kwalletd").call("isEnabled");
bool rc = false;
if (r.isValid()) {
r.get(rc);
}
return rc;
}
bool Wallet::isOpen(const QString& name) {
DCOPReply r = DCOPRef("kded", "kwalletd").call("isOpen", name);
bool rc = false;
if (r.isValid()) {
r.get(rc);
}
return rc;
}
int Wallet::closeWallet(const QString& name, bool force) {
DCOPReply r = DCOPRef("kded", "kwalletd").call("close", name, force);
int rc = -1;
if (r.isValid()) {
r.get(rc);
}
return rc;
}
int Wallet::deleteWallet(const QString& name) {
DCOPReply r = DCOPRef("kded", "kwalletd").call("deleteWallet", name);
int rc = -1;
if (r.isValid()) {
r.get(rc);
}
return rc;
}
Wallet *Wallet::openWallet(const QString& name, WId w, OpenType ot) {
if (ot == Asynchronous) {
Wallet *wallet = new Wallet(-1, name);
DCOPRef("kded", "kwalletd").send("openAsynchronous", name, wallet->objId(), uint(w));
return wallet;
}
// avoid deadlock if the app has some popup open (#65978/#71048)
while( QWidget* widget = qApp->activePopupWidget())
widget->close();
bool isPath = ot == Path;
DCOPReply r;
if (isPath) {
r = DCOPRef("kded", "kwalletd").call("openPath", name, uint(w));
} else {
r = DCOPRef("kded", "kwalletd").call("open", name, uint(w));
}
if (r.isValid()) {
int drc = -1;
r.get(drc);
if (drc != -1) {
return new Wallet(drc, name);
}
}
return 0;
}
bool Wallet::disconnectApplication(const QString& wallet, const QCString& app) {
DCOPReply r = DCOPRef("kded", "kwalletd").call("disconnectApplication", wallet, app);
bool rc = false;
if (r.isValid()) {
r.get(rc);
}
return rc;
}
QStringList Wallet::users(const QString& name) {
DCOPReply r = DCOPRef("kded", "kwalletd").call("users", name);
QStringList drc;
if (r.isValid()) {
r.get(drc);
}
return drc;
}
int Wallet::sync() {
if (_handle == -1) {
return -1;
}
_dcopRef->call("sync", _handle);
return 0;
}
int Wallet::lockWallet() {
if (_handle == -1) {
return -1;
}
DCOPReply r = _dcopRef->call("close", _handle, true);
_handle = -1;
_folder = QString::null;
_name = QString::null;
if (r.isValid()) {
int drc = -1;
r.get(drc);
return drc;
}
return -1;
}
const QString& Wallet::walletName() const {
return _name;
}
bool Wallet::isOpen() const {
return _handle != -1;
}
void Wallet::requestChangePassword(WId w) {
if (_handle == -1) {
return;
}
_dcopRef->send("changePassword", _name, uint(w));
}
void Wallet::slotWalletClosed(int handle) {
if (_handle == handle) {
_handle = -1;
_folder = QString::null;
_name = QString::null;
emit walletClosed();
}
}
QStringList Wallet::folderList() {
QStringList rc;
if (_handle == -1) {
return rc;
}
DCOPReply r = _dcopRef->call("folderList", _handle);
if (r.isValid()) {
r.get(rc);
}
return rc;
}
QStringList Wallet::entryList() {
QStringList rc;
if (_handle == -1) {
return rc;
}
DCOPReply r = _dcopRef->call("entryList", _handle, _folder);
if (r.isValid()) {
r.get(rc);
}
return rc;
}
bool Wallet::hasFolder(const QString& f) {
bool rc = false;
if (_handle == -1) {
return rc;
}
DCOPReply r = _dcopRef->call("hasFolder", _handle, f);
if (r.isValid()) {
r.get(rc);
}
return rc;
}
bool Wallet::createFolder(const QString& f) {
bool rc = true;
if (_handle == -1) {
return false;
}
if (!hasFolder(f)) {
DCOPReply r = _dcopRef->call("createFolder", _handle, f);
if (r.isValid()) {
r.get(rc);
}
}
return rc;
}
bool Wallet::setFolder(const QString& f) {
bool rc = false;
if (_handle == -1) {
return rc;
}
// Don't do this - the folder could have disappeared?
#if 0
if (f == _folder) {
return true;
}
#endif
if (hasFolder(f)) {
_folder = f;
rc = true;
}
return rc;
}
bool Wallet::removeFolder(const QString& f) {
bool rc = false;
if (_handle == -1) {
return rc;
}
DCOPReply r = _dcopRef->call("removeFolder", _handle, f);
if (r.isValid()) {
r.get(rc);
}
if (_folder == f) {
setFolder(QString::null);
}
return rc;
}
const QString& Wallet::currentFolder() const {
return _folder;
}
int Wallet::readEntry(const QString& key, QByteArray& value) {
int rc = -1;
if (_handle == -1) {
return rc;
}
DCOPReply r = _dcopRef->call("readEntry", _handle, _folder, key);
if (r.isValid()) {
r.get(value);
rc = 0;
}
return rc;
}
int Wallet::readEntryList(const QString& key, QMap<QString, QByteArray>& value) {
int rc = -1;
if (_handle == -1) {
return rc;
}
DCOPReply r = _dcopRef->call("readEntryList", _handle, _folder, key);
if (r.isValid()) {
r.get(value);
rc = 0;
}
return rc;
}
int Wallet::renameEntry(const QString& oldName, const QString& newName) {
int rc = -1;
if (_handle == -1) {
return rc;
}
DCOPReply r = _dcopRef->call("renameEntry", _handle, _folder, oldName, newName);
if (r.isValid()) {
r.get(rc);
}
return rc;
}
int Wallet::readMap(const QString& key, QMap<QString,QString>& value) {
int rc = -1;
if (_handle == -1) {
return rc;
}
DCOPReply r = _dcopRef->call("readMap", _handle, _folder, key);
if (r.isValid()) {
QByteArray v;
r.get(v);
if (!v.isEmpty()) {
QDataStream ds(v, IO_ReadOnly);
ds >> value;
}
rc = 0;
}
return rc;
}
int Wallet::readMapList(const QString& key, QMap<QString, QMap<QString, QString> >& value) {
int rc = -1;
if (_handle == -1) {
return rc;
}
DCOPReply r = _dcopRef->call("readMapList", _handle, _folder, key);
if (r.isValid()) {
QMap<QString,QByteArray> unparsed;
r.get(unparsed);
for (QMap<QString,QByteArray>::ConstIterator i = unparsed.begin(); i != unparsed.end(); ++i) {
if (!i.data().isEmpty()) {
QDataStream ds(i.data(), IO_ReadOnly);
QMap<QString,QString> v;
ds >> v;
value.insert(i.key(), v);
}
}
rc = 0;
}
return rc;
}
int Wallet::readPassword(const QString& key, QString& value) {
int rc = -1;
if (_handle == -1) {
return rc;
}
DCOPReply r = _dcopRef->call("readPassword", _handle, _folder, key);
if (r.isValid()) {
r.get(value);
rc = 0;
}
return rc;
}
int Wallet::readPasswordList(const QString& key, QMap<QString, QString>& value) {
int rc = -1;
if (_handle == -1) {
return rc;
}
DCOPReply r = _dcopRef->call("readPasswordList", _handle, _folder, key);
if (r.isValid()) {
r.get(value);
rc = 0;
}
return rc;
}
int Wallet::writeEntry(const QString& key, const QByteArray& value, EntryType entryType) {
int rc = -1;
if (_handle == -1) {
return rc;
}
DCOPReply r = _dcopRef->call("writeEntry", _handle, _folder, key, value, int(entryType));
if (r.isValid()) {
r.get(rc);
}
return rc;
}
int Wallet::writeEntry(const QString& key, const QByteArray& value) {
int rc = -1;
if (_handle == -1) {
return rc;
}
DCOPReply r = _dcopRef->call("writeEntry", _handle, _folder, key, value);
if (r.isValid()) {
r.get(rc);
}
return rc;
}
int Wallet::writeMap(const QString& key, const QMap<QString,QString>& value) {
int rc = -1;
if (_handle == -1) {
return rc;
}
QByteArray a;
QDataStream ds(a, IO_WriteOnly);
ds << value;
DCOPReply r = _dcopRef->call("writeMap", _handle, _folder, key, a);
if (r.isValid()) {
r.get(rc);
}
return rc;
}
int Wallet::writePassword(const QString& key, const QString& value) {
int rc = -1;
if (_handle == -1) {
return rc;
}
DCOPReply r = _dcopRef->call("writePassword", _handle, _folder, key, value);
if (r.isValid()) {
r.get(rc);
}
return rc;
}
bool Wallet::hasEntry(const QString& key) {
bool rc = false;
if (_handle == -1) {
return rc;
}
DCOPReply r = _dcopRef->call("hasEntry", _handle, _folder, key);
if (r.isValid()) {
r.get(rc);
}
return rc;
}
int Wallet::removeEntry(const QString& key) {
int rc = -1;
if (_handle == -1) {
return rc;
}
DCOPReply r = _dcopRef->call("removeEntry", _handle, _folder, key);
if (r.isValid()) {
r.get(rc);
}
return rc;
}
Wallet::EntryType Wallet::entryType(const QString& key) {
int rc = 0;
if (_handle == -1) {
return Wallet::Unknown;
}
DCOPReply r = _dcopRef->call("entryType", _handle, _folder, key);
if (r.isValid()) {
r.get(rc);
}
return static_cast<EntryType>(rc);
}
void Wallet::slotAppUnregistered(const QCString& app) {
if (_handle >= 0 && app == "kded") {
slotWalletClosed(_handle);
}
}
void Wallet::slotFolderUpdated(const QString& wallet, const QString& folder) {
if (_name == wallet) {
emit folderUpdated(folder);
}
}
void Wallet::slotFolderListUpdated(const QString& wallet) {
if (_name == wallet) {
emit folderListUpdated();
}
}
void Wallet::slotApplicationDisconnected(const QString& wallet, const QCString& application) {
if (_handle >= 0
&& _name == wallet
&& application == _dcopRef->dcopClient()->appId()) {
slotWalletClosed(_handle);
}
}
void Wallet::walletOpenResult(int id) {
if (_handle != -1) {
// This is BAD.
return;
}
if (id > 0) {
_handle = id;
emit walletOpened(true);
} else if (id < 0) {
emit walletOpened(false);
} // id == 0 => wait
}
bool Wallet::folderDoesNotExist(const QString& wallet, const QString& folder) {
DCOPReply r = DCOPRef("kded", "kwalletd").call("folderDoesNotExist", wallet, folder);
bool rc = true;
if (r.isValid()) {
r.get(rc);
}
return rc;
}
bool Wallet::keyDoesNotExist(const QString& wallet, const QString& folder, const QString& key) {
DCOPReply r = DCOPRef("kded", "kwalletd").call("keyDoesNotExist", wallet, folder, key);
bool rc = true;
if (r.isValid()) {
r.get(rc);
}
return rc;
}
void Wallet::virtual_hook(int, void*) {
//BASE::virtual_hook( id, data );
}
#include "kwallet.moc"
DCOPRef::call
DCOPReply call(const QCString &fun)
KWallet::Wallet::hasEntry
virtual bool hasEntry(const QString &key)
Determine if the current folder has they entry key.
Definition: kwallet.cc:596
DCOPRef::dcopClient
DCOPClient * dcopClient() const
KWallet::Wallet::Wallet
Wallet(int handle, const QString &name)
Construct a KWallet object.
Definition: kwallet.cc:75
KWallet::Wallet::walletName
virtual const QString & walletName() const
The name of the current wallet.
Definition: kwallet.cc:254
KWallet::Wallet::setFolder
virtual bool setFolder(const QString &f)
Set the current working folder to f.
Definition: kwallet.cc:349
KWallet::Wallet::renameEntry
virtual int renameEntry(const QString &oldName, const QString &newName)
Rename the entry oldName to newName.
Definition: kwallet.cc:431
KWallet::Wallet
KDE Wallet.
Definition: kwallet.h:48
KWallet::Wallet::entryType
virtual EntryType entryType(const QString &key)
Determine the type of the entry key in this folder.
Definition: kwallet.cc:628
KWallet::Wallet::folderListUpdated
void folderListUpdated()
Emitted when the folder list is changed in this wallet.
KWallet::Wallet::PasswordFolder
static const QString PasswordFolder()
The standardized name of the password folder.
Definition: kwallet.cc:65
KWallet::Wallet::~Wallet
virtual ~Wallet()
Destroy a KWallet object.
Definition: kwallet.cc:106
DCOPRef
KWallet::Wallet::isEnabled
static bool isEnabled()
Determine if the KDE wallet is enabled.
Definition: kwallet.cc:134
KWallet::Wallet::readMap
virtual int readMap(const QString &key, QMap< QString, QString > &value)
Read the map entry key from the current folder.
Definition: kwallet.cc:447
KWallet::Wallet::removeEntry
virtual int removeEntry(const QString &key)
Remove the entry key from the current folder.
Definition: kwallet.cc:612
KWallet::Wallet::readPasswordList
int readPasswordList(const QString &key, QMap< QString, QString > &value)
Read the password entry key from the current folder.
Definition: kwallet.cc:512
DCOPRef::obj
QCString obj() const
KWallet::Wallet::writePassword
virtual int writePassword(const QString &key, const QString &value)
Write key = value as a password to the current folder.
Definition: kwallet.cc:580
KWallet::Wallet::walletOpened
void walletOpened(bool success)
Emitted when a wallet is opened in asynchronous mode.
KWallet::Wallet::lockWallet
virtual int lockWallet()
This closes and locks the current wallet.
Definition: kwallet.cc:236
KWallet::Wallet::hasFolder
virtual bool hasFolder(const QString &f)
Determine if the folder f exists in the wallet.
Definition: kwallet.cc:315
KWallet::Wallet::folderUpdated
void folderUpdated(const QString &folder)
Emitted when a folder in this wallet is updated.
KWallet::Wallet::removeFolder
virtual bool removeFolder(const QString &f)
Remove the folder f and all its entries from the wallet.
Definition: kwallet.cc:372
DCOPClient::appId
QCString appId() const
KWallet::Wallet::readPassword
virtual int readPassword(const QString &key, QString &value)
Read the password entry key from the current folder.
Definition: kwallet.cc:495
KWallet::Wallet::entryList
virtual QStringList entryList()
Return the list of keys of all entries in this folder.
Definition: kwallet.cc:299
KWallet::Wallet::readEntryList
int readEntryList(const QString &key, QMap< QString, QByteArray > &value)
Read the entries matching key from the current folder.
Definition: kwallet.cc:414
KWallet::Wallet::FormDataFolder
static const QString FormDataFolder()
The standardized name of the form data folder.
Definition: kwallet.cc:69
KWallet::Wallet::walletList
static QStringList walletList()
List all the wallets available.
Definition: kwallet.cc:119
KWallet::Wallet::requestChangePassword
virtual void requestChangePassword(WId w=0)
Request to the wallet service to change the password of the current wallet.
Definition: kwallet.cc:264
KWallet::Wallet::walletClosed
void walletClosed()
Emitted when this wallet is closed.
KWallet::Wallet::readEntry
virtual int readEntry(const QString &key, QByteArray &value)
Read the entry key from the current folder.
Definition: kwallet.cc:397
DCOPRef::send
bool send(const QCString &fun)
DCOPClient::setNotifications
void setNotifications(bool enabled)
DCOPRef::app
QCString app() const
DCOPReply::get
bool get(T &t, const char *tname)
KWallet::Wallet::users
static QStringList users(const QString &wallet)
List the applications that are using the wallet wallet.
Definition: kwallet.cc:216
KWallet::Wallet::readMapList
int readMapList(const QString &key, QMap< QString, QMap< QString, QString > > &value)
Read the map entry key from the current folder.
Definition: kwallet.cc:469
KWallet::Wallet::createFolder
virtual bool createFolder(const QString &f)
Created the folder f.
Definition: kwallet.cc:331
KWallet::Wallet::sync
virtual int sync()
This syncs the wallet file on disk with what is in memory.
Definition: kwallet.cc:226
KWallet::Wallet::closeWallet
static int closeWallet(const QString &name, bool force)
Close the wallet name.
Definition: kwallet.cc:154
KWallet::Wallet::folderList
virtual QStringList folderList()
Obtain the list of all folders contained in the wallet.
Definition: kwallet.cc:283
KWallet::Wallet::openWallet
static Wallet * openWallet(const QString &name, WId w=0, OpenType ot=Synchronous)
Open the wallet name.
Definition: kwallet.cc:174
KWallet::Wallet::keyDoesNotExist
static bool keyDoesNotExist(const QString &wallet, const QString &folder, const QString &key)
Determine if an entry in a folder does not exist in a wallet.
Definition: kwallet.cc:699
KWallet::Wallet::deleteWallet
static int deleteWallet(const QString &name)
Delete the wallet name.
Definition: kwallet.cc:164
KWallet::Wallet::NetworkWallet
static const QString NetworkWallet()
The name of the wallet used to store network passwords.
Definition: kwallet.cc:54
KWallet::Wallet::changePassword
static void changePassword(const QString &name, WId w=0)
Request to the wallet service to change the password of the wallet name.
Definition: kwallet.cc:129
DCOPObject
DCOPObject::connectDCOPSignal
bool connectDCOPSignal(const QCString &sender, const QCString &senderObj, const QCString &signal, const QCString &slot, bool Volatile)
KWallet::Wallet::currentFolder
virtual const QString & currentFolder() const
Determine the current working folder in the wallet.
Definition: kwallet.cc:392
DCOPReply::isValid
bool isValid() const
KWallet::Wallet::disconnectApplication
static bool disconnectApplication(const QString &wallet, const QCString &app)
Disconnect the application app from wallet.
Definition: kwallet.cc:206
KWallet::Wallet::LocalWallet
static const QString LocalWallet()
The name of the wallet used to store local passwords.
Definition: kwallet.cc:36
KWallet::Wallet::writeEntry
virtual int writeEntry(const QString &key, const QByteArray &value, EntryType entryType)
Write key = value as a binary entry to the current folder.
Definition: kwallet.cc:529
KWallet::Wallet::isOpen
virtual bool isOpen() const
Determine if the current wallet is open, and is a valid wallet handle.
Definition: kwallet.cc:259
DCOPObject::objId
QCString objId() const
DCOPReply
KWallet::Wallet::folderDoesNotExist
static bool folderDoesNotExist(const QString &wallet, const QString &folder)
Determine if a folder does not exist in a wallet.
Definition: kwallet.cc:689
KWallet::Wallet::writeMap
virtual int writeMap(const QString &key, const QMap< QString, QString > &value)
Write key = value as a map to the current folder.
Definition: kwallet.cc:561

KDE Home | KDE Accessibility Home | Description of Access Keys