korganizer

koprefs.cpp

00001 /*
00002     This file is part of KOrganizer.
00003 
00004     Copyright (c) 2001,2003 Cornelius Schumacher <schumacher@kde.org>
00005     Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
00006 
00007     This program is free software; you can redistribute it and/or modify
00008     it under the terms of the GNU General Public License as published by
00009     the Free Software Foundation; either version 2 of the License, or
00010     (at your option) any later version.
00011 
00012     This program is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00015     GNU General Public License for more details.
00016 
00017     You should have received a copy of the GNU General Public License
00018     along with this program; if not, write to the Free Software
00019     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020 
00021     As a special exception, permission is given to link this program
00022     with any edition of Qt, and distribute the resulting executable,
00023     without including the source code for Qt in the source distribution.
00024 */
00025 
00026 #include <time.h>
00027 #include <unistd.h>
00028 
00029 #include <qdir.h>
00030 #include <qstring.h>
00031 #include <qfont.h>
00032 #include <qcolor.h>
00033 #include <qmap.h>
00034 #include <qstringlist.h>
00035 
00036 #include <kglobalsettings.h>
00037 #include <kglobal.h>
00038 #include <kconfig.h>
00039 #include <klocale.h>
00040 #include <kdebug.h>
00041 #include <kemailsettings.h>
00042 #include <kstaticdeleter.h>
00043 #include <kstringhandler.h>
00044 
00045 #include <libkmime/kmime_header_parsing.h>
00046 
00047 #include "koprefs.h"
00048 #include <libkpimidentities/identitymanager.h>
00049 #include <libkpimidentities/identity.h>
00050 #include <libemailfunctions/email.h>
00051 #include <kabc/stdaddressbook.h>
00052 #include <ktimezones.h>
00053 #include "kocore.h"
00054 
00055 KOPrefs *KOPrefs::mInstance = 0;
00056 static KStaticDeleter<KOPrefs> insd;
00057 
00058 QColor getTextColor(const QColor &c)
00059 {
00060   float luminance = (c.red() * 0.299) + (c.green() * 0.587) + (c.blue() * 0.114);
00061   return (luminance > 128.0) ? QColor( 0, 0 ,0 ) : QColor( 255, 255 ,255 );
00062 }
00063 
00064 
00065 KOPrefs::KOPrefs() :
00066   KOPrefsBase()
00067 {
00068   mCategoryColors.setAutoDelete( true );
00069   mResourceColors.setAutoDelete( true );
00070 
00071   mDefaultCategoryColor = QColor( 151, 235, 121 );
00072 
00073   mDefaultResourceColor = QColor();//Default is a color invalid
00074 
00075   mDefaultTimeBarFont = KGlobalSettings::generalFont();
00076   // make a large default time bar font, at least 16 points.
00077   mDefaultTimeBarFont.setPointSize(
00078     QMAX( mDefaultTimeBarFont.pointSize() + 4, 16 ) );
00079 
00080   mDefaultMonthViewFont = KGlobalSettings::generalFont();
00081   // make it a bit smaller
00082   mDefaultMonthViewFont.setPointSize( mDefaultMonthViewFont.pointSize() - 2 );
00083 
00084   KConfigSkeleton::setCurrentGroup( "General" );
00085 
00086   addItemPath( "Html Export File", mHtmlExportFile,
00087    QDir::homeDirPath() + "/" + i18n( "Default export file", "calendar.html" ) );
00088 
00089   timeBarFontItem()->setDefaultValue( mDefaultTimeBarFont );
00090   monthViewFontItem()->setDefaultValue( mDefaultMonthViewFont );
00091 
00092   // Load it now, not deep within some painting code
00093   mMyAddrBookMails = KABC::StdAddressBook::self()->whoAmI().emails();
00094 }
00095 
00096 
00097 KOPrefs::~KOPrefs()
00098 {
00099   kdDebug(5850) << "KOPrefs::~KOPrefs()" << endl;
00100 }
00101 
00102 
00103 KOPrefs *KOPrefs::instance()
00104 {
00105   if ( !mInstance ) {
00106     insd.setObject( mInstance, new KOPrefs() );
00107     mInstance->readConfig();
00108   }
00109 
00110   return mInstance;
00111 }
00112 
00113 void KOPrefs::usrSetDefaults()
00114 {
00115   // Default should be set a bit smarter, respecting username and locale
00116   // settings for example.
00117 
00118   KEMailSettings settings;
00119   QString tmp = settings.getSetting(KEMailSettings::RealName);
00120   if ( !tmp.isEmpty() ) setUserName( tmp );
00121   tmp = settings.getSetting(KEMailSettings::EmailAddress);
00122   if ( !tmp.isEmpty() ) setUserEmail( tmp );
00123   fillMailDefaults();
00124 
00125   mTimeBarFont = mDefaultTimeBarFont;
00126   mMonthViewFont = mDefaultMonthViewFont;
00127 
00128   setTimeZoneIdDefault();
00129 
00130   KPimPrefs::usrSetDefaults();
00131 }
00132 
00133 void KOPrefs::fillMailDefaults()
00134 {
00135   userEmailItem()->swapDefault();
00136   QString defEmail = userEmailItem()->value();
00137   userEmailItem()->swapDefault();
00138 
00139   if ( userEmail() == defEmail ) {
00140     // No korg settings - but maybe there's a kcontrol[/kmail] setting available
00141     KEMailSettings settings;
00142     if ( !settings.getSetting( KEMailSettings::EmailAddress ).isEmpty() )
00143       mEmailControlCenter = true;
00144   }
00145 }
00146 
00147 void KOPrefs::setTimeZoneIdDefault()
00148 {
00149   QString zone;
00150 
00151   zone = KTimezones().local()->name();
00152 
00153   kdDebug() << "----- time zone: " << zone << endl;
00154 
00155   mTimeZoneId = zone;
00156 }
00157 
00158 void KOPrefs::setCategoryDefaults()
00159 {
00160   mCustomCategories.clear();
00161 
00162   mCustomCategories << i18n("Appointment") << i18n("Business")
00163       << i18n("Meeting") << i18n("Phone Call") << i18n("Education")
00164       << i18n("Holiday") << i18n("Vacation") << i18n("Special Occasion")
00165       << i18n("Personal") << i18n("Travel") << i18n("Miscellaneous")
00166       << i18n("Birthday");
00167 }
00168 
00169 
00170 void KOPrefs::usrReadConfig()
00171 {
00172   config()->setGroup("General");
00173   mCustomCategories = config()->readListEntry("Custom Categories");
00174   if (mCustomCategories.isEmpty()) setCategoryDefaults();
00175 
00176   // old category colors, ignore if they have the old default
00177   // should be removed a few versions after 3.2...
00178   config()->setGroup("Category Colors");
00179   QValueList<QColor> oldCategoryColors;
00180   QStringList::Iterator it;
00181   for (it = mCustomCategories.begin();it != mCustomCategories.end();++it ) {
00182     QColor c = config()->readColorEntry(*it, &mDefaultCategoryColor);
00183     oldCategoryColors.append( (c == QColor(196,196,196)) ?
00184                               mDefaultCategoryColor : c);
00185   }
00186 
00187   // new category colors
00188   config()->setGroup("Category Colors2");
00189   QValueList<QColor>::Iterator it2;
00190   for (it = mCustomCategories.begin(), it2 = oldCategoryColors.begin();
00191        it != mCustomCategories.end(); ++it, ++it2 ) {
00192       QColor c = config()->readColorEntry(*it, &*it2);
00193       if ( c != mDefaultCategoryColor )
00194           setCategoryColor(*it,c);
00195   }
00196 
00197   config()->setGroup( "Resources Colors" );
00198   QMap<QString, QString> map = config()->entryMap( "Resources Colors" );
00199 
00200   QMapIterator<QString, QString> it3;
00201   for( it3 = map.begin(); it3 != map.end(); ++it3 ) {
00202     kdDebug(5850)<< "KOPrefs::usrReadConfig: key: " << it3.key() << " value: "
00203       << it3.data()<<endl;
00204     setResourceColor( it3.key(), config()->readColorEntry( it3.key(),
00205       &mDefaultResourceColor ) );
00206   }
00207 
00208 
00209   if (mTimeZoneId.isEmpty()) {
00210     setTimeZoneIdDefault();
00211   }
00212 
00213 #if 0
00214   config()->setGroup("FreeBusy");
00215   if( mRememberRetrievePw )
00216     mRetrievePassword = KStringHandler::obscure( config()->readEntry( "Retrieve Server Password" ) );
00217 #endif
00218   KPimPrefs::usrReadConfig();
00219   fillMailDefaults();
00220 }
00221 
00222 
00223 void KOPrefs::usrWriteConfig()
00224 {
00225   config()->setGroup("General");
00226   config()->writeEntry("Custom Categories",mCustomCategories);
00227 
00228   config()->setGroup("Category Colors2");
00229   QDictIterator<QColor> it(mCategoryColors);
00230   while (it.current()) {
00231     config()->writeEntry(it.currentKey(),*(it.current()));
00232     ++it;
00233   }
00234 
00235   config()->setGroup( "Resources Colors" );
00236   QDictIterator<QColor> it2( mResourceColors );
00237   while( it2.current() ) {
00238     config()->writeEntry( it2.currentKey(), *( it2.current() ) );
00239     ++it2;
00240   }
00241 
00242   if( !mFreeBusyPublishSavePassword ) {
00243     KConfigSkeleton::ItemPassword *i = freeBusyPublishPasswordItem();
00244     i->setValue( "" );
00245     i->writeConfig( config() );
00246   }
00247   if( !mFreeBusyRetrieveSavePassword ) {
00248     KConfigSkeleton::ItemPassword *i = freeBusyRetrievePasswordItem();
00249     i->setValue( "" );
00250     i->writeConfig( config() );
00251   }
00252 
00253 #if 0
00254   if( mRememberRetrievePw )
00255     config()->writeEntry( "Retrieve Server Password", KStringHandler::obscure( mRetrievePassword ) );
00256   else
00257     config()->deleteEntry( "Retrieve Server Password" );
00258 #endif
00259 
00260   KPimPrefs::usrWriteConfig();
00261 }
00262 
00263 void KOPrefs::setCategoryColor( const QString &cat, const QColor & color)
00264 {
00265   mCategoryColors.replace( cat, new QColor( color ) );
00266 }
00267 
00268 QColor *KOPrefs::categoryColor( const QString &cat )
00269 {
00270   QColor *color = 0;
00271 
00272   if ( !cat.isEmpty() ) color = mCategoryColors[ cat ];
00273 
00274   if ( color ) return color;
00275   else return &mDefaultCategoryColor;
00276 }
00277 
00278 
00279 bool KOPrefs::hasCategoryColor( const QString& cat ) const
00280 {
00281     return mCategoryColors[ cat ];
00282 }
00283 
00284 void KOPrefs::setResourceColor ( const QString &cal, const QColor &color )
00285 {
00286   kdDebug(5850)<<"KOPrefs::setResourceColor: " << cal << " color: "<<
00287     color.name()<<endl;
00288   mResourceColors.replace( cal, new QColor( color ) );
00289 }
00290 
00291 QColor* KOPrefs::resourceColor( const QString &cal )
00292 {
00293   QColor *color=0;
00294   if( !cal.isEmpty() ) color = mResourceColors[cal];
00295 
00296   // assign default color if enabled
00297   if ( !cal.isEmpty() && !color && assignDefaultResourceColors() ) {
00298     QColor defColor( 0x37, 0x7A, 0xBC );
00299     if ( defaultResourceColorSeed() > 0 && defaultResourceColorSeed() - 1 < (int)defaultResourceColors().size() ) {
00300         defColor = QColor( defaultResourceColors()[defaultResourceColorSeed()-1] );
00301     } else {
00302         int h, s, v;
00303         defColor.getHsv( h, s, v );
00304         h = ( defaultResourceColorSeed() % 12 ) * 30;
00305         s -= s * ( (defaultResourceColorSeed() / 12) % 2 ) * 0.5;
00306         defColor.setHsv( h, s, v );
00307     }
00308     setDefaultResourceColorSeed( defaultResourceColorSeed() + 1 );
00309     setResourceColor( cal, defColor );
00310     color = mResourceColors[cal];
00311   }
00312 
00313   if (color && color->isValid() )
00314     return color;
00315   else
00316     return &mDefaultResourceColor;
00317 }
00318 
00319 QString KOPrefs::fullName()
00320 {
00321   if ( mEmailControlCenter ) {
00322     KEMailSettings settings;
00323     return settings.getSetting( KEMailSettings::RealName );
00324   } else {
00325     return userName();
00326   }
00327 }
00328 
00329 QString KOPrefs::email()
00330 {
00331   if ( mEmailControlCenter ) {
00332     KEMailSettings settings;
00333     return settings.getSetting( KEMailSettings::EmailAddress );
00334   } else {
00335     return userEmail();
00336   }
00337 }
00338 
00339 QStringList KOPrefs::allEmails()
00340 {
00341   // Grab emails from the email identities
00342   QStringList lst = KOCore::self()->identityManager()->allEmails();
00343   // Add emails configured in korganizer
00344   lst += mAdditionalMails;
00345   // Add emails from the user's kaddressbook entry
00346   lst += mMyAddrBookMails;
00347   // Add the email entered as the userEmail here
00348   lst += email();
00349 
00350   // Warning, this list could contain duplicates.
00351   return lst;
00352 }
00353 
00354 QStringList KOPrefs::fullEmails()
00355 {
00356   QStringList fullEmails;
00357   // The user name and email from the config dialog:
00358   fullEmails << QString("%1 <%2>").arg( fullName() ).arg( email() );
00359 
00360   QStringList::Iterator it;
00361   // Grab emails from the email identities
00362   KPIM::IdentityManager *idmanager = KOCore::self()->identityManager();
00363   QStringList lst = idmanager->identities();
00364   KPIM::IdentityManager::ConstIterator it1;
00365   for ( it1 = idmanager->begin() ; it1 != idmanager->end() ; ++it1 ) {
00366     fullEmails << (*it1).fullEmailAddr();
00367   }
00368   // Add emails configured in korganizer
00369   lst = mAdditionalMails;
00370   for ( it = lst.begin(); it != lst.end(); ++it ) {
00371     fullEmails << QString("%1 <%2>").arg( fullName() ).arg( *it );
00372   }
00373   // Add emails from the user's kaddressbook entry
00374   KABC::Addressee me = KABC::StdAddressBook::self()->whoAmI();
00375   lst = me.emails();
00376   for ( it = lst.begin(); it != lst.end(); ++it ) {
00377     fullEmails << me.fullEmail( *it );
00378   }
00379 
00380   // Warning, this list could contain duplicates.
00381   return fullEmails;
00382 }
00383 
00384 bool KOPrefs::thatIsMe( const QString& _email )
00385 {
00386   // NOTE: this method is called for every created agenda view item, so we need to keep
00387   // performance in mind
00388 
00389   /* identityManager()->thatIsMe() is quite expensive since it does parsing of
00390      _email in a way which is unnecessarily complex for what we can have here,
00391      so we do that ourselves. This makes sense since this
00392 
00393   if ( KOCore::self()->identityManager()->thatIsMe( _email ) )
00394     return true;
00395   */
00396 
00397   // in case email contains a full name, strip it out
00398   // the below is the simpler but slower version of the following KMime code
00399   // const QString email = KPIM::getEmailAddress( _email );
00400   const QCString tmp = _email.utf8();
00401   const char *cursor = tmp.data();
00402   const char *end = tmp.data() + tmp.length();
00403   KMime::Types::Mailbox mbox;
00404   KMime::HeaderParsing::parseMailbox( cursor, end, mbox );
00405   const QString email = mbox.addrSpec.asString();
00406 
00407   for ( KPIM::IdentityManager::ConstIterator it = KOCore::self()->identityManager()->begin();
00408         it != KOCore::self()->identityManager()->end(); ++it ) {
00409     if ( email == (*it).emailAddr() )
00410       return true;
00411   }
00412 
00413   if ( mAdditionalMails.find( email ) != mAdditionalMails.end() )
00414     return true;
00415   QStringList lst = mMyAddrBookMails;
00416   if ( lst.find( email ) != lst.end() )
00417     return true;
00418   return false;
00419 }
KDE Home | KDE Accessibility Home | Description of Access Keys