Update ooo320-m1
[ooovba.git] / shell / source / unix / misc / open-url.c
blob32c7503344d28a3b674ebdc0ad67d307f1c14f43
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org 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 Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <process.h>
33 #include <time.h>
35 #define INCL_DOS
36 #define INCL_DOSERRORS
37 #define INCL_PM
38 #include <os2.h>
40 // OOo uses popen() to start us, so we cannot show PM dialogs.
41 // log message to disk.
42 void logMessage( char* msg)
44 PPIB pib;
45 CHAR szApplicationName[_MAX_PATH];
46 CHAR szDrive[_MAX_PATH];
47 CHAR szDir[_MAX_PATH];
48 CHAR szFileName[_MAX_PATH];
49 CHAR szExt[_MAX_PATH];
50 FILE* log;
51 time_t timeOfDay;
52 struct tm* localTime;
54 // get executable fullpath
55 DosGetInfoBlocks(NULL, &pib);
56 DosQueryModuleName(pib->pib_hmte, sizeof(szApplicationName), szApplicationName);
57 _splitpath( szApplicationName, szDrive, szDir, szFileName, szExt );
58 // log name
59 _makepath( szApplicationName, szDrive, szDir, szFileName, (".LOG") );
60 log = fopen( szApplicationName, "a");
61 if (!log)
62 return;
63 time( &timeOfDay);
64 localTime = localtime( &timeOfDay);
65 fprintf( log, "%04d/%02d/%02d %02d:%02d:%02d %s\n",
66 localTime->tm_year+1900, localTime->tm_mon+1, localTime->tm_mday,
67 localTime->tm_hour, localTime->tm_min, localTime->tm_sec, msg);
68 fclose( log);
71 // dump comand line arguments
72 void dumpArgs( int argc, char *argv[] )
74 int i;
76 logMessage( "Start of command line arguments dump:");
77 for( i=0; i<argc; i++)
78 logMessage( argv[i]);
81 /*
82 * The intended use of this tool is to pass the argument to
83 * the default URL exe.
85 int main(int argc, char *argv[] )
87 APIRET rc;
88 RESULTCODES result = {0};
89 char szAppFromINI[_MAX_PATH];
90 char szDirFromINI[_MAX_PATH];
91 char szCmdLine[1024];
92 char szFail[ _MAX_PATH];
93 ULONG ulSID;
94 PID pid;
96 // check parameters
97 if (argc != 2)
99 logMessage( "Usage: open-url <url>");
100 dumpArgs( argc, argv);
101 return -1;
104 // check configuration
105 rc = PrfQueryProfileString(HINI_USER, "WPURLDEFAULTSETTINGS",
106 "DefaultBrowserExe", "",
107 szAppFromINI, sizeof(szAppFromINI));
108 rc = PrfQueryProfileString(HINI_USER, "WPURLDEFAULTSETTINGS",
109 "DefaultWorkingDir", "",
110 szDirFromINI, sizeof(szDirFromINI));
111 if (*szAppFromINI == 0 || *szDirFromINI == 0)
113 logMessage( "Unable to find default url handler in USER.INI; exiting.");
114 dumpArgs( argc, argv);
115 return -1;
118 // get default parameter list
119 rc = PrfQueryProfileString(HINI_USER, "WPURLDEFAULTSETTINGS",
120 "DefaultParameters", "",
121 szCmdLine, sizeof(szCmdLine));
122 strcat( szCmdLine, " ");
123 strcat( szCmdLine, argv[1]);
125 // change default directory
126 _chdir( szDirFromINI);
128 // start default handler
129 STARTDATA SData;
130 CHAR szObjBuf[CCHMAXPATH];
132 SData.Length = sizeof(STARTDATA);
133 SData.Related = SSF_RELATED_INDEPENDENT;
134 SData.FgBg = (1) ? SSF_FGBG_FORE : SSF_FGBG_BACK;
135 SData.TraceOpt = SSF_TRACEOPT_NONE;
137 SData.PgmTitle = (PSZ)szAppFromINI;
139 SData.PgmName = (PSZ)szAppFromINI;
140 SData.PgmInputs = (PSZ)szCmdLine;
142 SData.TermQ = NULL;
143 SData.Environment = 0;
144 SData.InheritOpt = SSF_INHERTOPT_PARENT;
145 SData.SessionType = SSF_TYPE_PM;
146 SData.IconFile = 0;
147 SData.PgmHandle = 0;
149 SData.PgmControl = SSF_CONTROL_VISIBLE;
151 SData.InitXPos = 30;
152 SData.InitYPos = 40;
153 SData.InitXSize = 200;
154 SData.InitYSize = 140;
155 SData.Reserved = 0;
156 SData.ObjectBuffer = szFail;
157 SData.ObjectBuffLen = (ULONG)sizeof(szFail);
159 rc = DosStartSession( &SData, &ulSID, &pid);
160 // show error dialog in case of problems
161 if (rc != NO_ERROR && rc != ERROR_SMG_START_IN_BACKGROUND) {
162 char szMessage[ _MAX_PATH*2];
163 sprintf( szMessage, "Execution failed! rc: %d, failing module:%s", rc, szFail);
164 logMessage( szMessage);
165 dumpArgs( argc, argv);
166 return -1;
169 // ok
170 return 0;