dtime.cpp 15.1 KB
Newer Older
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
/*
 *  dtime.cpp
 *
 *  Copyright (C) 1998 Luca Montecchiani <m.luca@usa.net>
 *
 *  Plasma analog-clock drawing code:
 *
 *  Copyright 2007 by Aaron Seigo <aseigo@kde.org>
 *  Copyright 2007 by Riccardo Iaconelli <riccardo@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; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */
#include "dtime.h"

#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>

#include <QComboBox>
#include <QGroupBox>
#include <QPushButton>
#include <QPainter>
#include <QTimeEdit>

#include <QCheckBox>
#include <QPaintEvent>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QVBoxLayout>
44
#include <QDebug>
45
46
47
48
49
50
51
52
53
54
55
#include <kprocess.h>
#include <kmessagebox.h>
#include <kdialog.h>
#include <kconfig.h>
#include <kcolorscheme.h>
#include <ksystemtimezone.h>
#include <KTreeWidgetSearchLine>
#include <KGlobal>

#include <Plasma/Svg>

56
#include "timedated_interface.h"
57
58
59

#include "helper.h"

60
61
62
Dtime::Dtime(QWidget * parent, bool haveTimeDated):
    QWidget(parent),
    m_haveTimedated(haveTimeDated)
63
64
65
{
  setupUi(this);

66
67
  connect(setDateTimeAuto, &QCheckBox::toggled, this, &Dtime::serverTimeCheck);
  connect(setDateTimeAuto, &QCheckBox::toggled, this, &Dtime::configChanged);
68
69

  timeServerList->setEditable(false);
70
71
72
  connect(timeServerList, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated), this, &Dtime::configChanged);
  connect(timeServerList, &QComboBox::editTextChanged, this, &Dtime::configChanged);
  connect(setDateTimeAuto, &QCheckBox::toggled, timeServerList, &QComboBox::setEnabled);
73
74
  timeServerList->setEnabled(false);
  timeServerList->setEditable(true);
David Edmundson's avatar
David Edmundson committed
75

76
77
78
79
80
81
82
83
84
85
  if (!haveTimeDated) {
    findNTPutility();
    if (ntpUtility.isEmpty()) {
        QString toolTip = i18n("No NTP utility has been found. "
                                "Install 'ntpdate' or 'rdate' command to enable automatic "
                                "updating of date and time.");
        setDateTimeAuto->setEnabled(false);
        setDateTimeAuto->setToolTip(toolTip);
        timeServerList->setToolTip(toolTip);
    }
86
87
88
  }

  QVBoxLayout *v2 = new QVBoxLayout( timeBox );
Laurent Montel's avatar
Laurent Montel committed
89
  v2->setContentsMargins( 0, 0, 0, 0 );
90
91

  kclock = new Kclock( timeBox );
92
  kclock->setObjectName(QStringLiteral("Kclock"));
93
94
95
96
97
98
99
100
101
102
103
104
  kclock->setMinimumSize(150,150);
  v2->addWidget( kclock );

  v2->addSpacing( KDialog::spacingHint() );

  QHBoxLayout *v3 = new QHBoxLayout( );
  v2->addLayout( v3 );

  v3->addStretch();

  timeEdit = new QTimeEdit( timeBox );
  timeEdit->setWrapping(true);
Laurent Montel's avatar
Laurent Montel committed
105
  timeEdit->setDisplayFormat(KLocale::global()->use12Clock() ? "hh:mm:ss ap" : "HH:mm:ss");
106
107
108
109
110
111
112
113
114
  v3->addWidget(timeEdit);

  v3->addStretch();

  QString wtstr = i18n("Here you can change the system time. Click into the"
    " hours, minutes or seconds field to change the relevant value, either"
    " using the up and down buttons to the right or by entering a new value.");
  timeEdit->setWhatsThis( wtstr );

115
116
  connect(timeEdit, &QTimeEdit::timeChanged, this, &Dtime::set_time);
  connect(cal, &KDatePicker::dateChanged, this, &Dtime::changeDate);
117

118
  connect(&internalTimer, &QTimer::timeout, this, &Dtime::timeout);
119
120
121
122

  kclock->setEnabled(false);

  //Timezone
123
  connect(tzonelist, &K4TimeZoneWidget::itemSelectionChanged, this, &Dtime::configChanged);
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
  tzonesearch->setTreeWidget(tzonelist);
}

