• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

calendarsupport

  • sources
  • kde-4.14
  • kdepim
  • calendarsupport
kcalprefs.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2001,2003 Cornelius Schumacher <schumacher@kde.org>
3  Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 
19  As a special exception, permission is given to link this program
20  with any edition of Qt, and distribute the resulting executable,
21  without including the source code for Qt in the source distribution.
22 */
23 
24 #include "kcalprefs.h"
25 #include "identitymanager.h"
26 #include "categoryconfig.h"
27 
28 #include <KMime/HeaderParsing>
29 
30 #include <KPIMIdentities/Identity>
31 #include <KPIMIdentities/IdentityManager>
32 
33 #include <KPIMUtils/Email>
34 
35 #include <KEMailSettings>
36 #include <KSystemTimeZone>
37 
38 using namespace CalendarSupport;
39 
40 K_GLOBAL_STATIC( KCalPrefs, globalPrefs )
41 
42 class KCalPrefs::Private
43 {
44  public:
45  Private( KCalPrefs *qq ) : mDefaultCalendarId( -1 ), q( qq )
46  {
47  mDefaultCategoryColor = QColor( 151, 235, 121 );
48  mCategoryConfig = new CategoryConfig( q );
49  }
50 
51  ~Private()
52  {
53  delete mCategoryConfig;
54  }
55 
56  KDateTime::Spec mTimeSpec;
57  Akonadi::Entity::Id mDefaultCalendarId;
58 
59  CategoryConfig *mCategoryConfig;
60  QHash<QString,QColor> mCategoryColors;
61  QColor mDefaultCategoryColor;
62  QDateTime mDayBegins;
63 
64  private:
65  KCalPrefs *q;
66 };
67 
68 KCalPrefs::KCalPrefs() : KCalPrefsBase(), d( new Private( this ) )
69 {
70 }
71 
72 KCalPrefs::~KCalPrefs()
73 {
74  delete d;
75 }
76 
77 KCalPrefs *KCalPrefs::instance()
78 {
79  static bool firstCall = true;
80 
81  if ( firstCall ) {
82  firstCall = false;
83  globalPrefs->readConfig();
84  }
85 
86  return globalPrefs;
87 }
88 
89 void KCalPrefs::usrSetDefaults()
90 {
91  // Default should be set a bit smarter, respecting username and locale
92  // settings for example.
93 
94  KEMailSettings settings;
95  QString tmp = settings.getSetting( KEMailSettings::RealName );
96  if ( !tmp.isEmpty() ) {
97  setUserName( tmp );
98  }
99  tmp = settings.getSetting( KEMailSettings::EmailAddress );
100  if ( !tmp.isEmpty() ) {
101  setUserEmail( tmp );
102  }
103  fillMailDefaults();
104 
105  setTimeZoneDefault();
106 
107  KConfigSkeleton::usrSetDefaults();
108 }
109 
110 KDateTime::Spec KCalPrefs::timeSpec()
111 {
112  return KSystemTimeZones::local();
113 }
114 
115 void KCalPrefs::setTimeSpec( const KDateTime::Spec &spec )
116 {
117  d->mTimeSpec = spec;
118 }
119 
120 Akonadi::Entity::Id KCalPrefs::defaultCalendarId() const
121 {
122  return d->mDefaultCalendarId;
123 }
124 
125 void KCalPrefs::setDefaultCalendarId( const Akonadi::Entity::Id id )
126 {
127  d->mDefaultCalendarId = id;
128 }
129 
130 void KCalPrefs::setTimeZoneDefault()
131 {
132  KTimeZone zone = KSystemTimeZones::local();
133  if ( !zone.isValid() ) {
134  kError() << "KSystemTimeZones::local() return 0";
135  return;
136  }
137 
138  kDebug () << "----- time zone:" << zone.name();
139 
140  d->mTimeSpec = zone;
141 }
142 
143 void KCalPrefs::fillMailDefaults()
144 {
145  userEmailItem()->swapDefault();
146  QString defEmail = userEmailItem()->value();
147  userEmailItem()->swapDefault();
148 
149  if ( userEmail() == defEmail ) {
150  // No korg settings - but maybe there's a kcontrol[/kmail] setting available
151  KEMailSettings settings;
152  if ( !settings.getSetting( KEMailSettings::EmailAddress ).isEmpty() ) {
153  mEmailControlCenter = true;
154  }
155  }
156 }
157 
158 void KCalPrefs::usrReadConfig()
159 {
160  KConfigGroup generalConfig( config(), "General" );
161 
162  if ( !d->mTimeSpec.isValid() ) {
163  setTimeZoneDefault();
164  }
165 
166  KConfigGroup defaultCalendarConfig( config(), "Calendar" );
167  d->mDefaultCalendarId = defaultCalendarConfig.readEntry( "Default Calendar", -1 );
168 
169  // Category colors
170  d->mCategoryColors = d->mCategoryConfig->readColors();
171 #if 0
172  config()->setGroup( "FreeBusy" );
173  if ( mRememberRetrievePw ) {
174  d->mRetrievePassword =
175  KStringHandler::obscure( config()->readEntry( "Retrieve Server Password" ) );
176  }
177 #endif
178 
179  KConfigSkeleton::usrReadConfig();
180  fillMailDefaults();
181 }
182 
183 void KCalPrefs::usrWriteConfig()
184 {
185  KConfigGroup generalConfig( config(), "General" );
186  d->mCategoryConfig->setColors( d->mCategoryColors );
187 
188 #if 0
189  if ( mRememberRetrievePw ) {
190  config()->writeEntry( "Retrieve Server Password",
191  KStringHandler::obscure( d->mRetrievePassword ) );
192  } else {
193  config()->deleteEntry( "Retrieve Server Password" );
194  }
195 #endif
196 
197  KConfigGroup defaultCalendarConfig( config(), "Calendar" );
198  defaultCalendarConfig.writeEntry( "Default Calendar", defaultCalendarId() );
199 
200  KConfigSkeleton::usrWriteConfig();
201 }
202 
203 QString KCalPrefs::fullName()
204 {
205  QString tusername;
206  if ( mEmailControlCenter ) {
207  KEMailSettings settings;
208  tusername = settings.getSetting( KEMailSettings::RealName );
209  } else {
210  tusername = userName();
211  }
212 
213  // Quote the username as it might contain commas and other quotable chars.
214  tusername = KPIMUtils::quoteNameIfNecessary( tusername );
215 
216  QString tname, temail;
217  // ignore the return value from extractEmailAddressAndName() because
218  // it will always be false since tusername does not contain "@domain".
219  KPIMUtils::extractEmailAddressAndName( tusername, temail, tname );
220  return tname;
221 }
222 
223 QString KCalPrefs::email()
224 {
225  if ( mEmailControlCenter ) {
226  KEMailSettings settings;
227  return settings.getSetting( KEMailSettings::EmailAddress );
228  } else {
229  return userEmail();
230  }
231 }
232 
233 QStringList KCalPrefs::allEmails()
234 {
235  // Grab emails from the email identities
236  QStringList lst = CalendarSupport::identityManager()->allEmails();
237  // Add emails configured in korganizer
238  lst += mAdditionalMails;
239  // Add the email entered as the userEmail here
240  lst += email();
241 
242  // Warning, this list could contain duplicates.
243  return lst;
244 }
245 
246 QStringList KCalPrefs::fullEmails()
247 {
248  QStringList fullEmails;
249  // The user name and email from the config dialog:
250  fullEmails << QString::fromUtf8( "%1 <%2>" ).arg( fullName() ).arg( email() );
251 
252  QStringList::Iterator it;
253  // Grab emails from the email identities
254  KPIMIdentities::IdentityManager *idmanager = CalendarSupport::identityManager();
255  QStringList lst = idmanager->identities();
256  KPIMIdentities::IdentityManager::ConstIterator it1;
257  for ( it1 = idmanager->begin(); it1 != idmanager->end(); ++it1 ) {
258  fullEmails << (*it1).fullEmailAddr();
259  }
260  // Add emails configured in korganizer
261  lst = mAdditionalMails;
262  for ( it = lst.begin(); it != lst.end(); ++it ) {
263  fullEmails << QString::fromUtf8( "%1 <%2>" ).arg( fullName() ).arg( *it );
264  }
265 
266  // Warning, this list could contain duplicates.
267  return fullEmails;
268 }
269 
270 bool KCalPrefs::thatIsMe( const QString &_email )
271 {
272  // NOTE: this method is called for every created agenda view item,
273  // so we need to keep performance in mind
274 
275  /* identityManager()->thatIsMe() is quite expensive since it does parsing of
276  _email in a way which is unnecessarily complex for what we can have here,
277  so we do that ourselves. This makes sense since this
278 
279  if ( Akonadi::identityManager()->thatIsMe( _email ) ) {
280  return true;
281  }
282  */
283 
284  // in case email contains a full name, strip it out.
285  // the below is the simpler but slower version of the following code:
286  // const QString email = KPIM::getEmailAddress( _email );
287  const QByteArray tmp = _email.toUtf8();
288  const char *cursor = tmp.constData();
289  const char *end = tmp.data() + tmp.length();
290  KMime::Types::Mailbox mbox;
291  KMime::HeaderParsing::parseMailbox( cursor, end, mbox );
292  const QString email = mbox.addrSpec().asString();
293 
294  if ( this->email() == email ) {
295  return true;
296  }
297 
298  CalendarSupport::IdentityManager::ConstIterator it;
299  for ( it = CalendarSupport::identityManager()->begin();
300  it != CalendarSupport::identityManager()->end(); ++it ) {
301  if ( (*it).matchesEmailAddress( email ) ) {
302  return true;
303  }
304  }
305 
306  if ( mAdditionalMails.contains( email ) ) {
307  return true;
308  }
309 
310  return false;
311 }
312 
313 void KCalPrefs::setCategoryColor( const QString &cat, const QColor &color )
314 {
315  d->mCategoryColors.insert( cat, color );
316 }
317 
318 QColor KCalPrefs::categoryColor( const QString &cat ) const
319 {
320  QColor color;
321 
322  if ( !cat.isEmpty() ) {
323  color = d->mCategoryColors.value( cat );
324  }
325 
326  return color.isValid() ? color : d->mDefaultCategoryColor;
327 }
328 
329 bool KCalPrefs::hasCategoryColor( const QString &cat ) const
330 {
331  return d->mCategoryColors[ cat ].isValid();
332 }
333 
334 void KCalPrefs::setDayBegins( const QDateTime &dateTime )
335 {
336  d->mDayBegins = dateTime;
337 }
338 
339 QDateTime KCalPrefs::dayBegins() const
340 {
341  return d->mDayBegins;
342 }
CalendarSupport::KCalPrefs::setTimeSpec
void setTimeSpec(const KDateTime::Spec &spec)
Definition: kcalprefs.cpp:115
CalendarSupport::KCalPrefs::fullEmails
QStringList fullEmails()
Returns all email addresses together with the full username for the user.
Definition: kcalprefs.cpp:246
CalendarSupport::CategoryConfig
Definition: categoryconfig.h:33
CalendarSupport::KCalPrefs::usrWriteConfig
void usrWriteConfig()
Write preferences to config file.
Definition: kcalprefs.cpp:183
QByteArray
CalendarSupport::KCalPrefs::categoryColor
QColor categoryColor(const QString &cat) const
Definition: kcalprefs.cpp:318
CalendarSupport::KCalPrefs::usrSetDefaults
void usrSetDefaults()
Set preferences to default values.
Definition: kcalprefs.cpp:89
QByteArray::length
int length() const
CalendarSupport::KCalPrefs::allEmails
QStringList allEmails()
Returns all email addresses for the user.
Definition: kcalprefs.cpp:233
kcalprefs.h
CalendarSupport::identityManager
KPIMIdentities::IdentityManager * identityManager()
Definition: identitymanager.cpp:39
CalendarSupport::KCalPrefs::thatIsMe
bool thatIsMe(const QString &email)
Return true if the given email belongs to the user.
Definition: kcalprefs.cpp:270
QString::fromUtf8
QString fromUtf8(const char *str, int size)
CalendarSupport::KCalPrefs::defaultCalendarId
Akonadi::Entity::Id defaultCalendarId() const
Definition: kcalprefs.cpp:120
QHash< QString, QColor >
KCalPrefsBase
CalendarSupport::KCalPrefs::setCategoryColor
void setCategoryColor(const QString &cat, const QColor &color)
Definition: kcalprefs.cpp:313
QString::isEmpty
bool isEmpty() const
CalendarSupport::KCalPrefs::setDefaultCalendarId
void setDefaultCalendarId(const Akonadi::Entity::Id)
Definition: kcalprefs.cpp:125
QByteArray::constData
const char * constData() const
identitymanager.h
QList::Iterator
typedef Iterator
CalendarSupport::KCalPrefs::KCalPrefs
KCalPrefs()
Constructor disabled for public.
Definition: kcalprefs.cpp:68
QString
CalendarSupport::KCalPrefs
Definition: kcalprefs.h:34
QColor
CalendarSupport::KCalPrefs::instance
static KCalPrefs * instance()
Get instance of KCalPrefs.
Definition: kcalprefs.cpp:77
QStringList
CalendarSupport::KCalPrefs::email
QString email()
Definition: kcalprefs.cpp:223
QList::end
iterator end()
CalendarSupport::KCalPrefs::~KCalPrefs
virtual ~KCalPrefs()
Definition: kcalprefs.cpp:72
CalendarSupport::KCalPrefs::hasCategoryColor
bool hasCategoryColor(const QString &cat) const
Definition: kcalprefs.cpp:329
CalendarSupport::KCalPrefs::dayBegins
QDateTime dayBegins() const
Definition: kcalprefs.cpp:339
CalendarSupport::KCalPrefs::setDayBegins
void setDayBegins(const QDateTime &dateTime)
Definition: kcalprefs.cpp:334
CalendarSupport::KCalPrefs::setTimeZoneDefault
void setTimeZoneDefault()
Definition: kcalprefs.cpp:130
CalendarSupport::KCalPrefs::usrReadConfig
void usrReadConfig()
Read preferences from config file.
Definition: kcalprefs.cpp:158
QByteArray::data
char * data()
CalendarSupport::KCalPrefs::fullName
QString fullName()
Definition: kcalprefs.cpp:203
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
QList::begin
iterator begin()
categoryconfig.h
QDateTime
CalendarSupport::KCalPrefs::timeSpec
KDateTime::Spec timeSpec()
Definition: kcalprefs.cpp:110
CalendarSupport::KCalPrefs::fillMailDefaults
void fillMailDefaults()
Fill empty mail fields with default values.
Definition: kcalprefs.cpp:143
QColor::isValid
bool isValid() const
QString::toUtf8
QByteArray toUtf8() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:31:15 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

calendarsupport

Skip menu "calendarsupport"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal