CalendarSupport

kcalprefs.cpp
1/*
2 SPDX-FileCopyrightText: 2001, 2003 Cornelius Schumacher <schumacher@kde.org>
3 SPDX-FileCopyrightText: 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
4
5 SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
6*/
7
8#include "kcalprefs.h"
9#include "calendarsupport_debug.h"
10#include "identitymanager.h"
11
12#include <Akonadi/TagAttribute>
13#include <Akonadi/TagCache>
14#include <Akonadi/TagModifyJob>
15
16#include <KMime/HeaderParsing>
17
18#include <KEmailAddress>
19#include <KIdentityManagementCore/Identity>
20
21#include <KEMailSettings>
22
23using namespace CalendarSupport;
24
25Q_GLOBAL_STATIC(KCalPrefs, globalPrefs)
26
27class CalendarSupport::KCalPrefsPrivate
28{
29public:
30 KCalPrefsPrivate()
31 : mDefaultCalendarId(-1)
32 , mDefaultCategoryColor(QColor(151, 235, 121))
33 {
34 }
35
36 ~KCalPrefsPrivate() = default;
37
38 Akonadi::Collection::Id mDefaultCalendarId;
39
40 const QColor mDefaultCategoryColor;
41 QDateTime mDayBegins;
42};
43
44KCalPrefs::KCalPrefs()
45 : KCalPrefsBase()
46 , d(new KCalPrefsPrivate())
47{
49}
50
51KCalPrefs::~KCalPrefs() = default;
52
53KCalPrefs *KCalPrefs::instance()
54{
55 static bool firstCall = true;
56
57 if (firstCall) {
58 firstCall = false;
59 globalPrefs->load();
60 }
61
62 return globalPrefs;
63}
64
65void KCalPrefs::usrSetDefaults()
66{
67 // Default should be set a bit smarter, respecting username and locale
68 // settings for example.
69
70 KEMailSettings settings;
71 QString tmp = settings.getSetting(KEMailSettings::RealName);
72 if (!tmp.isEmpty()) {
73 setUserName(tmp);
74 }
75 tmp = settings.getSetting(KEMailSettings::EmailAddress);
76 if (!tmp.isEmpty()) {
77 setUserEmail(tmp);
78 }
79 fillMailDefaults();
80
82}
83
84Akonadi::Collection::Id KCalPrefs::defaultCalendarId() const
85{
86 return d->mDefaultCalendarId;
87}
88
89void KCalPrefs::setDefaultCalendarId(Akonadi::Collection::Id id)
90{
91 d->mDefaultCalendarId = id;
92}
93
94void KCalPrefs::fillMailDefaults()
95{
96 userEmailItem()->swapDefault();
97 QString defEmail = userEmailItem()->value();
98 userEmailItem()->swapDefault();
99
100 if (userEmail() == defEmail) {
101 // No korg settings - but maybe there's a kcontrol[/kmail] setting available
102 KEMailSettings settings;
103 if (!settings.getSetting(KEMailSettings::EmailAddress).isEmpty()) {
104 mEmailControlCenter = true;
105 }
106 }
107}
108
109void KCalPrefs::usrRead()
110{
111 KConfigGroup generalConfig(config(), QStringLiteral("General"));
112
113 KConfigGroup defaultCalendarConfig(config(), QStringLiteral("Calendar"));
114 d->mDefaultCalendarId = defaultCalendarConfig.readEntry("Default Calendar", -1);
115
116#if 0
117 config()->setGroup("FreeBusy");
118 if (mRememberRetrievePw) {
119 d->mRetrievePassword
120 = KStringHandler::obscure(config()->readEntry("Retrieve Server Password"));
121 }
122#endif
123
125 fillMailDefaults();
126}
127
128bool KCalPrefs::usrSave()
129{
130 KConfigGroup generalConfig(config(), QStringLiteral("General"));
131
132#if 0
133 if (mRememberRetrievePw) {
134 config()->writeEntry("Retrieve Server Password",
135 KStringHandler::obscure(d->mRetrievePassword));
136 } else {
137 config()->deleteEntry("Retrieve Server Password");
138 }
139#endif
140
141 KConfigGroup defaultCalendarConfig(config(), QStringLiteral("Calendar"));
142 defaultCalendarConfig.writeEntry("Default Calendar", defaultCalendarId());
143
145}
146
147QString KCalPrefs::fullName()
148{
149 QString tusername;
150 if (mEmailControlCenter) {
151 KEMailSettings settings;
152 tusername = settings.getSetting(KEMailSettings::RealName);
153 } else {
154 tusername = userName();
155 }
156
157 // Quote the username as it might contain commas and other quotable chars.
158 tusername = KEmailAddress::quoteNameIfNecessary(tusername);
159
160 QString tname;
161 QString temail;
162 // ignore the return value from extractEmailAddressAndName() because
163 // it will always be false since tusername does not contain "@domain".
164 KEmailAddress::extractEmailAddressAndName(tusername, temail, tname);
165 return tname;
166}
167
168QString KCalPrefs::email()
169{
170 if (mEmailControlCenter) {
171 KEMailSettings settings;
172 return settings.getSetting(KEMailSettings::EmailAddress);
173 } else {
174 return userEmail();
175 }
176}
177
178QStringList KCalPrefs::allEmails()
179{
180 // Grab emails from the email identities
181 QStringList lst = CalendarSupport::identityManager()->allEmails();
182 // Add emails configured in korganizer
183 lst += mAdditionalMails;
184 // Add the email entered as the userEmail here
185 lst += email();
186
187 // Warning, this list could contain duplicates.
188 return lst;
189}
190
191QStringList KCalPrefs::fullEmails()
192{
193 QStringList fullEmails;
194
195 // Grab emails from the email identities
196 KIdentityManagementCore::IdentityManager *idmanager = CalendarSupport::identityManager();
197 QStringList lst = idmanager->identities();
198
199 fullEmails.reserve(1 + mAdditionalMails.count() + lst.count());
200 // The user name and email from the config dialog:
201 fullEmails << QStringLiteral("%1 <%2>").arg(fullName(), email());
202
204 KIdentityManagementCore::IdentityManager::ConstIterator it1;
205 KIdentityManagementCore::IdentityManager::ConstIterator end1(idmanager->end());
206 for (it1 = idmanager->begin(); it1 != end1; ++it1) {
207 fullEmails << (*it1).fullEmailAddr();
208 }
209 // Add emails configured in korganizer
210 lst = mAdditionalMails;
211 for (it = lst.begin(); it != lst.end(); ++it) {
212 fullEmails << QStringLiteral("%1 <%2>").arg(fullName(), *it);
213 }
214
215 // Warning, this list could contain duplicates.
216 return fullEmails;
217}
218
219bool KCalPrefs::thatIsMe(const QString &_email)
220{
221 // NOTE: this method is called for every created agenda view item,
222 // so we need to keep performance in mind
223
224 /* identityManager()->thatIsMe() is quite expensive since it does parsing of
225 _email in a way which is unnecessarily complex for what we can have here,
226 so we do that ourselves. This makes sense since this
227
228 if ( Akonadi::identityManager()->thatIsMe( _email ) ) {
229 return true;
230 }
231 */
232
233 // in case email contains a full name, strip it out.
234 // the below is the simpler but slower version of the following code:
235 // const QString email = CalendarSupport::getEmailAddress( _email );
236 const QByteArray tmp = _email.toUtf8();
237 const char *cursor = tmp.constData();
238 const char *end = tmp.data() + tmp.length();
240 KMime::HeaderParsing::parseMailbox(cursor, end, mbox);
241 const QString email = mbox.addrSpec().asString();
242
243 if (this->email() == email) {
244 return true;
245 }
246
247 CalendarSupport::IdentityManager::ConstIterator it;
248 CalendarSupport::IdentityManager::ConstIterator endId(CalendarSupport::identityManager()->end());
249 for (it = CalendarSupport::identityManager()->begin(); it != endId; ++it) {
250 if ((*it).matchesEmailAddress(email)) {
251 return true;
252 }
253 }
254
255 if (mAdditionalMails.contains(email)) {
256 return true;
257 }
258
259 return false;
260}
261
262void KCalPrefs::setDayBegins(const QDateTime &dateTime)
263{
264 d->mDayBegins = dateTime;
265}
266
267QDateTime KCalPrefs::dayBegins() const
268{
269 return d->mDayBegins;
270}
271
272#include "moc_kcalprefs.cpp"
static TagCache * instance()
virtual void usrSetDefaults()
virtual bool usrSave()
virtual void usrRead()
QString getSetting(KEMailSettings::Setting s) const
KCODECS_EXPORT QString quoteNameIfNecessary(const QString &str)
KCODECS_EXPORT bool extractEmailAddressAndName(const QString &aStr, QString &mail, QString &name)
const QList< QKeySequence > & begin()
const QList< QKeySequence > & end()
KCOREADDONS_EXPORT QString obscure(const QString &str)
const char * constData() const const
char * data()
qsizetype length() const const
iterator begin()
qsizetype count() const const
iterator end()
void reserve(qsizetype size)
bool isEmpty() const const
QByteArray toUtf8() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:05:28 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.