korganizer

koprefs.cpp
1 /*
2  This file is part of KOrganizer.
3 
4  Copyright (c) 2001,2003 Cornelius Schumacher <schumacher@kde.org>
5  Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 
21  As a special exception, permission is given to link this program
22  with any edition of TQt, and distribute the resulting executable,
23  without including the source code for TQt in the source distribution.
24 */
25 
26 #include <time.h>
27 #include <unistd.h>
28 
29 #include <tqdir.h>
30 #include <tqstring.h>
31 #include <tqfont.h>
32 #include <tqcolor.h>
33 #include <tqmap.h>
34 #include <tqstringlist.h>
35 
36 #include <tdeglobalsettings.h>
37 #include <tdeglobal.h>
38 #include <tdeconfig.h>
39 #include <tdelocale.h>
40 #include <kdebug.h>
41 #include <tdeemailsettings.h>
42 #include <kstaticdeleter.h>
43 #include <kstringhandler.h>
44 
45 #include <libkmime/kmime_header_parsing.h>
46 
47 #include "koprefs.h"
48 #include <libkpimidentities/identitymanager.h>
49 #include <libkpimidentities/identity.h>
50 #include <libemailfunctions/email.h>
51 #include <tdeabc/stdaddressbook.h>
52 #include <ktimezones.h>
53 #include "kocore.h"
54 
55 KOPrefs *KOPrefs::mInstance = 0;
56 static KStaticDeleter<KOPrefs> insd;
57 
58 TQColor getTextColor(const TQColor &c)
59 {
60  float luminance = (c.red() * 0.299) + (c.green() * 0.587) + (c.blue() * 0.114);
61  return (luminance > 128.0) ? TQColor( 0, 0 ,0 ) : TQColor( 255, 255 ,255 );
62 }
63 
64 
65 KOPrefs::KOPrefs() :
66  KOPrefsBase()
67 {
68  mCategoryColors.setAutoDelete( true );
69  mResourceColors.setAutoDelete( true );
70 
71  mDefaultCategoryColor = TQColor( 151, 235, 121 );
72 
73  mDefaultResourceColor = TQColor();//Default is a color invalid
74 
75  mDefaultTimeBarFont = TDEGlobalSettings::generalFont();
76  // make a large default time bar font, at least 16 points.
77  mDefaultTimeBarFont.setPointSize(
78  TQMAX( mDefaultTimeBarFont.pointSize() + 4, 16 ) );
79 
80  mDefaultMonthViewFont = TDEGlobalSettings::generalFont();
81  // make it a bit smaller
82  mDefaultMonthViewFont.setPointSize( mDefaultMonthViewFont.pointSize() - 2 );
83 
84  TDEConfigSkeleton::setCurrentGroup( "General" );
85 
86  addItemPath( "Html Export File", mHtmlExportFile,
87  TQDir::homeDirPath() + "/" + i18n( "Default export file", "calendar.html" ) );
88 
89  timeBarFontItem()->setDefaultValue( mDefaultTimeBarFont );
90  monthViewFontItem()->setDefaultValue( mDefaultMonthViewFont );
91 
92  // Load it now, not deep within some painting code
93  mMyAddrBookMails = TDEABC::StdAddressBook::self()->whoAmI().emails();
94 }
95 
96 
97 KOPrefs::~KOPrefs()
98 {
99  kdDebug(5850) << "KOPrefs::~KOPrefs()" << endl;
100 }
101 
102 
103 KOPrefs *KOPrefs::instance()
104 {
105  if ( !mInstance ) {
106  insd.setObject( mInstance, new KOPrefs() );
107  mInstance->readConfig();
108  }
109 
110  return mInstance;
111 }
112 
113 void KOPrefs::usrSetDefaults()
114 {
115  // Default should be set a bit smarter, respecting username and locale
116  // settings for example.
117 
118  KEMailSettings settings;
119  TQString tmp = settings.getSetting(KEMailSettings::RealName);
120  if ( !tmp.isEmpty() ) setUserName( tmp );
121  tmp = settings.getSetting(KEMailSettings::EmailAddress);
122  if ( !tmp.isEmpty() ) setUserEmail( tmp );
123  fillMailDefaults();
124 
125  mTimeBarFont = mDefaultTimeBarFont;
126  mMonthViewFont = mDefaultMonthViewFont;
127 
128  setTimeZoneIdDefault();
129 
130  KPimPrefs::usrSetDefaults();
131 }
132 
133 void KOPrefs::fillMailDefaults()
134 {
135  userEmailItem()->swapDefault();
136  TQString defEmail = userEmailItem()->value();
137  userEmailItem()->swapDefault();
138 
139  if ( userEmail() == defEmail ) {
140  // No korg settings - but maybe there's a kcontrol[/kmail] setting available
141  KEMailSettings settings;
142  if ( !settings.getSetting( KEMailSettings::EmailAddress ).isEmpty() )
143  mEmailControlCenter = true;
144  }
145 }
146 
147 void KOPrefs::setTimeZoneIdDefault()
148 {
149  TQString zone;
150 
151  zone = KTimezones().local()->name();
152 
153  kdDebug() << "----- time zone: " << zone << endl;
154 
155  mTimeZoneId = zone;
156 }
157 
158 void KOPrefs::setCategoryDefaults()
159 {
160  mCustomCategories.clear();
161 
162  mCustomCategories << i18n("Appointment") << i18n("Business")
163  << i18n("Meeting") << i18n("Phone Call") << i18n("Education")
164  << i18n("Holiday") << i18n("Vacation") << i18n("Special Occasion")
165  << i18n("Personal") << i18n("Travel") << i18n("Miscellaneous")
166  << i18n("Birthday");
167 }
168 
169 
170 void KOPrefs::usrReadConfig()
171 {
172  config()->setGroup("General");
173  mCustomCategories = config()->readListEntry("Custom Categories");
174  if (mCustomCategories.isEmpty()) setCategoryDefaults();
175 
176  // old category colors, ignore if they have the old default
177  // should be removed a few versions after 3.2...
178  config()->setGroup("Category Colors");
179  TQValueList<TQColor> oldCategoryColors;
180  TQStringList::Iterator it;
181  for (it = mCustomCategories.begin();it != mCustomCategories.end();++it ) {
182  TQColor c = config()->readColorEntry(*it, &mDefaultCategoryColor);
183  oldCategoryColors.append( (c == TQColor(196,196,196)) ?
184  mDefaultCategoryColor : c);
185  }
186 
187  // new category colors
188  config()->setGroup("Category Colors2");
189  TQValueList<TQColor>::Iterator it2;
190  for (it = mCustomCategories.begin(), it2 = oldCategoryColors.begin();
191  it != mCustomCategories.end(); ++it, ++it2 ) {
192  TQColor c = config()->readColorEntry(*it, &*it2);
193  if ( c != mDefaultCategoryColor )
194  setCategoryColor(*it,c);
195  }
196 
197  config()->setGroup( "Resources Colors" );
198  TQMap<TQString, TQString> map = config()->entryMap( "Resources Colors" );
199 
200  TQMapIterator<TQString, TQString> it3;
201  for( it3 = map.begin(); it3 != map.end(); ++it3 ) {
202  // kdDebug(5850)<< "KOPrefs::usrReadConfig: key: " << it3.key() << " value: "
203  // << it3.data()<<endl;
204  setResourceColor( it3.key(), config()->readColorEntry( it3.key(),
205  &mDefaultResourceColor ) );
206  }
207 
208 
209  if (mTimeZoneId.isEmpty()) {
210  setTimeZoneIdDefault();
211  }
212 
213 #if 0
214  config()->setGroup("FreeBusy");
215  if( mRememberRetrievePw )
216  mRetrievePassword = KStringHandler::obscure( config()->readEntry( "Retrieve Server Password" ) );
217 #endif
218  KPimPrefs::usrReadConfig();
219  fillMailDefaults();
220 }
221 
222 
223 void KOPrefs::usrWriteConfig()
224 {
225  config()->setGroup("General");
226  config()->writeEntry("Custom Categories",mCustomCategories);
227 
228  config()->setGroup("Category Colors2");
229  TQDictIterator<TQColor> it(mCategoryColors);
230  while (it.current()) {
231  config()->writeEntry(it.currentKey(),*(it.current()));
232  ++it;
233  }
234 
235  config()->setGroup( "Resources Colors" );
236  TQDictIterator<TQColor> it2( mResourceColors );
237  while( it2.current() ) {
238  config()->writeEntry( it2.currentKey(), *( it2.current() ) );
239  ++it2;
240  }
241 
242  if( !mFreeBusyPublishSavePassword ) {
243  TDEConfigSkeleton::ItemPassword *i = freeBusyPublishPasswordItem();
244  i->setValue( "" );
245  i->writeConfig( config() );
246  }
247  if( !mFreeBusyRetrieveSavePassword ) {
248  TDEConfigSkeleton::ItemPassword *i = freeBusyRetrievePasswordItem();
249  i->setValue( "" );
250  i->writeConfig( config() );
251  }
252 
253 #if 0
254  if( mRememberRetrievePw )
255  config()->writeEntry( "Retrieve Server Password", KStringHandler::obscure( mRetrievePassword ) );
256  else
257  config()->deleteEntry( "Retrieve Server Password" );
258 #endif
259 
260  KPimPrefs::usrWriteConfig();
261 }
262 
263 void KOPrefs::setCategoryColor( const TQString &cat, const TQColor & color)
264 {
265  mCategoryColors.replace( cat, new TQColor( color ) );
266 }
267 
268 TQColor *KOPrefs::categoryColor( const TQString &cat )
269 {
270  TQColor *color = 0;
271 
272  if ( !cat.isEmpty() ) color = mCategoryColors[ cat ];
273 
274  if ( color ) return color;
275  else return &mDefaultCategoryColor;
276 }
277 
278 
279 bool KOPrefs::hasCategoryColor( const TQString& cat ) const
280 {
281  return mCategoryColors[ cat ];
282 }
283 
284 void KOPrefs::setResourceColor ( const TQString &cal, const TQColor &color )
285 {
286  // kdDebug(5850)<<"KOPrefs::setResourceColor: " << cal << " color: "<<
287  // color.name()<<endl;
288  mResourceColors.replace( cal, new TQColor( color ) );
289 }
290 
291 TQColor* KOPrefs::resourceColor( const TQString &cal )
292 {
293  TQColor *color=0;
294  if( !cal.isEmpty() ) color = mResourceColors[cal];
295 
296  // assign default color if enabled
297  if ( !cal.isEmpty() && !color && assignDefaultResourceColors() ) {
298  TQColor defColor( 0x37, 0x7A, 0xBC );
299  if ( defaultResourceColorSeed() > 0 && defaultResourceColorSeed() - 1 < (int)defaultResourceColors().size() ) {
300  defColor = TQColor( defaultResourceColors()[defaultResourceColorSeed()-1] );
301  } else {
302  int h, s, v;
303  defColor.getHsv( &h, &s, &v );
304  h = ( defaultResourceColorSeed() % 12 ) * 30;
305  s -= s * ( (defaultResourceColorSeed() / 12) % 2 ) * 0.5;
306  defColor.setHsv( h, s, v );
307  }
308  setDefaultResourceColorSeed( defaultResourceColorSeed() + 1 );
309  setResourceColor( cal, defColor );
310  color = mResourceColors[cal];
311  }
312 
313  if (color && color->isValid() )
314  return color;
315  else
316  return &mDefaultResourceColor;
317 }
318 
319 TQString KOPrefs::fullName()
320 {
321  TQString tusername;
322  if ( mEmailControlCenter ) {
323  KEMailSettings settings;
324  tusername = settings.getSetting( KEMailSettings::RealName );
325  } else {
326  tusername = userName();
327  }
328 
329  // Quote the username as it might contain commas and other quotable chars.
330  tusername = KPIM::quoteNameIfNecessary( tusername );
331 
332  TQString tname, temail;
333  KPIM::getNameAndMail( tusername, tname, temail ); // ignore return value
334  // which is always false
335  return tname;
336 }
337 
338 TQString KOPrefs::email()
339 {
340  if ( mEmailControlCenter ) {
341  KEMailSettings settings;
342  return settings.getSetting( KEMailSettings::EmailAddress );
343  } else {
344  return userEmail();
345  }
346 }
347 
348 TQStringList KOPrefs::allEmails()
349 {
350  // Grab emails from the email identities
351  TQStringList lst = KOCore::self()->identityManager()->allEmails();
352  // Add emails configured in korganizer
353  lst += mAdditionalMails;
354  // Add emails from the user's kaddressbook entry
355  lst += mMyAddrBookMails;
356  // Add the email entered as the userEmail here
357  lst += email();
358 
359  // Warning, this list could contain duplicates.
360  return lst;
361 }
362 
363 TQStringList KOPrefs::fullEmails()
364 {
365  TQStringList fullEmails;
366  // The user name and email from the config dialog:
367  fullEmails << TQString("%1 <%2>").arg( fullName() ).arg( email() );
368 
369  TQStringList::Iterator it;
370  // Grab emails from the email identities
371  KPIM::IdentityManager *idmanager = KOCore::self()->identityManager();
372  TQStringList lst = idmanager->identities();
373  KPIM::IdentityManager::ConstIterator it1;
374  for ( it1 = idmanager->begin() ; it1 != idmanager->end() ; ++it1 ) {
375  fullEmails << (*it1).fullEmailAddr();
376  }
377  // Add emails configured in korganizer
378  lst = mAdditionalMails;
379  for ( it = lst.begin(); it != lst.end(); ++it ) {
380  fullEmails << TQString("%1 <%2>").arg( fullName() ).arg( *it );
381  }
382  // Add emails from the user's kaddressbook entry
383  TDEABC::Addressee me = TDEABC::StdAddressBook::self()->whoAmI();
384  lst = me.emails();
385  for ( it = lst.begin(); it != lst.end(); ++it ) {
386  fullEmails << me.fullEmail( *it );
387  }
388 
389  // Warning, this list could contain duplicates.
390  return fullEmails;
391 }
392 
393 bool KOPrefs::thatIsMe( const TQString& _email )
394 {
395  // NOTE: this method is called for every created agenda view item, so we need to keep
396  // performance in mind
397 
398  /* identityManager()->thatIsMe() is quite expensive since it does parsing of
399  _email in a way which is unnecessarily complex for what we can have here,
400  so we do that ourselves. This makes sense since this
401 
402  if ( KOCore::self()->identityManager()->thatIsMe( _email ) )
403  return true;
404  */
405 
406  // in case email contains a full name, strip it out
407  // the below is the simpler but slower version of the following KMime code
408  // const TQString email = KPIM::getEmailAddress( _email );
409  const TQCString tmp = _email.utf8();
410  const char *cursor = tmp.data();
411  const char *end = tmp.data() + tmp.length();
412  KMime::Types::Mailbox mbox;
413  KMime::HeaderParsing::parseMailbox( cursor, end, mbox );
414  const TQString email = mbox.addrSpec.asString();
415 
416  for ( KPIM::IdentityManager::ConstIterator it = KOCore::self()->identityManager()->begin();
417  it != KOCore::self()->identityManager()->end(); ++it ) {
418  if ( email == (*it).primaryEmailAddress() )
419  return true;
420  const TQStringList & aliases = (*it).emailAliases();
421  if ( aliases.find( email ) != aliases.end() )
422  return true;
423  }
424 
425  if ( mAdditionalMails.find( email ) != mAdditionalMails.end() )
426  return true;
427  TQStringList lst = mMyAddrBookMails;
428  if ( lst.find( email ) != lst.end() )
429  return true;
430  return false;
431 }