Replace strcmp() with purple_strequal()
[pidgin-git.git] / pidgin / plugins / mailchk.c
blobcd0b677034693c495f43b5c18be82543714effb9
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"
12 #define ANY_MAIL 0x01
13 #define UNREAD_MAIL 0x02
14 #define NEW_MAIL 0x04
16 static guint timer = 0;
17 static GtkWidget *mail = NULL;
19 static gint
20 check_mail()
22 static off_t oldsize = 0;
23 gchar *filename;
24 off_t newsize;
25 struct stat s;
26 gint ret = 0;
28 filename = g_strdup(g_getenv("MAIL"));
29 if (!filename)
30 filename = g_strconcat("/var/spool/mail/", g_get_user_name(), NULL);
32 if (g_stat(filename, &s) < 0) {
33 g_free(filename);
34 return -1;
37 newsize = s.st_size;
38 if (newsize) ret |= ANY_MAIL;
39 if (s.st_mtime > s.st_atime && newsize) ret |= UNREAD_MAIL;
40 if (newsize != oldsize && (ret & UNREAD_MAIL)) ret |= NEW_MAIL;
41 oldsize = newsize;
43 g_free(filename);
45 return ret;
48 static void
49 destroy_cb()
51 mail = NULL;
54 static gboolean
55 check_timeout(gpointer data)
57 gint count = check_mail();
58 PurpleBuddyList *list = purple_get_blist();
60 if (count == -1)
61 return FALSE;
63 if (!list || !PURPLE_IS_GTK_BLIST(list) || !(PIDGIN_BLIST(list)->vbox))
64 return TRUE;
66 if (!mail) {
67 /* guess we better build it then :P */
68 GtkWidget *vbox = PIDGIN_BLIST(list)->vbox;
70 mail = gtk_label_new("No mail messages.");
71 gtk_box_pack_start(GTK_BOX(vbox), mail, FALSE, FALSE, 0);
72 gtk_box_reorder_child(GTK_BOX(vbox), mail, 1);
73 g_signal_connect(G_OBJECT(mail), "destroy", G_CALLBACK(destroy_cb), NULL);
74 gtk_widget_show(mail);
77 if (count & NEW_MAIL)
78 purple_sound_play_event(PURPLE_SOUND_POUNCE_DEFAULT, NULL);
80 if (count & UNREAD_MAIL)
81 gtk_label_set_text(GTK_LABEL(mail), "You have new mail!");
82 else if (count & ANY_MAIL)
83 gtk_label_set_text(GTK_LABEL(mail), "You have mail.");
84 else
85 gtk_label_set_text(GTK_LABEL(mail), "No mail messages.");
87 return TRUE;
90 static void
91 signon_cb(PurpleConnection *gc)
93 PurpleBuddyList *list = purple_get_blist();
94 if (list && PURPLE_IS_GTK_BLIST(list) && !timer) {
95 check_timeout(NULL); /* we want the box to be drawn immediately */
96 timer = purple_timeout_add_seconds(2, check_timeout, NULL);
100 static void
101 signoff_cb(PurpleConnection *gc)
103 PurpleBuddyList *list = purple_get_blist();
104 if ((!list || !PURPLE_IS_GTK_BLIST(list) || !PIDGIN_BLIST(list)->vbox) && timer) {
105 purple_timeout_remove(timer);
106 timer = 0;
111 * EXPORTED FUNCTIONS
114 static gboolean
115 plugin_load(PurplePlugin *plugin)
117 PurpleBuddyList *list = purple_get_blist();
118 void *conn_handle = purple_connections_get_handle();
120 if (!check_timeout(NULL)) {
121 purple_debug_warning("mailchk", "Could not read $MAIL or /var/spool/mail/$USER\n");
122 return FALSE;
125 if (list && PURPLE_IS_GTK_BLIST(list) && PIDGIN_BLIST(list)->vbox)
126 timer = purple_timeout_add_seconds(2, check_timeout, NULL);
128 purple_signal_connect(conn_handle, "signed-on",
129 plugin, PURPLE_CALLBACK(signon_cb), NULL);
130 purple_signal_connect(conn_handle, "signed-off",
131 plugin, PURPLE_CALLBACK(signoff_cb), NULL);
133 return TRUE;
136 static gboolean
137 plugin_unload(PurplePlugin *plugin)
139 if (timer)
140 purple_timeout_remove(timer);
141 timer = 0;
142 if (mail)
143 gtk_widget_destroy(mail);
144 mail = NULL;
146 return TRUE;
149 static PurplePluginInfo info =
151 PURPLE_PLUGIN_MAGIC,
152 PURPLE_MAJOR_VERSION,
153 PURPLE_MINOR_VERSION,
154 PURPLE_PLUGIN_STANDARD,
155 PIDGIN_PLUGIN_TYPE,
157 NULL,
158 PURPLE_PRIORITY_DEFAULT,
159 MAILCHK_PLUGIN_ID,
160 N_("Mail Checker"),
161 DISPLAY_VERSION,
162 N_("Checks for new local mail."),
163 N_("Adds a small box to the buddy list that"
164 " shows if you have new mail."),
165 "Eric Warmenhoven <eric@warmenhoven.org>",
166 PURPLE_WEBSITE,
167 plugin_load,
168 plugin_unload,
169 NULL,
170 NULL,
171 NULL,
172 NULL,
173 NULL
176 static void
177 init_plugin(PurplePlugin *plugin)
181 PURPLE_INIT_PLUGIN(mailchk, init_plugin, info)