Debian Codegraph @

src/af/xap/cocoa/xap_CocoaDlg_PluginManager.cpp

raw
1 /* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */
2
3 /* AbiSource Application Framework
4 * Copyright (C) 2001 AbiSource, Inc.
5 * Copyright (C) 2003, 2007 Hubert Figuiere
6 * Copyright (C) 2004 Francis James Franklin
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301 USA.
22 */
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27
28 #include "ut_assert.h"
29 #include "ut_debugmsg.h"
30 #include "ut_string.h"
31
32 #include "xap_CocoaApp.h"
33 #include "xap_CocoaAppController.h"
34 #include "xap_CocoaDialog_Utilities.h"
35 #include "xap_CocoaDlg_FileOpenSaveAs.h"
36 #include "xap_CocoaDlg_PluginManager.h"
37 #include "xap_CocoaFrame.h"
38 #include "xap_CocoaModule.h"
39 #include "xap_CocoaPlugin.h"
40 #include "xap_Dialog_Id.h"
41 #include "xap_Module.h"
42 #include "xap_ModuleManager.h"
43
44 #include "ie_types.h"
45
46 XAP_Dialog * XAP_CocoaDialog_PluginManager::static_constructor(XAP_DialogFactory * pFactory,
47 XAP_Dialog_Id dlgid)
48 {
49 XAP_CocoaDialog_PluginManager * p = new XAP_CocoaDialog_PluginManager(pFactory, dlgid);
50 return p;
51 }
52
53 XAP_CocoaDialog_PluginManager::XAP_CocoaDialog_PluginManager(XAP_DialogFactory * pDlgFactory,
54 XAP_Dialog_Id dlgid)
55 : XAP_Dialog_PluginManager(pDlgFactory, dlgid),
56 m_pFrame(NULL)
57 {
58 //
59 }
60
61 XAP_CocoaDialog_PluginManager::~XAP_CocoaDialog_PluginManager(void)
62 {
63 //
64 }
65
66 void XAP_CocoaDialog_PluginManager::event_Load ()
67 {
68 const XAP_StringSet * pSS = m_pApp->getStringSet();
69
70 XAP_DialogFactory * pDialogFactory = (XAP_DialogFactory *) m_pFrame->getDialogFactory();
71
72 XAP_Dialog_FileOpenSaveAs * pDialog = (XAP_Dialog_FileOpenSaveAs *) (pDialogFactory->requestDialog(XAP_DIALOG_ID_FILE_OPEN));
73 UT_ASSERT(pDialog);
74 if (!pDialog)
75 return;
76
77 std::string plugin_path = [[[NSBundle mainBundle] bundlePath] UTF8String];
78 // plugin_path += "/Contents/PlugIns/AbiHack.dylib";
79
80 pDialog->setCurrentPathname(plugin_path.c_str ());
81 pDialog->setSuggestFilename(false);
82
83 const char * szDescList[2];
84 const char * szSuffixList[2];
85 IEFileType nTypeList[2];
86
87 szDescList[0] = "AbiWord Plugin (.so)";
88 szSuffixList[0] = "*.so";
89 nTypeList[0] = (IEFileType) 1;
90
91 szDescList[1] = 0;
92 szSuffixList[1] = 0;
93 nTypeList[1] = 0;
94
95 pDialog->setFileTypeList(szDescList, szSuffixList, (const UT_sint32 *) nTypeList);
96
97 pDialog->setDefaultFileType((IEFileType) 1);
98
99 // todo: cd to the proper plugin directory
100
101 pDialog->runModal(m_pFrame);
102
103 if (pDialog->getAnswer() == XAP_Dialog_FileOpenSaveAs::a_OK)
104 {
105 const char * szResultPathname = pDialog->getPathname();
106
107 if (szResultPathname && *szResultPathname)
108 {
109 // bool bIsBundle = false;
110
111 // int length = strlen(szResultPathname);
112 // if (length > 4)
113 // if (strcmp(szResultPathname + length - 4, ".Abi") == 0)
114 // bIsBundle = true;
115
116 bool bActivated = activatePlugin(szResultPathname);
117 // if (bIsBundle)
118 // {
119 // XAP_CocoaAppController * pController = (XAP_CocoaAppController *) [NSApp delegate];
120 // XAP_CocoaPlugin * plugin = [pController loadPlugin:[NSString stringWithUTF8String:szResultPathname]];
121 // [[plugin delegate] pluginActivate];
122 // bActivated = true; // we don't really know if activation succeeded....
123 // }
124 // else bActivated = activatePlugin(szResultPathname);
125
126 if (!bActivated)
127 {
128 /* error message
129 */
130 m_pFrame->showMessageBox (pSS->getValue(XAP_STRING_ID_DLG_PLUGIN_MANAGER_COULDNT_LOAD),
131 XAP_Dialog_MessageBox::b_O,
132 XAP_Dialog_MessageBox::a_OK);
133 }
134 }
135 }
136 pDialogFactory->releaseDialog(pDialog);
137 }
138
139 void XAP_CocoaDialog_PluginManager::runModal(XAP_Frame * pFrame)
140 {
141 UT_ASSERT(pFrame);
142 m_pFrame = pFrame;
143
144 XAP_CocoaDlg_PluginManagerController * pDialog = [[XAP_CocoaDlg_PluginManagerController alloc] initFromNib];
145 if (pDialog)
146 {
147 [pDialog setXAPOwner:this];
148
149 NSWindow * window = [pDialog window];
150
151 [NSApp runModalForWindow:window];
152
153 [pDialog close];
154 [pDialog release];
155 }
156 }
157
158 /*****************************************************************/
159 /*****************************************************************/
160
161 @implementation XAP_CocoaPluginReference
162
163 - (id)initWithModule:(XAP_CocoaModule *)module
164 {
165 if (![super init]) {
166 return nil;
167 }
168 m_entry = 0;
169
170 m_name = 0;
171 m_author = 0;
172 m_version = 0;
173 m_description = 0;
174 m_usage = 0;
175
176 m_module = module;
177 m_plugin = 0;
178
179 if (!m_module) {
180 [self release];
181 return nil;
182 }
183 const XAP_ModuleInfo * mi = m_module->getModuleInfo();
184
185 if (mi->name)
186 m_name = [NSString stringWithUTF8String:(mi->name)];
187 if (mi->author)
188 m_author = [NSString stringWithUTF8String:(mi->author)];
189 if (mi->version)
190 m_version = [NSString stringWithUTF8String:(mi->version)];
191 if (mi->desc)
192 m_description = [NSString stringWithUTF8String:(mi->desc)];
193 if (mi->usage)
194 m_usage = [NSString stringWithUTF8String:(mi->usage)];
195
196 if (m_name == 0)
197 m_name = @"";
198 if (m_author == 0)
199 m_author = @"";
200 if (m_version == 0)
201 m_version = @"";
202 if (m_description == 0)
203 m_description = @"";
204 if (m_usage == 0)
205 m_usage = @"";
206
207 [m_name retain];
208 [m_author retain];
209 [m_version retain];
210 [m_description retain];
211 [m_usage retain];
212
213 m_entry = [[NSAttributedString alloc] initWithString:m_name];
214 return self;
215 }
216
217 - (id)initWithPlugin:(XAP_CocoaPlugin *)plugin
218 {
219 if (![super init]) {
220 return nil;
221 }
222 m_entry = 0;
223
224 m_name = 0;
225 m_author = 0;
226 m_version = 0;
227 m_description = 0;
228 m_usage = 0;
229
230 m_module = 0;
231 m_plugin = plugin;
232
233 if (!m_plugin)
234 {
235 [self release];
236 return nil;
237 }
238 else if (![m_plugin delegate])
239 {
240 [self release];
241 return nil;
242 }
243
244 id <XAP_CocoaPluginDelegate> delegate = [m_plugin delegate];
245
246 m_name = [delegate pluginName];
247 m_author = [delegate pluginAuthor];
248 m_version = [delegate pluginVersion];
249 m_description = [delegate pluginDescription];
250 m_usage = [delegate pluginUsage];
251
252 if (m_name == 0)
253 m_name = @"";
254 if (m_author == 0)
255 m_author = @"";
256 if (m_version == 0)
257 m_version = @"";
258 if (m_description == 0)
259 m_description = @"";
260 if (m_usage == 0)
261 m_usage = @"";
262
263 [m_name retain];
264 [m_author retain];
265 [m_version retain];
266 [m_description retain];
267 [m_usage retain];
268
269 NSDictionary * attr = 0;
270
271 if ([delegate pluginIsActive])
272 attr = [NSDictionary dictionaryWithObject:[NSColor blackColor] forKey:NSForegroundColorAttributeName];
273 else
274 attr = [NSDictionary dictionaryWithObject:[NSColor grayColor] forKey:NSForegroundColorAttributeName];
275
276 m_entry = [[NSAttributedString alloc] initWithString:m_name attributes:attr];
277 return self;
278 }
279
280 - (void)dealloc
281 {
282 [m_name release];
283 [m_author release];
284 [m_version release];
285 [m_description release];
286 [m_usage release];
287 [m_entry release];
288 [super dealloc];
289 }
290
291 - (void)setActive:(BOOL)active
292 {
293 [m_entry release];
294 m_entry = nil;
295
296 NSDictionary * attr = 0;
297
298 attr = [NSDictionary dictionaryWithObject:(active ? [NSColor blackColor] : [NSColor grayColor]) forKey:NSForegroundColorAttributeName];
299
300 m_entry = [[NSAttributedString alloc] initWithString:m_name attributes:attr];
301 }
302
303 - (NSAttributedString *)entry
304 {
305 return m_entry;
306 }
307
308 - (NSString *)name
309 {
310 return m_name;
311 }
312
313 - (NSString *)author
314 {
315 return m_author;
316 }
317
318 - (NSString *)version
319 {
320 return m_version;
321 }
322
323 - (NSString *)description
324 {
325 return m_description;
326 }
327
328 - (NSString *)usage
329 {
330 return m_usage;
331 }
332
333 - (XAP_CocoaModule *)module
334 {
335 return m_module;
336 }
337
338 - (XAP_CocoaPlugin *)plugin
339 {
340 return m_plugin;
341 }
342
343 @end
344
345 @implementation XAP_CocoaDlg_PluginManagerController
346
347 - (id)initFromNib
348 {
349 if (![super initWithWindowNibName:@"xap_CocoaDlg_PluginManager"]) {
350 return nil;
351 }
352 m_PluginRefs = [[NSMutableArray alloc] initWithCapacity:16];
353 return self;
354 }
355
356 - (void)dealloc
357 {
358 [m_PluginRefs release];
359 [super dealloc];
360 }
361
362 - (void)setXAPOwner:(XAP_Dialog*)owner
363 {
364 _xap = dynamic_cast<XAP_CocoaDialog_PluginManager*>(owner);
365 UT_ASSERT(_xap);
366 }
367
368 - (void)discardXAP
369 {
370 _xap = nil;
371 }
372
373 - (void)reloadPluginList
374 {
375 [m_PluginRefs removeAllObjects];
376
377 const UT_GenericVector<XAP_Module*> * pModuleVec = XAP_ModuleManager::instance().enumModules();
378
379 UT_uint32 count = pModuleVec->getItemCount();
380
381 for (UT_uint32 i = 0; i < count; i++) {
382 if (XAP_CocoaModule * pModule = static_cast<XAP_CocoaModule *>(pModuleVec->getNthItem(i))) {
383 XAP_CocoaPluginReference * pRef = [[XAP_CocoaPluginReference alloc] initWithModule:pModule];
384 if (pRef)
385 {
386 [m_PluginRefs addObject:pRef];
387 [pRef release];
388 }
389 }
390 }
391
392 XAP_CocoaAppController * pController = (XAP_CocoaAppController *) [NSApp delegate];
393
394 NSArray * Plugins = [pController plugins];
395
396 unsigned plugin_count = [Plugins count];
397
398 for (unsigned i = 0; i < plugin_count; i++)
399 {
400 XAP_CocoaPlugin * pPlugin = (XAP_CocoaPlugin *) [Plugins objectAtIndex:i];
401 XAP_CocoaPluginReference * pRef = [[XAP_CocoaPluginReference alloc] initWithPlugin:pPlugin];
402 if (pRef)
403 {
404 [m_PluginRefs addObject:pRef];
405 [pRef release];
406 }
407 }
408 }
409
410 - (void)windowDidLoad
411 {
412 const XAP_StringSet * pSS = XAP_App::getApp()->getStringSet();
413
414 LocalizeControl([self window], pSS, XAP_STRING_ID_DLG_PLUGIN_MANAGER_TITLE);
415
416 LocalizeControl(oInstallBtn, pSS, XAP_STRING_ID_DLG_PLUGIN_MANAGER_INSTALL);
417
418 LocalizeControl(oCloseBtn, pSS, XAP_STRING_ID_DLG_Close);
419
420 LocalizeControl(oNameLabel, pSS, XAP_STRING_ID_DLG_PLUGIN_MANAGER_NAME);
421 LocalizeControl(oAuthorLabel, pSS, XAP_STRING_ID_DLG_PLUGIN_MANAGER_AUTHOR);
422 LocalizeControl(oVersionLabel, pSS, XAP_STRING_ID_DLG_PLUGIN_MANAGER_VERSION);
423 LocalizeControl(oDescriptionLabel, pSS, XAP_STRING_ID_DLG_PLUGIN_MANAGER_DESC);
424 // LocalizeControl(oUsageLabel, pSS, XAP_STRING_ID_DLG_PLUGIN_MANAGER_ ?? );
425
426 [self reloadPluginList];
427
428 [oPluginList setDelegate:self];
429 [oPluginList setDataSource:self];
430
431 [oNameData setStringValue:@""];
432 [oAuthorData setStringValue:@""];
433 [oVersionData setStringValue:@""];
434 [oDescriptionData setStringValue:@""];
435 [oUsageData setStringValue:@""];
436
437 if ([m_PluginRefs count])
438 {
439 [oPluginList selectRowIndexes:[NSIndexSet indexSetWithIndex:0] byExtendingSelection:NO];
440 [self tableViewSelectionDidChange:nil];
441 }
442 }
443
444 - (IBAction)closeAction:(id)sender
445 {
446 UT_UNUSED(sender);
447 [NSApp stopModal];
448 }
449
450 - (IBAction)installAction:(id)sender
451 {
452 UT_UNUSED(sender);
453 _xap->event_Load();
454
455 [self reloadPluginList];
456
457 [oPluginList deselectAll:self];
458 [oPluginList reloadData];
459 }
460
461 /* NSTableView delegate method
462 */
463 - (void)tableViewSelectionDidChange:(NSNotification *)aNotification
464 {
465 UT_UNUSED(aNotification);
466 int selection = [oPluginList selectedRow];
467 if (selection >= 0)
468 {
469 XAP_CocoaPluginReference * pRef = (XAP_CocoaPluginReference *) [m_PluginRefs objectAtIndex:selection];
470
471 [oNameData setStringValue:[pRef name ]];
472 [oAuthorData setStringValue:[pRef author ]];
473 [oVersionData setStringValue:[pRef version ]];
474 [oDescriptionData setStringValue:[pRef description]];
475 [oUsageData setStringValue:[pRef usage ]];
476 }
477 else
478 {
479 [oNameData setStringValue:@""];
480 [oAuthorData setStringValue:@""];
481 [oVersionData setStringValue:@""];
482 [oDescriptionData setStringValue:@""];
483 [oUsageData setStringValue:@""];
484 }
485 }
486
487 /* NSTableDataSource methods
488 */
489 - (int)numberOfRowsInTableView:(NSTableView *)aTableView
490 {
491 UT_UNUSED(aTableView);
492 return (int) [m_PluginRefs count];
493 }
494
495 - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
496 {
497 UT_UNUSED(aTableView);
498 UT_UNUSED(aTableColumn);
499 XAP_CocoaPluginReference * pRef = (XAP_CocoaPluginReference *) [m_PluginRefs objectAtIndex:rowIndex];
500
501 return [pRef entry];
502 }
503
504 @end