OSDN Git Service

Renamed functions for consistency.
[mutilities/MUtilities.git] / src / Registry_Win32.cpp
index 6542bdb..39c60ab 100644 (file)
@@ -1,6 +1,6 @@
 ///////////////////////////////////////////////////////////////////////////////
 // MuldeR's Utilities for Qt
-// Copyright (C) 2004-2015 LoRd_MuldeR <MuldeR2@GMX.de>
+// Copyright (C) 2004-2016 LoRd_MuldeR <MuldeR2@GMX.de>
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 #include <Shlwapi.h>
 
 ///////////////////////////////////////////////////////////////////////////////
+// INTERNAL FUNCTIONS
+///////////////////////////////////////////////////////////////////////////////
 
-static HKEY registry_root(const int &rootKey)
-{
-       switch(rootKey)
-       {
-               case MUtils::Registry::root_classes: return HKEY_CLASSES_ROOT;  break;
-               case MUtils::Registry::root_user:    return HKEY_CURRENT_USER;  break;
-               case MUtils::Registry::root_machine: return HKEY_LOCAL_MACHINE; break;
-               default: MUTILS_THROW("Unknown root reg value was specified!");
-       }
-}
+#define ENUM2STR(X,Y) do \
+{ \
+       static const char *_name = #Y; \
+       if((X) == (Y)) return _name; \
+} \
+while(0)
 
-static DWORD registry_access(const int &access)
+namespace MUtils
 {
-       switch(access)
+       namespace Registry
        {
-               case MUtils::Registry::access_readonly:  return KEY_READ;               break;
-               case MUtils::Registry::access_writeonly: return KEY_WRITE;              break;
-               case MUtils::Registry::access_readwrite: return KEY_READ | KEY_WRITE;   break;
-               case MUtils::Registry::access_enumerate: return KEY_ENUMERATE_SUB_KEYS; break;
-               default: MUTILS_THROW("Unknown access value was specified!");
+               static HKEY registry_root(const reg_root_t &rootKey)
+               {
+                       switch(rootKey)
+                       {
+                               case root_classes: return HKEY_CLASSES_ROOT;  break;
+                               case root_user:    return HKEY_CURRENT_USER;  break;
+                               case root_machine: return HKEY_LOCAL_MACHINE; break;
+                               default: MUTILS_THROW("Unknown root reg value was specified!");
+                       }
+               }
+
+               static DWORD registry_access(const reg_access_t &access)
+               {
+                       switch(access)
+                       {
+                               case access_readonly:  return KEY_READ;               break;
+                               case access_writeonly: return KEY_WRITE;              break;
+                               case access_readwrite: return KEY_READ | KEY_WRITE;   break;
+                               case access_enumerate: return KEY_ENUMERATE_SUB_KEYS; break;
+                               default: MUTILS_THROW("Unknown access value was specified!");
+                       }
+               }
+
+               static DWORD registry_scope(const reg_scope_t &scope)
+               {
+                       switch (scope)
+                       {
+                               case scope_default: return 0;               break;
+                               case scope_wow_x32: return KEY_WOW64_32KEY; break;
+                               case scope_wow_x64: return KEY_WOW64_64KEY; break;
+                               default: MUTILS_THROW("Unknown scope value was specified!");
+                       }
+               }
+
+               static const char* reg_root2str(const reg_root_t &rootKey)
+               {
+                       ENUM2STR(rootKey, root_classes);
+                       ENUM2STR(rootKey, root_user);
+                       ENUM2STR(rootKey, root_machine);
+
+                       static const char *unknown = "<unknown>";
+                       return unknown;
+               }
+
+               static const char* reg_access2str(const reg_access_t &access)
+               {
+                       ENUM2STR(access, access_readonly);
+                       ENUM2STR(access, access_writeonly);
+                       ENUM2STR(access, access_readwrite);
+                       ENUM2STR(access, access_enumerate);
+
+                       static const char *unknown = "<unknown>";
+                       return unknown;
+               }
        }
 }
 
@@ -98,18 +145,18 @@ while(0)
 // Registry Key Class
 ///////////////////////////////////////////////////////////////////////////////
 
-MUtils::Registry::RegistryKey::RegistryKey(const int &rootKey, const QString &keyName, const int &access)
+MUtils::Registry::RegistryKey::RegistryKey(const reg_root_t &rootKey, const QString &keyName, const reg_access_t &access, const reg_scope_t &scope)
 :
        p(new Internal::RegistryKeyPrivate())
 {
        p->m_hKey   = NULL;
-       p->m_access = registry_access(access);
+       p->m_access = registry_access(access) | registry_scope(scope);
        p->m_isOpen = false;
 
        p->m_isOpen = (RegCreateKeyEx(registry_root(rootKey), MUTILS_WCHR(keyName), 0, NULL, 0, p->m_access, NULL, &p->m_hKey, NULL) == ERROR_SUCCESS);
        if(!p->m_isOpen)
        {
-               qWarning("Failed to open registry key!");
+               qWarning("Failed to open registry key \"%s\"! (rootKey: %s, access: %s)", MUTILS_UTF8(keyName), reg_root2str(rootKey), reg_access2str(access));
        }
 }
 
@@ -207,10 +254,10 @@ bool MUtils::Registry::RegistryKey::enum_subkeys(QStringList &list) const
 /*
  * Write registry value
  */
