update dev300-m58
[ooovba.git] / shell / source / unix / misc / senddoc.c
blob8f5bcfabd43375efa0b9d844ee7711e049c1b538
1 /*************************************************************************
3 * OpenOffice.org - a multi-platform office productivity suite
5 * $RCSfile:$
7 * $Revision:$
9 * last change: $Author:$
11 * The Contents of this file are made available subject to
12 * the terms of GNU Lesser General Public License Version 2.1.
15 * GNU Lesser General Public License Version 2.1
16 * =============================================
17 * Copyright 2005 by Sun Microsystems, Inc.
18 * 901 San Antonio Road, Palo Alto, CA 94303, USA
20 * This library is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU Lesser General Public
22 * License version 2.1, as published by the Free Software Foundation.
24 * This library is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * Lesser General Public License for more details.
29 * You should have received a copy of the GNU Lesser General Public
30 * License along with this library; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
32 * MA 02111-1307 USA
34 ************************************************************************/
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <process.h>
41 #include <time.h>
43 #define INCL_DOS
44 #define INCL_DOSERRORS
45 #define INCL_PM
46 #include <os2.h>
48 // OOo uses popen() to start us, so we cannot show PM dialogs.
49 // log message to disk.
50 void logMessage( char* msg)
52 PPIB pib;
53 CHAR szApplicationName[_MAX_PATH];
54 CHAR szDrive[_MAX_PATH];
55 CHAR szDir[_MAX_PATH];
56 CHAR szFileName[_MAX_PATH];
57 CHAR szExt[_MAX_PATH];
58 FILE* log;
59 time_t timeOfDay;
60 struct tm* localTime;
62 // get executable fullpath
63 DosGetInfoBlocks(NULL, &pib);
64 DosQueryModuleName(pib->pib_hmte, sizeof(szApplicationName), szApplicationName);
65 _splitpath( szApplicationName, szDrive, szDir, szFileName, szExt );
66 // log name
67 _makepath( szApplicationName, szDrive, szDir, szFileName, (".LOG") );
68 log = fopen( szApplicationName, "a");
69 if (!log)
70 return;
71 time( &timeOfDay);
72 localTime = localtime( &timeOfDay);
73 fprintf( log, "%04d/%02d/%02d %02d:%02d:%02d %s\n",
74 localTime->tm_year+1900, localTime->tm_mon+1, localTime->tm_mday,
75 localTime->tm_hour, localTime->tm_min, localTime->tm_sec, msg);
76 fclose( log);
79 // dump comand line arguments
80 void dumpArgs( int argc, char *argv[] )
82 int i;
84 logMessage( "Start of command line arguments dump:");
85 for( i=0; i<argc; i++)
86 logMessage( argv[i]);
89 /*
90 * The intended use of this tool is to pass the argument to
91 * the default mail handler.
93 int main(int argc, char *argv[] )
95 APIRET rc;
96 RESULTCODES result = {0};
97 char szAppFromINI[_MAX_PATH];
98 char szDirFromINI[_MAX_PATH];
99 char szCmdLine[1024];
100 char szFail[ _MAX_PATH];
101 ULONG ulSID;
102 PID pid;
103 int i;
104 BOOL bMailClient = FALSE;
106 // check parameters
107 if (argc < 5)
109 logMessage( "Usage: senddoc --mailclient <client> --attach <uri>");
110 dumpArgs( argc, argv);
111 return -1;
114 // check configuration
115 rc = PrfQueryProfileString(HINI_USER, "WPURLDEFAULTSETTINGS",
116 "DefaultMailExe", "",
117 szAppFromINI, sizeof(szAppFromINI));
118 rc = PrfQueryProfileString(HINI_USER, "WPURLDEFAULTSETTINGS",
119 "DefaultMailWorkingDir", "",
120 szDirFromINI, sizeof(szDirFromINI));
121 if (*szAppFromINI == 0 || *szDirFromINI == 0)
123 logMessage( "Unable to find default mail handler in USER.INI; exiting.");
124 dumpArgs( argc, argv);
125 return -1;
128 // get default parameter list, at leat -compose is required
129 rc = PrfQueryProfileString(HINI_USER, "WPURLDEFAULTSETTINGS",
130 "DefaultMailParameters", "",
131 szCmdLine, sizeof(szCmdLine));
132 if (strstr( szCmdLine, "-compose") == 0)
133 strcat( szCmdLine, " -compose"); // add if missing!
135 // parse cmdline arguments
136 for( i=1; i<argc; i++)
138 if (!strcmp( argv[i], "--mailclient")) {
139 // we support only Thunderbird/Mozilla command line options, check exe name
140 if (strstr( argv[i+1], "thunderbird") == 0
141 && strstr( argv[i+1], "mozilla") == 0
142 && strstr( argv[i+1], "seamonkey") == 0)
144 logMessage( "Only Thunderbird/Mozilla is currently supported. Exiting.");
145 dumpArgs( argc, argv);
146 return -1;
148 // mail client found
149 bMailClient = TRUE;
150 i++;
151 } else if (!strcmp( argv[i], "--attach")) {
152 strcat( szCmdLine, " attachment=file://");
153 strcat( szCmdLine, argv[i+1]);
154 i++;
156 // ignore other options (BTW currently none)
158 if (bMailClient == FALSE)
160 logMessage( "No mail client specified. Exiting.");
161 dumpArgs( argc, argv);
162 return -1;
165 // change default directory
166 _chdir( szDirFromINI);
168 // start default handler
169 STARTDATA SData;
170 CHAR szObjBuf[CCHMAXPATH];
172 SData.Length = sizeof(STARTDATA);
173 SData.Related = SSF_RELATED_INDEPENDENT;
174 SData.FgBg = (1) ? SSF_FGBG_FORE : SSF_FGBG_BACK;
175 SData.TraceOpt = SSF_TRACEOPT_NONE;
177 SData.PgmTitle = (PSZ)szAppFromINI;
179 SData.PgmName = (PSZ)szAppFromINI;
180 SData.PgmInputs = (PSZ)szCmdLine;
182 SData.TermQ = NULL;
183 SData.Environment = 0;
184 SData.InheritOpt = SSF_INHERTOPT_PARENT;
185 SData.SessionType = SSF_TYPE_PM;
186 SData.IconFile = 0;
187 SData.PgmHandle = 0;
189 SData.PgmControl = SSF_CONTROL_VISIBLE;
191 SData.InitXPos = 30;
192 SData.InitYPos = 40;
193 SData.InitXSize = 200;
194 SData.InitYSize = 140;
195 SData.Reserved = 0;
196 SData.ObjectBuffer = szFail;
197 SData.ObjectBuffLen = (ULONG)sizeof(szFail);
199 rc = DosStartSession( &SData, &ulSID, &pid);
200 // show error dialog in case of problems
201 if (rc != NO_ERROR && rc != ERROR_SMG_START_IN_BACKGROUND) {
202 char szMessage[ _MAX_PATH*2];
203 sprintf( szMessage, "Execution failed! rc: %d, failing module:%s", rc, szFail);
204 logMessage( szMessage);
205 dumpArgs( argc, argv);
206 return -1;
209 // ok
210 return 0;