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
32 Private(KTimeZoneComboBox
*parent
)
33 : mParent(parent
), mAdditionalZones(0)
37 KTimeZoneComboBox
*const mParent
;
39 const KCalCore::ICalTimeZones
*mAdditionalZones
;
42 void KTimeZoneComboBox::Private::fillComboBox()
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());
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))
79 KTimeZoneComboBox::KTimeZoneComboBox(const KCalCore::ICalTimeZones
*zones
, QWidget
*parent
)
80 : KComboBox(parent
), d(new KTimeZoneComboBox::Private(this))
82 d
->mAdditionalZones
= zones
;
86 void KTimeZoneComboBox::setAdditionalTimeZones(const KCalCore::ICalTimeZones
*zones
)
88 d
->mAdditionalZones
= zones
;
92 KTimeZoneComboBox::~KTimeZoneComboBox()
97 void KTimeZoneComboBox::selectTimeSpec(const KDateTime::Spec
&spec
)
99 int nCurrentlySet
= -1;
102 foreach (const QString
&z
, d
->mZones
) {
103 if (z
== spec
.timeZone().name()) {
110 if (nCurrentlySet
== -1) {
112 setCurrentIndex(2); // UTC
113 } else if (spec
.isLocalZone()) {
114 setCurrentIndex(0); // Local
116 setCurrentIndex(1); // Floating event
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
);
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
);
139 const KCalCore::ICalTimeZone additionalTz
=
140 d
->mAdditionalZones
->zone(d
->mZones
[currentIndex()]);
141 spec
.setType(additionalTz
);
149 void KTimeZoneComboBox::selectLocalTimeSpec()
151 selectTimeSpec(KDateTime::Spec(KSystemTimeZones::local()));
154 void KTimeZoneComboBox::setFloating(bool floating
, const KDateTime::Spec
&spec
)
157 selectTimeSpec(KDateTime::Spec(KDateTime::ClockTime
));
159 if (spec
.isValid()) {
160 selectTimeSpec(spec
);
162 selectLocalTimeSpec();