File: user.c

package info (click to toggle)
fbpanel 7.0-3
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 5,000 kB
  • ctags: 2,013
  • sloc: ansic: 11,684; sh: 404; python: 382; makefile: 315
file content (141 lines) | stat: -rw-r--r-- 3,289 bytes parent folder | download | duplicates (3)
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

#include "misc.h"
#include "run.h"
#include "../menu/menu.h"
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <signal.h>


//#define DEBUGPRN
#include "dbg.h"

typedef struct {
    menu_priv chart;
    gint dummy;
    guint sid;
    GPid pid;
} user_priv;

static menu_class *k;


static void user_destructor(plugin_instance *p);

#define GRAVATAR_LEN  300

static void
fetch_gravatar_done(GPid pid, gint status, gpointer data)
{
    user_priv *c G_GNUC_UNUSED = data;
    plugin_instance *p G_GNUC_UNUSED = data;
    gchar *image = NULL, *icon = NULL;

    ENTER;
    DBG("status %d\n", status);
    g_spawn_close_pid(c->pid);
    c->pid = 0;
    c->sid = 0;

    if (status)
        RET();
    DBG("rebuild menu\n");
    XCG(p->xc, "icon", &icon, strdup);
    XCG(p->xc, "image", &image, strdup);
    XCS(p->xc, "image", image, value);
    xconf_del(xconf_find(p->xc, "icon", 0), FALSE);
    PLUGIN_CLASS(k)->destructor(p);
    PLUGIN_CLASS(k)->constructor(p);
    if (image) {
        XCS(p->xc, "image", image, value);
        g_free(image);
    }
    if (icon) {
        XCS(p->xc, "icon", icon, value);
        g_free(icon);
    }
    RET();
}


static gboolean
fetch_gravatar(gpointer data)
{
    user_priv *c G_GNUC_UNUSED = data;
    plugin_instance *p G_GNUC_UNUSED = data;
    GChecksum *cs;
    gchar *gravatar = NULL;
    gchar buf[GRAVATAR_LEN];
    // FIXME: select more secure path
    gchar *image = "/tmp/gravatar";
    gchar *argv[] = { "wget", "-q", "-O", image, buf, NULL };

    ENTER;
    cs = g_checksum_new(G_CHECKSUM_MD5);
    XCG(p->xc, "gravataremail", &gravatar, str);
    g_checksum_update(cs, (guchar *) gravatar, -1);
    snprintf(buf, sizeof(buf), "http://www.gravatar.com/avatar/%s",
        g_checksum_get_string(cs));
    g_checksum_free(cs);
    DBG("gravatar '%s'\n", buf);
    c->pid = run_app_argv(argv);
    c->sid = g_child_watch_add(c->pid, fetch_gravatar_done, data);
    RET(FALSE);
}


static int
user_constructor(plugin_instance *p)
{
    user_priv *c G_GNUC_UNUSED = (user_priv *) p;
    gchar *image = NULL;
    gchar *icon = NULL;
    gchar *gravatar = NULL;

    ENTER;
    if (!(k = class_get("menu")))
        RET(0);
    XCG(p->xc, "image", &image, str);
    XCG(p->xc, "icon", &icon, str);
    if (!(image || icon))
        XCS(p->xc, "icon", "avatar-default", value);
    if (!PLUGIN_CLASS(k)->constructor(p))
        RET(0);
    XCG(p->xc, "gravataremail", &gravatar, str);
    DBG("gravatar email '%s'\n", gravatar);
    if (gravatar)
        g_timeout_add(300, fetch_gravatar, p);
    gtk_widget_set_tooltip_markup(p->pwid, "<b>User</b>");
    RET(1);
}


static void
user_destructor(plugin_instance *p)
{
    user_priv *c G_GNUC_UNUSED = (user_priv *) p;

    ENTER;
    PLUGIN_CLASS(k)->destructor(p);
    if (c->pid)
        kill(c->pid, SIGKILL);
    if (c->sid)
        g_source_remove(c->sid);
    class_put("menu");
    RET();
}


static plugin_class class = {
    .count       = 0,
    .type        = "user",
    .name        = "User menu",
    .version     = "1.0",
    .description = "User photo and menu of user actions",
    .priv_size   = sizeof(user_priv),

    .constructor = user_constructor,
    .destructor  = user_destructor,
};
static plugin_class *class_ptr = (plugin_class *) &class;