-bool MUtils::Registry::reg_value_write(const int &rootKey, const QString &keyName, const QString &valueName, const quint32 &value)
+bool MUtils::Registry::reg_value_write(const reg_root_t &rootKey, const QString &keyName, const QString &valueName, const quint32 &value, const reg_scope_t &scope)
 {
        bool success = false;
-       RegistryKey regKey(rootKey, keyName, access_readwrite);
+       RegistryKey regKey(rootKey, keyName, access_readwrite, scope);
        if(regKey.isOpen())
        {
                success = regKey.value_write(valueName, value);
@@ -221,10 +268,10 @@ bool MUtils::Registry::reg_value_write(const int &rootKey, const QString &keyNam
 /*
  * Write registry value
  */
-bool MUtils::Registry::reg_value_write(const int &rootKey, const QString &keyName, const QString &valueName, const QString &value)
+bool MUtils::Registry::reg_value_write(const reg_root_t &rootKey, const QString &keyName, const QString &valueName, const QString &value, const reg_scope_t &scope)
 {
        bool success = false;
-       RegistryKey regKey(rootKey, keyName, access_readwrite);
+       RegistryKey regKey(rootKey, keyName, access_readwrite, scope);
        if(regKey.isOpen())
        {
                success = regKey.value_write(valueName, value);
@@ -235,10 +282,10 @@ bool MUtils::Registry::reg_value_write(const int &rootKey, const QString &keyNam
 /*
  * Read registry value
  */
-bool MUtils::Registry::reg_value_read(const int &rootKey, const QString &keyName, const QString &valueName, quint32 &value)
+bool MUtils::Registry::reg_value_read(const reg_root_t &rootKey, const QString &keyName, const QString &valueName, quint32 &value, const reg_scope_t &scope)
 {
        bool success = false;
-       RegistryKey regKey(rootKey, keyName, access_readonly);
+       RegistryKey regKey(rootKey, keyName, access_readonly, scope);
        if(regKey.isOpen())
        {
                success = regKey.value_read(valueName, value);
@@ -253,10 +300,10 @@ bool MUtils::Registry::reg_value_read(const int &rootKey, const QString &keyName
 /*
  * Read registry value
  */
-bool MUtils::Registry::reg_value_read(const int &rootKey, const QString &keyName, const QString &valueName, QString &value)
+bool MUtils::Registry::reg_value_read(const reg_root_t &rootKey, const QString &keyName, const QString &valueName, QString &value, const reg_scope_t &scope)
 {
        bool success = false;
-       RegistryKey regKey(rootKey, keyName, access_readonly);
+       RegistryKey regKey(rootKey, keyName, access_readonly, scope);
        if(regKey.isOpen())
        {
                success = regKey.value_read(valueName, value);
@@ -271,10 +318,10 @@ bool MUtils::Registry::reg_value_read(const int &rootKey, const QString &keyName
 /*
  * Enumerate value names
  */
-bool MUtils::Registry::reg_enum_values(const int &rootKey, const QString &keyName, QStringList &values)
+bool MUtils::Registry::reg_enum_values(const reg_root_t &rootKey, const QString &keyName, QStringList &values, const reg_scope_t &scope)
 {
        bool success = false;
-       RegistryKey regKey(rootKey, keyName, access_readonly);
+       RegistryKey regKey(rootKey, keyName, access_readonly, scope);
        if(regKey.isOpen())
        {
                success = regKey.enum_values(values);
@@ -289,10 +336,10 @@ bool MUtils::Registry::reg_enum_values(const int &rootKey, const QString &keyNam
 /*
  * Enumerate subkey names
  */
-bool MUtils::Registry::reg_enum_subkeys(const int &rootKey, const QString &keyName, QStringList &subkeys)
+bool MUtils::Registry::reg_enum_subkeys(const reg_root_t &rootKey, const QString &keyName, QStringList &subkeys, const reg_scope_t &scope)
 {
        bool success = false;
-       RegistryKey regKey(rootKey, keyName, access_enumerate);
+       RegistryKey regKey(rootKey, keyName, access_enumerate, scope);
        if(regKey.isOpen())
        {
                success = regKey.enum_subkeys(subkeys);
@@ -305,9 +352,48 @@ bool MUtils::Registry::reg_enum_subkeys(const int &rootKey, const QString &keyNa
 }
 
 /*
+ * Check registry key existence
+ */
+bool MUtils::Registry::reg_key_exists(const reg_root_t &rootKey, const QString &keyName, const reg_scope_t &scope)
+{
+       HKEY hKey = NULL;
+       if(RegOpenKeyEx(registry_root(rootKey), MUTILS_WCHR(keyName), 0, STANDARD_RIGHTS_READ | registry_scope(scope), &hKey) == ERROR_SUCCESS)
+       {
+               RegCloseKey(hKey);
+               return true;
+       }
+       return false;
+}
+
+/*
  * Delete registry key
  */
-bool MUtils::Registry::reg_key_delete(const int &rootKey, const QString &keyName)
+bool MUtils::Registry::reg_key_delete(const reg_root_t &rootKey, const QString &keyName, const bool &recusrive, const bool &ascend, const reg_scope_t &scope)
 {
-       return (SHDeleteKey(registry_root(rootKey), MUTILS_WCHR(keyName)) == ERROR_SUCCESS);
+       bool okay = false;
+
+       if (scope != scope_default)
+       {
+               MUTILS_THROW("Scope option not currently supported by reg_key_delete() function!");
+       }
+
+       if(recusrive)
+       {
+               okay = (SHDeleteKey(registry_root(rootKey), MUTILS_WCHR(keyName)) == ERROR_SUCCESS);
+       }
+       else
+       {
+               okay = (RegDeleteKey(registry_root(rootKey), MUTILS_WCHR(keyName)) == ERROR_SUCCESS);
+       }
+
+       if(ascend && okay)
+       {
+               const int pos = qMax(keyName.lastIndexOf(QLatin1Char('/')), keyName.lastIndexOf(QLatin1Char('\\')));
+               if(pos > 0)
+               {
+                       reg_key_delete(rootKey, keyName.left(pos), false, true);
+               }
+       }
+
+       return okay;
 }