libkdepim

kpimprefs.cpp

00001 /*
00002     This file is part of libkdepim.
00003 
00004     Copyright (c) 2002 Cornelius Schumacher <schumacher@kde.org>
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library 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 GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to
00018     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019     Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #include <config.h>
00023 
00024 #include <time.h>
00025 #include <unistd.h>
00026 #include <stdlib.h>
00027 
00028 #include <qstring.h>
00029 
00030 #include <kstandarddirs.h>
00031 #include <kglobal.h>
00032 #include <kconfig.h>
00033 #include <klocale.h>
00034 #include <kdebug.h>
00035 
00036 #include "kpimprefs.h"
00037 
00038 KPimPrefs::KPimPrefs( const QString &name )
00039   : KConfigSkeleton( name )
00040 {
00041 }
00042 
00043 KPimPrefs::~KPimPrefs()
00044 {
00045 }
00046 
00047 void KPimPrefs::usrSetDefaults()
00048 {
00049   setCategoryDefaults();
00050 }
00051 
00052 void KPimPrefs::usrReadConfig()
00053 {
00054   kdDebug(5300) << "KPimPrefs::usrReadConfig()" << endl;
00055 
00056   config()->setGroup("General");
00057   mCustomCategories = config()->readListEntry( "Custom Categories" );
00058   if ( mCustomCategories.isEmpty() ) setCategoryDefaults();
00059   mCustomCategories.sort();
00060 }
00061 
00062 const QString KPimPrefs::timezone()
00063 {
00064   QString zone = "";
00065 
00066   // Read TimeZoneId from korganizerrc.
00067   KConfig korgcfg( locate( "config", "korganizerrc" ) );
00068   korgcfg.setGroup( "Time & Date" );
00069   QString tz( korgcfg.readEntry( "TimeZoneId" ) );
00070   if ( !tz.isEmpty() ) {
00071     zone = tz;
00072     kdDebug(5300) << "timezone from korganizerrc is " << zone << endl;
00073   }
00074 
00075   // If timezone not found in KOrg, use the system's default timezone.
00076   if ( zone.isEmpty() ) {
00077     char zonefilebuf[ PATH_MAX ];
00078 
00079     int len = readlink( "/etc/localtime", zonefilebuf, PATH_MAX );
00080     if ( len > 0 && len < PATH_MAX ) {
00081       zone = QString::fromLocal8Bit( zonefilebuf, len );
00082       zone = zone.mid( zone.find( "zoneinfo/" ) + 9 );
00083       kdDebug(5300) << "system timezone from /etc/localtime is " << zone
00084                     << endl;
00085     } else {
00086       tzset();
00087       zone = tzname[ 0 ];
00088       kdDebug(5300) << "system timezone from tzset() is " << zone << endl;
00089     }
00090   }
00091 
00092   return( zone );
00093 }
00094 
00095 QDateTime KPimPrefs::utcToLocalTime( const QDateTime &_dt,
00096                                      const QString &timeZoneId )
00097 {
00098   QDateTime dt(_dt);
00099 //  kdDebug() << "---   UTC: " << dt.toString() << endl;
00100 
00101   int yearCorrection = 0;
00102   // The timezone conversion only works for dates > 1970
00103   // For dates < 1970 we adjust the date to be in 1970, 
00104   // do the correction there and then re-adjust back.
00105   // Actually, we use 1971 to prevent errors around
00106   // January 1, 1970
00107   int year = dt.date().year();
00108   if (year < 1971)
00109   {
00110     yearCorrection = 1971 - year;
00111     dt = dt.addYears(yearCorrection);
00112 //    kdDebug() << "---   Adjusted UTC: " << dt.toString() << endl;
00113   }
00114   
00115   QCString origTz = getenv("TZ");
00116 
00117   setenv( "TZ", "UTC", 1 );
00118   time_t utcTime = dt.toTime_t();
00119 
00120   setenv( "TZ", timeZoneId.local8Bit(), 1 );
00121   struct tm *local = localtime( &utcTime );
00122 
00123   if ( origTz.isNull() ) {
00124     unsetenv( "TZ" );
00125   } else {
00126     setenv( "TZ", origTz, 1 );
00127   }
00128   tzset();
00129 
00130   QDateTime result( QDate( local->tm_year + 1900 - yearCorrection,
00131                            local->tm_mon + 1, local->tm_mday ),
00132                     QTime( local->tm_hour, local->tm_min, local->tm_sec ) );
00133 
00134 //  kdDebug() << "--- LOCAL: " << result.toString() << endl;
00135   return result;
00136 }
00137 
00138 QDateTime KPimPrefs::localTimeToUtc( const QDateTime &_dt,
00139                                      const QString &timeZoneId )
00140 {
00141   QDateTime dt(_dt);
00142 //  kdDebug() << "--- LOCAL: " << dt.toString() << endl;
00143 
00144   int yearCorrection = 0;
00145   // The timezone conversion only works for dates > 1970
00146   // For dates < 1970 we adjust the date to be in 1970, 
00147   // do the correction there and then re-adjust back.
00148   // Actually, we use 1971 to prevent errors around
00149   // January 1, 1970
00150   
00151   int year = dt.date().year();
00152   if (year < 1971)
00153   {
00154     yearCorrection = 1971 - year;
00155     dt = dt.addYears(yearCorrection);
00156 //    kdDebug() << "---   Adjusted LOCAL: " << dt.toString() << endl;
00157   }
00158 
00159   QCString origTz = getenv("TZ");
00160 
00161   setenv( "TZ", timeZoneId.local8Bit(), 1 );
00162   time_t localTime = dt.toTime_t();
00163 
00164   setenv( "TZ", "UTC", 1 );
00165   struct tm *utc = gmtime( &localTime );
00166 
00167   if ( origTz.isNull() ) {
00168     unsetenv( "TZ" );
00169   } else {
00170     setenv( "TZ", origTz, 1 );
00171   }
00172   tzset();
00173 
00174   QDateTime result( QDate( utc->tm_year + 1900 - yearCorrection,
00175                            utc->tm_mon + 1, utc->tm_mday ),
00176                     QTime( utc->tm_hour, utc->tm_min, utc->tm_sec ) );
00177 
00178 //  kdDebug() << "---   UTC: " << result.toString() << endl;
00179 
00180   return result;
00181 }
00182 
00183 void KPimPrefs::usrWriteConfig()
00184 {
00185   config()->setGroup( "General" );
00186   config()->writeEntry( "Custom Categories", mCustomCategories );
00187 }
KDE Home | KDE Accessibility Home | Description of Access Keys