void Dtime::currentZone()
{
    KTimeZone localZone = KSystemTimeZones::local();

    if (localZone.abbreviations().isEmpty()) {
        m_local->setText(i18nc("%1 is name of time zone", "Current local time zone: %1",
                              K4TimeZoneWidget::displayName(localZone)));
    } else {
        m_local->setText(i18nc("%1 is name of time zone, %2 is its abbreviation",
                               "Current local time zone: %1 (%2)",
                              K4TimeZoneWidget::displayName(localZone),
                              QString::fromUtf8(localZone.abbreviations().first())));
    }
}

void Dtime::serverTimeCheck() {
143
144
  // Enable time and date if the ntp utility is missing
  bool enabled = ntpUtility.isEmpty() || !setDateTimeAuto->isChecked();
145
146
147
148
149
150
151
  cal->setEnabled(enabled);
  timeEdit->setEnabled(enabled);
  //kclock->setEnabled(enabled);
}

void Dtime::findNTPutility(){
  QByteArray envpath = qgetenv("PATH");
Lukáš Tinkl's avatar
Lukáš Tinkl committed
152
  if (!envpath.isEmpty() && envpath.startsWith(':')) {
153
    envpath.remove(0, 1);
154
155
  }

156
  QStringList path = {"/sbin", "/usr/sbin"};
157
  if (!envpath.isEmpty()) {
158
    path += QFile::decodeName(envpath).split(QLatin1Char(':'));
159
  } else {
160
    path += {"/bin", "/usr/bin"};
161
162
  }

163
164
165
166
  const auto possible_ntputilities = {"ntpdate", "rdate"};
  for (const QString &possible_ntputility : possible_ntputilities) {
    auto ntpUtility = QStandardPaths::findExecutable(possible_ntputility, path);
    if (!ntpUtility.isEmpty()) {
167
      qDebug() << "ntpUtility = " << ntpUtility;
168
169
170
171
      return;
    }
  }

172
  qDebug() << "ntpUtility not found!";
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
}

void Dtime::set_time()
{
  if( ontimeout )
    return;

  internalTimer.stop();

  time = timeEdit->time();
  kclock->setTime( time );

  emit timeChanged( true );
}

void Dtime::changeDate(const QDate &d)
{
  date = d;
  emit timeChanged( true );
}

void Dtime::configChanged(){
  emit timeChanged( true );
}

void Dtime::load()
{
200
201
202
    QString currentTimeZone;

    if (m_haveTimedated) {
203
        OrgFreedesktopTimedate1Interface timeDatedIface(QStringLiteral("org.freedesktop.timedate1"), QStringLiteral("/org/freedesktop/timedate1"), QDBusConnection::systemBus());
204
205
206
207
208
209
210
211
212
213
        //the server list is not relevant for timesyncd, it fetches it from the network
        timeServerList->setVisible(false);
        timeServerLabel->setVisible(false);
        setDateTimeAuto->setEnabled(timeDatedIface.canNTP());
        setDateTimeAuto->setChecked(timeDatedIface.nTP());

        currentTimeZone = timeDatedIface.timezone();
    } else {
        // The config is actually written to the system config, but the user does not have any local config,
        // since there is nothing writing it.
214
        KConfig _config( QStringLiteral("kcmclockrc"), KConfig::NoGlobals );
215
216
217
218
219
220
221
222
223
224
225
226
227
228
        KConfigGroup config(&_config, "NTP");
        timeServerList->clear();
        timeServerList->addItems(config.readEntry("servers",
            i18n("Public Time Server (pool.ntp.org),\
        asia.pool.ntp.org,\
        europe.pool.ntp.org,\
        north-america.pool.ntp.org,\
        oceania.pool.ntp.org")).split(',', QString::SkipEmptyParts));
        setDateTimeAuto->setChecked(config.readEntry("enabled", false));

        if (ntpUtility.isEmpty()) {
            timeServerList->setEnabled(false);
        }
        currentTimeZone  = KSystemTimeZones::local().name();
229
230
  }

231
232
233
234
235
236
237
238
239
240
241
242
243
  // Reset to the current date and time
  time = QTime::currentTime();
  date = QDate::currentDate();
  cal->setDate(date);

  // start internal timer
  internalTimer.start( 1000 );

  timeout();

  //Timezone
  currentZone();

244
  tzonelist->setSelected(currentTimeZone, true);
Lukáš Tinkl's avatar
Lukáš Tinkl committed
245
  emit timeChanged(false);
246
247
}

David Edmundson's avatar
David Edmundson committed
248
249
250
251
252
253
254
255
256
257
258
QString Dtime::selectedTimeZone() const
{
    QStringList selectedZones(tzonelist->selection());
    if (!selectedZones.isEmpty()) {
        return selectedZones.first();
    }

    return QString();
}

