You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
188 lines
5.1 KiB
188 lines
5.1 KiB
15 years ago
|
/*
|
||
13 years ago
|
This file is part of libtdepim.
|
||
15 years ago
|
|
||
|
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>
|
||
|
|
||
15 years ago
|
#include <tqstring.h>
|
||
15 years ago
|
|
||
|
#include <kstandarddirs.h>
|
||
|
#include <kglobal.h>
|
||
|
#include <kconfig.h>
|
||
|
#include <klocale.h>
|
||
|
#include <kdebug.h>
|
||
|
|
||
|
#include "kpimprefs.h"
|
||
|
|
||
15 years ago
|
KPimPrefs::KPimPrefs( const TQString &name )
|
||
15 years ago
|
: 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();
|
||
|
}
|
||
|
|
||
15 years ago
|
const TQString KPimPrefs::timezone()
|
||
15 years ago
|
{
|
||
15 years ago
|
TQString zone = "";
|
||
15 years ago
|
|
||
|
// Read TimeZoneId from korganizerrc.
|
||
|
KConfig korgcfg( locate( "config", "korganizerrc" ) );
|
||
|
korgcfg.setGroup( "Time & Date" );
|
||
15 years ago
|
TQString tz( korgcfg.readEntry( "TimeZoneId" ) );
|
||
15 years ago
|
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 ) {
|
||
15 years ago
|
zone = TQString::fromLocal8Bit( zonefilebuf, len );
|
||
13 years ago
|
zone = zone.mid( zone.find( "zoneinfo/" ) + 9 );
|
||
15 years ago
|
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 );
|
||
|
}
|
||
|
|
||
15 years ago
|
TQDateTime KPimPrefs::utcToLocalTime( const TQDateTime &_dt,
|
||
|
const TQString &timeZoneId )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQDateTime dt(_dt);
|
||
15 years ago
|
// 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;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQCString origTz = getenv("TZ");
|
||
15 years ago
|
|
||
|
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();
|
||
|
|
||
15 years ago
|
TQDateTime result( TQDate( local->tm_year + 1900 - yearCorrection,
|
||
15 years ago
|
local->tm_mon + 1, local->tm_mday ),
|
||
15 years ago
|
TQTime( local->tm_hour, local->tm_min, local->tm_sec ) );
|
||
15 years ago
|
|
||
|
// kdDebug() << "--- LOCAL: " << result.toString() << endl;
|
||
|
return result;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQDateTime KPimPrefs::localTimeToUtc( const TQDateTime &_dt,
|
||
|
const TQString &timeZoneId )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQDateTime dt(_dt);
|
||
15 years ago
|
// 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;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQCString origTz = getenv("TZ");
|
||
15 years ago
|
|
||
|
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();
|
||
|
|
||
15 years ago
|
TQDateTime result( TQDate( utc->tm_year + 1900 - yearCorrection,
|
||
15 years ago
|
utc->tm_mon + 1, utc->tm_mday ),
|
||
15 years ago
|
TQTime( utc->tm_hour, utc->tm_min, utc->tm_sec ) );
|
||
15 years ago
|
|
||
|
// kdDebug() << "--- UTC: " << result.toString() << endl;
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
void KPimPrefs::usrWriteConfig()
|
||
|
{
|
||
|
config()->setGroup( "General" );
|
||
|
config()->writeEntry( "Custom Categories", mCustomCategories );
|
||
|
}
|