Code

OCAL. Fix for Bug #638844 (Errors printed to console if openclipart search fails).
[inkscape.git] / src / registrytool.cpp
index 7f6ff8a9a3b51a23cb26c59ff4549a9df3d82313..af41c3eaf963807e35293d8bebfc49a03fd358db 100644 (file)
@@ -8,7 +8,7 @@
  * Authors:
  *   Bob Jamison
  *
- * Copyright (C) 2005 Bob Jamison
+ * Copyright (C) 2005-2008 Bob Jamison
  *
  *  This library is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU Lesser General Public
 
 #include <windows.h>
 #include <string>
+#include <cstdio>
 
 #include "registrytool.h"
 
 
 typedef struct
 {
-    HKEY key;
-    int  strlen;
-    char *str;
+    HKEY       key;
+    int        strlen;
+    const char *str;
 } KeyTableEntry;
 
 
@@ -50,11 +51,16 @@ KeyTableEntry keyTable[] =
     { NULL,                 0, NULL                    }
 };
 
+
+/**
+ * Set the string value of a key/name registry entry
+ */ 
 bool RegistryTool::setStringValue(const Glib::ustring &keyNameArg,
                                   const Glib::ustring &valueName,
                                   const Glib::ustring &value)
 {
     Glib::ustring keyName = keyNameArg;
+    bool ret = false;
 
     HKEY rootKey = HKEY_LOCAL_MACHINE; //default root
     //Trim out the root key if necessary
@@ -69,47 +75,54 @@ bool RegistryTool::setStringValue(const Glib::ustring &keyNameArg,
     //printf("trimmed string: '%s'\n", keyName.c_str());
 
     //Get or create the key
+    gunichar2 *keyw       = g_utf8_to_utf16(keyName.data(), -1, 0,0,0);
+    gunichar2 *valuenamew = g_utf8_to_utf16(valueName.data(), -1, 0,0,0);
+
     HKEY key;
-    if (RegCreateKeyEx(rootKey, keyName.c_str(),
+    if (RegCreateKeyExW(rootKey, (WCHAR*) keyw,
                        0, NULL, REG_OPTION_NON_VOLATILE,
                        KEY_WRITE, NULL, &key, NULL))
-       {
+    {
        fprintf(stderr, "RegistryTool: Could not create the registry key '%s'\n", keyName.c_str());
-       return false;
-       }
+       goto fail;
+    }
 
-    //Set the value
-    if (RegSetValueEx(key, valueName.c_str(),
-          0,  REG_SZ, (LPBYTE) value.c_str(), (DWORD) value.size()))
-       {
+    // Set the value
+    if (RegSetValueExW(key, (WCHAR*) valuenamew,
+          0,  REG_SZ, (LPBYTE) value.data(), (DWORD) (value.size() + 1)))
+    {
        fprintf(stderr, "RegistryTool: Could not set the value '%s'\n", value.c_str());
-       RegCloseKey(key);
-       return false;
-       }
-
+       goto failkey;
+    }
 
+    ret = true;
+    
+    failkey:
     RegCloseKey(key);
-
-    return true;
+    
+    fail:
+    g_free(keyw);
+    g_free(valuenamew);
+    return ret;
 }
 
+
+
+/**
+ * Get the full path, directory, and base file name of this running executable
+ */ 
 bool RegistryTool::getExeInfo(Glib::ustring &fullPath,
                               Glib::ustring &path,
                               Glib::ustring &exeName)
 {
+    const int pathbuf = 2048;
+    gunichar2 pathw[pathbuf];
+    GetModuleFileNameW(NULL, (WCHAR*) pathw, pathbuf);
 
-    char buf[MAX_PATH+1];
-    if (!GetModuleFileName(NULL, buf, MAX_PATH))
-        {
-        fprintf(stderr, "Could not fetch executable file name\n");
-        return false;
-        }
-    else
-        {
-        //printf("Executable file name: '%s'\n", buf);
-        }
+    gchar *utf8path = g_utf16_to_utf8(pathw, -1, 0,0,0);
+    fullPath = utf8path;
+    g_free(utf8path);
 
-    fullPath = buf;
     path     = "";
     exeName  = "";
     Glib::ustring::size_type pos = fullPath.rfind('\\');
@@ -123,6 +136,11 @@ bool RegistryTool::getExeInfo(Glib::ustring &fullPath,
 }
 
 
+
+/**
+ * Append our subdirectories to the Application Path for this
+ * application
+ */  
 bool RegistryTool::setPathInfo()
 {
     Glib::ustring fullPath;
@@ -162,13 +180,22 @@ bool RegistryTool::setPathInfo()
 
 #ifdef TESTREG
 
+
+/**
+ * Compile this file with
+ *      g++ -DTESTREG registrytool.cpp -o registrytool
+ *  to run these tests.
+ */
+    
 void testReg()
 {
     RegistryTool rt;
     char *key =
     "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\inkscape.exe";
-    char *name  = "";
-    char *value = "c:\\inkscape\\inkscape.exe";
+    char const *name  = "";
+    char const *value = "c:\\inkscape\\inkscape.exe";
     if (!rt.setStringValue(key, name, value))
         {
         printf("Test failed\n");