34 #include <tqstringlist.h>
36 #include <tdeglobalsettings.h>
37 #include <tdeglobal.h>
38 #include <tdeconfig.h>
39 #include <tdelocale.h>
41 #include <tdeemailsettings.h>
42 #include <kstaticdeleter.h>
43 #include <kstringhandler.h>
45 #include <libkmime/kmime_header_parsing.h>
48 #include <libkpimidentities/identitymanager.h>
49 #include <libkpimidentities/identity.h>
50 #include <libemailfunctions/email.h>
51 #include <tdeabc/stdaddressbook.h>
52 #include <ktimezones.h>
55 KOPrefs *KOPrefs::mInstance = 0;
56 static KStaticDeleter<KOPrefs> insd;
58 TQColor getTextColor(
const TQColor &c)
60 float luminance = (c.red() * 0.299) + (c.green() * 0.587) + (c.blue() * 0.114);
61 return (luminance > 128.0) ? TQColor( 0, 0 ,0 ) : TQColor( 255, 255 ,255 );
68 mCategoryColors.setAutoDelete(
true );
69 mResourceColors.setAutoDelete(
true );
71 mDefaultCategoryColor = TQColor( 151, 235, 121 );
73 mDefaultResourceColor = TQColor();
75 mDefaultTimeBarFont = TDEGlobalSettings::generalFont();
77 mDefaultTimeBarFont.setPointSize(
78 TQMAX( mDefaultTimeBarFont.pointSize() + 4, 16 ) );
80 mDefaultMonthViewFont = TDEGlobalSettings::generalFont();
82 mDefaultMonthViewFont.setPointSize( mDefaultMonthViewFont.pointSize() - 2 );
84 TDEConfigSkeleton::setCurrentGroup(
"General" );
86 addItemPath(
"Html Export File", mHtmlExportFile,
87 TQDir::homeDirPath() +
"/" + i18n(
"Default export file",
"calendar.html" ) );
89 timeBarFontItem()->setDefaultValue( mDefaultTimeBarFont );
90 monthViewFontItem()->setDefaultValue( mDefaultMonthViewFont );
93 mMyAddrBookMails = TDEABC::StdAddressBook::self()->whoAmI().emails();
99 kdDebug(5850) <<
"KOPrefs::~KOPrefs()" << endl;
103 KOPrefs *KOPrefs::instance()
106 insd.setObject( mInstance,
new KOPrefs() );
107 mInstance->readConfig();
113 void KOPrefs::usrSetDefaults()
118 KEMailSettings settings;
119 TQString tmp = settings.getSetting(KEMailSettings::RealName);
120 if ( !tmp.isEmpty() ) setUserName( tmp );
121 tmp = settings.getSetting(KEMailSettings::EmailAddress);
122 if ( !tmp.isEmpty() ) setUserEmail( tmp );
125 mTimeBarFont = mDefaultTimeBarFont;
126 mMonthViewFont = mDefaultMonthViewFont;
128 setTimeZoneIdDefault();
130 KPimPrefs::usrSetDefaults();
133 void KOPrefs::fillMailDefaults()
135 userEmailItem()->swapDefault();
136 TQString defEmail = userEmailItem()->value();
137 userEmailItem()->swapDefault();
139 if ( userEmail() == defEmail ) {
141 KEMailSettings settings;
142 if ( !settings.getSetting( KEMailSettings::EmailAddress ).isEmpty() )
143 mEmailControlCenter =
true;
147 void KOPrefs::setTimeZoneIdDefault()
151 zone = KTimezones().local()->name();
153 kdDebug() <<
"----- time zone: " << zone << endl;
158 void KOPrefs::setCategoryDefaults()
160 mCustomCategories.clear();
162 mCustomCategories << i18n(
"Appointment") << i18n(
"Business")
163 << i18n(
"Meeting") << i18n(
"Phone Call") << i18n(
"Education")
164 << i18n(
"Holiday") << i18n(
"Vacation") << i18n(
"Special Occasion")
165 << i18n(
"Personal") << i18n(
"Travel") << i18n(
"Miscellaneous")
170 void KOPrefs::usrReadConfig()
172 config()->setGroup(
"General");
173 mCustomCategories = config()->readListEntry(
"Custom Categories");
174 if (mCustomCategories.isEmpty()) setCategoryDefaults();
178 config()->setGroup(
"Category Colors");
179 TQValueList<TQColor> oldCategoryColors;
180 TQStringList::Iterator it;
181 for (it = mCustomCategories.begin();it != mCustomCategories.end();++it ) {
182 TQColor c = config()->readColorEntry(*it, &mDefaultCategoryColor);
183 oldCategoryColors.append( (c == TQColor(196,196,196)) ?
184 mDefaultCategoryColor : c);
188 config()->setGroup(
"Category Colors2");
189 TQValueList<TQColor>::Iterator it2;
190 for (it = mCustomCategories.begin(), it2 = oldCategoryColors.begin();
191 it != mCustomCategories.end(); ++it, ++it2 ) {
192 TQColor c = config()->readColorEntry(*it, &*it2);
193 if ( c != mDefaultCategoryColor )
194 setCategoryColor(*it,c);
197 config()->setGroup(
"Resources Colors" );
198 TQMap<TQString, TQString> map = config()->entryMap(
"Resources Colors" );
200 TQMapIterator<TQString, TQString> it3;
201 for( it3 = map.begin(); it3 != map.end(); ++it3 ) {
204 setResourceColor( it3.key(), config()->readColorEntry( it3.key(),
205 &mDefaultResourceColor ) );
209 if (mTimeZoneId.isEmpty()) {
210 setTimeZoneIdDefault();
214 config()->setGroup(
"FreeBusy");
215 if( mRememberRetrievePw )
216 mRetrievePassword = KStringHandler::obscure( config()->readEntry(
"Retrieve Server Password" ) );
218 KPimPrefs::usrReadConfig();
223 void KOPrefs::usrWriteConfig()
225 config()->setGroup(
"General");
226 config()->writeEntry(
"Custom Categories",mCustomCategories);
228 config()->setGroup(
"Category Colors2");
229 TQDictIterator<TQColor> it(mCategoryColors);
230 while (it.current()) {
231 config()->writeEntry(it.currentKey(),*(it.current()));
235 config()->setGroup(
"Resources Colors" );
236 TQDictIterator<TQColor> it2( mResourceColors );
237 while( it2.current() ) {
238 config()->writeEntry( it2.currentKey(), *( it2.current() ) );
242 if( !mFreeBusyPublishSavePassword ) {
243 TDEConfigSkeleton::ItemPassword *i = freeBusyPublishPasswordItem();
245 i->writeConfig( config() );
247 if( !mFreeBusyRetrieveSavePassword ) {
248 TDEConfigSkeleton::ItemPassword *i = freeBusyRetrievePasswordItem();
250 i->writeConfig( config() );
254 if( mRememberRetrievePw )
255 config()->writeEntry(
"Retrieve Server Password", KStringHandler::obscure( mRetrievePassword ) );
257 config()->deleteEntry(
"Retrieve Server Password" );
260 KPimPrefs::usrWriteConfig();
263 void KOPrefs::setCategoryColor(
const TQString &cat,
const TQColor & color)
265 mCategoryColors.replace( cat,
new TQColor( color ) );
268 TQColor *KOPrefs::categoryColor(
const TQString &cat )
272 if ( !cat.isEmpty() ) color = mCategoryColors[ cat ];
274 if ( color )
return color;
275 else return &mDefaultCategoryColor;
279 bool KOPrefs::hasCategoryColor(
const TQString& cat )
const
281 return mCategoryColors[ cat ];
284 void KOPrefs::setResourceColor (
const TQString &cal,
const TQColor &color )
288 mResourceColors.replace( cal,
new TQColor( color ) );
291 TQColor* KOPrefs::resourceColor(
const TQString &cal )
294 if( !cal.isEmpty() ) color = mResourceColors[cal];
297 if ( !cal.isEmpty() && !color && assignDefaultResourceColors() ) {
298 TQColor defColor( 0x37, 0x7A, 0xBC );
299 if ( defaultResourceColorSeed() > 0 && defaultResourceColorSeed() - 1 < (
int)defaultResourceColors().size() ) {
300 defColor = TQColor( defaultResourceColors()[defaultResourceColorSeed()-1] );
303 defColor.getHsv( &h, &s, &v );
304 h = ( defaultResourceColorSeed() % 12 ) * 30;
305 s -= s * ( (defaultResourceColorSeed() / 12) % 2 ) * 0.5;
306 defColor.setHsv( h, s, v );
308 setDefaultResourceColorSeed( defaultResourceColorSeed() + 1 );
309 setResourceColor( cal, defColor );
310 color = mResourceColors[cal];
313 if (color && color->isValid() )
316 return &mDefaultResourceColor;
319 TQString KOPrefs::fullName()
322 if ( mEmailControlCenter ) {
323 KEMailSettings settings;
324 tusername = settings.getSetting( KEMailSettings::RealName );
326 tusername = userName();
330 tusername = KPIM::quoteNameIfNecessary( tusername );
332 TQString tname, temail;
333 KPIM::getNameAndMail( tusername, tname, temail );
338 TQString KOPrefs::email()
340 if ( mEmailControlCenter ) {
341 KEMailSettings settings;
342 return settings.getSetting( KEMailSettings::EmailAddress );
348 TQStringList KOPrefs::allEmails()
351 TQStringList lst = KOCore::self()->identityManager()->allEmails();
353 lst += mAdditionalMails;
355 lst += mMyAddrBookMails;
363 TQStringList KOPrefs::fullEmails()
365 TQStringList fullEmails;
367 fullEmails << TQString(
"%1 <%2>").arg( fullName() ).arg( email() );
369 TQStringList::Iterator it;
371 KPIM::IdentityManager *idmanager = KOCore::self()->identityManager();
372 TQStringList lst = idmanager->identities();
373 KPIM::IdentityManager::ConstIterator it1;
374 for ( it1 = idmanager->begin() ; it1 != idmanager->end() ; ++it1 ) {
375 fullEmails << (*it1).fullEmailAddr();
378 lst = mAdditionalMails;
379 for ( it = lst.begin(); it != lst.end(); ++it ) {
380 fullEmails << TQString(
"%1 <%2>").arg( fullName() ).arg( *it );
383 TDEABC::Addressee me = TDEABC::StdAddressBook::self()->whoAmI();
385 for ( it = lst.begin(); it != lst.end(); ++it ) {
386 fullEmails << me.fullEmail( *it );
393 bool KOPrefs::thatIsMe(
const TQString& _email )
409 const TQCString tmp = _email.utf8();
410 const char *cursor = tmp.data();
411 const char *end = tmp.data() + tmp.length();
412 KMime::Types::Mailbox mbox;
413 KMime::HeaderParsing::parseMailbox( cursor, end, mbox );
414 const TQString email = mbox.addrSpec.asString();
416 for ( KPIM::IdentityManager::ConstIterator it = KOCore::self()->identityManager()->begin();
417 it != KOCore::self()->identityManager()->end(); ++it ) {
418 if ( email == (*it).primaryEmailAddress() )
420 const TQStringList & aliases = (*it).emailAliases();
421 if ( aliases.find( email ) != aliases.end() )
425 if ( mAdditionalMails.find( email ) != mAdditionalMails.end() )
427 TQStringList lst = mMyAddrBookMails;
428 if ( lst.find( email ) != lst.end() )