QStringList Dtime::ntpServers() const
259
260
261
262
263
264
265
266
267
268
269
270
271
{
  // Save the order, but don't duplicate!
  QStringList list;
  if( timeServerList->count() != 0)
    list.append(timeServerList->currentText());
  for ( int i=0; i<timeServerList->count();i++ ) {
    QString text = timeServerList->itemText(i);
    if( !list.contains(text) )
      list.append(text);
    // Limit so errors can go away and not stored forever
    if( list.count() == 10)
      break;
  }
David Edmundson's avatar
David Edmundson committed
272
273
  return list;
}
274

David Edmundson's avatar
David Edmundson committed
275
276
277
278
bool Dtime::ntpEnabled() const
{
    return setDateTimeAuto->isChecked();
}
279

David Edmundson's avatar
David Edmundson committed
280
281
282
QDateTime Dtime::userTime() const
{
    return QDateTime(date, QTime(timeEdit->time()));
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
}

void Dtime::processHelperErrors( int code )
{
  if( code & ClockHelper::NTPError ) {
    KMessageBox::error( this, i18n("Unable to contact time server: %1.", timeServer) );
    setDateTimeAuto->setChecked( false );
  }
  if( code & ClockHelper::DateError ) {
    KMessageBox::error( this, i18n("Can not set date."));
  }
  if( code & ClockHelper::TimezoneError)
    KMessageBox::error( this, i18n("Error setting new time zone."),
                        i18n("Time zone Error"));
}

void Dtime::timeout()
{
  // get current time
  time = QTime::currentTime();

  ontimeout = true;
  timeEdit->setTime(time);
  ontimeout = false;

  kclock->setTime( time );
}

QString Dtime::quickHelp() const
{
  return i18n("<h1>Date & Time</h1> This system settings module can be used to set the system date and"
    " time. As these settings do not only affect you as a user, but rather the whole system, you"
    " can only change these settings when you start the System Settings as root. If you do not have"
    " the root password, but feel the system time should be corrected, please contact your system"
    " administrator.");
}

Kclock::Kclock(QWidget *parent)
    : QWidget(parent)
{
    m_theme = new Plasma::Svg(this);
324
    m_theme->setImagePath(QStringLiteral("widgets/clock"));
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
    m_theme->setContainsMultipleImages(true);
}

Kclock::~Kclock()
{
    delete m_theme;
}

void Kclock::showEvent( QShowEvent *event )
{
    setClockSize( size() );
    QWidget::showEvent( event );
}

void Kclock::resizeEvent( QResizeEvent * )
{
    setClockSize( size() );
}

void Kclock::setClockSize(const QSize &size)
{
    int dim = qMin(size.width(), size.height());
347
    QSize newSize = QSize(dim, dim) * devicePixelRatioF();
348
349
350
351
352

    if (newSize != m_faceCache.size()) {
        m_faceCache = QPixmap(newSize);
        m_handsCache = QPixmap(newSize);
        m_glassCache = QPixmap(newSize);
353
354
355
        m_faceCache.setDevicePixelRatio(devicePixelRatioF());
        m_handsCache.setDevicePixelRatio(devicePixelRatioF());
        m_glassCache.setDevicePixelRatio(devicePixelRatioF());
356

Marco Martin's avatar
Marco Martin committed
357
        m_theme->resize(QSize(dim, dim));
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
        m_repaintCache = RepaintAll;
    }
}

void Kclock::setTime(const QTime &time)
{
    if (time.minute() != this->time.minute() || time.hour() != this->time.hour()) {
        if (m_repaintCache == RepaintNone) {
            m_repaintCache = RepaintHands;
        }
    }
    this->time = time;
    update();
}

void Kclock::drawHand(QPainter *p, const QRect &rect, const qreal verticalTranslation, const qreal rotation, const QString &handName)
{
    // this code assumes the following conventions in the svg file:
    // - the _vertical_ position of the hands should be set with respect to the center of the face
    // - the _horizontal_ position of the hands does not matter
    // - the _shadow_ elements should have the same vertical position as their _hand_ element counterpart

    QRectF elementRect;
    QString name = handName + "HandShadow";
    if (m_theme->hasElement(name)) {
        p->save();

        elementRect = m_theme->elementRect(name);
        if( rect.height() < 64 )
            elementRect.setWidth( elementRect.width() * 2.5 );
        static const QPoint offset = QPoint(2, 3);

        p->translate(rect.x() + (rect.width() / 2) + offset.x(), rect.y() + (rect.height() / 2) + offset.y());
        p->rotate(rotation);
        p->translate(-elementRect.width()/2, elementRect.y()-verticalTranslation);
        m_theme->paint(p, QRectF(QPointF(0, 0), elementRect.size()), name);

        p->restore();
    }

    p->save();

    name = handName + "Hand";
    elementRect = m_theme->elementRect(name);
    if (rect.height() < 64) {
        elementRect.setWidth(elementRect.width() * 2.5);
    }

    p->translate(rect.x() + rect.width()/2, rect.y() + rect.height()/2);
    p->rotate(rotation);
    p->translate(-elementRect.width()/2, elementRect.y()-verticalTranslation);
    m_theme->paint(p, QRectF(QPointF(0, 0), elementRect.size()), name);

    p->restore();
}

