summaryrefslogtreecommitdiffstats
path: root/kworldwatch/zoneclock.cpp
blob: 7d0ad6bfa37d3a1dd9ba5090ee09b01108d0a789 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
**
** Copyright (C) 1998-2001 by Matthias H�lzer-Kl�pfel <hoelzer@kde.org>
**	Maintainence has ceased - send questions to kde-devel@kde.org.
**
*/

/*
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program in a file called COPYING; if not, write to
** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
** MA 02110-1301, USA.
*/

/*
** Bug reports and questions can be sent to kde-devel@kde.org
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif // HAVE_CONFIG_H

#include <stdlib.h>
#include <time.h>


#include <tqlabel.h>
#include <tqlayout.h>
#include <tqdatetime.h>
#include <tqtimer.h>
#include <tqcombobox.h>
#include <tqlineedit.h>
#include <tqpopupmenu.h>


#include <tdeglobal.h>
#include <tdelocale.h>
#include <tdeconfig.h>


#include "flow.h"
#include "clock.h"
#include "cities.h"
#include "zoneclock.moc"
#include <kdebug.h>

ZoneClock::ZoneClock(const TQString &zone, const TQString &name, TQWidget *parent, const char *n)
  : TQFrame(parent, n), _zone(zone), _name(name)
{
  setFrameStyle(TQFrame::Panel | TQFrame::Raised);
  TQHBoxLayout *hbox = new TQHBoxLayout(this, 2,2);

  _name.append(":");
  _nameLabel = new TQLabel(_name, this);
  hbox->addWidget(_nameLabel, 1);
  hbox->addSpacing(4);

  _timeLabel = new TQLabel(this);
  hbox->addWidget(_timeLabel, 0, TQt::AlignRight);

  _popup = new TQPopupMenu(this);
  _popup->insertItem(i18n("&Edit..."), this, TQT_SLOT(editClock()));
  _popup->insertItem(i18n("&Add..."), this, TQT_SLOT(slotAddClock()));
  _popup->insertItem(i18n("&Remove"), this, TQT_SLOT(slotRemoveClock()));

  _nameLabel->installEventFilter(this);
  _timeLabel->installEventFilter(this);

  updateTime();
}


void ZoneClock::slotRemoveClock()
{
  // Note: this is stupid, but we can't get us deleted
  // from this slot, as we would return and crash.
  // So instead we fire up an idle event triggering the delete
  // after the return.

  TQTimer::singleShot(0, this, TQT_SLOT(removeTimeout()));
}


void ZoneClock::removeTimeout()
{
  emit removeMe(this);
}


void ZoneClock::slotAddClock()
{
  emit addClock(_zone);
}


void ZoneClock::editClock()
{
  ClockDialog *_dlg = new ClockDialog(this, 0, true);
  CityList cities;
  TQStringList timezones = cities.timezones();
  for (TQStringList::iterator it = timezones.begin(); it != timezones.end(); ++it)
    _dlg->ClockZone->insertItem(i18n((*it).utf8()));

  _dlg->ClockCaption->setText(_nameLabel->text().left(_nameLabel->text().length()-1));
  for (int i=0; i<_dlg->ClockZone->count(); ++i)
    if (_dlg->ClockZone->text(i) == i18n(_zone.utf8()))
      {
        _dlg->ClockZone->setCurrentItem(i);
        break;
      }

  if (_dlg->exec() == TQDialog::Accepted)
    {
      _zone = timezones[_dlg->ClockZone->currentItem()];
      _name = _dlg->ClockCaption->text().append(":");
      _nameLabel->setText(_dlg->ClockCaption->text().append(":"));
      updateTime();
      layout()->invalidate();
      emit changed();
    }

  delete _dlg;
}


bool ZoneClock::eventFilter(TQObject *obj, TQEvent *ev)
{
  if (ev->type() == TQEvent::MouseButtonPress)
    {
      TQMouseEvent *e = (TQMouseEvent*)ev;
      if (e->button() == TQt::RightButton)
	_popup->exec(e->globalPos());
    }

  return TQFrame::eventFilter(obj, ev);
}


void ZoneClock::updateTime()
{
  char *initial_TZ = getenv("TZ");
  setenv("TZ", _zone.latin1(), 1);
  tzset();

  time_t t = time(NULL);
  TQDateTime dt;
  dt.setTime_t(t);
  _timeLabel->setText(TQString("%1, %2").arg(TDEGlobal::locale()->formatTime(dt.time(), true)).arg(TDEGlobal::locale()->formatDate(dt.date(), true)));

  if (initial_TZ != 0)
    setenv("TZ", initial_TZ, 1);
  else
    unsetenv("TZ");
  tzset();
}


ZoneClockPanel::ZoneClockPanel(TQWidget *parent, const char *name)
  : TQFrame(parent, name), _dlg(0)
{
  _flow = new SimpleFlow(this,1,1);

  TQTimer *t = new TQTimer(this);

  connect(t, TQT_SIGNAL(timeout()), this, TQT_SLOT(updateTimer()));
  t->start(500);

  _clocks.setAutoDelete(true);
}


void ZoneClockPanel::createDialog()
{
  if (!_dlg)
    {
      _dlg = new ClockDialog(this, 0, true);
      CityList cities;
      TQStringList timezones = cities.timezones();
      for (TQStringList::iterator it = timezones.begin(); it != timezones.end(); ++it)
        _dlg->ClockZone->insertItem(i18n((*it).utf8()));
    }
}


void ZoneClockPanel::addClock(const TQString &zone, const TQString &name)
{
  // add the clocks
  ZoneClock *zc = new ZoneClock(zone, name, this);
  _flow->add(zc);
  _clocks.append(zc);
  zc->show();

  realign();

  connect(zc, TQT_SIGNAL(addClock(const TQString &)), this, TQT_SLOT(addClock(const TQString &)));
  connect(zc, TQT_SIGNAL(changed()), this, TQT_SLOT(realign()));
  connect(zc, TQT_SIGNAL(removeMe(ZoneClock *)), this, TQT_SLOT(removeClock(ZoneClock *)));
}


void ZoneClockPanel::removeClock(ZoneClock *clock)
{
  _clocks.remove(clock);
  realign();
}


void ZoneClockPanel::realign()
{
  // realign the labels
  int w = 0;
  TQPtrListIterator<ZoneClock> it(_clocks);
  for ( ; it.current(); ++it)
    if (it.current()->sizeHint().width() > w)
      w = it.current()->sizeHint().width();
  it.toFirst();
  for ( ; it.current(); ++it)
    it.current()->setFixedWidth(w);
}


void ZoneClockPanel::updateTimer()
{
  TQPtrListIterator<ZoneClock> it(_clocks);
  for ( ; it.current(); ++it)
    it.current()->updateTime();
}

void ZoneClockPanel::addClock(const TQString &zone)
{
  createDialog();

  _dlg->ClockCaption->setText(i18n(zone.utf8()).section('/', -1));
  for (int i=0; i<_dlg->ClockZone->count(); ++i)
    if (_dlg->ClockZone->text(i) == i18n(zone.utf8()))
      {
        _dlg->ClockZone->setCurrentItem(i);
        break;
      }

  if (_dlg->exec() == TQDialog::Accepted)
    {
      CityList cities;
      TQStringList timezones = cities.timezones();
      TQString newzone = timezones[_dlg->ClockZone->currentItem()];
      addClock(newzone, _dlg->ClockCaption->text());
      update();
    }
}


void ZoneClockPanel::save(TDEConfig *config)
{
  config->writeEntry("Clocks", _clocks.count());

  TQPtrListIterator<ZoneClock> it(_clocks);
  int cnt=0;
  for ( ; it.current(); ++it)
    {
      TQString n = it.current()->name();
      n = n.left(n.length()-1);
      config->writeEntry(TQString("Clock_%1_Name").arg(cnt), n);
      config->writeEntry(TQString("Clock_%1_Zone").arg(cnt), it.current()->zone());
      cnt++;
    }
}


void ZoneClockPanel::load(TDEConfig *config)
{
  _clocks.clear();

  int num = config->readNumEntry("Clocks", 0);

  for (int i=0; i<num; ++i)
    {
      addClock(config->readEntry(TQString("Clock_%1_Zone").arg(i)), config->readEntry(TQString("Clock_%1_Name").arg(i)));
    }
}