summaryrefslogtreecommitdiffstats
path: root/kresources/caldav/preferences.cpp
blob: 10da60e0e81554de5a2fb75ee1ef1bc63f991cf5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*=========================================================================
| KCalDAV
|--------------------------------------------------------------------------
| (c) 2010  Timothy Pearson
| (c) 2009  Kumaran Santhanam (initial KDE4 version)
|
| This project is released under the GNU General Public License.
| Please see the file COPYING for more details.
|--------------------------------------------------------------------------
| CalDAV resource preferences class.
 ========================================================================*/

/*=========================================================================
| INCLUDES
 ========================================================================*/

#include "preferences.h"

#include <kwallet.h>
#include <tqstring.h>
#include <tqurl.h>
#include <kdebug.h>

/*=========================================================================
| NAMESPACES
 ========================================================================*/

using namespace KCal;
using namespace KWallet;

/*=========================================================================
| CONSTANTS
 ========================================================================*/

const TQString CalDavPrefs::NO_PASSWORD = "";
const TQString CalDavPrefs::WALLET_FOLDER = "CalDAV resource";
const TQString CalDavPrefs::WALLET_PWD_SUFFIX = ":caldav_password";

/*=========================================================================
| METHODS
 ========================================================================*/

bool CalDavPrefs::setWalletFolder(const TQString& folder) {
    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 CalDAV passwords";
            }
        }
        if (!mWallet->setFolder(folder)) {
            ret = false;
            kdWarning() << "can't set the wallet folder for CalDAV passwords";
        }
    } else {
        // the wallet is inaccessible or not configured
        ret = false;
    }

    return ret;
}

Wallet* CalDavPrefs::getWallet() {
    Wallet* ret = NULL;

    if (!mNoWallet) {
        // the wallet is not marked as inaccessible

        if (NULL == mWallet) {
            kdDebug() << "creating wallet for " + mPrefix << '\n';

            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 CalDAV 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 CalDAV passwords";
                    removeWallet(true);
                }
            }
        }

        ret = mWallet;
    }

    return ret;
}

void CalDavPrefs::removeWallet(bool noWallet) {
    delete mWallet;
    mWallet = NULL;
    mNoWallet = noWallet;
}

void CalDavPrefs::addPrefix(const TQString& prefix) {
    TDEConfigSkeletonItem::List itemList = items();
    TDEConfigSkeletonItem::List::Iterator it;

    for ( it = itemList.begin(); it != itemList.end(); ++it ) {
        (*it)->setGroup( prefix + ':' + (*it)->group() );
    }
}

bool CalDavPrefs::writePasswordToWallet(const TQString& password) {

    Wallet* w = getWallet();

    bool ret = false;
    if (NULL != w) {
        int rc = w->writePassword(mPrefix + WALLET_PWD_SUFFIX, password);
        if (0 != rc) {
            kdWarning() << "CalDAV: can't write password to the wallet";
        } else {
            ret = true;
        }
    }

    return ret;
}

bool CalDavPrefs::readPasswordFromWallet(TQString& password) {
    Wallet* w = getWallet();

    bool ret = false;
    if (NULL != w) {
        TQString p;
        int rc = w->readPassword(mPrefix + WALLET_PWD_SUFFIX, p);
        if (0 == rc) {
            //CalDavPrefsSkel::setPassword(p);
            password = p;
            ret = true;
        } else {
            kdWarning() << "CalDAV: can't read password from the wallet";
            password = NO_PASSWORD;
        }
    }

    return ret;
}

bool CalDavPrefs::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() << "CalDAV: can't remove password from the wallet";
        }
    }

    return ret;
}

void CalDavPrefs::setPassword(const TQString& p) {

    mPassword = p;

    if (rememberPassword()) {
        writePasswordToWallet(p);
    }
}

TQString CalDavPrefs::password() {
    if (NO_PASSWORD == mPassword) {
        readPasswordFromWallet(mPassword);
    }
    return mPassword;
}

TQString CalDavPrefs::getusername() {
    return username();
}

void CalDavPrefs::setRememberPassword(bool v) {
    kdDebug() << "remember: " << v << '\n';

    CalDavPrefsSkel::setRememberPassword(v);

    if (!v) {
        // we should not remember password. If there is one already stored, it must be removed.
        kdDebug() << "removing password from wallet" << '\n';
        removePasswordFromWallet();
    }
}

void CalDavPrefs::writeConfig() {
    CalDavPrefsSkel::writeConfig();
}

void CalDavPrefs::readConfig() {

    CalDavPrefsSkel::readConfig();

    // the password is not in config file, try to restore it from the wallet.
    /*if (rememberPassword()) {
        readPasswordFromWallet();
    }*/
}

TQString CalDavPrefs::getFullUrl() {

    TQUrl t(url());
    TQString safeURL;
    int firstAt;

    t.setUser(username());
    t.setPassword(password());

    safeURL = t.toString();

    firstAt = safeURL.find("@") + 1;
    while (safeURL.find("@", firstAt) != -1) {
        safeURL.replace(safeURL.find("@", firstAt), 1, "%40");
    }

    // Unencode the username, as Zimbra stupidly rejects the %40
    safeURL.replace("%40", "@");

    // Encode any spaces, as libcaldav stupidly fails otherwise
    safeURL.replace(" ", "%20");

    return safeURL;
}

TQString CalDavPrefs::getFullTasksUrl() {
    if (useSTasks() == 0)
      return getFullUrl();

    TQUrl t(tasksUrl());
    TQString safeURL;
    int firstAt;

    t.setUser(username());
    t.setPassword(password());

    safeURL = t.toString();

    firstAt = safeURL.find("@") + 1;
    while (safeURL.find("@", firstAt) != -1) {
        safeURL.replace(safeURL.find("@", firstAt), 1, "%40");
    }

    // Unencode the username, as Zimbra stupidly rejects the %40
    safeURL.replace("%40", "@");

    // Encode any spaces, as libcaldav stupidly fails otherwise
    safeURL.replace(" ", "%20");

    return safeURL;
}

TQString CalDavPrefs::getFullJournalsUrl() {
    if (useSJournals() == 0)
      return getFullUrl();

    TQUrl t(journalsUrl());
    TQString safeURL;
    int firstAt;

    t.setUser(username());
    t.setPassword(password());

    safeURL = t.toString();

    firstAt = safeURL.find("@") + 1;
    while (safeURL.find("@", firstAt) != -1) {
        safeURL.replace(safeURL.find("@", firstAt), 1, "%40");
    }

    // Unencode the username, as Zimbra stupidly rejects the %40
    safeURL.replace("%40", "@");

    // Encode any spaces, as libcaldav stupidly fails otherwise
    safeURL.replace(" ", "%20");

    return safeURL;
}

// EOF ========================================================================