xref: /aoo42x/main/shell/source/unix/misc/open-url.c (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <process.h>
33 #include <time.h>
34 
35 #define INCL_DOS
36 #define INCL_DOSERRORS
37 #define INCL_PM
38 #include <os2.h>
39 
40 // OOo uses popen() to start us, so we cannot show PM dialogs.
41 // log message to disk.
42 void logMessage( char* msg)
43 {
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;
53 
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);
69 }
70 
71 // dump comand line arguments
72 void dumpArgs( int argc, char *argv[] )
73 {
74     int	i;
75 
76     logMessage( "Start of command line arguments dump:");
77     for( i=0; i<argc; i++)
78 	logMessage( argv[i]);
79 }
80 
81 /*
82  * The intended use of this tool is to pass the argument to
83  * the default URL exe.
84  */
85 int main(int argc, char *argv[] )
86 {
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;
95 
96     // check parameters
97     if (argc != 2)
98     {
99 	logMessage( "Usage: open-url <url>");
100 	dumpArgs( argc, argv);
101         return -1;
102     }
103 
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)
112     {
113 	logMessage( "Unable to find default url handler in USER.INI; exiting.");
114 	dumpArgs( argc, argv);
115         return -1;
116     }
117 
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]);
124 
125     // change default directory
126     _chdir( szDirFromINI);
127 
128     // start default handler
129     STARTDATA   SData;
130     CHAR        szObjBuf[CCHMAXPATH];
131 
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;
136 
137     SData.PgmTitle = (PSZ)szAppFromINI;
138 
139     SData.PgmName = (PSZ)szAppFromINI;
140     SData.PgmInputs = (PSZ)szCmdLine;
141 
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;
148 
149     SData.PgmControl = SSF_CONTROL_VISIBLE;
150 
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);
158 
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;
167     }
168 
169     // ok
170     return 0;
171 }
172 
173