Make the KOrganizer plugin loading work again.
[kdepim.git] / incidenceeditor-ng / src / ktimezonecombobox.cpp
blobbd22d0f5b765afab35a7833c057cf6f7d07d40bb
1 /*
2 Copyright (C) 2007 Bruno Virlet <bruno.virlet@gmail.com>
3 Copyright 2008-2009,2013 Allen Winter <winter@kde.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, 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 General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "ktimezonecombobox.h"
22 #include <KCalCore/ICalTimeZones>
24 #include <KLocalizedString>
25 #include <KSystemTimeZone>
27 using namespace IncidenceEditorNG;
29 class Q_DECL_HIDDEN KTimeZoneComboBox::Private
31 public:
32 Private(KTimeZoneComboBox *parent)
33 : mParent(parent), mAdditionalZones(0)
36 void fillComboBox();
37 KTimeZoneComboBox *const mParent;
38 QStringList mZones;
39 const KCalCore::ICalTimeZones *mAdditionalZones;
42 void KTimeZoneComboBox::Private::fillComboBox()
44 mParent->clear();
45 mZones.clear();
47 // Read all system time zones
48 const KTimeZones::ZoneMap timezones = KSystemTimeZones::zones();
49 for (KTimeZones::ZoneMap::ConstIterator it = timezones.begin(); it != timezones.end(); ++it) {
50 mZones.append(it.key().toUtf8());
52 mZones.sort();
54 // Prepend the list of additional timezones
55 if (mAdditionalZones) {
56 const KCalCore::ICalTimeZones::ZoneMap calzones = mAdditionalZones->zones();
57 for (KCalCore::ICalTimeZones::ZoneMap::ConstIterator it = calzones.begin();
58 it != calzones.end(); ++it) {
59 mZones.prepend(it.key().toUtf8());
62 // Prepend Local, UTC and Floating, for convenience
63 mZones.prepend(QStringLiteral("UTC")); // do not use i18n here index=2
64 mZones.prepend(QStringLiteral("Floating")); // do not use i18n here index=1
65 mZones.prepend(KSystemTimeZones::local().name()); // index=0
67 // Put translated zones into the combobox
68 foreach (const QString &z, mZones) {
69 mParent->addItem(i18n(z.toUtf8()).replace('_', ' '));
73 KTimeZoneComboBox::KTimeZoneComboBox(QWidget *parent)
74 : KComboBox(parent), d(new KTimeZoneComboBox::Private(this))
76 d->fillComboBox();
79 KTimeZoneComboBox::KTimeZoneComboBox(const KCalCore::ICalTimeZones *zones, QWidget *parent)
80 : KComboBox(parent), d(new KTimeZoneComboBox::Private(this))
82 d->mAdditionalZones = zones;
83 d->fillComboBox();
86 void KTimeZoneComboBox::setAdditionalTimeZones(const KCalCore::ICalTimeZones *zones)
88 d->mAdditionalZones = zones;
89 d->fillComboBox();
92 KTimeZoneComboBox::~KTimeZoneComboBox()
94 delete d;
97 void KTimeZoneComboBox::selectTimeSpec(const KDateTime::Spec &spec)
99 int nCurrentlySet = -1;
101 int i = 0;
102 foreach (const QString &z, d->mZones) {
103 if (z == spec.timeZone().name()) {
104 nCurrentlySet = i;
105 break;
107 ++i;
110 if (nCurrentlySet == -1) {
111 if (spec.isUtc()) {
112 setCurrentIndex(2); // UTC
113 } else if (spec.isLocalZone()) {
114 setCurrentIndex(0); // Local
115 } else {
116 setCurrentIndex(1); // Floating event
118 } else {
119 setCurrentIndex(nCurrentlySet);
123 KDateTime::Spec KTimeZoneComboBox::selectedTimeSpec() const
125 KDateTime::Spec spec;
126 if (currentIndex() >= 0) {
127 if (currentIndex() == 0) { // Local
128 spec = KDateTime::Spec(KDateTime::LocalZone);
129 } else if (currentIndex() == 1) { // Floating event
130 spec = KDateTime::Spec(KDateTime::ClockTime);
131 } else if (currentIndex() == 2) { // UTC
132 spec.setType(KDateTime::UTC);
133 } else {
134 const KTimeZone systemTz = KSystemTimeZones::zone(d->mZones[currentIndex()]);
135 // If it's not valid, then it's an additional Tz
136 if (systemTz.isValid()) {
137 spec.setType(systemTz);
138 } else {
139 const KCalCore::ICalTimeZone additionalTz =
140 d->mAdditionalZones->zone(d->mZones[currentIndex()]);
141 spec.setType(additionalTz);
146 return spec;
149 void KTimeZoneComboBox::selectLocalTimeSpec()
151 selectTimeSpec(KDateTime::Spec(KSystemTimeZones::local()));
154 void KTimeZoneComboBox::setFloating(bool floating, const KDateTime::Spec &spec)
156 if (floating) {
157 selectTimeSpec(KDateTime::Spec(KDateTime::ClockTime));
158 } else {
159 if (spec.isValid()) {
160 selectTimeSpec(spec);
161 } else {
162 selectLocalTimeSpec();