Skip to content

Commit

Permalink
Try to handle timezones with timedatectl provided by systemd instead …
Browse files Browse the repository at this point in the history
…of liboobs.
  • Loading branch information
PCMan committed Jun 18, 2016
1 parent 59338dd commit ff6c8f1
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 5 deletions.
2 changes: 2 additions & 0 deletions lxqt-admin-time/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ set ( lxqt-admin-time_HDRS
timeadmindialog.h
datetime.h
timezone.h
timedatectl.h
)

set ( lxqt-admin-time_SRCS
main.cpp
timeadmindialog.cpp
datetime.cpp
timezone.cpp
timedatectl.cpp
)

set ( lxqt-admin-time_MOCS
Expand Down
13 changes: 8 additions & 5 deletions lxqt-admin-time/timeadmindialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
#include <QMessageBox>
#include <QDateTime>
#include <QMap>
#include <QDebug>

#include "datetime.h"
#include "timezone.h"


#define ZONETAB_PATH "/usr/share/zoneinfo/zone.tab"

TimeAdminDialog::TimeAdminDialog(QWidget *parent):
Expand Down Expand Up @@ -107,6 +107,8 @@ void TimeAdminDialog::closeEvent(QCloseEvent *event)

void TimeAdminDialog::loadTimeZones(QStringList & timeZones, QString & currentTimezone)
{
currentTimezone = mTimeDateCtl.timeZone();

timeZones.clear();
QFile file(ZONETAB_PATH);
if(file.open(QIODevice::ReadOnly))
Expand All @@ -124,17 +126,18 @@ void TimeAdminDialog::loadTimeZones(QStringList & timeZones, QString & currentTi
}
file.close();
}
currentTimezone = QString::fromLatin1(oobs_time_config_get_timezone(mTimeConfig));
}



void TimeAdminDialog::saveChangesToSystem()
{
QByteArray timeZone = mTimezoneWidget->timezone().toLatin1();
QString timeZone = mTimezoneWidget->timezone();
// FIXME: currently timezone settings does not work. is this a bug of system-tools-backend?
if(!timeZone.isEmpty() && mWidgetsModified.testFlag(M_TIMEZONE))
oobs_time_config_set_timezone(mTimeConfig, timeZone.constData());
if(!timeZone.isEmpty() && mWidgetsModified.testFlag(M_TIMEZONE)) {
mTimeDateCtl.setTimeZone(timeZone);
mTimeDateCtl.commit();
}

if(mWidgetsModified.testFlag(M_TIMEDATE))
{
Expand Down
3 changes: 3 additions & 0 deletions lxqt-admin-time/timeadmindialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include <oobs/oobs-timeconfig.h>
#include <oobs/oobs-ntpconfig.h>
#include <oobs/oobs-ntpserver.h>
#include "timedatectl.h"


class DateTime;
class Timezone;
Expand Down Expand Up @@ -59,6 +61,7 @@ private Q_SLOTS:
void showChangedStar();

private:
TimeDateCtl mTimeDateCtl;
OobsTimeConfig* mTimeConfig;
DateTime * mDateTimeWidget;
Timezone * mTimezoneWidget;
Expand Down
35 changes: 35 additions & 0 deletions lxqt-admin-time/timedatectl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "timedatectl.h"
#include <QProcess>
#include <QDebug>

TimeDateCtl::TimeDateCtl()
{
QProcess timedatectl;
timedatectl.start(QStringLiteral("timedatectl"));
timedatectl.waitForFinished();
while(!timedatectl.atEnd()) {
QString line = timedatectl.readLine().trimmed();
int findTZ = line.indexOf(QLatin1String("Time zone:"));
if(findTZ != -1) {
findTZ += 10;
while(line[findTZ] == ' ')
++findTZ;
int space = line.indexOf(' ', findTZ);
mTimeZone = line.mid(findTZ, space - findTZ);
break;
}
}
timedatectl.close();
}

bool TimeDateCtl::commit()
{
QProcess timedatectl;
QStringList args;
if(mTimeZoneChanged) {
args << "set-timezone" << mTimeZone;
}
timedatectl.start(QStringLiteral("timedatectl"), args);
timedatectl.waitForFinished();
return timedatectl.exitCode() == 0;
}
36 changes: 36 additions & 0 deletions lxqt-admin-time/timedatectl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef TIMEDATECTL_H
#define TIMEDATECTL_H

#include <QString>
#include <QDateTime>

class TimeDateCtl
{
public:
explicit TimeDateCtl();

QString timeZone() const {
return mTimeZone;
}

void setTimeZone(QString timeZone) {
mTimeZone = timeZone;
mTimeZoneChanged = true;
}

void setDateTime(QDateTime dateTime) {
mDateTime = dateTime;
mTimeChanged = true;
}

// really commit the changes to the system
bool commit();

private:
bool mTimeZoneChanged;
QString mTimeZone;
bool mTimeChanged;
QDateTime mDateTime;
};

#endif // TIMEDATECTL_H

0 comments on commit ff6c8f1

Please sign in to comment.