kcontrol: return when error occurs in ClockHelper::tz()

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2020-12-28 03:11:11 +00:00
parent 0b66b72b98
commit 75d6a1da15
2 changed files with 50 additions and 52 deletions

View file

@ -58,10 +58,8 @@ static QString findNtpUtility()
return QString();
}
int ClockHelper::ntp( const QStringList& ntpServers, bool ntpEnabled )
ClockHelper::CH_Error ClockHelper::ntp( const QStringList& ntpServers, bool ntpEnabled )
{
int ret = 0;
// write to the system config file
QFile config_file(KDE_CONFDIR "/kcmclockrc");
if(!config_file.exists()) {
@ -86,16 +84,16 @@ int ClockHelper::ntp( const QStringList& ntpServers, bool ntpEnabled )
}
if ( !QProcess::execute(ntpUtility, QStringList() << timeServer) ) {
ret |= NTPError;
return NTPError;
}
} else if( ntpEnabled ) {
ret |= NTPError;
return NTPError;
}
return ret;
return NoError;
}
int ClockHelper::date( const QString& newdate, const QString& olddate )
ClockHelper::CH_Error ClockHelper::date( const QString& newdate, const QString& olddate )
{
struct timeval tv;
@ -109,58 +107,58 @@ int ClockHelper::date( const QString& newdate, const QString& olddate )
if (!hwclock.isEmpty()) {
QProcess::execute(hwclock, QStringList() << "--systohc");
}
return 0;
return NoError;
}
int ClockHelper::tz( const QString& selectedzone )
ClockHelper::CH_Error ClockHelper::tz( const QString& selectedzone )
{
int ret = 0;
//only allow letters, numbers hyphen underscore plus and forward slash
//allowed pattern taken from time-util.c in systemd
if (!QRegExp("[a-zA-Z0-9-_+/]*").exactMatch(selectedzone)) {
return ret;
return TimezoneError;
}
// from ktimezoned
QString ZONE_INFO_DIR;
if (QDir("/usr/share/zoneinfo").exists()) {
ZONE_INFO_DIR = "/usr/share/zoneinfo/";
} else if (QDir("/usr/lib/zoneinfo").exists()) {
ZONE_INFO_DIR = "/usr/lib/zoneinfo/";
} else if (QDir("/share/zoneinfo").exists()) {
ZONE_INFO_DIR = "/share/zoneinfo/";
} else if (QDir("/lib/zoneinfo").exists()) {
ZONE_INFO_DIR = "/lib/zoneinfo/";
} else {
// /usr is kind of standard
ZONE_INFO_DIR = "/usr/share/zoneinfo/";
}
QString tz = ZONE_INFO_DIR + selectedzone;
// from ktimezoned
QString ZONE_INFO_DIR;
QFile f("/etc/localtime");
if (f.exists() && !f.remove()) {
ret |= TimezoneError;
}
if (!QFile::link(tz, "/etc/localtime")) {
ret |= TimezoneError;
}
if (QDir("/usr/share/zoneinfo").exists()) {
ZONE_INFO_DIR = "/usr/share/zoneinfo/";
} else if (QDir("/usr/lib/zoneinfo").exists()) {
ZONE_INFO_DIR = "/usr/lib/zoneinfo/";
} else if (QDir("/share/zoneinfo").exists()) {
ZONE_INFO_DIR = "/share/zoneinfo/";
} else if (QDir("/lib/zoneinfo").exists()) {
ZONE_INFO_DIR = "/lib/zoneinfo/";
} else {
// /usr is kind of standard
ZONE_INFO_DIR = "/usr/share/zoneinfo/";
}
QString tz = ZONE_INFO_DIR + selectedzone;
QString val = ':' + tz;
QFile f("/etc/localtime");
if (f.exists() && !f.remove()) {
return TimezoneError;
}
setenv("TZ", val.toAscii(), 1);
tzset();
if (!QFile::link(tz, "/etc/localtime")) {
return TimezoneError;
}
return ret;
QString val = ':' + tz;
setenv("TZ", val.toAscii(), 1);
tzset();
return NoError;
}
int ClockHelper::tzreset()
ClockHelper::CH_Error ClockHelper::tzreset()
{
unlink( "/etc/localtime" );
setenv("TZ", "", 1);
tzset();
return 0;
return NoError;
}
ActionReply ClockHelper::save(const QVariantMap &args)
@ -172,7 +170,7 @@ ActionReply ClockHelper::save(const QVariantMap &args)
KComponentData data( "kcmdatetimehelper" );
int ret = 0; // error code
int ret = NoError; // error code
// The order here is important
if( _ntp )
ret |= ntp( args.value("ntpServers").toStringList(), args.value("ntpEnabled").toBool());
@ -183,7 +181,7 @@ ActionReply ClockHelper::save(const QVariantMap &args)
if( _tzreset )
ret |= tzreset();
if (ret == 0) {
if (ret == NoError) {
return ActionReply::SuccessReply;
} else {
ActionReply reply(ActionReply::HelperError);

View file

@ -30,22 +30,22 @@ class ClockHelper : public QObject
Q_OBJECT
public:
enum
{
CallError = 1 << 0,
TimezoneError = 1 << 1,
NTPError = 1 << 2,
DateError = 1 << 3
enum CH_Error {
NoError = 0,
CallError = 1,
TimezoneError = 2,
NTPError = 3,
DateError = 4
};
public slots:
ActionReply save(const QVariantMap &map);
private:
int ntp(const QStringList& ntpServers, bool ntpEnabled);
int date(const QString& newdate, const QString& olddate);
int tz(const QString& selectedzone);
int tzreset();
CH_Error ntp(const QStringList& ntpServers, bool ntpEnabled);
CH_Error date(const QString& newdate, const QString& olddate);
CH_Error tz(const QString& selectedzone);
CH_Error tzreset();
};
#endif // CLOCK_HELPER_H