aboutsummaryrefslogtreecommitdiffstats
path: root/src/bookmarks/ephy-bookmark-action.c
blob: 1362b8d02085d2bfa01ff5c408249a300f45b307 (plain) (blame)
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/*
 *  Copyright (C) 2003 Marco Pesenti Gritti
 *
 *  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 2, 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, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <bonobo/bonobo-i18n.h>
#include <libgnomevfs/gnome-vfs-uri.h>

#include "ephy-bookmark-action.h"
#include "ephy-bookmark-toolitem.h"
#include "ephy-bookmarks.h"
#include "ephy-shell.h"
#include "ephy-string.h"
#include "eggtoolitem.h"
#include "ephy-debug.h"

#define MAX_LABEL_LENGTH 30

static void ephy_bookmark_action_init       (EphyBookmarkAction *action);
static void ephy_bookmark_action_class_init (EphyBookmarkActionClass *class);

struct EphyBookmarkActionPrivate
{
    int bookmark_id;
    char *location;
    gboolean smart_url;
    char *icon;
};

enum
{
    PROP_0,
    PROP_BOOKMARK_ID,
    PROP_LOCATION,
    PROP_SMART_URL,
    PROP_ICON
};

enum
{
    GO_LOCATION,
    LAST_SIGNAL
};

static GObjectClass *parent_class = NULL;

static guint ephy_bookmark_action_signals[LAST_SIGNAL] = { 0 };

GType
ephy_bookmark_action_get_type (void)
{
    static GType type = 0;

    if (!type)
    {
        static const GTypeInfo type_info =
        {
            sizeof (EphyBookmarkActionClass),
            (GBaseInitFunc) NULL,
            (GBaseFinalizeFunc) NULL,
            (GClassInitFunc) ephy_bookmark_action_class_init,
            (GClassFinalizeFunc) NULL,
            NULL,
            sizeof (EphyBookmarkAction),
            0, /* n_preallocs */
            (GInstanceInitFunc) ephy_bookmark_action_init,
        };

        type = g_type_register_static (EGG_TYPE_ACTION,
                           "EphyBookmarkAction",
                           &type_info, 0);
    }
    return type;
}

static void
ephy_bookmark_action_sync_smart_url (EggAction *action, GParamSpec *pspec, GtkWidget *proxy)
{
    if (EGG_IS_TOOL_ITEM (proxy))
    {
        GtkWidget *entry;
        gboolean smart_url;

        smart_url = EPHY_BOOKMARK_ACTION (action)->priv->smart_url;
        entry = GTK_WIDGET (g_object_get_data (G_OBJECT (proxy), "entry"));

        if (smart_url)
        {
            gtk_widget_show (entry);
        }
        else
        {
            gtk_widget_hide (entry);
        }
    }
}

static void
ephy_bookmark_action_sync_icon (EggAction *action, GParamSpec *pspec, GtkWidget *proxy)
{
    char *icon_location;
    EphyFaviconCache *cache;
    GdkPixbuf *pixbuf = NULL;

    icon_location = EPHY_BOOKMARK_ACTION (action)->priv->icon;

    cache = ephy_embed_shell_get_favicon_cache
        (EPHY_EMBED_SHELL (ephy_shell));

    if (icon_location)
    {
        pixbuf = ephy_favicon_cache_get (cache, icon_location);
    }

    if (pixbuf == NULL) return;

    if (EGG_IS_TOOL_ITEM (proxy))
    {
        GtkImage *icon;

        icon = GTK_IMAGE (g_object_get_data (G_OBJECT (proxy), "icon"));
        g_return_if_fail (icon != NULL);

        gtk_image_set_from_pixbuf (icon, pixbuf);
    }
    else if (GTK_IS_MENU_ITEM (proxy))
    {
        GtkWidget *image;

        image = gtk_image_new_from_pixbuf (pixbuf);
        gtk_widget_show (image);

        gtk_image_menu_item_set_image
            (GTK_IMAGE_MENU_ITEM (proxy), image);
    }

    g_object_unref (pixbuf);
}

static void
ephy_bookmark_action_sync_label (EggAction *action, GParamSpec *pspec, GtkWidget *proxy)
{
    GtkWidget *label;
    char *label_text;
    char *title;

    LOG ("Set bookmark action proxy label to %s", action->label)

    title = ephy_string_shorten (action->label, MAX_LABEL_LENGTH);

    if (EPHY_BOOKMARK_ACTION (action)->priv->smart_url)
    {
        label_text = g_strdup_printf (_("%s:"), title);
    }
    else
    {
        label_text = g_strdup (title);
    }

    if (EGG_IS_TOOL_ITEM (proxy))
    {
        label = g_object_get_data (G_OBJECT (proxy), "label");
        g_return_if_fail (label != NULL);
    }
    else if (GTK_IS_MENU_ITEM (proxy))
    {
        label = GTK_BIN (proxy)->child;
    }
    else
    {
        g_warning ("Unkown widget");
        return;
    }

    gtk_label_set_label (GTK_LABEL (label), label_text);

    g_free (label_text);
    g_free (title);
}