void Kclock::paintInterface(QPainter *p, const QRect &rect)
{
    const bool m_showSecondHand = true;

    // compute hand angles
    const qreal minutes = 6.0 * time.minute() - 180;
    const qreal hours = 30.0 * time.hour() - 180 +
            ((time.minute() / 59.0) * 30.0);
    qreal seconds = 0;
    if (m_showSecondHand) {
        static const double anglePerSec = 6;
        seconds = anglePerSec * time.second() - 180;
    }

    // paint face and glass cache
    QRect faceRect = m_faceCache.rect();
430
    QRect targetRect = QRect(QPoint(0, 0), QSize(m_faceCache.width() / devicePixelRatioF(), m_faceCache.height() / devicePixelRatioF()));
Marco Martin's avatar
Marco Martin committed
431

432
433
434
435
436
437
438
439
440
    if (m_repaintCache == RepaintAll) {
        m_faceCache.fill(Qt::transparent);
        m_glassCache.fill(Qt::transparent);

        QPainter facePainter(&m_faceCache);
        QPainter glassPainter(&m_glassCache);
        facePainter.setRenderHint(QPainter::SmoothPixmapTransform);
        glassPainter.setRenderHint(QPainter::SmoothPixmapTransform);

441
        m_theme->paint(&facePainter, targetRect, QStringLiteral("ClockFace"));
442
443

        glassPainter.save();
444
        QRectF elementRect = QRectF(QPointF(0, 0), m_theme->elementSize(QStringLiteral("HandCenterScrew")));
445
        glassPainter.translate(faceRect.width() / (2 * devicePixelRatioF()) - elementRect.width() / 2, faceRect.height() / (2 * devicePixelRatioF()) - elementRect.height() / 2);
446
        m_theme->paint(&glassPainter, elementRect, QStringLiteral("HandCenterScrew"));
447
448
        glassPainter.restore();

449
        m_theme->paint(&glassPainter, targetRect, QStringLiteral("Glass"));
450
451

        // get vertical translation, see drawHand() for more details
452
        m_verticalTranslation = m_theme->elementRect(QStringLiteral("ClockFace")).center().y();
453
454
455
456
457
458
459
    }

    // paint hour and minute hands cache
    if (m_repaintCache == RepaintHands || m_repaintCache == RepaintAll) {
        m_handsCache.fill(Qt::transparent);

        QPainter handsPainter(&m_handsCache);
Marco Martin's avatar
Marco Martin committed
460
        handsPainter.drawPixmap(targetRect, m_faceCache, faceRect);
461
462
        handsPainter.setRenderHint(QPainter::SmoothPixmapTransform);

463
464
        drawHand(&handsPainter, targetRect, m_verticalTranslation, hours, QStringLiteral("Hour"));
        drawHand(&handsPainter, targetRect, m_verticalTranslation, minutes, QStringLiteral("Minute"));
465
466
467
468
469
470
471
472
473
474
475
476
477
    }

    // reset repaint cache flag
    m_repaintCache = RepaintNone;

    // paint caches and second hand
    if (targetRect.width() < rect.width()) {
        targetRect.moveLeft((rect.width() - targetRect.width()) / 2);
    }

    p->drawPixmap(targetRect, m_handsCache, faceRect);
    if (m_showSecondHand) {
        p->setRenderHint(QPainter::SmoothPixmapTransform);
478
        drawHand(p, targetRect, m_verticalTranslation, seconds, QStringLiteral("Second"));
479
480
481
482
483
484
485
486
487
488
489
490
    }
    p->drawPixmap(targetRect, m_glassCache, faceRect);
}

void Kclock::paintEvent( QPaintEvent * )
{
  QPainter paint(this);

  paint.setRenderHint(QPainter::Antialiasing);
  paintInterface(&paint, rect());
}