summaryrefslogtreecommitdiffstats
path: root/app/widgets/gimppluginview.c
blob: 43058b38851ab219f9a529eaf7dce8fd32c5d554 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * gimppluginview.c
 * Copyright (C) 2017  Michael Natterer <mitch@gimp.org>
 * Copyright (C) 2004  Sven Neumann <sven@gimp.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <gtk/gtk.h>
#include <gegl.h>

#include "libgimpconfig/gimpconfig.h"

#include "widgets-types.h"

#include "core/gimpmarshal.h"

#include "plug-in/gimppluginprocedure.h"

#include "gimppluginview.h"

#include "gimp-intl.h"


enum
{
  COLUMN_FILE,
  COLUMN_PATH,
  N_COLUMNS
};

enum
{
  CHANGED,
  LAST_SIGNAL
};


static void  gimp_plug_in_view_finalize          (GObject          *object);

static void  gimp_plug_in_view_selection_changed (GtkTreeSelection *selection,
                                                  GimpPlugInView   *view);


G_DEFINE_TYPE (GimpPlugInView, gimp_plug_in_view, GTK_TYPE_TREE_VIEW)

#define parent_class gimp_plug_in_view_parent_class

static guint view_signals[LAST_SIGNAL] = { 0 };


static void
gimp_plug_in_view_class_init (GimpPlugInViewClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->finalize = gimp_plug_in_view_finalize;

  klass->changed         = NULL;

  view_signals[CHANGED] = g_signal_new ("changed",
                                        G_TYPE_FROM_CLASS (klass),
                                        G_SIGNAL_RUN_LAST,
                                        G_STRUCT_OFFSET (GimpPlugInViewClass,
                                                         changed),
                                        NULL, NULL,
                                        gimp_marshal_VOID__VOID,
                                        G_TYPE_NONE, 0);
}

static void
gimp_plug_in_view_init (GimpPlugInView *view)
{
  view->plug_in_hash = g_hash_table_new_full (g_file_hash,
                                              (GEqualFunc) g_file_equal,
                                              (GDestroyNotify) g_object_unref,
                                              (GDestroyNotify) g_free);
}

static void
gimp_plug_in_view_finalize (GObject *object)
{
  GimpPlugInView *view = GIMP_PLUG_IN_VIEW (object);

  g_clear_pointer (&view->plug_in_hash, g_hash_table_unref);

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

GtkWidget *
gimp_plug_in_view_new (GSList *procedures)
{
  GtkTreeView       *view;
  GtkTreeViewColumn *column;
  GtkCellRenderer   *cell;
  GtkListStore      *store;
  GSList            *list;

  store = gtk_list_store_new (N_COLUMNS,
                              G_TYPE_FILE,    /* COLUMN_FILE */
                              G_TYPE_STRING); /* COLUMN_PATH */

  view = g_object_new (GIMP_TYPE_PLUG_IN_VIEW,
                       "model", store,
                       NULL);

  g_object_unref (store);

  for (list = procedures; list; list = g_slist_next (list))
    {
      GimpPlugInProcedure *proc = list->data;
      GFile               *file = gimp_plug_in_procedure_get_file (proc);

      if (! g_hash_table_lookup (GIMP_PLUG_IN_VIEW (view)->plug_in_hash, file))
        {
          GtkTreeIter  iter;
          gchar       *path = gimp_file_get_config_path (file, NULL);

          gtk_list_store_append (store, &iter);
          gtk_list_store_set (store, &iter,
                              COLUMN_FILE, file,
                              COLUMN_PATH, path,
                              -1);

          g_free (path);

          g_hash_table_insert (GIMP_PLUG_IN_VIEW (view)->plug_in_hash,
                               g_object_ref (file),
                               g_memdup (&iter, sizeof (GtkTreeIter)));
        }
    }

  column = gtk_tree_view_column_new ();
  gtk_tree_view_column_set_title (column, _("Plug-In"));
  gtk_tree_view_column_set_expand (column, TRUE);

  cell = gtk_cell_renderer_text_new ();
  gtk_tree_view_column_pack_start (column, cell, TRUE);
  gtk_tree_view_column_set_attributes (column, cell,
                                       "text", COLUMN_PATH,
                                       NULL);

  gtk_tree_view_append_column (view, column);

  g_signal_connect (gtk_tree_view_get_selection (view), "changed",
                    G_CALLBACK (gimp_plug_in_view_selection_changed),
                    view);

  return GTK_WIDGET (view);
}

gchar *
gimp_plug_in_view_get_plug_in (GimpPlugInView *view)
{
  GtkTreeModel     *model;
  GtkTreeSelection *selection;
  GtkTreeIter       iter;

  g_return_val_if_fail (GIMP_IS_PLUG_IN_VIEW (view), NULL);

  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));

  if (gtk_tree_selection_get_selected (selection, &model, &iter))
    {
      gchar *path;

      gtk_tree_model_get (model, &iter,
                          COLUMN_PATH,  &path,
                          -1);

      return path;
    }

  return NULL;
}

gboolean
gimp_plug_in_view_set_plug_in (GimpPlugInView *view,
                               const gchar    *path)
{
  GFile            *file;
  GtkTreeIter      *iter;
  GtkTreeSelection *selection;

  g_return_val_if_fail (GIMP_IS_PLUG_IN_VIEW (view), FALSE);

  file = gimp_file_new_for_config_path (path, NULL);

  iter = g_hash_table_lookup (view->plug_in_hash, file);

  g_object_unref (file);

  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));

  if (iter)
    {
      gtk_tree_selection_select_iter (selection, iter);

      return TRUE;
    }

  gtk_tree_selection_unselect_all (selection);

  return FALSE;
}

static void
gimp_plug_in_view_selection_changed (GtkTreeSelection *selection,
                                     GimpPlugInView   *view)
{
  g_signal_emit (view, view_signals[CHANGED], 0);
}