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 TQt, and distribute the resulting executable, 00023 without including the source code for TQt in the source distribution. 00024 */ 00025 00026 #include <time.h> 00027 #include <unistd.h> 00028 00029 #include <tqdir.h> 00030 #include <tqstring.h> 00031 #include <tqfont.h> 00032 #include <tqcolor.h> 00033 #include <tqmap.h> 00034 #include <tqstringlist.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 TQColor getTextColor(const TQColor &c) 00059 { 00060 float luminance = (c.red() * 0.299) + (c.green() * 0.587) + (c.blue() * 0.114); 00061 return (luminance > 128.0) ? TQColor( 0, 0 ,0 ) : TQColor( 255, 255 ,255 ); 00062 } 00063 00064 00065 KOPrefs::KOPrefs() : 00066 KOPrefsBase() 00067 { 00068 mCategoryColors.setAutoDelete( true ); 00069 mResourceColors.setAutoDelete( true ); 00070 00071 mDefaultCategoryColor = TQColor( 151, 235, 121 ); 00072 00073 mDefaultResourceColor = TQColor();//Default is a color invalid 00074 00075 mDefaultTimeBarFont = KGlobalSettings::generalFont(); 00076 // make a large default time bar font, at least 16 points. 00077 mDefaultTimeBarFont.setPointSize( 00078 TQMAX( mDefaultTimeBarFont.pointSize() + 4, 16 ) ); 00079 00080 mDefaultMonthViewFont = KGlobalSettings::generalFont(); 00081 // make it a bit smaller 00082 mDefaultMonthViewFont.setPointSize( mDefaultMonthViewFont.pointSize() - 2 ); 00083 00084 KConfigSkeleton::setCurrentGroup( "General" ); 00085 00086 addItemPath( "Html Export File", mHtmlExportFile, 00087 TQDir::homeDirPath() + "/" + i18n( "Default export file", "calendar.html" ) ); 00088 00089 timeBarFontItem()->setDefaultValue( mDefaultTimeBarFont ); 00090 monthViewFontItem()->setDefaultValue( mDefaultMonthViewFont ); 00091 00092 // Load it now, not deep within some painting code 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 // Default should be set a bit smarter, respecting username and locale 00116 // settings for example. 00117 00118 KEMailSettings settings; 00119 TQString 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 TQString defEmail = userEmailItem()->value(); 00137 userEmailItem()->swapDefault(); 00138 00139 if ( userEmail() == defEmail ) { 00140 // No korg settings - but maybe there's a kcontrol[/kmail] setting available 00141 KEMailSettings settings; 00142 if ( !settings.getSetting( KEMailSettings::EmailAddress ).isEmpty() ) 00143 mEmailControlCenter = true; 00144 } 00145 } 00146 00147 void KOPrefs::setTimeZoneIdDefault() 00148 { 00149 TQString 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 // 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 TQValueList<TQColor> oldCategoryColors; 00180 TQStringList::Iterator it; 00181 for (it = mCustomCategories.begin();it != mCustomCategories.end();++it ) { 00182 TQColor c = config()->readColorEntry(*it, &mDefaultCategoryColor); 00183 oldCategoryColors.append( (c == TQColor(196,196,196)) ? 00184 mDefaultCategoryColor : c); 00185 } 00186 00187 // new category colors 00188 config()->setGroup("Category Colors2"); 00189 TQValueList<TQColor>::Iterator it2; 00190 for (it = mCustomCategories.begin(), it2 = oldCategoryColors.begin(); 00191 it != mCustomCategories.end(); ++it, ++it2 ) { 00192 TQColor c = config()->readColorEntry(*it, &*it2); 00193 if ( c != mDefaultCategoryColor ) 00194 setCategoryColor(*it,c); 00195 } 00196 00197 config()->setGroup( "Resources Colors" ); 00198 TQMap<TQString, TQString> map = config()->entryMap( "Resources Colors" ); 00199 00200 TQMapIterator<TQString, TQString> 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 TQDictIterator<TQColor> it(mCategoryColors); 00230 while (it.current()) { 00231 config()->writeEntry(it.currentKey(),*(it.current())); 00232 ++it; 00233 } 00234 00235 config()->setGroup( "Resources Colors" ); 00236 TQDictIterator<TQColor> 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 TQString &cat, const TQColor & color) 00264 { 00265 mCategoryColors.replace( cat, new TQColor( color ) ); 00266 } 00267 00268 TQColor *KOPrefs::categoryColor( const TQString &cat ) 00269 { 00270 TQColor *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 TQString& cat ) const 00280 { 00281 return mCategoryColors[ cat ]; 00282 } 00283 00284 void KOPrefs::setResourceColor ( const TQString &cal, const TQColor &color ) 00285 { 00286 // kdDebug(5850)<<"KOPrefs::setResourceColor: " << cal << " color: "<< 00287 // color.name()<<endl; 00288 mResourceColors.replace( cal, new TQColor( color ) ); 00289 } 00290 00291 TQColor* KOPrefs::resourceColor( const TQString &cal ) 00292 { 00293 TQColor *color=0; 00294 if( !cal.isEmpty() ) color = mResourceColors[cal]; 00295 00296 // assign default color if enabled 00297 if ( !cal.isEmpty() && !color && assignDefaultResourceColors() ) { 00298 TQColor defColor( 0x37, 0x7A, 0xBC ); 00299 if ( defaultResourceColorSeed() > 0 && defaultResourceColorSeed() - 1 < (int)defaultResourceColors().size() ) { 00300 defColor = TQColor( 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 TQString KOPrefs::fullName() 00320 { 00321 TQString tusername; 00322 if ( mEmailControlCenter ) { 00323 KEMailSettings settings; 00324 tusername = settings.getSetting( KEMailSettings::RealName ); 00325 } else { 00326 tusername = userName(); 00327 } 00328 00329 // Quote the username as it might contain commas and other quotable chars. 00330 tusername = KPIM::quoteNameIfNecessary( tusername ); 00331 00332 TQString tname, temail; 00333 KPIM::getNameAndMail( tusername, tname, temail ); // ignore return value 00334 // which is always false 00335 return tname; 00336 } 00337 00338 TQString KOPrefs::email() 00339 { 00340 if ( mEmailControlCenter ) { 00341 KEMailSettings settings; 00342 return settings.getSetting( KEMailSettings::EmailAddress ); 00343 } else { 00344 return userEmail(); 00345 } 00346 } 00347 00348 TQStringList KOPrefs::allEmails() 00349 { 00350 // Grab emails from the email identities 00351 TQStringList lst = KOCore::self()->identityManager()->allEmails(); 00352 // Add emails configured in korganizer 00353 lst += mAdditionalMails; 00354 // Add emails from the user's kaddressbook entry 00355 lst += mMyAddrBookMails; 00356 // Add the email entered as the userEmail here 00357 lst += email(); 00358 00359 // Warning, this list could contain duplicates. 00360 return lst; 00361 } 00362 00363 TQStringList KOPrefs::fullEmails() 00364 { 00365 TQStringList fullEmails; 00366 // The user name and email from the config dialog: 00367 fullEmails << TQString("%1 <%2>").arg( fullName() ).arg( email() ); 00368 00369 TQStringList::Iterator it; 00370 // Grab emails from the email identities 00371 KPIM::IdentityManager *idmanager = KOCore::self()->identityManager(); 00372 TQStringList lst = idmanager->identities(); 00373 KPIM::IdentityManager::ConstIterator it1; 00374 for ( it1 = idmanager->begin() ; it1 != idmanager->end() ; ++it1 ) { 00375 fullEmails << (*it1).fullEmailAddr(); 00376 } 00377 // Add emails configured in korganizer 00378 lst = mAdditionalMails; 00379 for ( it = lst.begin(); it != lst.end(); ++it ) { 00380 fullEmails << TQString("%1 <%2>").arg( fullName() ).arg( *it ); 00381 } 00382 // Add emails from the user's kaddressbook entry 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 // Warning, this list could contain duplicates. 00390 return fullEmails; 00391 } 00392 00393 bool KOPrefs::thatIsMe( const TQString& _email ) 00394 { 00395 // NOTE: this method is called for every created agenda view item, so we need to keep 00396 // performance in mind 00397 00398 /* identityManager()->thatIsMe() is quite expensive since it does parsing of 00399 _email in a way which is unnecessarily complex for what we can have here, 00400 so we do that ourselves. This makes sense since this 00401 00402 if ( KOCore::self()->identityManager()->thatIsMe( _email ) ) 00403 return true; 00404 */ 00405 00406 // in case email contains a full name, strip it out 00407 // the below is the simpler but slower version of the following KMime code 00408 // const TQString email = KPIM::getEmailAddress( _email ); 00409 const TQCString 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 TQString 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).primaryEmailAddress() ) 00419 return true; 00420 const TQStringList & aliases = (*it).emailAliases(); 00421 if ( aliases.find( email ) != aliases.end() ) 00422 return true; 00423 } 00424 00425 if ( mAdditionalMails.find( email ) != mAdditionalMails.end() ) 00426 return true; 00427 TQStringList lst = mMyAddrBookMails; 00428 if ( lst.find( email ) != lst.end() ) 00429 return true; 00430 return false; 00431 }