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

Legend:
Removed from v.5  
changed lines
  Added in v.193

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26