]> git.sesse.net Git - vlc/blob - modules/gui/qt4/util/registry.cpp
a05f2d3eadf7258a010c3a6928254d54628c256f
[vlc] / modules / gui / qt4 / util / registry.cpp
1 /*****************************************************************************
2  * registry.cpp: Windows Registry Manipulation
3  ****************************************************************************
4  * Copyright (C) 2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Andre Weber <WeberAndre # gmx - de>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #ifdef WIN32
29
30 #include "registry.hpp"
31
32 QVLCRegistry::QVLCRegistry( HKEY rootKey )
33 {
34     m_RootKey = rootKey;
35 }
36
37 QVLCRegistry::~QVLCRegistry( void )
38 {
39 }
40
41 bool QVLCRegistry::RegistryKeyExists( const char *path )
42 {
43     HKEY keyHandle;
44     if(  RegOpenKeyEx( m_RootKey, path, 0, KEY_READ, &keyHandle ) == ERROR_SUCCESS )
45     {
46         RegCloseKey( keyHandle );
47         return true;
48     }
49     return false;
50 }
51
52 bool QVLCRegistry::RegistryValueExists( const char *path, const char *valueName )
53 {
54     HKEY keyHandle;
55     bool temp = false;
56     DWORD size1;
57     DWORD valueType;
58
59     if(  RegOpenKeyEx( m_RootKey, path, 0, KEY_READ, &keyHandle ) == ERROR_SUCCESS )
60     {
61         if( RegQueryValueEx( keyHandle, valueName, NULL,
62                              &valueType, NULL, &size1 ) == ERROR_SUCCESS )
63         {
64            temp = true;
65         }
66         RegCloseKey( keyHandle );
67     }
68     return temp;
69 }
70
71 void QVLCRegistry::WriteRegistryInt( const char *path, const char *valueName, int value )
72 {
73     HKEY keyHandle;
74
75     if(  RegCreateKeyEx( m_RootKey, path, 0, NULL, REG_OPTION_NON_VOLATILE,
76                          KEY_WRITE, NULL, &keyHandle, NULL )  == ERROR_SUCCESS )
77     {
78         RegSetValueEx( keyHandle, valueName, 0, REG_DWORD,
79                 (LPBYTE)&value, sizeof( int ) );
80         RegCloseKey( keyHandle );
81     }
82 }
83
84 void QVLCRegistry::WriteRegistryString( const char *path, const char *valueName, const char *value )
85 {
86     HKEY keyHandle;
87
88     if(  RegCreateKeyEx( m_RootKey, path, 0, NULL, REG_OPTION_NON_VOLATILE,
89                          KEY_WRITE, NULL, &keyHandle, NULL )  == ERROR_SUCCESS )
90     {
91         RegSetValueEx( keyHandle, valueName, 0, REG_SZ, (LPBYTE)value,
92                 (DWORD)( strlen( value ) + 1 ) );
93         RegCloseKey( keyHandle );
94     }
95 }
96
97 void QVLCRegistry::WriteRegistryDouble( const char *path, const char *valueName, double value )
98 {
99     HKEY keyHandle;
100     if( RegCreateKeyEx( m_RootKey, path, 0, NULL, REG_OPTION_NON_VOLATILE,
101                        KEY_WRITE, NULL, &keyHandle, NULL ) == ERROR_SUCCESS )
102     {
103         RegSetValueEx( keyHandle, valueName, 0, REG_BINARY, (LPBYTE)&value, sizeof( double ) );
104         RegCloseKey( keyHandle );
105     }
106 }
107
108 int QVLCRegistry::ReadRegistryInt( const char *path, const char *valueName, int default_value ) {
109     HKEY keyHandle;
110     int tempValue;
111     DWORD size1;
112     DWORD valueType;
113
114     if(  RegOpenKeyEx( m_RootKey, path, 0, KEY_READ, &keyHandle ) == ERROR_SUCCESS )
115     {
116         if( RegQueryValueEx(  keyHandle, valueName, NULL, &valueType, NULL, &size1 ) == ERROR_SUCCESS )
117         {
118            if( valueType == REG_DWORD )
119            {
120                if( RegQueryValueEx(  keyHandle, valueName, NULL, &valueType, (LPBYTE)&tempValue, &size1 ) == ERROR_SUCCESS )
121                {
122                   default_value = tempValue;
123                };
124            }
125         }
126         RegCloseKey( keyHandle );
127     }
128     return default_value;
129 }
130
131 char * QVLCRegistry::ReadRegistryString( const char *path, const char *valueName, char *default_value )
132 {
133     HKEY keyHandle;
134     char *tempValue = NULL;
135     DWORD size1;
136     DWORD valueType;
137
138     if( RegOpenKeyEx( m_RootKey, path, 0, KEY_READ, &keyHandle ) == ERROR_SUCCESS )
139     {
140         if( RegQueryValueEx(  keyHandle, valueName, NULL, &valueType, NULL, &size1 ) == ERROR_SUCCESS )
141         {
142            if( valueType == REG_SZ )
143            {
144                // free
145                tempValue = ( char * )malloc( size1+1 ); // +1 für NullByte`?
146                if( RegQueryValueEx(  keyHandle, valueName, NULL, &valueType, (LPBYTE)tempValue, &size1 ) == ERROR_SUCCESS )
147                {
148                   default_value = tempValue;
149                };
150            }
151         }
152         RegCloseKey( keyHandle );
153     }
154     if( tempValue == NULL )
155     {
156         // wenn tempValue nicht aus registry gelesen wurde dafür sorgen das ein neuer String mit der Kopie von DefaultValue
157         // geliefert wird - das macht das Handling des Rückgabewertes der Funktion einfacher - immer schön mit free freigeben!
158         default_value = strdup( default_value );
159     }
160
161     return default_value;
162 }
163
164 double QVLCRegistry::ReadRegistryDouble( const char *path, const char *valueName, double default_value )
165 {
166     HKEY keyHandle;
167     double tempValue;
168     DWORD size1;
169     DWORD valueType;
170
171     if( RegOpenKeyEx( m_RootKey, path, 0, KEY_READ, &keyHandle ) == ERROR_SUCCESS )
172     {
173         if( RegQueryValueEx( keyHandle, valueName, NULL, &valueType,
174                              NULL, &size1 ) == ERROR_SUCCESS )
175         {
176            if( ( valueType == REG_BINARY ) && ( size1 == sizeof( double ) ) )
177            {
178                if( RegQueryValueEx(  keyHandle, valueName, NULL, &valueType,
179                            (LPBYTE)&tempValue, &size1 ) == ERROR_SUCCESS )
180                {
181                   default_value = tempValue;
182                };
183            }
184         }
185         RegCloseKey( keyHandle );
186     }
187     return default_value;
188 }
189
190 int QVLCRegistry::DeleteValue( char *path, char *valueName )
191 {
192     HKEY keyHandle;
193     long result;
194     if( (result = RegOpenKeyEx(m_RootKey, path, 0, KEY_WRITE, &keyHandle)) == ERROR_SUCCESS)
195     {
196         result = RegDeleteValue(keyHandle, valueName);
197         RegCloseKey(keyHandle);
198     }
199     //ERROR_SUCCESS = ok everything else you have a problem*g*,
200     return result;
201 }
202
203 long QVLCRegistry::DeleteKey( char *path, char *keyName )
204 {
205     HKEY keyHandle;
206     long result;
207     if( (result = RegOpenKeyEx(m_RootKey, path, 0, KEY_WRITE, &keyHandle)) == ERROR_SUCCESS)
208     {
209          // be warned the key "keyName" will not be deleted if there are subkeys below him, values
210         // I think are ok and will be recusively deleted, but not keys...
211         // for this case we have to do a little bit more work!
212         result = RegDeleteKey(keyHandle, keyName);
213         RegCloseKey(keyHandle);
214     }
215     //ERROR_SUCCESS = ok everything else you have a problem*g*,
216     return result;
217 }
218
219 #endif /* WIN32 */