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

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

Had an initial attempt at merging the old 'server' and 'single user'
versions of the windows local library. Works ok but may need some work
on the wording and some discussion on what the various versions should
be called.

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