~midori/midori/trunk

« back to all changes in this revision

Viewing changes to panels/midori-extensions.c

  • Committer: Christian Dywan
  • Date: 2008-12-04 00:44:33 UTC
  • Revision ID: git-v1:e4f401c7e73a4844d5bc12177f3321bff3b38378
Implement new Extensions panel, in the new folder extensions

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 Copyright (C) 2008 Christian Dywan <christian@twotoasts.de>
 
3
 
 
4
 This library is free software; you can redistribute it and/or
 
5
 modify it under the terms of the GNU Lesser General Public
 
6
 License as published by the Free Software Foundation; either
 
7
 version 2.1 of the License, or (at your option) any later version.
 
8
 
 
9
 See the file COPYING for the full license text.
 
10
*/
 
11
 
 
12
#include "midori-extensions.h"
 
13
 
 
14
#include "midori-app.h"
 
15
#include "midori-extension.h"
 
16
#include "midori-stock.h"
 
17
#include "midori-viewable.h"
 
18
 
 
19
#include "sokoke.h"
 
20
#include <glib/gi18n.h>
 
21
 
 
22
struct _MidoriExtensions
 
23
{
 
24
    GtkVBox parent_instance;
 
25
 
 
26
    GtkWidget* toolbar;
 
27
    GtkWidget* treeview;
 
28
    MidoriApp* app;
 
29
};
 
30
 
 
31
struct _MidoriExtensionsClass
 
32
{
 
33
    GtkVBoxClass parent_class;
 
34
};
 
35
 
 
36
static void
 
37
midori_extensions_viewable_iface_init (MidoriViewableIface* iface);
 
38
 
 
39
G_DEFINE_TYPE_WITH_CODE (MidoriExtensions, midori_extensions, GTK_TYPE_VBOX,
 
40
                         G_IMPLEMENT_INTERFACE (MIDORI_TYPE_VIEWABLE,
 
41
                             midori_extensions_viewable_iface_init));
 
42
 
 
43
enum
 
44
{
 
45
    PROP_0,
 
46
 
 
47
    PROP_APP
 
48
};
 
49
 
 
50
static void
 
51
midori_extensions_set_property (GObject*      object,
 
52
                                guint         prop_id,
 
53
                                const GValue* value,
 
54
                                GParamSpec*   pspec);
 
55
 
 
56
static void
 
57
midori_extensions_get_property (GObject*    object,
 
58
                                guint       prop_id,
 
59
                                GValue*     value,
 
60
                                GParamSpec* pspec);
 
61
 
 
62
static void
 
63
midori_extensions_class_init (MidoriExtensionsClass* class)
 
64
{
 
65
    GObjectClass* gobject_class;
 
66
    GParamFlags flags;
 
67
 
 
68
    gobject_class = G_OBJECT_CLASS (class);
 
69
    gobject_class->set_property = midori_extensions_set_property;
 
70
    gobject_class->get_property = midori_extensions_get_property;
 
71
 
 
72
    flags = G_PARAM_READWRITE | G_PARAM_CONSTRUCT;
 
73
 
 
74
    g_object_class_install_property (gobject_class,
 
75
                                     PROP_APP,
 
76
                                     g_param_spec_object (
 
77
                                     "app",
 
78
                                     "App",
 
79
                                     "The app",
 
80
                                     MIDORI_TYPE_APP,
 
81
                                     flags));
 
82
}
 
83
 
 
84
static const gchar*
 
85
midori_extensions_get_label (MidoriViewable* viewable)
 
86
{
 
87
    return _("Extensions");
 
88
}
 
89
 
 
90
static const gchar*
 
91
midori_extensions_get_stock_id (MidoriViewable* viewable)
 
92
{
 
93
    return STOCK_EXTENSIONS;
 
94
}
 
95
 
 
96
static void
 
97
midori_extensions_button_status_clicked_cb (GtkToolItem*      toolitem,
 
98
                                            MidoriExtensions* extensions)
 
99
{
 
100
    GtkTreeView* treeview;
 
101
    GtkTreeModel* model;
 
102
    GtkTreeIter iter;
 
103
    MidoriExtension* extension;
 
104
 
 
105
    treeview = GTK_TREE_VIEW (extensions->treeview);
 
106
 
 
107
    if (sokoke_tree_view_get_selected_iter (treeview, &model, &iter))
 
108
    {
 
109
        GtkToolItem* button_enable = gtk_toolbar_get_nth_item (
 
110
            GTK_TOOLBAR (extensions->toolbar), 1);
 
111
        GtkToolItem* button_disable = gtk_toolbar_get_nth_item (
 
112
            GTK_TOOLBAR (extensions->toolbar), 2);
 
113
 
 
114
        gtk_tree_model_get (model, &iter, 0, &extension, -1);
 
115
        if (toolitem == button_enable)
 
116
            g_signal_emit_by_name (extension, "activate", extensions->app);
 
117
        else if (toolitem == button_disable)
 
118
            midori_extension_deactivate (extension);
 
119
 
 
120
        gtk_widget_set_sensitive (GTK_WIDGET (button_enable),
 
121
            !midori_extension_is_active (extension));
 
122
        gtk_widget_set_sensitive (GTK_WIDGET (button_enable),
 
123
            midori_extension_is_active (extension));
 
124
    }
 
125
}
 
