You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
240 lines
6.3 KiB
240 lines
6.3 KiB
15 years ago
|
/*=========================================================================
|
||
|
| KABCDAV
|
||
|
|--------------------------------------------------------------------------
|
||
|
| (c) 2010 Timothy Pearson
|
||
|
|
|
||
|
| This project is released under the GNU General Public License.
|
||
|
| Please see the file COPYING for more details.
|
||
|
|--------------------------------------------------------------------------
|
||
|
| CardDAV resource preferences class.
|
||
|
========================================================================*/
|
||
|
|
||
|
/*=========================================================================
|
||
|
| INCLUDES
|
||
|
========================================================================*/
|
||
|
|
||
|
#include "preferences.h"
|
||
|
|
||
12 years ago
|
#include <tdewallet.h>
|
||
15 years ago
|
#include <tqstring.h>
|
||
|
#include <tqurl.h>
|
||
15 years ago
|
#include <kdebug.h>
|
||
|
|
||
|
/*=========================================================================
|
||
|
| NAMESPACES
|
||
|
========================================================================*/
|
||
|
|
||
|
using namespace KABC;
|
||
|
using namespace KWallet;
|
||
|
|
||
|
/*=========================================================================
|
||
|
| CONSTANTS
|
||
|
========================================================================*/
|
||
|
|
||
15 years ago
|
const TQString CardDavPrefs::NO_PASSWORD = "";
|
||
|
const TQString CardDavPrefs::WALLET_FOLDER = "CardDAV resource";
|
||
|
const TQString CardDavPrefs::WALLET_PWD_SUFFIX = ":carddav_password";
|
||
15 years ago
|
|
||
|
/*=========================================================================
|
||
|
| METHODS
|
||
|
========================================================================*/
|
||
|
|
||
15 years ago
|
bool CardDavPrefs::setWalletFolder(const TQString& folder) {
|
||
15 years ago
|
bool ret = true;
|
||
|
|
||
|
if (!mNoWallet && NULL != mWallet) {
|
||
|
if (!mWallet->hasFolder(folder)) {
|
||
|
if (!mWallet->createFolder(folder)) {
|
||
|
ret = false;
|
||
|
kdWarning() << "can't create the wallet folder for CardDAV passwords";
|
||
|
}
|
||
|
}
|
||
|
if (!mWallet->setFolder(folder)) {
|
||
|
ret = false;
|
||
|
kdWarning() << "can't set the wallet folder for CardDAV passwords";
|
||
|
}
|
||
|
} else {
|
||
|
// the wallet is inaccessible or not configured
|
||
|
ret = false;
|
||
|
}
|
||
|
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
Wallet* CardDavPrefs::getWallet() {
|
||
|
Wallet* ret = NULL;
|
||
|
|
||
|
if (!mNoWallet) {
|
||
|
// the wallet is not marked as inaccessible
|
||
|
|
||
|
if (NULL == mWallet) {
|
||
13 years ago
|
kdDebug() << "creating wallet for " + mPrefix << '\n';
|
||
15 years ago
|
|
||
|
mWallet = Wallet::openWallet(Wallet::NetworkWallet(), 0);
|
||
|
if (NULL == mWallet) {
|
||
|
mNoWallet = true; // can't open the wallet, mark it inaccessible
|
||
|
kdWarning() << "can't create a wallet for CardDAV passwords";
|
||
|
} else {
|
||
|
if (setWalletFolder(WALLET_FOLDER)) {
|
||
|
// reserved
|
||
|
} else {
|
||
|
// can't set the wallet folder, remove the wallet and mark it inaccessible
|
||
|
kdWarning() << "can't set the walet folder for CardDAV passwords";
|
||
|
removeWallet(true);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
ret = mWallet;
|
||
|
}
|
||
|
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
void CardDavPrefs::removeWallet(bool noWallet) {
|
||
|
delete mWallet;
|
||
|
mWallet = NULL;
|
||
|
mNoWallet = noWallet;
|
||
|
}
|
||
|
|
||
15 years ago
|
void CardDavPrefs::addPrefix(const TQString& prefix) {
|
||
12 years ago
|
TDEConfigSkeletonItem::List itemList = items();
|
||
|
TDEConfigSkeletonItem::List::Iterator it;
|
||
15 years ago
|
|
||
|
for ( it = itemList.begin(); it != itemList.end(); ++it ) {
|
||
|
(*it)->setGroup( prefix + ':' + (*it)->group() );
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
bool CardDavPrefs::writePasswordToWallet(const TQString& password) {
|
||
15 years ago
|
|
||
|
Wallet* w = getWallet();
|
||
|
|
||
|
bool ret = false;
|
||
|
if (NULL != w) {
|
||
|
int rc = w->writePassword(mPrefix + WALLET_PWD_SUFFIX, password);
|
||
|
if (0 != rc) {
|
||
|
kdWarning() << "CardDAV: can't write password to the wallet";
|
||
|
} else {
|
||
|
ret = true;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
15 years ago
|
bool CardDavPrefs::readPasswordFromWallet(TQString& password) {
|
||
15 years ago
|
Wallet* w = getWallet();
|
||
|
|
||
|
bool ret = false;
|
||
|
if (NULL != w) {
|
||
15 years ago
|
TQString p;
|
||
15 years ago
|
int rc = w->readPassword(mPrefix + WALLET_PWD_SUFFIX, p);
|
||
|
if (0 == rc) {
|
||
|
//CardDavPrefsSkel::setPassword(p);
|
||
|
password = p;
|
||
|
ret = true;
|
||
|
} else {
|
||
|
kdWarning() << "CardDAV: can't read password from the wallet";
|
||
|
password = NO_PASSWORD;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
bool CardDavPrefs::removePasswordFromWallet() {
|
||
|
|
||
|
Wallet* w = getWallet();
|
||
|
|
||
|
bool ret = false;
|
||
|
if (NULL != w) {
|
||
|
int rc = w->removeEntry(mPrefix + WALLET_PWD_SUFFIX);
|
||
|
if (0 == rc) {
|
||
|
ret = true;
|
||
|
} else {
|
||
|
kdWarning() << "CardDAV: can't remove password from the wallet";
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
15 years ago
|
void CardDavPrefs::setPassword(const TQString& p) {
|
||
15 years ago
|
|
||
|
mPassword = p;
|
||
|
|
||
|
if (rememberPassword()) {
|
||
|
writePasswordToWallet(p);
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString CardDavPrefs::password() {
|
||
15 years ago
|
if (NO_PASSWORD == mPassword) {
|
||
|
readPasswordFromWallet(mPassword);
|
||
|
}
|
||
|
return mPassword;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString CardDavPrefs::getusername() {
|
||
15 years ago
|
return username();
|
||
|
}
|
||
|
|
||
|
void CardDavPrefs::setRememberPassword(bool v) {
|
||
13 years ago
|
kdDebug() << "remember: " << v << '\n';
|
||
15 years ago
|
|
||
|
CardDavPrefsSkel::setRememberPassword(v);
|
||
|
|
||
|
if (!v) {
|
||
|
// we should not remember password. If there is one already stored, it must be removed.
|
||
13 years ago
|
kdDebug() << "removing password from wallet" << '\n';
|
||
15 years ago
|
removePasswordFromWallet();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void CardDavPrefs::writeConfig() {
|
||
|
CardDavPrefsSkel::writeConfig();
|
||
|
}
|
||
|
|
||
|
void CardDavPrefs::readConfig() {
|
||
|
|
||
|
CardDavPrefsSkel::readConfig();
|
||
|
|
||
|
// the password is not in config file, try to restore it from the wallet.
|
||
|
/*if (rememberPassword()) {
|
||
|
readPasswordFromWallet();
|
||
|
}*/
|
||
|
}
|
||
|
|
||
15 years ago
|
bool CardDavPrefs::getUseURI() {
|
||
|
return useURI();
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString CardDavPrefs::getFullUrl() {
|
||
15 years ago
|
|
||
15 years ago
|
TQUrl t(url());
|
||
|
TQString safeURL;
|
||
15 years ago
|
int firstAt;
|
||
|
|
||
|
t.setUser(username());
|
||
|
t.setPassword(password());
|
||
|
|
||
|
safeURL = t.toString();
|
||
|
|
||
13 years ago
|
firstAt = safeURL.find("@") + 1;
|
||
|
while (safeURL.find("@", firstAt) != -1) {
|
||
|
safeURL.replace(safeURL.find("@", firstAt), 1, "%40");
|
||
15 years ago
|
}
|
||
|
|
||
|
// Unencode the username, as Zimbra stupidly rejects the %40
|
||
13 years ago
|
safeURL.replace("%40", "@");
|
||
15 years ago
|
|
||
|
// Encode any spaces, as libcarddav stupidly fails otherwise
|
||
13 years ago
|
safeURL.replace(" ", "%20");
|
||
15 years ago
|
|
||
|
return safeURL;
|
||
|
}
|
||
|
|
||
|
// EOF ========================================================================
|
||
|
|