merge the formfield patch from ooo-build
[ooovba.git] / shell / source / unix / misc / senddoc.c
blob3de1b6118faf01d67ec0d038e560b00f7006f4f6
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 mail handler.
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;
95 int i;
96 BOOL bMailClient = FALSE;
98 // check parameters
99 if (argc < 5)
101 logMessage( "Usage: senddoc --mailclient <client> --attach <uri>");
102 dumpArgs( argc, argv);
103 return -1;
106 // check configuration
107 rc = PrfQueryProfileString(HINI_USER, "WPURLDEFAULTSETTINGS",
108 "DefaultMailExe", "",
109 szAppFromINI, sizeof(szAppFromINI));
110 rc = PrfQueryProfileString(HINI_USER, "WPURLDEFAULTSETTINGS",
111 "DefaultMailWorkingDir", "",
112 szDirFromINI, sizeof(szDirFromINI));
113 if (*szAppFromINI == 0 || *szDirFromINI == 0)
115 logMessage( "Unable to find default mail handler in USER.INI; exiting.");
116 dumpArgs( argc, argv);
117 return -1;
120 // get default parameter list, at leat -compose is required
121 rc = PrfQueryProfileString(HINI_USER, "WPURLDEFAULTSETTINGS",
122 "DefaultMailParameters", "",
123 szCmdLine, sizeof(szCmdLine));
124 if (strstr( szCmdLine, "-compose") == 0)
125 strcat( szCmdLine, " -compose"); // add if missing!
127 // parse cmdline arguments
128 for( i=1; i<argc; i++)
130 if (!strcmp( argv[i], "--mailclient")) {
131 // we support only Thunderbird/Mozilla command line options, check exe name
132 if (strstr( argv[i+1], "thunderbird") == 0
133 && strstr( argv[i+1], "mozilla") == 0
134 && strstr( argv[i+1], "seamonkey") == 0)
136 logMessage( "Only Thunderbird/Mozilla is currently supported. Exiting.");
137 dumpArgs( argc, argv);
138 return -1;
140 // mail client found
141 bMailClient = TRUE;
142 i++;
143 } else if (!strcmp( argv[i], "--attach")) {
144 strcat( szCmdLine, " attachment=file://");
145 strcat( szCmdLine, argv[i+1]);
146 i++;
148 // ignore other options (BTW currently none)
150 if (bMailClient == FALSE)
152 logMessage( "No mail client specified. Exiting.");
153 dumpArgs( argc, argv);
154 return -1;
157 // change default directory
158 _chdir( szDirFromINI);
160 // start default handler
161 STARTDATA SData;
162 CHAR szObjBuf[CCHMAXPATH];
164 SData.Length = sizeof(STARTDATA);
165 SData.Related = SSF_RELATED_INDEPENDENT;
166 SData.FgBg = (1) ? SSF_FGBG_FORE : SSF_FGBG_BACK;
167 SData.TraceOpt = SSF_TRACEOPT_NONE;
169 SData.PgmTitle = (PSZ)szAppFromINI;
171 SData.PgmName = (PSZ)szAppFromINI;
172 SData.PgmInputs = (PSZ)szCmdLine;
174 SData.TermQ = NULL;
175 SData.Environment = 0;
176 SData.InheritOpt = SSF_INHERTOPT_PARENT;
177 SData.SessionType = SSF_TYPE_PM;
178 SData.IconFile = 0;
179 SData.PgmHandle = 0;
181 SData.PgmControl = SSF_CONTROL_VISIBLE;
183 SData.InitXPos = 30;
184 SData.InitYPos = 40;
185 SData.InitXSize = 200;
186 SData.InitYSize = 140;
187 SData.Reserved = 0;
188 SData.ObjectBuffer = szFail;
189 SData.ObjectBuffLen = (ULONG)sizeof(szFail);
191 rc = DosStartSession( &SData, &ulSID, &pid);
192 // show error dialog in case of problems
193 if (rc != NO_ERROR && rc != ERROR_SMG_START_IN_BACKGROUND) {
194 char szMessage[ _MAX_PATH*2];
195 sprintf( szMessage, "Execution failed! rc: %d, failing module:%s", rc, szFail);
196 logMessage( szMessage);
197 dumpArgs( argc, argv);
198 return -1;
201 // ok
202 return 0;