126
 
 
127
static GtkWidget*
 
128
midori_extensions_get_toolbar (MidoriViewable* extensions)
 
129
{
 
130
    if (!MIDORI_EXTENSIONS (extensions)->toolbar)
 
131
    {
 
132
        GtkWidget* toolbar;
 
133
        GtkToolItem* toolitem;
 
134
 
 
135
        toolbar = gtk_toolbar_new ();
 
136
        gtk_toolbar_set_style (GTK_TOOLBAR (toolbar), GTK_TOOLBAR_BOTH_HORIZ);
 
137
        gtk_toolbar_set_icon_size (GTK_TOOLBAR (toolbar), GTK_ICON_SIZE_BUTTON);
 
138
        toolitem = gtk_tool_item_new ();
 
139
        gtk_toolbar_insert (GTK_TOOLBAR (toolbar), toolitem, -1);
 
140
        gtk_widget_show (GTK_WIDGET (toolitem));
 
141
 
 
142
        /* enable button */
 
143
        toolitem = gtk_tool_button_new_from_stock (GTK_STOCK_YES);
 
144
        gtk_tool_button_set_label (GTK_TOOL_BUTTON (toolitem), _("_Enable"));
 
145
        g_signal_connect (toolitem, "clicked",
 
146
            G_CALLBACK (midori_extensions_button_status_clicked_cb), extensions);
 
147
        gtk_toolbar_insert (GTK_TOOLBAR (toolbar), toolitem, -1);
 
148
        gtk_widget_set_sensitive (GTK_WIDGET (toolitem), FALSE);
 
149
        gtk_widget_show (GTK_WIDGET (toolitem));
 
150
 
 
151
        /* disable button */
 
152
        toolitem = gtk_tool_button_new_from_stock (GTK_STOCK_NO);
 
153
        gtk_tool_button_set_label (GTK_TOOL_BUTTON (toolitem), _("_Disable"));
 
154
        g_signal_connect (toolitem, "clicked",
 
155
            G_CALLBACK (midori_extensions_button_status_clicked_cb), extensions);
 
156
        gtk_toolbar_insert (GTK_TOOLBAR (toolbar), toolitem, -1);
 
157
        gtk_widget_set_sensitive (GTK_WIDGET (toolitem), FALSE);
 
158
        gtk_widget_show (GTK_WIDGET (toolitem));
 
159
 
 
160
        MIDORI_EXTENSIONS (extensions)->toolbar = toolbar;
 
161
    }
 
162
 
 
163
    return MIDORI_EXTENSIONS (extensions)->toolbar;
 
164
}
 
165
 
 
166
static void
 
167
midori_extensions_viewable_iface_init (MidoriViewableIface* iface)
 
168
{
 
169
    iface->get_stock_id = midori_extensions_get_stock_id;
 
170
    iface->get_label = midori_extensions_get_label;
 
171
    iface->get_toolbar = midori_extensions_get_toolbar;
 
172
}
 
173
 
 
174
static void
 
175
midori_extensions_add_item_cb (KatzeArray*       array,
 
176
                               MidoriExtension*  extension,
 
177
                               MidoriExtensions* extensions)
 
178
{
 
179
    GtkTreeIter iter;
 
180
    GtkListStore* liststore = GTK_LIST_STORE (gtk_tree_view_get_model (
 
181
        GTK_TREE_VIEW (extensions->treeview)));
 
182
    gtk_list_store_append (liststore, &iter);
 
183
        gtk_list_store_set (liststore, &iter, 0, extension, -1);
 
184
}
 
185
 
 
186
static void
 
187
midori_extensions_set_property (GObject*      object,
 
188
                                guint         prop_id,
 
189
                                const GValue* value,
 
190
                                GParamSpec*   pspec)
 
191
{
 
192
    MidoriExtensions* extensions = MIDORI_EXTENSIONS (object);
 
193
 
 
194
    switch (prop_id)
 
195
    {
 
196
    case PROP_APP:
 
197
    {
 
198
        KatzeArray* array;
 
199
        guint i, n;
 
200
 
 
201
        extensions->app = g_value_get_object (value);
 
202
        array = katze_object_get_object (extensions->app, "extensions");
 
203
        g_signal_connect (array, "add-item",
 
204
            G_CALLBACK (midori_extensions_add_item_cb), extensions);
 
205
 
 
206
        if ((n = katze_array_get_length (array)))
 
207
            for (i = 0; i < n; i++)
 
208
                midori_extensions_add_item_cb (array,
 
209
                    katze_array_get_nth_item (array, i), extensions);
 
210
    }
 
211
        break;
 
212
    default:
 
213
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
214
        break;
 
215
    }
 
216
}
 
217
 
 
218
static void
 
219
midori_extensions_get_property (GObject*    object,
 
220
                                guint       prop_id,
 
221
                                GValue*     value,
 
222
                                GParamSpec* pspec)
 
