root/gsdl/trunk/runtime-src/src/w32server/settings.cpp @ 18018

Revision 18018, 27.3 KB (checked in by ak19, 12 years ago)

Max discovered that gsdlsite.cfg set the logfile to the path c:\gsdl.log. What if the main drive was D: or something else? Also, it would help for the zip releases of GS2 if the logfile path also made use of gsdlhome (as other gsdlsite.cfg properties do) instead of using a location external to gsdlhome. The template values in gsdlsite.cfg are replaced with the value of gsdlhome where required, including now for the logfilename property.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1/**********************************************************************
2 *
3 * settings.cpp
4 * Copyright (C) 1996
5 *
6 * A component of the fnord webserver written by bmorin@wpi.edu.
7 *
8 * Altered for use with the Greenstone digital library software by the
9 * New Zealand Digital Library Project at the University of Waikato,
10 * New Zealand.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *
26 *********************************************************************/
27
28#include "text_t.h"
29#include "fileutil.h"
30
31#if defined(GSDL_USE_OBJECTSPACE)
32#  include <ospace\std\fstream>
33#elif defined(GSDL_USE_IOS_H)
34#  include <fstream.h>
35#else
36#  include <fstream>
37#endif
38
39#include <windows.h>
40#include "httpsrv.h"
41#include "settings.h"
42#include "locate.h"
43#include "resource.h"
44#include "cgiwrapper.h"
45
46// library settings
47text_t gsdl_enterlib;
48text_t gsdl_gsdlhome;
49text_t gsdl_collecthome;
50text_t gsdl_dbhome;
51text_t gsdl_collections;
52colinfo_tmap gsdl_collectinfo;
53
54char gsdl_staticpages[MAX_FILENAME_SIZE];
55
56// debug settings
57char gsdl_log_name[MAX_FILENAME_SIZE] = "c:\\gsdl.log";
58int gsdl_keep_log = 0;
59int gsdl_show_console = 0;
60
61// general settings
62int gsdl_port_num = 80;
63int gsdl_auto_enter = 0;
64int gsdl_browser = GS_DEFAULT;
65char gsdl_browser_exe[MAX_FILENAME_SIZE] = "";
66text_t gsdl_url;
67text_t gsdl_conffile;
68int gsdl_start_browser = 1;
69int gsdl_address_resolution_method = 1;  // Get an IP, but don't resolve to a name
70
71// private data
72
73// pathnames of the latest installed versions of netscape and
74// internet explorer
75static char netscape_exe[MAX_FILENAME_SIZE]="";
76static char iexplore_exe[MAX_FILENAME_SIZE]="";
77
78// pathname of the default browser
79static char default_browser_exe[MAX_FILENAME_SIZE]="";
80
81// whether netscape is needed (used in the settings dialog box)
82static int gsdl_netscapeneeded = 0;
83
84static void remove_end_slashes(char *str) {
85  int len = strlen (str);
86  while ((len > 0) && ((str[len-1] == '\\') || (str[len-1] == '/'))) {
87    --len;
88  }
89  str[len] = '\0';
90}
91
92static void remove_end_slashes (text_t &str) {
93  char *cstr = str.getcstr();
94  remove_end_slashes (cstr);
95  str = cstr;
96  delete []cstr;
97}
98
99// returns 1 if successful, 0 otherwise
100// checks for a file's existence
101static int check_program_exe (char *path) {
102  UINT curerrormode;
103  OFSTRUCT reOpenBuff;
104  int retvalue = 0;
105 
106  reOpenBuff.cBytes = sizeof (OFSTRUCT);
107 
108  curerrormode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
109  if (OpenFile(path, &reOpenBuff, OF_EXIST) != HFILE_ERROR) retvalue = 1;
110  SetErrorMode(curerrormode);
111 
112  return retvalue;
113}
114
115
116// returns 1 if successful, 0 otherwise
117static int get_default_browser_path (char *providedhtmltype, char *path, int maxsize) {
118  HKEY hk1, hk2;
119  char htmltype[256];
120  long size;
121  int retval = 0;
122 
123  htmltype[0] = '\0';
124  path[0] = '\0';
125 
126  // try and get information about the default browser from the
127  // registry
128 
129  if (providedhtmltype != NULL) strcpy (htmltype, providedhtmltype);
130 
131  else if (RegOpenKey(HKEY_CLASSES_ROOT, ".htm", &hk1) == ERROR_SUCCESS) {
132    size = 256;
133    RegQueryValue(hk1, NULL, htmltype, &size);
134    RegCloseKey(hk1);
135  }
136 
137  if (RegOpenKey(HKEY_CLASSES_ROOT, htmltype, &hk1) == ERROR_SUCCESS) {
138    if (RegOpenKey(hk1, "shell\\open", &hk2) == ERROR_SUCCESS) {
139      // look for the command to start the browser
140      size = maxsize;
141      if (RegQueryValue(hk2,"command",path,&size) == ERROR_SUCCESS) {
142                // strip all excess stuff off the command
143    char endchar = ' ';
144    int i = 0, len = 0;
145    int state = 0; // 0=strip initial white-space and quotes, 1=grab path
146    while (path[i] != '\0') {
147      if (state == 0) {
148        if (path[i] == ' ') {
149          // do nothing
150        } else if (path[i] == '"') {
151          endchar = '"';
152          state = 1;
153        } else {
154          state = 1;
155          path[len] = path[i];
156          ++len;
157        }
158      } else {
159        if (path[i] == endchar) break;
160        path[len] = path[i];
161        ++len;
162      }
163      ++i;
164    }
165    path[len] = '\0';
166   
167    // check the resulting pathname
168    if (check_program_exe (path)) {
169      retval = 1; // succeeded
170    } else {
171      path[0] = '\0'; // failed (must have been deleted or something...)
172    }
173      }
174      RegCloseKey(hk2);
175    }
176    RegCloseKey(hk1);
177  }
178 
179  return retval;
180}
181
182
183// returns 1 if successful, 0 otherwise
184// note that appname should include .exe (e.g. netscape.exe)
185static int registry_get_app_paths (char *appname, char *path, int maxsize) {
186  HKEY hk1;
187  long size;
188  int retval = 0;
189 
190  path[0] = '\0';
191 
192  if (RegOpenKey(HKEY_LOCAL_MACHINE,
193         "Software\\Microsoft\\Windows\\CurrentVersion\\App Paths",
194         &hk1) == ERROR_SUCCESS) {
195    size = maxsize;
196    if (RegQueryValue (hk1, appname, path, &size) == ERROR_SUCCESS &&
197    check_program_exe (path));
198    retval = 1;
199   
200    RegCloseKey(hk1);
201  }
202 
203  return retval;
204}
205
206static void check_installed_browsers (int netscapeneeded) {
207  netscape_exe[0] = '\0';
208  iexplore_exe[0] = '\0';
209  default_browser_exe[0] = '\0';
210 
211  // update the default browser information (if netscape isn't needed)
212  if (!netscapeneeded) {
213    get_default_browser_path (NULL, default_browser_exe, MAX_FILENAME_SIZE);
214    _strlwr (default_browser_exe);
215  }
216 
217  // look for netscape
218 
219  // first look for netscape in the registry
220  if (!registry_get_app_paths("netscape.exe", netscape_exe, MAX_FILENAME_SIZE)) {
221    // next look for "NetscapeMarkup"
222    if (get_default_browser_path ("NetscapeMarkup", netscape_exe, MAX_FILENAME_SIZE)) {
223      _strlwr (netscape_exe);
224      if (!strstr (netscape_exe, "netscape.exe")) {
225    netscape_exe[0] = '\0';
226   
227    // next look for \netscape\netscape.exe
228   
229    // get drive letter of the windows directory
230    GetWindowsDirectory(netscape_exe, MAX_FILENAME_SIZE);
231    netscape_exe[1] = '\0';
232    strcat (netscape_exe, ":\\netscape\\netscape.exe");
233   
234    if (!check_program_exe (netscape_exe)) {
235      // lastly look for \netscape\program\netscape.exe
236      netscape_exe[1] = '\0';
237      strcat (netscape_exe, ":\\netscape\\program\\netscape.exe");
238     
239    }
240      }
241    }
242  }
243  _strlwr (netscape_exe);
244  if ((strlen (netscape_exe) > 0) && !check_program_exe (netscape_exe)) {
245    netscape_exe[0] = '\0';  // couldn't find netscape
246  }
247 
248  // look for internet explorer (if netscape isn't needed)
249 
250  // first look for iexplore in the registry
251  if (!netscapeneeded && !registry_get_app_paths("iexplore.exe",
252                         iexplore_exe, MAX_FILENAME_SIZE)) {
253    // next look for "htmlfile"
254    if (get_default_browser_path ("htmlfile", iexplore_exe, MAX_FILENAME_SIZE)) {
255      _strlwr (iexplore_exe);
256      if (!strstr (iexplore_exe, "iexplore.exe")) {
257    iexplore_exe[0] = '\0';
258   
259    // next look for \iexplore\iexplore.exe
260   
261    // get drive letter of the windows directory
262    GetWindowsDirectory(iexplore_exe, MAX_FILENAME_SIZE);
263    iexplore_exe[1] = '\0';
264    strcat (iexplore_exe, ":\\iexplore\\iexplore.exe");
265      }
266    }
267  }
268  _strlwr (iexplore_exe);
269  if ((strlen (iexplore_exe) > 0) && !check_program_exe (iexplore_exe)) {
270    iexplore_exe[0] = '\0';  // couldn't find internet explorer
271  }
272}
273
274void write_settings (const text_t url) {
275 
276  if (gsdl_conffile.empty()) {
277    find_location();
278    gsdl_conffile = filename_cat(data_location, "gsdlsite.cfg");
279  }
280
281  char *conffile_c = gsdl_conffile.getcstr();
282  ofstream fout (conffile_c);
283  delete []conffile_c;
284  if (fout) {
285   
286    outconvertclass text_t2ascii;
287
288    // top (gsdl) level stuff
289    fout << "[gsdl]\n";
290   
291    if (!url.empty()) {
292      // url entry should only be written to gsdlsite.cfg when the
293      // server is actually running, not when it's stopped (since we
294      // can't be sure what the url is at that stage). That is,
295      // write_settings() is called with the url argument set when the
296      // server first starts up, it's then called without url set as
297      // the server shuts down.
298      write_ini_line(fout, "url", url);     
299    }
300
301    write_ini_line(fout, "enterlib", gsdl_enterlib);
302    write_ini_line(fout, "gsdlhome", gsdl_gsdlhome);
303    write_ini_line(fout, "collecthome", gsdl_collecthome);
304    write_ini_line(fout, "gdbmhome", gsdl_dbhome);
305   
306    write_ini_line(fout, "logfilename", gsdl_log_name);
307    write_ini_line(fout, "keeplog", text_t(gsdl_keep_log));
308    write_ini_line(fout, "consolelog", text_t(gsdl_show_console));
309   
310    write_ini_line(fout, "portnumber", text_t(gsdl_port_num));
311    write_ini_line(fout, "autoenter", text_t(gsdl_auto_enter));
312    write_ini_line(fout, "browser", text_t(gsdl_browser));
313    write_ini_line(fout, "browserexe", gsdl_browser_exe);
314    write_ini_line(fout, "collections", gsdl_collections);
315    write_ini_line(fout, "start_browser", text_t(gsdl_start_browser));
316    write_ini_line(fout, "address_resolution_method", text_t(gsdl_address_resolution_method));
317
318    // collection levels
319    colinfo_tmap::iterator here = gsdl_collectinfo.begin();
320    colinfo_tmap::iterator end = gsdl_collectinfo.end();
321    while (here != end) {
322      fout << text_t2ascii << "[" << (*here).first << "]\n";
323      if (!(*here).second.gsdl_gsdlhome.empty())
324    write_ini_line(fout, "gsdlhome", (*here).second.gsdl_gsdlhome);
325      if (!(*here).second.gsdl_collecthome.empty())
326    write_ini_line(fout, "collecthome", (*here).second.gsdl_collecthome);
327      if (!(*here).second.gsdl_dbhome.empty())
328    write_ini_line(fout, "gdbmhome", (*here).second.gsdl_dbhome);
329     
330      ++here;
331    }
332    fout.close();
333  }
334}
335
336
337// browser_compare assumes that check_exe is in lowercase
338static int browser_compare (char *browser_exe_path, char *check_exe) {
339  char tmp_exe[MAX_FILENAME_SIZE];
340  strcpy (tmp_exe, browser_exe_path);
341  _strlwr (tmp_exe);
342  return (strstr (tmp_exe, check_exe) != NULL);
343}
344
345// This function assumes that check_installed_browsers
346// has been recently called
347// It also assumes that browser_exe is large
348// enough to receive any path names to any browser
349static void check_browser_settings (int &browser, char *browser_exe,
350                    int netscapeneeded) {
351  // sort out which browser to use
352  if ((browser == GS_NETSCAPE) && (netscape_exe[0] == '\0'))
353    browser = GS_IEXPLORE;
354  if ((browser == GS_IEXPLORE) && (netscapeneeded || (iexplore_exe[0] == '\0'))) {
355    if (netscape_exe[0] != '\0') browser = GS_NETSCAPE;
356    else browser = GS_OTHER;
357  }
358  if ((browser == GS_OTHER) && netscapeneeded &&
359      !browser_compare (browser_exe, "netscape.exe") && (netscape_exe[0] != '\0')) {
360    // this other browser isn't netscape and netscape is available
361    browser = GS_NETSCAPE;
362  }
363 
364 
365  // get the browser's executable
366  if (browser == GS_DEFAULT) {
367    strcpy (browser_exe, default_browser_exe);
368  } else if (browser == GS_NETSCAPE) {
369    strcpy (browser_exe, netscape_exe);
370  } else if (browser == GS_IEXPLORE) {
371    strcpy (browser_exe, iexplore_exe);
372  } else {
373    browser = GS_OTHER;
374   
375    // if the browser should be netscape then check to
376    // make sure it is
377    if (netscapeneeded && !browser_compare (browser_exe, "netscape.exe")) {
378      // not netscape, set to no browser
379      browser_exe[0] = '\0';
380    }
381  }
382}
383
384
385static void revert_defaults (int netscapeneeded) {
386  find_location();
387 
388  // library settings
389  gsdl_enterlib = "/gsdl";
390  gsdl_gsdlhome = data_location;
391  gsdl_collecthome = filename_cat(data_location,"collect");
392  gsdl_dbhome = data_location;
393 
394  // debug settings
395  gsdl_keep_log = 0;
396  char *cstr_value = filename_cat(data_location,"gsdl.log").getcstr();
397  strcpy (gsdl_log_name, cstr_value); //strcpy (gsdl_log_name, "c:\\gsdl.log");
398  delete []cstr_value;
399  gsdl_show_console = 0;
400 
401  // general settings
402  gsdl_port_num = 80;
403  gsdl_auto_enter = 0;
404  gsdl_start_browser = 1;
405
406  check_installed_browsers (netscapeneeded);
407  gsdl_browser = GS_DEFAULT;
408  strcpy (gsdl_browser_exe, default_browser_exe);
409  check_browser_settings (gsdl_browser, gsdl_browser_exe, netscapeneeded);
410}
411
412void read_settings (int netscapeneeded) {
413 
414  if (gsdl_conffile.empty()) {
415    find_location();
416    gsdl_conffile = filename_cat(data_location, "gsdlsite.cfg");
417  }
418 
419  // set up defaults
420  revert_defaults (netscapeneeded);
421 
422  text_t key, value, section;
423  char *cstr_value;
424  char *conffile_c = gsdl_conffile.getcstr();
425#if defined (GSDL_USE_IOS_H)
426  ifstream conf (conffile_c, ios::nocreate);
427#else
428  ifstream conf (conffile_c);
429#endif
430  delete []conffile_c;
431  if (conf) {
432    while (read_ini_line(conf, key, value) >= 0) {
433      if (key.empty()) continue;
434      if (value.empty()) {
435    // should be a section title
436    section = key;
437    // remove square brackets
438    section.erase (section.begin());
439    section.erase (section.end()-1);
440
441      } else {
442
443    cstr_value = value.getcstr ();
444     
445    if (section == "gsdl") {
446
447      if (key == "enterlib") {
448        gsdl_enterlib = value;
449     
450        // 'logfilename' should occur in the configuration file
451        // before 'keeplog'
452      } else if (key == "logfilename") {
453        strcpy (gsdl_log_name, cstr_value);
454     
455      } else if (key == "keeplog") {
456        gsdl_keep_log = value.getint();
457        if (gsdl_keep_log) open_log_file();
458   
459      } else if (key == "consolelog") {
460        if (value.getint()) activate_console();
461        else deactivate_console();
462   
463      } else if (key == "portnumber") {
464        gsdl_port_num = value.getint();
465       
466      } else if (key == "autoenter") {
467        gsdl_auto_enter = value.getint();
468       
469      } else if (key == "start_browser") {
470        gsdl_start_browser = value.getint();
471   
472      } else if (key == "browser") {
473        gsdl_browser = value.getint();
474   
475      } else if (key == "browserexe") {
476        strcpy (gsdl_browser_exe, cstr_value);
477
478      } else if (key == "address_resolution_method") {
479        gsdl_address_resolution_method = value.getint();
480
481      } else if (key == "collections") {
482        gsdl_collections = value;
483      }
484      // gsdlhome must occur in file before dbhome     
485      else if (key == "gsdlhome") {
486        gsdl_gsdlhome = value;
487        gsdl_collecthome = filename_cat(value,"collect");
488        gsdl_dbhome = value;     
489      }
490      else if (key == "collecthome") {
491        gsdl_collecthome = value;
492      }
493      else if (key == "gdbmhome") {
494        gsdl_dbhome = value;
495      }
496    } else {
497     
498      // gsdlhome must occur in file before dbhome
499      if (key == "gsdlhome") {
500        gsdl_collectinfo[section].gsdl_gsdlhome = value;
501        gsdl_collectinfo[section].gsdl_collecthome = filename_cat(value,"collect");
502        gsdl_collectinfo[section].gsdl_dbhome = value;
503      }
504      else if (key == "collecthome") {
505        gsdl_collectinfo[section].gsdl_collecthome = value;
506      }
507      else if (key == "gdbmhome") {
508        gsdl_collectinfo[section].gsdl_dbhome = value;
509      }
510    }
511    delete []cstr_value;
512      }
513    }
514    conf.close();
515  }
516 
517  // check the homes to make sure they don't contain
518  // extra slashes at the end
519  remove_end_slashes (gsdl_gsdlhome);
520  remove_end_slashes (gsdl_collecthome);
521  remove_end_slashes (gsdl_dbhome);
522  colinfo_tmap::iterator here = gsdl_collectinfo.begin();
523  colinfo_tmap::iterator end = gsdl_collectinfo.end();
524  while (here != end) {
525    remove_end_slashes ((*here).second.gsdl_gsdlhome);
526    remove_end_slashes ((*here).second.gsdl_collecthome);
527    remove_end_slashes ((*here).second.gsdl_dbhome);
528    ++here;
529  }
530
531  // check the browser settings
532  check_browser_settings (gsdl_browser, gsdl_browser_exe, netscapeneeded);
533}
534
535void gsdl_check_browser_settings (int netscapeneeded) {
536  check_installed_browsers (netscapeneeded);
537  check_browser_settings (gsdl_browser, gsdl_browser_exe, netscapeneeded);
538}
539
540
541static void dialog_update_enables (HWND hwndDlg) {
542  int res;
543 
544  // if they want to use another browser, they should be able
545  // edit its filename
546  res = SendDlgItemMessage(hwndDlg, ID_RADIO_OTHER, BM_GETCHECK,0,0);
547  SendDlgItemMessage(hwndDlg, ID_OTHER_NAME, WM_ENABLE, (res == 1), 0);
548}
549
550
551static int read_dialog_browser_field (HWND hwndDlg) {
552  if (SendDlgItemMessage(hwndDlg, ID_RADIO_DEFAULT,
553             BM_GETCHECK, 0, 0) == 1) return GS_DEFAULT;
554  if (SendDlgItemMessage(hwndDlg, ID_RADIO_NETSCAPE,
555             BM_GETCHECK, 0, 0) == 1) return GS_NETSCAPE;
556  if (SendDlgItemMessage(hwndDlg, ID_RADIO_IE,
557             BM_GETCHECK, 0, 0) == 1) return GS_IEXPLORE;
558  if (SendDlgItemMessage(hwndDlg, ID_RADIO_OTHER,
559             BM_GETCHECK, 0, 0) == 1) return GS_OTHER;
560  return GS_NETSCAPE;
561}
562
563
564static void set_dialog_browser_field (HWND hwndDlg, int browser, char *othername) {
565  // if we are trying to set the browser to default, netscape or
566  // internet explorer and we can't find them, set the browser
567  // to 'other'
568  if ((browser == GS_DEFAULT) && (default_browser_exe[0] == '\0'))
569    browser = GS_NETSCAPE;
570  if ((browser == GS_NETSCAPE) && (netscape_exe[0] == '\0'))
571    browser = GS_IEXPLORE;
572  if ((browser == GS_IEXPLORE) && (iexplore_exe[0] == '\0')) {
573    if (netscape_exe[0] != '\0') browser = GS_NETSCAPE;
574    else browser = GS_OTHER;
575  }
576 
577  // update the radio buttons
578  SendDlgItemMessage(hwndDlg, ID_RADIO_DEFAULT, BM_SETCHECK,
579             (browser == GS_DEFAULT) ? BST_CHECKED : BST_UNCHECKED, 0);
580  SendDlgItemMessage(hwndDlg, ID_RADIO_NETSCAPE, BM_SETCHECK,
581             (browser == GS_NETSCAPE) ? BST_CHECKED : BST_UNCHECKED, 0);
582  SendDlgItemMessage(hwndDlg, ID_RADIO_IE, BM_SETCHECK,
583             (browser == GS_IEXPLORE) ? BST_CHECKED : BST_UNCHECKED, 0);
584  SendDlgItemMessage(hwndDlg, ID_RADIO_OTHER, BM_SETCHECK,
585             (browser == GS_OTHER) ? BST_CHECKED : BST_UNCHECKED, 0);
586 
587  // update the other name field
588  if (browser == GS_DEFAULT) {
589    SetDlgItemText(hwndDlg, ID_OTHER_NAME, default_browser_exe);
590  } else if (browser == GS_NETSCAPE) {
591    SetDlgItemText(hwndDlg, ID_OTHER_NAME, netscape_exe);
592  } else if (browser == GS_IEXPLORE) {
593    SetDlgItemText(hwndDlg, ID_OTHER_NAME, iexplore_exe);
594  } else if (othername != NULL) {
595    SetDlgItemText(hwndDlg, ID_OTHER_NAME, othername);
596  }
597}
598
599
600static void read_browser_exe_field (HWND hwndDlg, char *filename, int maxsize) {
601  filename[0] = '\0';
602  GetDlgItemText(hwndDlg, ID_OTHER_NAME, filename, maxsize);
603}
604
605static int read_address_resolution_field (HWND hwndDlg) {
606  if (SendDlgItemMessage(hwndDlg, ID_ARM_NAME,
607             BM_GETCHECK, 0, 0) == 1) return ARM_NAME;
608  if (SendDlgItemMessage(hwndDlg, ID_ARM_IP,
609             BM_GETCHECK, 0, 0) == 1) return ARM_IP;
610  if (SendDlgItemMessage(hwndDlg, ID_ARM_LOCALHOST,
611             BM_GETCHECK, 0, 0) == 1) return ARM_LOCALHOST;
612  if (SendDlgItemMessage(hwndDlg, ID_ARM_127_0_0_1,
613             BM_GETCHECK, 0, 0) == 1) return ARM_127_0_0_1;
614  return ARM_NAME;
615}
616
617static void set_address_resolution_field (HWND hwndDlg,
618                      int address_res_method) {
619  // update the radio buttons
620  SendDlgItemMessage(hwndDlg, ID_ARM_NAME, BM_SETCHECK,
621             (address_res_method == ARM_NAME) ? BST_CHECKED : BST_UNCHECKED, 0);
622  SendDlgItemMessage(hwndDlg, ID_ARM_IP, BM_SETCHECK,
623             (address_res_method == ARM_IP) ? BST_CHECKED : BST_UNCHECKED, 0);
624  SendDlgItemMessage(hwndDlg, ID_ARM_LOCALHOST, BM_SETCHECK,
625             (address_res_method == ARM_LOCALHOST) ? BST_CHECKED : BST_UNCHECKED, 0);
626  SendDlgItemMessage(hwndDlg, ID_ARM_127_0_0_1, BM_SETCHECK,
627             (address_res_method == ARM_127_0_0_1) ? BST_CHECKED : BST_UNCHECKED, 0);
628
629}
630
631static void read_dialog_fields (HWND hwndDlg) {
632  int res;  BOOL bres;
633 
634  // port number
635  gsdl_port_num = GetDlgItemInt(hwndDlg, SERVER_PORT_EDIT_BOX, &bres, 0);
636  if (!bres) gsdl_port_num = 80;
637 
638  // whether to enter the library automatically on startup
639  res = SendDlgItemMessage(hwndDlg, ID_AUTO_ENTER_LIBRARY, BM_GETCHECK, 0, 0);
640  gsdl_auto_enter = (res == 1);
641 
642  // which browser to use
643  gsdl_browser = read_dialog_browser_field (hwndDlg);
644 
645  // find out where the browser exe lives
646  read_browser_exe_field (hwndDlg, gsdl_browser_exe, MAX_FILENAME_SIZE);
647
648  // which address resolution method
649  gsdl_address_resolution_method = read_address_resolution_field(hwndDlg);
650}
651
652
653
654
655static BOOL CALLBACK dialog_proc(HWND hwndDlg, UINT uMsg,
656                 WPARAM wParam,LPARAM  lParam) {
657  int wNotifyCode = HIWORD(wParam);
658  int wID = LOWORD(wParam);
659  char *filefilter = "Program Files (*.exe)\0*.exe\0All Files (*.*)\0*.*\0\0";
660  OPENFILENAME fname;
661  char filename[MAX_FILENAME_SIZE];
662 
663  switch (uMsg) {
664  case WM_INITDIALOG:
665    // set current values
666    SetDlgItemInt(hwndDlg, SERVER_PORT_EDIT_BOX, gsdl_port_num, FALSE);
667    SendDlgItemMessage(hwndDlg, ID_AUTO_ENTER_LIBRARY, BM_SETCHECK,
668               gsdl_auto_enter ? 1 : 0, 0);
669    set_dialog_browser_field (hwndDlg, gsdl_browser, gsdl_browser_exe);
670    set_address_resolution_field(hwndDlg, gsdl_address_resolution_method);
671    dialog_update_enables(hwndDlg);
672   
673    // make sure that the default browser, netscape and internet explorer
674    // radio buttons are only enabled if they could be found
675    if (default_browser_exe[0] == '\0')
676      EnableWindow (GetDlgItem (hwndDlg, ID_RADIO_DEFAULT), FALSE);
677    if (netscape_exe[0] == '\0')
678      EnableWindow (GetDlgItem (hwndDlg, ID_RADIO_NETSCAPE), FALSE);
679    if (iexplore_exe[0] == '\0')
680      EnableWindow (GetDlgItem (hwndDlg, ID_RADIO_IE), FALSE);
681   
682    return 1;
683   
684  case WM_COMMAND:
685    if (wNotifyCode == BN_CLICKED) {
686      switch (wID) {
687      case ID_CANCEL_BUTTON:
688    EndDialog(hwndDlg, 0);
689    return 1;
690   
691      case ID_OK_BUTTON:
692    // check to make sure we can find the browser
693    filename[0] = '\0';
694    read_browser_exe_field (hwndDlg, filename, MAX_FILENAME_SIZE);
695    if (check_program_exe (filename)) {
696      // the file exists
697      // if netscape is needed, make sure the browser is netscape
698      _strlwr(filename);
699      if (!gsdl_netscapeneeded || strstr (filename, "netscape.exe") != NULL) {
700        // everything is ok
701        read_dialog_fields(hwndDlg);
702        EndDialog(hwndDlg, 1);
703       
704      } else {
705        // message: must use netscape with this version
706        MessageBox(hwndDlg,
707               "You are using this software on a machine which\n"
708               "currently does not have a working TCP/IP network layer.\n"
709               "To allow the Greenstone Digital Library software to use\n"
710               "its own TCP/IP network layer you must use a Netscape\n"
711               "web browser.",
712               "Greenstone Digital Library Software",
713               MB_OK|MB_APPLMODAL);
714      }
715    } else {
716      // message: the file did not exist
717      MessageBox(hwndDlg,
718             "Could not find the selected browser. You must have a\n"
719             "web browser installed to use the Greenstone Digital\n"
720             "Library software.",
721             "Greenstone Digital Library Software",
722             MB_OK|MB_APPLMODAL);
723    }
724    return 1;
725
726      case ID_RADIO_DEFAULT:
727    set_dialog_browser_field (hwndDlg, GS_DEFAULT, NULL);
728    dialog_update_enables(hwndDlg);
729    return 1;
730   
731      case ID_RADIO_NETSCAPE:
732    set_dialog_browser_field (hwndDlg, GS_NETSCAPE, NULL);
733    dialog_update_enables(hwndDlg);
734    return 1;
735   
736      case ID_RADIO_IE:
737    set_dialog_browser_field (hwndDlg, GS_IEXPLORE, NULL);
738    dialog_update_enables(hwndDlg);
739    return 1;
740   
741      case ID_RADIO_OTHER:
742    set_dialog_browser_field (hwndDlg, GS_OTHER, NULL);
743    dialog_update_enables(hwndDlg);
744    return 1;
745   
746      case ID_BROWSE_BUTTON:
747    filename[0] = '\0';
748    memset(&fname, 0, sizeof(OPENFILENAME));
749    fname.lStructSize = sizeof(OPENFILENAME);
750    fname.hwndOwner = hwndDlg;
751    fname.hInstance = NULL;
752    fname.lpstrFilter = filefilter;
753    fname.lpstrCustomFilter = NULL;
754    fname.nMaxCustFilter = 0;
755    fname.nFilterIndex = 1;
756    fname.lpstrFile = filename;
757    fname.nMaxFile = MAX_FILENAME_SIZE-1;
758    fname.lpstrFileTitle = NULL;
759    fname.nMaxFileTitle = 0;
760    fname.lpstrInitialDir = NULL;
761    fname.lpstrTitle = "Choose Web Browser";
762    fname.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
763   
764    if(GetOpenFileName(&fname)) {
765      set_dialog_browser_field (hwndDlg, GS_OTHER, filename);
766      dialog_update_enables(hwndDlg);
767    }
768    return 1;
769
770      case ID_ARM_NAME:
771    set_address_resolution_field (hwndDlg, ARM_NAME);
772    dialog_update_enables(hwndDlg);
773    return 1;
774     case ID_ARM_IP:
775    set_address_resolution_field (hwndDlg, ARM_IP);
776    dialog_update_enables(hwndDlg);
777    return 1;
778    case ID_ARM_LOCALHOST:
779    set_address_resolution_field (hwndDlg, ARM_LOCALHOST);
780    dialog_update_enables(hwndDlg);
781    return 1;
782     case ID_ARM_127_0_0_1:
783    set_address_resolution_field (hwndDlg, ARM_127_0_0_1);
784    dialog_update_enables(hwndDlg);
785    return 1;
786
787      }
788    }
789    return 0;
790   
791  case WM_CLOSE:
792  case WM_DESTROY:
793    EndDialog(hwndDlg, 0);
794    return 1;
795  }
796 
797  return 0;
798}
799
800static void dialog_debug_update_enables (HWND hwndDlg) {
801  int res;
802 
803  // if they want a log of program use allow them to
804  // edit its filename
805  res = SendDlgItemMessage(hwndDlg, LOG_CHECK, BM_GETCHECK,0,0);
806  SendDlgItemMessage(hwndDlg, LOG_NAME_BOX, WM_ENABLE, (res == 1), 0);
807}
808
809
810static void read_dialog_debug_fields (HWND hwndDlg) {
811  int res;
812 
813  // whether to log program use
814  res = SendDlgItemMessage(hwndDlg, LOG_CHECK, BM_GETCHECK,0,0);
815  gsdl_keep_log = (res == 1);
816 
817  // log filename
818  res = SendDlgItemMessage(hwndDlg, LOG_NAME_BOX, EM_GETMODIFY,0,0);
819  if (res != 0) {
820    res = GetDlgItemText(hwndDlg, LOG_NAME_BOX, gsdl_log_name, MAX_FILENAME_SIZE);
821  }
822 
823  // whether to display log information on console
824  res = SendDlgItemMessage(hwndDlg, CONSOLE_CHECK, BM_GETCHECK,0,0);
825  gsdl_show_console = (res == 1);
826}
827
828
829static BOOL CALLBACK dialog_debug_proc(HWND hwndDlg, UINT uMsg,
830                       WPARAM wParam,LPARAM  lParam) {
831  int wNotifyCode = HIWORD(wParam);
832  int wID = LOWORD(wParam);
833 
834  switch (uMsg) {
835  case WM_INITDIALOG:
836    // set current values
837    SendDlgItemMessage(hwndDlg, LOG_CHECK, BM_SETCHECK,
838               gsdl_keep_log ? 1 : 0, 0);
839    SetDlgItemText(hwndDlg, LOG_NAME_BOX, gsdl_log_name);
840    SendDlgItemMessage(hwndDlg, CONSOLE_CHECK, BM_SETCHECK,
841               gsdl_show_console ? 1 : 0, 0);
842    dialog_debug_update_enables(hwndDlg);
843    return 1;
844   
845  case WM_COMMAND:
846    if (wNotifyCode == BN_CLICKED) {
847      switch (wID) {
848      case ID_CANCEL_BUTTON:
849    EndDialog(hwndDlg, 0);
850    return 1;
851   
852      case ID_OK_BUTTON:
853    read_dialog_debug_fields(hwndDlg);
854    EndDialog(hwndDlg, 1);
855    return 1;
856   
857      case LOG_CHECK:
858    dialog_debug_update_enables(hwndDlg);
859    return 1;
860      }
861    }
862    return 0;
863   
864  case WM_CLOSE:
865  case WM_DESTROY:
866    EndDialog(hwndDlg, 0);
867    return 1;
868  }
869 
870  return 0;
871}
872
873
874void Settings_Dialog(HWND window, int netscapeneeded)
875{
876  int old_gsdl_show_console = gsdl_show_console;
877  int old_gsdl_port_num = gsdl_port_num;
878 
879  int res = 0;
880 
881  gsdl_netscapeneeded = netscapeneeded;
882 
883  if ((GetAsyncKeyState(VK_CONTROL) & 0x8000) &&
884      (GetAsyncKeyState(VkKeyScan('d')) & 0x8000)) {
885    // control-D was pressed, display debug settings
886    // dialog box
887    res = DialogBox (NULL, MAKEINTRESOURCE(COMMS_DIALOG_DEBUG),
888             window, (DLGPROC)dialog_debug_proc);
889   
890    if (res == 1) {
891      // only update if exited via the OK button
892      if (gsdl_keep_log) open_log_file();
893      else close_log_file();
894     
895      // turn the console on/off if we need to
896      if (gsdl_show_console != old_gsdl_show_console) {
897    if (gsdl_show_console) activate_console ();
898    else deactivate_console ();
899      }
900    }
901   
902  } else {
903    // display normal settings dialog box
904   
905    // make sure everything is up-to-date
906    gsdl_check_browser_settings (netscapeneeded);
907   
908    res = DialogBox (NULL, MAKEINTRESOURCE(COMMS_DIALOG2),
909             window, (DLGPROC)dialog_proc);
910
911    if (res == 1) {
912      // only update if exited via the OK button
913      if (gsdl_port_num != old_gsdl_port_num) {
914    EndHTTPServer();
915    StartHTTPServer(window);
916      }
917    }
918  }
919 
920  // save the settings if we exited via the ok button
921  if (res == 1) {
922    write_settings(gsdl_url);
923    // reset httpprefix in case port number was changed
924    //    configure_httpprefix ();
925  }
926}
Note: See TracBrowser for help on using the browser.