libkdepim Library API Documentation

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., 59 Temple Place - Suite 330,
00019     Boston, MA 02111-1307, 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 }
00060 
00061 const QString KPimPrefs::timezone()
00062 {
00063   QString zone = "";
00064 
00065   // Read TimeZoneId from korganizerrc.
00066   KConfig korgcfg( locate( "config", "korganizerrc" ) );
00067   korgcfg.setGroup( "Time & Date" );
00068   QString tz( korgcfg.readEntry( "TimeZoneId" ) );
00069   if ( !tz.isEmpty() ) {
00070     zone = tz;
00071     kdDebug(5300) << "timezone from korganizerrc is " << zone << endl;
00072   }
00073 
00074   // If timezone not found in KOrg, use the system's default timezone.
00075   if ( zone.isEmpty() ) {
00076     char zonefilebuf[ PATH_MAX ];
00077 
00078     int len = readlink( "/etc/localtime", zonefilebuf, PATH_MAX );
00079     if ( len > 0 && len < PATH_MAX ) {
00080       zone = QString::fromLocal8Bit( zonefilebuf, len );
00081       zone = zone.mid( zone.find( "zoneinfo/" ) + 9 );
00082       kdDebug(5300) << "system timezone from /etc/localtime is " << zone
00083                     << endl;
00084     } else {
00085       tzset();
00086       zone = tzname[ 0 ];
00087       kdDebug(5300) << "system timezone from tzset() is " << zone << endl;
00088     }
00089   }
00090 
00091   return( zone );
00092 }
00093 
00094 QDateTime KPimPrefs::utcToLocalTime( const QDateTime &_dt,
00095                                      const QString &timeZoneId )
00096 {
00097   QDateTime dt(_dt);
00098 //  kdDebug() << "---   UTC: " << dt.toString() << endl;
00099 
00100   int yearCorrection = 0;
00101   // The timezone conversion only works for dates > 1970
00102   // For dates < 1970 we adjust the date to be in 1970, 
00103   // do the correction there and then re-adjust back.
00104   // Actually, we use 1971 to prevent errors around
00105   // January 1, 1970
00106   int year = dt.date().year();
00107   if (year < 1971)
00108   {
00109     yearCorrection = 1971 - year;
00110     dt = dt.addYears(yearCorrection);
00111 //    kdDebug() << "---   Adjusted UTC: " << dt.toString() << endl;
00112   }
00113   
00114   QCString origTz = getenv("TZ");
00115 
00116   setenv( "TZ", "UTC", 1 );
00117   time_t utcTime = dt.toTime_t();
00118 
00119   setenv( "TZ", timeZoneId.local8Bit(), 1 );
00120   struct tm *local = localtime( &utcTime );
00121 
00122   if ( origTz.isNull() ) {
00123     unsetenv( "TZ" );
00124   } else {
00125     setenv( "TZ", origTz, 1 );
00126   }
00127   tzset();
00128 
00129   QDateTime result( QDate( local->tm_year + 1900 - yearCorrection,
00130                            local->tm_mon + 1, local->tm_mday ),
00131                     QTime( local->tm_hour, local->tm_min, local->tm_sec ) );
00132 
00133 //  kdDebug() << "--- LOCAL: " << result.toString() << endl;
00134   return result;
00135 }
00136 
00137 QDateTime KPimPrefs::localTimeToUtc( const QDateTime &_dt,
00138                                      const QString &timeZoneId )
00139 {
00140   QDateTime dt(_dt);
00141 //  kdDebug() << "--- LOCAL: " << dt.toString() << endl;
00142 
00143   int yearCorrection = 0;
00144   // The timezone conversion only works for dates > 1970
00145   // For dates < 1970 we adjust the date to be in 1970, 
00146   // do the correction there and then re-adjust back.
00147   // Actually, we use 1971 to prevent errors around
00148   // January 1, 1970
00149   
00150   int year = dt.date().year();
00151   if (year < 1971)
00152   {
00153     yearCorrection = 1971 - year;
00154     dt = dt.addYears(yearCorrection);
00155 //    kdDebug() << "---   Adjusted LOCAL: " << dt.toString() << endl;
00156   }
00157 
00158   QCString origTz = getenv("TZ");
00159 
00160   setenv( "TZ", timeZoneId.local8Bit(), 1 );
00161   time_t localTime = dt.toTime_t();
00162 
00163   setenv( "TZ", "UTC", 1 );
00164   struct tm *utc = gmtime( &localTime );
00165 
00166   if ( origTz.isNull() ) {
00167     unsetenv( "TZ" );
00168   } else {
00169     setenv( "TZ", origTz, 1 );
00170   }
00171   tzset();
00172 
00173   QDateTime result( QDate( utc->tm_year + 1900 - yearCorrection,
00174                            utc->tm_mon + 1, utc->tm_mday ),
00175                     QTime( utc->tm_hour, utc->tm_min, utc->tm_sec ) );
00176 
00177 //  kdDebug() << "---   UTC: " << result.toString() << endl;
00178 
00179   return result;
00180 }
00181 
00182 void KPimPrefs::usrWriteConfig()
00183 {
00184   config()->setGroup( "General" );
00185   config()->writeEntry( "Custom Categories", mCustomCategories );
00186 }
KDE Logo
This file is part of the documentation for libkdepim Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Dec 21 14:23:17 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003