Merged pidgin/main into default
[pidgin-git.git] / libpurple / plugins / statenotify.c
blob2a91e82ac82ea55d3d77cbd08905f0379cd20577
1 #include "internal.h"
3 #include "buddylist.h"
4 #include "conversation.h"
5 #include "debug.h"
6 #include "signals.h"
7 #include "version.h"
9 #include "plugins.h"
10 #include "pluginpref.h"
11 #include "prefs.h"
13 #define STATENOTIFY_PLUGIN_ID "core-statenotify"
15 static void
16 write_status(PurpleBuddy *buddy, const char *message)
18 PurpleAccount *account = NULL;
19 PurpleIMConversation *im;
20 const char *who;
21 char buf[256];
22 char *escaped;
23 const gchar *buddy_name = NULL;
25 account = purple_buddy_get_account(buddy);
26 buddy_name = purple_buddy_get_name(buddy);
28 im = purple_conversations_find_im_with_account(buddy_name, account);
30 if (im == NULL)
31 return;
33 /* Prevent duplicate notifications for buddies in multiple groups */
34 if (buddy != purple_blist_find_buddy(account, buddy_name))
35 return;
37 who = purple_buddy_get_alias(buddy);
38 escaped = g_markup_escape_text(who, -1);
40 g_snprintf(buf, sizeof(buf), message, escaped);
41 g_free(escaped);
43 purple_conversation_write_system_message(PURPLE_CONVERSATION(im), buf,
44 PURPLE_MESSAGE_ACTIVE_ONLY | PURPLE_MESSAGE_NO_LINKIFY);
47 static void
48 buddy_status_changed_cb(PurpleBuddy *buddy, PurpleStatus *old_status,
49 PurpleStatus *status, void *data)
51 gboolean available, old_available;
53 if (!purple_status_is_exclusive(status) ||
54 !purple_status_is_exclusive(old_status))
55 return;
57 available = purple_status_is_available(status);
58 old_available = purple_status_is_available(old_status);
60 if (purple_prefs_get_bool("/plugins/core/statenotify/notify_away")) {
61 if (available && !old_available)
62 write_status(buddy, _("%s is no longer away."));
63 else if (!available && old_available)
64 write_status(buddy, _("%s has gone away."));
68 static void
69 buddy_idle_changed_cb(PurpleBuddy *buddy, gboolean old_idle, gboolean idle,
70 void *data)
72 if (purple_prefs_get_bool("/plugins/core/statenotify/notify_idle")) {
73 if (idle && !old_idle) {
74 write_status(buddy, _("%s has become idle."));
75 } else if (!idle && old_idle) {
76 write_status(buddy, _("%s is no longer idle."));
81 static void
82 buddy_signon_cb(PurpleBuddy *buddy, void *data)
84 if (purple_prefs_get_bool("/plugins/core/statenotify/notify_signon"))
85 write_status(buddy, _("%s has signed on."));
88 static void
89 buddy_signoff_cb(PurpleBuddy *buddy, void *data)
91 if (purple_prefs_get_bool("/plugins/core/statenotify/notify_signon"))
92 write_status(buddy, _("%s has signed off."));
95 static PurplePluginPrefFrame *
96 get_plugin_pref_frame(PurplePlugin *plugin)
98 PurplePluginPrefFrame *frame;
99 PurplePluginPref *ppref;
101 frame = purple_plugin_pref_frame_new();
103 ppref = purple_plugin_pref_new_with_label(_("Notify When"));
104 purple_plugin_pref_frame_add(frame, ppref);
106 ppref = purple_plugin_pref_new_with_name_and_label("/plugins/core/statenotify/notify_away", _("Buddy Goes _Away"));
107 purple_plugin_pref_frame_add(frame, ppref);
109 ppref = purple_plugin_pref_new_with_name_and_label("/plugins/core/statenotify/notify_idle", _("Buddy Goes _Idle"));
110 purple_plugin_pref_frame_add(frame, ppref);
112 ppref = purple_plugin_pref_new_with_name_and_label("/plugins/core/statenotify/notify_signon", _("Buddy _Signs On/Off"));
113 purple_plugin_pref_frame_add(frame, ppref);
115 return frame;
118 static PurplePluginInfo *
119 plugin_query(GError **error)
121 const gchar * const authors[] = {
122 "Christian Hammond <chipx86@gnupdate.org>",
123 NULL
126 return purple_plugin_info_new(
127 "id", STATENOTIFY_PLUGIN_ID,
128 "name", N_("Buddy State Notification"),
129 "version", DISPLAY_VERSION,
130 "category", N_("Notification"),
131 "summary", N_("Notifies in a conversation window when a "
132 "buddy goes or returns from away or idle."),
133 "description", N_("Notifies in a conversation window when a "
134 "buddy goes or returns from away or idle."),
135 "authors", authors,
136 "website", PURPLE_WEBSITE,
137 "abi-version", PURPLE_ABI_VERSION,
138 "pref-frame-cb", get_plugin_pref_frame,
139 NULL
143 static gboolean
144 plugin_load(PurplePlugin *plugin, GError **error)
146 void *blist_handle = purple_blist_get_handle();
148 purple_prefs_add_none("/plugins/core/statenotify");
149 purple_prefs_add_bool("/plugins/core/statenotify/notify_away", TRUE);
150 purple_prefs_add_bool("/plugins/core/statenotify/notify_idle", TRUE);
151 purple_prefs_add_bool("/plugins/core/statenotify/notify_signon", TRUE);
153 purple_signal_connect(blist_handle, "buddy-status-changed", plugin,
154 PURPLE_CALLBACK(buddy_status_changed_cb), NULL);
155 purple_signal_connect(blist_handle, "buddy-idle-changed", plugin,
156 PURPLE_CALLBACK(buddy_idle_changed_cb), NULL);
157 purple_signal_connect(blist_handle, "buddy-signed-on", plugin,
158 PURPLE_CALLBACK(buddy_signon_cb), NULL);
159 purple_signal_connect(blist_handle, "buddy-signed-off", plugin,
160 PURPLE_CALLBACK(buddy_signoff_cb), NULL);
162 return TRUE;
165 static gboolean
166 plugin_unload(PurplePlugin *plugin, GError **error)
168 return TRUE;
171 PURPLE_PLUGIN_INIT(statenotify, plugin_query, plugin_load, plugin_unload);