not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / plasma / dataengines / time / timeengine.cpp
blob6c8aa5f36be71ad5e430bd6907c827f0b949c5bb
1 /*
2 * Copyright 2007 Aaron Seigo <aseigo@kde.org>
3 * Copyright 2008 Alex Merry <alex.merry@kdemail.net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Library General Public License as
7 * published by the Free Software Foundation; either version 2 or
8 * (at your option) any later version.
10 * This program 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
13 * GNU General Public License for more details
15 * You should have received a copy of the GNU Library General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "timeengine.h"
23 #include <QDate>
24 #include <QDBusConnection>
25 #include <QStringList>
26 #include <QTime>
28 #include <KLocale>
29 #include <KSystemTimeZones>
30 #include <KDateTime>
32 //timezone is defined in msvc
33 #ifdef timezone
34 #undef timezone
35 #endif
37 TimeEngine::TimeEngine(QObject *parent, const QVariantList &args)
38 : Plasma::DataEngine(parent, args)
40 Q_UNUSED(args)
41 setMinimumPollingInterval(333);
43 // To have translated timezone names
44 // (effectively a noop if the catalog is already present).
45 KGlobal::locale()->insertCatalog("timezones4");
48 void TimeEngine::init()
50 //QDBusInterface *ktimezoned = new QDBusInterface("org.kde.kded", "/modules/ktimezoned", "org.kde.KTimeZoned");
51 QDBusConnection dbus = QDBusConnection::sessionBus();
52 dbus.connect(QString(), QString(), "org.kde.KTimeZoned", "configChanged", this, SLOT(updateAllSources()));
55 QStringList TimeEngine::sources() const
57 QStringList timezones(KSystemTimeZones::zones().keys());
58 timezones << "Local";
59 return timezones;
62 bool TimeEngine::sourceRequestEvent(const QString &name)
64 return updateSourceEvent(name);
67 bool TimeEngine::updateSourceEvent(const QString &tz)
69 QString timezone;
71 static const QString localName = I18N_NOOP("Local");
72 if (tz == localName) {
73 setData(localName, I18N_NOOP("Time"), QTime::currentTime());
74 setData(localName, I18N_NOOP("Date"), QDate::currentDate());
75 // this is relatively cheap - KSTZ::local() is cached
76 timezone = KSystemTimeZones::local().name();
77 } else {
78 KTimeZone newTz = KSystemTimeZones::zone(tz);
79 if (!newTz.isValid()) {
80 return false;
83 KDateTime dt = KDateTime::currentDateTime(newTz);
84 setData(tz, I18N_NOOP("Time"), dt.time());
85 setData(tz, I18N_NOOP("Date"), dt.date());
86 timezone = tz;
89 QString trTimezone = i18n(timezone.toUtf8());
90 setData(tz, I18N_NOOP("Timezone"), trTimezone);
91 QStringList tzParts = trTimezone.split("/");
93 setData(tz, I18N_NOOP("Timezone Continent"), tzParts.value(0));
94 setData(tz, I18N_NOOP("Timezone City"), tzParts.value(1));
96 return true;
100 K_EXPORT_PLASMA_DATAENGINE(time, TimeEngine)
102 #include "timeengine.moc"