[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [kcm-grub2] /: Use the Construct-On-First-Use idiom for built-in paths.
From:       Konstantinos Smanis <konstantinos.smanis () gmail ! com>
Date:       2013-10-27 13:41:41
Message-ID: E1VaQb7-0003Pe-8C () scm ! kde ! org
[Download RAW message or body]

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 <QFile>
+
+const QString & grubInstallExePath()
+{
+    static const QString str = QFile::decodeName("@GRUB_INSTALL_EXE@");
+    return str;
+}
+const QString & grubMkconfigExePath()
+{
+    static const QString str = QFile::decodeName("@GRUB_MKCONFIG_EXE@");
+    return str;
+}
+const QString & grubProbeExePath()
+{
+    static const QString str = QFile::decodeName("@GRUB_PROBE_EXE@");
+    return str;
+}
+const QString & grubSetDefaultExePath()
+{
+    static const QString str = QFile::decodeName("@GRUB_SET_DEFAULT_EXE@");
+    return str;
+}
+const QString & grubMenuPath()
+{
+    static const QString str = QFile::decodeName("@GRUB_MENU@");
+    return str;
+}
+const QString & grubConfigPath()
+{
+    static const QString str = QFile::decodeName("@GRUB_CONFIG@");
+    return str;
+}
+const QString & grubEnvPath()
+{
+    static const QString str = QFile::decodeName("@GRUB_ENV@");
+    return str;
+}
+const QString & grubMemtestPath()
+{
+    static const QString str = QFile::decodeName("@GRUB_MEMTEST@");
+    return str;
+}
+const QString & grubLocalePath()
+{
+    static const QString str = 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 &command)
 }
 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 = 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=)\\S*\\n")), \
QString(QLatin1String("\\1%1\n")).arg(lang));  if (file.write(fileContents.toUtf8()) \
                == -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 = QString::fromUtf8(GRUB_CONFIG);
+    QString configFileName = grubConfigPath();
     QString originalConfigFileName = configFileName + QLatin1String(".original");
 
     if (!QFile::exists(originalConfigFileName)) {
@@ -145,7 +145,7 @@ ActionReply Helper::install(QVariantMap args)
     }
 
     QStringList grub_installCommand;
