00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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 <libkmime/kmime_header_parsing.h>
00046
00047 #include "koprefs.h"
00048 #include <libkpimidentities/identitymanager.h>
00049 #include <libkpimidentities/identity.h>
00050 #include <libemailfunctions/email.h>
00051 #include <kabc/stdaddressbook.h>
00052 #include <ktimezones.h>
00053 #include "kocore.h"
00054
00055 KOPrefs *KOPrefs::mInstance = 0;
00056 static KStaticDeleter<KOPrefs> insd;
00057
00058 QColor getTextColor(const QColor &c)
00059 {
00060 float luminance = (c.red() * 0.299) + (c.green() * 0.587) + (c.blue() * 0.114);
00061 return (luminance > 128.0) ? QColor( 0, 0 ,0 ) : QColor( 255, 255 ,255 );
00062 }
00063
00064
00065 KOPrefs::KOPrefs() :
00066 KOPrefsBase()
00067 {
00068 mCategoryColors.setAutoDelete( true );
00069 mResourceColors.setAutoDelete( true );
00070
00071 mDefaultCategoryColor = QColor( 151, 235, 121 );
00072
00073 mDefaultResourceColor = QColor();
00074
00075 mDefaultTimeBarFont = KGlobalSettings::generalFont();
00076
00077 mDefaultTimeBarFont.setPointSize(
00078 QMAX( mDefaultTimeBarFont.pointSize() + 4, 16 ) );
00079
00080 mDefaultMonthViewFont = KGlobalSettings::generalFont();
00081
00082 mDefaultMonthViewFont.setPointSize( mDefaultMonthViewFont.pointSize() - 2 );
00083
00084 KConfigSkeleton::setCurrentGroup( "General" );
00085
00086 addItemPath( "Html Export File", mHtmlExportFile,
00087 QDir::homeDirPath() + "/" + i18n( "Default export file", "calendar.html" ) );
00088
00089 timeBarFontItem()->setDefaultValue( mDefaultTimeBarFont );
00090 monthViewFontItem()->setDefaultValue( mDefaultMonthViewFont );
00091
00092
00093 mMyAddrBookMails = KABC::StdAddressBook::self()->whoAmI().emails();
00094 }
00095
00096
00097 KOPrefs::~KOPrefs()
00098 {
00099 kdDebug(5850) << "KOPrefs::~KOPrefs()" << endl;
00100 }
00101
00102
00103 KOPrefs *KOPrefs::instance()
00104 {
00105 if ( !mInstance ) {
00106 insd.setObject( mInstance, new KOPrefs() );
00107 mInstance->readConfig();
00108 }
00109
00110 return mInstance;
00111 }
00112
00113 void KOPrefs::usrSetDefaults()
00114 {
00115
00116
00117
00118 KEMailSettings settings;
00119 QString tmp = settings.getSetting(KEMailSettings::RealName);
00120 if ( !tmp.isEmpty() ) setUserName( tmp );
00121 tmp = settings.getSetting(KEMailSettings::EmailAddress);
00122 if ( !tmp.isEmpty() ) setUserEmail( tmp );
00123 fillMailDefaults();
00124
00125 mTimeBarFont = mDefaultTimeBarFont;
00126 mMonthViewFont = mDefaultMonthViewFont;
00127
00128 setTimeZoneIdDefault();
00129
00130 KPimPrefs::usrSetDefaults();
00131 }
00132
00133 void KOPrefs::fillMailDefaults()
00134 {
00135 userEmailItem()->swapDefault();
00136 QString defEmail = userEmailItem()->value();
00137 userEmailItem()->swapDefault();
00138
00139 if ( userEmail() == defEmail ) {
00140
00141 KEMailSettings settings;
00142 if ( !settings.getSetting( KEMailSettings::EmailAddress ).isEmpty() )
00143 mEmailControlCenter = true;
00144 }
00145 }
00146
00147 void KOPrefs::setTimeZoneIdDefault()
00148 {
00149 QString zone;
00150
00151 zone = KTimezones().local()->name();
00152
00153 kdDebug() << "----- time zone: " << zone << endl;
00154
00155 mTimeZoneId = zone;
00156 }
00157
00158 void KOPrefs::setCategoryDefaults()
00159 {
00160 mCustomCategories.clear();
00161
00162 mCustomCategories << i18n("Appointment") << i18n("Business")
00163 << i18n("Meeting") << i18n("Phone Call") << i18n("Education")
00164 << i18n("Holiday") << i18n("Vacation") << i18n("Special Occasion")
00165 << i18n("Personal") << i18n("Travel") << i18n("Miscellaneous")
00166 << i18n("Birthday");
00167 }
00168
00169
00170 void KOPrefs::usrReadConfig()
00171 {
00172 config()->setGroup("General");
00173 mCustomCategories = config()->readListEntry("Custom Categories");
00174 if (mCustomCategories.isEmpty()) setCategoryDefaults();
00175
00176
00177
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
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 QColor c = config()->readColorEntry(*it, &*it2);
00193 if ( c != mDefaultCategoryColor )
00194 setCategoryColor(*it,c);
00195 }
00196
00197 config()->setGroup( "Resources Colors" );
00198 QMap<QString, QString> map = config()->entryMap( "Resources Colors" );
00199
00200 QMapIterator<QString, QString> it3;
00201 for( it3 = map.begin(); it3 != map.end(); ++it3 ) {
00202 kdDebug(5850)<< "KOPrefs::usrReadConfig: key: " << it3.key() << " value: "
00203 << it3.data()<<endl;
00204 setResourceColor( it3.key(), config()->readColorEntry( it3.key(),
00205 &mDefaultResourceColor ) );
00206 }
00207
00208
00209 if (mTimeZoneId.isEmpty()) {
00210 setTimeZoneIdDefault();
00211 }
00212
00213 #if 0
00214 config()->setGroup("FreeBusy");
00215 if( mRememberRetrievePw )
00216 mRetrievePassword = KStringHandler::obscure( config()->readEntry( "Retrieve Server Password" ) );
00217 #endif
00218 KPimPrefs::usrReadConfig();
00219 fillMailDefaults();
00220 }
00221
00222
00223 void KOPrefs::usrWriteConfig()
00224 {
00225 config()->setGroup("General");
00226 config()->writeEntry("Custom Categories",mCustomCategories);
00227
00228 config()->setGroup("Category Colors2");
00229 QDictIterator<QColor> it(mCategoryColors);
00230 while (it.current()) {
00231 config()->writeEntry(it.currentKey(),*(it.current()));
00232 ++it;
00233 }
00234
00235 config()->setGroup( "Resources Colors" );
00236 QDictIterator<QColor> it2( mResourceColors );
00237 while( it2.current() ) {
00238 config()->writeEntry( it2.currentKey(), *( it2.current() ) );
00239 ++it2;
00240 }
00241
00242 if( !mFreeBusyPublishSavePassword ) {
00243 KConfigSkeleton::ItemPassword *i = freeBusyPublishPasswordItem();
00244 i->setValue( "" );
00245 i->writeConfig( config() );
00246 }
00247 if( !mFreeBusyRetrieveSavePassword ) {
00248 KConfigSkeleton::ItemPassword *i = freeBusyRetrievePasswordItem();
00249 i->setValue( "" );
00250 i->writeConfig( config() );
00251 }
00252
00253 #if 0
00254 if( mRememberRetrievePw )
00255 config()->writeEntry( "Retrieve Server Password", KStringHandler::obscure( mRetrievePassword ) );
00256 else
00257 config()->deleteEntry( "Retrieve Server Password" );
00258 #endif
00259
00260 KPimPrefs::usrWriteConfig();
00261 }
00262
00263 void KOPrefs::setCategoryColor( const QString &cat, const QColor & color)
00264 {
00265 mCategoryColors.replace( cat, new QColor( color ) );
00266 }
00267
00268 QColor *KOPrefs::categoryColor( const QString &cat )
00269 {
00270 QColor *color = 0;
00271
00272 if ( !cat.isEmpty() ) color = mCategoryColors[ cat ];
00273
00274 if ( color ) return color;
00275 else return &mDefaultCategoryColor;
00276 }
00277
00278
00279 bool KOPrefs::hasCategoryColor( const QString& cat ) const
00280 {
00281 return mCategoryColors[ cat ];
00282 }
00283
00284 void KOPrefs::setResourceColor ( const QString &cal, const QColor &color )
00285 {
00286 kdDebug(5850)<<"KOPrefs::setResourceColor: " << cal << " color: "<<
00287 color.name()<<endl;
00288 mResourceColors.replace( cal, new QColor( color ) );
00289 }
00290
00291 QColor* KOPrefs::resourceColor( const QString &cal )
00292 {
00293 QColor *color=0;
00294 if( !cal.isEmpty() ) color = mResourceColors[cal];
00295
00296
00297 if ( !cal.isEmpty() && !color && assignDefaultResourceColors() ) {
00298 QColor defColor( 0x37, 0x7A, 0xBC );
00299 if ( defaultResourceColorSeed() > 0 && defaultResourceColorSeed() - 1 < (int)defaultResourceColors().size() ) {
00300 defColor = QColor( defaultResourceColors()[defaultResourceColorSeed()-1] );
00301 } else {
00302 int h, s, v;
00303 defColor.getHsv( h, s, v );
00304 h = ( defaultResourceColorSeed() % 12 ) * 30;
00305 s -= s * ( (defaultResourceColorSeed() / 12) % 2 ) * 0.5;
00306 defColor.setHsv( h, s, v );
00307 }
00308 setDefaultResourceColorSeed( defaultResourceColorSeed() + 1 );
00309 setResourceColor( cal, defColor );
00310 color = mResourceColors[cal];
00311 }
00312
00313 if (color && color->isValid() )
00314 return color;
00315 else
00316 return &mDefaultResourceColor;
00317 }
00318
00319 QString KOPrefs::fullName()
00320 {
00321 QString tusername;
00322 if ( mEmailControlCenter ) {
00323 KEMailSettings settings;
00324 tusername = settings.getSetting( KEMailSettings::RealName );
00325 } else {
00326 tusername = userName();
00327 }
00328
00329
00330 tusername = KPIM::quoteNameIfNecessary( tusername );
00331
00332 QString tname, temail;
00333 KPIM::getNameAndMail( tusername, tname, temail );
00334
00335 return tname;
00336 }
00337
00338 QString KOPrefs::email()
00339 {
00340 if ( mEmailControlCenter ) {
00341 KEMailSettings settings;
00342 return settings.getSetting( KEMailSettings::EmailAddress );
00343 } else {
00344 return userEmail();
00345 }
00346 }
00347
00348 QStringList KOPrefs::allEmails()
00349 {
00350
00351 QStringList lst = KOCore::self()->identityManager()->allEmails();
00352
00353 lst += mAdditionalMails;
00354
00355 lst += mMyAddrBookMails;
00356
00357 lst += email();
00358
00359
00360 return lst;
00361 }
00362
00363 QStringList KOPrefs::fullEmails()
00364 {
00365 QStringList fullEmails;
00366
00367 fullEmails << QString("%1 <%2>").arg( fullName() ).arg( email() );
00368
00369 QStringList::Iterator it;
00370
00371 KPIM::IdentityManager *idmanager = KOCore::self()->identityManager();
00372 QStringList lst = idmanager->identities();
00373 KPIM::IdentityManager::ConstIterator it1;
00374 for ( it1 = idmanager->begin() ; it1 != idmanager->end() ; ++it1 ) {
00375 fullEmails << (*it1).fullEmailAddr();
00376 }
00377
00378 lst = mAdditionalMails;
00379 for ( it = lst.begin(); it != lst.end(); ++it ) {
00380 fullEmails << QString("%1 <%2>").arg( fullName() ).arg( *it );
00381 }
00382
00383 KABC::Addressee me = KABC::StdAddressBook::self()->whoAmI();
00384 lst = me.emails();
00385 for ( it = lst.begin(); it != lst.end(); ++it ) {
00386 fullEmails << me.fullEmail( *it );
00387 }
00388
00389
00390 return fullEmails;
00391 }
00392
00393 bool KOPrefs::thatIsMe( const QString& _email )
00394 {
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409 const QCString tmp = _email.utf8();
00410 const char *cursor = tmp.data();
00411 const char *end = tmp.data() + tmp.length();
00412 KMime::Types::Mailbox mbox;
00413 KMime::HeaderParsing::parseMailbox( cursor, end, mbox );
00414 const QString email = mbox.addrSpec.asString();
00415
00416 for ( KPIM::IdentityManager::ConstIterator it = KOCore::self()->identityManager()->begin();
00417 it != KOCore::self()->identityManager()->end(); ++it ) {
00418 if ( email == (*it).emailAddr() )
00419 return true;
00420 }
00421
00422 if ( mAdditionalMails.find( email ) != mAdditionalMails.end() )
00423 return true;
00424 QStringList lst = mMyAddrBookMails;
00425 if ( lst.find( email ) != lst.end() )
00426 return true;
00427 return false;
00428 }