Code

Make curvature work again by fixing a minor omission
[inkscape.git] / src / registrytool.cpp
1 /**
2  * Inkscape Registry Tool
3  *
4  * This simple tool is intended for allowing Inkscape to append subdirectories
5  * to its path.  This will allow extensions and other files to be accesses
6  * without explicit user intervention.
7  *
8  * Authors:
9  *   Bob Jamison
10  *
11  * Copyright (C) 2005-2008 Bob Jamison
12  *
13  *  This library is free software; you can redistribute it and/or
14  *  modify it under the terms of the GNU Lesser General Public
15  *  License as published by the Free Software Foundation; either
16  *  version 2.1 of the License, or (at your option) any later version.
17  *
18  *  This library is distributed in the hope that it will be useful,
19  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21  *  Lesser General Public License for more details.
22  *
23  *  You should have received a copy of the GNU Lesser General Public
24  *  License along with this library; if not, write to the Free Software
25  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
26  */
28 #include <windows.h>
29 #include <string>
31 #include "registrytool.h"
34 typedef struct
35 {
36     HKEY       key;
37     int        strlen;
38     const char *str;
39 } KeyTableEntry;
43 KeyTableEntry keyTable[] =
44 {
45     { HKEY_CLASSES_ROOT,   18, "HKEY_CLASSES_ROOT\\"   },
46     { HKEY_CURRENT_CONFIG, 20, "HKEY_CURRENT_CONFIG\\" },
47     { HKEY_CURRENT_USER,   18, "HKEY_CURRENT_USER\\"   },
48     { HKEY_LOCAL_MACHINE,  19, "HKEY_LOCAL_MACHINE\\"  },
49     { HKEY_USERS,          11, "HKEY_USERS\\"          },
50     { NULL,                 0, NULL                    }
51 };
54 /**
55  * Set the string value of a key/name registry entry
56  */ 
57 bool RegistryTool::setStringValue(const Glib::ustring &keyNameArg,
58                                   const Glib::ustring &valueName,
59                                   const Glib::ustring &value)
60 {
61     Glib::ustring keyName = keyNameArg;
63     HKEY rootKey = HKEY_LOCAL_MACHINE; //default root
64     //Trim out the root key if necessary
65     for (KeyTableEntry *entry = keyTable; entry->key; entry++)
66         {
67         if (keyName.compare(0, entry->strlen, entry->str)==0)
68             {
69             rootKey = entry->key;
70             keyName = keyName.substr(entry->strlen);
71             }
72         }
73     //printf("trimmed string: '%s'\n", keyName.c_str());
75     //Get or create the key
76     HKEY key;
77     if (RegCreateKeyEx(rootKey, keyName.c_str(),
78                        0, NULL, REG_OPTION_NON_VOLATILE,
79                        KEY_WRITE, NULL, &key, NULL))
80        {
81        fprintf(stderr, "RegistryTool: Could not create the registry key '%s'\n", keyName.c_str());
82        return false;
83        }
85     //Set the value
86     if (RegSetValueEx(key, valueName.c_str(),
87           0,  REG_SZ, (LPBYTE) value.c_str(), (DWORD) value.size()))
88        {
89        fprintf(stderr, "RegistryTool: Could not set the value '%s'\n", value.c_str());
90        RegCloseKey(key);
91        return false;
92        }
95     RegCloseKey(key);
97     return true;
98 }
102 /**
103  * Get the full path, directory, and base file name of this running executable
104  */ 
105 bool RegistryTool::getExeInfo(Glib::ustring &fullPath,
106                               Glib::ustring &path,
107                               Glib::ustring &exeName)
110     char buf[MAX_PATH+1];
111     if (!GetModuleFileName(NULL, buf, MAX_PATH))
112         {
113         fprintf(stderr, "Could not fetch executable file name\n");
114         return false;
115         }
116     else
117         {
118         //printf("Executable file name: '%s'\n", buf);
119         }
121     fullPath = buf;
122     path     = "";
123     exeName  = "";
124     Glib::ustring::size_type pos = fullPath.rfind('\\');
125     if (pos != fullPath.npos)
126         {
127         path = fullPath.substr(0, pos);
128         exeName = fullPath.substr(pos+1);
129         }
131     return true;
136 /**
137  * Append our subdirectories to the Application Path for this
138  * application
139  */  
140 bool RegistryTool::setPathInfo()
142     Glib::ustring fullPath;
143     Glib::ustring path;
144     Glib::ustring exeName;
146     if (!getExeInfo(fullPath, path, exeName))
147         return false;
149     //printf("full:'%s' path:'%s' exe:'%s'\n",
150     //    fullPath.c_str(), path.c_str(), exeName.c_str());
152     Glib::ustring keyName =
153     "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\";
154     keyName.append(exeName);
156     Glib::ustring valueName = "";
157     Glib::ustring value     = fullPath;
159     if (!setStringValue(keyName, valueName, value))
160         return false;
162     //add our subdirectories
163     Glib::ustring appPath = path;
164     appPath.append("\\python;");
165     appPath.append(path);
166     appPath.append("\\perl");
167     valueName = "Path";
168     value     = appPath;
170     if (!setStringValue(keyName, valueName, value))
171         return false;
173     return true;
177 #ifdef TESTREG
180 /**
181  * Compile this file with
182  *      g++ -DTESTREG registrytool.cpp -o registrytool
183  *  to run these tests.
184  */
185  
186  
187     
188 void testReg()
190     RegistryTool rt;
191     char *key =
192     "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\inkscape.exe";
193     char const *name  = "";
194     char const *value = "c:\\inkscape\\inkscape.exe";
195     if (!rt.setStringValue(key, name, value))
196         {
197         printf("Test failed\n");
198         }
199     else
200         {
201         printf("Test succeeded\n");
202         }
203     name  = "Path";
204     value = "c:\\inkscape\\python";
205     if (!rt.setStringValue(key, name, value))
206         {
207         printf("Test failed\n");
208         }
209     else
210         {
211         printf("Test succeeded\n");
212         }
216 void testPath()
218     RegistryTool rt;
219     rt.setPathInfo();
223 int main(int argc, char **argv)
225     //testReg();
226     testPath();
227     return 0;
230 #endif /* TESTREG */
232 //########################################################################
233 //# E N D    O F    F I L E
234 //########################################################################