Commit 52bff7c3 authored by Lukáš Tinkl's avatar Lukáš Tinkl
Browse files

fix saving the timezone

also don't mark the module as changed right after the new timezone
gets loaded back
parent a9f14429
......@@ -144,13 +144,13 @@ void Dtime::serverTimeCheck() {
void Dtime::findNTPutility(){
QByteArray envpath = qgetenv("PATH");
if (!envpath.isEmpty() && envpath[0] == ':') {
if (!envpath.isEmpty() && envpath.startsWith(':')) {
envpath = envpath.mid(1);
}
QString path = "/sbin:/usr/sbin:";
if (!envpath.isEmpty()) {
path += QString::fromLocal8Bit(envpath);
path += QFile::decodeName(envpath);
} else {
path += QLatin1String("/bin:/usr/bin");
}
......@@ -218,6 +218,7 @@ oceania.pool.ntp.org")).split(',', QString::SkipEmptyParts));
// read the currently set time zone
tzonelist->setSelected(KSystemTimeZones::local().name(), true);
emit timeChanged(false);
}
void Dtime::save( QVariantMap& helperargs )
......@@ -261,12 +262,11 @@ void Dtime::save( QVariantMap& helperargs )
QStringList selectedZones(tzonelist->selection());
if (selectedZones.count() > 0) {
QString selectedzone(selectedZones[0]);
if (!selectedZones.isEmpty()) {
helperargs["tz"] = true;
helperargs["tzone"] = selectedzone;
helperargs["tzone"] = selectedZones.first();
} else {
helperargs["tzreset"] = true; // // make the helper reset the timezone
helperargs["tzreset"] = true; // make the helper reset the timezone
}
currentZone();
......
......@@ -55,41 +55,41 @@ static const QString exePath = QLatin1String("/usr/sbin:/usr/bin:/sbin:/bin");
int ClockHelper::ntp( const QStringList& ntpServers, bool ntpEnabled,
const QString& ntpUtility )
{
int ret = 0;
// write to the system config file
QFile config_file(KDE_CONFDIR "/kcmclockrc");
if(!config_file.exists()) {
config_file.open(QIODevice::WriteOnly);
config_file.close();
config_file.setPermissions(QFile::ReadOwner | QFile::WriteOwner | QFile::ReadGroup | QFile::ReadOther);
}
KConfig _config(config_file.fileName(), KConfig::SimpleConfig);
KConfigGroup config(&_config, "NTP");
config.writeEntry("servers", ntpServers );
config.writeEntry("enabled", ntpEnabled );
if ( ntpEnabled && !ntpUtility.isEmpty() ) {
// NTP Time setting
QString timeServer = ntpServers.first();
if( timeServer.indexOf( QRegExp(".*\\(.*\\)$") ) != -1 ) {
timeServer.replace( QRegExp(".*\\("), "" );
timeServer.replace( QRegExp("\\).*"), "" );
// Would this be better?: s/^.*\(([^)]*)\).*$/\1/
int ret = 0;
// write to the system config file
QFile config_file(KDE_CONFDIR "/kcmclockrc");
if(!config_file.exists()) {
config_file.open(QIODevice::WriteOnly);
config_file.close();
config_file.setPermissions(QFile::ReadOwner | QFile::WriteOwner | QFile::ReadGroup | QFile::ReadOther);
}
KConfig _config(config_file.fileName(), KConfig::SimpleConfig);
KConfigGroup config(&_config, "NTP");
config.writeEntry("servers", ntpServers );
config.writeEntry("enabled", ntpEnabled );
if ( ntpEnabled && !ntpUtility.isEmpty() ) {
// NTP Time setting
QString timeServer = ntpServers.first();
if( timeServer.indexOf( QRegExp(".*\\(.*\\)$") ) != -1 ) {
timeServer.replace( QRegExp(".*\\("), "" );
timeServer.replace( QRegExp("\\).*"), "" );
// Would this be better?: s/^.*\(([^)]*)\).*$/\1/
}
KProcess proc;
proc << ntpUtility << timeServer;
if ( proc.execute() != 0 ) {
ret |= NTPError;
} else {
toHwclock();
KProcess proc;
proc << ntpUtility << timeServer;
if ( proc.execute() != 0 ) {
ret |= NTPError;
} else {
toHwclock();
}
} else if( ntpEnabled ) {
ret |= NTPError;
}
} else if( ntpEnabled ) {
ret |= NTPError;
}
return ret;
return ret;
}
int ClockHelper::date( const QString& newdate, const QString& olddate )
......@@ -110,94 +110,94 @@ int ClockHelper::date( const QString& newdate, const QString& olddate )
int ClockHelper::tz( const QString& selectedzone )
{
int ret = 0;
QString val;
#if defined(USE_SOLARIS) // MARCO
KTemporaryFile tf;
tf.setPrefix("kde-tzone");
tf.open();
QTextStream ts(&tf);
KTemporaryFile tf;
tf.setPrefix("kde-tzone");
tf.open();
QTextStream ts(&tf);
QFile fTimezoneFile(INITFILE);
bool updatedFile = false;
QFile fTimezoneFile(INITFILE);
bool updatedFile = false;
if (fTimezoneFile.open(QIODevice::ReadOnly))
{
bool found = false;
if (fTimezoneFile.open(QIODevice::ReadOnly))
{
bool found = false;
QTextStream is(&fTimezoneFile);
QTextStream is(&fTimezoneFile);
for (QString line = is.readLine(); !line.isNull();
line = is.readLine())
for (QString line = is.readLine(); !line.isNull();
line = is.readLine())
{
if (line.find("TZ=") == 0)
{
if (line.find("TZ=") == 0)
{
ts << "TZ=" << selectedzone << endl;
found = true;
}
else
{
ts << line << endl;
}
ts << "TZ=" << selectedzone << endl;
found = true;
}
if (!found)
else
{
ts << "TZ=" << selectedzone << endl;
ts << line << endl;
}
}
updatedFile = true;
fTimezoneFile.close();
if (!found)
{
ts << "TZ=" << selectedzone << endl;
}
if (updatedFile)
updatedFile = true;
fTimezoneFile.close();
}
if (updatedFile)
{
ts.device()->reset();
fTimezoneFile.remove();
if (fTimezoneFile.open(QIODevice::WriteOnly | QIODevice::Truncate))
{
ts.device()->reset();
fTimezoneFile.remove();
QTextStream os(&fTimezoneFile);
if (fTimezoneFile.open(QIODevice::WriteOnly | QIODevice::Truncate))
for (QString line = ts->readLine(); !line.isNull();
line = ts->readLine())
{
QTextStream os(&fTimezoneFile);
for (QString line = ts->readLine(); !line.isNull();
line = ts->readLine())
{
os << line << endl;
}
fchmod(fTimezoneFile.handle(),
S_IXUSR | S_IRUSR | S_IRGRP | S_IXGRP |
S_IROTH | S_IXOTH);
fTimezoneFile.close();
os << line << endl;
}
fchmod(fTimezoneFile.handle(),
S_IXUSR | S_IRUSR | S_IRGRP | S_IXGRP |
S_IROTH | S_IXOTH);
fTimezoneFile.close();
}
}
QString val = selectedzone;
val = selectedzone;
#else
QString tz = "/usr/share/zoneinfo/" + selectedzone;
QString zic = KStandardDirs::findExe("zic", exePath);
if (!zic.isEmpty()) {
KProcess::execute(zic, QStringList() << "-l" << selectedzone);
} else if (!QFile::remove("/etc/localtime")) {
ret |= TimezoneError;
} else if (!QFile::copy(tz, "/etc/localtime")) {
ret |= TimezoneError;
}
QString tz = "/usr/share/zoneinfo/" + selectedzone;
if (QFile::exists(tz)) { // make sure the new TZ really exists
QFile::remove("/etc/localtime");
} else {
return TimezoneError;
}
QFile fTimezoneFile("/etc/timezone");
if (!QFile::link(tz, "/etc/localtime")) { // fail if we can't setup the new timezone
return TimezoneError;
}
if (fTimezoneFile.exists() && fTimezoneFile.open(QIODevice::WriteOnly | QIODevice::Truncate) ) {
QTextStream t(&fTimezoneFile);
t << selectedzone;
fTimezoneFile.close();
}
QFile fTimezoneFile("/etc/timezone");
QString val = ':' + tz;
if (fTimezoneFile.exists() && fTimezoneFile.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
QTextStream t(&fTimezoneFile);
t << selectedzone;
fTimezoneFile.close();
}
#endif // !USE_SOLARIS
val = ':' + selectedzone;
setenv("TZ", val.toAscii(), 1);
tzset();
setenv("TZ", val.toLocal8Bit().constData(), 1);
tzset();
return ret;
}
......@@ -205,50 +205,50 @@ int ClockHelper::tz( const QString& selectedzone )
int ClockHelper::tzreset()
{
#if !defined(USE_SOLARIS) // Do not update the System!
unlink( "/etc/timezone" );
unlink( "/etc/localtime" );
unlink( "/etc/timezone" );
unlink( "/etc/localtime" );
setenv("TZ", "", 1);
tzset();
setenv("TZ", "", 1);
tzset();
#endif // !USE SOLARIS
return 0;
}
void ClockHelper::toHwclock()
{
QString hwclock = KStandardDirs::findExe("hwclock", exePath);
if (!hwclock.isEmpty()) {
KProcess::execute(hwclock, QStringList() << "--systohc");
}
QString hwclock = KStandardDirs::findExe("hwclock", exePath);
if (!hwclock.isEmpty()) {
KProcess::execute(hwclock, QStringList() << "--systohc");
}
}
ActionReply ClockHelper::save(const QVariantMap &args)
{
bool _ntp = args.value("ntp").toBool();
bool _date = args.value("date").toBool();
bool _tz = args.value("tz").toBool();
bool _tzreset = args.value("tzreset").toBool();
KComponentData data( "kcmdatetimehelper" );
int ret = 0; // error code
// The order here is important
if( _ntp )
ret |= ntp( args.value("ntpServers").toStringList(), args.value("ntpEnabled").toBool(), args.value("ntpUtility").toString() );
if( _date )
ret |= date( args.value("newdate").toString(), args.value("olddate").toString() );
if( _tz )
ret |= tz( args.value("tzone").toString() );
if( _tzreset )
ret |= tzreset();
if (ret == 0) {
return ActionReply::SuccessReply();
} else {
ActionReply reply(ActionReply::HelperErrorReply());
reply.setErrorCode(static_cast<ActionReply::Error>(ret));
return reply;
}
bool _ntp = args.value("ntp").toBool();
bool _date = args.value("date").toBool();
bool _tz = args.value("tz").toBool();
bool _tzreset = args.value("tzreset").toBool();
KComponentData data( "kcmdatetimehelper" );
int ret = 0; // error code
// The order here is important
if( _ntp )
ret |= ntp( args.value("ntpServers").toStringList(), args.value("ntpEnabled").toBool(), args.value("ntpUtility").toString() );
if( _date )
ret |= date( args.value("newdate").toString(), args.value("olddate").toString() );
if( _tz )
ret |= tz( args.value("tzone").toString() );
if( _tzreset )
ret |= tzreset();
if (ret == 0) {
return ActionReply::SuccessReply();
} else {
ActionReply reply(ActionReply::HelperErrorReply());
reply.setErrorCode(static_cast<ActionReply::Error>(ret));
return reply;
}
}
KAUTH_HELPER_MAIN("org.kde.kcontrol.kcmclock", ClockHelper)
......@@ -89,7 +89,7 @@ void KclockModule::save()
ExecuteJob *job = action.execute();
if (!job->exec()) {
KMessageBox::error(this, i18n("Unable to authenticate/execute the action: %1, %2", job->error(), job->errorString()));
KMessageBox::error(this, i18n("Unable to authenticate/execute the action: %1, %2", job->error(), job->errorString()));
}
else {
QDBusMessage msg = QDBusMessage::createSignal("/org/kde/kcmshell_clock", "org.kde.kcmshell_clock", "clockUpdated");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment