[2286] | 1 | /**********************************************************************
|
---|
| 2 | *
|
---|
| 3 | * startbrowser.cpp
|
---|
| 4 | * Copyright (C) 1999 The New Zealand Digital Library Project
|
---|
| 5 | *
|
---|
| 6 | * A component of the Greenstone digital library software
|
---|
| 7 | * from the New Zealand Digital Library Project at the
|
---|
| 8 | * University of Waikato, New Zealand.
|
---|
| 9 | *
|
---|
| 10 | * This program is free software; you can redistribute it and/or modify
|
---|
| 11 | * it under the terms of the GNU General Public License as published by
|
---|
| 12 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 13 | * (at your option) any later version.
|
---|
| 14 | *
|
---|
| 15 | * This program is distributed in the hope that it will be useful,
|
---|
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 18 | * GNU General Public License for more details.
|
---|
| 19 | *
|
---|
| 20 | * You should have received a copy of the GNU General Public License
|
---|
| 21 | * along with this program; if not, write to the Free Software
|
---|
| 22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 23 | *
|
---|
| 24 | *********************************************************************/
|
---|
| 25 |
|
---|
[611] | 26 | #include "startbrowser.h"
|
---|
| 27 | #include <windows.h>
|
---|
| 28 | #include <DDE.H>
|
---|
| 29 | #include <DDEML.H>
|
---|
| 30 | #include <string.h>
|
---|
| 31 | #include <direct.h>
|
---|
| 32 | #include "locate.h"
|
---|
| 33 |
|
---|
| 34 | #define SB_COMLEN 1024
|
---|
| 35 | #define SB_DDETIMEOUT 60000UL // One minute in milliseconds
|
---|
| 36 |
|
---|
| 37 | int numbrowserstarted = 0;
|
---|
| 38 |
|
---|
| 39 | static int inited = 0;
|
---|
| 40 |
|
---|
| 41 | static DWORD idInst = 0;
|
---|
| 42 | static HINSTANCE hinst = NULL;
|
---|
| 43 | static HSZ hszNetscapeName = NULL;
|
---|
| 44 | static HSZ hszIExploreName = NULL;
|
---|
| 45 | static HSZ hszOpenURLTopic = NULL;
|
---|
| 46 | static HSZ hszActivateTopic = NULL;
|
---|
| 47 |
|
---|
| 48 | // budget callback
|
---|
| 49 | HDDEDATA CALLBACK DdeCallback(UINT uType, /* transaction type */
|
---|
[2286] | 50 | UINT uFmt, /* clipboard data format */
|
---|
| 51 | HCONV hconv, /* handle of conversation */
|
---|
| 52 | HSZ hsz1, /* handle of string */
|
---|
| 53 | HSZ hsz2, /* handle of string */
|
---|
| 54 | HDDEDATA hdata, /* handle of global memory object */
|
---|
| 55 | DWORD dwData1, /* transaction-specific data */
|
---|
| 56 | DWORD dwData2) /* transaction-specific data */
|
---|
[611] | 57 | {
|
---|
[2286] | 58 | return (HDDEDATA) NULL;
|
---|
[611] | 59 | }
|
---|
| 60 |
|
---|
| 61 | // returns 0 on failure
|
---|
| 62 | static int talkdde (HSZ hszServName, HSZ hszSysTopic, char *requeststr) {
|
---|
[2286] | 63 | HSZ hszRequest;
|
---|
| 64 | HCONV hConv;
|
---|
| 65 | HDDEDATA result;
|
---|
| 66 |
|
---|
| 67 | // try to connect to the newly started browser
|
---|
| 68 | hConv = DdeConnect (idInst, hszServName, hszSysTopic, (PCONVCONTEXT) NULL);
|
---|
| 69 | if (hConv == NULL) return 0;
|
---|
| 70 |
|
---|
| 71 | hszRequest = DdeCreateStringHandle (idInst, requeststr, CP_WINANSI);
|
---|
| 72 | result = DdeClientTransaction (
|
---|
| 73 | NULL, // pointer to data to pass to server
|
---|
| 74 | 0, // length of data
|
---|
| 75 | hConv, // handle to conversation
|
---|
| 76 | hszRequest, // handle to item name string
|
---|
| 77 | CF_TEXT, // clipboard data format
|
---|
| 78 | XTYP_REQUEST, // transaction type
|
---|
| 79 | SB_DDETIMEOUT, // time-out duration
|
---|
| 80 | NULL // pointer to transaction result
|
---|
| 81 | );
|
---|
[611] | 82 |
|
---|
[2286] | 83 | if (result != NULL) DdeFreeDataHandle (result);
|
---|
| 84 | DdeDisconnect(hConv);
|
---|
| 85 | DdeFreeStringHandle (idInst, hszRequest);
|
---|
[611] | 86 |
|
---|
[2286] | 87 | return 1;
|
---|
[611] | 88 | }
|
---|
| 89 |
|
---|
| 90 | // returns 0 on failure
|
---|
| 91 | static int openurl(HSZ hszServName, HSZ hszActivateTopic, HSZ hszOpenURLTopic, char *url) {
|
---|
[2286] | 92 | char requeststr[65600];
|
---|
| 93 |
|
---|
| 94 | // bring the browser to the front, don't worry if this fails
|
---|
| 95 | talkdde (hszServName, hszActivateTopic, "0xFFFFFFFF,0x0");
|
---|
| 96 |
|
---|
| 97 | // tell the browser to open a url
|
---|
| 98 | strcpy (requeststr, "\"");
|
---|
| 99 | strcat (requeststr, url);
|
---|
| 100 | strcat (requeststr, "\",,0xFFFFFFFF,0x0,,,");
|
---|
| 101 |
|
---|
| 102 | return talkdde (hszServName, hszOpenURLTopic, requeststr);
|
---|
[611] | 103 | }
|
---|
| 104 |
|
---|
| 105 | // returns 0 on failure
|
---|
| 106 | static int tryconnect(HSZ hszServName, HSZ hszSysTopic) {
|
---|
[2286] | 107 | // try to connect to a running browser
|
---|
| 108 | HCONV hConv = DdeConnect (idInst, hszServName, hszSysTopic, (PCONVCONTEXT) NULL);
|
---|
| 109 | if (hConv == NULL) return 0;
|
---|
| 110 | DdeDisconnect(hConv);
|
---|
| 111 | return 1;
|
---|
[611] | 112 | }
|
---|
| 113 |
|
---|
| 114 | static void freestrings () {
|
---|
[2286] | 115 | // free up any currently allocated strings
|
---|
| 116 | if (hszNetscapeName != NULL) DdeFreeStringHandle (idInst, hszNetscapeName);
|
---|
| 117 | hszNetscapeName = NULL;
|
---|
| 118 | if (hszIExploreName != NULL) DdeFreeStringHandle (idInst, hszIExploreName);
|
---|
| 119 | hszIExploreName = NULL;
|
---|
| 120 | if (hszOpenURLTopic != NULL) DdeFreeStringHandle (idInst, hszOpenURLTopic);
|
---|
| 121 | hszOpenURLTopic = NULL;
|
---|
| 122 | if (hszActivateTopic != NULL) DdeFreeStringHandle (idInst, hszActivateTopic);
|
---|
| 123 | hszActivateTopic = NULL;
|
---|
[611] | 124 | }
|
---|
| 125 |
|
---|
| 126 | static void getbrowserinfo() {
|
---|
[2286] | 127 | freestrings();
|
---|
| 128 |
|
---|
| 129 | hszNetscapeName = DdeCreateStringHandle(idInst,"NETSCAPE",CP_WINANSI);
|
---|
| 130 | hszIExploreName = DdeCreateStringHandle(idInst,"IExplore",CP_WINANSI);
|
---|
| 131 |
|
---|
| 132 | hszOpenURLTopic = DdeCreateStringHandle(idInst,"WWW_OpenURL",CP_WINANSI);
|
---|
| 133 | hszActivateTopic = DdeCreateStringHandle(idInst,"WWW_Activate",CP_WINANSI);
|
---|
[611] | 134 | }
|
---|
| 135 |
|
---|
| 136 | // returns SB_NOERROR on success,
|
---|
| 137 | // SB_ALREADYINIT or SB_DDE_FAILINIT on failure
|
---|
| 138 | int initstartbrowser () {
|
---|
[2286] | 139 | if (inited) return SB_ALREADYINIT;
|
---|
| 140 |
|
---|
| 141 | // from nstest if(DdeInitialize(&CDDEObject::m_dwidInst, NstestDdeCallBack, APPCLASS_STANDARD, 0ul))
|
---|
| 142 | if (DdeInitialize (&idInst, (PFNCALLBACK) DdeCallback,
|
---|
| 143 | APPCLASS_STANDARD, 0)
|
---|
| 144 | != DMLERR_NO_ERROR) {
|
---|
| 145 | freestrings();
|
---|
| 146 | inited = 0;
|
---|
| 147 | return SB_DDE_FAILINIT;
|
---|
| 148 | }
|
---|
[611] | 149 |
|
---|
[2286] | 150 | getbrowserinfo();
|
---|
| 151 |
|
---|
| 152 | inited = 1;
|
---|
| 153 | return SB_NOERROR;
|
---|
[611] | 154 | }
|
---|
| 155 |
|
---|
| 156 |
|
---|
| 157 | // returns SB_NOERROR on success,
|
---|
| 158 | // SB_DDE_FAILDEINIT of failure
|
---|
| 159 | int deinitstartbrowser () {
|
---|
[2286] | 160 | // make sure this has been inited
|
---|
| 161 | // not being inited here, however, is not
|
---|
| 162 | // important
|
---|
| 163 | if (!inited) return SB_NOERROR;
|
---|
| 164 | inited = 0;
|
---|
| 165 |
|
---|
| 166 | freestrings();
|
---|
| 167 |
|
---|
| 168 | if (DdeUninitialize(idInst)) return SB_NOERROR;
|
---|
| 169 |
|
---|
| 170 | return SB_DDE_FAILDEINIT;
|
---|
[611] | 171 | }
|
---|
| 172 |
|
---|
| 173 |
|
---|
| 174 | // startbrowser will try to start a browser (or connect to a running browser).
|
---|
| 175 | //
|
---|
| 176 | // Returns SB_NOERROR on success,
|
---|
| 177 | // SB_NOTINITED, SB_FAIL_BADFORMAT, SB_FAIL_NOTFOUND, SB_FAIL_NORESOURCES, or
|
---|
| 178 | // SB_FAILCONNECTBROWSER on failure
|
---|
| 179 | int startbrowser (char *url, char *browser_exe, char *startdir) {
|
---|
[2286] | 180 | char newcommand[SB_COMLEN];
|
---|
| 181 | int usingnetscape = 0;
|
---|
| 182 | int usingiexplore = 0;
|
---|
| 183 |
|
---|
| 184 | if (!inited) return SB_NOTINITED; // has to be inited first
|
---|
| 185 |
|
---|
| 186 | // find out which browser we are dealing with
|
---|
| 187 | strcpy (newcommand, browser_exe);
|
---|
| 188 | _strlwr (newcommand);
|
---|
| 189 | if (strstr (newcommand, "netscape.exe") != NULL) {
|
---|
| 190 | // netscape
|
---|
| 191 | usingnetscape = 1;
|
---|
| 192 | } else if (strstr (newcommand, "iexplore.exe") != NULL) {
|
---|
| 193 | // internet explorer
|
---|
| 194 | usingiexplore = 1;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | // only try to communicate with a running browser if a startdir
|
---|
| 198 | // is not specified
|
---|
| 199 | if (startdir == NULL) {
|
---|
| 200 | if (usingnetscape && hszNetscapeName != NULL &&
|
---|
| 201 | openurl(hszNetscapeName, hszActivateTopic, hszOpenURLTopic, url)) {
|
---|
| 202 | return SB_NOERROR;
|
---|
| 203 | }
|
---|
| 204 | if (usingiexplore && hszIExploreName != NULL &&
|
---|
| 205 | openurl(hszIExploreName, hszActivateTopic, hszOpenURLTopic, url)) {
|
---|
| 206 | return SB_NOERROR;
|
---|
| 207 | }
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | strcpy (newcommand, browser_exe);
|
---|
| 211 |
|
---|
| 212 | // don't put the url on the command line for internet explorer
|
---|
| 213 | if (usingiexplore) {
|
---|
| 214 | strcat (newcommand, " -nohome");
|
---|
| 215 |
|
---|
| 216 | } else {
|
---|
| 217 | // will have to put the url on the command line
|
---|
| 218 | strcat (newcommand, " \"");
|
---|
| 219 | strcat (newcommand, url);
|
---|
| 220 | strcat (newcommand, "\"");
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | // change the working directory (if needed)
|
---|
| 224 | char cwd[1024];
|
---|
| 225 | cwd[0] = '\0';
|
---|
| 226 | if (startdir != NULL) {
|
---|
| 227 | _getcwd(cwd, 1024);
|
---|
| 228 | _chdir(startdir);
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | // attempt to start the browser
|
---|
| 232 | int res = WinExec(newcommand, SW_SHOW);
|
---|
| 233 |
|
---|
| 234 | // change the working directory back (if needed)
|
---|
| 235 | if (startdir != NULL) _chdir(cwd);
|
---|
| 236 |
|
---|
| 237 | if (res == ERROR_BAD_FORMAT) return SB_FAIL_BADFORMAT;
|
---|
| 238 | if ((res == ERROR_FILE_NOT_FOUND) || (res == ERROR_PATH_NOT_FOUND))
|
---|
| 239 | return SB_FAIL_NOTFOUND;
|
---|
| 240 | if (res <= 31) return SB_FAIL_NORESOURCES;
|
---|
| 241 |
|
---|
| 242 | numbrowserstarted++;
|
---|
| 243 |
|
---|
| 244 | // if we aren't using iexplore then the url was
|
---|
| 245 | // put on the command line and there is nothing left to do
|
---|
| 246 | if (!usingiexplore) return SB_NOERROR;
|
---|
| 247 |
|
---|
| 248 | // connect to the browser and get it to open a page,
|
---|
| 249 | // time out after 1 minute
|
---|
| 250 | DWORD lastcheck = GetTickCount ();
|
---|
| 251 | while (DiffTickCounts (lastcheck, GetTickCount()) <= 60000) {
|
---|
| 252 | if (usingnetscape && hszNetscapeName != NULL &&
|
---|
| 253 | openurl(hszNetscapeName, hszActivateTopic, hszOpenURLTopic, url)) {
|
---|
| 254 | return SB_NOERROR;
|
---|
| 255 | }
|
---|
| 256 | if (usingiexplore && hszIExploreName != NULL &&
|
---|
| 257 | openurl(hszIExploreName, hszActivateTopic, hszOpenURLTopic, url)) {
|
---|
| 258 | return SB_NOERROR;
|
---|
| 259 | }
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | // timed out trying to connect to the browser
|
---|
| 263 | return SB_FAILCONNECTBROWSER;
|
---|
[611] | 264 | }
|
---|
| 265 |
|
---|
| 266 | // returns SB_NOERROR on success,
|
---|
| 267 | // SB_NOTINITED, SB_FAILCONNECTBROWSER on failure
|
---|
| 268 | int browserrunning (char *browser_exe) {
|
---|
[2286] | 269 | char newcommand[SB_COMLEN];
|
---|
| 270 | if (!inited) return SB_NOTINITED; // has to be inited first
|
---|
| 271 |
|
---|
| 272 | strcpy (newcommand, browser_exe);
|
---|
| 273 | _strlwr (newcommand);
|
---|
| 274 |
|
---|
| 275 | if ((strstr (newcommand, "netscape.exe") != NULL) && (hszNetscapeName != NULL) &&
|
---|
| 276 | tryconnect(hszNetscapeName, hszOpenURLTopic)) {
|
---|
| 277 | // succeeded connecting to netscape
|
---|
| 278 | return SB_NOERROR;
|
---|
| 279 |
|
---|
| 280 | } else if ((strstr (newcommand, "iexplore.exe") != NULL) && (hszIExploreName != NULL) &&
|
---|
| 281 | tryconnect(hszIExploreName, hszOpenURLTopic)) {
|
---|
| 282 | // succeeded connecting to internet explorer
|
---|
| 283 | return SB_NOERROR;
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 286 | return SB_FAILCONNECTBROWSER;
|
---|
| 287 | }
|
---|