Fix for a crash which happened when a document couldn't be opened.
[AROS-Contrib.git] / vpdf / vpdf.c
bloba7b60be0640c39450f9cdb20c1c07126ba90e721
2 #if defined(__AROS__)
3 #define MUIMASTER_YES_INLINE_STDARG
4 #endif
6 #define SYSTEM_PRIVATE
7 #define SYSTEM_REALLY_PRIVATE
8 #include <stdio.h>
9 #include <stdlib.h>
11 #include <proto/muimaster.h>
12 #include <proto/intuition.h>
13 #include <proto/dos.h>
14 #include <proto/exec.h>
15 #include <proto/locale.h>
16 #include <proto/icon.h>
18 #if 0
19 #include <exec/execbase.h>
20 #endif
22 #include <private/vapor/vapor.h>
23 #include <libraries/mui.h>
24 #include <workbench/startup.h>
25 #include <clib/alib_protos.h>
26 #include <proto/rexxsyslib.h>
28 #include "util.h"
29 #include "version.h"
30 #include "mcc/classes.h"
32 #include "system/system.h"
34 #include "mcc/pageview_class.h"
35 #include "mcc/documentview_class.h"
36 #include "mcc/outlineview_class.h"
37 #include "mcc/documentlayout_class.h"
38 #include "mcc/toolbar_class.h"
39 #include "mcc/renderer_class.h"
40 #include "mcc/title_class.h"
41 #include "mcc/search_class.h"
42 #include "mcc/thumbnaillist_class.h"
43 #include "mcc/lay_class.h"
44 #include "mcc/annotation_class.h"
45 #include "application.h"
46 #include "logger.h"
47 #include "window.h"
48 #include "pagenumberclass.h"
49 #include "arrowstring_class.h"
50 #include "printer.h"
51 #include "settings.h"
52 #include "locale.h"
54 int __stack = 1000000;
56 #define PROGNAME "VPDF"
57 #define ARG_FILE 0
58 #define ARG_NEWAPP 1
59 #define ARG_NUMARGS 3
60 #define ARG_TEMPLATE "FILE/M,NEWAPP/S,APPTOFRONT/S"
63 extern struct Library *CairoBase;
64 extern char * _ProgramName; // from libnix startup code
66 struct classinitialization_function
68 ULONG (*initfunc)(void);
69 void (*endfunc)(void);
70 int done;
74 static struct classinitialization_function classinitialization_functions[] = {
75 {create_RendererClass, delete_RendererClass},
76 {create_AnnotationClass, delete_AnnotationClass},
77 {create_LayClass, delete_LayClass},
78 //{create_ThumbnailClass, delete_ThumbnailClass},
79 {create_SearchClass, delete_SearchClass},
80 {create_TitleClass, delete_TitleClass},
81 {create_ToolbarClass, delete_ToolbarClass},
82 {create_ContinuousLayoutClass, delete_ContinuousLayoutClass},
83 {create_ContinuousDynamicLayoutClass, delete_ContinuousDynamicLayoutClass},
84 {create_SinglePageLayoutClass, delete_SinglePageLayoutClass},
85 {create_PageViewClass, delete_PageViewClass},
86 {create_OutlineViewClass, delete_OutlineViewClass},
87 {create_DocumentViewClass, delete_DocumentViewClass},
88 {create_ThumbnailListClass, delete_ThumbnailListClass},
89 {create_VPDFSettingsClass, delete_VPDFSettingsClass},
90 {create_VPDFWindowClass, delete_VPDFWindowClass},
91 {create_VPDFTitleButtonClass, delete_VPDFTitleButtonClass},
92 {create_ArrowStringClass, delete_ArrowStringClass},
93 {create_VPDFNumberGeneratorClass, delete_VPDFNumberGeneratorClass},
94 {create_VPDFPrinterClass, delete_VPDFPrinterClass},
95 {create_VPDFClass, delete_VPDFClass},
96 {create_LoggerClass, delete_LoggerClass},
97 {NULL}
100 static void cleanupclasses(void)
102 int i = sizeof(classinitialization_functions) / sizeof(*classinitialization_functions) - 1;
104 while (i >= 0)
106 if (classinitialization_functions[ i ].done)
107 classinitialization_functions[ i ].endfunc();
108 i--;
112 static Object *_app = NULL;
114 void vpdfErrorFunction(int pos, char *message)
116 if (_app != NULL && message != NULL)
117 DoMethod(_app, MUIM_Application_PushMethod, _app, 3, MUIM_VPDF_LogMessage, MUIV_VPDF_LogMessage_Error, strdup(message));
120 static void MessageAddFile(struct MsgPort *port, struct MsgPort *replyport, struct RexxMsg *rexxmsg, STRPTR file)
122 char arexxstr[1024];
123 char filepath[1024];
125 BPTR lock = Lock(file, ACCESS_READ);
127 strcpy(arexxstr, "OPEN FILE=");
129 if(lock == NULL)
131 lock = Lock("", ACCESS_READ);
132 NameFromLock(lock, filepath, sizeof(filepath));
133 AddPart(filepath, file, sizeof(filepath));
135 else
136 NameFromLock(lock, filepath, sizeof(filepath));
138 if(lock != NULL)
140 char *ftemp = filepath;
142 UnLock(lock);
144 while(*ftemp != '\0')
146 if(*ftemp == ' ')
147 *ftemp = '\\';
148 ftemp++;
151 strlcat(arexxstr, filepath, sizeof(arexxstr));
152 rexxmsg->rm_Args[0] = (char *)CreateArgstring(arexxstr, strlen(arexxstr));
153 rexxmsg->rm_Action = RXCOMM;
154 PutMsg(port, (struct Message*)rexxmsg);
155 WaitPort(replyport);
156 GetMsg(replyport);
157 DeleteArgstring(rexxmsg->rm_Args[0]);
161 int main(int argc, char *argv[])
163 #if 0
164 struct ExecBase *MySysBase = *(unsigned int*)4;
166 /* make us halt on first hit! */
168 struct Task *this = FindTask(NULL);
169 this->tc_ETask->MaxHits = 1;
173 MySysBase->ex_AlertMsgPort = NULL;
174 #endif
175 InitSystem(argc, argv);
176 /* initialize classes */
178 locale_init();
181 int i = 0;
183 while (classinitialization_functions[ i ].initfunc != NULL)
185 if (!classinitialization_functions[ i ].initfunc())
187 char message[ 512 ];
189 snprintf(message, sizeof(message), "Failed to initialize class (%d)", i);
190 MUI_Request(NULL, NULL, 0, "Error...", "OK", message, TAG_END);
191 cleanupclasses();
192 return NULL;
194 classinitialization_functions[ i ].done = TRUE;
195 i++;
199 /* handle params */
201 struct RDArgs *myargs = NULL;
202 LONG args[ARG_NUMARGS] = {0};
203 Object *app, *window;
204 ULONG sigs = 0;
205 char buf[80];
206 struct MsgPort *port = NULL;
207 struct DiskObject *disk_object = NULL;
209 app = NULL;
211 if(argc && !(myargs = ReadArgs(ARG_TEMPLATE, args, NULL)))
212 Fault(IoErr(), 0, buf, sizeof(buf));
213 else
216 if(!args[ARG_NEWAPP])
218 Forbid();
219 port = FindPort("VPDF.1");
220 if(port)
222 struct MsgPort *replyport;
223 struct RexxMsg *rexxmsg;
225 Permit();
226 replyport = CreateMsgPort();
227 rexxmsg = CreateRexxMsg(replyport, NULL, "VPDF.1");
228 if(rexxmsg != NULL)
230 char arexxstr[1024];
232 if(args[ARG_FILE])
234 char **files = (char**)args[ARG_FILE];
235 int i;
237 for(i = 0; files[i]; i++)
238 MessageAddFile(port, replyport, rexxmsg, files[i]);
240 else if(argc == 0)
242 struct WBStartup *wbs = (struct WBStartup *)argv;
243 struct WBArg *wba;
244 char filename[1024];
245 int i = 1;
247 while(i < wbs->sm_NumArgs)
249 wba = wbs->sm_ArgList + i;
251 if(wba->wa_Lock)
253 if(NameFromLock(wba->wa_Lock, filename, sizeof(filename)))
255 if(AddPart(filename, (char*)wba->wa_Name, sizeof(filename)))
256 MessageAddFile(port, replyport, rexxmsg, filename);
260 i++;
264 strcpy(arexxstr, "APPTOFRONT");
265 rexxmsg->rm_Args[0] = (char *)CreateArgstring(arexxstr, strlen(arexxstr));
266 rexxmsg->rm_Action = RXCOMM;
268 Forbid();
269 port = FindPort("VPDF.1");
270 if(port)
273 PutMsg(port, (struct Message*)rexxmsg);
274 WaitPort(replyport);
275 GetMsg(replyport);
277 Permit();
278 DeleteArgstring(rexxmsg->rm_Args[0]);
279 DeleteRexxMsg(rexxmsg);
281 if(replyport)
282 DeleteMsgPort(replyport);
284 else
285 Permit();
288 if(!port)
290 /* build new application object */
291 disk_object =GetDiskObject(_ProgramName);
293 app = VPDFObject,
294 MUIA_Application_Title, PROGNAME,
295 MUIA_Application_Version, "$VER: " PROGNAME " " VERSION "." REVISION " ("VERSIONDATE ")""© 2009-2012 by Michal Wozniak",
296 MUIA_Application_Copyright, "© 2009-2012 by Michal Wozniak",
297 MUIA_Application_Author, "Michal Wozniak",
298 MUIA_Application_Description, LOCSTR( MSG_APPLICATION_DESCRIPTION ),
299 MUIA_Application_Base, "VPDF",
300 MUIA_Application_DiskObject, disk_object,
301 End;
303 if(app != NULL)
305 int opened = 0;
306 DoMethod(app, MUIM_Application_Load, MUIV_Application_Load_ENV);
307 _app = app;
309 if(argc)
311 if(args[ARG_FILE])
313 char **files = (char**)args[ARG_FILE];
314 int i;
316 for(i = 0; files[i] != NULL; i++)
318 DoMethod(app, MUIM_VPDF_OpenFile, 0, files[i], MUIV_VPDFWindow_OpenFile_NewTab);
319 opened++;
323 else
325 struct WBStartup *wbs = (struct WBStartup *)argv;
326 struct WBArg *wba;
327 char filename[1024];
328 int i = 1;
330 while(i < wbs->sm_NumArgs)
332 wba = wbs->sm_ArgList + i;
334 if(wba->wa_Lock)
336 if(NameFromLock(wba->wa_Lock, filename, sizeof(filename)))
338 if(AddPart(filename, (char*)wba->wa_Name, sizeof(filename)))
340 DoMethod(app, MUIM_VPDF_OpenFile, 0, filename, MUIV_VPDFWindow_OpenFile_NewTab);
341 opened++;
346 i++;
350 if (opened == 0)
351 DoMethod(app, MUIM_VPDF_CreateWindow);
353 while(DoMethod(app, MUIM_Application_NewInput, &sigs) != (ULONG)MUIV_Application_ReturnID_Quit)
355 if(sigs)
357 sigs = Wait(sigs | SIGBREAKF_CTRL_C);
358 if(sigs & SIGBREAKF_CTRL_C) break;
362 DoMethod(app, MUIM_Application_Save, MUIV_Application_Save_ENV);
363 DoMethod(app, MUIM_Application_Save, MUIV_Application_Save_ENVARC);
365 MUI_DisposeObject(app);
367 if (disk_object)
368 FreeDiskObject(disk_object);
372 if(myargs)
373 FreeArgs(myargs);
380 cleanupclasses();
381 locale_cleanup();
382 return 0;