krop's commit fixes my problem in a better way, reverting
[kdepim.git] / libkdepim / ktimezonecombobox.cpp
blobffc2dcf8f87963767d3a92cc101705d4e36bb485
1 /*
2 Copyright (C) 2007 Bruno Virlet <bruno.virlet@gmail.com>
3 Copyright 2008-2009 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 <kcal/calendar.h>
23 #include <kcal/icaltimezones.h>
25 #include <KDebug>
26 #include <KGlobal>
27 #include <KLocale>
28 #include <KSystemTimeZone>
30 using namespace KPIM;
31 using namespace KCal;
33 class KPIM::KTimeZoneComboBox::Private
35 public:
36 Private( Calendar *calendar, KTimeZoneComboBox *parent )
37 : mParent( parent ), mCalendar( calendar )
40 void fillComboBox();
41 KTimeZoneComboBox *mParent;
42 Calendar *mCalendar;
43 QStringList mZones;
46 void KPIM::KTimeZoneComboBox::Private::fillComboBox()
48 // Read all system time zones
50 const KTimeZones::ZoneMap timezones = KSystemTimeZones::zones();
51 for ( KTimeZones::ZoneMap::ConstIterator it=timezones.begin(); it != timezones.end(); ++it ) {
52 mZones.append( it.key().toUtf8() );
54 mZones.sort();
56 // Prepend the list of timezones from the Calendar
57 if ( mCalendar ) {
58 const ICalTimeZones::ZoneMap calzones = mCalendar->timeZones()->zones();
59 for ( ICalTimeZones::ZoneMap::ConstIterator it=calzones.begin(); it != calzones.end(); ++it ) {
60 kDebug() << "Prepend timezone " << it.key().toUtf8();
61 mZones.prepend( it.key().toUtf8() );
65 // Prepend UTC and Floating, for convenience
66 mZones.prepend( "UTC" ); // do not use i18n here
67 mZones.prepend( "Floating" ); // do not use i18n here
69 // Put translated zones into the combobox
70 foreach( const QString &z, mZones ) {
71 mParent->addItem( i18n( z.toUtf8() ).replace( '_', ' ' ) );
75 KTimeZoneComboBox::KTimeZoneComboBox( Calendar *calendar, QWidget *parent )
76 : KComboBox( parent ), d( new KPIM::KTimeZoneComboBox::Private( calendar, this ) )
78 KGlobal::locale()->insertCatalog( "timezones4" ); // for translated timezones
79 d->fillComboBox();
82 void KTimeZoneComboBox::setCalendar( Calendar *calendar )
84 d->mCalendar = calendar;
87 KTimeZoneComboBox::~KTimeZoneComboBox()
89 delete d;
92 void KTimeZoneComboBox::selectTimeSpec( const KDateTime::Spec &spec )
94 int nCurrentlySet = -1;
96 int i = 0;
97 foreach( const QString &z, d->mZones ) {
98 if ( z == spec.timeZone().name() ) {
99 nCurrentlySet = i;
100 break;
102 i++;
105 if ( nCurrentlySet == -1 ) {
106 if ( spec.isUtc() ) {
107 setCurrentIndex( 1 ); // UTC
108 } else {
109 setCurrentIndex( 0 ); // Floating event
111 } else {
112 setCurrentIndex( nCurrentlySet );
116 KDateTime::Spec KTimeZoneComboBox::selectedTimeSpec()
118 KDateTime::Spec spec;
119 if ( currentIndex() == 0 ) { // Floating event
120 spec = KDateTime::Spec( KDateTime::ClockTime );
122 else if ( currentIndex() == 1 ) { // UTC
123 spec.setType( KDateTime::UTC );
125 else {
126 spec.setType( KSystemTimeZones::zone( d->mZones[currentIndex()] ) );
129 return spec;
132 void KTimeZoneComboBox::selectLocalTimeSpec()
134 selectTimeSpec( KDateTime::Spec( KSystemTimeZones::local() ) );
137 void KTimeZoneComboBox::setFloating( bool floating, const KDateTime::Spec &spec )
139 if ( floating ) {
140 selectTimeSpec( KDateTime::ClockTime );
141 } else {
142 if ( spec.isValid() ) {
143 selectTimeSpec( spec );
144 } else {
145 selectLocalTimeSpec();