]> git.sesse.net Git - vlc/blob - modules/gui/qt4/util/registry.cpp
Remove some debug
[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, const char *default_value )
132 {
133     HKEY keyHandle;
134     char *tempValue = NULL;
135     char *tempValue2 = NULL;
136
137     DWORD size1;
138     DWORD valueType;
139
140     if( RegOpenKeyEx( m_RootKey, path, 0, KEY_READ, &keyHandle ) == ERROR_SUCCESS )
141     {
142         if( RegQueryValueEx(  keyHandle, valueName, NULL, &valueType, NULL, &size1 ) == ERROR_SUCCESS )
143         {
144            if( valueType == REG_SZ )
145            {
146                // free
147                tempValue = ( char * )malloc( size1+1 ); // +1 für NullByte`?
148                if( RegQueryValueEx(  keyHandle, valueName, NULL, &valueType, (LPBYTE)tempValue, &size1 ) == ERROR_SUCCESS )
149                {
150                   tempValue2 = tempValue;
151                };
152            }
153         }
154         RegCloseKey( keyHandle );
155     }
156
157     return tempValue == NULL ? strdup( default_value ) : tempValue2;
158 }
159
160 double QVLCRegistry::ReadRegistryDouble( const char *path, const char *valueName, double default_value )
161 {
162     HKEY keyHandle;
163     double tempValue;
164     DWORD size1;
165     DWORD valueType;
166
167     if( RegOpenKeyEx( m_RootKey, path, 0, KEY_READ, &keyHandle ) == ERROR_SUCCESS )
168     {
169         if( RegQueryValueEx( keyHandle, valueName, NULL, &valueType,
170                              NULL, &size1 ) == ERROR_SUCCESS )
171         {
172            if( ( valueType == REG_BINARY ) && ( size1 == sizeof( double ) ) )
173            {
174                if( RegQueryValueEx(  keyHandle, valueName, NULL, &valueType,
175                            (LPBYTE)&tempValue, &size1 ) == ERROR_SUCCESS )
176                {
177                   default_value = tempValue;
178                };
179            }
180         }
181         RegCloseKey( keyHandle );
182     }
183     return default_value;
184 }
185
186 int QVLCRegistry::DeleteValue( const char *path, const char *valueName )
187 {
188     HKEY keyHandle;
189     long result;
190     if( (result = RegOpenKeyEx(m_RootKey, path, 0, KEY_WRITE, &keyHandle)) == ERROR_SUCCESS)
191     {
192         result = RegDeleteValue(keyHandle, valueName);
193         RegCloseKey(keyHandle);
194     }
195     //ERROR_SUCCESS = ok everything else you have a problem*g*,
196     return result;
197 }
198
199 long QVLCRegistry::DeleteKey( const char *path, const char *keyName )
200 {
201     HKEY keyHandle;
202     long result;
203     if( (result = RegOpenKeyEx(m_RootKey, path, 0, KEY_WRITE, &keyHandle)) == ERROR_SUCCESS)
204     {
205          // be warned the key "keyName" will not be deleted if there are subkeys below him, values
206         // I think are ok and will be recusively deleted, but not keys...
207         // for this case we have to do a little bit more work!
208         result = RegDeleteKey(keyHandle, keyName);
209         RegCloseKey(keyHandle);
210     }
211     //ERROR_SUCCESS = ok everything else you have a problem*g*,
212     return result;
213 }
214
215 #endif /* WIN32 */