source: trunk/gsdl/src/w32server/fnord.cpp@ 1011

Last change on this file since 1011 was 1011, checked in by sjboddie, 24 years ago

tidied up w32server

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 29.7 KB
Line 
1/*
2Copyright (C) 1996
3
4This program is free software; you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation; either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program; if not, write to the Free Software
16Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18The author can be contacted via Email at [email protected]
19*/
20#include <windows.h>
21#include <stdlib.h>
22#include <stdio.h>
23#include <string.h>
24#include <memory.h>
25#pragma hdrstop
26#include "netio.h"
27#include "settings.h"
28#include "httpsrv.h"
29#include "resource.h"
30#include "locate.h"
31#include "cgiwrapper.h"
32#include "startbrowser.h"
33#include "d_winsock.h"
34
35#define MAINWINDOWWIDTH 400
36#define MAINWINDOWHEIGHT 200
37
38#define RESTHEIGHT 45 // Extra window size reqd below logo
39#define WASTEHEIGHT 44 // Error in ht due to caption and menu bar
40#define WASTEWIDTH 6
41
42// size and position of the collection title
43#define COLTITLEWIDTH 351
44#define COLTITLEHEIGHT 35
45#define COLTITLEX ((MAINWINDOWWIDTH-COLTITLEWIDTH)/2)
46#define COLTITLEY 10
47
48// size and position of the version string
49#define VERSIONWIDTH MAINWINDOWWIDTH
50#define VERSIONHEIGHT 20
51#define VERSIONX 0
52#define VERSIONY (COLTITLEY+COLTITLEHEIGHT)
53
54// size and position of the Greenstone digital library logo
55#define LOGOWIDTH 135
56#define LOGOHEIGHT 55
57#define LOGOX (MAINWINDOWWIDTH-LOGOWIDTH-20)
58#define LOGOY (MAINWINDOWHEIGHT-LOGOHEIGHT-RESTHEIGHT)
59
60// size and position of the status string
61#define STATUSX 20
62#define STATUSY ((LOGOY-VERSIONY-VERSIONHEIGHT)/2+VERSIONY+VERSIONHEIGHT)
63#define STATUSWIDTH MAINWINDOWWIDTH-STATUSX
64#define STATUSHEIGHT (MAINWINDOWHEIGHT-RESTHEIGHT-STATUSY)
65
66// size and position of the info string (same position as the button)
67#define INFOWIDTH MAINWINDOWWIDTH
68#define INFOHEIGHT (MAINWINDOWHEIGHT-INFOY)
69#define INFOX 0
70#define INFOY ((MAINWINDOWHEIGHT-RESTHEIGHT)+10)
71
72// size and position of the button
73#define ENTERBUTTONWIDTH 150
74#define ENTERBUTTONHEIGHT 30
75#define ENTERBUTTONX ((MAINWINDOWWIDTH-ENTERBUTTONWIDTH)/2)
76#define ENTERBUTTONY ((MAINWINDOWHEIGHT-RESTHEIGHT)+5)
77
78#define VERSIONSTRING "version 2.12"
79const char versionstring[] = VERSIONSTRING;
80
81const char strnothing[] = "";
82const char strinit[] = "Initialising...";
83const char strsb[] = "Starting browser...";
84const char strenterlib[] = "Press the 'Enter Library' button to start a browser\n"
85"and begin using this library";
86const char strenterstaticlib[] = "Press the 'Enter Library' button to start a browser\n"
87"and begin using the static version\n"
88"of this library";
89const char *statusstring = NULL; // points to the current status string
90const char *infostring = NULL; // points to the current info string
91
92
93// globals to do with networking and browsers
94int have_networking = 0;
95int netscapeneeded = 0;
96char startbrowserdir[1024] = ""; // HARD LIMIT!!!!!
97
98
99// stats to do with the last time the enter library button was pressed
100// these stats are needed to make suggestions about the proxy
101int enterlib_libaccessnum = -1; // -1 = NA
102DWORD enterlib_time = 0;
103
104
105enum { undefined_window, console_window, plain_window }
106window_state;
107
108HDC coltitledc = NULL;
109HBITMAP defcoltitlebitmap = NULL, coltitlebitmap=NULL;
110
111HDC logodc=NULL;
112HBITMAP deflogobitmap=NULL, logobitmap=NULL;
113
114HWND GoButton = NULL;
115
116
117void finish_up() {
118 // remember the current preferences
119 write_settings();
120
121 // Shutdown the HTTP server
122 EndHTTPServer();
123
124 // Shutdown the main server modules
125 CleanUpNetIO();
126 // RemoveFnordFromTray();
127
128 // Unload the wsock32 dll
129 d_UnloadWinsock();
130
131 // Clean up graphics stuff from window
132
133 if (defcoltitlebitmap != NULL)
134 SelectObject (coltitledc, defcoltitlebitmap);
135 if (coltitlebitmap != NULL)
136 DeleteObject (coltitlebitmap);
137 if (coltitledc != NULL)
138 DeleteDC (coltitledc);
139
140 if (deflogobitmap != NULL)
141 SelectObject (logodc, deflogobitmap);
142 if (logobitmap != NULL)
143 DeleteObject (logobitmap);
144 if (logodc != NULL)
145 DeleteDC (logodc);
146
147 // Shutdown the main window
148 PostQuitMessage(0);
149}
150
151
152int overlap(int left, int top, int right, int bottom,
153 RECT &r2)
154{
155 if (left <= r2.right && r2.left <= right &&
156 top <= r2.bottom && r2.top <= bottom)
157 return 1;
158 else
159 return 0;
160}
161
162
163void paint_window (HDC pdc, RECT &updateRect) {
164 // make sure the various DCs are set up
165 if (coltitledc == NULL) {
166 coltitledc = CreateCompatibleDC(pdc);
167 defcoltitlebitmap = SelectObject (coltitledc, coltitlebitmap);
168 }
169
170 if (logodc == NULL) {
171 logodc = CreateCompatibleDC(pdc);
172 deflogobitmap = SelectObject (logodc, logobitmap);
173 }
174
175 // update the collection title if needed
176 if (overlap(COLTITLEX, COLTITLEY,
177 COLTITLEX+COLTITLEWIDTH, COLTITLEY+COLTITLEHEIGHT,
178 updateRect)) {
179 BitBlt (pdc, COLTITLEX, COLTITLEY,
180 COLTITLEX+COLTITLEWIDTH, COLTITLEY+COLTITLEHEIGHT,
181 coltitledc, 0, 0, SRCCOPY);
182 }
183
184 // update the version string if needed
185 if (overlap (VERSIONX, VERSIONY,
186 VERSIONX+VERSIONWIDTH, VERSIONY+VERSIONHEIGHT,
187 updateRect)) {
188 RECT versionRect;
189 versionRect.left = VERSIONX;
190 versionRect.top = VERSIONY;
191 versionRect.right = VERSIONX+VERSIONWIDTH;
192 versionRect.bottom = VERSIONY+VERSIONHEIGHT;
193 DrawText(pdc, versionstring, -1, &versionRect, DT_CENTER);
194 }
195
196 // decide what we want to draw
197 if (gsdl_show_console) {
198 // we want to draw a "console" window
199 if (window_state != console_window ||
200 overlap (text_rect.left, text_rect.top,
201 text_rect.right, text_rect.bottom,
202 updateRect)) {
203 refresh_console (pdc);
204 }
205
206 window_state = console_window;
207
208 } else {
209 // we want to draw a "plain" window
210
211 // update the status string if needed
212 if ((statusstring != NULL) && (strlen(statusstring) > 0)
213 && overlap (STATUSX, STATUSY,
214 STATUSX+STATUSWIDTH, STATUSY+STATUSHEIGHT,
215 updateRect)) {
216 RECT statusRect;
217 statusRect.left = STATUSX;
218 statusRect.top = STATUSY;
219 statusRect.right = STATUSX+STATUSWIDTH;
220 statusRect.bottom = STATUSY+STATUSHEIGHT;
221 FillRect(pdc, &statusRect, GetStockObject(WHITE_BRUSH));
222
223 int cury = STATUSY;
224 int startline = 0, here = 0;
225 while (statusstring[here] != '\0') {
226 if (statusstring[here] < ' ') {
227 if (here - startline > 0) {
228 // output the text
229 TextOut (pdc, STATUSX, cury,
230 &statusstring[startline], here-startline);
231 cury += line_spacing;
232 }
233 startline = here+1;
234 }
235 here++;
236 }
237 // output any remaining text
238 if (here - startline > 0) {
239 TextOut (pdc, STATUSX, cury, &statusstring[startline], here-startline);
240 }
241 }
242
243 // update the logo (always)
244 BitBlt (pdc, LOGOX, LOGOY, LOGOX+LOGOWIDTH, LOGOY+LOGOHEIGHT,
245 logodc, 0, 0, SRCCOPY);
246
247 window_state = plain_window;
248 }
249
250
251 // update the info string if needed
252 if ((GoButton == NULL) && (infostring != NULL) &&
253 overlap (INFOX, INFOY,
254 INFOX+INFOWIDTH, INFOY+INFOHEIGHT,
255 updateRect)) {
256 RECT infoRect;
257 infoRect.left = INFOX;
258 infoRect.top = INFOY;
259 infoRect.right = INFOX+INFOWIDTH;
260 infoRect.bottom = INFOY+INFOHEIGHT;
261 FillRect(pdc, &infoRect, GetStockObject(WHITE_BRUSH));
262 DrawText(pdc, infostring, -1, &infoRect, DT_CENTER);
263 }
264}
265
266
267void handle_painting (HWND Window) {
268 HDC pdc; PAINTSTRUCT ps;
269 pdc = BeginPaint(Window, &ps);
270
271 paint_window (pdc, ps.rcPaint);
272
273 EndPaint(Window, &ps);
274}
275
276
277// returns 1 on success, 0 otherwise
278int enter_library (HWND Window) {
279 int res;
280
281 // get the url and attempt to start a browser
282 char *localname = GetLocalName(NULL);
283 if (have_networking) {
284 text_t url = "http://" + text_t(localname);
285 if (gsdl_port_num != 80)
286 url += ":" + text_t(gsdl_port_num);
287
288 url += gsdl_enterlib;
289
290 // add a unique ending so it will always result in a request
291 text_t::const_iterator it =
292 findchar (gsdl_enterlib.begin(), gsdl_enterlib.end(), '?');
293 if (it == gsdl_enterlib.end()) url.push_back ('?');
294 else url.push_back ('&');
295 int tcount = GetTickCount();
296 url += "uq=" + text_t(tcount);
297
298 // remember the library access number for later proxy checking
299 enterlib_libaccessnum = libaccessnum;
300
301 // do a quick check to make sure we're using netscape when
302 // we should be (it might have been just installed)
303 if (netscapeneeded) gsdl_check_browser_settings (netscapeneeded);
304
305 char *cstr_url = url.getcstr();
306 if (strlen (startbrowserdir) <= 0) {
307 res = startbrowser (cstr_url, gsdl_browser_exe, NULL);
308
309 } else res = startbrowser (cstr_url, gsdl_browser_exe, startbrowserdir);
310
311 delete cstr_url;
312
313 } else {
314 /*
315 char staticpath[256];
316
317 // try to find the static pages
318 strcpy (url, gsdl_staticpages);
319
320 // remove any slashes from the static pathname
321 int len = strlen (url);
322 while ((len > 0) && ((url[len-1] == '\\') || (url[len-1] == '/'))) {
323 len--;
324 }
325 url[len] = '\0';
326
327 // add one slash to the end
328 strcat (url, "\\");
329
330 // check to make sure the static directory is available
331 strcpy (staticpath, url);
332 strcat (staticpath, "static\\");
333
334 // check this directory
335 if (!cstrcheckdir(staticpath)) {
336 PostMessage(Window,WM_CLOSE,0,0);
337 return 0;
338 }
339
340 // add filename of the first page
341 strcat (url, "niupepa.htm");
342
343 res = startbrowser (url, gsdl_browser_exe, NULL);
344 startbrowserdir[0] = '\0'; // doesn't make sense for non-networked case
345 */
346 }
347
348 if (res == SB_NOERROR) {
349 // success !!
350
351 // remember the time the browser was started for later proxy checking
352 if (have_networking) enterlib_time = GetTickCount();
353
354 return 1;
355 }
356
357 // no browser was started
358 enterlib_libaccessnum = -1;
359
360 if ((res == SB_FAIL_BADFORMAT) || (res == SB_FAIL_NOTFOUND)) {
361 if (!netscapeneeded) { // any browser
362 MessageBox(Window,
363 "Failed to start your chosen browser. It seems that your\n"
364 "chosen browser has been removed or corrupted.\n"
365 "Please check you browser choice by going to the\n"
366 "'Settings...' item under the 'File' menu.",
367 "Greenstone Digital Library Software", MB_OK);
368 } else { // netscape used
369 MessageBox(Window,
370 "Failed to start Netscape. It seems that Netscape has\n"
371 "been removed or corrupted. You need Netscape\n"
372 "installed to use the Greenstone Digital Library\n"
373 "software on non-networked machines.\n\n"
374 "You can install Netscape by choosing\n"
375 "'Install Netscape 4.05' from the 'File' menu.",
376 "Greenstone Digital Library Software", MB_OK);
377 }
378
379 } else {
380 MessageBox(Window,
381 "Failed to start your browser. This may have been\n"
382 "because there was not enough memory available. Shut\n"
383 "down all other applications you have running, and\n"
384 "try again.",
385 "Greenstone Digital Library Software", MB_OK);
386 }
387
388 return 0; // failed
389}
390
391
392void install_netscape (HWND Window) {
393 char installpath[256];
394
395 // try to find the browser directory
396 char *gsdlhome = gsdl_gsdlhome.getcstr();
397 strcpy (installpath, gsdlhome); // use gsdlhome to find the CD-ROM drive...
398 delete gsdlhome;
399
400 // remove any slashes from the pathname
401 int len = strlen (installpath);
402 while ((len > 0) && ((installpath[len-1] == '\\') || (installpath[len-1] == '/'))) {
403 len--;
404 }
405 // remove '\gsdl'
406 len = len - 5;
407 installpath[len] = '\0';
408
409 strcat (installpath, "\\netscape\\");
410
411 // check this directory
412 if (!cstrcheckdir(installpath)) {
413 return;
414 }
415
416 // get the operating system information
417 OSVERSIONINFO osver;
418 memset(&osver, 0, sizeof(OSVERSIONINFO));
419 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
420 GetVersionEx(&osver);
421
422 // get the install program for this operating system
423 if (osver.dwPlatformId == VER_PLATFORM_WIN32s) {
424 strcat (installpath, "n16e405.exe");
425 } else {
426 strcat (installpath, "n32e405.exe");
427 }
428
429 // run the install program
430 int res = WinExec (installpath, SW_SHOW);
431 if (res == 0) {
432 // out of resources or memory
433 MessageBox (Window,
434 "Did not have enough resources or memory to run the\n"
435 "install program. Try shutting down all other programs\n"
436 "and trying again.",
437 "Greenstone Digital Library Software",
438 MB_OK|MB_APPLMODAL);
439
440 } else if (res == ERROR_BAD_FORMAT) {
441 // executable image is corrupt -- ????
442 MessageBox (Window,
443 "The install program seems to be corrupt.",
444 "Greenstone Digital Library Software",
445 MB_OK|MB_APPLMODAL);
446
447 } else if ((res == ERROR_FILE_NOT_FOUND) || (res == ERROR_PATH_NOT_FOUND)) {
448 // couldn't find the executable -- ????
449 MessageBox (Window,
450 "Could not find the install program. Try installing\n"
451 "Netscape from the Greenstone Digital Library\n"
452 "software's install program.",
453 "Greenstone Digital Library Software",
454 MB_OK|MB_APPLMODAL);
455 }
456}
457
458void open_help () {
459 char topdir[256], cmd[256];
460
461 // try to find the browser directory
462 char *gsdlhome = gsdl_gsdlhome.getcstr();
463 strcpy (topdir, gsdlhome); // use gsdlhome to find the CD-ROM drive...
464 delete gsdlhome;
465 strcpy (cmd, "notepad ");
466
467 // remove any slashes from the pathname
468 int len = strlen (topdir);
469 while ((len > 0) && ((topdir[len-1] == '\\') || (topdir[len-1] == '/'))) {
470 len--;
471 }
472 // remove the '\gsdl'
473 len = len - 5;
474 topdir[len] = '\0';
475
476 strcat (topdir, "\\README.txt");
477 strcat (cmd, topdir);
478
479 WinExec (cmd, SW_SHOW);
480}
481
482
483long __stdcall WndProc(HWND Window, UINT Message, WPARAM WParam, LPARAM LParam) {
484 switch(Message) {
485 // Constant Messages
486 case WM_CREATE:
487 break;
488
489 case WM_CHAR:
490 // if the user hit the return key and if this is not a
491 // repeat key press then attempt to start a browser
492 if ((WParam == '\r') && !(LParam & 0xe0000000)) {
493 if (enter_library (Window) && !gsdl_show_console)
494 ShowWindow(Window,SW_MINIMIZE);
495 }
496
497 case WM_COMMAND:
498 if ((HWND)LParam == GoButton) {
499 // attempt to start a browser
500 if (enter_library (Window) && !gsdl_show_console)
501 ShowWindow(Window,SW_MINIMIZE);
502
503 } else {
504 switch (LOWORD(WParam)) {
505 case ID_PROJECT_SETTINGS:
506 Settings_Dialog(Window, netscapeneeded);
507 break;
508 case ID_INSTALL_NETSCAPE:
509 install_netscape(Window);
510 break;
511 case IDHELP:
512 open_help();
513 break;
514 case ID_PROJECT_EXIT:
515 finish_up();
516 break;
517 default:
518 break;
519 }
520 }
521 break;
522
523 case WM_PAINT:
524 handle_painting(Window);
525 break;
526
527 case HTTP_SERVER_MSG:
528 ProcessHTTPServerMsg(WParam, LParam);
529 break;
530
531 case WM_DESTROY:
532 finish_up();
533 break;
534
535 default:
536 //Unhandled Messages end up here (DefWindowProc)
537 return DefWindowProc(Window, Message, WParam, LParam);
538 }
539
540 return(0);
541}
542
543// returns 0 if successful,
544// otherwise a WSA error number
545int tryinitnetwork (HANDLE Instance, HWND MsgWindow, char *path) {
546 // try to load winsock
547 int err = d_LoadWinsock (path);
548
549 if (err == D_NOERROR) {
550 // next, try to init netio
551 err = InitNetIO();
552
553 if (err == 0) {
554 // next, try to start the http server
555 err = StartHTTPServer(MsgWindow);
556
557 if (err == 0) {
558 // finally, get the host name (no error value
559 // is returned from this function)
560 GetLocalName(Instance);
561
562 } else {
563 // couldn't start the http server,
564 // deinit netio and unload winsock
565 CleanUpNetIO();
566 d_UnloadWinsock();
567 }
568
569 } else {
570 // couldn't init netio, unload winsock
571 d_UnloadWinsock();
572 }
573
574 } else {
575 // couldn't load winsock
576 err = WSASYSNOTREADY;
577 }
578
579 return err;
580}
581
582
583// inits all the network functionality
584// returns 0 if an unrecoverable error occurred,
585// and 1 if everything successfully initialised.
586int initnetwork (HANDLE Instance, HWND MsgWindow) {
587 OSVERSIONINFO osver;
588
589 // get the operating system information
590 memset(&osver, 0, sizeof(OSVERSIONINFO));
591 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
592 GetVersionEx(&osver);
593
594 // first try to initialise the network with system installed
595 // networking software
596 startbrowserdir[0] = '\0';
597 netscapeneeded = 0;
598 int err = tryinitnetwork (Instance, MsgWindow, NULL);
599 //int err = 1;
600
601 // if an error occurred, try again with billsock
602 if (err != 0) {
603 // get the path of billsock (current directory + ("net16" | "net32"))
604 char winsockpath[1024]; // HARD LIMIT!!!!
605
606 find_location();
607 strcpy (winsockpath, data_location);
608
609 // remove all the slashes at the end of billpath
610 int len = strlen (winsockpath);
611 while ((len > 0) && ((winsockpath[len-1] == '\\') || (winsockpath[len-1] == '/'))) {
612 len--;
613 }
614 winsockpath[len] = '\0';
615
616 if (osver.dwPlatformId == VER_PLATFORM_WIN32s)
617 strcat (winsockpath, "\\net16\\");
618 else {
619 strcat (winsockpath, "\\net32\\");
620 strcpy (startbrowserdir, winsockpath);
621 }
622
623 // try again
624 err = tryinitnetwork (Instance, MsgWindow, winsockpath);
625
626 if (err == 0) {
627 // we will need to use netscape on 95/98/NT machines
628 if (osver.dwPlatformId != VER_PLATFORM_WIN32s) {
629 netscapeneeded = 1;
630 // make sure the browser state is in step with the network condition
631 gsdl_check_browser_settings (netscapeneeded);
632 }
633
634 // check to see if a browser is running
635 if (browserrunning(gsdl_browser_exe) == NO_ERROR) {
636 MessageBox (NULL,
637 "The Greenstone Digital Library software has detected a\n"
638 "running browser. To let our networking software\n"
639 "correctly initialize, please shut down your browser\n"
640 "and restart the Greenstone Digital Library software.",
641 "Greenstone Digital Library Software", MB_OK|MB_APPLMODAL);
642 PostMessage(MsgWindow,WM_CLOSE,0,0);
643 finish_up(); // will unload winsock
644 exit(0); // nothing more to do
645 }
646 }
647 }
648
649 // if an error occurred display a nice error message
650 if (err != 0) {
651
652 // WSASYSNOTREADY Couldn't load winsock
653 // WSAVERNOTSUPPORTED The version of Windows Sockets support requested
654 // is not provided by this particular Windows Sockets implementation.
655 // WSAEINVAL The Windows Sockets version specified by the application
656 // is not supported by this DLL.
657 // WSAEPROTONOSUPPORT The specified protocol is not supported.
658 // WSAEPROTOTYPE The specified protocol is the wrong type for this socket.
659 // WSAESOCKTNOSUPPORT The specified socket type is not supported in
660 // this address family.
661 // WSANOTINITIALISED A successful WSAStartup must occur before
662 // using this function.
663 // WSAENETDOWN The Windows Sockets implementation
664 // has detected that the network subsystem has failed.
665 // WSAEADDRINUSE The specified address
666 // is already in use. (See the SO_REUSEADDR socket option under setsockopt.)
667 // WSAEFAULT The namelen argument is too small (less
668 // than the size of a struct sockaddr).
669 // WSAEINPROGRESS A blocking Windows Sockets call is in progress.
670 // WSAEAFNOSUPPORT The specified address family is not supported
671 // by this protocol.
672 // WSAENOBUFS Not enough buffers available, too many connections.
673 // WSAENOTSOCK The descriptor is not a socket.
674 // WSAEISCONN The socket is already connected.
675 // WSAEMFILE No more file descriptors are available.
676 // WSAEOPNOTSUPP The referenced socket is not of a type that
677 // supports the listen operation.
678
679 // get a text version of the error number
680 char errstr[256];
681
682 switch (err) {
683 case WSASYSNOTREADY: strcpy(errstr, "WSASYSNOTREADY");
684 break;
685 case WSAVERNOTSUPPORTED: strcpy (errstr, "WSAVERNOTSUPPORTED");
686 break;
687 case WSAEINVAL: strcpy (errstr, "WSAEINVAL");
688 break;
689 case WSAEPROTONOSUPPORT: strcpy (errstr, "WSAEPROTONOSUPPORT");
690 break;
691 case WSAEPROTOTYPE: strcpy (errstr, "WSAEPROTOTYPE");
692 break;
693 case WSAESOCKTNOSUPPORT: strcpy (errstr, "WSAESOCKTNOSUPPORT");
694 break;
695 case WSANOTINITIALISED: strcpy (errstr, "WSANOTINITIALISED");
696 break;
697 case WSAEFAULT: strcpy (errstr, "WSAEFAULT");
698 break;
699 case WSAEAFNOSUPPORT: strcpy (errstr, "WSAEAFNOSUPPORT");
700 break;
701 case WSAENOTSOCK: strcpy (errstr, "WSAENOTSOCK");
702 break;
703 case WSAEISCONN: strcpy (errstr, "WSAEISCONN");
704 break;
705 case WSAEOPNOTSUPP: strcpy (errstr, "WSAEOPNOTSUPP");
706 break;
707 case WSAENETDOWN: strcpy (errstr, "WSAENETDOWN");
708 break;
709 case WSAEADDRINUSE: strcpy (errstr, "WSAEADDRINUSE");
710 break;
711 case WSAEINPROGRESS: strcpy (errstr, "WSAEINPROGRESS");
712 break;
713 case WSAENOBUFS: strcpy (errstr, "WSAENOBUFS");
714 break;
715 case WSAEMFILE: strcpy (errstr, "WSAEMFILE");
716 break;
717 default: sprintf (errstr, "WSA ERROR: %i", err);
718 break;
719 }
720
721 // create a nice error message
722 char message[2048];
723
724 switch (err) {
725 case WSAEADDRINUSE:
726 // couldn't bind socket
727 sprintf (message, "Could not find a free socket.", errstr);
728 break;
729
730 default:
731 // cannot init winsock
732 sprintf (message, "Could not initialize the network\n"
733 " (Reason: %s).", errstr);
734 break;
735 }
736
737 strcat (message, "\n\n"
738 // no static library for this collection
739 // "You will still be able to use the static version of this\n"
740 // "collection, however, searching will be unavailable.");
741 "Please submit a bug report using the support.htm file\n"
742 "on the CD-ROM.");
743
744 MessageBox (NULL, message, "Greestone Digital Library Software", MB_OK);
745 }
746
747 return (err == 0);
748}
749
750
751void log_computer_info () {
752 char tmpstr[1024];
753
754 // get various information about this computer
755 if (gsdl_keep_log || gsdl_show_console) {
756 // get operating system information
757 OSVERSIONINFO osver;
758 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
759
760 if (GetVersionEx(&osver)) {
761 if (osver.dwPlatformId == VER_PLATFORM_WIN32s) {
762 log_message ("Platform: win32s\n");
763 } else if (osver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
764 log_message ("Platform: windows 95\n");
765 } else if (osver.dwPlatformId == VER_PLATFORM_WIN32_NT) {
766 log_message ("Platform: windows NT\n");
767 }
768
769 sprintf (tmpstr, "MajorVersion: %i\n", (int)osver.dwMajorVersion);
770 log_message(tmpstr);
771 sprintf (tmpstr, "MinorVersion: %i\n", (int)osver.dwMinorVersion);
772 log_message(tmpstr);
773 sprintf (tmpstr, "BuildNumber: %i\n", (int)osver.dwBuildNumber);
774 log_message(tmpstr);
775 sprintf (tmpstr, "CSDVersion: %s\n\n", osver.szCSDVersion);
776 log_message(tmpstr);
777 }
778
779 // get memory information
780 MEMORYSTATUS memstatus;
781 memstatus.dwLength = sizeof(MEMORYSTATUS);
782 GlobalMemoryStatus(&memstatus);
783
784 sprintf (tmpstr, "TotalPhys: %i Meg\n", (int)(memstatus.dwTotalPhys/1024/1024));
785 log_message (tmpstr);
786 sprintf (tmpstr, "AvailPhys: %i Meg\n", (int)(memstatus.dwAvailPhys/1024/1024));
787 log_message (tmpstr);
788 sprintf (tmpstr, "TotalPageFile: %i Meg\n", (int)(memstatus.dwTotalPageFile/1024/1024));
789 log_message (tmpstr);
790 sprintf (tmpstr, "AvailPageFile: %i Meg\n", (int)(memstatus.dwAvailPageFile/1024/1024));
791 log_message (tmpstr);
792 sprintf (tmpstr, "TotalVirtual: %i Meg\n", (int)(memstatus.dwTotalVirtual/1024/1024));
793 log_message (tmpstr);
794 sprintf (tmpstr, "AvailVirtual: %i Meg\n\n", (int)(memstatus.dwAvailVirtual/1024/1024));
795 log_message (tmpstr);
796
797 // log the version number
798 log_message("GSDL version: " VERSIONSTRING "\n");
799 }
800
801}
802
803
804int __stdcall WinMain(HANDLE Instance, HANDLE /*PrevInstance*/, LPSTR CmdLineStr, int /*CmdShow*/) {
805 HWND MainWindow; MSG Message; WNDCLASS MainClass;
806
807 //Create a window class
808 MainClass.style = CS_HREDRAW | CS_VREDRAW;
809 MainClass.lpfnWndProc = WndProc;
810 MainClass.cbClsExtra = 0;
811 MainClass.cbWndExtra = 0;
812 MainClass.hInstance = Instance;
813 MainClass.hIcon = LoadIcon(Instance, MAKEINTRESOURCE(TRAY_ICON));
814 MainClass.hCursor = LoadCursor(NULL, IDC_ARROW);
815 MainClass.hbrBackground = GetStockObject(WHITE_BRUSH);
816 MainClass.lpszMenuName = MAKEINTRESOURCE(Main_Menu);
817 MainClass.lpszClassName = "Greenstone Digital Library Software";
818 if (!RegisterClass(&MainClass))
819 return 0;
820
821 // Load various bitmaps
822 coltitlebitmap = LoadBitmap (Instance, MAKEINTRESOURCE(GSDL_COL_TITLE));
823 logobitmap= LoadBitmap (Instance, MAKEINTRESOURCE(GSDL_LOGO));
824
825 // Create the main window
826 MainWindow = CreateWindow("Greenstone Digital Library Software",
827 "Greenstone Digital Library Software",
828 WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU |
829 WS_MINIMIZEBOX,
830 CW_USEDEFAULT, CW_USEDEFAULT,
831 MAINWINDOWWIDTH + WASTEWIDTH,
832 MAINWINDOWHEIGHT + WASTEHEIGHT,
833 NULL, NULL, Instance, NULL);
834
835 text_rect.left = 0;
836 text_rect.top = VERSIONY+VERSIONHEIGHT + 5;
837 text_rect.right = MAINWINDOWWIDTH;
838 text_rect.bottom = MAINWINDOWHEIGHT-RESTHEIGHT - 1;
839
840 window_state = undefined_window;
841
842 ShowWindow(MainWindow, SW_SHOW);
843 set_location(CmdLineStr);
844
845 // get ready to draw the main window
846 RECT windowRect;
847 GetClientRect (MainWindow, &windowRect);
848 HDC pdc = GetDC (MainWindow);
849
850 // retrieve and save the text metrics
851 TEXTMETRIC tm;
852 GetTextMetrics(pdc, &tm);
853 line_spacing = tm.tmHeight + tm.tmExternalLeading;
854 GSDL_Window = MainWindow;
855
856 // draw the main window containing an init message
857 infostring = strinit;
858 paint_window (pdc, windowRect);
859 DWORD lastcheck = GetTickCount();
860
861 // init various modules
862 read_settings (0); // don't know if netscape is needed at this point
863
864 // attempt to initialise the network
865 // we do this first now as gsdl_init
866 // needs networking initialized to
867 // set httpprefix - Stefan.
868 // -- gsdl_init doesn't currently set httpprefix,
869 // it did for a brief time when I was using some
870 // scary html that didn't like relative links
871 have_networking = initnetwork (Instance, MainWindow);
872 if (!have_networking) {
873 MessageBox(NULL,
874 "Failed to initialize networking.",
875 "Greenstone Digital Library Software",MB_OK|MB_SYSTEMMODAL);
876 exit (0); // there is no static library for this collection
877 }
878
879 if (!gsdl_init()) // quit if can't initialise the library correctly
880 exit (0);
881 initstartbrowser();
882 log_computer_info ();
883
884 // show the initialising message for at least 1 second
885 while (DiffTickCounts (lastcheck, GetTickCount()) < 1000) {
886 Sleep (1);
887 }
888
889 // if autoenter is enabled attempt to start a browser
890 if (have_networking && gsdl_auto_enter) {
891 infostring = strsb;
892 paint_window (pdc, windowRect);
893 lastcheck = GetTickCount();
894 if (enter_library (MainWindow) && !gsdl_show_console) {
895 // stay maximised for at least 1 second
896 while (DiffTickCounts (lastcheck, GetTickCount()) < 1000) Sleep (1);
897 ShowWindow(MainWindow,SW_MINIMIZE);
898 }
899 }
900
901 // remove the last info message
902 infostring = strnothing;
903 paint_window (pdc, windowRect);
904
905 // add the "enter library" button
906 GoButton = CreateWindow(
907 "BUTTON", // predefined class
908 "Enter Library", // button text
909 WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, // styles
910 ENTERBUTTONX, // starting x position
911 ENTERBUTTONY, // starting y position
912 ENTERBUTTONWIDTH, // button width
913 ENTERBUTTONHEIGHT, // button height
914 MainWindow, // parent window
915 NULL, // No menu
916 Instance,
917 NULL); // pointer not needed
918
919 if (have_networking) statusstring = strenterlib;
920 else statusstring = strenterstaticlib;
921 paint_window (pdc, windowRect);
922
923 // release the DC used to display the init messages
924 ReleaseDC (MainWindow, pdc);
925
926 //Message loop
927 lastcheck = GetTickCount();
928 int seenbrowser = 0; // if we see a browser then it disappears
929 // we will ask if they want to close the
930 // the library down
931 char last_gsdl_browser_exe[MAX_FILENAME_SIZE]; // need to notice when the
932 // browser setting changes
933 strcpy (last_gsdl_browser_exe, gsdl_browser_exe);
934
935 for (;;) {
936 if (PeekMessage(&Message, NULL, 0, 0, PM_REMOVE)) {
937 if (Message.message == WM_QUIT) break;
938 TranslateMessage(&Message); /* translate keyboard messages */
939 DispatchMessage(&Message); /* return control to Windows NT */
940
941 } else {
942 if (DiffTickCounts (lastcheck, GetTickCount()) > 500) {
943 // make sure the browser hasn't changed
944 if (strcmp (last_gsdl_browser_exe, gsdl_browser_exe) != 0) {
945 seenbrowser = 0;
946 strcpy (last_gsdl_browser_exe, gsdl_browser_exe);
947 }
948
949 // do check
950 lastcheck = GetTickCount();
951 if (browserrunning(gsdl_browser_exe) == NO_ERROR) {
952 // we were able to connect to a browser
953 seenbrowser = 1;
954
955 // see if the enter library button has been pressed
956 // with nothing happening for 20 seconds
957 if ((enterlib_libaccessnum >= 0) &&
958 (enterlib_libaccessnum == libaccessnum) &&
959 (DiffTickCounts (enterlib_time, GetTickCount()) > 20000)) {
960 // something could be wrong!! (most likely cause is a proxy)
961 MessageBox(NULL,
962 "Your browser does not seem to be responding to the 'Enter Library'\n"
963 "request. Try turning off all proxy servers in your browser settings.",
964 "Greenstone Digital Library Software",MB_OK|MB_SYSTEMMODAL);
965 enterlib_libaccessnum = -1;
966 }
967
968
969 } else {
970 // no browser was found
971 if (seenbrowser) {
972 // we have seen a browser in the past
973 if (MessageBox(NULL,
974 "The Greenstone Digital Library software has noticed that you\n"
975 "shut down your browser. Would you also like to shut down the\n"
976 "Greenstone Digital Library software?",
977 "Greenstone Digital Library Software",MB_YESNO|MB_SYSTEMMODAL) == IDYES)
978 PostMessage(MainWindow,WM_CLOSE,0,0);
979 }
980 seenbrowser = 0;
981 }
982 }
983
984 Sleep (1); // millisecond
985 }
986 }
987
988 return Message.wParam;
989}
Note: See TracBrowser for help on using the repository browser.