Git commit 2ea303e708630e63bdef94ebb4e93f9514a42657 by Konstantinos Smanis. Committed on 27/10/2013 at 13:38. Pushed by ksmanis into branch 'master'. Use the Construct-On-First-Use idiom for built-in paths. To avoid a static de-initialization order fiasco [1] don't use these paths in static destructors. This is not a library so it's pretty safe. [1] http://www.parashift.com/c++-faq-lite/static-init-order.html M +48 -9 config.h.cmake M +17 -17 src/helper/helper.cpp M +10 -10 src/kcm_grub2.cpp http://commits.kde.org/kcm-grub2/2ea303e708630e63bdef94ebb4e93f9514a42657 diff --git a/config.h.cmake b/config.h.cmake index f5db630..24d2b68 100644 --- a/config.h.cmake +++ b/config.h.cmake @@ -9,14 +9,53 @@ #define HAVE_QPACKAGEKIT @HAVE_QPACKAGEKIT@ #define QAPT_VERSION_MAJOR @QAPT_VERSION_MAJOR@ = -#define GRUB_INSTALL_EXE "@GRUB_INSTALL_EXE@" -#define GRUB_MKCONFIG_EXE "@GRUB_MKCONFIG_EXE@" -#define GRUB_PROBE_EXE "@GRUB_PROBE_EXE@" -#define GRUB_SET_DEFAULT_EXE "@GRUB_SET_DEFAULT_EXE@" -#define GRUB_MENU "@GRUB_MENU@" -#define GRUB_CONFIG "@GRUB_CONFIG@" -#define GRUB_ENV "@GRUB_ENV@" -#define GRUB_MEMTEST "@GRUB_MEMTEST@" -#define GRUB_LOCALE "@GRUB_LOCALE@" +//Qt +#include + +const QString & grubInstallExePath() +{ + static const QString str =3D QFile::decodeName("@GRUB_INSTALL_EXE@"); + return str; +} +const QString & grubMkconfigExePath() +{ + static const QString str =3D QFile::decodeName("@GRUB_MKCONFIG_EXE@"); + return str; +} +const QString & grubProbeExePath() +{ + static const QString str =3D QFile::decodeName("@GRUB_PROBE_EXE@"); + return str; +} +const QString & grubSetDefaultExePath() +{ + static const QString str =3D QFile::decodeName("@GRUB_SET_DEFAULT_EXE@= "); + return str; +} +const QString & grubMenuPath() +{ + static const QString str =3D QFile::decodeName("@GRUB_MENU@"); + return str; +} +const QString & grubConfigPath() +{ + static const QString str =3D QFile::decodeName("@GRUB_CONFIG@"); + return str; +} +const QString & grubEnvPath() +{ + static const QString str =3D QFile::decodeName("@GRUB_ENV@"); + return str; +} +const QString & grubMemtestPath() +{ + static const QString str =3D QFile::decodeName("@GRUB_MEMTEST@"); + return str; +} +const QString & grubLocalePath() +{ + static const QString str =3D QFile::decodeName("@GRUB_LOCALE@"); + return str; +} = #endif diff --git a/src/helper/helper.cpp b/src/helper/helper.cpp index a68c207..2581dff 100644 --- a/src/helper/helper.cpp +++ b/src/helper/helper.cpp @@ -70,9 +70,9 @@ ActionReply Helper::executeCommand(const QStringList &com= mand) } bool Helper::setLang(const QString &lang) { - QFile file(QString::fromUtf8(GRUB_MENU)); + QFile file(grubMenuPath()); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { - kError() << "Failed to open file for reading:" << GRUB_MENU; + kError() << "Failed to open file for reading:" << grubMenuPath(); kError() << "Error code:" << file.error(); kError() << "Error description:" << file.errorString(); return false; @@ -80,14 +80,14 @@ bool Helper::setLang(const QString &lang) QString fileContents =3D QString::fromUtf8(file.readAll().constData()); file.close(); if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { - kError() << "Failed to open file for writing:" << GRUB_MENU; + kError() << "Failed to open file for writing:" << grubMenuPath(); kError() << "Error code:" << file.error(); kError() << "Error description:" << file.errorString(); return false; } fileContents.replace(QRegExp(QLatin1String("(\\n\\s*set\\s+lang=3D)\\S= *\\n")), QString(QLatin1String("\\1%1\n")).arg(lang)); if (file.write(fileContents.toUtf8()) =3D=3D -1) { - kError() << "Failed to write data to file:" << GRUB_MENU; + kError() << "Failed to write data to file:" << grubMenuPath(); kError() << "Error code:" << file.error(); kError() << "Error description:" << file.errorString(); return false; @@ -100,7 +100,7 @@ ActionReply Helper::defaults(QVariantMap args) { Q_UNUSED(args) ActionReply reply; - QString configFileName =3D QString::fromUtf8(GRUB_CONFIG); + QString configFileName =3D grubConfigPath(); QString originalConfigFileName =3D configFileName + QLatin1String(".or= iginal"); = if (!QFile::exists(originalConfigFileName)) { @@ -145,7 +145,7 @@ ActionReply Helper::install(QVariantMap args) } = QStringList grub_installCommand; - grub_installCommand << QString::fromUtf8(GRUB_INSTALL_EXE) << QLatin1S= tring("--root-directory") << mountPoint; + grub_installCommand << grubInstallExePath() << QLatin1String("--root-d= irectory") << mountPoint; if (mbrInstall) { grub_installCommand << partition.remove(QRegExp(QLatin1String("\\d= +"))); } else { @@ -159,7 +159,7 @@ ActionReply Helper::load(QVariantMap args) LoadOperations operations =3D (LoadOperations)(args.value(QLatin1Strin= g("operations")).toInt()); = if (operations.testFlag(MenuFile)) { - QFile file(QString::fromUtf8(GRUB_MENU)); + QFile file(grubMenuPath()); bool ok =3D file.open(QIODevice::ReadOnly | QIODevice::Text); reply.addData(QLatin1String("menuSuccess"), ok); if (ok) { @@ -170,7 +170,7 @@ ActionReply Helper::load(QVariantMap args) } } if (operations.testFlag(ConfigurationFile)) { - QFile file(QString::fromUtf8(GRUB_CONFIG)); + QFile file(grubConfigPath()); bool ok =3D file.open(QIODevice::ReadOnly | QIODevice::Text); reply.addData(QLatin1String("configSuccess"), ok); if (ok) { @@ -181,7 +181,7 @@ ActionReply Helper::load(QVariantMap args) } } if (operations.testFlag(EnvironmentFile)) { - QFile file(QString::fromUtf8(GRUB_ENV)); + QFile file(grubEnvPath()); bool ok =3D file.open(QIODevice::ReadOnly | QIODevice::Text); reply.addData(QLatin1String("envSuccess"), ok); if (ok) { @@ -192,10 +192,10 @@ ActionReply Helper::load(QVariantMap args) } } if (operations.testFlag(MemtestFile)) { - bool memtest =3D QFile::exists(QString::fromUtf8(GRUB_MEMTEST)); + bool memtest =3D QFile::exists(grubMemtestPath()); reply.addData(QLatin1String("memtest"), memtest); if (memtest) { - reply.addData(QLatin1String("memtestOn"), (bool)(QFile::permis= sions(QString::fromUtf8(GRUB_MEMTEST)) & (QFile::ExeOwner | QFile::ExeGroup= | QFile::ExeOther))); + reply.addData(QLatin1String("memtestOn"), (bool)(QFile::permis= sions(grubMemtestPath()) & (QFile::ExeOwner | QFile::ExeGroup | QFile::ExeO= ther))); } } #if HAVE_HD @@ -215,14 +215,14 @@ ActionReply Helper::load(QVariantMap args) } #endif if (operations.testFlag(Locales)) { - reply.addData(QLatin1String("locales"), QDir(QString::fromUtf8(GRU= B_LOCALE)).entryList(QStringList() << QLatin1String("*.mo"), QDir::Files).r= eplaceInStrings(QRegExp(QLatin1String("\\.mo$")), QString())); + reply.addData(QLatin1String("locales"), QDir(grubLocalePath()).ent= ryList(QStringList() << QLatin1String("*.mo"), QDir::Files).replaceInString= s(QRegExp(QLatin1String("\\.mo$")), QString())); } return reply; } ActionReply Helper::save(QVariantMap args) { ActionReply reply; - QString configFileName =3D QString::fromUtf8(GRUB_CONFIG); + QString configFileName =3D grubConfigPath(); QByteArray rawConfigFileContents =3D args.value(QLatin1String("rawConf= igFileContents")).toByteArray(); QByteArray rawDefaultEntry =3D args.value(QLatin1String("rawDefaultEnt= ry")).toByteArray(); bool memtest =3D args.value(QLatin1String("memtest")).toBool(); @@ -240,19 +240,19 @@ ActionReply Helper::save(QVariantMap args) file.close(); = if (args.contains(QLatin1String("memtest"))) { - QFile::Permissions permissions =3D QFile::permissions(QString::fro= mUtf8(GRUB_MEMTEST)); + QFile::Permissions permissions =3D QFile::permissions(grubMemtestP= ath()); if (memtest) { permissions |=3D (QFile::ExeOwner | QFile::ExeUser | QFile::Ex= eGroup | QFile::ExeOther); } else { permissions &=3D ~(QFile::ExeOwner | QFile::ExeUser | QFile::E= xeGroup | QFile::ExeOther); } - QFile::setPermissions(QString::fromUtf8(GRUB_MEMTEST), permissions= ); + QFile::setPermissions(grubMemtestPath(), permissions); } = if (args.contains(QLatin1String("LANG"))) { qputenv("LANG", args.value(QLatin1String("LANG")).toByteArray()); } - ActionReply grub_mkconfigReply =3D executeCommand(QStringList() << QSt= ring::fromUtf8(GRUB_MKCONFIG_EXE) << QLatin1String("-o") << QString::fromUt= f8(GRUB_MENU)); + ActionReply grub_mkconfigReply =3D executeCommand(QStringList() << gru= bMkconfigExePath() << QLatin1String("-o") << grubMenuPath()); if (grub_mkconfigReply.failed()) { return grub_mkconfigReply; } @@ -263,7 +263,7 @@ ActionReply Helper::save(QVariantMap args) } } = - ActionReply grub_set_defaultReply =3D executeCommand(QStringList() << = QString::fromUtf8(GRUB_SET_DEFAULT_EXE) << QString::fromUtf8(rawDefaultEntr= y.constData())); + ActionReply grub_set_defaultReply =3D executeCommand(QStringList() << = grubSetDefaultExePath() << QString::fromUtf8(rawDefaultEntry.constData())); if (grub_set_defaultReply.failed()) { return grub_set_defaultReply; } diff --git a/src/kcm_grub2.cpp b/src/kcm_grub2.cpp index 5832960..0e99f13 100644 --- a/src/kcm_grub2.cpp +++ b/src/kcm_grub2.cpp @@ -982,24 +982,24 @@ void KCMGRUB2::readAll() QByteArray fileContents; LoadOperations operations =3D NoOperation; = - if (readFile(QString::fromUtf8(GRUB_MENU), fileContents)) { + if (readFile(grubMenuPath(), fileContents)) { parseEntries(QString::fromUtf8(fileContents.constData())); } else { operations |=3D MenuFile; } - if (readFile(QString::fromUtf8(GRUB_CONFIG), fileContents)) { + if (readFile(grubConfigPath(), fileContents)) { parseSettings(QString::fromUtf8(fileContents.constData())); } else { operations |=3D ConfigurationFile; } - if (readFile(QString::fromUtf8(GRUB_ENV), fileContents)) { + if (readFile(grubEnvPath(), fileContents)) { parseEnv(QString::fromUtf8(fileContents.constData())); } else { operations |=3D EnvironmentFile; } - if (QFile::exists(QString::fromUtf8(GRUB_MEMTEST))) { + if (QFile::exists(grubMemtestPath())) { m_memtest =3D true; - m_memtestOn =3D (bool)(QFile::permissions(QString::fromUtf8(GRUB_M= EMTEST)) & (QFile::ExeOwner | QFile::ExeGroup | QFile::ExeOther)); + m_memtestOn =3D (bool)(QFile::permissions(grubMemtestPath()) & (QF= ile::ExeOwner | QFile::ExeGroup | QFile::ExeOther)); } else { operations |=3D MemtestFile; } @@ -1008,8 +1008,8 @@ void KCMGRUB2::readAll() operations |=3D Vbe; } #endif - if (QFileInfo(QString::fromUtf8(GRUB_LOCALE)).isReadable()) { - m_locales =3D QDir(QString::fromUtf8(GRUB_LOCALE)).entryList(QStri= ngList() << QLatin1String("*.mo"), QDir::Files).replaceInStrings(QRegExp(QL= atin1String("\\.mo$")), QString()); + if (QFileInfo(grubLocalePath()).isReadable()) { + m_locales =3D QDir(grubLocalePath()).entryList(QStringList() << QL= atin1String("*.mo"), QDir::Files).replaceInStrings(QRegExp(QLatin1String("\= \.mo$")), QString()); } else { operations |=3D Locales; } @@ -1036,7 +1036,7 @@ void KCMGRUB2::readAll() if (reply.data().value(QLatin1String("menuSuccess")).toBool())= { parseEntries(QString::fromUtf8(reply.data().value(QLatin1S= tring("menuContents")).toByteArray().constData())); } else { - kError() << "Helper failed to read file:" << GRUB_MENU; + kError() << "Helper failed to read file:" << grubMenuPath(= ); kError() << "Error code:" << reply.data().value(QLatin1Str= ing("menuError")).toInt(); kError() << "Error description:" << reply.data().value(QLa= tin1String("menuErrorString")).toString(); } @@ -1045,7 +1045,7 @@ void KCMGRUB2::readAll() if (reply.data().value(QLatin1String("configSuccess")).toBool(= )) { parseSettings(QString::fromUtf8(reply.data().value(QLatin1= String("configContents")).toByteArray().constData())); } else { - kError() << "Helper failed to read file:" << GRUB_CONFIG; + kError() << "Helper failed to read file:" << grubConfigPat= h(); kError() << "Error code:" << reply.data().value(QLatin1Str= ing("configError")).toInt(); kError() << "Error description:" << reply.data().value(QLa= tin1String("configErrorString")).toString(); } @@ -1054,7 +1054,7 @@ void KCMGRUB2::readAll() if (reply.data().value(QLatin1String("envSuccess")).toBool()) { parseEnv(QString::fromUtf8(reply.data().value(QLatin1Strin= g("envContents")).toByteArray().constData())); } else { - kError() << "Helper failed to read file:" << GRUB_ENV; + kError() << "Helper failed to read file:" << grubEnvPath(); kError() << "Error code:" << reply.data().value(QLatin1Str= ing("envError")).toInt(); kError() << "Error description:" << reply.data().value(QLa= tin1String("envErrorString")).toString(); }