1 /* AbiWord
2 * Copyright (C) 2002 Dom Lachowicz and others
3 * Copyright (C) 2004, 2009 Hubert Figuiere
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * 02110-1301 USA.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <stdio.h>
26
27 #include "ev_EditMethod.h"
28 #include "ap_Features.h"
29 #include "ap_App.h"
30 #include "ap_Args.h"
31 #include "ap_Prefs_SchemeIds.h"
32 #include "ap_Strings.h"
33 #include "xap_Frame.h"
34 #include "xap_ModuleManager.h"
35 #include "pd_Document.h"
36 #include "ie_imp.h"
37
38 #if defined(TOOLKIT_WIN)
39
40 #include "ut_Win32LocaleString.h"
41 #include "ap_Win32App.h" //needed for AP_Win32App::s_fromWinLocaleToUTF8()
42
AP_App(HINSTANCE hInstance,const char * szAppName)43 AP_App::AP_App (HINSTANCE hInstance, const char * szAppName)
44 : XAP_App_BaseClass ( hInstance, szAppName )
45 #else
46 AP_App::AP_App (const char * szAppName)
47 : XAP_App_BaseClass ( szAppName )
48 #endif
49 {
50 }
51
~AP_App()52 AP_App::~AP_App ()
53 {
54 }
55
56 /*!
57 * Open windows requested on commandline.
58 *
59 * \return False if an unknown command line option was used, true
60 * otherwise.
61 */
openCmdLineFiles(const AP_Args * args)62 bool AP_App::openCmdLineFiles(const AP_Args * args)
63 {
64 int kWindowsOpened = 0;
65 const char *file = NULL;
66
67 if (AP_Args::m_sFiles == NULL) {
68 // no files to open, this is ok
69 XAP_Frame * pFrame = newFrame();
70 pFrame->loadDocument((const char *)NULL, IEFT_Unknown);
71 return true;
72 }
73
74 int i = 0;
75 while ((file = AP_Args::m_sFiles[i++]) != NULL) {
76 char * uri = NULL;
77
78 uri = UT_go_shell_arg_to_uri (file);
79
80 XAP_Frame * pFrame = newFrame();
81
82 UT_Error error = pFrame->loadDocument (uri, IEFT_Unknown, true);
83 g_free (uri);
84
85 if (UT_IS_IE_SUCCESS(error))
86 {
87 kWindowsOpened++;
88 if (error == UT_IE_TRY_RECOVER) {
89 pFrame->showMessageBox(AP_STRING_ID_MSG_OpenRecovered,
90 XAP_Dialog_MessageBox::b_O,
91 XAP_Dialog_MessageBox::a_OK);
92 }
93 }
94 else
95 {
96 // TODO we crash if we just delete this without putting something
97 // TODO in it, so let's go ahead and open an untitled document
98 // TODO for now. this would cause us to get 2 untitled documents
99 // TODO if the user gave us 2 bogus pathnames....
100
101 // Because of the incremental loader, we should not crash anymore;
102 // I've got other things to do now though.
103 kWindowsOpened++;
104 pFrame->loadDocument((const char *)NULL, IEFT_Unknown);
105 pFrame->raise();
106
107 errorMsgBadFile (pFrame, file, error);
108 }
109
110 if (args->m_sMerge) {
111 PD_Document * pDoc = static_cast<PD_Document*>(pFrame->getCurrentDoc());
112 pDoc->setMailMergeLink(args->m_sMerge);
113 }
114 }
115
116 if (kWindowsOpened == 0)
117 {
118 // no documents specified or openable, open an untitled one
119
120 XAP_Frame * pFrame = newFrame();
121 pFrame->loadDocument((const char *)NULL, IEFT_Unknown);
122 if (args->m_sMerge) {
123 PD_Document * pDoc = static_cast<PD_Document*>(pFrame->getCurrentDoc());
124 pDoc->setMailMergeLink(args->m_sMerge);
125 }
126 }
127
128 return true;
129 }
130
openCmdLinePlugins(const AP_Args * Args,bool & bSuccess)131 bool AP_App::openCmdLinePlugins(const AP_Args * Args, bool &bSuccess)
132 {
133 if(Args->m_sPluginArgs)
134 {
135 //
136 // Start a plugin rather than the main abiword application.
137 //
138 const char * szName = NULL;
139 XAP_Module * pModule = NULL;
140 const char * szRequest = NULL;
141 bool bFound = false;
142 if(Args->m_sPluginArgs[0])
143 {
144 szRequest = Args->m_sPluginArgs[0];
145 const UT_GenericVector<XAP_Module*> * pVec = XAP_ModuleManager::instance().enumModules ();
146 UT_DEBUGMSG((" %d plugins loaded \n",pVec->getItemCount()));
147 for (UT_sint32 i = 0; (i < pVec->size()) && !bFound; i++)
148 {
149 pModule = pVec->getNthItem (i);
150 szName = pModule->getModuleInfo()->name;
151 UT_DEBUGMSG(("%s\n", szName));
152 if(strcmp(szName,szRequest) == 0)
153 {
154 bFound = true;
155 }
156 }
157 }
158 if(!bFound)
159 {
160 fprintf(stderr, "Plugin %s not found or loaded \n",szRequest);
161 bSuccess = false;
162 return false;
163 }
164 //
165 // You must put the name of the ev_EditMethod in the usage field
166 // of the plugin registered information.
167 //
168 const char * evExecute = pModule->getModuleInfo()->usage;
169 EV_EditMethodContainer* pEMC = Args->getApp()->getEditMethodContainer();
170 const EV_EditMethod * pInvoke = pEMC->findEditMethodByName(evExecute);
171 if(!pInvoke)
172 {
173 fprintf(stderr, "Plugin %s invoke method %s not found \n",
174 Args->m_sPluginArgs[0],evExecute);
175 bSuccess = false;
176 return false;
177 }
178 //
179 // Execute the plugin, then quit
180 //
181 UT_String *sCommandLine = Args->getPluginOptions();
182 ev_EditMethod_invoke(pInvoke, *sCommandLine);
183 delete sCommandLine;
184 return false;
185 }
186 return true;
187 }
188
189
initialize(void)190 bool AP_App::initialize(void)
191 {
192 return XAP_App_BaseClass::initialize(AP_PREF_KEY_KeyBindings,AP_PREF_DEFAULT_KeyBindings);
193 }
194
errorMsgBadArg(AP_Args *,int)195 void AP_App::errorMsgBadArg (AP_Args *, int)
196 {
197 UT_ASSERT_HARMLESS(UT_SHOULD_NOT_HAPPEN);
198 }
199
errorMsgBadFile(XAP_Frame *,const char *,UT_Error)200 void AP_App::errorMsgBadFile(XAP_Frame *, const char *, UT_Error)
201 {
202 UT_ASSERT_HARMLESS(UT_SHOULD_NOT_HAPPEN);
203 }
204
doWindowlessArgs(const AP_Args *,bool &)205 bool AP_App::doWindowlessArgs (const AP_Args *, bool & /*bSuccess*/)
206 {
207 return false;
208 }
209
210