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

calendarsupport

  • sources
  • kde-4.12
  • 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  private:
63  KCalPrefs *q;
64 };
65 
66 KCalPrefs::KCalPrefs() : KCalPrefsBase(), d( new Private( this ) )
67 {
68 }
69 
70 KCalPrefs::~KCalPrefs()
71 {
72  delete d;
73 }
74 
75 KCalPrefs *KCalPrefs::instance()
76 {
77  static bool firstCall = true;
78 
79  if ( firstCall ) {
80  firstCall = false;
81  globalPrefs->readConfig();
82  }
83 
84  return globalPrefs;
85 }
86 
87 void KCalPrefs::usrSetDefaults()
88 {
89  // Default should be set a bit smarter, respecting username and locale
90  // settings for example.
91 
92  KEMailSettings settings;
93  QString tmp = settings.getSetting( KEMailSettings::RealName );
94  if ( !tmp.isEmpty() ) {
95  setUserName( tmp );
96  }
97  tmp = settings.getSetting( KEMailSettings::EmailAddress );
98  if ( !tmp.isEmpty() ) {
99  setUserEmail( tmp );
100  }
101  fillMailDefaults();
102 
103  setTimeZoneDefault();
104 
105  KConfigSkeleton::usrSetDefaults();
106 }
107 
108 KDateTime::Spec KCalPrefs::timeSpec()
109 {
110  return KSystemTimeZones::local();
111 }
112 
113 void KCalPrefs::setTimeSpec( const KDateTime::Spec &spec )
114 {
115  d->mTimeSpec = spec;
116 }
117 
118 Akonadi::Entity::Id KCalPrefs::defaultCalendarId() const
119 {
120  return d->mDefaultCalendarId;
121 }
122 
123 void KCalPrefs::setDefaultCalendarId( const Akonadi::Entity::Id id )
124 {
125  d->mDefaultCalendarId = id;
126 }
127 
128 void KCalPrefs::setTimeZoneDefault()
129 {
130  KTimeZone zone = KSystemTimeZones::local();
131  if ( !zone.isValid() ) {
132  kError() << "KSystemTimeZones::local() return 0";
133  return;
134  }
135 
136  kDebug () << "----- time zone:" << zone.name();
137 
138  d->mTimeSpec = zone;
139 }
140 
141 void KCalPrefs::fillMailDefaults()
142 {
143  userEmailItem()->swapDefault();
144  QString defEmail = userEmailItem()->value();
145  userEmailItem()->swapDefault();
146 
147  if ( userEmail() == defEmail ) {
148  // No korg settings - but maybe there's a kcontrol[/kmail] setting available
149  KEMailSettings settings;
150  if ( !settings.getSetting( KEMailSettings::EmailAddress ).isEmpty() ) {
151  mEmailControlCenter = true;
152  }
153  }
154 }
155 
156 void KCalPrefs::usrReadConfig()
157 {
158  KConfigGroup generalConfig( config(), "General" );
159 
160  if ( !d->mTimeSpec.isValid() ) {
161  setTimeZoneDefault();
162  }
163 
164  KConfigGroup defaultCalendarConfig( config(), "Calendar" );
165  d->mDefaultCalendarId = defaultCalendarConfig.readEntry( "Default Calendar", -1 );
166 
167  // Category colors
168  d->mCategoryColors = d->mCategoryConfig->readColors();
169 #if 0
170  config()->setGroup( "FreeBusy" );
171  if ( mRememberRetrievePw ) {
172  d->mRetrievePassword =
173  KStringHandler::obscure( config()->readEntry( "Retrieve Server Password" ) );
174  }
175 #endif
176 
177  KConfigSkeleton::usrReadConfig();
178  fillMailDefaults();
179 }
180 
181 void KCalPrefs::usrWriteConfig()
182 {
183  KConfigGroup generalConfig( config(), "General" );
184  d->mCategoryConfig->setColors( d->mCategoryColors );
185 
186 #if 0
187  if ( mRememberRetrievePw ) {
188  config()->writeEntry( "Retrieve Server Password",
189  KStringHandler::obscure( d->mRetrievePassword ) );
190  } else {
191  config()->deleteEntry( "Retrieve Server Password" );
192  }
193 #endif
194 
195  KConfigGroup defaultCalendarConfig( config(), "Calendar" );
196  defaultCalendarConfig.writeEntry( "Default Calendar", defaultCalendarId() );
197 
198  KConfigSkeleton::usrWriteConfig();
199 }
200 
201 QString KCalPrefs::fullName()
202 {
203  QString tusername;
204  if ( mEmailControlCenter ) {
205  KEMailSettings settings;
206  tusername = settings.getSetting( KEMailSettings::RealName );
207  } else {
208  tusername = userName();
209  }
210 
211  // Quote the username as it might contain commas and other quotable chars.
212  tusername = KPIMUtils::quoteNameIfNecessary( tusername );
213 
214  QString tname, temail;
215  // ignore the return value from extractEmailAddressAndName() because
216  // it will always be false since tusername does not contain "@domain".
217  KPIMUtils::extractEmailAddressAndName( tusername, temail, tname );
218  return tname;
219 }
220 
221 QString KCalPrefs::email()
222 {
223  if ( mEmailControlCenter ) {
224  KEMailSettings settings;
225  return settings.getSetting( KEMailSettings::EmailAddress );
226  } else {
227  return userEmail();
228  }
229 }
230 
231 QStringList KCalPrefs::allEmails()
232 {
233  // Grab emails from the email identities
234  QStringList lst = CalendarSupport::identityManager()->allEmails();
235  // Add emails configured in korganizer
236  lst += mAdditionalMails;
237  // Add the email entered as the userEmail here
238  lst += email();
239 
240  // Warning, this list could contain duplicates.
241  return lst;
242 }
243 
244 QStringList KCalPrefs::fullEmails()
245 {
246  QStringList fullEmails;
247  // The user name and email from the config dialog:
248  fullEmails << QString::fromUtf8( "%1 <%2>" ).arg( fullName() ).arg( email() );
249 
250  QStringList::Iterator it;
251  // Grab emails from the email identities
252  KPIMIdentities::IdentityManager *idmanager = CalendarSupport::identityManager();
253  QStringList lst = idmanager->identities();
254  KPIMIdentities::IdentityManager::ConstIterator it1;
255  for ( it1 = idmanager->begin(); it1 != idmanager->end(); ++it1 ) {
256  fullEmails << (*it1).fullEmailAddr();
257  }
258  // Add emails configured in korganizer
259  lst = mAdditionalMails;
260  for ( it = lst.begin(); it != lst.end(); ++it ) {
261  fullEmails << QString::fromUtf8( "%1 <%2>" ).arg( fullName() ).arg( *it );
262  }
263 
264  // Warning, this list could contain duplicates.
265  return fullEmails;
266 }
267 
268 bool KCalPrefs::thatIsMe( const QString &_email )
269 {
270  // NOTE: this method is called for every created agenda view item,
271  // so we need to keep performance in mind
272 
273  /* identityManager()->thatIsMe() is quite expensive since it does parsing of
274  _email in a way which is unnecessarily complex for what we can have here,
275  so we do that ourselves. This makes sense since this
276 
277  if ( Akonadi::identityManager()->thatIsMe( _email ) ) {
278  return true;
279  }
280  */
281 
282  // in case email contains a full name, strip it out.
283  // the below is the simpler but slower version of the following code:
284  // const QString email = KPIM::getEmailAddress( _email );
285  const QByteArray tmp = _email.toUtf8();
286  const char *cursor = tmp.constData();
287  const char *end = tmp.data() + tmp.length();
288  KMime::Types::Mailbox mbox;
289  KMime::HeaderParsing::parseMailbox( cursor, end, mbox );
290  const QString email = mbox.addrSpec().asString();
291 
292  if ( this->email() == email ) {
293  return true;
294  }
295 
296  CalendarSupport::IdentityManager::ConstIterator it;
297  for ( it = CalendarSupport::identityManager()->begin();
298  it != CalendarSupport::identityManager()->end(); ++it ) {
299  if ( (*it).matchesEmailAddress( email ) ) {
300  return true;
301  }
302  }
303 
304  if ( mAdditionalMails.contains( email ) ) {
305  return true;
306  }
307 
308  return false;
309 }
310 
311 void KCalPrefs::setCategoryColor( const QString &cat, const QColor &color )
312 {
313  d->mCategoryColors.insert( cat, color );
314 }
315 
316 QColor KCalPrefs::categoryColor( const QString &cat ) const
317 {
318  QColor color;
319 
320  if ( !cat.isEmpty() ) {
321  color = d->mCategoryColors.value( cat );
322  }
323 
324  return color.isValid() ? color : d->mDefaultCategoryColor;
325 }
326 
327 bool KCalPrefs::hasCategoryColor( const QString &cat ) const
328 {
329  return d->mCategoryColors[ cat ].isValid();
330 }
CalendarSupport::KCalPrefs::setTimeSpec
void setTimeSpec(const KDateTime::Spec &spec)
Definition: kcalprefs.cpp:113
CalendarSupport::KCalPrefs::fullEmails
QStringList fullEmails()
Returns all email addresses together with the full username for the user.
Definition: kcalprefs.cpp:244
CalendarSupport::KCalPrefsBase::userEmail
QString userEmail() const
Get E&mail address.
Definition: kcalprefs_base.h:611
CalendarSupport::CategoryConfig
Definition: categoryconfig.h:33
CalendarSupport::KCalPrefs::usrWriteConfig
void usrWriteConfig()
Write preferences to config file.
Definition: kcalprefs.cpp:181
CalendarSupport::KCalPrefs::categoryColor
QColor categoryColor(const QString &cat) const
Definition: kcalprefs.cpp:316
CalendarSupport::KCalPrefsBase::setUserName
void setUserName(const QString &v)
Set Full &name.
Definition: kcalprefs_base.h:577
CalendarSupport::KCalPrefs::usrSetDefaults
void usrSetDefaults()
Set preferences to default values.
Definition: kcalprefs.cpp:87
CalendarSupport::KCalPrefs::allEmails
QStringList allEmails()
Returns all email addresses for the user.
Definition: kcalprefs.cpp:231
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:268
CalendarSupport::KCalPrefsBase
Definition: kcalprefs_base.h:15
CalendarSupport::KCalPrefs::defaultCalendarId
Akonadi::Entity::Id defaultCalendarId() const
Definition: kcalprefs.cpp:118
CalendarSupport::KCalPrefs::setCategoryColor
void setCategoryColor(const QString &cat, const QColor &color)
Definition: kcalprefs.cpp:311
CalendarSupport::KCalPrefs::setDefaultCalendarId
void setDefaultCalendarId(const Akonadi::Entity::Id)
Definition: kcalprefs.cpp:123
identitymanager.h
CalendarSupport::KCalPrefsBase::setUserEmail
void setUserEmail(const QString &v)
Set E&mail address.
Definition: kcalprefs_base.h:602
CalendarSupport::KCalPrefs::KCalPrefs
KCalPrefs()
Constructor disabled for public.
Definition: kcalprefs.cpp:66
CalendarSupport::KCalPrefs
Definition: kcalprefs.h:34
CalendarSupport::KCalPrefs::instance
static KCalPrefs * instance()
Get instance of KCalPrefs.
Definition: kcalprefs.cpp:75
CalendarSupport::KCalPrefsBase::userEmailItem
ItemString * userEmailItem()
Get Item object corresponding to UserEmail()
Definition: kcalprefs_base.h:619
CalendarSupport::KCalPrefs::email
QString email()
Definition: kcalprefs.cpp:221
CalendarSupport::KCalPrefsBase::mAdditionalMails
QStringList mAdditionalMails
Definition: kcalprefs_base.h:771
CalendarSupport::KCalPrefs::~KCalPrefs
virtual ~KCalPrefs()
Definition: kcalprefs.cpp:70
CalendarSupport::KCalPrefs::hasCategoryColor
bool hasCategoryColor(const QString &cat) const
Definition: kcalprefs.cpp:327
CalendarSupport::KCalPrefsBase::mEmailControlCenter
bool mEmailControlCenter
Definition: kcalprefs_base.h:753
CalendarSupport::KCalPrefs::setTimeZoneDefault
void setTimeZoneDefault()
Definition: kcalprefs.cpp:128
CalendarSupport::KCalPrefs::usrReadConfig
void usrReadConfig()
Read preferences from config file.
Definition: kcalprefs.cpp:156
CalendarSupport::KCalPrefs::fullName
QString fullName()
Definition: kcalprefs.cpp:201
CalendarSupport::KCalPrefsBase::userName
QString userName() const
Get Full &name.
Definition: kcalprefs_base.h:586
categoryconfig.h
CalendarSupport::KCalPrefs::timeSpec
KDateTime::Spec timeSpec()
Definition: kcalprefs.cpp:108
CalendarSupport::KCalPrefs::fillMailDefaults
void fillMailDefaults()
Fill empty mail fields with default values.
Definition: kcalprefs.cpp:141
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:54:59 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

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