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