/[winpt]/trunk/Src/wptRegistry.cpp
ViewVC logotype

Diff of /trunk/Src/wptRegistry.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2 by twoaday, Mon Jan 31 11:02:21 2005 UTC revision 76 by twoaday, Thu Nov 10 14:07:06 2005 UTC
# Line 1  Line 1 
1  /* wptRegistry.cpp - W32 Registry access  /* wptRegistry.cpp - W32 Registry access
2   *      Copyright (C) 2000-2005 Timo Schulz   *      Copyright (C) 2000-2005 Timo Schulz
3   *   *
4   * This file is part of WinPT.   * This file is part of WinPT.
5   *   *
6   * WinPT is free software; you can redistribute it and/or   * WinPT is free software; you can redistribute it and/or
7   * modify it under the terms of the GNU General Public License   * modify it under the terms of the GNU General Public License
8   * as published by the Free Software Foundation; either version 2   * as published by the Free Software Foundation; either version 2
9   * of the License, or (at your option) any later version.   * of the License, or (at your option) any later version.
10   *     *  
11   * WinPT is distributed in the hope that it will be useful,   * WinPT is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   * General Public License for more details.   * General Public License for more details.
15   *   *
16   * You should have received a copy of the GNU General Public License   * You should have received a copy of the GNU General Public License
17   * along with WinPT; if not, write to the Free Software Foundation,   * along with WinPT; if not, write to the Free Software Foundation,
18   * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA   * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19   */   */
20    
21  #include <windows.h>  #ifdef HAVE_CONFIG_H
22  #include <stdio.h>  #include <config.h>
23    #endif
24  #include "wptErrors.h"  
25  #include "wptW32API.h"  #include <windows.h>
26  #include "wptGPG.h"  #include <stdio.h>
27  #include "wptRegistry.h"  
28  #include "wptKeyserver.h"  #include "wptErrors.h"
29  #include "wptTypes.h"  #include "wptW32API.h"
30  #include "wptNLS.h"  #include "wptGPG.h"
31  #include "wptVersion.h"  #include "wptRegistry.h"
32    #include "wptKeyserver.h"
33  #define rc_ok(rc) ((rc) == ERROR_SUCCESS)  #include "wptTypes.h"
34    #include "wptNLS.h"
35  struct reg_hotkey_s reg_hotkeys[] = {  #include "wptVersion.h"
36      {"ClipEncrypt", NULL, 0},  
37      {"ClipDecrypt", NULL, 0},  #define rc_ok(rc) ((rc) == ERROR_SUCCESS)
38      {"ClipSign",    NULL, 0},  
39      {"ClipSignEnc", NULL, 0},  static gpg_filetype gpg_filetypes[] = {
40      {"CwsEncrypt",  NULL, 0},      {"GPG Detached Signature", ".sig", 1},
41      {"CwsDecrypt",  NULL, 0},      {"GPG Encrypted Data",     ".gpg", 2},
42      {"CwsSign",     NULL, 0},      {"GPG Armored Data",       ".asc", 2},
43      {"CwsSignEnc",  NULL, 0},      {0}
44      {0}  };
45  };  
46    struct reg_hotkey_s reg_hotkeys[] = {
47  winpt_reg_prefs_s reg_prefs;      {"ClipEncrypt", "", 0},
48        {"ClipDecrypt", "", 0},
49  #define WINPT_REG "Software\\WinPT"      {"ClipSign",    "", 0},
50        {"ClipSignEnc", "", 0},
51        {"CwsEncrypt",  "", 0},
52  void      {"CwsDecrypt",  "", 0},
53  free_reg_prefs (void)      {"CwsSign",     "", 0},
54  {      {"CwsSignEnc",  "", 0},
55      free_if_alloc (reg_prefs.backup.path);      {NULL, "", 0}
56      free_if_alloc (reg_prefs.kserv_conf);  };
57      memset (&reg_prefs, 0, sizeof reg_prefs);  
58  } /* free_reg_prefs */  winpt_reg_prefs_s reg_prefs;
59    
60    #define WINPT_REG "Software\\WinPT"
61  /*  
62   * Register the given WinPT filetype.  
63   */  void
64  static int  free_reg_prefs (void)
65  regist_single_filetype (gpg_filetype *gfile)  {
66  {      free_if_alloc (reg_prefs.backup.path);
67      char icon[256], prog[256];      free_if_alloc (reg_prefs.kserv_conf);
68            free_if_alloc (reg_prefs.homedir);
69      memset (&icon, 0, sizeof (icon));      memset (&reg_prefs, 0, sizeof reg_prefs);
70      GetModuleFileName (glob_hinst, prog, sizeof (prog)-1);  } /* free_reg_prefs */
71      _snprintf (icon, sizeof (icon) -1, "%s,%d", prog, gfile->nicon);  
72      return create_file_type (prog, gfile->ext, gfile->descr, icon);  
73  } /* regist_single_filetype */  /* Register the given WinPT filetype. */
74    static int
75    regist_single_filetype (gpg_filetype *gfile)
76  /*  {
77   * Install the GPG related into the W32 resgistry, if the entry already      char icon[256], prog[256];
78   * exists< the function returns immediately.      
79   */      memset (&icon, 0, sizeof (icon));
80  int      GetModuleFileName (glob_hinst, prog, sizeof (prog)-1);
81  regist_inst_gnupg( int create_mokey )      _snprintf (icon, sizeof (icon) -1, "%s,%d", prog, gfile->nicon);
82  {      return create_file_type (prog, gfile->ext, gfile->descr, icon);
83      int rc;  } /* regist_single_filetype */
84      HKEY reg;  
85    
86      rc = RegOpenKeyEx( HKEY_CURRENT_USER, "Software\\GNU\\GnuPG", 0, KEY_READ, &reg );    /* Install the GPG related into the W32 resgistry, if the entry already
87      if( rc_ok( rc ) ) {     exists the function returns immediately. */
88          RegCloseKey( reg );  int
89          return 0;        regist_inst_gnupg (int create_mokey)
90      }  {
91      rc = RegCreateKey( HKEY_CURRENT_USER, "Software\\GNU\\GnuPG", &reg );      int rc;
92      if( !rc_ok( rc ) )                HKEY reg;
93          return WPTERR_REGISTRY;  
94      RegCloseKey( reg );      rc = RegOpenKeyEx( HKEY_CURRENT_USER, "Software\\GNU\\GnuPG", 0, KEY_READ, &reg );  
95      if( create_mokey ) {      if( rc_ok( rc ) ) {
96          rc = RegOpenKeyEx( HKEY_CURRENT_USER,  "Control Panel\\MingW32\\NLS", 0, KEY_READ, &reg );          RegCloseKey( reg );
97          if( rc_ok( rc ) ) {          return 0;      
98              RegCloseKey( reg );      }
99              return 0;        rc = RegCreateKey( HKEY_CURRENT_USER, "Software\\GNU\\GnuPG", &reg );
100          }      if( !rc_ok( rc ) )          
101          rc = RegCreateKey( HKEY_CURRENT_USER, "Control Panel\\MingW32\\NLS", &reg );          return WPTERR_REGISTRY;
102          if( !rc_ok( rc ) )      RegCloseKey( reg );
103              return WPTERR_REGISTRY;          if( create_mokey ) {
104          RegCloseKey( reg );          rc = RegOpenKeyEx( HKEY_CURRENT_USER,  "Control Panel\\MingW32\\NLS", 0, KEY_READ, &reg );
105      }          if( rc_ok( rc ) ) {
106                RegCloseKey( reg );
107      return 0;              return 0;  
108  } /* regist_inst_gpg */          }
109            rc = RegCreateKey( HKEY_CURRENT_USER, "Control Panel\\MingW32\\NLS", &reg );
110            if( !rc_ok( rc ) )
111  /*              return WPTERR_REGISTRY;    
112   * Install WinPT into the W32 registry, if the entry already          RegCloseKey( reg );
113   * exists the function returns immediately.      }
114   */  
115  int      return 0;
116  regist_inst_winpt( int with_ext, int * created )  } /* regist_inst_gpg */
117  {  
118      HKEY reg;  
119      char * p = NULL;  /* Install WinPT into the W32 registry, if the entry already
120      int rc, i, id, n = 0;     exists the function returns immediately.*/
121    int
122      if( created )  regist_inst_winpt (int with_ext, int * created)
123          *created = 0;  {
124        HKEY reg;
125      p = get_reg_entry( HKEY_CURRENT_USER, WINPT_REG, "Extensions" );      char * p = NULL;
126      if( p && *p == '1' )      int rc, i, id, n = 0;
127          with_ext = 0;  
128      free_if_alloc( p );      if( created )
129            *created = 0;
130      if( with_ext ) {  
131          id = msg_box( NULL, _("WinPT can register some GPG file types for you so they can "      p = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG, "Extensions");
132                                "be processed with a double click in the explorer.\n"      if (p && *p == '1')
133                                "Do you want to continue?"), _("WinPT"), MB_YESNO|MB_INFO );          with_ext = 0;
134          if( id == IDNO ) {      free_if_alloc( p );
135              set_reg_entry( HKEY_CURRENT_USER, WINPT_REG, "Extensions", "1" );  
136              goto start;      if( with_ext ) {
137          }          id = msg_box( NULL, _("WinPT can register some GPG file types for you so they can "
138          for( i = 0; gpg_filetypes[i].ext; i++ ) {                                "be processed with a double click in the explorer.\n"
139              rc = RegOpenKeyEx( HKEY_CLASSES_ROOT, gpg_filetypes[i].ext, 0, KEY_READ, &reg );                                "Do you want to continue?"), _("WinPT"), MB_YESNO|MB_INFO );
140              if( rc_ok( rc ) ) {          if( id == IDNO ) {
141                  RegCloseKey( reg );              set_reg_entry( HKEY_CURRENT_USER, WINPT_REG, "Extensions", "1" );
142                  id = log_box( _("WinPT WARNING"), MB_YESNO|MB_INFO,              goto start;
143                                _("It seems there was already a '%s' file type registered by another application.\n"          }
144                                  "Do you want to overwrite it?"), gpg_filetypes[i].ext );          for( i = 0; gpg_filetypes[i].ext; i++ ) {
145                  if( id == IDNO )              rc = RegOpenKeyEx( HKEY_CLASSES_ROOT, gpg_filetypes[i].ext, 0, KEY_READ, &reg );
146                      continue;              if( rc_ok( rc ) ) {
147              }                  RegCloseKey( reg );
148              regist_single_filetype( &gpg_filetypes[i] );                  id = log_box( _("WinPT WARNING"), MB_YESNO|MB_INFO,
149              n++;                                _("It seems there was already a '%s' file type registered by another application.\n"
150          }                                  "Do you want to overwrite it?"), gpg_filetypes[i].ext );
151      }                  if( id == IDNO )
152                                continue;
153  start:              }
154      rc = RegOpenKeyEx( HKEY_CURRENT_USER, WINPT_REG, 0, KEY_READ, &reg );              regist_single_filetype( &gpg_filetypes[i] );
155      if( rc_ok( rc ) ) {              n++;
156          RegCloseKey( reg );          }
157          rc = RegOpenKeyEx( HKEY_CURRENT_USER, WINPT_REG"\\Keyserver", 0, KEY_READ, &reg );      }
158          if( !rc_ok( rc ) ) {          
159              RegCreateKey( HKEY_CURRENT_USER, WINPT_REG"\\Keyserver", &reg );  start:
160              RegCloseKey( reg );      rc = RegOpenKeyEx( HKEY_CURRENT_USER, WINPT_REG, 0, KEY_READ, &reg );
161          }      if( rc_ok( rc ) ) {
162          p = get_reg_entry_keyserver ("Default");          RegCloseKey( reg );
163          if (!p) {          rc = RegOpenKeyEx( HKEY_CURRENT_USER, WINPT_REG"\\Keyserver", 0, KEY_READ, &reg );
164              char buf[16];          if( !rc_ok( rc ) ) {
165              sprintf (buf, "%d", HKP_PORT);              RegCreateKey( HKEY_CURRENT_USER, WINPT_REG"\\Keyserver", &reg );
166              set_reg_entry_keyserver ("Default_Port", buf);              RegCloseKey( reg );
167              set_reg_entry_keyserver ("Default", DEF_HKP_KEYSERVER);          }
168          }          p = get_reg_entry_keyserver ("Default");
169          free_if_alloc( p );          if (!p) {
170          if( n )              char buf[16];
171              set_reg_entry( HKEY_CURRENT_USER, WINPT_REG, "Extensions", "1" );              sprintf (buf, "%d", HKP_PORT);
172          return 0;              set_reg_entry_keyserver ("Default_Port", buf);
173      }              set_reg_entry_keyserver ("Default", DEF_HKP_KEYSERVER);
174      rc = RegCreateKey( HKEY_CURRENT_USER, WINPT_REG, &reg );          }
175      if( !rc_ok( rc ) )          free_if_alloc( p );
176          return WPTERR_REGISTRY;          if( n )
177      if( created )              set_reg_entry( HKEY_CURRENT_USER, WINPT_REG, "Extensions", "1" );
178          *created = 1;          return 0;
179      RegCloseKey( reg );      }
180      if( n )      rc = RegCreateKey( HKEY_CURRENT_USER, WINPT_REG, &reg );
181          set_reg_entry( HKEY_CURRENT_USER, WINPT_REG, "Extensions", "1" );      if( !rc_ok( rc ) )
182      return 0;          return WPTERR_REGISTRY;
183  } /* regist_inst_winpt */      if( created )
184            *created = 1;
185        RegCloseKey( reg );
186  /*      if( n )
187   * Create a new filetype in the W32 registry.          set_reg_entry( HKEY_CURRENT_USER, WINPT_REG, "Extensions", "1" );
188   * We should really care of errors! Otherwise we can damage the registry!      return 0;
189   */  } /* regist_inst_winpt */
190  int  
191  create_file_type( const char *exefile, const char *ext, const char *extname, char *iconfile )  
192  {  /* Create a new filetype in the W32 registry.
193      int rc;     We should really care of errors! Otherwise we can damage the registry! */
194      HKEY reg = NULL;  int
195      char deficon[256], defexec[256], p_exefile[256];  create_file_type( const char *exefile, const char *ext, const char *extname, char *iconfile )
196        {
197            int rc;
198      rc = RegCreateKey( HKEY_CLASSES_ROOT, ext, &reg );      HKEY reg = NULL;
199      if( rc_ok( rc ) )      char deficon[256], defexec[256], p_exefile[256];
200          rc = RegSetValueEx( reg, NULL, 0, REG_SZ, (byte *)extname, strlen( extname ) );      
201      if( rc_ok( rc ) )      
202          rc = RegCloseKey( reg );      rc = RegCreateKey( HKEY_CLASSES_ROOT, ext, &reg );
203      if( rc_ok( rc ) )          if( rc_ok( rc ) )
204          rc = RegCreateKey( HKEY_CLASSES_ROOT, extname, &reg );          rc = RegSetValueEx( reg, NULL, 0, REG_SZ, (byte *)extname, strlen( extname ) );
205      if( rc_ok( rc ) )      if( rc_ok( rc ) )
206          rc = RegSetValueEx( reg, NULL, 0, REG_SZ, (byte *) extname, strlen( extname ) );          rc = RegCloseKey( reg );
207      if( rc_ok( rc ) )      if( rc_ok( rc ) )    
208          rc = RegCloseKey( reg );          rc = RegCreateKey( HKEY_CLASSES_ROOT, extname, &reg );
209      if( !rc_ok( rc ) ) {      if( rc_ok( rc ) )
210          rc = WPTERR_REGISTRY;          rc = RegSetValueEx( reg, NULL, 0, REG_SZ, (byte *) extname, strlen( extname ) );
211          goto leave;      if( rc_ok( rc ) )
212      }          rc = RegCloseKey( reg );
213            if( !rc_ok( rc ) ) {
214      memset( &deficon, 0, sizeof deficon );          rc = WPTERR_REGISTRY;
215      _snprintf( deficon, sizeof deficon - 1, "%s\\DefaultIcon", extname );          goto leave;
216      memset( &defexec, 0, sizeof defexec );      }
217      _snprintf( defexec, sizeof defexec - 1, "%s\\shell\\open\\command", extname );            
218      memset( &p_exefile, 0, sizeof p_exefile );      memset( &deficon, 0, sizeof deficon );
219      _snprintf( p_exefile, sizeof p_exefile - 1, "%s %%1", exefile );      _snprintf( deficon, sizeof deficon - 1, "%s\\DefaultIcon", extname );
220            memset( &defexec, 0, sizeof defexec );
221      rc = RegCreateKey( HKEY_CLASSES_ROOT, deficon, &reg );      _snprintf( defexec, sizeof defexec - 1, "%s\\shell\\open\\command", extname );      
222      if( rc_ok( rc ) )      memset( &p_exefile, 0, sizeof p_exefile );
223          rc = RegSetValueEx(reg, NULL, 0, REG_SZ, (byte *)iconfile, strlen( iconfile ) );      _snprintf( p_exefile, sizeof p_exefile - 1, "%s %%1", exefile );
224      if( rc_ok( rc ) )      
225          rc = RegCloseKey( reg );      rc = RegCreateKey( HKEY_CLASSES_ROOT, deficon, &reg );
226      if( rc_ok( rc ) )      if( rc_ok( rc ) )
227          rc = RegCreateKey( HKEY_CLASSES_ROOT, defexec, &reg );          rc = RegSetValueEx(reg, NULL, 0, REG_SZ, (byte *)iconfile, strlen( iconfile ) );
228      if( rc_ok( rc ) )      if( rc_ok( rc ) )
229          rc = RegSetValueEx( reg, NULL, 0, REG_SZ, (byte *)p_exefile, strlen( exefile ) );          rc = RegCloseKey( reg );
230      if( rc_ok( rc ) )      if( rc_ok( rc ) )
231          rc = RegCloseKey( reg );          rc = RegCreateKey( HKEY_CLASSES_ROOT, defexec, &reg );
232      if( !rc_ok( rc ) ) {      if( rc_ok( rc ) )
233          rc = WPTERR_REGISTRY;          rc = RegSetValueEx( reg, NULL, 0, REG_SZ, (byte *)p_exefile, strlen( exefile ) );
234          goto leave;          if( rc_ok( rc ) )
235      }          rc = RegCloseKey( reg );
236            if( !rc_ok( rc ) ) {
237  leave:          rc = WPTERR_REGISTRY;
238      if( reg )          goto leave;    
239          RegCloseKey( reg );      }
240      return rc;      
241  } /* create_file_type */  leave:
242        if( reg )
243            RegCloseKey( reg );
244  static char *      return rc;
245  expand_path( const char *path )  } /* create_file_type */
246  {  
247      DWORD len;  
248      char *p;  /* Expand a string with %foo% entries so that %foo% will
249       be replaced with its actual value. */
250      len = ExpandEnvironmentStrings( path, NULL, 0 );  static char *
251      if( !len )  expand_path (const char *path)
252          return NULL;  {
253      len += 1;      DWORD len;
254      p = new char[len];          char *p;
255      if( !p )  
256          return NULL;      len = ExpandEnvironmentStrings (path, NULL, 0);
257      len = ExpandEnvironmentStrings( path, p, len );      if (!len)
258      if( !len ) {          return NULL;
259          free_if_alloc( p );      len += 1;
260          return NULL;      p = new char[len+1];
261      }      if( !p )
262      return p;          return NULL;
263  } /* expand_path */      len = ExpandEnvironmentStrings (path, p, len);
264        if (!len) {
265            free_if_alloc (p);
266  char *          return NULL;
267  get_reg_entry( HKEY root_key, const char * dir, const char * key )      }
268  {      return p;
269      int rc;  } /* expand_path */
270      char text[384] = {0};  
271      DWORD nbytes, type, n1 = 0;  
272      HKEY reg_key = NULL;  /* Retrieve a registry entry with the directory given in @dir
273      char * p = NULL, * tmp = NULL;     and the key given in @key.
274           Return value is the value or NULL otherwise. */
275      rc = RegOpenKeyEx( root_key, dir, 0, KEY_QUERY_VALUE, &reg_key );  char*
276      if( !rc_ok( rc ) )  get_reg_entry (HKEY root_key, const char * dir, const char * key)
277          goto leave;  {
278      nbytes = sizeof text - 1;      int rc;
279      type = REG_SZ;      char text[384] = {0};
280            DWORD nbytes, type;
281      rc = RegQueryValueEx (reg_key, key, 0, &type, (BYTE *)&text, &nbytes);      HKEY reg_key = NULL;
282      if (!rc_ok (rc) || !nbytes)      char *p = NULL;
283          goto leave;      
284            rc = RegOpenKeyEx (root_key, dir, 0, KEY_QUERY_VALUE, &reg_key);
285      if (type == REG_EXPAND_SZ && strchr (text, '%'))      if( !rc_ok( rc ) )
286          p = expand_path (text);          goto leave;
287      else {      nbytes = sizeof (text) - 1;
288          p = new char[nbytes + 1];      type = REG_SZ;
289          if (!p)      
290              BUG (0);      rc = RegQueryValueEx (reg_key, key, 0, &type, (BYTE *)&text, &nbytes);
291          memcpy (p, text, nbytes);      if (!rc_ok (rc) || !nbytes)
292          p[nbytes] = '\0';          goto leave;
293      }      
294            if (type == REG_EXPAND_SZ && strchr (text, '%'))
295  leave:          p = expand_path (text);
296      if (reg_key)      else {
297          RegCloseKey (reg_key);          p = new char[nbytes + 1];
298      return p;          if (!p)
299  } /* get_reg_entry */              BUG (0);
300            memcpy (p, text, nbytes);
301            p[nbytes] = '\0';
302  int      }
303  set_reg_entry( HKEY root_key, const char * dir, const char * key,      
304                 const char * value )  leave:
305  {      if (reg_key)
306      int rc = 0;          RegCloseKey (reg_key);
307      HKEY reg_key;      return p;
308    }
309      rc = RegOpenKeyEx( root_key, dir, 0, KEY_WRITE, &reg_key );  
310      if( !rc_ok( rc ) )  
311          return WPTERR_REGISTRY;  /* XXX: use REG_EXPAND_SZ to support multi user environments.
312      rc = RegSetValueEx( reg_key, key, 0, REG_SZ, (BYTE *)value, strlen( value ) );          keep this type and do not overwrite it with REG_SZ. */
313      if( !rc_ok( rc ) )  
314          rc = WPTERR_REGISTRY;    /* Set a registry entry. */
315      RegCloseKey( reg_key );  int
316      return rc;  set_reg_entry (HKEY root_key, const char *dir, const char *key,
317  } /* set_reg_entry */                 const char *value)
318    {
319        int rc = 0;
320  int      HKEY reg_key;
321  set_reg_key( HKEY root_key, const char * dir, const char * key,  
322               const char * value )      rc = RegOpenKeyEx( root_key, dir, 0, KEY_WRITE, &reg_key );
323  {      if( !rc_ok( rc ) )
324      int rc = 0;          return WPTERR_REGISTRY;
325      HKEY reg_key;      rc = RegSetValueEx( reg_key, key, 0, REG_SZ, (BYTE *)value, strlen( value ) );
326            if( !rc_ok( rc ) )
327      rc = RegOpenKeyEx( root_key, dir, 0, KEY_WRITE, &reg_key );          rc = WPTERR_REGISTRY;  
328      if( !rc_ok( rc ) )      RegCloseKey( reg_key );
329          return WPTERR_REGISTRY;      return rc;
330        } /* set_reg_entry */
331      rc = RegSetValueEx( reg_key, key, 0, REG_SZ, (BYTE *)value, strlen( value ) );        
332      if( !rc_ok( rc ) ) {          
333          if ( RegCreateKey( root_key, key, &reg_key ) != ERROR_SUCCESS ) {  int
334              rc = WPTERR_REGISTRY;  set_reg_key( HKEY root_key, const char * dir, const char * key,
335              goto leave;               const char * value )
336          }  {
337          rc = RegSetValueEx( reg_key, key, 0, REG_SZ, (BYTE *)value, strlen( value ) );                int rc = 0;
338          if ( !rc_ok( rc ) )      HKEY reg_key;
339              rc = WPTERR_REGISTRY;      
340      }      rc = RegOpenKeyEx( root_key, dir, 0, KEY_WRITE, &reg_key );
341            if( !rc_ok( rc ) )
342  leave:          return WPTERR_REGISTRY;
343      RegCloseKey( reg_key );      
344      return rc;      rc = RegSetValueEx( reg_key, key, 0, REG_SZ, (BYTE *)value, strlen( value ) );      
345  } /* set_reg_key */      if( !rc_ok( rc ) ) {        
346            if ( RegCreateKey( root_key, key, &reg_key ) != ERROR_SUCCESS ) {
347                rc = WPTERR_REGISTRY;
348  int              goto leave;
349  set_reg_entry_gpg( const char * key, const char * value )          }
350  {                rc = RegSetValueEx( reg_key, key, 0, REG_SZ, (BYTE *)value, strlen( value ) );          
351      return set_reg_entry( HKEY_CURRENT_USER, "Software\\GNU\\GnuPG", key, value );          if ( !rc_ok( rc ) )
352  } /* set_reg_entry_gpg */              rc = WPTERR_REGISTRY;
353        }
354        
355  int  leave:
356  set_reg_entry_mo( const char * value )      RegCloseKey( reg_key );
357  {            return rc;
358      return set_reg_entry( HKEY_CURRENT_USER, "Control Panel\\Mingw32\\NLS", "MODir", value );  } /* set_reg_key */
359  } /* set_reg_entry_mo */  
360    
361    int
362  char *  set_reg_entry_gpg (const char * key, const char * value)
363  get_reg_entry_gpg( const char *key )  {      
364  {      return set_reg_entry (HKEY_CURRENT_USER, "Software\\GNU\\GnuPG", key, value);
365      return get_reg_entry( HKEY_CURRENT_USER, "Software\\GNU\\GnuPG", key );  }
366  } /* get_reg_entry_gpg */  
367    
368    int
369  char *  set_reg_entry_mo (const char * value)
370  get_reg_entry_mo( void )  {      
371  {            return set_reg_entry (HKEY_CURRENT_USER, "Control Panel\\Mingw32\\NLS",
372      return get_reg_entry( HKEY_CURRENT_USER, "Control Panel\\Mingw32\\NLS", "MODir" );                            "MODir", value);
373  } /* get_reg_entry_mo */  }
374    
375    
376  static const char * cfg [] = {  char *
377          NULL,  get_reg_entry_gpg (const char *key)
378          "CacheTime",  {
379          "WordWrap",      return get_reg_entry (HKEY_CURRENT_USER, "Software\\GNU\\GnuPG", key);
380          "FastMode",  }
381          "Viewer",  
382          "KeylistMode",  
383          "WipeMode",  /* Return if possible the GPG4Win installation directory concatenated
384          "AlwaysTrust",     with the string in @path if given. */
385          "AutoBackup",  char*
386          "BackupMode",  get_reg_entry_gpg4win (const char *path)
387          "DisableHotkeys",        {
388          "NoCompressMultiMedia",      char *p, *pp;
389          "Expert",      p = get_reg_entry (HKEY_LOCAL_MACHINE,
390      };                         "Software\\GNU\\GnuPG", "Install Directory");
391        if (!p)
392            return NULL;
393  int      if (!path)
394  set_reg_winpt_single (int id, int val)          return p;
395  {      pp = new char[strlen (p) + strlen (path) + 4];
396      char buf[64];      if (!pp)
397      int rc;          BUG (NULL);
398        sprintf (pp, "%s\\%s", p, path);
399      sprintf (buf, "%d", val);      free_if_alloc (p);
400      rc = set_reg_entry (HKEY_CURRENT_USER, WINPT_REG, cfg[id], buf);      return pp;
401      return rc;  }
402  }  
403        
404    char*
405  int  get_reg_entry_mo (void)
406  get_reg_winpt_single (int id)  {      
407  {      char *p, *pp;
408      char * buf = NULL;      const char *lang;
409      int val = 0;      
410        p = get_reg_entry (HKEY_CURRENT_USER,
411      buf = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG, cfg[id]);                         "Control Panel\\Mingw32\\NLS", "MODir");
412      if (buf && *buf != ' ')      if (p)
413          val = 1;          return p;
414      else if (!buf)      
415          val = -1;      lang = get_gettext_langid ();
416      free_if_alloc (buf);      if (!lang)
417      return val;          return NULL;
418  }      pp = new char[strlen ("share\\xxxxx\\locale\\LC_MESSAGES")+8];
419        if (!pp)
420            BUG (NULL);
421  int      sprintf (pp, "share\\locale\\%s\\LC_MESSAGES", lang);
422  set_reg_winpt_prefs (winpt_reg_prefs_s * opt)      p = get_reg_entry_gpg4win (pp);
423  {      free_if_alloc (pp);
424      char buf[128];      return p;
425      int rc = 0, i;  }
426    
427      for (i=1; i < DIM (cfg); i++) {  
428          switch (i) {  /* All valid configuration commands. */
429          case CFG_CACHETIME:  static const char * cfg [] = {
430              sprintf (buf, "%d", opt->cache_time);          NULL,
431              break;          "CacheTime",
432          case CFG_WORDWRAP:          "WordWrap",
433              sprintf (buf, "%d", opt->word_wrap);          "FastMode",
434              break;          "Viewer",
435          case CFG_WIPEMODE:          "KeylistMode",
436              sprintf (buf, "%d", opt->wipe_mode);          "WipeMode",
437              break;          "AlwaysTrust",
438          case CFG_FASTMODE:          "AutoBackup",
439              sprintf (buf, "%d", opt->use_tmpfiles);          "BackupMode",
440              break;          "DisableHotkeys",      
441          case CFG_NOZIP_MMEDIA:          "NoCompressMultiMedia",
442              sprintf (buf, "%d", opt->no_zip_mmedia);          "Expert",
443              break;          "FMProgressBar",
444          case CFG_VIEWER:      };
445              sprintf (buf, "%d", opt->use_viewer);  
446              break;  
447          case CFG_KEYLISTMODE:  int
448              sprintf (buf, "%d", opt->keylist_mode);  set_reg_winpt_single (int id, int val)
449              break;  {
450          case CFG_ALWAYSTRUST:      char buf[64];
451              sprintf (buf, "%d", opt->always_trust);      int rc;
452              break;  
453          case CFG_AUTOBACKUP:      sprintf (buf, "%d", val);
454              sprintf (buf, "%d", opt->auto_backup);      rc = set_reg_entry (HKEY_CURRENT_USER, WINPT_REG, cfg[id], buf);
455              break;      return rc;
456          case CFG_AUTOBAKMODE:  }
457              sprintf (buf, "%d", opt->backup.mode);  
458              break;  
459          case CFG_DISHOTKEYS:  int
460              sprintf (buf, "%d", opt->no_hotkeys);  get_reg_winpt_single (int id)
461              break;  {
462          case CFG_EXPERT:      char * buf = NULL;
463              sprintf (buf, "%d", opt->expert);      int val = 0;
464              break;  
465          }      buf = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG, cfg[id]);
466          rc = set_reg_entry (HKEY_CURRENT_USER, WINPT_REG, cfg[i], buf);      if (buf && *buf != ' ')
467          if (rc)          val = 1;
468              goto leave;      else if (!buf)
469      }          val = -1;
470        free_if_alloc (buf);
471      if (opt->backup.path) {      return val;
472          rc = set_reg_entry (HKEY_CURRENT_USER, WINPT_REG, "BackupPath",  }
473                              opt->backup.path);  
474          if (rc)  
475              goto leave;  /* Saves the winpt preferences in the registry. */
476      }  int
477      if (opt->kserv_conf) {  set_reg_winpt_prefs (winpt_reg_prefs_s * opt)
478          rc = set_reg_entry (HKEY_CURRENT_USER, WINPT_REG, "KeyserverConfig",  {
479                              opt->kserv_conf);      char buf[128];
480          if (rc)      size_t i;
481              goto leave;      int rc = 0;
482      }  
483        for (i=1; i < DIM (cfg); i++) {
484      for (i=0; reg_hotkeys[i].reg_entry; i++) {          switch (i) {
485          strcpy (buf, " ");          case CFG_CACHETIME:
486          if (reg_hotkeys[i].enabled)              sprintf (buf, "%d", opt->cache_time);
487              strcpy (buf, reg_hotkeys[i].key);              break;
488          rc = set_reg_key (HKEY_CURRENT_USER, WINPT_REG,          case CFG_WORDWRAP:
489                            reg_hotkeys[i].reg_entry, buf);              sprintf (buf, "%d", opt->word_wrap);
490          if (rc)              break;
491              break;          case CFG_WIPEMODE:
492      }              sprintf (buf, "%d", opt->wipe_mode);
493                    break;
494  leave:          case CFG_FASTMODE:
495      if (rc) {              sprintf (buf, "%d", opt->use_tmpfiles);
496          msg_box (NULL, _("Could not write to Registry."), _("Preferences"), MB_ERR);              break;
497          return rc;          case CFG_NOZIP_MMEDIA:
498      }              sprintf (buf, "%d", opt->no_zip_mmedia);
499      return 0;              break;
500  } /* set_reg_winpt_prefs */          case CFG_VIEWER:
501                sprintf (buf, "%d", opt->use_viewer);
502                break;
503  int          case CFG_KEYLISTMODE:
504  set_reg_winpt_flag (const char * name, int val)              sprintf (buf, "%d", opt->keylist_mode);
505  {              break;
506      return set_reg_entry (HKEY_CURRENT_USER, WINPT_REG, name, val? "1" : "0");          case CFG_ALWAYSTRUST:
507  } /* set_reg_winpt_flag */              sprintf (buf, "%d", opt->always_trust);
508                break;
509            case CFG_AUTOBACKUP:
510  int              sprintf (buf, "%d", opt->auto_backup);
511  get_reg_winpt_flag (const char * name)              break;
512  {          case CFG_AUTOBAKMODE:
513      char * buf;              sprintf (buf, "%d", opt->backup.mode);
514      int flag = 0;              break;
515            case CFG_DISHOTKEYS:
516      buf = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG, name);              sprintf (buf, "%d", opt->no_hotkeys);
517      if (buf && buf[0] == '1')              break;
518          flag = 1;          case CFG_EXPERT:
519      else if (!buf || buf && buf[0] != '0')              sprintf (buf, "%d", opt->expert);
520          flag = -1;              break;
521      free_if_alloc (buf);  
522      return flag;          case CFG_FM_PROGRESS:
523  } /* get_reg_winpt_flag */              sprintf (buf, "%d", opt->fm.progress);
524                break;
525            }
526  int          rc = set_reg_entry (HKEY_CURRENT_USER, WINPT_REG, cfg[i], buf);
527  get_reg_winpt_prefs (winpt_reg_prefs_s * opt)          if (rc)
528  {              goto leave;
529      char * val = NULL;      }
530      int i;  
531        if (opt->backup.path) {
532      for (i=1; i < DIM (cfg); i++) {          rc = set_reg_entry (HKEY_CURRENT_USER, WINPT_REG, "BackupPath",
533          val = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG, cfg[i]);                              opt->backup.path);
534          if (!val || *val == ' ') {          if (rc)
535              free_if_alloc (val);              goto leave;
536              continue;      }
537          }      if (opt->kserv_conf) {
538          switch (i) {          rc = set_reg_entry (HKEY_CURRENT_USER, WINPT_REG, "KeyserverConfig",
539          case CFG_CACHETIME:                              opt->kserv_conf);
540              opt->cache_time = atol (val);          if (rc)
541              /* We do NOT support passphrase caching for more than an hour.              goto leave;
542               * Perhaps we should allow it, but for now we silently drop this.      }
543               */  
544              if (opt->cache_time > 3600)      for (i=0; reg_hotkeys[i].reg_entry; i++) {
545                  opt->cache_time = 3600;          strcpy (buf, " ");
546              break;          if (reg_hotkeys[i].enabled)
547          case CFG_WORDWRAP:              strcpy (buf, reg_hotkeys[i].key);
548              opt->word_wrap = atol (val);          rc = set_reg_key (HKEY_CURRENT_USER, WINPT_REG,
549              break;                            reg_hotkeys[i].reg_entry, buf);
550          case CFG_FASTMODE:          if (rc)
551              opt->use_tmpfiles = atol (val);              break;
552              break;      }
553          case CFG_NOZIP_MMEDIA:      
554              opt->no_zip_mmedia = atol (val);  leave:
555              break;      if (rc) {
556          case CFG_VIEWER:          msg_box (NULL, _("Could not write to Registry."), _("Preferences"), MB_ERR);
557              opt->use_viewer = atol (val);          return rc;
558              break;      }
559          case CFG_KEYLISTMODE:      return 0;
560              opt->keylist_mode = atol (val);  }
561              break;  
562          case CFG_WIPEMODE:  
563              opt->wipe_mode = atol (val);  int
564              break;  set_reg_winpt_flag (const char * name, int val)
565          case CFG_DISHOTKEYS:  {
566              opt->no_hotkeys = atol (val);      return set_reg_entry (HKEY_CURRENT_USER, WINPT_REG, name, val? "1" : "0");
567              break;  } /* set_reg_winpt_flag */
568          case CFG_ALWAYSTRUST:  
569              opt->always_trust = atol (val);  
570              break;  int
571          case CFG_AUTOBACKUP:  get_reg_winpt_flag (const char * name)
572              opt->auto_backup = atol (val);  {
573              break;      char * buf;
574          case CFG_AUTOBAKMODE:      int flag = 0;
575              opt->backup.mode = atol (val);  
576              break;      buf = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG, name);
577          case CFG_EXPERT:      if (buf && buf[0] == '1')
578              opt->expert = atol (val);          flag = 1;
579              break;      else if (!buf || buf && buf[0] != '0')
580          }          flag = -1;
581          free_if_alloc (val);      free_if_alloc (buf);
582      }          return flag;
583    } /* get_reg_winpt_flag */
584      val = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG, "BackupPath");  
585      if (val && val[0] != ' ')  
586          opt->backup.path = m_strdup (val);  /* Retrieve the winpt preferences from the registry. */
587      free_if_alloc (val);  int
588    get_reg_winpt_prefs (winpt_reg_prefs_s * opt)
589      val = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG, "KeyserverConfig");  {
590      if (val && val[0] != ' ')      char * val = NULL;
591          opt->kserv_conf = m_strdup (val);      size_t i;
592      free_if_alloc (val);  
593            for (i=1; i < DIM (cfg); i++) {
594      for (i=0; reg_hotkeys[i].reg_entry; i++) {          val = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG, cfg[i]);
595          val = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG, reg_hotkeys[i].reg_entry);          if (!val || *val == ' ') {
596          if (val && val[0] != ' ') {              free_if_alloc (val);
597              reg_hotkeys[i].key[0] = *val;              continue;
598              reg_hotkeys[i].enabled = 1;          }
599          }          switch (i) {
600          else          case CFG_CACHETIME:
601              reg_hotkeys[i].enabled = 0;              opt->cache_time = atol (val);
602          free_if_alloc (val);              /* We do NOT support passphrase caching for more than an hour.
603      }               * Perhaps we should allow it, but for now we silently drop this.
604      return 0;               */
605  } /* get_reg_winpt_prefs */              if (opt->cache_time > 3600)
606                    opt->cache_time = 3600;
607                break;
608  char *          case CFG_WORDWRAP:
609  get_reg_entry_keyserver (const char *name)              opt->word_wrap = atol (val);
610  {              break;
611      char * p = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG"\\Keyserver", name);          case CFG_FASTMODE:
612      if (p && !strcmp (p, "")) {              opt->use_tmpfiles = atol (val);
613          free_if_alloc (p);              break;
614          return NULL;          case CFG_NOZIP_MMEDIA:
615      }              opt->no_zip_mmedia = atol (val);
616      return p;              break;
617  } /* get_reg_entry_keyserver */          case CFG_VIEWER:
618                opt->use_viewer = atol (val);
619                break;
620  void          case CFG_KEYLISTMODE:
621  get_reg_proxy_prefs (char ** host, int * port, char ** user, char ** pass)              opt->keylist_mode = atol (val);
622  {              break;
623      if (host)          case CFG_WIPEMODE:
624          *host = get_reg_entry_keyserver ("Host");              opt->wipe_mode = atol (val);
625      if (user)              break;
626          *user = get_reg_entry_keyserver ("User");          case CFG_DISHOTKEYS:
627      if (pass)              opt->no_hotkeys = atol (val);
628          *pass = get_reg_entry_keyserver ("Pass");              break;
629      if (port) {          case CFG_ALWAYSTRUST:
630          char * p = get_reg_entry_keyserver ("Port");              opt->always_trust = atol (val);
631          if (p) {              break;
632              *port = atol (p);          case CFG_AUTOBACKUP:
633              free_if_alloc (p);              opt->auto_backup = atol (val);
634          }              break;
635      }          case CFG_AUTOBAKMODE:
636  } /* get_reg_proxy_prefs */              opt->backup.mode = atol (val);
637                break;
638            case CFG_EXPERT:
639  int              opt->expert = atol (val);
640  set_reg_entry_keyserver( const char * name, const char * val )              break;
641  {  
642      return set_reg_entry( HKEY_CURRENT_USER, WINPT_REG"\\Keyserver", name, val );          case CFG_FM_PROGRESS:
643  } /* set_reg_entry_keyserver */              opt->fm.progress = atol (val);
644                break;
645            }
646  int          free_if_alloc (val);
647  set_reg_proxy_prefs( const char * host, int port, const char * user, const char * pass )      }    
648  {  
649      int rc;      val = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG, "BackupPath");
650            if (val && val[0] != ' ')
651      rc = set_reg_entry_keyserver( "Host", host? host : "" );          opt->backup.path = m_strdup (val);
652      if( !rc ) {      free_if_alloc (val);
653          char buf[32];  
654          sprintf( buf, "%d", port );      val = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG, "KeyserverConfig");
655          rc = set_reg_entry_keyserver( "Port", buf );      if (val && val[0] != ' ')
656      }          opt->kserv_conf = m_strdup (val);
657      if( !rc )      free_if_alloc (val);
658          rc = set_reg_entry_keyserver( "User", user? user : "" );      
659      if( !rc )      for (i=0; reg_hotkeys[i].reg_entry; i++) {
660          rc = set_reg_entry_keyserver( "Pass", pass? pass : "" );          val = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG, reg_hotkeys[i].reg_entry);
661      return rc;          if (val && val[0] != ' ') {
662  } /* set_reg_proxy_prefs */              reg_hotkeys[i].key[0] = *val;
663                reg_hotkeys[i].enabled = 1;
664            }
665            else
666                reg_hotkeys[i].enabled = 0;
667            free_if_alloc (val);
668        }
669        return 0;
670    } /* get_reg_winpt_prefs */
671    
672    
673    char*
674    get_reg_entry_keyserver (const char *name)
675    {
676        char * p = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG"\\Keyserver", name);
677        if (p && !strcmp (p, "")) {
678            free_if_alloc (p);
679            return NULL;
680        }
681        return p;
682    }
683    
684    
685    void
686    get_reg_proxy_prefs (char ** host, int * port, char ** user, char ** pass)
687    {
688        if (host)
689            *host = get_reg_entry_keyserver ("Host");
690        if (user)
691            *user = get_reg_entry_keyserver ("User");
692        if (pass)
693            *pass = get_reg_entry_keyserver ("Pass");
694        if (port) {
695            char * p = get_reg_entry_keyserver ("Port");
696            if (p) {
697                *port = atol (p);
698                free_if_alloc (p);
699            }
700        }
701    } /* get_reg_proxy_prefs */
702    
703    
704    int
705    set_reg_entry_keyserver( const char * name, const char * val )
706    {
707        return set_reg_entry( HKEY_CURRENT_USER, WINPT_REG"\\Keyserver", name, val );
708    } /* set_reg_entry_keyserver */
709    
710    
711    int
712    set_reg_proxy_prefs( const char * host, int port, const char * user, const char * pass )
713    {
714        int rc;
715        
716        rc = set_reg_entry_keyserver( "Host", host? host : "" );
717        if( !rc ) {
718            char buf[32];
719            sprintf( buf, "%d", port );
720            rc = set_reg_entry_keyserver( "Port", buf );
721        }
722        if( !rc )
723            rc = set_reg_entry_keyserver( "User", user? user : "" );
724        if( !rc )
725            rc = set_reg_entry_keyserver( "Pass", pass? pass : "" );
726        return rc;
727    } /* set_reg_proxy_prefs */

Legend:
Removed from v.2  
changed lines
  Added in v.76

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26