aboutsummaryrefslogtreecommitdiffstats
path: root/libempathy/empathy-avatar.c
blob: fe4e613c614a41f8c337c2276bba9559a5fc7271 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2002-2007 Imendio AB
 *
 * 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.
 *
 * Authors: Martyn Russell <martyn@imendio.com>
 *          Xavier Claessens <xclaesse@gmail.com>
 */

#include "config.h"

#include "empathy-avatar.h"
#include "empathy-utils.h"
#include "empathy-debug.h"

#define DEBUG_DOMAIN "Avatar"

GType
empathy_avatar_get_type (void)
{
    static GType type_id = 0;

    if (!type_id) {
        type_id = g_boxed_type_register_static ("EmpathyAvatar",
                            (GBoxedCopyFunc) empathy_avatar_ref,
                            (GBoxedFreeFunc) empathy_avatar_unref);
    }

    return type_id;
}

static gchar *
avatar_get_filename (const gchar *token)
{
    gchar *avatar_path;
    gchar *avatar_file;
    gchar *token_escaped;

    avatar_path = g_build_filename (g_get_home_dir (),
                    ".gnome2",
                    PACKAGE_NAME,
                    "avatars",
                    NULL);
    g_mkdir_with_parents (avatar_path, 0700);

    token_escaped = empathy_escape_as_identifier (token);
    avatar_file = g_build_filename (avatar_path, token_escaped, NULL);

    g_free (token_escaped);
    g_free (avatar_path);

    return avatar_file;
}

static EmpathyAvatar *
avatar_new (guchar *data,
        gsize   len,
        gchar  *format,
        gchar  *token)
{
    EmpathyAvatar *avatar;

    avatar = g_slice_new0 (EmpathyAvatar);
    avatar->data = data;
    avatar->len = len;
    avatar->format = format;
    avatar->token = token;
    avatar->refcount = 1;

    return avatar;
}

EmpathyAvatar *
empathy_avatar_new (const guchar *data,
            const gsize   len,
            const gchar  *format,
            const gchar  *token)
{
    EmpathyAvatar *avatar;
    gchar         *filename;
    GError        *error = NULL;

    g_return_val_if_fail (data != NULL, NULL);
    g_return_val_if_fail (len > 0, NULL);
    g_return_val_if_fail (format != NULL, NULL);
    g_return_val_if_fail (!G_STR_EMPTY (token), NULL);

    avatar = avatar_new (g_memdup (data, len),
                 len,
                 g_strdup (format),
                 g_strdup (token));

    /* Save to cache if not yet in it */
    filename = avatar_get_filename (token);
    if (!g_file_test (filename, G_FILE_TEST_EXISTS)) {
        if (!g_file_set_contents (filename, data, len, &error)) {
            empathy_debug (DEBUG_DOMAIN,
                       "Failed to save avatar in cache: %s",
                       error ? error->message : "No error given");
            g_clear_error (&error);
        } else {
            empathy_debug (DEBUG_DOMAIN, "Avatar saved to %s", filename);
        }
    }
    g_free (filename);

    return avatar;
}

EmpathyAvatar *
empathy_avatar_new_from_cache (const gchar *token)
{
    EmpathyAvatar *avatar = NULL;
    gchar         *filename;
    gchar         *data = NULL;
    gsize          len;
    GError        *error = NULL;

    g_return_val_if_fail (!G_STR_EMPTY (token), NULL);

    /* Load the avatar from file if it exists */
    filename = avatar_get_filename (token);
    if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
        if (!g_file_get_contents (filename, &data, &len, &error)) {
            empathy_debug (DEBUG_DOMAIN,
                       "Failed to load avatar from cache: %s",
                       error ? error->message : "No error given");
            g_clear_error (&error);
        }
    }

    if (data) {
        empathy_debug (DEBUG_DOMAIN, "Avatar loaded from %s", filename);
        avatar = avatar_new (data, len, NULL, g_strdup (token));
    }

    g_free (filename);

    return avatar;
}

void
empathy_avatar_unref (EmpathyAvatar *avatar)
{
    g_return_if_fail (avatar != NULL);

    avatar->refcount--;
    if (avatar->refcount == 0) {
        g_free (avatar->data);
        g_free (avatar->format);
        g_free (avatar->token);
        g_slice_free (EmpathyAvatar, avatar);
    }
}

EmpathyAvatar *
empathy_avatar_ref (EmpathyAvatar *avatar)
{
    g_return_val_if_fail (avatar != NULL, NULL);

    avatar->refcount++;

    return avatar;
}