/[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 68 by twoaday, Sat Nov 5 12:00:55 2005 UTC revision 179 by twoaday, Fri Feb 24 13:12:26 2006 UTC
# Line 36  Line 36 
36    
37  #define rc_ok(rc) ((rc) == ERROR_SUCCESS)  #define rc_ok(rc) ((rc) == ERROR_SUCCESS)
38    
39  static gpg_filetype gpg_filetypes[] = {  #define WINPT_REG "Software\\WinPT"
40      {"GPG Detached Signature", ".sig", 1},  
41      {"GPG Encrypted Data",     ".gpg", 2},  /* GPG file association context. */
42      {"GPG Armored Data",       ".asc", 2},  struct gpg_filetype {    
43      {0}      const char *descr;
44        const char *ext;
45        int nicon;
46  };  };
47    
48  struct reg_hotkey_s reg_hotkeys[] = {  struct reg_hotkey_s reg_hotkeys[] = {
49      {"ClipEncrypt", NULL, 0},      {"ClipEncrypt", "", 0},
50      {"ClipDecrypt", NULL, 0},      {"ClipDecrypt", "", 0},
51      {"ClipSign",    NULL, 0},      {"ClipSign",    "", 0},
52      {"ClipSignEnc", NULL, 0},      {"ClipSignEnc", "", 0},
53      {"CwsEncrypt",  NULL, 0},      {"CwsEncrypt",  "", 0},
54      {"CwsDecrypt",  NULL, 0},      {"CwsDecrypt",  "", 0},
55      {"CwsSign",     NULL, 0},      {"CwsSign",     "", 0},
56      {"CwsSignEnc",  NULL, 0},      {"CwsSignEnc",  "", 0},
57      {NULL, NULL, 0}      {NULL, "", 0}
58  };  };
59    
60    /* Global WinPT registry prefereneces. */
61  winpt_reg_prefs_s reg_prefs;  winpt_reg_prefs_s reg_prefs;
62    
63  #define WINPT_REG "Software\\WinPT"  
64    /* Return != 0 if GPG4win is installed. */
65    int
66    is_gpg4win_installed (void)
67    {
68        char *p;
69    
70        p = get_reg_entry_gpg4win (NULL);
71        if (!p)
72            return 0;
73        if (dir_exist_check (p)) {
74            free_if_alloc (p);
75            return 0;
76        }
77        free_if_alloc (p);
78        return -1;
79    }
80    
81    
82    /* Return != 0 if GPGee is installed. */
83    int
84    is_gpgee_installed (void)
85    {
86        HKEY hk;
87        LONG ec;
88    
89        ec = RegOpenKeyEx (HKEY_CURRENT_USER, "Software\\GPGee", 0, KEY_READ, &hk);
90        if (ec == ERROR_SUCCESS) {
91            RegCloseKey (hk);
92            return -1;
93        }
94    
95        return 0;
96    }
97    
98    
99    /* Free all members in the registry preference struct. */
100  void  void
101  free_reg_prefs (void)  free_reg_prefs (void)
102  {  {
# Line 67  free_reg_prefs (void) Line 104  free_reg_prefs (void)
104      free_if_alloc (reg_prefs.kserv_conf);      free_if_alloc (reg_prefs.kserv_conf);
105      free_if_alloc (reg_prefs.homedir);      free_if_alloc (reg_prefs.homedir);
106      memset (&reg_prefs, 0, sizeof reg_prefs);      memset (&reg_prefs, 0, sizeof reg_prefs);
107  } /* free_reg_prefs */  }
108    
109    
110  /* Register the given WinPT filetype. */  /* Register the given WinPT filetype. */
111  static int  static int
112  regist_single_filetype (gpg_filetype *gfile)  regist_single_filetype (gpg_filetype *gfile)
113  {  {
114      char icon[256], prog[256];      char icon[256];
115        char prog[256];
116            
117      memset (&icon, 0, sizeof (icon));      memset (&icon, 0, sizeof (icon));
118      GetModuleFileName (glob_hinst, prog, sizeof (prog)-1);      GetModuleFileName (glob_hinst, prog, sizeof (prog)-1);
119      _snprintf (icon, sizeof (icon) -1, "%s,%d", prog, gfile->nicon);      _snprintf (icon, sizeof (icon) -1, "%s,%d", prog, gfile->nicon);
120      return create_file_type (prog, gfile->ext, gfile->descr, icon);      return create_file_type (prog, gfile->ext, gfile->descr, icon);
121  } /* regist_single_filetype */  }
122    
123    
124  /* Install the GPG related into the W32 resgistry, if the entry already  /* Install the GPG related into the W32 resgistry, if the entry already
# Line 113  regist_inst_gnupg (int create_mokey) Line 151  regist_inst_gnupg (int create_mokey)
151      }      }
152    
153      return 0;      return 0;
154  } /* regist_inst_gpg */  }
155    
156    
157  /* Install WinPT into the W32 registry, if the entry already  /* Install WinPT into the W32 registry, if the entry already
158     exists the function returns immediately.*/     exists the function returns immediately. @with_ext can be
159       used to register some file types (if 1). @created contains
160       1 if the registry key was created.
161       Return value: 0 on success. */
162  int  int
163  regist_inst_winpt (int with_ext, int * created)  regist_inst_winpt (int with_ext, int *created)
164  {  {    
165      HKEY reg;      HKEY reg;
166      char * p = NULL;      char *p = NULL;
167      int rc, i, id, n = 0;      char modpath[MAX_PATH+1];
168        int rc, i, id, n;
169    
170        gpg_filetype gpg_filetypes[] = {
171            {_("GPG Detached Signature"), ".sig", 1},
172            {_("GPG Encrypted Data"),     ".gpg", 2},
173            {_("GPG Armored Data"),       ".asc", 2},
174            {0}
175        };
176    
177      if( created )      if (created)
178          *created = 0;          *created = 0;
179        if (is_gpgee_installed ())
     p = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG, "Extensions");  
     if (p && *p == '1')  
180          with_ext = 0;          with_ext = 0;
     free_if_alloc( p );  
181    
182      if( with_ext ) {      rc = RegOpenKeyEx (HKEY_CURRENT_USER, WINPT_REG, 0, KEY_READ, &reg);
183          id = msg_box( NULL, _("WinPT can register some GPG file types for you so they can "      if (rc_ok (rc)) {
184                                "be processed with a double click in the explorer.\n"          RegCloseKey (reg);
185                                "Do you want to continue?"), _("WinPT"), MB_YESNO|MB_INFO );          rc = RegOpenKeyEx (HKEY_CURRENT_USER, WINPT_REG"\\Keyserver", 0, KEY_READ, &reg);
186          if( id == IDNO ) {          if (!rc_ok (rc)) {
187              set_reg_entry( HKEY_CURRENT_USER, WINPT_REG, "Extensions", "1" );              RegCreateKey (HKEY_CURRENT_USER, WINPT_REG"\\Keyserver", &reg);
188              goto start;              RegCloseKey (reg);
         }  
         for( i = 0; gpg_filetypes[i].ext; i++ ) {  
             rc = RegOpenKeyEx( HKEY_CLASSES_ROOT, gpg_filetypes[i].ext, 0, KEY_READ, &reg );  
             if( rc_ok( rc ) ) {  
                 RegCloseKey( reg );  
                 id = log_box( _("WinPT WARNING"), MB_YESNO|MB_INFO,  
                               _("It seems there was already a '%s' file type registered by another application.\n"  
                                 "Do you want to overwrite it?"), gpg_filetypes[i].ext );  
                 if( id == IDNO )  
                     continue;  
             }  
             regist_single_filetype( &gpg_filetypes[i] );  
             n++;  
         }  
     }  
           
 start:  
     rc = RegOpenKeyEx( HKEY_CURRENT_USER, WINPT_REG, 0, KEY_READ, &reg );  
     if( rc_ok( rc ) ) {  
         RegCloseKey( reg );  
         rc = RegOpenKeyEx( HKEY_CURRENT_USER, WINPT_REG"\\Keyserver", 0, KEY_READ, &reg );  
         if( !rc_ok( rc ) ) {  
             RegCreateKey( HKEY_CURRENT_USER, WINPT_REG"\\Keyserver", &reg );  
             RegCloseKey( reg );  
189          }          }
190          p = get_reg_entry_keyserver ("Default");          p = get_reg_entry_keyserver ("Default");
191          if (!p) {          if (!p) {
# Line 172  start: Line 194  start:
194              set_reg_entry_keyserver ("Default_Port", buf);              set_reg_entry_keyserver ("Default_Port", buf);
195              set_reg_entry_keyserver ("Default", DEF_HKP_KEYSERVER);              set_reg_entry_keyserver ("Default", DEF_HKP_KEYSERVER);
196          }          }
197          free_if_alloc( p );          free_if_alloc (p);
         if( n )  
             set_reg_entry( HKEY_CURRENT_USER, WINPT_REG, "Extensions", "1" );  
198          return 0;          return 0;
199      }      }
200      rc = RegCreateKey( HKEY_CURRENT_USER, WINPT_REG, &reg );      rc = RegCreateKey (HKEY_CURRENT_USER, WINPT_REG, &reg);
201      if( !rc_ok( rc ) )      if (!rc_ok (rc))
202          return WPTERR_REGISTRY;          return WPTERR_REGISTRY;
203      if( created )      if (created)
204          *created = 1;          *created = 1;
205      RegCloseKey( reg );      RegCloseKey (reg);
206      if( n )      if (with_ext) {
207          set_reg_entry( HKEY_CURRENT_USER, WINPT_REG, "Extensions", "1" );          id = msg_box (NULL, _("WinPT can register some GPG file types for you so they can "
208                                  "be processed with a double click in the explorer.\n"
209                                  "Do you want to continue?"), _("WinPT"), MB_YESNO|MB_INFO);
210            if (id == IDYES) {
211                for (i = 0; gpg_filetypes[i].ext; i++) {
212                    rc = RegOpenKeyEx (HKEY_CLASSES_ROOT, gpg_filetypes[i].ext, 0, KEY_READ, &reg);
213                    if (rc_ok (rc)) {
214                    RegCloseKey (reg);
215                        id = log_box (_("WinPT WARNING"), MB_YESNO|MB_INFO,
216                                      _("It seems there was already a '%s' file type registered by another application.\n"
217                                        "Do you want to overwrite it?"), gpg_filetypes[i].ext);
218                        if (id == IDNO)
219                            continue;
220                    }
221                    regist_single_filetype (&gpg_filetypes[i]);
222                }
223            }
224        }
225        if ((n=GetModuleFileName (NULL, modpath, MAX_PATH-1)) > 0) {
226            while (n-- > 0) {
227                if (modpath[n] == '\\') {
228                    modpath[n] = 0;
229                    break;
230                }
231            }
232            set_reg_entry (HKEY_CURRENT_USER, WINPT_REG, "Install Directory", modpath);
233        }
234      return 0;      return 0;
235  } /* regist_inst_winpt */  }
236    
237    
238  /* Create a new filetype in the W32 registry.  /* Create a new filetype in the W32 registry.
239     We should really care of errors! Otherwise we can damage the registry! */     We should really care of errors! Otherwise we can damage the registry! */
240  int  int
241  create_file_type( const char *exefile, const char *ext, const char *extname, char *iconfile )  create_file_type (const char *exefile, const char *ext,
242                      const char *extname, char *iconfile)
243  {  {
244      int rc;      int rc;
245      HKEY reg = NULL;      HKEY reg = NULL;
# Line 258  expand_path (const char *path) Line 305  expand_path (const char *path)
305          return NULL;          return NULL;
306      len += 1;      len += 1;
307      p = new char[len+1];      p = new char[len+1];
308      if( !p )      if (!p)
309          return NULL;          return NULL;
310      len = ExpandEnvironmentStrings (path, p, len);      len = ExpandEnvironmentStrings (path, p, len);
311      if (!len) {      if (!len) {
# Line 266  expand_path (const char *path) Line 313  expand_path (const char *path)
313          return NULL;          return NULL;
314      }      }
315      return p;      return p;
316  } /* expand_path */  }
317    
318    
319  /* Retrieve a registry entry with the directory given in @dir  /* Retrieve a registry entry with the directory given in @dir
# Line 277  get_reg_entry (HKEY root_key, const char Line 324  get_reg_entry (HKEY root_key, const char
324  {  {
325      int rc;      int rc;
326      char text[384] = {0};      char text[384] = {0};
327      DWORD nbytes, type, n1 = 0;      DWORD nbytes, type;
328      HKEY reg_key = NULL;      HKEY reg_key = NULL;
329      char * p = NULL, * tmp = NULL;      char * p = NULL;
330            
331      rc = RegOpenKeyEx (root_key, dir, 0, KEY_QUERY_VALUE, &reg_key);      rc = RegOpenKeyEx (root_key, dir, 0, KEY_QUERY_VALUE, &reg_key);
332      if( !rc_ok( rc ) )      if( !rc_ok( rc ) )
# Line 373  set_reg_entry_mo (const char * value) Line 420  set_reg_entry_mo (const char * value)
420  }  }
421    
422    
423  char *  char*
424  get_reg_entry_gpg (const char *key)  get_reg_entry_gpg (const char *key)
425  {  {
426      return get_reg_entry (HKEY_CURRENT_USER, "Software\\GNU\\GnuPG", key);      char *p;
427        p = get_reg_entry (HKEY_CURRENT_USER, "Software\\GNU\\GnuPG", key);
428        if (!p || strlen (p) == 0) {
429            free_if_alloc (p);
430            return NULL;
431        }
432        return p;
433  }  }
434    
435    
436  char *  /* Return if possible the GPG4Win installation directory concatenated
437  get_reg_entry_mo(void)     with the string in @path if given. */
438    char*
439    get_reg_entry_gpg4win (const char *path)
440    {
441        char *p, *pp;
442        p = get_reg_entry (HKEY_LOCAL_MACHINE,
443                           "Software\\GNU\\GnuPG", "Install Directory");
444        if (!p)
445            return NULL;
446        if (!path)
447            return p;
448        pp = new char[strlen (p) + strlen (path) + 4];
449        if (!pp)
450            BUG (NULL);
451        sprintf (pp, "%s\\%s", p, path);
452        free_if_alloc (p);
453        return pp;
454    }
455    
456        
457    char*
458    get_reg_entry_mo (void)
459  {        {      
460      return get_reg_entry(HKEY_CURRENT_USER,      char *p, *pp;
461                           "Control Panel\\Mingw32\\NLS", "MODir");      const char *lang;
462    
463        p = get_reg_entry (HKEY_CURRENT_USER,
464                           "Control Panel\\Mingw32\\NLS", "MODir");
465        if (p)
466            return p;
467    
468        lang = get_gettext_langid ();
469        if (!lang)    
470            return NULL;    
471        pp = new char[strlen ("share\\xxxxx\\locale\\LC_MESSAGES")+8];
472        if (!pp)    
473            BUG (NULL);
474        sprintf (pp, "share\\locale\\%s\\LC_MESSAGES", lang);
475        p = get_reg_entry_gpg4win (pp);
476        free_if_alloc (pp);
477        return p;
478  }  }
479    
480    
481  /* All valid configuration commands. */  /* All valid configuration commands. */
482  static const char * cfg [] = {  static const char *cfg [] = {
483          NULL,      NULL,      
484          "CacheTime",      "CacheTime",
485          "WordWrap",      "WordWrap",
486          "FastMode",      "FastMode",
487          "Viewer",      "Viewer",
488          "KeylistMode",      "WipeMode",
489          "WipeMode",      "AlwaysTrust",
490          "AlwaysTrust",      "AutoBackup",
491          "AutoBackup",      "BackupMode",
492          "BackupMode",      "DisableHotkeys",  
493          "DisableHotkeys",            "NoCompressMultiMedia",    
494          "NoCompressMultiMedia",      "Expert",
495          "Expert",      "FMProgressBar",
496          "FMProgressBar",  };
     };  
