aboutsummaryrefslogtreecommitdiffstats
path: root/lib/toolbar/ephy-toolbar-item-factory.c
blob: 60cfbbc4189d88abfa4665f20f0d269e4e49cc51 (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
/*
 *  Copyright (C) 2002  Ricardo Fernández Pascual
 *
 *  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 "ephy-toolbar-item-factory.h"
#include <string.h>

#include "ephy-tbi-zoom.h"
#include "ephy-tbi-separator.h"
#include "ephy-tbi-favicon.h"
#include "ephy-tbi-spinner.h"
#include "ephy-tbi-location.h"
#include "ephy-tbi-navigation-history.h"
#include "ephy-tbi-std-toolitem.h"

#define NOT_IMPLEMENTED g_warning ("not implemented: " G_STRLOC);
//#define DEBUG_MSG(x) g_print x
#define DEBUG_MSG(x)

typedef struct
{
    const char *type_name;
    EphyTbItemConstructor constructor;
} EphyTbItemTypeInfo;

static EphyTbItemTypeInfo ephy_tb_item_default_types[] =
{
    { "std_toolitem",       (EphyTbItemConstructor) ephy_tbi_std_toolitem_new },
    { "navigation_history",     (EphyTbItemConstructor) ephy_tbi_navigation_history_new },
    { "zoom",           (EphyTbItemConstructor) ephy_tbi_zoom_new },
    { "location",           (EphyTbItemConstructor) ephy_tbi_location_new },
    { "spinner",            (EphyTbItemConstructor) ephy_tbi_spinner_new },
    { "favicon",            (EphyTbItemConstructor) ephy_tbi_favicon_new },
    { "separator",          (EphyTbItemConstructor) ephy_tbi_separator_new },
    { NULL,             NULL }
};

static GHashTable *ephy_tb_item_known_types = NULL;

static void
ephy_tb_item_factory_init (void)
{
    if (ephy_tb_item_known_types == NULL)
    {
        int i;
        ephy_tb_item_known_types = g_hash_table_new (g_str_hash, g_str_equal);

        for (i = 0; ephy_tb_item_default_types[i].type_name; ++i)
        {
            ephy_toolbar_item_register_type (ephy_tb_item_default_types[i].type_name,
                             ephy_tb_item_default_types[i].constructor);
        }
    }
}

EphyTbItem *
ephy_toolbar_item_create_from_string (const gchar *str)
{
    EphyTbItem *ret = NULL;
    gchar *type;
    gchar *props;
    gchar *id;
    const gchar *rest;
    const gchar *lpar;
    const gchar *rpar;
    const gchar *eq;
    EphyTbItemConstructor constructor;

    ephy_tb_item_factory_init ();

    rest = str;

    eq = strchr (rest, '=');
    if (eq)
    {
        id = g_strndup (rest, eq - rest);
        rest = eq + 1;
    }
    else
    {
        id = NULL;
    }

    lpar = strchr (rest, '(');
    if (lpar)
    {
        type = g_strndup (rest, lpar - rest);
        rest = lpar + 1;

        rpar = strchr (rest, ')');
        if (rpar)
        {
            props = g_strndup (rest, rpar - rest);
            rest = rpar + 1;
        }
        else
        {
            props = g_strdup (rest);
        }
    }
    else
    {
        type = g_strdup (rest);
        props = NULL;
    }

    DEBUG_MSG (("ephy_toolbar_item_create_from_string id=%s type=%s props=%s\n", id, type, props));

    constructor = g_hash_table_lookup (ephy_tb_item_known_types, type);

    if (constructor)
    {
        ret = constructor ();
        if (id)
        {
            ephy_tb_item_set_id (ret, id);
        }
        if (props)
        {
            ephy_tb_item_parse_properties (ret, props);
        }
    }

    if (!ret)
    {
        g_warning ("Error creating toolbar item of type %s", type);
    }

    if (id)
    {
        g_free (id);
    }
    if (type)
    {
        g_free (type);
    }
    if (props)
    {
        g_free (props);
    }

    return ret;
}

void
ephy_toolbar_item_register_type (const gchar *type, EphyTbItemConstructor constructor)
{
    ephy_tb_item_factory_init ();
    g_hash_table_insert (ephy_tb_item_known_types, g_strdup (type), constructor);
}