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   QStringList::Iterator it;
00168   for (it = mCustomCategories.begin();it != mCustomCategories.end();++it ) {
00169     setCategoryColor(*it,mDefaultCategoryColor);
00170   }
00171 }
00172 
00173 
00174 void KOPrefs::usrReadConfig()
00175 {
00176   config()->setGroup("General");
00177   mCustomCategories = config()->readListEntry("Custom Categories");
00178   if (mCustomCategories.isEmpty()) setCategoryDefaults();
00179 
00180   // old category colors, ignore if they have the old default
00181   // should be removed a few versions after 3.2...
00182   config()->setGroup("Category Colors");
00183   QValueList<QColor> oldCategoryColors;
00184   QStringList::Iterator it;
00185   for (it = mCustomCategories.begin();it != mCustomCategories.end();++it ) {
00186     QColor c = config()->readColorEntry(*it, &mDefaultCategoryColor);
00187     oldCategoryColors.append( (c == QColor(196,196,196)) ?
00188                               mDefaultCategoryColor : c);
00189   }
00190 
00191   // new category colors
00192   config()->setGroup("Category Colors2");
00193   QValueList<QColor>::Iterator it2;
00194   for (it = mCustomCategories.begin(), it2 = oldCategoryColors.begin();
00195        it != mCustomCategories.end(); ++it, ++it2 ) {
00196     setCategoryColor(*it,config()->readColorEntry(*it, &*it2));
00197   }
00198 
00199   config()->setGroup( "Resources Colors" );
00200   QMap<QString, QString> map = config()->entryMap( "Resources Colors" );
00201 
00202   QMapIterator<QString, QString> it3;
00203   for( it3 = map.begin(); it3 != map.end(); ++it3 ) {
00204     kdDebug(5850)<< "KOPrefs::usrReadConfig: key: " << it3.key() << " value: "
00205       << it3.data()<<endl;
00206     setResourceColor( it3.key(), config()->readColorEntry( it3.key(),
00207       &mDefaultResourceColor ) );
00208   }
00209 
00210 
00211   if (mTimeZoneId.isEmpty()) {
00212     setTimeZoneIdDefault();
00213   }
00214 
00215 #if 0
00216   config()->setGroup("FreeBusy");
00217   if( mRememberRetrievePw )
00218     mRetrievePassword = KStringHandler::obscure( config()->readEntry( "Retrieve Server Password" ) );
00219 #endif
00220   KPimPrefs::usrReadConfig();
00221   fillMailDefaults();
00222 }
00223 
00224 
00225 void KOPrefs::usrWriteConfig()
00226 {
00227   config()->setGroup("General");
00228   config()->writeEntry("Custom Categories",mCustomCategories);
00229 
00230   config()->setGroup("Category Colors2");
00231   QDictIterator<QColor> it(mCategoryColors);
00232   while (it.current()) {
00233     config()->writeEntry(it.currentKey(),*(it.current()));
00234     ++it;
00235   }
00236 
00237   config()->setGroup( "Resources Colors" );
00238   QDictIterator<QColor> it2( mResourceColors );
00239   while( it2.current() ) {
00240     config()->writeEntry( it2.currentKey(), *( it2.current() ) );
00241     ++it2;
00242   }
00243 
00244   if( !mFreeBusyPublishSavePassword ) {
00245     KConfigSkeleton::ItemPassword *i = freeBusyPublishPasswordItem();
00246     i->setValue( "" );
00247     i->writeConfig( config() );
00248   }
00249   if( !mFreeBusyRetrieveSavePassword ) {
00250     KConfigSkeleton::ItemPassword *i = freeBusyRetrievePasswordItem();
00251     i->setValue( "" );
00252     i->writeConfig( config() );
00253   }
00254 
00255 #if 0
00256   if( mRememberRetrievePw )
00257     config()->writeEntry( "Retrieve Server Password", KStringHandler::obscure( mRetrievePassword ) );
00258   else
00259     config()->deleteEntry( "Retrieve Server Password" );
00260 #endif
00261 
00262   KPimPrefs::usrWriteConfig();
00263 }
00264 
00265 void KOPrefs::setCategoryColor( const QString &cat, const QColor & color)
00266 {
00267   mCategoryColors.replace( cat, new QColor( color ) );
00268 }
00269 
00270 QColor *KOPrefs::categoryColor( const QString &cat )
00271 {
00272   QColor *color = 0;
00273 
00274   if ( !cat.isEmpty() ) color = mCategoryColors[ cat ];
00275 
00276   if ( color ) return color;
00277   else return &mDefaultCategoryColor;
00278 }
00279 
00280 void KOPrefs::setResourceColor ( const QString &cal, const QColor &color )
00281 {
00282   kdDebug(5850)<<"KOPrefs::setResourceColor: " << cal << " color: "<<
00283     color.name()<<endl;
00284   mResourceColors.replace( cal, new QColor( color ) );
00285 }
00286 
00287 QColor* KOPrefs::resourceColor( const QString &cal )
00288 {
00289   QColor *color=0;
00290   if( !cal.isEmpty() ) color = mResourceColors[cal];
00291 
00292   if (color && color->isValid() )
00293     return color;
00294   else
00295     return &mDefaultResourceColor;
00296 }
00297 
00298 QString KOPrefs::fullName()
00299 {
00300   if ( mEmailControlCenter ) {
00301     KEMailSettings settings;
00302     return settings.getSetting( KEMailSettings::RealName );
00303   } else {
00304     return userName();
00305   }
00306 }
00307 
00308 QString KOPrefs::email()
00309 {
00310   if ( mEmailControlCenter ) {
00311     KEMailSettings settings;
00312     return settings.getSetting( KEMailSettings::EmailAddress );
00313   } else {
00314     return userEmail();
00315   }
00316 }
00317 
00318 QStringList KOPrefs::allEmails()
00319 {
00320   // Grab emails from the email identities
00321   QStringList lst = KOCore::self()->identityManager()->allEmails();
00322   // Add emails configured in korganizer
00323   lst += mAdditionalMails;
00324   // Add emails from the user's kaddressbook entry
00325   lst += KABC::StdAddressBook::self()->whoAmI().emails();
00326   // Add the email entered as the userEmail here
00327   lst += email();
00328 
00329   // Warning, this list could contain duplicates.
00330   return lst;
00331 }
00332 
00333 QStringList KOPrefs::fullEmails()
00334 {
00335   QStringList fullEmails;
00336   // The user name and email from the config dialog:
00337   fullEmails << QString("%1 <%2>").arg( fullName() ).arg( email() );
00338 
00339   QStringList::Iterator it;
00340   // Grab emails from the email identities
00341   KPIM::IdentityManager *idmanager = KOCore::self()->identityManager();
00342   QStringList lst = idmanager->identities();
00343   KPIM::IdentityManager::ConstIterator it1;
00344   for ( it1 = idmanager->begin() ; it1 != idmanager->end() ; ++it1 ) {
00345     fullEmails << (*it1).fullEmailAddr();
00346   }
00347   // Add emails configured in korganizer
00348   lst = mAdditionalMails;
00349   for ( it = lst.begin(); it != lst.end(); ++it ) {
00350     fullEmails << QString("%1 <%2>").arg( fullName() ).arg( *it );
00351   }
00352   // Add emails from the user's kaddressbook entry
00353   KABC::Addressee me = KABC::StdAddressBook::self()->whoAmI();
00354   lst = me.emails();
00355   for ( it = lst.begin(); it != lst.end(); ++it ) {
00356     fullEmails << me.fullEmail( *it );
00357   }
00358 
00359   // Warning, this list could contain duplicates.
00360   return fullEmails;
00361 }
00362 
00363 bool KOPrefs::thatIsMe( const QString& _email )
00364 {
00365   if ( KOCore::self()->identityManager()->thatIsMe( _email ) )
00366     return true;
00367   // in case email contains a full name, strip it out
00368   QString email = KPIM::getEmailAddress( _email );
00369   if ( mAdditionalMails.find( email ) != mAdditionalMails.end() )
00370     return true;
00371   QStringList lst = KABC::StdAddressBook::self()->whoAmI().emails();
00372   if ( lst.find( email ) != lst.end() )
00373     return true;
00374   return false;
00375 }
KDE Home | KDE Accessibility Home | Description of Access Keys