static void
activate_cb (GtkWidget *widget, const char *text, EggAction *action)
{
    char *location = NULL;

    if (text != NULL && text[0] != '\0')
    {
        char *smart_url;
        EphyBookmarks *bookmarks;

        bookmarks = ephy_shell_get_bookmarks (ephy_shell);

        smart_url = EPHY_BOOKMARK_ACTION (action)->priv->location;
        location = ephy_bookmarks_solve_smart_url (bookmarks,
                               smart_url,
                               text);
    }
    else
    {
        EphyBookmarkAction *baction = EPHY_BOOKMARK_ACTION (action);

        if (baction->priv->smart_url)
        {
            GnomeVFSURI *uri;

            uri = gnome_vfs_uri_new (baction->priv->location);

            if (uri)
            {
                location = g_strdup (gnome_vfs_uri_get_host_name (uri));
                gnome_vfs_uri_unref (uri);
            }
        }

        if (location == NULL)
        {
            location = g_strdup (baction->priv->location);
        }
    }

    g_signal_emit (action,
               ephy_bookmark_action_signals[GO_LOCATION],
               0, location);

    g_free (location);
}

static void
menu_activate_cb (GtkWidget *widget, EggAction *action)
{
    activate_cb (widget, NULL, action);
}

static void
connect_proxy (EggAction *action, GtkWidget *proxy)
{
    LOG ("Connecting action %p to proxy %p", action, proxy)

    (* EGG_ACTION_CLASS (parent_class)->connect_proxy) (action, proxy);

    ephy_bookmark_action_sync_label (action, NULL, proxy);
    g_signal_connect_object (action, "notify::label",
                     G_CALLBACK (ephy_bookmark_action_sync_label), proxy, 0);

    ephy_bookmark_action_sync_icon (action, NULL, proxy);
    g_signal_connect_object (action, "notify::icon",
                     G_CALLBACK (ephy_bookmark_action_sync_icon), proxy, 0);

    ephy_bookmark_action_sync_smart_url (action, NULL, proxy);
    g_signal_connect_object (action, "notify::smarturl",
                     G_CALLBACK (ephy_bookmark_action_sync_smart_url), proxy, 0);

    if (EGG_IS_TOOL_ITEM (proxy))
    {
        g_signal_connect (proxy, "activate", G_CALLBACK (activate_cb), action);
    }
    else if (GTK_IS_MENU_ITEM (proxy))
    {
        g_signal_connect (proxy, "activate", G_CALLBACK (menu_activate_cb), action);
    }
}

static void
ephy_bookmark_action_set_property (GObject *object,
                                   guint prop_id,
                                   const GValue *value,
                                   GParamSpec *pspec)
{
    EphyBookmarkAction *bmk;

    bmk = EPHY_BOOKMARK_ACTION (object);

    switch (prop_id)
    {
        case PROP_BOOKMARK_ID:
            bmk->priv->bookmark_id = g_value_get_int (value);
            break;
        case PROP_LOCATION:
            g_free (bmk->priv->location);
            bmk->priv->location = g_strdup (g_value_get_string (value));
            break;
        case PROP_SMART_URL:
            bmk->priv->smart_url = g_value_get_boolean (value);
            g_object_notify (object, "smarturl");
            break;
        case PROP_ICON:
            g_free (bmk->priv->icon);
            bmk->priv->icon = g_strdup (g_value_get_string (value));
            g_object_notify (object, "icon");
            break;
    }
}

static void
ephy_bookmark_action_get_property (GObject *object,
                                   guint prop_id,
                                   GValue *value,
                                   GParamSpec *pspec)
{
    EphyBookmarkAction *bmk;

    bmk = EPHY_BOOKMARK_ACTION (object);

    switch (prop_id)
    {
        case PROP_BOOKMARK_ID:
            g_value_set_boolean (value, bmk->priv->bookmark_id);
            break;
    }
}

