]> git.sesse.net Git - vlc/blob - modules/gui/qt4/util/registry.cpp
Qt4 - Fix Compilation on CYGWin. This is a quick fix but not a solution. Again, MOC...
[vlc] / modules / gui / qt4 / util / registry.cpp
1 /*****************************************************************************
2  * registry.cpp: Windows Registry Manipulation
3  ****************************************************************************
4  * Copyright (C) 2008 the VideoLAN team
5  * $Id: input_slider.cpp 24525 2008-01-23 21:50:58Z courmisch $
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 #include "registry.hpp"
29
30 QVLCRegistry::QVLCRegistry( HKEY rootKey )
31 {
32     m_RootKey = rootKey;
33 }
34
35 QVLCRegistry::~QVLCRegistry( void )
36 {
37 }
38
39 bool QVLCRegistry::RegistryKeyExists( char *path )
40 {
41     HKEY keyHandle;
42     if(  RegOpenKeyEx( m_RootKey, path, 0, KEY_READ, &keyHandle ) == ERROR_SUCCESS )
43     {
44         RegCloseKey( keyHandle );
45         return true;
46     }
47     return false;
48 }
49
50 bool QVLCRegistry::RegistryValueExists( char *path, char *valueName )
51 {
52     HKEY keyHandle;
53     bool temp = false;
54     DWORD size1;
55     DWORD valueType;
56
57     if(  RegOpenKeyEx( m_RootKey, path, 0, KEY_READ, &keyHandle ) == ERROR_SUCCESS )
58     {
59         if( RegQueryValueEx( keyHandle, valueName, NULL,
60                              &valueType, NULL, &size1 ) == ERROR_SUCCESS )
61         {
62            temp = true;
63         }
64         RegCloseKey( keyHandle );
65     }
66     return temp;
67 }
68
69 void QVLCRegistry::WriteRegistryInt( char *path, char *valueName, int value )
70 {
71     HKEY keyHandle;
72
73     if(  RegCreateKeyEx( m_RootKey, path, 0, NULL, REG_OPTION_NON_VOLATILE,
74                          KEY_WRITE, NULL, &keyHandle, NULL )  == ERROR_SUCCESS )
75     {
76         RegSetValueEx( keyHandle, valueName, 0, REG_DWORD,
77                 (LPBYTE)&value, sizeof( int ) );
78         RegCloseKey( keyHandle );
79     }
80 }
81
82 void QVLCRegistry::WriteRegistryString( char *path, char *valueName, char *value )
83 {
84     HKEY keyHandle;
85
86     if(  RegCreateKeyEx( m_RootKey, path, 0, NULL, REG_OPTION_NON_VOLATILE,
87                          KEY_WRITE, NULL, &keyHandle, NULL )  == ERROR_SUCCESS )
88     {
89         RegSetValueEx( keyHandle, valueName, 0, REG_SZ, (LPBYTE)value,
90                 (DWORD)( strlen( value ) + 1 ) );
91         RegCloseKey( keyHandle );
92     }
93 }
94
95 void QVLCRegistry::WriteRegistryDouble( char *path, char *valueName, double value )
96 {
97     HKEY keyHandle;
98     if( RegCreateKeyEx( m_RootKey, path, 0, NULL, REG_OPTION_NON_VOLATILE,
99                        KEY_WRITE, NULL, &keyHandle, NULL ) == ERROR_SUCCESS )
100     {
101         RegSetValueEx( keyHandle, valueName, 0, REG_BINARY, (LPBYTE)&value, sizeof( double ) );
102         RegCloseKey( keyHandle );
103     }
104 }
105
106 int QVLCRegistry::ReadRegistryInt( char *path, char *valueName, int default_value ) {
107     HKEY keyHandle;
108     int tempValue;
109     DWORD size1;
110     DWORD valueType;
111
112     if(  RegOpenKeyEx( m_RootKey, path, 0, KEY_READ, &keyHandle ) == ERROR_SUCCESS )
113     {
114         if( RegQueryValueEx(  keyHandle, valueName, NULL, &valueType, NULL, &size1 ) == ERROR_SUCCESS )
115         {
116            if( valueType == REG_DWORD )
117            {
118                if( RegQueryValueEx(  keyHandle, valueName, NULL, &valueType, (LPBYTE)&tempValue, &size1 ) == ERROR_SUCCESS )
119                {
120                   default_value = tempValue;
121                };
122            }
123         }
124         RegCloseKey( keyHandle );
125     }
126     return default_value;
127 }
128
129 char * QVLCRegistry::ReadRegistryString( char *path, char *valueName, char *default_value )
130 {
131     HKEY keyHandle;
132     char *tempValue = NULL;
133     DWORD size1;
134     DWORD valueType;
135
136     if( RegOpenKeyEx( m_RootKey, path, 0, KEY_READ, &keyHandle ) == ERROR_SUCCESS )
137     {
138         if( RegQueryValueEx(  keyHandle, valueName, NULL, &valueType, NULL, &size1 ) == ERROR_SUCCESS )
139         {
140            if( valueType == REG_SZ )
141            {
142                // free
143                tempValue = ( char * )malloc( size1+1 ); // +1 für NullByte`?
144                if( RegQueryValueEx(  keyHandle, valueName, NULL, &valueType, (LPBYTE)tempValue, &size1 ) == ERROR_SUCCESS )
145                {
146                   default_value = tempValue;
147                };
148            }
149         }
150         RegCloseKey( keyHandle );
151     }
152     if( tempValue == NULL )
153     {
154         // wenn tempValue nicht aus registry gelesen wurde dafür sorgen das ein neuer String mit der Kopie von DefaultValue
155         // geliefert wird - das macht das Handling des Rückgabewertes der Funktion einfacher - immer schön mit free freigeben!
156         default_value = strdup( default_value );
157     }
158
159     return default_value;
160 }
161
162 double QVLCRegistry::ReadRegistryDouble( char *path, char *valueName, double default_value )
163 {
164     HKEY keyHandle;
165     double tempValue;
166     DWORD size1;
167     DWORD valueType;
168
169     if( RegOpenKeyEx( m_RootKey, path, 0, KEY_READ, &keyHandle ) == ERROR_SUCCESS )
170     {
171         if( RegQueryValueEx( keyHandle, valueName, NULL, &valueType,
172                              NULL, &size1 ) == ERROR_SUCCESS )
173         {
174            if( ( valueType == REG_BINARY ) && ( size1 == sizeof( double ) ) )
175            {
176                if( RegQueryValueEx(  keyHandle, valueName, NULL, &valueType,
177                            (LPBYTE)&tempValue, &size1 ) == ERROR_SUCCESS )
178                {
179                   default_value = tempValue;
180                };
181            }
182         }
183         RegCloseKey( keyHandle );
184     }
185     return default_value;
186 }
187