examples/PIPS/antiword/src/main_ros.c

00001 /*
00002  * main_ros.c
00003  *
00004  * Released under GPL
00005  *
00006  * Copyright (C) 1998-2005 A.J. van Os
00007  *
00008  * This program is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU General Public License
00010  * as published by the Free Software Foundation; either version 2
00011  * of the License, or (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00021  *
00022  * Description:
00023  * The main program of !Antiword (RISC OS version)
00024  */
00025 
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 #include <string.h>
00029 #include "DeskLib:Dialog2.h"
00030 #include "DeskLib:Error.h"
00031 #include "DeskLib:Event.h"
00032 #include "DeskLib:EventMsg.h"
00033 #include "DeskLib:Handler.h"
00034 #include "DeskLib:Menu.h"
00035 #include "DeskLib:Resource.h"
00036 #include "DeskLib:Screen.h"
00037 #include "DeskLib:Template.h"
00038 #include "DeskLib:Window.h"
00039 #if defined(__GNUC__)
00040 #include "flexlib:flex.h"
00041 #endif /* __GNUC__ */
00042 #include "version.h"
00043 #include "antiword.h"
00044 
00045 
00046 /* The name of this program */
00047 static char     *szTask = "!Antiword";
00048 
00049 /* The window handle of the choices window */
00050 static window_handle    tChoicesWindow = 0;
00051 
00052 /* Dummy diagram with the iconbar menu pointer */
00053 static diagram_type     tDummyDiagram;
00054 
00055 /* Program information Box */
00056 static dialog2_block    *pInfoBox = NULL;
00057 
00058 /* Info box fields */
00059 #define PURPOSE_INFO_FIELD      2
00060 #define AUTHOR_INFO_FIELD       3
00061 #define VERSION_INFO_FIELD      4
00062 #define STATUS_INFO_FIELD       5
00063 
00064 /* Iconbar menu fields */
00065 #define ICONBAR_INFO_FIELD      0
00066 #define ICONBAR_CHOICES_FIELD   1
00067 #define ICONBAR_QUIT_FIELD      2
00068 
00069 
00070 /*
00071  * bBarInfo - Show iconbar information
00072  */
00073 static BOOL
00074 bBarInfo(event_pollblock *pEvent, void *pvReference)
00075 {
00076         diagram_type    *pDiag;
00077 
00078         TRACE_MSG("bBarInfo");
00079 
00080         fail(pEvent == NULL);
00081         fail(pEvent->type != event_SEND);
00082         fail(pEvent->data.message.header.action != message_MENUWARN);
00083         fail(pvReference == NULL);
00084 
00085         pDiag = (diagram_type *)pvReference;
00086 
00087         if (menu_currentopen != pDiag->pSaveMenu ||
00088             pEvent->data.message.data.menuwarn.selection[0] != ICONBAR_INFO_FIELD) {
00089                 return FALSE;
00090         }
00091 
00092         Dialog2_OpenDialogMenuLeaf(pEvent, pInfoBox);
00093         return TRUE;
00094 } /* end of bBarInfo */
00095 
00096 /*
00097  * vBarInfoSetText - Set the iconbar infobox text
00098  */
00099 static void
00100 vBarInfoSetText(dialog2_block *pBox)
00101 {
00102         TRACE_MSG("vBarInfoSetText");
00103 
00104         fail(pBox == NULL);
00105         fail(pBox != pInfoBox);
00106 
00107         Icon_SetText(pBox->window, PURPOSE_INFO_FIELD, PURPOSESTRING);
00108         Icon_SetText(pBox->window, AUTHOR_INFO_FIELD, AUTHORSTRING);
00109         Icon_SetText(pBox->window, VERSION_INFO_FIELD, VERSIONSTRING);
00110         Icon_SetText(pBox->window, STATUS_INFO_FIELD, STATUSSTRING);
00111 } /* end of vBarInfoSetText */
00112 
00113 /*
00114  * bMouseButtonClick - respond to mouse button click
00115  */
00116 static BOOL
00117 bMouseButtonClick(event_pollblock *pEvent, void *pvReference)
00118 {
00119         diagram_type    *pDiag;
00120         menu_ptr        pMenu;
00121         int             iPosY;
00122 
00123         TRACE_MSG("bMouseButtonClick");
00124 
00125         fail(pEvent == NULL);
00126         fail(pEvent->type != event_CLICK);
00127         fail(pvReference == NULL);
00128 
00129         pDiag = (diagram_type *)pvReference;
00130 
00131         if (pEvent->data.mouse.button.data.menu) {
00132                 pMenu = pDiag->pSaveMenu;
00133                 iPosY = (pMenu == tDummyDiagram.pSaveMenu) ?
00134                                         -1 : pEvent->data.mouse.pos.y;
00135                 Menu_Show(pMenu, pEvent->data.mouse.pos.x, iPosY);
00136                 return TRUE;
00137         }
00138         if (pEvent->data.mouse.window == pDiag->tMainWindow &&
00139             pEvent->data.mouse.icon == -1) {
00140                 vMainButtonClick(&pEvent->data.mouse);
00141                 return TRUE;
00142         }
00143         if (pEvent->data.mouse.window == pDiag->tScaleWindow &&
00144             pEvent->data.mouse.icon >= 0) {
00145                 vScaleButtonClick(&pEvent->data.mouse, pDiag);
00146                 return TRUE;
00147         }
00148         return FALSE;
00149 } /* end of bMouseButtonClick */
00150 
00151 /*
00152  * bAutoRedrawWindow - the redraw is handled by the WIMP
00153  */
00154 static BOOL
00155 bAutoRedrawWindow(event_pollblock *pEvent, void *pvReference)
00156 {
00157         return TRUE;
00158 } /* end of bAutoRedrawWindow */
00159 
00160 static BOOL
00161 bSaveSelect(event_pollblock *pEvent, void *pvReference)
00162 {
00163         TRACE_MSG("bSaveSelect");
00164 
00165         fail(pEvent == NULL);
00166         fail(pEvent->type != event_MENU);
00167         fail(pvReference == NULL);
00168 
00169         DBG_DEC(pEvent->data.selection[0]);
00170 
00171         switch (pEvent->data.selection[0]) {
00172         case SAVEMENU_SCALEVIEW:
00173                 return bScaleOpenAction(pEvent, pvReference);
00174         case SAVEMENU_SAVEDRAW:
00175                 return bSaveDrawfile(pEvent, pvReference);
00176         case SAVEMENU_SAVETEXT:
00177                 return bSaveTextfile(pEvent, pvReference);
00178         default:
00179                 DBG_DEC(pEvent->data.selection[0]);
00180                 return FALSE;
00181         }
00182 } /* end of bSaveSelect */
00183 
00184 /*
00185  * Create the window for the text from the given file
00186  */
00187 static diagram_type *
00188 pCreateTextWindow(const char *szFilename)
00189 {
00190         diagram_type    *pDiag;
00191 
00192         TRACE_MSG("pCreateTextWindow");
00193 
00194         fail(szFilename == NULL || szFilename[0] == '\0');
00195 
00196         /* Create the diagram */
00197         pDiag = pCreateDiagram(szTask+1, szFilename);
00198         if (pDiag == NULL) {
00199                 werr(0, "Sorry, no new diagram object");
00200                 return NULL;
00201         }
00202 
00203         /* Prepare a save menu for this diagram */
00204         pDiag->pSaveMenu = Menu_New(szTask+1,
00205                 ">Scale view,"
00206                 ">Save (Drawfile)   F3,"
00207                 ">Save (Text only) \213F3");
00208         if (pDiag->pSaveMenu == NULL) {
00209                 werr(1, "Sorry, no Savemenu object");
00210         }
00211         Menu_Warn(pDiag->pSaveMenu, SAVEMENU_SCALEVIEW,
00212                                         TRUE, bScaleOpenAction, pDiag);
00213         Menu_Warn(pDiag->pSaveMenu, SAVEMENU_SAVEDRAW,
00214                                         TRUE, bSaveDrawfile, pDiag);
00215         Menu_Warn(pDiag->pSaveMenu, SAVEMENU_SAVETEXT,
00216                                         TRUE, bSaveTextfile, pDiag);
00217 
00218         /* Claim events for the main window */
00219         Event_Claim(event_REDRAW, pDiag->tMainWindow, icon_ANY,
00220                                         bRedrawMainWindow, pDiag);
00221         Event_Claim(event_CLOSE, pDiag->tMainWindow, icon_ANY,
00222                                         bDestroyDiagram, pDiag);
00223         Event_Claim(event_CLICK, pDiag->tMainWindow, icon_ANY,
00224                                         bMouseButtonClick, pDiag);
00225         Event_Claim(event_KEY, pDiag->tMainWindow, icon_ANY,
00226                                         bMainKeyPressed, pDiag);
00227 
00228         /* Claim events for the scale window */
00229         Event_Claim(event_REDRAW, pDiag->tScaleWindow, icon_ANY,
00230                                         bAutoRedrawWindow, NULL);
00231         Event_Claim(event_CLICK, pDiag->tScaleWindow, icon_ANY,
00232                                         bMouseButtonClick, pDiag);
00233         Event_Claim(event_KEY, pDiag->tScaleWindow, icon_ANY,
00234                                         bScaleKeyPressed, pDiag);
00235 
00236         /* Set the window title */
00237         vSetTitle(pDiag);
00238         return pDiag;
00239 } /* end of pCreateTextWindow */
00240 
00241 /*
00242  * vProcessFile - process one file
00243  */
00244 static void
00245 vProcessFile(const char *szFilename, int iFiletype)
00246 {
00247         options_type    tOptions;
00248         FILE            *pFile;
00249         diagram_type    *pDiag;
00250         long            lFilesize;
00251         int             iWordVersion;
00252 
00253         TRACE_MSG("vProcessFile");
00254 
00255         fail(szFilename == NULL || szFilename[0] == '\0');
00256 
00257         DBG_MSG(szFilename);
00258 
00259         pFile = fopen(szFilename, "rb");
00260         if (pFile == NULL) {
00261                 werr(0, "I can't open '%s' for reading", szFilename);
00262                 return;
00263         }
00264 
00265         lFilesize = lGetFilesize(szFilename);
00266         if (lFilesize < 0) {
00267                 (void)fclose(pFile);
00268                 werr(0, "I can't get the size of '%s'", szFilename);
00269                 return;
00270         }
00271 
00272         iWordVersion = iGuessVersionNumber(pFile, lFilesize);
00273         if (iWordVersion < 0 || iWordVersion == 3) {
00274                 if (bIsRtfFile(pFile)) {
00275                         werr(0, "%s is not a Word Document."
00276                                 " It is probably a Rich Text Format file",
00277                                 szFilename);
00278                 } if (bIsWordPerfectFile(pFile)) {
00279                         werr(0, "%s is not a Word Document."
00280                                 " It is probably a Word Perfect file",
00281                                 szFilename);
00282                 } else {
00283                         werr(0, "%s is not a Word Document.", szFilename);
00284                 }
00285                 (void)fclose(pFile);
00286                 return;
00287         }
00288         /* Reset any reading done during file-testing */
00289         rewind(pFile);
00290 
00291         if (iFiletype != FILETYPE_MSWORD) {
00292                 vGetOptions(&tOptions);
00293                 if (tOptions.bAutofiletypeAllowed) {
00294                         vSetFiletype(szFilename, FILETYPE_MSWORD);
00295                 }
00296         }
00297 
00298         pDiag = pCreateTextWindow(szFilename);
00299         if (pDiag == NULL) {
00300                 (void)fclose(pFile);
00301                 return;
00302         }
00303 
00304         (void)bWordDecryptor(pFile, lFilesize, pDiag);
00305         Error_CheckFatal(Drawfile_VerifyDiagram(&pDiag->tInfo));
00306         vShowDiagram(pDiag);
00307         TRACE_MSG("After vShowDiagram");
00308 
00309         TRACE_MSG("before debug print");
00310         DBG_HEX(pFile);
00311         TRACE_MSG("before fclose");
00312         (void)fclose(pFile);
00313         TRACE_MSG("after fclose");
00314 } /* end of vProcessFile */
00315 
00316 /*
00317  * vSendAck - send an acknowledge
00318  */
00319 static void
00320 vSendAck(event_pollblock *pEvent)
00321 {
00322         message_block   tMessage;
00323 
00324         TRACE_MSG("vSendAck");
00325 
00326         fail(pEvent == NULL);
00327         fail(pEvent->type != event_SEND && pEvent->type != event_SENDWANTACK);
00328         fail(pEvent->data.message.header.action != message_DATALOAD &&
00329                 pEvent->data.message.header.action != message_DATAOPEN);
00330 
00331         tMessage.header.action = message_DATALOADACK;
00332         tMessage.header.size = sizeof(tMessage);
00333         tMessage.header.yourref = pEvent->data.message.header.myref;
00334         Error_CheckFatal(Wimp_SendMessage(event_SEND, &tMessage,
00335                                 pEvent->data.message.header.sender, 0));
00336 } /* end of vSendAck */
00337 
00338 static BOOL
00339 bEventMsgHandler(event_pollblock *pEvent, void *pvReference)
00340 {
00341         TRACE_MSG("bEventMsgHandler");
00342 
00343         fail(pEvent == NULL);
00344 
00345         switch (pEvent->type) {
00346         case event_SEND:
00347         case event_SENDWANTACK:
00348                 switch (pEvent->data.message.header.action) {
00349                 case message_CLOSEDOWN:
00350                         exit(EXIT_SUCCESS);
00351                         break;
00352                 case message_DATALOAD:
00353                 case message_DATAOPEN:
00354                         vProcessFile(
00355                                 pEvent->data.message.data.dataload.filename,
00356                                 pEvent->data.message.data.dataload.filetype);
00357                         vSendAck(pEvent);
00358                         break;
00359                 default:
00360                         DBG_DEC(pEvent->data.message.header.action);
00361                         break;
00362                 }
00363                 return TRUE;
00364         default:
00365                 DBG_DEC(pEvent->type);
00366                 return FALSE;
00367         }
00368 } /* end of bEventMsgHandler */
00369 
00370 /*
00371  * bMenuSelect - select from the iconbar menu
00372  */
00373 static BOOL
00374 bMenuSelect(event_pollblock *pEvent, void *pvReference)
00375 {
00376         TRACE_MSG("bMenuSelect");
00377 
00378         fail(pEvent == NULL);
00379         fail(pEvent->type != event_MENU);
00380 
00381         DBG_DEC(pEvent->data.selection[0]);
00382 
00383         switch (pEvent->data.selection[0]) {
00384         case ICONBAR_INFO_FIELD:
00385                 return bBarInfo(pEvent, pvReference);
00386         case ICONBAR_CHOICES_FIELD:
00387                 vChoicesOpenAction(tChoicesWindow);
00388                 Window_BringToFront(tChoicesWindow);
00389                 break;
00390         case ICONBAR_QUIT_FIELD:
00391                 TRACE_MSG("before exit");
00392                 exit(EXIT_SUCCESS);
00393                 break;
00394         default:
00395                 DBG_DEC(pEvent->data.selection[0]);
00396                 break;
00397         }
00398         return TRUE;
00399 } /* end of bMenuSelect */
00400 
00401 /*
00402  * bMenuClick - respond to an menu click
00403  */
00404 static BOOL
00405 bMenuClick(event_pollblock *pEvent, void *pvReference)
00406 {
00407         TRACE_MSG("bMenuClick");
00408 
00409         fail(pEvent == NULL);
00410         fail(pEvent->type != event_MENU);
00411 
00412         if (menu_currentopen == tDummyDiagram.pSaveMenu) {
00413                 return bMenuSelect(pEvent, pvReference);
00414         } else if (pvReference == NULL) {
00415                 return FALSE;
00416         }
00417         return bSaveSelect(pEvent, pvReference);
00418 } /* end of bMenuClick */
00419 
00420 static void
00421 vTemplates(void)
00422 {
00423         TRACE_MSG("vTemplates");
00424 
00425         Template_Initialise();
00426         Template_LoadFile("Templates");
00427 
00428         tChoicesWindow = Window_Create("Choices", template_TITLEMIN);
00429         if (tChoicesWindow == 0) {
00430                 werr(1, "I can't find the 'Choices' template");
00431         }
00432 
00433         /* Claim events for the choices window */
00434         Event_Claim(event_REDRAW, tChoicesWindow, icon_ANY,
00435                                         bAutoRedrawWindow, NULL);
00436         Event_Claim(event_CLICK, tChoicesWindow, icon_ANY,
00437                                         bChoicesMouseClick, NULL);
00438         Event_Claim(event_KEY, tChoicesWindow, icon_ANY,
00439                                         bChoicesKeyPressed, NULL);
00440 } /* end of vTemplates */
00441 
00442 static void
00443 vInitialise(void)
00444 {
00445         int     aiMessages[] = {0};
00446         icon_handle     tBarIcon;
00447 
00448 
00449         TRACE_MSG("vInitialise");
00450 
00451         Resource_Initialise(szTask+1);
00452         Event_Initialise3(szTask+1, 310, aiMessages);
00453         EventMsg_Initialise();
00454         Screen_CacheModeInfo();
00455 #if defined(__GNUC__)
00456         flex_init(szTask+1, 0, 0);
00457         flex_set_budge(1);
00458 #endif /* __GNUC__ */
00459         vTemplates();
00460 
00461         /* Prepare iconbar menu */
00462         tDummyDiagram.tInfo.data = NULL;
00463         tDummyDiagram.tInfo.length = 0;
00464         tDummyDiagram.pSaveMenu = Menu_New(szTask+1, ">Info,Choices...,Quit");
00465         if (tDummyDiagram.pSaveMenu == NULL) {
00466                 werr(1, "Sorry, no Barmenu object");
00467         }
00468         pInfoBox = Dialog2_CreateDialogBlock("ProgInfo", -1, -1,
00469                                         vBarInfoSetText, NULL, NULL);
00470 
00471         if (pInfoBox == NULL) {
00472                 werr(1, "Sorry, no Infobox object");
00473         }
00474         Menu_Warn(tDummyDiagram.pSaveMenu, ICONBAR_INFO_FIELD,
00475                                         TRUE, bBarInfo, &tDummyDiagram);
00476 
00477         /* Create an icon on the icon bar */
00478         tBarIcon = Icon_BarIcon(szTask, iconbar_RIGHT);
00479         Event_Claim(event_CLICK, window_ICONBAR, tBarIcon,
00480                                         bMouseButtonClick, &tDummyDiagram);
00481 
00482         /* Generic claims */
00483         Event_Claim(event_OPEN, window_ANY, icon_ANY,
00484                                         Handler_OpenWindow, NULL);
00485         Event_Claim(event_CLOSE, window_ANY, icon_ANY,
00486                                         Handler_CloseWindow, NULL);
00487         Event_Claim(event_MENU, window_ANY, icon_ANY,
00488                                         bMenuClick, NULL);
00489         EventMsg_Claim(message_DATALOAD, window_ICONBAR,
00490                                         bEventMsgHandler, NULL);
00491         EventMsg_Claim(message_MODECHANGE, window_ANY,
00492                                         Handler_ModeChange, NULL);
00493 } /* end of vInitialise */
00494 
00495 int
00496 main(int argc, char **argv)
00497 {
00498         int     iFirst, iFiletype;
00499 
00500         TRACE_MSG("main");
00501 
00502         vInitialise();
00503         iFirst = iReadOptions(argc, argv);
00504         if (iFirst != 1) {
00505                 return EXIT_FAILURE;
00506         }
00507 
00508         if (argc > 1) {
00509                 iFiletype = iGetFiletype(argv[1]);
00510                 if (iFiletype < 0) {
00511                         return EXIT_FAILURE;
00512                 }
00513                 vProcessFile(argv[1], iFiletype);
00514                 TRACE_MSG("main after vProcessFile");
00515         }
00516 
00517         for (;;) {
00518                 Event_Poll();
00519         }
00520 } /* end of main */

Generated by  doxygen 1.6.2