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>
28 #include <KSystemTimeZone>
33 class KPIM::KTimeZoneComboBox::Private
36 Private( Calendar
*calendar
, KTimeZoneComboBox
*parent
)
37 : mParent( parent
), mCalendar( calendar
)
41 KTimeZoneComboBox
*mParent
;
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() );
56 // Prepend the list of timezones from the Calendar
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
82 void KTimeZoneComboBox::setCalendar( Calendar
*calendar
)
84 d
->mCalendar
= calendar
;
87 KTimeZoneComboBox::~KTimeZoneComboBox()
92 void KTimeZoneComboBox::selectTimeSpec( const KDateTime::Spec
&spec
)
94 int nCurrentlySet
= -1;
97 foreach( const QString
&z
, d
->mZones
) {
98 if ( z
== spec
.timeZone().name() ) {
105 if ( nCurrentlySet
== -1 ) {
106 if ( spec
.isUtc() ) {
107 setCurrentIndex( 1 ); // UTC
109 setCurrentIndex( 0 ); // Floating event
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
);
126 spec
.setType( KSystemTimeZones::zone( d
->mZones
[currentIndex()] ) );
132 void KTimeZoneComboBox::selectLocalTimeSpec()
134 selectTimeSpec( KDateTime::Spec( KSystemTimeZones::local() ) );
137 void KTimeZoneComboBox::setFloating( bool floating
, const KDateTime::Spec
&spec
)
140 selectTimeSpec( KDateTime::ClockTime
);
142 if ( spec
.isValid() ) {
143 selectTimeSpec( spec
);
145 selectLocalTimeSpec();