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 "koprefs.h"
00046 #include <libkpimidentities/identitymanager.h>
00047 #include <libkpimidentities/identity.h>
00048 #include <libemailfunctions/email.h>
00049 #include <kabc/stdaddressbook.h>
00050 #include <ktimezones.h>
00051 #include "kocore.h"
00052 
00053 KOPrefs *KOPrefs::mInstance = 0;
00054 static KStaticDeleter<KOPrefs> insd;
00055 
00056 QColor getTextColor(const QColor &c)
00057 {
00058   float luminance = (c.red() * 0.299) + (c.green() * 0.587) + (c.blue() * 0.114);
00059   return (luminance > 128.0) ? QColor( 0, 0 ,0 ) : QColor( 255, 255 ,255 );
00060 }
00061 
00062 
00063 KOPrefs::KOPrefs() :
00064   KOPrefsBase()
00065 {
00066   mCategoryColors.setAutoDelete( true );
00067   mResourceColors.setAutoDelete( true );
00068 
00069   mDefaultCategoryColor = QColor( 151, 235, 121 );
00070 
00071   mDefaultResourceColor = QColor();//Default is a color invalid
00072 
00073   mDefaultTimeBarFont = KGlobalSettings::generalFont();
00074   // make a large default time bar font, at least 16 points.
00075   mDefaultTimeBarFont.setPointSize(
00076     QMAX( mDefaultTimeBarFont.pointSize() + 4, 16 ) );
00077 
00078   mDefaultMonthViewFont = KGlobalSettings::generalFont();
00079   // make it a bit smaller
00080   mDefaultMonthViewFont.setPointSize( mDefaultMonthViewFont.pointSize() - 2 );
00081 
00082   KConfigSkeleton::setCurrentGroup( "General" );
00083 
00084   addItemPath( "Html Export File", mHtmlExportFile,
00085    QDir::homeDirPath() + "/" + i18n( "Default export file", "calendar.html" ) );
00086 
00087   timeBarFontItem()->setDefaultValue( mDefaultTimeBarFont );
00088   monthViewFontItem()->setDefaultValue( mDefaultMonthViewFont );
00089   eventColorItem()->setDefaultValue( mDefaultCategoryColor );
00090 
00091   // Load it now, not deep within some painting code
00092   KABC::StdAddressBook::self();
00093 }
00094 
00095 
00096 KOPrefs::~KOPrefs()
00097 {
00098   kdDebug(5850) << "KOPrefs::~KOPrefs()" << endl;
00099 }
00100 
00101 
00102 KOPrefs *KOPrefs::instance()
00103 {
00104   if ( !mInstance ) {
00105     insd.setObject( mInstance, new KOPrefs() );
00106     mInstance->readConfig();
00107   }
00108 
00109   return mInstance;
00110 }
00111 
00112 void KOPrefs::usrSetDefaults()
00113 {
00114   // Default should be set a bit smarter, respecting username and locale
00115   // settings for example.
00116 
00117   KEMailSettings settings;
00118   QString tmp = settings.getSetting(KEMailSettings::RealName);
00119   if ( !tmp.isEmpty() ) setUserName( tmp );
00120   tmp = settings.getSetting(KEMailSettings::EmailAddress);
00121   if ( !tmp.isEmpty() ) setUserEmail( tmp );
00122   fillMailDefaults();
00123 
00124   mTimeBarFont = mDefaultTimeBarFont;
00125   mMonthViewFont = mDefaultMonthViewFont;
00126 
00127   setTimeZoneIdDefault();
00128 
00129   KPimPrefs::usrSetDefaults();
00130 }
00131 
00132 void KOPrefs::fillMailDefaults()
00133 {
00134   userEmailItem()->swapDefault();
00135   QString defEmail = userEmailItem()->value();
00136   userEmailItem()->swapDefault();
00137 
00138   if ( userEmail() == defEmail ) {
00139     // No korg settings - but maybe there's a kcontrol[/kmail] setting available
00140     KEMailSettings settings;
00141     if ( !settings.getSetting( KEMailSettings::EmailAddress ).isEmpty() )
00142       mEmailControlCenter = true;
00143   }
00144 }
00145 
00146 void KOPrefs::setTimeZoneIdDefault()
00147 {
00148   QString zone;
00149 
00150   zone = KTimezones().local()->name();
00151 
00152   kdDebug() << "----- time zone: " << zone << endl;
00153 
00154   mTimeZoneId = zone;
00155 }
00156 
00157 void KOPrefs::setCategoryDefaults()
00158 {
00159   mCustomCategories.clear();
00160 
00161   mCustomCategories << i18n("Appointment") << i18n("Business")
00162       << i18n("Meeting") << i18n("Phone Call") << i18n("Education")
00163       << i18n("Holiday") << i18n("Vacation") << i18n("Special Occasion")
00164       << i18n("Personal") << i18n("Travel") << i18n("Miscellaneous")
00165       << i18n("Birthday");
00166 }
00167 
00168 
00169 void KOPrefs::usrReadConfig()
00170 {
00171   config()->setGroup("General");
00172   mCustomCategories = config()->readListEntry("Custom Categories");
00173   if (mCustomCategories.isEmpty()) setCategoryDefaults();
00174 
00175   // old category colors, ignore if they have the old default
00176   // should be removed a few versions after 3.2...
00177   config()->setGroup("Category Colors");
00178   QValueList<QColor> oldCategoryColors;
00179   QStringList::Iterator it;
00180   for (it = mCustomCategories.begin();it != mCustomCategories.end();++it ) {
00181     QColor c = config()->readColorEntry(*it, &mDefaultCategoryColor);
00182     oldCategoryColors.append( (c == QColor(196,196,196)) ?
00183                               mDefaultCategoryColor : c);
00184   }
00185 
00186   // new category colors
00187   config()->setGroup("Category Colors2");
00188   QValueList<QColor>::Iterator it2;
00189   for (it = mCustomCategories.begin(), it2 = oldCategoryColors.begin();
00190        it != mCustomCategories.end(); ++it, ++it2 ) {
00191       QColor c = config()->readColorEntry(*it, &*it2);
00192       if ( c != mDefaultCategoryColor )
00193           setCategoryColor(*it,c);
00194   }
00195 
00196   config()->setGroup( "Resources Colors" );
00197   QMap<QString, QString> map = config()->entryMap( "Resources Colors" );
00198 
00199   QMapIterator<QString, QString> it3;
00200   for( it3 = map.begin(); it3 != map.end(); ++it3 ) {
00201     kdDebug(5850)<< "KOPrefs::usrReadConfig: key: " << it3.key() << " value: "
00202       << it3.data()<<endl;
00203     setResourceColor( it3.key(), config()->readColorEntry( it3.key(),
00204       &mDefaultResourceColor ) );
00205   }
00206 
00207 
00208   if (mTimeZoneId.isEmpty()) {
00209     setTimeZoneIdDefault();
00210   }
00211 
00212 #if 0
00213   config()->setGroup("FreeBusy");
00214   if( mRememberRetrievePw )
00215     mRetrievePassword = KStringHandler::obscure( config()->readEntry( "Retrieve Server Password" ) );
00216 #endif
00217   KPimPrefs::usrReadConfig();
00218   fillMailDefaults();
00219 }
00220 
00221 
00222 void KOPrefs::usrWriteConfig()
00223 {
00224   config()->setGroup("General");
00225   config()->writeEntry("Custom Categories",mCustomCategories);
00226 
00227   config()->setGroup("Category Colors2");
00228   QDictIterator<QColor> it(mCategoryColors);
00229   while (it.current()) {
00230     config()->writeEntry(it.currentKey(),*(it.current()));
00231     ++it;
00232   }
00233 
00234   config()->setGroup( "Resources Colors" );
00235   QDictIterator<QColor> it2( mResourceColors );
00236   while( it2.current() ) {
00237     config()->writeEntry( it2.currentKey(), *( it2.current() ) );
00238     ++it2;
00239   }
00240 
00241   if( !mFreeBusyPublishSavePassword ) {
00242     KConfigSkeleton::ItemPassword *i = freeBusyPublishPasswordItem();
00243     i->setValue( "" );
00244     i->writeConfig( config() );
00245   }
00246   if( !mFreeBusyRetrieveSavePassword ) {
00247     KConfigSkeleton::ItemPassword *i = freeBusyRetrievePasswordItem();
00248     i->setValue( "" );
00249     i->writeConfig( config() );
00250   }
00251 
00252 #if 0
00253   if( mRememberRetrievePw )
00254     config()->writeEntry( "Retrieve Server Password", KStringHandler::obscure( mRetrievePassword ) );
00255   else
00256     config()->deleteEntry( "Retrieve Server Password" );
00257 #endif
00258 
00259   KPimPrefs::usrWriteConfig();
00260 }
00261 
00262 void KOPrefs::setCategoryColor( const QString &cat, const QColor & color)
00263 {
00264   mCategoryColors.replace( cat, new QColor( color ) );
00265 }
00266 
00267 QColor *KOPrefs::categoryColor( const QString &cat )
00268 {
00269   QColor *color = 0;
00270 
00271   if ( !cat.isEmpty() ) color = mCategoryColors[ cat ];
00272 
00273   if ( color ) return color;
00274   else return &mDefaultCategoryColor;
00275 }
00276 
00277 
00278 bool KOPrefs::hasCategoryColor( const QString& cat ) const
00279 {
00280     return mCategoryColors[ cat ];
00281 }
00282 
00283 void KOPrefs::setResourceColor ( const QString &cal, const QColor &color )
00284 {
00285   kdDebug(5850)<<"KOPrefs::setResourceColor: " << cal << " color: "<<
00286     color.name()<<endl;
00287   mResourceColors.replace( cal, new QColor( color ) );
00288 }
00289 
00290 QColor* KOPrefs::resourceColor( const QString &cal )
00291 {
00292   QColor *color=0;
00293   if( !cal.isEmpty() ) color = mResourceColors[cal];
00294 
00295   // assign default color if enabled
00296   if ( !cal.isEmpty() && !color && assignDefaultResourceColors() ) {
00297     QColor defColor( 0x37, 0x7A, 0xBC );
00298     if ( defaultResourceColorSeed() > 0 && defaultResourceColorSeed() - 1 < (int)defaultResourceColors().size() ) {
00299         defColor = QColor( defaultResourceColors()[defaultResourceColorSeed()-1] );
00300     } else {
00301         int h, s, v;
00302         defColor.getHsv( h, s, v );
00303         h = ( defaultResourceColorSeed() % 12 ) * 30;
00304         s -= s * ( (defaultResourceColorSeed() / 12) % 2 ) * 0.5;
00305         defColor.setHsv( h, s, v );
00306     }
00307     setDefaultResourceColorSeed( defaultResourceColorSeed() + 1 );
00308     setResourceColor( cal, defColor );
00309     color = mResourceColors[cal];
00310   }
00311 
00312   if (color && color->isValid() )
00313     return color;
00314   else
00315     return &mDefaultResourceColor;
00316 }
00317 
00318 QString KOPrefs::fullName()
00319 {
00320   if ( mEmailControlCenter ) {
00321     KEMailSettings settings;
00322     return settings.getSetting( KEMailSettings::RealName );
00323   } else {
00324     return userName();
00325   }
00326 }
00327 
00328 QString KOPrefs::email()
00329 {
00330   if ( mEmailControlCenter ) {
00331     KEMailSettings settings;
00332     return settings.getSetting( KEMailSettings::EmailAddress );
00333   } else {
00334     return userEmail();
00335   }
00336 }
00337 
00338 QStringList KOPrefs::allEmails()
00339 {
00340   // Grab emails from the email identities
00341   QStringList lst = KOCore::self()->identityManager()->allEmails();
00342   // Add emails configured in korganizer
00343   lst += mAdditionalMails;
00344   // Add emails from the user's kaddressbook entry
00345   lst += KABC::StdAddressBook::self()->whoAmI().emails();
00346   // Add the email entered as the userEmail here
00347   lst += email();
00348 
00349   // Warning, this list could contain duplicates.
00350   return lst;
00351 }
00352 
00353 QStringList KOPrefs::fullEmails()
00354 {
00355   QStringList fullEmails;
00356   // The user name and email from the config dialog:
00357   fullEmails << QString("%1 <%2>").arg( fullName() ).arg( email() );
00358 
00359   QStringList::Iterator it;
00360   // Grab emails from the email identities
00361   KPIM::IdentityManager *idmanager = KOCore::self()->identityManager();
00362   QStringList lst = idmanager->identities();
00363   KPIM::IdentityManager::ConstIterator it1;
00364   for ( it1 = idmanager->begin() ; it1 != idmanager->end() ; ++it1 ) {
00365     fullEmails << (*it1).fullEmailAddr();
00366   }
00367   // Add emails configured in korganizer
00368   lst = mAdditionalMails;
00369   for ( it = lst.begin(); it != lst.end(); ++it ) {
00370     fullEmails << QString("%1 <%2>").arg( fullName() ).arg( *it );
00371   }
00372   // Add emails from the user's kaddressbook entry
00373   KABC::Addressee me = KABC::StdAddressBook::self()->whoAmI();
00374   lst = me.emails();
00375   for ( it = lst.begin(); it != lst.end(); ++it ) {
00376     fullEmails << me.fullEmail( *it );
00377   }
00378 
00379   // Warning, this list could contain duplicates.
00380   return fullEmails;
00381 }
00382 
00383 bool KOPrefs::thatIsMe( const QString& _email )
00384 {
00385   if ( KOCore::self()->identityManager()->thatIsMe( _email ) )
00386     return true;
00387   // in case email contains a full name, strip it out
00388   QString email = KPIM::getEmailAddress( _email );
00389   if ( mAdditionalMails.find( email ) != mAdditionalMails.end() )
00390     return true;
00391   QStringList lst = KABC::StdAddressBook::self()->whoAmI().emails();
00392   if ( lst.find( email ) != lst.end() )
00393     return true;
00394   return false;
00395 }
KDE Home | KDE Accessibility Home | Description of Access Keys