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

Revision 22770, 28.1 KB (checked in by ak19, 9 years ago)

More changes for ticket 152: making the collectdir moveable. When using server.exe, if GLI was last shutdown when another collectdir was active, this needs to be read in from the user config.xml by GLI, and server.exe needs to use it.

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