Debian Codegraph @

src/af/xap/gtk/xap_UnixDlg_PluginManager.cpp

raw
1 /* AbiSource Application Framework
2 * Copyright (C) 2003 Marc Maurer
3 * Copyright (C) 2001-2003 AbiSource, Inc.
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 #include <string.h>
27 #include <stdlib.h>
28 #include <gtk/gtk.h>
29 #include "ut_assert.h"
30 #include "ut_string.h"
31 #include "xap_UnixDialogHelper.h"
32 #include "xap_UnixApp.h"
33 #include "xap_Frame.h"
34 #include "xap_UnixFrameImpl.h"
35
36 #include "xap_Dialog_Id.h"
37 #include "xap_UnixDlg_PluginManager.h"
38 #include "xap_UnixDlg_FileOpenSaveAs.h"
39
40 #include "xap_Module.h"
41 #include "xap_ModuleManager.h"
42
43 #include "ie_types.h"
44 #include "ut_string_class.h"
45
46 /*****************************************************************/
47
48 static void _errorMessage (XAP_Frame * pFrame, XAP_String_Id id)
49 {
50 // just a little simple error message box
51 UT_return_if_fail(pFrame);
52 pFrame->showMessageBox (id,
53 XAP_Dialog_MessageBox::b_O,
54 XAP_Dialog_MessageBox::a_OK);
55 }
56
57
58 /*****************************************************************/
59
60 XAP_Dialog * XAP_UnixDialog_PluginManager::static_constructor(XAP_DialogFactory * pFactory,
61 XAP_Dialog_Id id)
62 {
63 return new XAP_UnixDialog_PluginManager(pFactory,id);
64 }
65
66 XAP_UnixDialog_PluginManager::XAP_UnixDialog_PluginManager(XAP_DialogFactory * pDlgFactory,
67 XAP_Dialog_Id id)
68 : XAP_Dialog_PluginManager(pDlgFactory,id)
69 {
70 }
71
72 XAP_UnixDialog_PluginManager::~XAP_UnixDialog_PluginManager(void)
73 {
74 }
75
76 /*****************************************************************/
77
78 void XAP_UnixDialog_PluginManager::event_DeactivateAll ()
79 {
80 // deactivate all plugins in the Plugin Manager
81 deactivateAllPlugins ();
82
83 // remove all plugins from the TreeView
84 GtkTreeModel * model;
85 model = gtk_tree_view_get_model(GTK_TREE_VIEW(m_list));
86 gtk_list_store_clear(GTK_LIST_STORE (model));
87 }
88
89 void XAP_UnixDialog_PluginManager::event_Deactivate ()
90 {
91 XAP_Module * pModule = 0;
92
93 GtkTreeSelection * selection;
94 GtkTreeIter iter;
95 GtkTreeModel * model;
96
97 selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_list) );
98 if (selection && gtk_tree_selection_get_selected (selection, &model, &iter))
99 {
100 // remove the plugin from our TreeView
101 GtkTreePath *path;
102 path = gtk_tree_model_get_path (model, &iter);
103 guint rowNumber = gtk_tree_path_get_indices (path)[0];
104 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
105
106 if ((UT_sint32)rowNumber < XAP_ModuleManager::instance().enumModules()->size() - 1)
107 {
108 // select the next item in the TreeView
109 gtk_tree_view_set_cursor(GTK_TREE_VIEW(m_list),
110 path,
111 gtk_tree_view_get_column (GTK_TREE_VIEW(m_list), 0),
112 FALSE);
113 }
114 gtk_tree_path_free (path);
115
116 pModule = static_cast<XAP_Module *>(XAP_ModuleManager::instance().enumModules()->getNthItem(rowNumber));
117 if (pModule)
118 {
119 if (deactivatePlugin (pModule))
120 {
121 // worked
122 _updatePluginList ();
123 }
124 else
125 {
126 // error message box
127 _errorMessage (m_pFrame,
128 XAP_STRING_ID_DLG_PLUGIN_MANAGER_COULDNT_UNLOAD);
129 }
130 }
131 else
132 {
133 // error message box
134 _errorMessage (m_pFrame,
135 XAP_STRING_ID_DLG_PLUGIN_MANAGER_COULDNT_UNLOAD);
136 }
137 }
138 else
139 {
140 // error message box - didn't select a plugin
141 _errorMessage (m_pFrame,
142 XAP_STRING_ID_DLG_PLUGIN_MANAGER_NONE_SELECTED);
143 return;
144 }
145 }
146
147 void XAP_UnixDialog_PluginManager::event_Load ()
148 {
149 XAP_DialogFactory * pDialogFactory
150 = static_cast<XAP_DialogFactory *>(m_pFrame->getDialogFactory());
151
152 XAP_Dialog_FileOpenSaveAs * pDialog
153 = static_cast<XAP_Dialog_FileOpenSaveAs *>(pDialogFactory->requestDialog(XAP_DIALOG_ID_FILE_OPEN));
154 UT_return_if_fail(pDialog);
155
156 // set the intial plugin directory to the user-local plugin directory
157 // could also set to: XAP_App::getApp()->getAbiSuiteLibDir()/plugins
158 UT_String pluginDir (XAP_App::getApp()->getUserPrivateDirectory());
159 pluginDir += "/";
160 pluginDir += PACKAGE_NAME;
161 pluginDir += "-";
162 pluginDir += ABIWORD_SERIES;
163 pluginDir += "/plugins/";
164 pDialog->setCurrentPathname (pluginDir.c_str());
165 pDialog->setSuggestFilename(false);
166
167 UT_uint32 filterCount = 1;
168 const char ** szDescList = static_cast<const char **>(UT_calloc(filterCount + 1,
169 sizeof(char *)));
170 const char ** szSuffixList = static_cast<const char **>(UT_calloc(filterCount + 1,
171 sizeof(char *)));
172 IEFileType * nTypeList = static_cast<IEFileType *>(UT_calloc(filterCount + 1,
173 sizeof(IEFileType)));
174
175 szDescList[0] = "AbiWord Plugin (.G_MODULE_SUFFIX)";
176 szSuffixList[0] = "*.G_MODULE_SUFFIX";
177 nTypeList[0] = static_cast<IEFileType>(1);
178
179 pDialog->setFileTypeList(szDescList, szSuffixList,
180 static_cast<const UT_sint32 *>(nTypeList));
181
182 pDialog->setDefaultFileType(static_cast<IEFileType>(1));
183
184 pDialog->runModal(m_pFrame);
185
186 XAP_Dialog_FileOpenSaveAs::tAnswer ans = pDialog->getAnswer();
187 bool bOK = (ans == XAP_Dialog_FileOpenSaveAs::a_OK);
188
189 if (bOK)
190 {
191 const char * szResultPathname = pDialog->getPathname();
192 if (szResultPathname && *szResultPathname)
193 {
194 if (activatePlugin (szResultPathname))
195 {
196 // worked!
197 _updatePluginList ();
198 }
199 else
200 {
201 // error message
202 _errorMessage (m_pFrame,
203 XAP_STRING_ID_DLG_PLUGIN_MANAGER_COULDNT_LOAD);
204 }
205 }
206 }
207
208 FREEP(szDescList);
209 FREEP(szSuffixList);
210 FREEP(nTypeList);
211
212 pDialogFactory->releaseDialog(pDialog);
213 }
214
215 /*****************************************************************/
216
217 /*!
218 * Update the list of loaded plugins.
219 */
220 void XAP_UnixDialog_PluginManager::_updatePluginList ()
221 {
222 const UT_GenericVector<XAP_Module*> * pVec = XAP_ModuleManager::instance().enumModules ();
223
224 GtkTreeModel *tm = gtk_tree_view_get_model (GTK_TREE_VIEW (m_list));
225 GtkListStore *model = NULL;
226 if (tm != NULL) {
227 model = GTK_LIST_STORE (tm);
228 // detach model for faster updates
229 g_object_ref (G_OBJECT (model));
230 gtk_tree_view_set_model (GTK_TREE_VIEW (m_list), NULL);
231 gtk_list_store_clear (model);
232 }
233 else {
234 model = gtk_list_store_new (1, G_TYPE_STRING);
235 }
236
237 // build a list of all items
238 GtkTreeIter iter;
239 for (UT_sint32 i = 0; i < pVec->size(); i++) {
240
241 XAP_Module * pModule = pVec->getNthItem (i);
242 gtk_list_store_append (model, &iter);
243 gtk_list_store_set (model, &iter,
244 0, pModule->getModuleInfo()->name,
245 -1);
246 }
247
248 gtk_tree_view_set_model (GTK_TREE_VIEW (m_list), reinterpret_cast<GtkTreeModel *>(model));
249
250 if(pVec->size ())
251 _selectFirstEntry ();
252
253 g_object_unref (model);
254 }
255
256 void XAP_UnixDialog_PluginManager::_refresh ()
257 {
258 XAP_Module * pModule = 0;
259
260 GtkTreeSelection * selection;
261 GtkTreeIter iter;
262 GtkTreeModel * model;
263
264 if (XAP_ModuleManager::instance().enumModules()->size())
265 {
266 selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_list) );
267 if (selection && gtk_tree_selection_get_selected (selection, &model, &iter))
268 {
269 // get the selected module from the TreeView
270 GtkTreePath *path;
271 path = gtk_tree_model_get_path (model, &iter);
272 gint rowNumber = gtk_tree_path_get_indices (path)[0];
273
274 pModule = static_cast<XAP_Module *>(XAP_ModuleManager::instance().enumModules()->getNthItem(rowNumber));
275 gtk_tree_path_free(path);
276 }
277 }
278
279 // just a blank space, to represent an empty entry
280 const char * name = NULL;
281 const char * author = NULL;
282 const char * version = NULL;
283 const char * desc = NULL;
284
285 const char * na = m_pApp->getStringSet()->getValue(XAP_STRING_ID_DLG_PLUGIN_MANAGER_NOT_AVAILABLE);
286
287 if (pModule)
288 {
289 const XAP_ModuleInfo * mi = pModule->getModuleInfo ();
290 if (mi)
291 {
292 name = mi->name;
293 author = mi->author;
294 desc = mi->desc;
295 version = mi->version;
296 }
297 }
298
299 if (!name) name = na;
300 if (!author) author = na;
301 if (!version) version = na;
302 if (!desc) desc = na;
303
304 gtk_label_set_text (GTK_LABEL (m_name), name);
305 gtk_label_set_text (GTK_LABEL (m_author), author);
306 gtk_label_set_text (GTK_LABEL (m_version), version);
307 gtk_label_set_text (GTK_LABEL (m_desc), desc) ;
308 }
309
310 /*****************************************************************/
311
312 void XAP_UnixDialog_PluginManager::s_load_clicked (GtkWidget *,
313 XAP_UnixDialog_PluginManager * dlg)
314 {
315 UT_return_if_fail (dlg);
316 dlg->event_Load ();
317 }
318
319 void XAP_UnixDialog_PluginManager::s_list_clicked(GtkTreeSelection *,
320 XAP_UnixDialog_PluginManager * dlg)
321 {
322 UT_return_if_fail(dlg);
323 dlg->_refresh ();
324 }
325
326 /*****************************************************************/
327
328 void XAP_UnixDialog_PluginManager::runModal(XAP_Frame * pFrame)
329 {
330 m_pFrame = pFrame;
331
332 // build the dialog
333 GtkWidget * cf = _constructWindow();
334 UT_return_if_fail(cf);
335
336 gtk_window_set_default_size(GTK_WINDOW(cf), 500, 300);
337
338 // load the data
339 _updatePluginList ();
340
341 abiRunModalDialog (GTK_DIALOG(cf), pFrame, this, GTK_RESPONSE_CLOSE, true);
342 }
343
344 GtkWidget * XAP_UnixDialog_PluginManager::_constructWindow ()
345 {
346 const XAP_StringSet * pSS = m_pApp->getStringSet();
347
348 // load the dialog from the UI file
349 #if GTK_CHECK_VERSION(3,0,0)
350 GtkBuilder* builder = newDialogBuilder("xap_UnixDlg_PluginManager.ui");
351 #else
352 GtkBuilder* builder = newDialogBuilder("xap_UnixDlg_PluginManager-2.ui");
353 #endif
354 m_windowMain = GTK_WIDGET(gtk_builder_get_object(builder, "xap_UnixDlg_PluginManager"));
355 m_list = GTK_WIDGET(gtk_builder_get_object(builder, "tvPlugins"));
356 m_name = GTK_WIDGET(gtk_builder_get_object(builder, "lbPluginName"));
357 m_author = GTK_WIDGET(gtk_builder_get_object(builder, "lbPluginAuthor"));
358 m_version = GTK_WIDGET(gtk_builder_get_object(builder, "lbPluginVersion"));
359 m_desc = GTK_WIDGET(gtk_builder_get_object(builder, "lbPluginDescription"));
360
361 gtk_window_set_title(GTK_WINDOW(m_windowMain), pSS->getValue(XAP_STRING_ID_DLG_PLUGIN_MANAGER_TITLE));
362
363 localizeLabelMarkup(GTK_WIDGET(gtk_builder_get_object(builder, "lbActivePlugins")), pSS, XAP_STRING_ID_DLG_PLUGIN_MANAGER_ACTIVE);
364 localizeLabelMarkup(GTK_WIDGET(gtk_builder_get_object(builder, "lbPluginDetails")), pSS, XAP_STRING_ID_DLG_PLUGIN_MANAGER_DETAILS);
365 localizeLabel(GTK_WIDGET(gtk_builder_get_object(builder, "lbNameLabel")), pSS, XAP_STRING_ID_DLG_PLUGIN_MANAGER_NAME);
366 localizeLabel(GTK_WIDGET(gtk_builder_get_object(builder, "lbDescriptionLabel")), pSS, XAP_STRING_ID_DLG_PLUGIN_MANAGER_DESC);
367 localizeLabel(GTK_WIDGET(gtk_builder_get_object(builder, "lbAuthorLabel")), pSS, XAP_STRING_ID_DLG_PLUGIN_MANAGER_AUTHOR);
368 localizeLabel(GTK_WIDGET(gtk_builder_get_object(builder, "lbVersionLabel")), pSS, XAP_STRING_ID_DLG_PLUGIN_MANAGER_VERSION);
369
370 GtkWidget * btInstall = GTK_WIDGET(gtk_builder_get_object(builder, "btInstall"));
371
372 #if defined(FLATPAK_BUILD)
373 gtk_widget_hide(btInstall);
374 #else
375 localizeButton(btInstall, pSS, XAP_STRING_ID_DLG_PLUGIN_MANAGER_INSTALL);
376 #endif
377
378 GtkCellRenderer *renderer;
379 GtkTreeViewColumn *column;
380
381 renderer = gtk_cell_renderer_text_new ();
382 column = gtk_tree_view_column_new_with_attributes ("Format",
383 renderer,
384 "text",
385 0,
386 NULL);
387 gtk_tree_view_append_column( GTK_TREE_VIEW(m_list), column);
388
389 #if !defined(FLATPAK_BUILD)
390 g_signal_connect (G_OBJECT(btInstall), "clicked",
391 G_CALLBACK(s_load_clicked),
392 static_cast<gpointer>(this));
393 #endif
394
395 g_signal_connect_after(G_OBJECT(gtk_tree_view_get_selection (GTK_TREE_VIEW (m_list))),
396 "changed",
397 G_CALLBACK(s_list_clicked),
398 static_cast<gpointer>(this));
399
400 g_object_unref(G_OBJECT(builder));
401
402 return m_windowMain;
403 }
404
405 /*!
406 * Select the plugin list's first entry.
407 */
408 void
409 XAP_UnixDialog_PluginManager::_selectFirstEntry ()
410 {
411 GtkTreeSelection *selection = gtk_tree_view_get_selection ( GTK_TREE_VIEW (m_list));
412 GtkTreePath* path = gtk_tree_path_new_first();
413 gtk_tree_selection_select_path (selection, path);
414 gtk_tree_path_free (path);
415 }