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 
00045 KOPrefs *KOPrefs::mInstance = 0;
00046 static KStaticDeleter<KOPrefs> insd;
00047 
00048 QColor getTextColor(const QColor &c)
00049 {
00050   float luminance = (c.red() * 0.299) + (c.green() * 0.587) + (c.blue() * 0.114);
00051   return (luminance > 128.0) ? QColor( 0, 0 ,0 ) : QColor( 255, 255 ,255 );
00052 }
00053 
00054 
00055 KOPrefs::KOPrefs() :
00056   KOPrefsBase()
00057 {
00058   mCategoryColors.setAutoDelete(true);
00059 
00060   mDefaultCategoryColor = QColor(151, 235, 121);
00061 
00062   mDefaultMonthViewFont = KGlobalSettings::generalFont();
00063   // make it a bit smaller
00064   mDefaultMonthViewFont.setPointSize(mDefaultMonthViewFont.pointSize()-2);
00065 
00066   KConfigSkeleton::setCurrentGroup("General");
00067 
00068   addItemPath("Html Export File",mHtmlExportFile,
00069       QDir::homeDirPath() + "/" + i18n("Default export file", "calendar.html"));
00070 
00071   monthViewFontItem()->setDefaultValue( mDefaultMonthViewFont );
00072   eventColorItem()->setDefaultValue( mDefaultCategoryColor );
00073 }
00074 
00075 
00076 KOPrefs::~KOPrefs()
00077 {
00078   kdDebug(5850) << "KOPrefs::~KOPrefs()" << endl;
00079 }
00080 
00081 
00082 KOPrefs *KOPrefs::instance()
00083 {
00084   if ( !mInstance ) {
00085     insd.setObject( mInstance, new KOPrefs() );
00086     mInstance->readConfig();
00087   }
00088 
00089   return mInstance;
00090 }
00091 
00092 void KOPrefs::usrSetDefaults()
00093 {
00094   // Default should be set a bit smarter, respecting username and locale
00095   // settings for example.
00096 
00097   KEMailSettings settings;
00098   mName = settings.getSetting(KEMailSettings::RealName);
00099   mEmail = settings.getSetting(KEMailSettings::RealName);
00100   fillMailDefaults();
00101 
00102   mMonthViewFont = mDefaultMonthViewFont;
00103 
00104   setTimeZoneIdDefault();
00105 
00106   mRememberPublishPw = false;
00107   mRememberRetrievePw = false;
00108 
00109   KPimPrefs::usrSetDefaults();
00110 }
00111 
00112 void KOPrefs::fillMailDefaults()
00113 {
00114   if (mName.isEmpty()) mName = i18n("Anonymous");
00115   if (mEmail.isEmpty()) mEmail = i18n("nobody@nowhere");
00116 }
00117 
00118 void KOPrefs::setTimeZoneIdDefault()
00119 {
00120   QString zone;
00121 
00122   char zonefilebuf[100];
00123   int len = readlink("/etc/localtime",zonefilebuf,100);
00124   if (len > 0 && len < 100) {
00125     zonefilebuf[len] = '\0';
00126     zone = zonefilebuf;
00127     zone = zone.mid(zone.find("zoneinfo/") + 9);
00128   } else {
00129     tzset();
00130     zone = tzname[0];
00131   }
00132 
00133   kdDebug () << "----- time zone: " << zone << endl;
00134 
00135   mTimeZoneId = zone;
00136 }
00137 
00138 void KOPrefs::setCategoryDefaults()
00139 {
00140   mCustomCategories.clear();
00141 
00142   mCustomCategories << i18n("Appointment") << i18n("Business")
00143       << i18n("Meeting") << i18n("Phone Call") << i18n("Education")
00144       << i18n("Holiday") << i18n("Vacation") << i18n("Special Occasion")
00145       << i18n("Personal") << i18n("Travel") << i18n("Miscellaneous")
00146       << i18n("Birthday");
00147 
00148   QStringList::Iterator it;
00149   for (it = mCustomCategories.begin();it != mCustomCategories.end();++it ) {
00150     setCategoryColor(*it,mDefaultCategoryColor);
00151   }
00152 }
00153 
00154 
00155 void KOPrefs::usrReadConfig()
00156 {
00157   config()->setGroup("General");
00158   mCustomCategories = config()->readListEntry("Custom Categories");
00159   if (mCustomCategories.isEmpty()) setCategoryDefaults();
00160 
00161   config()->setGroup("Personal Settings");
00162   mName = config()->readEntry("user_name");
00163   mEmail = config()->readEntry("user_email");
00164   fillMailDefaults();
00165 
00166   // old category colors, ignore if they have the old default
00167   // should be removed a few versions after 3.2...
00168   config()->setGroup("Category Colors");
00169   QValueList<QColor> oldCategoryColors;
00170   QStringList::Iterator it;
00171   for (it = mCustomCategories.begin();it != mCustomCategories.end();++it ) {
00172     QColor c = config()->readColorEntry(*it, &mDefaultCategoryColor);
00173     oldCategoryColors.append( (c == QColor(196,196,196)) ?
00174                               mDefaultCategoryColor : c);
00175   }
00176 
00177   // new category colors
00178   config()->setGroup("Category Colors2");
00179   QValueList<QColor>::Iterator it2;
00180   for (it = mCustomCategories.begin(), it2 = oldCategoryColors.begin();
00181        it != mCustomCategories.end(); ++it, ++it2 ) {
00182     setCategoryColor(*it,config()->readColorEntry(*it, &*it2));
00183   }
00184 
00185   if (mTimeZoneId.isEmpty()) {
00186     setTimeZoneIdDefault();
00187   }
00188 
00189   config()->setGroup("Groupware");
00190   if( mRememberPublishPw )
00191     mPublishPassword = KStringHandler::obscure( config()->readEntry( "Publish Server Password" ) );
00192   if( mRememberRetrievePw )
00193     mRetrievePassword = KStringHandler::obscure( config()->readEntry( "Retrieve Server Password" ) );
00194 
00195   KPimPrefs::usrReadConfig();
00196 }
00197 
00198 
00199 void KOPrefs::usrWriteConfig()
00200 {
00201   config()->setGroup("General");
00202   config()->writeEntry("Custom Categories",mCustomCategories);
00203 
00204   config()->setGroup("Personal Settings");
00205   config()->writeEntry("user_name",mName);
00206   config()->writeEntry("user_email",mEmail);
00207 
00208   config()->setGroup("Category Colors2");
00209   QDictIterator<QColor> it(mCategoryColors);
00210   while (it.current()) {
00211     config()->writeEntry(it.currentKey(),*(it.current()));
00212     ++it;
00213   }
00214 
00215   config()->setGroup( "Groupware" );
00216   if( mRememberPublishPw )
00217     config()->writeEntry( "Publish Server Password", KStringHandler::obscure( mPublishPassword ) );
00218   else
00219     config()->deleteEntry( "Publish Server Password" );
00220   if( mRememberRetrievePw )
00221     config()->writeEntry( "Retrieve Server Password", KStringHandler::obscure( mRetrievePassword ) );
00222   else
00223     config()->deleteEntry( "Retrieve Server Password" );
00224 
00225   KPimPrefs::usrWriteConfig();
00226 }
00227 
00228 void KOPrefs::setCategoryColor(QString cat,const QColor & color)
00229 {
00230   mCategoryColors.replace(cat,new QColor(color));
00231 }
00232 
00233 QColor *KOPrefs::categoryColor(QString cat)
00234 {
00235   QColor *color = 0;
00236 
00237   if (!cat.isEmpty()) color = mCategoryColors[cat];
00238 
00239   if (color) return color;
00240   else return &mDefaultCategoryColor;
00241 }
00242 
00243 void KOPrefs::setFullName(const QString &name)
00244 {
00245   mName = name;
00246 }
00247 
00248 void KOPrefs::setEmail(const QString &email)
00249 {
00250   mEmail = email;
00251 }
00252 
00253 QString KOPrefs::fullName()
00254 {
00255   if (mEmailControlCenter) {
00256     KEMailSettings settings;
00257     return settings.getSetting(KEMailSettings::RealName);
00258   } else {
00259     return mName;
00260   }
00261 }
00262 
00263 QString KOPrefs::email()
00264 {
00265   if (mEmailControlCenter) {
00266     KEMailSettings settings;
00267     return settings.getSetting(KEMailSettings::EmailAddress);
00268   } else {
00269     return mEmail;
00270   }
00271 }
KDE Logo
This file is part of the documentation for korganizer Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:38:30 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003