Create interfaces for string-seeded blinking
[darcs-mirror-pidgin-blinklight.debian.git] / src / pidgin-blinklight.c
1 /* © 2005-2007 Joachim Breitner
2  *
3  * This program is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU General Public License
5  * as published by the Free Software Foundation; either version 2
6  * of the License, or (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16  */
17 #ifdef HAVE_CONFIG_H
18 # include "../config.h"
19 #endif
20
21 #define PURPLE_PLUGINS
22
23 #include <unistd.h>
24
25 #ifdef ENABLE_NLS
26 #  include <locale.h>
27 #  include <libintl.h>
28 #  define _(x) dgettext(PACKAGE, x)
29 #  ifdef dgettext_noop
30 #    define N_(String) dgettext_noop (PACKAGE, String)
31 #  else
32 #    define N_(String) (String)
33 #  endif
34 #else
35 #  include <locale.h>
36 #  define N_(String) (String)
37 #  define _(x) (x)
38 #  define ngettext(Singular, Plural, Number) ((Number == 1) ? (Singular) : (Plural))
39 #endif
40
41
42
43 #include <debug.h>
44 // #include <pidgin.h>
45 #include <plugin.h>
46 #include <conversation.h>
47 #include <version.h>
48 #include <eventloop.h>
49 #include <string.h>
50
51 #include "blink.h"
52
53 guint
54 blinklight_timeout_add (guint interval, GSourceFunc function, gpointer data) {
55         return purple_timeout_add(interval, function, data);
56 }
57
58 static void
59 gt_blink(PurpleAccount *account, const gchar *sender,
60          const gchar *message, int flags, gpointer data)
61 {
62         if (purple_prefs_get_bool("/plugins/core/pidgin-blinklight/focus"))
63         {
64                 // Blink in any case
65                 blinklight_startblink(NULL);
66         } else {
67                 // Only blink when not having focus
68                 PurpleConversation *conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_ANY, sender, account);
69                 if (! purple_conversation_has_focus(conv)) 
70                         blinklight_startblink(NULL);
71         }
72 }
73
74 static PurplePluginPrefFrame *
75 get_plugin_pref_frame(PurplePlugin *plugin)
76 {
77         PurplePluginPrefFrame *frame;
78         PurplePluginPref *ppref;
79
80         frame = purple_plugin_pref_frame_new();
81
82         ppref = purple_plugin_pref_new_with_name_and_label(
83                 "/plugins/core/pidgin-blinklight/focus",
84                 _("Blink even when conversation has the _focus.")
85         );
86
87         purple_plugin_pref_frame_add(frame, ppref);
88        
89         return frame;
90 }
91
92 static PurplePluginUiInfo prefs_info = 
93 {
94         get_plugin_pref_frame
95 };
96
97 static gboolean
98 gt_load(PurplePlugin *plugin) {
99
100         // Make /proc file writable
101         if (!fork())
102                 execl("/usr/lib/pidgin-blinklight/blinklight-fixperm","blinklight-fixperm",NULL);
103         
104
105         char *chosen_file = blinklight_init();
106         if (chosen_file == NULL) {
107                 purple_debug_info("pidgin-blinklight","no suitable file found, deactivating plugin.");
108         } else {
109                 purple_debug_info("pidgin-blinklight","chose file %s.\n", chosen_file);
110                 purple_signal_connect(
111                         purple_conversations_get_handle(),
112                         "received-im-msg",
113                         plugin,
114                         PURPLE_CALLBACK(gt_blink),
115                         NULL
116                 );
117         }
118
119         // purple_debug_info("pidgin-blinklight","pidgin-blinklight has loaded\n");
120         return TRUE;
121 }
122
123 static gboolean
124 gt_unload(PurplePlugin *plugin) {
125
126         // purple_debug_info("pidgin-blinklight","pidgin-blinklight has unloaded\n");
127         return TRUE;
128 }
129
130 static PurplePluginInfo gt_info = {
131         PURPLE_PLUGIN_MAGIC,                                                                    /* plugin magic         */
132         PURPLE_MAJOR_VERSION,                                                                   /* major purple version */
133         PURPLE_MINOR_VERSION,                                                                   /* minor purple version */
134         PURPLE_PLUGIN_STANDARD,                                                                 /* type                 */
135         NULL,                                                                                   /* ui requirement       */
136         0,                                                                                      /* flags                */
137         NULL,                                                                                   /* dependencies         */
138         PURPLE_PRIORITY_DEFAULT,                                                                        /* priority             */
139         "gtk-nomeata-blinklight",                                                               /* id                   */
140         NULL,                                                                                   /* name                 */
141         VERSION,                                                                                /* version              */
142         NULL,                                                                                   /* summary              */
143         NULL,                                                                                   /* description          */
144
145         "Joachim Breitner <mail@joachim-breitner.de>",                                          /* author               */
146         "http://www.joachim-breitner.de/blog/",                                                                         /* homepage             */
147
148         gt_load,                                                                                /* load                 */
149         gt_unload,                                                                              /* unload               */
150         NULL,                                                                                   /* destroy              */
151
152         NULL,                                                                                   /* ui info              */
153         NULL,                                                                                   /* extra info           */
154         &prefs_info,                                                                            /* plugin prefs         */
155         NULL                                                                                    /* plugin actions       */
156 };
157
158 static void
159 init_plugin(PurplePlugin *plugin) {
160
161         purple_prefs_add_none("/plugins/core/pidgin-blinklight");
162         purple_prefs_add_bool("/plugins/core/pidgin-blinklight/focus", TRUE);
163
164 #ifdef ENABLE_NLS
165         bindtextdomain(PACKAGE, LOCALEDIR);
166         bind_textdomain_codeset(PACKAGE, "UTF-8");
167 #endif
168
169         gt_info.name            = _("Pidgin-Blinklight");
170         gt_info.summary         = _("Flickering Messages");
171         gt_info.description     = _(    "Pidgin-Blinklight:\n"
172                                         "Flashes the Blinklight upon new messages\n"
173                                         "To use this, you need to have the ibm-acpi or asus-laptop "
174                                         "kernel module loaded.");
175
176         // purple_debug_info("pidgin-blinklight","pidgin-blinklight has init'ed");
177 }
178
179 PURPLE_INIT_PLUGIN(Pidgin-Blinklight, init_plugin, gt_info)