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
|
/*
This file is part of the KDE libraries
Copyright (c) 2009 Till Adam <adam@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 "ktimezoned_win.moc"
#include "ktimezonedbase.moc"
#include <climits>
#include <cstdlib>
#include <QStringList>
#include <QTextStream>
#include <QtDBus/QtDBus>
#include <QThread>
#include <QDebug>
#include <kglobal.h>
#include <klocale.h>
#include <kcodecs.h>
#include <kdebug.h>
#include <kconfiggroup.h>
#include <kpluginfactory.h>
#include <kpluginloader.h>
#ifdef Q_OS_WIN
#include <Windows.h>
#endif
K_PLUGIN_FACTORY_WITH_JSON(KTimeZonedFactory,
"ktimezoned.json",
registerPlugin<KTimeZoned>();
)
// Config file entry names
const char LOCAL_ZONE[] = "LocalZone"; // name of local time zone
static const TCHAR currentTimeZoneKey[] = TEXT("System\\CurrentControlSet\\Control\\TimeZoneInformation");
class RegistryWatcherThread : public QThread
{
public:
RegistryWatcherThread(KTimeZoned* parent)
:QThread(parent),q(parent)
{
}
~RegistryWatcherThread()
{
RegCloseKey(key);
}
void run()
{
//FIXME: the timezonechange needs to be handled diffrently
#ifndef _WIN32
if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE, currentTimeZoneKey, 0, KEY_READ, &key ) == ERROR_SUCCESS )
{
while(true)
{
RegNotifyChangeKeyValue( key, true, REG_NOTIFY_CHANGE_LAST_SET,
NULL, false /*async, we want it to block*/ );
q->updateLocalZone();
}
}
#endif
}
private:
KTimeZoned* q;
HKEY key;
};
KTimeZoned::KTimeZoned(QObject* parent, const QList<QVariant>& l)
: KTimeZonedBase(parent, l), mRegistryWatcherThread(0)
{
init(false);
}
KTimeZoned::~KTimeZoned()
{
if (mRegistryWatcherThread)
{
mRegistryWatcherThread->quit();
mRegistryWatcherThread->wait(100);
}
delete mRegistryWatcherThread;
}
void KTimeZoned::init(bool restart)
{
if (restart)
{
qDebug() << "KTimeZoned::init(restart)";
delete mRegistryWatcherThread;
mRegistryWatcherThread = 0;
}
KConfig config(QLatin1String("ktimezonedrc"));
if (restart)
config.reparseConfiguration();
KConfigGroup group(&config, "TimeZones");
mConfigLocalZone = group.readEntry(LOCAL_ZONE);
updateLocalZone();
if (!mRegistryWatcherThread)
{
mRegistryWatcherThread = new RegistryWatcherThread(this);
mRegistryWatcherThread->start();
}
}
// Check if the local zone has been updated, and if so, write the new
// zone to the config file and notify interested parties.
void KTimeZoned::updateLocalZone()
{
// On Windows, HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones
// holds the time zone database. The TZI binary value is the TIME_ZONE_INFORMATION structure.
TIME_ZONE_INFORMATION tzinfo;
DWORD res = GetTimeZoneInformation(&tzinfo);
if (res == TIME_ZONE_ID_INVALID) return; // hm
mLocalZone = QString::fromUtf16( reinterpret_cast<ushort*>( tzinfo.StandardName ) );
if (mConfigLocalZone != mLocalZone)
{
qDebug() << "Local timezone is now: " << mLocalZone;
KConfig config(QLatin1String("ktimezonedrc"));
KConfigGroup group(&config, "TimeZones");
mConfigLocalZone = mLocalZone;
group.writeEntry(LOCAL_ZONE, mConfigLocalZone);
group.sync();
QDBusMessage message = QDBusMessage::createSignal("/Daemon", "org.kde.KTimeZoned", "timeZoneChanged");
QDBusConnection::sessionBus().send(message);
}
}
|