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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26