libtdepim

kpimprefs.cpp
1 /*
2  This file is part of libtdepim.
3 
4  Copyright (c) 2002 Cornelius Schumacher <schumacher@kde.org>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 */
21 
22 #include <config.h>
23 
24 #include <time.h>
25 #include <unistd.h>
26 #include <stdlib.h>
27 
28 #include <tqstring.h>
29 
30 #include <kstandarddirs.h>
31 #include <tdeglobal.h>
32 #include <tdeconfig.h>
33 #include <tdelocale.h>
34 #include <kdebug.h>
35 
36 #include "kpimprefs.h"
37 
38 KPimPrefs::KPimPrefs( const TQString &name )
39  : TDEConfigSkeleton( name )
40 {
41 }
42 
43 KPimPrefs::~KPimPrefs()
44 {
45 }
46 
47 void KPimPrefs::usrSetDefaults()
48 {
49  setCategoryDefaults();
50 }
51 
52 void KPimPrefs::usrReadConfig()
53 {
54  kdDebug(5300) << "KPimPrefs::usrReadConfig()" << endl;
55 
56  config()->setGroup("General");
57  mCustomCategories = config()->readListEntry( "Custom Categories" );
58  if ( mCustomCategories.isEmpty() ) setCategoryDefaults();
59  mCustomCategories.sort();
60 }
61 
62 const TQString KPimPrefs::timezone()
63 {
64  TQString zone = "";
65 
66  // Read TimeZoneId from korganizerrc.
67  TDEConfig korgcfg( locate( "config", "korganizerrc" ) );
68  korgcfg.setGroup( "Time & Date" );
69  TQString tz( korgcfg.readEntry( "TimeZoneId" ) );
70  if ( !tz.isEmpty() ) {
71  zone = tz;
72  kdDebug(5300) << "timezone from korganizerrc is " << zone << endl;
73  }
74 
75  // If timezone not found in KOrg, use the system's default timezone.
76  if ( zone.isEmpty() ) {
77  char zonefilebuf[ PATH_MAX ];
78 
79  int len = readlink( "/etc/localtime", zonefilebuf, PATH_MAX );
80  if ( len > 0 && len < PATH_MAX ) {
81  zone = TQString::fromLocal8Bit( zonefilebuf, len );
82  zone = zone.mid( zone.find( "zoneinfo/" ) + 9 );
83  kdDebug(5300) << "system timezone from /etc/localtime is " << zone
84  << endl;
85  } else {
86  tzset();
87  zone = tzname[ 0 ];
88  kdDebug(5300) << "system timezone from tzset() is " << zone << endl;
89  }
90  }
91 
92  return( zone );
93 }
94 
95 TQDateTime KPimPrefs::utcToLocalTime( const TQDateTime &_dt,
96  const TQString &timeZoneId )
97 {
98  TQDateTime dt(_dt);
99 // kdDebug() << "--- UTC: " << dt.toString() << endl;
100 
101  int yearCorrection = 0;
102  // The timezone conversion only works for dates > 1970
103  // For dates < 1970 we adjust the date to be in 1970,
104  // do the correction there and then re-adjust back.
105  // Actually, we use 1971 to prevent errors around
106  // January 1, 1970
107  int year = dt.date().year();
108  if (year < 1971)
109  {
110  yearCorrection = 1971 - year;
111  dt = dt.addYears(yearCorrection);
112 // kdDebug() << "--- Adjusted UTC: " << dt.toString() << endl;
113  }
114 
115  TQCString origTz = getenv("TZ");
116 
117  setenv( "TZ", "UTC", 1 );
118  time_t utcTime = dt.toTime_t();
119 
120  setenv( "TZ", timeZoneId.local8Bit(), 1 );
121  struct tm *local = localtime( &utcTime );
122 
123  if ( origTz.isNull() ) {
124  unsetenv( "TZ" );
125  } else {
126  setenv( "TZ", origTz, 1 );
127  }
128  tzset();
129 
130  TQDateTime result( TQDate( local->tm_year + 1900 - yearCorrection,
131  local->tm_mon + 1, local->tm_mday ),
132  TQTime( local->tm_hour, local->tm_min, local->tm_sec ) );
133 
134 // kdDebug() << "--- LOCAL: " << result.toString() << endl;
135  return result;
136 }
137 
138 TQDateTime KPimPrefs::localTimeToUtc( const TQDateTime &_dt,
139  const TQString &timeZoneId )
140 {
141  TQDateTime dt(_dt);
142 // kdDebug() << "--- LOCAL: " << dt.toString() << endl;
143 
144  int yearCorrection = 0;
145  // The timezone conversion only works for dates > 1970
146  // For dates < 1970 we adjust the date to be in 1970,
147  // do the correction there and then re-adjust back.
148  // Actually, we use 1971 to prevent errors around
149  // January 1, 1970
150 
151  int year = dt.date().year();
152  if (year < 1971)
153  {
154  yearCorrection = 1971 - year;
155  dt = dt.addYears(yearCorrection);
156 // kdDebug() << "--- Adjusted LOCAL: " << dt.toString() << endl;
157  }
158 
159  TQCString origTz = getenv("TZ");
160 
161  setenv( "TZ", timeZoneId.local8Bit(), 1 );
162  time_t localTime = dt.toTime_t();
163 
164  setenv( "TZ", "UTC", 1 );
165  struct tm *utc = gmtime( &localTime );
166 
167  if ( origTz.isNull() ) {
168  unsetenv( "TZ" );
169  } else {
170  setenv( "TZ", origTz, 1 );
171  }
172  tzset();
173 
174  TQDateTime result( TQDate( utc->tm_year + 1900 - yearCorrection,
175  utc->tm_mon + 1, utc->tm_mday ),
176  TQTime( utc->tm_hour, utc->tm_min, utc->tm_sec ) );
177 
178 // kdDebug() << "--- UTC: " << result.toString() << endl;
179 
180  return result;
181 }
182 
183 void KPimPrefs::usrWriteConfig()
184 {
185  config()->setGroup( "General" );
186  config()->writeEntry( "Custom Categories", mCustomCategories );
187 }