223
{
 
224
    MidoriExtensions* extensions = MIDORI_EXTENSIONS (object);
 
225
 
 
226
    switch (prop_id)
 
227
    {
 
228
    case PROP_APP:
 
229
        g_value_set_object (value, extensions->app);
 
230
        break;
 
231
    default:
 
232
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
233
        break;
 
234
    }
 
235
}
 
236
 
 
237
static void
 
238
midori_extensions_treeview_render_icon_cb (GtkTreeViewColumn* column,
 
239
                                           GtkCellRenderer*   renderer,
 
240
                                           GtkTreeModel*      model,
 
241
                                           GtkTreeIter*       iter,
 
242
                                           GtkWidget*         treeview)
 
243
{
 
244
    g_object_set (renderer, "stock-id", GTK_STOCK_EXECUTE, NULL);
 
245
}
 
246
 
 
247
static void
 
248
midori_extensions_treeview_render_text_cb (GtkTreeViewColumn* column,
 
249
                                           GtkCellRenderer*   renderer,
 
250
                                           GtkTreeModel*      model,
 
251
                                           GtkTreeIter*       iter,
 
252
                                           GtkWidget*         treeview)
 
253
{
 
254
    MidoriExtension* extension;
 
255
    gchar* text;
 
256
 
 
257
    gtk_tree_model_get (model, iter, 0, &extension, -1);
 
258
 
 
259
    text = g_strdup_printf ("%s\n%s",
 
260
        katze_object_get_string (extension, "name"),
 
261
        katze_object_get_string (extension, "description"));
 
262
    g_object_set (renderer, "text", text, NULL);
 
263
    g_free (text);
 
264
}
 
265
 
 
266
static void
 
267
midori_extensions_treeview_row_activated_cb (GtkTreeView*       treeview,
 
268
                                             GtkTreePath*       path,
 
269
                                             GtkTreeViewColumn* column,
 
270
                                             MidoriExtensions*  extensions)
 
271
{
 
272
    GtkTreeModel* model = gtk_tree_view_get_model (treeview);
 
273
    GtkTreeIter iter;
 
274
 
 
275
    if (gtk_tree_model_get_iter (model, &iter, path))
 
276
    {
 
277
        MidoriExtension* extension;
 
278
 
 
279
        gtk_tree_model_get (model, &iter, 0, &extension, -1);
 
280
        if (midori_extension_is_active (extension))
 
281
        {
 
282
            midori_extension_deactivate (extension);
 
283
            gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
 
284
        }
 
285
        else
 
286
            g_signal_emit_by_name (extension, "activate", extensions->app);
 
287
    }
 
288
}
 
289
 
 
290
static void
 
291
midori_extensions_init (MidoriExtensions* extensions)
 
292
{
 
293
    /* Create the treeview */
 
294
    GtkTreeViewColumn* column;
 
295
    GtkCellRenderer* renderer_text;
 
296
    GtkCellRenderer* renderer_pixbuf;
 
297
    GtkListStore* liststore = gtk_list_store_new (1, G_TYPE_OBJECT);
 
298
    extensions->treeview = gtk_tree_view_new_with_model (GTK_TREE_MODEL (liststore));
 
299
    gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (extensions->treeview), FALSE);
 
300
    column = gtk_tree_view_column_new ();
 
301
    renderer_pixbuf = gtk_cell_renderer_pixbuf_new ();
 
302
    gtk_tree_view_column_pack_start (column, renderer_pixbuf, FALSE);
 
303
    gtk_tree_view_column_set_cell_data_func (column, renderer_pixbuf,
 
304
        (GtkTreeCellDataFunc)midori_extensions_treeview_render_icon_cb,
 
305
        extensions->treeview, NULL);
 
306
    renderer_text = gtk_cell_renderer_text_new ();
 
307
    gtk_tree_view_column_pack_start (column, renderer_text, FALSE);
 
308
    gtk_tree_view_column_set_cell_data_func (column, renderer_text,
 
309
        (GtkTreeCellDataFunc)midori_extensions_treeview_render_text_cb,
 
310
        extensions->treeview, NULL);
 
311
    gtk_tree_view_append_column (GTK_TREE_VIEW (extensions->treeview), column);
 
312
    g_object_unref (liststore);
 
313
    g_signal_connect (extensions->treeview, "row-activated",
 
314
                      G_CALLBACK (midori_extensions_treeview_row_activated_cb),
 
315
                      extensions);
 
316
    gtk_widget_show (extensions->treeview);
 
317
    gtk_box_pack_start (GTK_BOX (extensions), extensions->treeview, TRUE, TRUE, 0);
 
318
}
 
319
 
 
320
/**
 
321
 * midori_extensions_new:
 
322
 *
 
323
 * Creates a new empty extensions.
 
324
 *
 
325
 * Return value: a new #MidoriExtensions
 
326
 *
 
327
 * Since: 0.1.2
 
328
 **/
 
329
GtkWidget*
 
330
midori_extensions_new (void)
 
331
{
 
332
    MidoriExtensions* extensions = g_object_new (MIDORI_TYPE_EXTENSIONS,
 
333
                                                 NULL);
 
334
 
 
335
    return GTK_WIDGET (extensions);
 
336
}