static void
ephy_bookmark_action_finalize (GObject *object)
{
        EphyBookmarkAction *eba;

    g_return_if_fail (EPHY_IS_BOOKMARK_ACTION (object));

    eba = EPHY_BOOKMARK_ACTION (object);

        g_return_if_fail (eba->priv != NULL);

    g_free (eba->priv->location);
    g_free (eba->priv->icon);

        g_free (eba->priv);

    LOG ("Bookmark action finalized")

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

static void
ephy_bookmark_action_class_init (EphyBookmarkActionClass *class)
{
    EggActionClass *action_class;
    GObjectClass *object_class = G_OBJECT_CLASS (class);

    parent_class = g_type_class_peek_parent (class);
    action_class = EGG_ACTION_CLASS (class);

    action_class->toolbar_item_type = EPHY_TYPE_BOOKMARK_TOOLITEM;
    action_class->menu_item_type = GTK_TYPE_IMAGE_MENU_ITEM;
    action_class->connect_proxy = connect_proxy;

    object_class->finalize = ephy_bookmark_action_finalize;
    object_class->set_property = ephy_bookmark_action_set_property;
    object_class->get_property = ephy_bookmark_action_get_property;

    ephy_bookmark_action_signals[GO_LOCATION] =
                g_signal_new ("go_location",
                              G_OBJECT_CLASS_TYPE (object_class),
                              G_SIGNAL_RUN_FIRST,
                              G_STRUCT_OFFSET (EphyBookmarkActionClass, go_location),
                              NULL, NULL,
                              g_cclosure_marshal_VOID__STRING,
                              G_TYPE_NONE,
                              1,
                  G_TYPE_STRING);

    g_object_class_install_property (object_class,
                                         PROP_BOOKMARK_ID,
                                         g_param_spec_int ("bookmark_id",
                                                           "bookmark_id",
                                                           "bookmark_id",
                               0,
                               G_MAXINT,
                                                           0,
                                                           G_PARAM_READWRITE));
    g_object_class_install_property (object_class,
                                         PROP_LOCATION,
                                         g_param_spec_string  ("location",
                                                               "Location",
                                                               "Location",
                                                               NULL,
                                                               G_PARAM_READWRITE));
    g_object_class_install_property (object_class,
                                         PROP_SMART_URL,
                                         g_param_spec_boolean  ("smarturl",
                                                                "Smart url",
                                                                "Smart url",
                                                                FALSE,
                                                                G_PARAM_READWRITE));
    g_object_class_install_property (object_class,
                                         PROP_ICON,
                                         g_param_spec_string  ("icon",
                                                               "Icon",
                                                               "Icon",
                                                               NULL,
                                                               G_PARAM_READWRITE));
}

static void
sync_bookmark_properties (EggAction *action, EphyNode *bmk)
{
    const char *title, *location, *icon;
    gboolean smart_url;

    icon = ephy_node_get_property_string
        (bmk, EPHY_NODE_BMK_PROP_ICON);
    title = ephy_node_get_property_string
        (bmk, EPHY_NODE_BMK_PROP_TITLE);
    location = ephy_node_get_property_string
        (bmk, EPHY_NODE_BMK_PROP_LOCATION);
    smart_url = ephy_node_get_property_boolean
        (bmk, EPHY_NODE_BMK_PROP_HAS_SMART_ADDRESS);

    g_object_set (action,
              "label", title,
              "location", location,
              "smarturl", smart_url,
              "icon", icon,
              NULL);
}

static void
bookmarks_child_changed_cb (EphyNode *node, EphyNode *child, EggAction *action)
{
    gulong id;

    id = EPHY_BOOKMARK_ACTION (action)->priv->bookmark_id;

    if (id == ephy_node_get_id (child))
    {
        sync_bookmark_properties (action, child);
    }
}

static void
ephy_bookmark_action_init (EphyBookmarkAction *action)
{
    EphyBookmarks *bookmarks;
    EphyNode *node;

    action->priv = g_new0 (EphyBookmarkActionPrivate, 1);

    action->priv->location = NULL;
    action->priv->icon = NULL;


    bookmarks = ephy_shell_get_bookmarks (ephy_shell);
    node = ephy_bookmarks_get_bookmarks (bookmarks);
    ephy_node_signal_connect_object (node, EPHY_NODE_CHILD_CHANGED,
                         (EphyNodeCallback) bookmarks_child_changed_cb,
                         G_OBJECT (action));
}

EggAction *
ephy_bookmark_action_new (const char *name, guint id)
{
    EphyNode *bmk;
    EphyBookmarks *bookmarks;
    EggAction *action;

    bookmarks = ephy_shell_get_bookmarks (ephy_shell);

    bmk = ephy_bookmarks_get_from_id (bookmarks, id);
    g_return_val_if_fail (bmk != NULL, NULL);

    action =  EGG_ACTION (g_object_new (EPHY_TYPE_BOOKMARK_ACTION,
                            "name", name,
                        "bookmark_id", id,
                        NULL));

    sync_bookmark_properties (action, bmk);

    return action;
}