aboutsummaryrefslogtreecommitdiffstats
path: root/shell/e-folder-type-repository.c
blob: c96ab5a5b6dbebdbd7281da6be400f34b2edfc97 (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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* e-folder-type-repository.c
 *
 * Copyright (C) 2000  Helix Code, Inc.
 *
 * 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 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, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Ettore Perazzoli
 */

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

#include <glib.h>
#include <gtk/gtktypeutils.h>

#include "e-util/e-util.h"

#include "e-shell-utils.h"

#include "e-folder-type-repository.h"


#define PARENT_TYPE GTK_TYPE_OBJECT
static GtkObjectClass *parent_class = NULL;

struct _FolderType {
    char *name;
    char *icon_name;
    GdkPixbuf *icon_pixbuf;
    char *control_id;
};
typedef struct _FolderType FolderType;

struct _EFolderTypeRepositoryPrivate {
    GHashTable *name_to_type;
};


/* FIXME these are hardcoded for now.  */

#ifdef USING_OAF
#       define CALENDAR_CONTROL_ID "OAFIID:control:calendar:dd34ddae-25c6-486b-a8a8-3e8f0286b54c"
#       define CONTACTS_CONTROL_ID "OAFIID:control:addressbook:851f883b-2fe7-4c94-a1e3-a1f2a7a03c49"
#       define MAIL_CONTROL_ID     "OAFIID:control:evolution-mail:833d5a71-a201-4a0e-b7e6-5475c5c4cb45"
#else
#       define CALENDAR_CONTROL_ID "control:calendar"
#       define CONTACTS_CONTROL_ID "control:addressbook"
#       define MAIL_CONTROL_ID     "control:evolution-mail"
#endif


/* FolderType handling.  */

static FolderType *
folder_type_new (const char *name,
         const char *icon_name,
         const char *control_id)
{
    FolderType *new;
    char *icon_path;

    new = g_new (FolderType, 1);

    new->name       = g_strdup (name);
    new->icon_name  = g_strdup (icon_name);
    new->control_id = g_strdup (control_id);

    icon_path = e_shell_get_icon_path (icon_name);
    if (icon_path == NULL)
        new->icon_pixbuf = NULL;
    else
        new->icon_pixbuf = gdk_pixbuf_new_from_file (icon_path);

    return new;
}

static void
folder_type_free (FolderType *folder_type)
{
    g_free (folder_type->name);
    g_free (folder_type->icon_name);
    g_free (folder_type->control_id);

    if (folder_type->icon_pixbuf != NULL)
        gdk_pixbuf_unref (folder_type->icon_pixbuf);

    g_free (folder_type);
}

static const FolderType *
get_folder_type (EFolderTypeRepository *folder_type_repository,
         const char *type_name)
{
    EFolderTypeRepositoryPrivate *priv;

    priv = folder_type_repository->priv;

    return g_hash_table_lookup (priv->name_to_type, type_name);
}

static gboolean
add_folder_type (EFolderTypeRepository *folder_type_repository,
         const char *name,
         const char *icon_name,
         const char *control_id)
{
    EFolderTypeRepositoryPrivate *priv;
    FolderType *folder_type;

    priv = folder_type_repository->priv;

    /* Make sure we don't add the same type twice.  */
    if (get_folder_type (folder_type_repository, name) != NULL)
        return FALSE;

    folder_type = folder_type_new (name, icon_name, control_id);
    g_hash_table_insert (priv->name_to_type, folder_type->name, folder_type);

    return TRUE;
}


/* GtkObject methods.  */

static void
hash_forall_free_folder_type (gpointer key,
                  gpointer value,
                  gpointer data)
{
    FolderType *folder_type;

    folder_type = (FolderType *) value;
    folder_type_free (folder_type);
}

static void
destroy (GtkObject *object)
{
    EFolderTypeRepository *folder_type_repository;
    EFolderTypeRepositoryPrivate *priv;

    folder_type_repository = E_FOLDER_TYPE_REPOSITORY (object);
    priv = folder_type_repository->priv;

    g_hash_table_foreach (priv->name_to_type, hash_forall_free_folder_type, NULL);
    g_hash_table_destroy (priv->name_to_type);

    g_free (priv);

    (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
}


static void
class_init (EFolderTypeRepositoryClass *class)
{
    GtkObjectClass *object_class;

    object_class = GTK_OBJECT_CLASS (class);
    object_class->destroy = destroy;

    parent_class = gtk_type_class (gtk_object_get_type ());
}

static void
init (EFolderTypeRepository *folder_type_repository)
{
    EFolderTypeRepositoryPrivate *priv;

    priv = g_new (EFolderTypeRepositoryPrivate, 1);
    priv->name_to_type = g_hash_table_new (g_str_hash, g_str_equal);

    folder_type_repository->priv = priv;
}


void
e_folder_type_repository_construct (EFolderTypeRepository *folder_type_repository)
{
    g_return_if_fail (folder_type_repository != NULL);
    g_return_if_fail (E_IS_FOLDER_TYPE_REPOSITORY (folder_type_repository));

    GTK_OBJECT_UNSET_FLAGS (GTK_OBJECT (folder_type_repository), GTK_FLOATING);

    /* FIXME these are hardcoded for now.  */

    add_folder_type (folder_type_repository,
             "mail", "evolution-inbox.png", MAIL_CONTROL_ID);
    add_folder_type (folder_type_repository,
             "calendar", "evolution-calendar.png", CALENDAR_CONTROL_ID);
    add_folder_type (folder_type_repository,
             "contacts", "evolution-contacts.png", CONTACTS_CONTROL_ID);
}

EFolderTypeRepository *
e_folder_type_repository_new (void)
{
    EFolderTypeRepository *new;

    new = gtk_type_new (e_folder_type_repository_get_type ());

    e_folder_type_repository_construct (new);

    return new;
}


const char *
e_folder_type_repository_get_icon_name_for_type (EFolderTypeRepository *folder_type_repository,
                         const char *type_name)
{
    const FolderType *folder_type;

    g_return_val_if_fail (folder_type_repository != NULL, NULL);
    g_return_val_if_fail (E_IS_FOLDER_TYPE_REPOSITORY (folder_type_repository), NULL);
    g_return_val_if_fail (type_name != NULL, NULL);

    folder_type = get_folder_type (folder_type_repository, type_name);
    if (folder_type == NULL) {
        g_warning ("%s: Unknown type -- %s", __FUNCTION__, type_name);
        return NULL;
    }

    return folder_type->icon_name;
}

GdkPixbuf *
e_folder_type_repository_get_icon_for_type (EFolderTypeRepository *folder_type_repository,
                        const char *type_name)
{
    const FolderType *folder_type;

    g_return_val_if_fail (folder_type_repository != NULL, NULL);
    g_return_val_if_fail (E_IS_FOLDER_TYPE_REPOSITORY (folder_type_repository), NULL);
    g_return_val_if_fail (type_name != NULL, NULL);

    folder_type = get_folder_type (folder_type_repository, type_name);
    if (folder_type == NULL) {
        g_warning ("%s: Unknown type -- %s", __FUNCTION__, type_name);
        return NULL;
    }

    return folder_type->icon_pixbuf;
}

const char *
e_folder_type_repository_get_control_id_for_type (EFolderTypeRepository *folder_type_repository,
                          const char *type_name)
{
    const FolderType *folder_type;

    g_return_val_if_fail (folder_type_repository != NULL, NULL);
    g_return_val_if_fail (E_IS_FOLDER_TYPE_REPOSITORY (folder_type_repository), NULL);
    g_return_val_if_fail (type_name != NULL, NULL);

    folder_type = get_folder_type (folder_type_repository, type_name);
    if (folder_type == NULL) {
        g_warning ("%s: Unknown type -- %s", __FUNCTION__, type_name);
        return NULL;
    }

    return folder_type->control_id;
}


E_MAKE_TYPE (e_folder_type_repository, "EFolderTypeRepository", EFolderTypeRepository,
         class_init, init, PARENT_TYPE)