summaryrefslogtreecommitdiffstats
path: root/kicker/applets/clock/zone.cpp
blob: 3a313fbe86d1fde4bdf38665f425ab54eb850f3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/************************************************************

Copyright (c) 1996-2002 the kicker authors. See file AUTHORS.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

******************************************************************/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "zone.h"

#include <kcolorbutton.h>
#include <tdeconfig.h>
#include <kdebug.h>
#include <kstandarddirs.h>
#include <kstringhandler.h>
#include <klocale.h>

#include <tqfile.h>
#include <tqtooltip.h>
#include <tdelistview.h>

#include <time.h>
#include <stdlib.h> // for getenv()

Zone::Zone(TDEConfig* conf):
    config(conf),
    _zoneIndex(0)
{
    _defaultTZ = ::getenv("TZ");
    tzset();

    config->setGroup("General");

    /* default displayable timezones */
    TQString tzList = config->readEntry("RemoteZones");
    _remotezonelist = TQStringList::split(",", tzList);
    setZone(config->readNumEntry("Initial_TZ", 0));
}

Zone::~Zone()
{
    writeSettings();
}

void Zone::setZone(int z)
{
    if (_zoneIndex > _remotezonelist.count())
        z = 0;

    _zoneIndex = z;
}

TQString Zone::zone(int z) const
{
    return (z == 0 ? _defaultTZ : _remotezonelist[z-1]);
}

int Zone::calc_TZ_offset(const TQString& zone, bool /* reset */)
{
  const KTimezone *z = zone.isEmpty() ? m_zoneDb.local() : m_zoneDb.zone(zone);

  if (!z)
  {
    z = m_zoneDb.local();
  }

  if (z)
  {
    return -z->offset(Qt::LocalTime);
  }

  return 0;
}

void Zone::readZoneList(TDEListView *listView )
{
  const KTimezones::ZoneMap zones = m_zoneDb.allZones();
  TQMap<TQString, TQListViewItem*> KontinentMap;

  listView->setRootIsDecorated(true);
  for (KTimezones::ZoneMap::ConstIterator it = zones.begin(); it != zones.end(); ++it)
  {
    const KTimezone *zone = it.data();
    TQString tzName = zone->name();
    TQString comment = zone->comment();
    if (!comment.isEmpty())
      comment = i18n(comment.utf8());

    const TQStringList KontCity = TQStringList::split("/", i18n(tzName.utf8()).replace("_", " "));
    TQListViewItem* Kontinent = KontinentMap[KontCity[0]];
    if (!Kontinent) {
	KontinentMap[KontCity[0]] = new TQListViewItem(listView, KontCity[0]);
	Kontinent = KontinentMap[KontCity[0]];
	Kontinent->setExpandable(true);
    }

    TQCheckListItem *li = new TQCheckListItem(Kontinent, KontCity[1], TQCheckListItem::CheckBox);
    li->setText(1, comment);
    li->setText(2, tzName); /* store complete path in ListView */

    if (_remotezonelist.findIndex(tzName) != -1)
       li->setOn(true);

    // locate the flag from /l10n/%1/flag.png
    // if not available select default "C" flag
    TQString flag = locate( "locale", TQString("l10n/%1/flag.png").arg(zone->countryCode().lower()) );
    if (!TQFile::exists(flag))
       flag = locate( "locale", "l10n/C/flag.png" );
    if (TQFile::exists(flag))
       li->setPixmap(0, TQPixmap(flag));
  }
}

void Zone::getSelectedZonelist(TDEListView *listView)
{
     _remotezonelist.clear();

     /* loop through all entries */
     TQListViewItem *root = listView->firstChild();
     while (root) {
	if (root->firstChild()) {
		root = root->firstChild();
		continue;
	}

	TQCheckListItem *cl = (TQCheckListItem*) root;
	if (cl->isOn()) {
		_remotezonelist.append(cl->text(2));
	}

	if (root->nextSibling()) {
		root = root->nextSibling();
		continue;
	}
	root = root->parent();
	if (root)
		root = root->nextSibling();
     }
}

void Zone::nextZone()
{
    if (++_zoneIndex > _remotezonelist.count())
	_zoneIndex = 0;
}

void Zone::prevZone()
{
    if (_zoneIndex == 0)
	_zoneIndex = _remotezonelist.count();
    else
	--_zoneIndex;
}

void Zone::writeSettings()
{
    config->setGroup("General");
    config->writeEntry("RemoteZones", _remotezonelist.join(","));
    config->writeEntry("Initial_TZ",_zoneIndex);
    config->sync();
}