summaryrefslogtreecommitdiffstats
path: root/kresources/caldav/preferences.h
blob: b8c3bb1919b818863da43c8c267641234209ae63 (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
/*=========================================================================
| 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
 ========================================================================*/

#ifndef KCAL_CALDAVPREFS_H
#define KCAL_CALDAVPREFS_H

#include "prefsskel.h"

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

class QString;

namespace KCal {

/*=========================================================================
| CLASS
 ========================================================================*/

/**
 * This class provides access to ResourceCalDav preferences.
 * It inherits auto-generated CalDavPrefsSkel class to add password-handling code.
 * KWallet is used for storing passwords.
 * It also adds code to allow multiple CalDAV resources to store settings in the same
 * config file.
 */
class CalDavPrefs : public CalDavPrefsSkel {

public:

    /**
     * @param prefix Unique prefix of the resource instance (use identifier() method).
     */
    CalDavPrefs(const QString& prefix)
        : mWallet(NULL)
        , mNoWallet(false)
        , mPrefix(prefix)
        , mPassword(NO_PASSWORD)
    {
        addPrefix(prefix);
    }

    virtual ~CalDavPrefs() {
        kdDebug() << "removing wallet";
        removeWallet();
    }

    virtual void writeConfig();
    virtual void readConfig();

    /**
     * Sets a new password. Also, if remember password flag is true,
     * remembers the password in the wallet. So, if you want the password
     * to be properly saved, call this method after ensuring the remember flag
     * is set.
     */
    void setPassword(const QString& p);

    /**
     * Returns password. The password is taken from the wallet.
     * May return an empty string, if there is no password available.
     */
    QString password();

    /**
     * Returns the username.
     */
    QString getusername();

    void setRememberPassword(bool v);

    /**
     * @return A full URL to connect to CalDAV server (including username and password).
     */
    QString getFullUrl();

protected:

    /**
     * Add an unique prefix to KConfigGroup, so that different instances of the resource
     * can use the same config file.
     * @param prefix Unique prefix of the resource instance.
     */
    void addPrefix(const QString& prefix);

    /**
     * Returns the wallet or NULL, if the wallet can't be obtained.
     */
    KWallet::Wallet* getWallet();

    /**
     * Tries to set a working folder for the wallet. If the wallet is not configured yet, does nothing.
     * @param folder the wallet working folder
     * @return true, if the folder has been set, and false otherwise.
     */
    bool setWalletFolder(const QString& folder);

    /**
     * Removes the wallet. If @p noWallet is set, the wallet has been marked inaccessible, so that subsequent
     * getWallet calls will not try to recreate it.
     */
    void removeWallet(bool noWallet = false);

    /**
     * Wrire password to the wallet.
     * @param password password to write
     * @return true on success, false on failure
     */
    bool writePasswordToWallet(const QString& password);

    /**
     * Extracts password from the wallet.
     * @param password a variable to save read password to.
     * @return true on success, false on failure
     */
    bool readPasswordFromWallet(QString& password);

    /**
     * Clears password in the wallet.
     * @return true on success, false on failure
     */
    bool removePasswordFromWallet();

private:

    static const QString NO_PASSWORD;
    static const QString WALLET_FOLDER;
    static const QString WALLET_PWD_SUFFIX;

    KWallet::Wallet* mWallet;
    bool mNoWallet;

    QString mPrefix;
    QString mPassword;
};

} // namespace KCal

#endif // KCAL_CALDAVPREFS_H