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   eventColorItem()->setDefaultValue( mDefaultCategoryColor );
00092 
00093   // Load it now, not deep within some painting code
00094   mMyAddrBookMails = KABC::StdAddressBook::self()->whoAmI().emails();
00095 }
00096 
00097 
00098 KOPrefs::~KOPrefs()
00099 {
00100   kdDebug(5850) << "KOPrefs::~KOPrefs()" << endl;
00101 }
00102 
00103 
00104 KOPrefs *KOPrefs::instance()
00105 {
00106   if ( !mInstance ) {
00107     insd.setObject( mInstance, new KOPrefs() );
00108     mInstance->readConfig();
00109   }
00110 
00111   return mInstance;
00112 }
00113 
00114 void KOPrefs::usrSetDefaults()
00115 {
00116   // Default should be set a bit smarter, respecting username and locale
00117   // settings for example.
00118 
00119   KEMailSettings settings;
00120   QString tmp = settings.getSetting(KEMailSettings::RealName);
00121   if ( !tmp.isEmpty() ) setUserName( tmp );
00122   tmp = settings.getSetting(KEMailSettings::EmailAddress);
00123   if ( !tmp.isEmpty() ) setUserEmail( tmp );
00124   fillMailDefaults();
00125 
00126   mTimeBarFont = mDefaultTimeBarFont;
00127   mMonthViewFont = mDefaultMonthViewFont;
00128 
00129   setTimeZoneIdDefault();
00130 
00131   KPimPrefs::usrSetDefaults();
00132 }
00133 
00134 void KOPrefs::fillMailDefaults()
00135 {
00136   userEmailItem()->swapDefault();
00137   QString defEmail = userEmailItem()->value();
00138   userEmailItem()->swapDefault();
00139 
00140   if ( userEmail() == defEmail ) {
00141     // No korg settings - but maybe there's a kcontrol[/kmail] setting available
00142     KEMailSettings settings;
00143     if ( !settings.getSetting( KEMailSettings::EmailAddress ).isEmpty() )
00144       mEmailControlCenter = true;
00145   }
00146 }
00147 
00148 void KOPrefs::setTimeZoneIdDefault()
00149 {
00150   QString zone;
00151 
00152   zone = KTimezones().local()->name();
00153 
00154   kdDebug() << "----- time zone: " << zone << endl;
00155 
00156   mTimeZoneId = zone;
00157 }
00158 
00159 void KOPrefs::setCategoryDefaults()
00160 {
00161   mCustomCategories.clear();
00162 
00163   mCustomCategories << i18n("Appointment") << i18n("Business")
00164       << i18n("Meeting") << i18n("Phone Call") << i18n("Education")
00165       << i18n("Holiday") << i18n("Vacation") << i18n("Special Occasion")
00166       << i18n("Personal") << i18n("Travel") << i18n("Miscellaneous")
00167       << i18n("Birthday");
00168 }
00169 
00170 
00171 void KOPrefs::usrReadConfig()
00172 {
00173   config()->setGroup("General");
00174   mCustomCategories = config()->readListEntry("Custom Categories");
00175   if (mCustomCategories.isEmpty()) setCategoryDefaults();
00176 
00177   // old category colors, ignore if they have the old default
00178   // should be removed a few versions after 3.2...
00179   config()->setGroup("Category Colors");
00180   QValueList<QColor> oldCategoryColors;
00181   QStringList::Iterator it;
00182   for (it = mCustomCategories.begin();it != mCustomCategories.end();++it ) {
00183     QColor c = config()->readColorEntry(*it, &mDefaultCategoryColor);
00184     oldCategoryColors.append( (c == QColor(196,196,196)) ?
00185                               mDefaultCategoryColor : c);
00186   }
00187 
00188   // new category colors
00189   config()->setGroup("Category Colors2");
00190   QValueList<QColor>::Iterator it2;
00191   for (it = mCustomCategories.begin(), it2 = oldCategoryColors.begin();
00192        it != mCustomCategories.end(); ++it, ++it2 ) {
00193       QColor c = config()->readColorEntry(*it, &*it2);
00194       if ( c != mDefaultCategoryColor )
00195           setCategoryColor(*it,c);
00196   }
00197 
00198   config()->setGroup( "Resources Colors" );
00199   QMap<QString, QString> map = config()->entryMap( "Resources Colors" );
00200 
00201   QMapIterator<QString, QString> it3;
00202   for( it3 = map.begin(); it3 != map.end(); ++it3 ) {
00203     kdDebug(5850)<< "KOPrefs::usrReadConfig: key: " << it3.key() << " value: "
00204       << it3.data()<<endl;
00205     setResourceColor( it3.key(), config()->readColorEntry( it3.key(),
00206       &mDefaultResourceColor ) );
00207   }
00208 
00209 
00210   if (mTimeZoneId.isEmpty()) {
00211     setTimeZoneIdDefault();
00212   }
00213 
00214 #if 0
00215   config()->setGroup("FreeBusy");
00216   if( mRememberRetrievePw )
00217     mRetrievePassword = KStringHandler::obscure( config()->readEntry( "Retrieve Server Password" ) );
00218 #endif
00219   KPimPrefs::usrReadConfig();
00220   fillMailDefaults();
00221 }
00222 
00223 
00224 void KOPrefs::usrWriteConfig()
00225 {
00226   config()->setGroup("General");
00227   config()->writeEntry("Custom Categories",mCustomCategories);
00228 
00229   config()->setGroup("Category Colors2");
00230   QDictIterator<QColor> it(mCategoryColors);
00231   while (it.current()) {
00232     config()->writeEntry(it.currentKey(),*(it.current()));
00233     ++it;
00234   }
00235 
00236   config()->setGroup( "Resources Colors" );
00237   QDictIterator<QColor> it2( mResourceColors );
00238   while( it2.current() ) {
00239     config()->writeEntry( it2.currentKey(), *( it2.current() ) );
00240     ++it2;
00241   }
00242 
00243   if( !mFreeBusyPublishSavePassword ) {
00244     KConfigSkeleton::ItemPassword *i = freeBusyPublishPasswordItem();
00245     i->setValue( "" );
00246     i->writeConfig( config() );
00247   }
00248   if( !mFreeBusyRetrieveSavePassword ) {
00249     KConfigSkeleton::ItemPassword *i = freeBusyRetrievePasswordItem();
00250     i->setValue( "" );
00251     i->writeConfig( config() );
00252   }
00253 
00254 #if 0
00255   if( mRememberRetrievePw )
00256     config()->writeEntry( "Retrieve Server Password", KStringHandler::obscure( mRetrievePassword ) );
00257   else
00258     config()->deleteEntry( "Retrieve Server Password" );
00259 #endif
00260 
00261   KPimPrefs::usrWriteConfig();
00262 }
00263 
00264 void KOPrefs::setCategoryColor( const QString &cat, const QColor & color)
00265 {
00266   mCategoryColors.replace( cat, new QColor( color ) );
00267 }
00268 
00269 QColor *KOPrefs::categoryColor( const QString &cat )
00270 {
00271   QColor *color = 0;
00272 
00273   if ( !cat.isEmpty() ) color = mCategoryColors[ cat ];
00274 
00275   if ( color ) return color;
00276   else return &mDefaultCategoryColor;
00277 }
00278 
00279 
00280 bool KOPrefs::hasCategoryColor( const QString& cat ) const
00281 {
00282     return mCategoryColors[ cat ];
00283 }
00284 
00285 void KOPrefs::setResourceColor ( const QString &cal, const QColor &color )
00286 {
00287   kdDebug(5850)<<"KOPrefs::setResourceColor: " << cal << " color: "<<
00288     color.name()<<endl;
00289   mResourceColors.replace( cal, new QColor( color ) );
00290 }
00291 
00292 QColor* KOPrefs::resourceColor( const QString &cal )
00293 {
00294   QColor *color=0;
00295   if( !cal.isEmpty() ) color = mResourceColors[cal];
00296 
00297   // assign default color if enabled
00298   if ( !cal.isEmpty() && !color && assignDefaultResourceColors() ) {
00299     QColor defColor( 0x37, 0x7A, 0xBC );
00300     if ( defaultResourceColorSeed() > 0 && defaultResourceColorSeed() - 1 < (int)defaultResourceColors().size() ) {
00301         defColor = QColor( defaultResourceColors()[defaultResourceColorSeed()-1] );
00302     } else {
00303         int h, s, v;
00304         defColor.getHsv( h, s, v );
00305         h = ( defaultResourceColorSeed() % 12 ) * 30;
00306         s -= s * ( (defaultResourceColorSeed() / 12) % 2 ) * 0.5;
00307         defColor.setHsv( h, s, v );
00308     }
00309     setDefaultResourceColorSeed( defaultResourceColorSeed() + 1 );
00310     setResourceColor( cal, defColor );
00311     color = mResourceColors[cal];
00312   }
00313 
00314   if (color && color->isValid() )
00315     return color;
00316   else
00317     return &mDefaultResourceColor;
00318 }
00319 
00320 QString KOPrefs::fullName()
00321 {
00322   if ( mEmailControlCenter ) {
00323     KEMailSettings settings;
00324     return settings.getSetting( KEMailSettings::RealName );
00325   } else {
00326     return userName();
00327   }
00328 }
00329 
00330 QString KOPrefs::email()
00331 {
00332   if ( mEmailControlCenter ) {
00333     KEMailSettings settings;
00334     return settings.getSetting( KEMailSettings::EmailAddress );
00335   } else {
00336     return userEmail();
00337   }
00338 }
00339 
00340 QStringList KOPrefs::allEmails()
00341 {
00342   // Grab emails from the email identities
00343   QStringList lst = KOCore::self()->identityManager()->allEmails();
00344   // Add emails configured in korganizer
00345   lst += mAdditionalMails;
00346   // Add emails from the user's kaddressbook entry
00347   lst += mMyAddrBookMails;
00348   // Add the email entered as the userEmail here
00349   lst += email();
00350 
00351   // Warning, this list could contain duplicates.
00352   return lst;
00353 }
00354 
00355 QStringList KOPrefs::fullEmails()
00356 {
00357   QStringList fullEmails;
00358   // The user name and email from the config dialog:
00359   fullEmails << QString("%1 <%2>").arg( fullName() ).arg( email() );
00360 
00361   QStringList::Iterator it;
00362   // Grab emails from the email identities
00363   KPIM::IdentityManager *idmanager = KOCore::self()->identityManager();
00364   QStringList lst = idmanager->identities();
00365   KPIM::IdentityManager::ConstIterator it1;
00366   for ( it1 = idmanager->begin() ; it1 != idmanager->end() ; ++it1 ) {
00367     fullEmails << (*it1).fullEmailAddr();
00368   }
00369   // Add emails configured in korganizer
00370   lst = mAdditionalMails;
00371   for ( it = lst.begin(); it != lst.end(); ++it ) {
00372     fullEmails << QString("%1 <%2>").arg( fullName() ).arg( *it );
00373   }
00374   // Add emails from the user's kaddressbook entry
00375   KABC::Addressee me = KABC::StdAddressBook::self()->whoAmI();
00376   lst = me.emails();
00377   for ( it = lst.begin(); it != lst.end(); ++it ) {
00378     fullEmails << me.fullEmail( *it );
00379   }
00380 
00381   // Warning, this list could contain duplicates.
00382   return fullEmails;
00383 }
00384 
00385 bool KOPrefs::thatIsMe( const QString& _email )
00386 {
00387   // NOTE: this method is called for every created agenda view item, so we need to keep
00388   // performance in mind
00389 
00390   /* identityManager()->thatIsMe() is quite expensive since it does parsing of
00391      _email in a way which is unnecessarily complex for what we can have here,
00392      so we do that ourselves. This makes sense since this
00393 
00394   if ( KOCore::self()->identityManager()->thatIsMe( _email ) )
00395     return true;
00396   */
00397 
00398   // in case email contains a full name, strip it out
00399   // the below is the simpler but slower version of the following KMime code
00400   // const QString email = KPIM::getEmailAddress( _email );
00401   const QCString tmp = _email.utf8();
00402   const char *cursor = tmp.data();
00403   const char *end = tmp.data() + tmp.length();
00404   KMime::Types::Mailbox mbox;
00405   KMime::HeaderParsing::parseMailbox( cursor, end, mbox );
00406   const QString email = mbox.addrSpec.asString();
00407 
00408   for ( KPIM::IdentityManager::ConstIterator it = KOCore::self()->identityManager()->begin();
00409         it != KOCore::self()->identityManager()->end(); ++it ) {
00410     if ( email == (*it).emailAddr() )
00411       return true;
00412   }
00413 
00414   if ( mAdditionalMails.find( email ) != mAdditionalMails.end() )
00415     return true;
00416   QStringList lst = mMyAddrBookMails;
00417   if ( lst.find( email ) != lst.end() )
00418     return true;
00419   return false;
00420 }
KDE Home | KDE Accessibility Home | Description of Access Keys