kcontrol: use message widget to warn that day and time cannot be changed in clock KCM

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2024-04-06 11:12:31 +03:00
parent 1f28be5d1c
commit cc1afece41
2 changed files with 29 additions and 12 deletions

View file

@ -52,6 +52,8 @@ K_EXPORT_PLUGIN(KCMClockFactory("kcmclock", "kcmclock"))
KCMClock::KCMClock(QWidget *parent, const QVariantList &args) KCMClock::KCMClock(QWidget *parent, const QVariantList &args)
: KCModule(KCMClockFactory::componentData(), parent), : KCModule(KCMClockFactory::componentData(), parent),
m_layout(nullptr), m_layout(nullptr),
m_canchangeclock(false),
m_messagewidget(nullptr),
m_datetimebox(nullptr), m_datetimebox(nullptr),
m_datetimelayout(nullptr), m_datetimelayout(nullptr),
m_timeedit(nullptr), m_timeedit(nullptr),
@ -82,6 +84,14 @@ KCMClock::KCMClock(QWidget *parent, const QVariantList &args)
m_layout = new QVBoxLayout(this); m_layout = new QVBoxLayout(this);
setLayout(m_layout); setLayout(m_layout);
m_canchangeclock = kCanChangeClock();
m_messagewidget = new KMessageWidget(this);
m_messagewidget->setMessageType(KMessageWidget::Warning);
m_messagewidget->setCloseButtonVisible(false);
m_messagewidget->setText(i18n("Neither 'hwclock' nor 'timedatectl' utility found, setting the date and time is not possible."));
m_messagewidget->setVisible(!m_canchangeclock);
m_layout->addWidget(m_messagewidget);
m_datetimebox = new QGroupBox(this); m_datetimebox = new QGroupBox(this);
m_datetimebox->setTitle(i18n("Date and time")); m_datetimebox->setTitle(i18n("Date and time"));
m_datetimelayout = new QHBoxLayout(m_datetimebox); m_datetimelayout = new QHBoxLayout(m_datetimebox);
@ -146,17 +156,6 @@ KCMClock::KCMClock(QWidget *parent, const QVariantList &args)
m_timer = new QTimer(this); m_timer = new QTimer(this);
m_timer->setInterval(s_updatetime); m_timer->setInterval(s_updatetime);
connect(m_timer, SIGNAL(timeout()), this, SLOT(slotUpdate())); connect(m_timer, SIGNAL(timeout()), this, SLOT(slotUpdate()));
if (!KAuthorization::isAuthorized("org.kde.kcontrol.kcmclock")) {
setUseRootOnlyMessage(true);
setRootOnlyMessage(i18n("You are not allowed to save the configuration"));
setDisabled(true);
} else if (!kCanChangeClock()) {
setUseRootOnlyMessage(true);
setRootOnlyMessage(i18n("Neither 'hwclock' nor 'timedatectl' utility not found, setting the date is not possible"));
m_timeedit->setEnabled(false);
m_dateedit->setEnabled(false);
}
} }
void KCMClock::save() void KCMClock::save()
@ -167,7 +166,7 @@ void KCMClock::save()
m_datechanged = false; m_datechanged = false;
m_zonechanged = false; m_zonechanged = false;
QVariantMap savearguments; QVariantMap savearguments;
if (kCanChangeClock()) { if (m_canchangeclock) {
const QDateTime datetime = QDateTime(m_dateedit->date(), m_timeedit->time()); const QDateTime datetime = QDateTime(m_dateedit->date(), m_timeedit->time());
savearguments.insert("datetime", datetime.toString(s_dateformat)); savearguments.insert("datetime", datetime.toString(s_dateformat));
// qDebug() << Q_FUNC_INFO << datetime; // qDebug() << Q_FUNC_INFO << datetime;
@ -205,6 +204,15 @@ void KCMClock::save()
void KCMClock::load() void KCMClock::load()
{ {
setEnabled(false); setEnabled(false);
if (!KAuthorization::isAuthorized("org.kde.kcontrol.kcmclock")) {
setUseRootOnlyMessage(true);
setRootOnlyMessage(i18n("You are not allowed to save the configuration"));
setDisabled(true);
}
m_canchangeclock = kCanChangeClock();
m_messagewidget->setVisible(!m_canchangeclock);
m_timeedit->setEnabled(m_canchangeclock);
m_dateedit->setEnabled(m_canchangeclock);
m_timechanged = false; m_timechanged = false;
m_datechanged = false; m_datechanged = false;
m_zonechanged = false; m_zonechanged = false;
@ -214,6 +222,11 @@ void KCMClock::load()
setEnabled(true); setEnabled(true);
} }
void KCMClock::defaults()
{
// TODO: set timezone to UTC, check the time vs http://worldtimeapi.org/api/timezone/UTC
}
void KCMClock::slotUpdate() void KCMClock::slotUpdate()
{ {
const QDateTime now = QDateTime::currentDateTime(); const QDateTime now = QDateTime::currentDateTime();

View file

@ -27,6 +27,7 @@
#include <QTreeWidget> #include <QTreeWidget>
#include <QTimer> #include <QTimer>
#include <kcmodule.h> #include <kcmodule.h>
#include <kmessagewidget.h>
#include <ktreewidgetsearchline.h> #include <ktreewidgetsearchline.h>
class KCMClock : public KCModule class KCMClock : public KCModule
@ -39,6 +40,7 @@ public:
public Q_SLOTS: public Q_SLOTS:
void load() final; void load() final;
void save() final; void save() final;
void defaults() final;
private Q_SLOTS: private Q_SLOTS:
void slotUpdate(); void slotUpdate();
@ -48,6 +50,8 @@ private Q_SLOTS:
private: private:
QVBoxLayout* m_layout; QVBoxLayout* m_layout;
bool m_canchangeclock;
KMessageWidget* m_messagewidget;
QGroupBox* m_datetimebox; QGroupBox* m_datetimebox;
QHBoxLayout* m_datetimelayout; QHBoxLayout* m_datetimelayout;
QTimeEdit* m_timeedit; QTimeEdit* m_timeedit;