summaryrefslogtreecommitdiffstats
path: root/libkdepim/kpimprefs.cpp
blob: 55a6a799f8eadcb88fd6060b2548fd395a3403f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
    This file is part of libkdepim.

    Copyright (c) 2002 Cornelius Schumacher <schumacher@kde.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include <config.h>

#include <time.h>
#include <unistd.h>
#include <stdlib.h>

#include <tqstring.h>

#include <kstandarddirs.h>
#include <kglobal.h>
#include <kconfig.h>
#include <klocale.h>
#include <kdebug.h>

#include "kpimprefs.h"

KPimPrefs::KPimPrefs( const TQString &name )
  : KConfigSkeleton( name )
{
}

KPimPrefs::~KPimPrefs()
{
}

void KPimPrefs::usrSetDefaults()
{
  setCategoryDefaults();
}

void KPimPrefs::usrReadConfig()
{
  kdDebug(5300) << "KPimPrefs::usrReadConfig()" << endl;

  config()->setGroup("General");
  mCustomCategories = config()->readListEntry( "Custom Categories" );
  if ( mCustomCategories.isEmpty() ) setCategoryDefaults();
  mCustomCategories.sort();
}

const TQString KPimPrefs::timezone()
{
  TQString zone = "";

  // Read TimeZoneId from korganizerrc.
  KConfig korgcfg( locate( "config", "korganizerrc" ) );
  korgcfg.setGroup( "Time & Date" );
  TQString tz( korgcfg.readEntry( "TimeZoneId" ) );
  if ( !tz.isEmpty() ) {
    zone = tz;
    kdDebug(5300) << "timezone from korganizerrc is " << zone << endl;
  }

  // If timezone not found in KOrg, use the system's default timezone.
  if ( zone.isEmpty() ) {
    char zonefilebuf[ PATH_MAX ];

    int len = readlink( "/etc/localtime", zonefilebuf, PATH_MAX );
    if ( len > 0 && len < PATH_MAX ) {
      zone = TQString::fromLocal8Bit( zonefilebuf, len );
      zone = zone.mid( zone.tqfind( "zoneinfo/" ) + 9 );
      kdDebug(5300) << "system timezone from /etc/localtime is " << zone
                    << endl;
    } else {
      tzset();
      zone = tzname[ 0 ];
      kdDebug(5300) << "system timezone from tzset() is " << zone << endl;
    }
  }

  return( zone );
}

TQDateTime KPimPrefs::utcToLocalTime( const TQDateTime &_dt,
                                     const TQString &timeZoneId )
{
  TQDateTime dt(_dt);
//  kdDebug() << "---   UTC: " << dt.toString() << endl;

  int yearCorrection = 0;
  // The timezone conversion only works for dates > 1970
  // For dates < 1970 we adjust the date to be in 1970, 
  // do the correction there and then re-adjust back.
  // Actually, we use 1971 to prevent errors around
  // January 1, 1970
  int year = dt.date().year();
  if (year < 1971)
  {
    yearCorrection = 1971 - year;
    dt = dt.addYears(yearCorrection);
//    kdDebug() << "---   Adjusted UTC: " << dt.toString() << endl;
  }
  
  TQCString origTz = getenv("TZ");

  setenv( "TZ", "UTC", 1 );
  time_t utcTime = dt.toTime_t();

  setenv( "TZ", timeZoneId.local8Bit(), 1 );
  struct tm *local = localtime( &utcTime );

  if ( origTz.isNull() ) {
    unsetenv( "TZ" );
  } else {
    setenv( "TZ", origTz, 1 );
  }
  tzset();

  TQDateTime result( TQDate( local->tm_year + 1900 - yearCorrection,
                           local->tm_mon + 1, local->tm_mday ),
                    TQTime( local->tm_hour, local->tm_min, local->tm_sec ) );

//  kdDebug() << "--- LOCAL: " << result.toString() << endl;
  return result;
}

TQDateTime KPimPrefs::localTimeToUtc( const TQDateTime &_dt,
                                     const TQString &timeZoneId )
{
  TQDateTime dt(_dt);
//  kdDebug() << "--- LOCAL: " << dt.toString() << endl;

  int yearCorrection = 0;
  // The timezone conversion only works for dates > 1970
  // For dates < 1970 we adjust the date to be in 1970, 
  // do the correction there and then re-adjust back.
  // Actually, we use 1971 to prevent errors around
  // January 1, 1970
  
  int year = dt.date().year();
  if (year < 1971)
  {
    yearCorrection = 1971 - year;
    dt = dt.addYears(yearCorrection);
//    kdDebug() << "---   Adjusted LOCAL: " << dt.toString() << endl;
  }

  TQCString origTz = getenv("TZ");

  setenv( "TZ", timeZoneId.local8Bit(), 1 );
  time_t localTime = dt.toTime_t();

  setenv( "TZ", "UTC", 1 );
  struct tm *utc = gmtime( &localTime );

  if ( origTz.isNull() ) {
    unsetenv( "TZ" );
  } else {
    setenv( "TZ", origTz, 1 );
  }
  tzset();

  TQDateTime result( TQDate( utc->tm_year + 1900 - yearCorrection,
                           utc->tm_mon + 1, utc->tm_mday ),
                    TQTime( utc->tm_hour, utc->tm_min, utc->tm_sec ) );

//  kdDebug() << "---   UTC: " << result.toString() << endl;

  return result;
}

void KPimPrefs::usrWriteConfig()
{
  config()->setGroup( "General" );
  config()->writeEntry( "Custom Categories", mCustomCategories );
}