Warning: That file was not part of the compilation database. It may have many parsing errors.

1/*
2 This file is part of the KDE libraries
3 Copyright (c) 2009 Till Adam <adam@kde.org>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21#include "ktimezoned_win.moc"
22#include "ktimezonedbase.moc"
23
24#include <climits>
25#include <cstdlib>
26
27#include <QStringList>
28#include <QTextStream>
29#include <QtDBus/QtDBus>
30#include <QThread>
31#include <QDebug>
32
33#include <kglobal.h>
34#include <klocale.h>
35#include <kcodecs.h>
36#include <kdebug.h>
37#include <kconfiggroup.h>
38
39#include <kpluginfactory.h>
40#include <kpluginloader.h>
41
42#ifdef Q_OS_WIN
43#include <Windows.h>
44#endif
45
46K_PLUGIN_FACTORY(KTimeZonedFactory,
47 registerPlugin<KTimeZoned>();
48 )
49K_EXPORT_PLUGIN(KTimeZonedFactory("ktimezoned"))
50
51
52// Config file entry names
53const char LOCAL_ZONE[] = "LocalZone"; // name of local time zone
54static const TCHAR currentTimeZoneKey[] = TEXT("System\\CurrentControlSet\\Control\\TimeZoneInformation");
55
56class RegistryWatcherThread : public QThread
57{
58 public:
59 RegistryWatcherThread(KTimeZoned* parent)
60 :QThread(parent),q(parent)
61 {
62 }
63
64 ~RegistryWatcherThread()
65 {
66 RegCloseKey(key);
67 }
68
69 void run()
70 {
71//FIXME: the timezonechange needs to be handled diffrently
72#ifndef _WIN32
73 if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE, currentTimeZoneKey, 0, KEY_READ, &key ) == ERROR_SUCCESS )
74 {
75 while(true)
76 {
77 RegNotifyChangeKeyValue( key, true, REG_NOTIFY_CHANGE_LAST_SET,
78 NULL, false /*async, we want it to block*/ );
79 q->updateLocalZone();
80 }
81 }
82#endif
83 }
84 private:
85 KTimeZoned* q;
86 HKEY key;
87};
88
89KTimeZoned::KTimeZoned(QObject* parent, const QList<QVariant>& l)
90 : KTimeZonedBase(parent, l), mRegistryWatcherThread(0)
91{
92 init(false);
93}
94
95KTimeZoned::~KTimeZoned()
96{
97 if (mRegistryWatcherThread)
98 {
99 mRegistryWatcherThread->quit();
100 mRegistryWatcherThread->wait(100);
101 }
102 delete mRegistryWatcherThread;
103}
104
105void KTimeZoned::init(bool restart)
106{
107 if (restart)
108 {
109 kDebug(1221) << "KTimeZoned::init(restart)";
110 delete mRegistryWatcherThread;
111 mRegistryWatcherThread = 0;
112 }
113
114 KConfig config(QLatin1String("ktimezonedrc"));
115 if (restart)
116 config.reparseConfiguration();
117 KConfigGroup group(&config, "TimeZones");
118 mConfigLocalZone = group.readEntry(LOCAL_ZONE);
119
120 updateLocalZone();
121 if (!mRegistryWatcherThread)
122 {
123 mRegistryWatcherThread = new RegistryWatcherThread(this);
124 mRegistryWatcherThread->start();
125 }
126}
127
128// Check if the local zone has been updated, and if so, write the new
129// zone to the config file and notify interested parties.
130void KTimeZoned::updateLocalZone()
131{
132 // On Windows, HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones
133 // holds the time zone database. The TZI binary value is the TIME_ZONE_INFORMATION structure.
134
135 TIME_ZONE_INFORMATION tzinfo;
136 DWORD res = GetTimeZoneInformation(&tzinfo);
137 if (res == TIME_ZONE_ID_INVALID) return; // hm
138 mLocalZone = QString::fromUtf16( reinterpret_cast<ushort*>( tzinfo.StandardName ) );
139
140 if (mConfigLocalZone != mLocalZone)
141 {
142 kDebug(1221) << "Local timezone is now: " << mLocalZone;
143 KConfig config(QLatin1String("ktimezonedrc"));
144 KConfigGroup group(&config, "TimeZones");
145 mConfigLocalZone = mLocalZone;
146 group.writeEntry(LOCAL_ZONE, mConfigLocalZone);
147 group.sync();
148
149 QDBusMessage message = QDBusMessage::createSignal("/Daemon", "org.kde.KTimeZoned", "configChanged");
150 QDBusConnection::sessionBus().send(message);
151 }
152}
153
154
155

Warning: That file was not part of the compilation database. It may have many parsing errors.