Replace functions which called once with their bodies
[pidgin-git.git] / pidgin / plugins / mailchk.c
blob68fe1d8e68b660b5c77dc3e5f71b395c4188a015
1 #include "internal.h"
3 #include "debug.h"
4 #include "sound.h"
5 #include "version.h"
7 #include "gtkblist.h"
8 #include "gtkplugin.h"
10 #define MAILCHK_PLUGIN_ID "gtk-mailchk"
11 #define MAILCHK_PLUGIN_DOMAIN (g_quark_from_static_string(MAILCHK_PLUGIN_ID))
13 #define ANY_MAIL 0x01
14 #define UNREAD_MAIL 0x02
15 #define NEW_MAIL 0x04
17 static guint timer = 0;
18 static GtkWidget *mail = NULL;
20 static gint
21 check_mail()
23 static off_t oldsize = 0;
24 gchar *filename;
25 off_t newsize;
26 GStatBuf st;
27 gint ret = 0;
29 filename = g_strdup(g_getenv("MAIL"));
30 if (!filename)
31 filename = g_strconcat("/var/spool/mail/", g_get_user_name(), NULL);
33 if (g_stat(filename, &st) < 0) {
34 g_free(filename);
35 return -1;
38 newsize = st.st_size;
39 if (newsize) ret |= ANY_MAIL;
40 if (st.st_mtime > st.st_atime && newsize) ret |= UNREAD_MAIL;
41 if (newsize != oldsize && (ret & UNREAD_MAIL)) ret |= NEW_MAIL;
42 oldsize = newsize;
44 g_free(filename);
46 return ret;
49 static void
50 destroy_cb()
52 mail = NULL;
55 static gboolean
56 check_timeout(gpointer data)
58 gint count = check_mail();
59 PurpleBuddyList *list = purple_blist_get_default();
61 if (count == -1)
62 return FALSE;
64 if (!list || !(PIDGIN_BLIST(list)->vbox))
65 return TRUE;
67 if (!mail) {
68 /* guess we better build it then :P */
69 GtkWidget *vbox = PIDGIN_BLIST(list)->vbox;
71 mail = gtk_label_new("No mail messages.");
72 gtk_box_pack_start(GTK_BOX(vbox), mail, FALSE, FALSE, 0);
73 gtk_box_reorder_child(GTK_BOX(vbox), mail, 1);
74 g_signal_connect(G_OBJECT(mail), "destroy", G_CALLBACK(destroy_cb), NULL);
75 gtk_widget_show(mail);
78 if (count & NEW_MAIL)
79 purple_sound_play_event(PURPLE_SOUND_POUNCE_DEFAULT, NULL);
81 if (count & UNREAD_MAIL)
82 gtk_label_set_text(GTK_LABEL(mail), "You have new mail!");
83 else if (count & ANY_MAIL)
84 gtk_label_set_text(GTK_LABEL(mail), "You have mail.");
85 else
86 gtk_label_set_text(GTK_LABEL(mail), "No mail messages.");
88 return TRUE;
91 static void
92 signon_cb(PurpleConnection *gc)
94 PurpleBuddyList *list = purple_blist_get_default();
95 if (list && !timer) {
96 check_timeout(NULL); /* we want the box to be drawn immediately */
97 timer = g_timeout_add_seconds(2, check_timeout, NULL);
101 static void
102 signoff_cb(PurpleConnection *gc)
104 PurpleBuddyList *list = purple_blist_get_default();
105 if ((!list || !PIDGIN_BLIST(list)->vbox) && timer) {
106 g_source_remove(timer);
107 timer = 0;
112 * EXPORTED FUNCTIONS
115 static PidginPluginInfo *
116 plugin_query(GError **error)
118 const gchar * const authors[] = {
119 "Eric Warmenhoven <eric@warmenhoven.org>",
120 NULL
123 return pidgin_plugin_info_new(
124 "id", MAILCHK_PLUGIN_ID,
125 "name", N_("Mail Checker"),
126 "version", DISPLAY_VERSION,
127 "category", N_("Utility"),
128 "summary", N_("Checks for new local mail."),
129 "description", N_("Adds a small box to the buddy list that shows if "
130 "you have new mail."),
131 "authors", authors,
132 "website", PURPLE_WEBSITE,
133 "abi-version", PURPLE_ABI_VERSION,
134 NULL
138 static gboolean
139 plugin_load(PurplePlugin *plugin, GError **error)
141 PurpleBuddyList *list = purple_blist_get_default();
142 void *conn_handle = purple_connections_get_handle();
144 if (!check_timeout(NULL)) {
145 g_set_error(error, MAILCHK_PLUGIN_DOMAIN, 0, _("Could not read $MAIL "
146 "or /var/spool/mail/$USER\n"));
147 return FALSE;
150 if (list && PIDGIN_BLIST(list)->vbox)
151 timer = g_timeout_add_seconds(2, check_timeout, NULL);
153 purple_signal_connect(conn_handle, "signed-on",
154 plugin, PURPLE_CALLBACK(signon_cb), NULL);
155 purple_signal_connect(conn_handle, "signed-off",
156 plugin, PURPLE_CALLBACK(signoff_cb), NULL);
158 return TRUE;
161 static gboolean
162 plugin_unload(PurplePlugin *plugin, GError **error)
164 if (timer)
165 g_source_remove(timer);
166 timer = 0;
167 if (mail)
168 gtk_widget_destroy(mail);
169 mail = NULL;
171 return TRUE;
174 PURPLE_PLUGIN_INIT(mailchk, plugin_query, plugin_load, plugin_unload);