1 /* GIMP - The GNU Image Manipulation Program
2  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3  *
4  * gimppluginview.c
5  * Copyright (C) 2017  Michael Natterer <mitch@gimp.org>
6  * Copyright (C) 2004  Sven Neumann <sven@gimp.org>
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (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, see <https://www.gnu.org/licenses/>.
20  */
21 
22 #include "config.h"
23 
24 #include <gtk/gtk.h>
25 #include <gegl.h>
26 
27 #include "libgimpconfig/gimpconfig.h"
28 
29 #include "widgets-types.h"
30 
31 #include "core/gimpmarshal.h"
32 
33 #include "plug-in/gimppluginprocedure.h"
34 
35 #include "gimppluginview.h"
36 
37 #include "gimp-intl.h"
38 
39 
40 enum
41 {
42   COLUMN_FILE,
43   COLUMN_PATH,
44   N_COLUMNS
45 };
46 
47 enum
48 {
49   CHANGED,
50   LAST_SIGNAL
51 };
52 
53 
54 static void  gimp_plug_in_view_finalize          (GObject          *object);
55 
56 static void  gimp_plug_in_view_selection_changed (GtkTreeSelection *selection,
57                                                   GimpPlugInView   *view);
58 
59 
60 G_DEFINE_TYPE (GimpPlugInView, gimp_plug_in_view, GTK_TYPE_TREE_VIEW)
61 
62 #define parent_class gimp_plug_in_view_parent_class
63 
64 static guint view_signals[LAST_SIGNAL] = { 0 };
65 
66 
67 static void
gimp_plug_in_view_class_init(GimpPlugInViewClass * klass)68 gimp_plug_in_view_class_init (GimpPlugInViewClass *klass)
69 {
70   GObjectClass *object_class = G_OBJECT_CLASS (klass);
71 
72   object_class->finalize = gimp_plug_in_view_finalize;
73 
74   klass->changed         = NULL;
75 
76   view_signals[CHANGED] = g_signal_new ("changed",
77                                         G_TYPE_FROM_CLASS (klass),
78                                         G_SIGNAL_RUN_LAST,
79                                         G_STRUCT_OFFSET (GimpPlugInViewClass,
80                                                          changed),
81                                         NULL, NULL,
82                                         gimp_marshal_VOID__VOID,
83                                         G_TYPE_NONE, 0);
84 }
85 
86 static void
gimp_plug_in_view_init(GimpPlugInView * view)87 gimp_plug_in_view_init (GimpPlugInView *view)
88 {
89   view->plug_in_hash = g_hash_table_new_full (g_file_hash,
90                                               (GEqualFunc) g_file_equal,
91                                               (GDestroyNotify) g_object_unref,
92                                               (GDestroyNotify) g_free);
93 }
94 
95 static void
gimp_plug_in_view_finalize(GObject * object)96 gimp_plug_in_view_finalize (GObject *object)
97 {
98   GimpPlugInView *view = GIMP_PLUG_IN_VIEW (object);
99 
100   g_clear_pointer (&view->plug_in_hash, g_hash_table_unref);
101 
102   G_OBJECT_CLASS (parent_class)->finalize (object);
103 }
104 
105 GtkWidget *
gimp_plug_in_view_new(GSList * procedures)106 gimp_plug_in_view_new (GSList *procedures)
107 {
108   GtkTreeView       *view;
109   GtkTreeViewColumn *column;
110   GtkCellRenderer   *cell;
111   GtkListStore      *store;
112   GSList            *list;
113 
114   store = gtk_list_store_new (N_COLUMNS,
115                               G_TYPE_FILE,    /* COLUMN_FILE */
116                               G_TYPE_STRING); /* COLUMN_PATH */
117 
118   view = g_object_new (GIMP_TYPE_PLUG_IN_VIEW,
119                        "model", store,
120                        NULL);
121 
122   g_object_unref (store);
123 
124   for (list = procedures; list; list = g_slist_next (list))
125     {
126       GimpPlugInProcedure *proc = list->data;
127       GFile               *file = gimp_plug_in_procedure_get_file (proc);
128 
129       if (! g_hash_table_lookup (GIMP_PLUG_IN_VIEW (view)->plug_in_hash, file))
130         {
131           GtkTreeIter  iter;
132           gchar       *path = gimp_file_get_config_path (file, NULL);
133 
134           gtk_list_store_append (store, &iter);
135           gtk_list_store_set (store, &iter,
136                               COLUMN_FILE, file,
137                               COLUMN_PATH, path,
138                               -1);
139 
140           g_free (path);
141 
142           g_hash_table_insert (GIMP_PLUG_IN_VIEW (view)->plug_in_hash,
143                                g_object_ref (file),
144                                g_memdup (&iter, sizeof (GtkTreeIter)));
145         }
146     }
147 
148   column = gtk_tree_view_column_new ();
149   gtk_tree_view_column_set_title (column, _("Plug-In"));
150   gtk_tree_view_column_set_expand (column, TRUE);
151 
152   cell = gtk_cell_renderer_text_new ();
153   gtk_tree_view_column_pack_start (column, cell, TRUE);
154   gtk_tree_view_column_set_attributes (column, cell,
155                                        "text", COLUMN_PATH,
156                                        NULL);
157 
158   gtk_tree_view_append_column (view, column);
159 
160   g_signal_connect (gtk_tree_view_get_selection (view), "changed",
161                     G_CALLBACK (gimp_plug_in_view_selection_changed),
162                     view);
163 
164   return GTK_WIDGET (view);
165 }
166 
167 gchar *
gimp_plug_in_view_get_plug_in(GimpPlugInView * view)168 gimp_plug_in_view_get_plug_in (GimpPlugInView *view)
169 {
170   GtkTreeModel     *model;
171   GtkTreeSelection *selection;
172   GtkTreeIter       iter;
173 
174   g_return_val_if_fail (GIMP_IS_PLUG_IN_VIEW (view), NULL);
175 
176   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
177 
178   if (gtk_tree_selection_get_selected (selection, &model, &iter))
179     {
180       gchar *path;
181 
182       gtk_tree_model_get (model, &iter,
183                           COLUMN_PATH,  &path,
184                           -1);
185 
186       return path;
187     }
188 
189   return NULL;
190 }
191 
192 gboolean
gimp_plug_in_view_set_plug_in(GimpPlugInView * view,const gchar * path)193 gimp_plug_in_view_set_plug_in (GimpPlugInView *view,
194                                const gchar    *path)
195 {
196   GFile            *file;
197   GtkTreeIter      *iter;
198   GtkTreeSelection *selection;
199 
200   g_return_val_if_fail (GIMP_IS_PLUG_IN_VIEW (view), FALSE);
201 
202   file = gimp_file_new_for_config_path (path, NULL);
203 
204   iter = g_hash_table_lookup (view->plug_in_hash, file);
205 
206   g_object_unref (file);
207 
208   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
209 
210   if (iter)
211     {
212       gtk_tree_selection_select_iter (selection, iter);
213 
214       return TRUE;
215     }
216 
217   gtk_tree_selection_unselect_all (selection);
218 
219   return FALSE;
220 }
221 
222 static void
gimp_plug_in_view_selection_changed(GtkTreeSelection * selection,GimpPlugInView * view)223 gimp_plug_in_view_selection_changed (GtkTreeSelection *selection,
224                                      GimpPlugInView   *view)
225 {
226   g_signal_emit (view, view_signals[CHANGED], 0);
227 }
228