497    
498    
499  int  int
# Line 440  int Line 529  int
529  set_reg_winpt_prefs (winpt_reg_prefs_s * opt)  set_reg_winpt_prefs (winpt_reg_prefs_s * opt)
530  {  {
531      char buf[128];      char buf[128];
532      int rc = 0, i;      size_t i;
533        int rc = 0;
534    
535      for (i=1; i < DIM (cfg); i++) {      for (i=1; i < DIM (cfg); i++) {
536          switch (i) {          switch (i) {
# Line 462  set_reg_winpt_prefs (winpt_reg_prefs_s * Line 552  set_reg_winpt_prefs (winpt_reg_prefs_s *
552          case CFG_VIEWER:          case CFG_VIEWER:
553              sprintf (buf, "%d", opt->use_viewer);              sprintf (buf, "%d", opt->use_viewer);
554              break;              break;
         case CFG_KEYLISTMODE:  
             sprintf (buf, "%d", opt->keylist_mode);  
             break;  
555          case CFG_ALWAYSTRUST:          case CFG_ALWAYSTRUST:
556              sprintf (buf, "%d", opt->always_trust);              sprintf (buf, "%d", opt->always_trust);
557              break;              break;
# Line 496  set_reg_winpt_prefs (winpt_reg_prefs_s * Line 583  set_reg_winpt_prefs (winpt_reg_prefs_s *
583          if (rc)          if (rc)
584              goto leave;              goto leave;
585      }      }
     if (opt->kserv_conf) {  
         rc = set_reg_entry (HKEY_CURRENT_USER, WINPT_REG, "KeyserverConfig",  
                             opt->kserv_conf);  
         if (rc)  
             goto leave;  
     }  
586    
587      for (i=0; reg_hotkeys[i].reg_entry; i++) {      for (i=0; reg_hotkeys[i].reg_entry; i++) {
588          strcpy (buf, " ");          strcpy (buf, " ");
# Line 549  get_reg_winpt_flag (const char * name) Line 630  get_reg_winpt_flag (const char * name)
630  int  int
631  get_reg_winpt_prefs (winpt_reg_prefs_s * opt)  get_reg_winpt_prefs (winpt_reg_prefs_s * opt)
632  {  {
633      char * val = NULL;      char *val = NULL;
634      int i;      size_t i;
635    
636      for (i=1; i < DIM (cfg); i++) {      for (i=1; i < DIM (cfg); i++) {
637          val = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG, cfg[i]);          val = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG, cfg[i]);
# Line 579  get_reg_winpt_prefs (winpt_reg_prefs_s * Line 660  get_reg_winpt_prefs (winpt_reg_prefs_s *
660          case CFG_VIEWER:          case CFG_VIEWER:
661              opt->use_viewer = atol (val);              opt->use_viewer = atol (val);
662              break;              break;
         case CFG_KEYLISTMODE:  
             opt->keylist_mode = atol (val);  
             break;  
663          case CFG_WIPEMODE:          case CFG_WIPEMODE:
664              opt->wipe_mode = atol (val);              opt->wipe_mode = atol (val);
665              break;              break;
# Line 612  get_reg_winpt_prefs (winpt_reg_prefs_s * Line 690  get_reg_winpt_prefs (winpt_reg_prefs_s *
690      if (val && val[0] != ' ')      if (val && val[0] != ' ')
691          opt->backup.path = m_strdup (val);          opt->backup.path = m_strdup (val);
692      free_if_alloc (val);      free_if_alloc (val);
   
     val = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG, "KeyserverConfig");  
     if (val && val[0] != ' ')  
         opt->kserv_conf = m_strdup (val);  
     free_if_alloc (val);  
693            
694      for (i=0; reg_hotkeys[i].reg_entry; i++) {      for (i=0; reg_hotkeys[i].reg_entry; i++) {
695          val = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG, reg_hotkeys[i].reg_entry);          val = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG, reg_hotkeys[i].reg_entry);

Legend:
Removed from v.68  
changed lines
  Added in v.179

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26