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