~ubuntu-branches/ubuntu/oneiric/kdepim/oneiric-updates

« back to all changes in this revision

Viewing changes to incidenceeditor-ng/ktimezonecombobox.cpp

  • Committer: Package Import Robot
  • Author(s): Philip Muškovac
  • Date: 2011-06-28 19:33:24 UTC
  • mfrom: (0.2.13) (0.1.13 sid)
  • Revision ID: package-import@ubuntu.com-20110628193324-8yvjs8sdv9rdoo6c
Tags: 4:4.7.0-0ubuntu1
* New upstream release
  - update install files
  - add missing kdepim-doc package to control file
  - Fix Vcs lines
  - kontact breaks/replaces korganizer << 4:4.6.80
  - tighten the dependency of kdepim-dev on libkdepim4 to fix lintian error

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright (C) 2007 Bruno Virlet <bruno.virlet@gmail.com>
 
3
  Copyright 2008-2009 Allen Winter <winter@kde.org>
 
4
 
 
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.
 
9
 
 
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.
 
14
 
 
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.
 
18
*/
 
19
 
 
20
#include "ktimezonecombobox.h"
 
21
 
 
22
#include <KCalCore/ICalTimeZones>
 
23
 
 
24
#include <KDebug>
 
25
#include <KGlobal>
 
26
#include <KLocale>
 
27
#include <KSystemTimeZone>
 
28
 
 
29
using namespace IncidenceEditorNG;
 
30
 
 
31
class KTimeZoneComboBox::Private
 
32
{
 
33
  public:
 
34
    Private( KTimeZoneComboBox *parent )
 
35
      : mParent( parent ), mAdditionalZones( 0 )
 
36
    {}
 
37
 
 
38
    void fillComboBox();
 
39
    KTimeZoneComboBox *const mParent;
 
40
    QStringList mZones;
 
41
    const KCalCore::ICalTimeZones *mAdditionalZones;
 
42
};
 
43
 
 
44
void KTimeZoneComboBox::Private::fillComboBox()
 
45
{
 
46
  mParent->clear();
 
47
  // Read all system time zones
 
48
 
 
49
  const KTimeZones::ZoneMap timezones = KSystemTimeZones::zones();
 
50
  for ( KTimeZones::ZoneMap::ConstIterator it=timezones.begin(); it != timezones.end(); ++it ) {
 
51
    mZones.append( it.key().toUtf8() );
 
52
  }
 
53
  mZones.sort();
 
54
 
 
55
  // Prepend the list of additional timezones
 
56
  if ( mAdditionalZones ) {
 
57
    const KCalCore::ICalTimeZones::ZoneMap calzones = mAdditionalZones->zones();
 
58
    for ( KCalCore::ICalTimeZones::ZoneMap::ConstIterator it=calzones.begin();
 
59
          it != calzones.end(); ++it ) {
 
60
      kDebug() << "Prepend timezone " << it.key().toUtf8();
 
61
      mZones.prepend( it.key().toUtf8() );
 
62
    }
 
63
  }
 
64
  // Prepend UTC and Floating, for convenience
 
65
  mZones.prepend( "UTC" );      // do not use i18n here
 
66
  mZones.prepend( "Floating" ); // do not use i18n here
 
67
 
 
68
  // Put translated zones into the combobox
 
69
  foreach ( const QString &z, mZones ) {
 
70
    mParent->addItem( i18n( z.toUtf8() ).replace( '_', ' ' ) );
 
71
  }
 
72
}
 
73
 
 
74
KTimeZoneComboBox::KTimeZoneComboBox( QWidget *parent )
 
75
  : KComboBox( parent ), d( new KTimeZoneComboBox::Private( this ) )
 
76
{
 
77
  KGlobal::locale()->insertCatalog( "timezones4" ); // for translated timezones
 
78
  d->fillComboBox();
 
79
}
 
80
 
 
81
KTimeZoneComboBox::KTimeZoneComboBox( const KCalCore::ICalTimeZones *zones, QWidget *parent )
 
82
  : KComboBox( parent ), d( new KTimeZoneComboBox::Private( this ) )
 
83
{
 
84
  d->mAdditionalZones = zones;
 
85
  KGlobal::locale()->insertCatalog( "timezones4" ); // for translated timezones
 
86
  d->fillComboBox();
 
87
}
 
88
 
 
89
void KTimeZoneComboBox::setAdditionalTimeZones( const KCalCore::ICalTimeZones *zones )
 
90
{
 
91
  d->mAdditionalZones = zones;
 
92
  d->fillComboBox();
 
93
}
 
94
 
 
95
KTimeZoneComboBox::~KTimeZoneComboBox()
 
96
{
 
97
  delete d;
 
98
}
 
99
 
 
100
void KTimeZoneComboBox::selectTimeSpec( const KDateTime::Spec &spec )
 
101
{
 
102
  int nCurrentlySet = -1;
 
103
 
 
104
  int i = 0;
 
105
  foreach ( const QString &z, d->mZones ) {
 
106
    if ( z == spec.timeZone().name() ) {
 
107
      nCurrentlySet = i;
 
108
      break;
 
109
    }
 
110
    i++;
 
111
  }
 
112
 
 
113
  if ( nCurrentlySet == -1 ) {
 
114
    if ( spec.isUtc() ) {
 
115
      setCurrentIndex( 1 ); // UTC
 
116
    } else {
 
117
      setCurrentIndex( 0 ); // Floating event
 
118
    }
 
119
  } else {
 
120
    setCurrentIndex( nCurrentlySet );
 
121
  }
 
122
}
 
123
 
 
124
KDateTime::Spec KTimeZoneComboBox::selectedTimeSpec() const
 
125
{
 
126
  KDateTime::Spec spec;
 
127
  if ( currentIndex() == 0 ) { // Floating event
 
128
    spec = KDateTime::Spec( KDateTime::ClockTime );
 
129
  }
 
130
  else if ( currentIndex() == 1 ) { // UTC
 
131
    spec.setType( KDateTime::UTC );
 
132
  } else {
 
133
    spec.setType( KSystemTimeZones::zone( d->mZones[currentIndex()] ) );
 
134
  }
 
135
 
 
136
  return spec;
 
137
}
 
138
 
 
139
void KTimeZoneComboBox::selectLocalTimeSpec()
 
140
{
 
141
  selectTimeSpec( KDateTime::Spec( KSystemTimeZones::local() ) );
 
142
}
 
143
 
 
144
void KTimeZoneComboBox::setFloating( bool floating, const KDateTime::Spec &spec )
 
145
{
 
146
  if ( floating ) {
 
147
    selectTimeSpec( KDateTime::Spec( KDateTime::ClockTime ) );
 
148
  } else {
 
149
    if ( spec.isValid() ) {
 
150
      selectTimeSpec( spec );
 
151
    } else {
 
152
      selectLocalTimeSpec();
 
153
    }
 
154
  }
 
155
}