-    grub_installCommand << QString::fromUtf8(GRUB_INSTALL_EXE) << \
QLatin1String("--root-directory") << mountPoint; +    grub_installCommand << \
grubInstallExePath() << QLatin1String("--root-directory") << mountPoint;  if \
                (mbrInstall) {
         grub_installCommand << partition.remove(QRegExp(QLatin1String("\\d+")));
     } else {
@@ -159,7 +159,7 @@ ActionReply Helper::load(QVariantMap args)
     LoadOperations operations = \
(LoadOperations)(args.value(QLatin1String("operations")).toInt());  
     if (operations.testFlag(MenuFile)) {
-        QFile file(QString::fromUtf8(GRUB_MENU));
+        QFile file(grubMenuPath());
         bool ok = 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 = 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 = 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 = QFile::exists(QString::fromUtf8(GRUB_MEMTEST));
+        bool memtest = QFile::exists(grubMemtestPath());
         reply.addData(QLatin1String("memtest"), memtest);
         if (memtest) {
-            reply.addData(QLatin1String("memtestOn"), \
(bool)(QFile::permissions(QString::fromUtf8(GRUB_MEMTEST)) & (QFile::ExeOwner | \
QFile::ExeGroup | QFile::ExeOther))); +            \
reply.addData(QLatin1String("memtestOn"), \
(bool)(QFile::permissions(grubMemtestPath()) & (QFile::ExeOwner | QFile::ExeGroup | \
QFile::ExeOther)));  }
     }
 #if HAVE_HD
@@ -215,14 +215,14 @@ ActionReply Helper::load(QVariantMap args)
     }
 #endif
     if (operations.testFlag(Locales)) {
-        reply.addData(QLatin1String("locales"), \
QDir(QString::fromUtf8(GRUB_LOCALE)).entryList(QStringList() << \
QLatin1String("*.mo"), \
QDir::Files).replaceInStrings(QRegExp(QLatin1String("\\.mo$")), QString())); +        \
reply.addData(QLatin1String("locales"), \
QDir(grubLocalePath()).entryList(QStringList() << QLatin1String("*.mo"), \
QDir::Files).replaceInStrings(QRegExp(QLatin1String("\\.mo$")), QString()));  }
     return reply;
 }
 ActionReply Helper::save(QVariantMap args)
 {
     ActionReply reply;
-    QString configFileName = QString::fromUtf8(GRUB_CONFIG);
+    QString configFileName = grubConfigPath();
     QByteArray rawConfigFileContents = \
                args.value(QLatin1String("rawConfigFileContents")).toByteArray();
     QByteArray rawDefaultEntry = \
args.value(QLatin1String("rawDefaultEntry")).toByteArray();  bool memtest = \
args.value(QLatin1String("memtest")).toBool(); @@ -240,19 +240,19 @@ ActionReply \
Helper::save(QVariantMap args)  file.close();
 
     if (args.contains(QLatin1String("memtest"))) {
-        QFile::Permissions permissions = \
QFile::permissions(QString::fromUtf8(GRUB_MEMTEST)); +        QFile::Permissions \
permissions = QFile::permissions(grubMemtestPath());  if (memtest) {
             permissions |= (QFile::ExeOwner | QFile::ExeUser | QFile::ExeGroup | \
QFile::ExeOther);  } else {
             permissions &= ~(QFile::ExeOwner | QFile::ExeUser | QFile::ExeGroup | \
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 = executeCommand(QStringList() << \
QString::fromUtf8(GRUB_MKCONFIG_EXE) << QLatin1String("-o") << \
QString::fromUtf8(GRUB_MENU)); +    ActionReply grub_mkconfigReply = \
executeCommand(QStringList() << grubMkconfigExePath() << QLatin1String("-o") << \
grubMenuPath());  if (grub_mkconfigReply.failed()) {
         return grub_mkconfigReply;
     }
@@ -263,7 +263,7 @@ ActionReply Helper::save(QVariantMap args)
         }
     }
 
-    ActionReply grub_set_defaultReply = executeCommand(QStringList() << \
QString::fromUtf8(GRUB_SET_DEFAULT_EXE) << \
QString::fromUtf8(rawDefaultEntry.constData())); +    ActionReply \
grub_set_defaultReply = 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 = NoOperation;
 
-    if (readFile(QString::fromUtf8(GRUB_MENU), fileContents)) {
+    if (readFile(grubMenuPath(), fileContents)) {
         parseEntries(QString::fromUtf8(fileContents.constData()));
     } else {
         operations |= MenuFile;
     }
-    if (readFile(QString::fromUtf8(GRUB_CONFIG), fileContents)) {
+    if (readFile(grubConfigPath(), fileContents)) {
         parseSettings(QString::fromUtf8(fileContents.constData()));
     } else {
         operations |= ConfigurationFile;
     }
-    if (readFile(QString::fromUtf8(GRUB_ENV), fileContents)) {
+    if (readFile(grubEnvPath(), fileContents)) {
         parseEnv(QString::fromUtf8(fileContents.constData()));
     } else {
         operations |= EnvironmentFile;
     }
-    if (QFile::exists(QString::fromUtf8(GRUB_MEMTEST))) {
+    if (QFile::exists(grubMemtestPath())) {
         m_memtest = true;
-        m_memtestOn = (bool)(QFile::permissions(QString::fromUtf8(GRUB_MEMTEST)) & \
(QFile::ExeOwner | QFile::ExeGroup | QFile::ExeOther)); +        m_memtestOn = \
(bool)(QFile::permissions(grubMemtestPath()) & (QFile::ExeOwner | QFile::ExeGroup | \
QFile::ExeOther));  } else {
         operations |= MemtestFile;
     }
@@ -1008,8 +1008,8 @@ void KCMGRUB2::readAll()
         operations |= Vbe;
     }
 #endif
-    if (QFileInfo(QString::fromUtf8(GRUB_LOCALE)).isReadable()) {
-        m_locales = QDir(QString::fromUtf8(GRUB_LOCALE)).entryList(QStringList() << \
QLatin1String("*.mo"), \
QDir::Files).replaceInStrings(QRegExp(QLatin1String("\\.mo$")), QString()); +    if \
(QFileInfo(grubLocalePath()).isReadable()) { +        m_locales = \
QDir(grubLocalePath()).entryList(QStringList() << QLatin1String("*.mo"), \
QDir::Files).replaceInStrings(QRegExp(QLatin1String("\\.mo$")), QString());  } else {
         operations |= Locales;
     }
@@ -1036,7 +1036,7 @@ void KCMGRUB2::readAll()
             if (reply.data().value(QLatin1String("menuSuccess")).toBool()) {
                 parseEntries(QString::fromUtf8(reply.data().value(QLatin1String("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(QLatin1String("menuError")).toInt();
                 kError() << "Error description:" << \
reply.data().value(QLatin1String("menuErrorString")).toString();  }
@@ -1045,7 +1045,7 @@ void KCMGRUB2::readAll()
             if (reply.data().value(QLatin1String("configSuccess")).toBool()) {
                 parseSettings(QString::fromUtf8(reply.data().value(QLatin1String("configContents")).toByteArray().constData()));
  } else {
-                kError() << "Helper failed to read file:" << GRUB_CONFIG;
+                kError() << "Helper failed to read file:" << grubConfigPath();
                 kError() << "Error code:" << \
                reply.data().value(QLatin1String("configError")).toInt();
                 kError() << "Error description:" << \
reply.data().value(QLatin1String("configErrorString")).toString();  }
@@ -1054,7 +1054,7 @@ void KCMGRUB2::readAll()
             if (reply.data().value(QLatin1String("envSuccess")).toBool()) {
                 parseEnv(QString::fromUtf8(reply.data().value(QLatin1String("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(QLatin1String("envError")).toInt();
                 kError() << "Error description:" << \
reply.data().value(QLatin1String("envErrorString")).toString();  }


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic