korganizer Library API Documentation

koprefs.cpp

00001 /*
00002     This file is part of KOrganizer.
00003 
00004     Copyright (c) 2001,2003 Cornelius Schumacher <schumacher@kde.org>
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019 
00020     As a special exception, permission is given to link this program
00021     with any edition of Qt, and distribute the resulting executable,
00022     without including the source code for Qt in the source distribution.
00023 */
00024 
00025 #include <time.h>
00026 #include <unistd.h>
00027 
00028 #include <qdir.h>
00029 #include <qstring.h>
00030 #include <qfont.h>
00031 #include <qcolor.h>
00032 #include <qstringlist.h>
00033 
00034 #include <kglobalsettings.h>
00035 #include <kglobal.h>
00036 #include <kconfig.h>
00037 #include <klocale.h>
00038 #include <kdebug.h>
00039 #include <kemailsettings.h>
00040 #include <kstaticdeleter.h>
00041 #include <kstringhandler.h>
00042 
00043 #include "koprefs.h"
00044 #include <libkpimidentities/identitymanager.h>
00045 #include <libkpimidentities/identity.h>
00046 #include <libkdepim/email.h>
00047 #include <kabc/stdaddressbook.h>
00048 #include "kocore.h"
00049 
00050 KOPrefs *KOPrefs::mInstance = 0;
00051 static KStaticDeleter<KOPrefs> insd;
00052 
00053 QColor getTextColor(const QColor &c)
00054 {
00055   float luminance = (c.red() * 0.299) + (c.green() * 0.587) + (c.blue() * 0.114);
00056   return (luminance > 128.0) ? QColor( 0, 0 ,0 ) : QColor( 255, 255 ,255 );
00057 }
00058 
00059 
00060 KOPrefs::KOPrefs() :
00061   KOPrefsBase()
00062 {
00063   mCategoryColors.setAutoDelete(true);
00064 
00065   mDefaultCategoryColor = QColor(151, 235, 121);
00066 
00067   mDefaultMonthViewFont = KGlobalSettings::generalFont();
00068   // make it a bit smaller
00069   mDefaultMonthViewFont.setPointSize(mDefaultMonthViewFont.pointSize()-2);
00070 
00071   KConfigSkeleton::setCurrentGroup("General");
00072 
00073   addItemPath("Html Export File",mHtmlExportFile,
00074       QDir::homeDirPath() + "/" + i18n("Default export file", "calendar.html"));
00075 
00076   monthViewFontItem()->setDefaultValue( mDefaultMonthViewFont );
00077   eventColorItem()->setDefaultValue( mDefaultCategoryColor );
00078 }
00079 
00080 
00081 KOPrefs::~KOPrefs()
00082 {
00083   kdDebug(5850) << "KOPrefs::~KOPrefs()" << endl;
00084 }
00085 
00086 
00087 KOPrefs *KOPrefs::instance()
00088 {
00089   if ( !mInstance ) {
00090     insd.setObject( mInstance, new KOPrefs() );
00091     mInstance->readConfig();
00092   }
00093 
00094   return mInstance;
00095 }
00096 
00097 void KOPrefs::usrSetDefaults()
00098 {
00099   // Default should be set a bit smarter, respecting username and locale
00100   // settings for example.
00101 
00102   KEMailSettings settings;
00103   mName = settings.getSetting(KEMailSettings::RealName);
00104   mEmail = settings.getSetting(KEMailSettings::EmailAddress);
00105   fillMailDefaults();
00106 
00107   mMonthViewFont = mDefaultMonthViewFont;
00108 
00109   setTimeZoneIdDefault();
00110 
00111   KPimPrefs::usrSetDefaults();
00112 }
00113 
00114 void KOPrefs::fillMailDefaults()
00115 {
00116   QString defaultEmail = i18n("nobody@nowhere");
00117   if (mEmail.isEmpty())
00118     mEmail = defaultEmail;
00119   if ( mEmail == defaultEmail ) { // from the line above, or from using korganizer previously
00120     // No korg settings - but maybe there's a kcontrol[/kmail] setting available
00121     KEMailSettings settings;
00122     if ( !settings.getSetting( KEMailSettings::EmailAddress ).isEmpty() )
00123       mEmailControlCenter = true;
00124   }
00125   if (mName.isEmpty()) mName = i18n("Anonymous");
00126 }
00127 
00128 void KOPrefs::setTimeZoneIdDefault()
00129 {
00130   QString zone;
00131 
00132   char zonefilebuf[100];
00133   int len = readlink("/etc/localtime",zonefilebuf,100);
00134   if (len > 0 && len < 100) {
00135     zonefilebuf[len] = '\0';
00136     zone = zonefilebuf;
00137     zone = zone.mid(zone.find("zoneinfo/") + 9);
00138   } else {
00139     tzset();
00140     zone = tzname[0];
00141   }
00142 
00143   kdDebug () << "----- time zone: " << zone << endl;
00144 
00145   mTimeZoneId = zone;
00146 }
00147 
00148 void KOPrefs::setCategoryDefaults()
00149 {
00150   mCustomCategories.clear();
00151 
00152   mCustomCategories << i18n("Appointment") << i18n("Business")
00153       << i18n("Meeting") << i18n("Phone Call") << i18n("Education")
00154       << i18n("Holiday") << i18n("Vacation") << i18n("Special Occasion")
00155       << i18n("Personal") << i18n("Travel") << i18n("Miscellaneous")
00156       << i18n("Birthday");
00157 
00158   QStringList::Iterator it;
00159   for (it = mCustomCategories.begin();it != mCustomCategories.end();++it ) {
00160     setCategoryColor(*it,mDefaultCategoryColor);
00161   }
00162 }
00163 
00164 
00165 void KOPrefs::usrReadConfig()
00166 {
00167   config()->setGroup("General");
00168   mCustomCategories = config()->readListEntry("Custom Categories");
00169   if (mCustomCategories.isEmpty()) setCategoryDefaults();
00170 
00171   config()->setGroup("Personal Settings");
00172   mName = config()->readEntry("user_name");
00173   mEmail = config()->readEntry("user_email");
00174   fillMailDefaults();
00175 
00176   // old category colors, ignore if they have the old default
00177   // should be removed a few versions after 3.2...
00178   config()->setGroup("Category Colors");
00179   QValueList<QColor> oldCategoryColors;
00180   QStringList::Iterator it;
00181   for (it = mCustomCategories.begin();it != mCustomCategories.end();++it ) {
00182     QColor c = config()->readColorEntry(*it, &mDefaultCategoryColor);
00183     oldCategoryColors.append( (c == QColor(196,196,196)) ?
00184                               mDefaultCategoryColor : c);
00185   }
00186 
00187   // new category colors
00188   config()->setGroup("Category Colors2");
00189   QValueList<QColor>::Iterator it2;
00190   for (it = mCustomCategories.begin(), it2 = oldCategoryColors.begin();
00191        it != mCustomCategories.end(); ++it, ++it2 ) {
00192     setCategoryColor(*it,config()->readColorEntry(*it, &*it2));
00193   }
00194 
00195   if (mTimeZoneId.isEmpty()) {
00196     setTimeZoneIdDefault();
00197   }
00198 
00199   config()->setGroup("FreeBusy");
00200 #if 0
00201   if( mRememberRetrievePw )
00202     mRetrievePassword = KStringHandler::obscure( config()->readEntry( "Retrieve Server Password" ) );
00203 #endif
00204 kdDebug()<<"KOPrefs::usrReadConfig()"<<endl;
00205   KPimPrefs::usrReadConfig();
00206 }
00207 
00208 
00209 void KOPrefs::usrWriteConfig()
00210 {
00211   config()->setGroup("General");
00212   config()->writeEntry("Custom Categories",mCustomCategories);
00213 
00214   config()->setGroup("Personal Settings");
00215   config()->writeEntry("user_name",mName);
00216   config()->writeEntry("user_email",mEmail);
00217 
00218   config()->setGroup("Category Colors2");
00219   QDictIterator<QColor> it(mCategoryColors);
00220   while (it.current()) {
00221     config()->writeEntry(it.currentKey(),*(it.current()));
00222     ++it;
00223   }
00224 
00225   if( !mFreeBusyPublishSavePassword ) {
00226     KConfigSkeleton::ItemPassword *i = freeBusyPublishPasswordItem();
00227     i->setValue( "" );
00228     i->writeConfig( config() );
00229   }
00230   if( !mFreeBusyRetrieveSavePassword ) {
00231     KConfigSkeleton::ItemPassword *i = freeBusyRetrievePasswordItem();
00232     i->setValue( "" );
00233     i->writeConfig( config() );
00234   }
00235 
00236 #if 0
00237   if( mRememberRetrievePw )
00238     config()->writeEntry( "Retrieve Server Password", KStringHandler::obscure( mRetrievePassword ) );
00239   else
00240     config()->deleteEntry( "Retrieve Server Password" );
00241 #endif
00242 
00243   KPimPrefs::usrWriteConfig();
00244 }
00245 
00246 void KOPrefs::setCategoryColor(QString cat,const QColor & color)
00247 {
00248   mCategoryColors.replace( cat, new QColor( color ) );
00249 }
00250 
00251 QColor *KOPrefs::categoryColor(QString cat)
00252 {
00253   QColor *color = 0;
00254 
00255   if ( !cat.isEmpty() ) color = mCategoryColors[ cat ];
00256 
00257   if ( color ) return color;
00258   else return &mDefaultCategoryColor;
00259 }
00260 
00261 void KOPrefs::setFullName(const QString &name)
00262 {
00263   mName = name;
00264 }
00265 
00266 void KOPrefs::setEmail(const QString &email)
00267 {
00268   mEmail = email;
00269 }
00270 
00271 QString KOPrefs::fullName()
00272 {
00273   if (mEmailControlCenter) {
00274     KEMailSettings settings;
00275     return settings.getSetting(KEMailSettings::RealName);
00276   } else {
00277     return mName;
00278   }
00279 }
00280 
00281 QString KOPrefs::email()
00282 {
00283   if (mEmailControlCenter) {
00284     KEMailSettings settings;
00285     return settings.getSetting(KEMailSettings::EmailAddress);
00286   } else {
00287     return mEmail;
00288   }
00289 }
00290 
00291 QStringList KOPrefs::allEmails()
00292 {
00293   // Grab emails from the email identities
00294   QStringList lst = KOCore::self()->identityManager()->allEmails();
00295   // Add emails configured in korganizer
00296   lst += mAdditionalMails;
00297   // Add emails from the user's kaddressbook entry
00298   lst += KABC::StdAddressBook::self()->whoAmI().emails();
00299 
00300   // Warning, this list could contain duplicates.
00301   return lst;
00302 }
00303 
00304 QStringList KOPrefs::fullEmails()
00305 {
00306   QStringList fullEmails;
00307   // The user name and email from the config dialog:
00308   fullEmails << QString("%1 <%2>").arg( fullName() ).arg( email() );
00309   
00310   QStringList::Iterator it;
00311   // Grab emails from the email identities
00312   KPIM::IdentityManager *idmanager = KOCore::self()->identityManager();
00313   QStringList lst = idmanager->identities();
00314   KPIM::IdentityManager::ConstIterator it1;
00315   for ( it1 = idmanager->begin() ; it1 != idmanager->end() ; ++it1 ) {
00316     fullEmails << (*it1).fullEmailAddr();
00317   }
00318   // Add emails configured in korganizer
00319   lst = mAdditionalMails;
00320   for ( it = lst.begin(); it != lst.end(); ++it ) {
00321     fullEmails << QString("%1 <%2>").arg( fullName() ).arg( *it );
00322   }
00323   // Add emails from the user's kaddressbook entry
00324   KABC::Addressee me = KABC::StdAddressBook::self()->whoAmI();
00325   lst = me.emails();
00326   for ( it = lst.begin(); it != lst.end(); ++it ) {
00327     fullEmails << me.fullEmail( *it );
00328   }
00329 
00330   // Warning, this list could contain duplicates.
00331   return fullEmails;
00332 }
00333 
00334 bool KOPrefs::thatIsMe( const QString& _email )
00335 {
00336   if ( KOCore::self()->identityManager()->thatIsMe( _email ) )
00337     return true;
00338   // in case email contains a full name, strip it out
00339   QString email = KPIM::getEmailAddr( _email );
00340   if ( mAdditionalMails.find( email ) != mAdditionalMails.end() )
00341     return true;
00342   QStringList lst = KABC::StdAddressBook::self()->whoAmI().emails();
00343   if ( lst.find( email ) != lst.end() )
00344     return true;
00345   return false;
00346 }
KDE Logo
This file is part of the documentation for korganizer Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Mar 23 22:45:25 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003