root/main/trunk/greenstone2/runtime-src/src/w32server/settings.cpp @ 24498

Revision 24498, 28.3 KB (checked in by ak19, 8 years ago)

Introduced the --mode=gli command line parameter, where the gli value is used as the property prefix when retrieving autoenter and start_browser properties from the config file. These changes go towards working with a single merged server config file (glisite.cfg and llssite.cfg)

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