initial git release
[lcreminder] / src / lens-cover-reminder-sp.c
1 /*
2  *  lenscover reminder statusbar plugin.
3  *  Copyright (C) 2010 Nicolai Hess
4  *  
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *  
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *  
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #include <fcntl.h>
21 #include <gtk/gtk.h>
22 #include <hildon/hildon.h>
23 #include <mce/dbus-names.h>
24 #include <mce/mode-names.h>
25
26 #include "lens-cover-reminder-sp.h"
27
28 #define LENS_COVER_STATUS_PLUGIN_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE(obj, \
29                                                                                TYPE_LENS_COVER_STATUS_PLUGIN, LensCoverStatusPluginPrivate))
30
31
32 struct _LensCoverStatusPluginPrivate
33 {
34   DBusGConnection *dbus_conn;
35   DBusGProxy *dbus_proxy_cover;
36   DBusGProxy *dbus_proxy_lock;
37 };
38
39 HD_DEFINE_PLUGIN_MODULE(LensCoverStatusPlugin, lens_cover_status_plugin, HD_TYPE_STATUS_MENU_ITEM);
40
41 static gboolean
42 lens_cover_status_plugin_cover_is_open()
43 {
44   int fd;
45   char state[7];
46   fd = open("/sys/devices/platform/gpio-switch/cam_shutter/state", O_RDONLY);
47   if(fd)
48   {
49     read(fd, state, 7 );
50     if(!g_str_has_prefix(state, "closed"))
51     {
52       return TRUE;
53     }
54     close( fd );
55   }
56   return FALSE;
57 }
58
59 static void
60 lens_cover_status_plugin_show_icon(LensCoverStatusPlugin* plugin)
61 {
62   GdkPixbuf* pixbuf = gtk_icon_theme_load_icon(gtk_icon_theme_get_default(), "statusarea_lens_cover_reminder", 18, 0, NULL);
63   if(pixbuf)
64   {
65     hd_status_plugin_item_set_status_area_icon(HD_STATUS_PLUGIN_ITEM(plugin), pixbuf);
66     g_object_unref(pixbuf);
67   }
68 }
69
70 static void
71 lens_cover_status_plugin_hide_icon(LensCoverStatusPlugin* plugin)
72 {
73   hd_status_plugin_item_set_status_area_icon(HD_STATUS_PLUGIN_ITEM(plugin), NULL);
74 }
75
76 static void
77 lens_cover_status_plugin_vibrate(LensCoverStatusPlugin* plugin,
78                                  gboolean activate)
79 {
80   static gchar* pattern = "PatternIncomingMessage";
81   const gchar* method = (activate) ? MCE_ACTIVATE_VIBRATOR_PATTERN : MCE_DEACTIVATE_VIBRATOR_PATTERN;
82   if(plugin->priv->dbus_conn)
83   {
84     DBusGProxy* proxy = dbus_g_proxy_new_for_name(plugin->priv->dbus_conn,
85                                                   MCE_SERVICE,
86                                                   MCE_REQUEST_PATH,
87                                                   MCE_REQUEST_IF);
88     if(proxy)
89     {
90       dbus_g_proxy_call_no_reply(proxy,
91                                  method,
92                                  G_TYPE_STRING, pattern,
93                                  G_TYPE_INVALID);
94       g_object_unref(proxy);
95     }
96   } 
97 }
98
99 static void
100 handle_lock_mode_changed(DBusGProxy* object,
101                          const gchar* tklock_mode,
102                          LensCoverStatusPlugin* plugin)
103 {
104   if(lens_cover_status_plugin_cover_is_open())
105   {
106     if(g_str_has_prefix(tklock_mode, MCE_TK_LOCKED))
107       lens_cover_status_plugin_vibrate(plugin, TRUE);
108     else
109       lens_cover_status_plugin_vibrate(plugin, FALSE);
110   }
111 }
112
113 static void
114 handle_cover_switch_changed(DBusGProxy* object,
115                             const gchar* event,
116                             const gchar* source,
117                             LensCoverStatusPlugin* plugin)
118 {
119   if(lens_cover_status_plugin_cover_is_open())
120   {
121     lens_cover_status_plugin_show_icon(plugin);
122   }
123   else
124   {
125     lens_cover_status_plugin_hide_icon(plugin);
126   }
127 }
128
129 static void
130 lens_cover_status_plugin_finalize(GObject* object)
131 {
132   LensCoverStatusPlugin* plugin = LENS_COVER_STATUS_PLUGIN(object);
133   if(plugin->priv->dbus_proxy_cover)
134   {
135     dbus_g_proxy_disconnect_signal(plugin->priv->dbus_proxy_cover,
136                                    "Condition",
137                                    G_CALLBACK(handle_cover_switch_changed),
138                                    plugin);
139   }
140   if(plugin->priv->dbus_proxy_lock)
141   {
142     dbus_g_proxy_disconnect_signal(plugin->priv->dbus_proxy_lock,
143                                    MCE_TKLOCK_MODE_SIG,
144                                    G_CALLBACK(handle_lock_mode_changed),
145                                    plugin);
146   }
147 }
148
149 static void
150 register_dbus_signal_on_cover_switch(LensCoverStatusPlugin* plugin)
151 {
152   plugin->priv->dbus_conn = NULL;
153   plugin->priv->dbus_proxy_cover = NULL;
154   plugin->priv->dbus_proxy_lock = NULL;
155   plugin->priv->dbus_conn = hd_status_plugin_item_get_dbus_g_connection(HD_STATUS_PLUGIN_ITEM(&plugin->parent),
156                                                                         DBUS_BUS_SYSTEM,
157                                                                         NULL);
158
159   if(plugin->priv->dbus_conn)
160   {
161     plugin->priv->dbus_proxy_cover = dbus_g_proxy_new_for_name(plugin->priv->dbus_conn,
162                                                          "org.freedesktop.Hal",
163                                                          "/org/freedesktop/Hal/devices/platform_cam_shutter",
164                                                          "org.freedesktop.Hal.Device");
165     dbus_g_proxy_add_signal(plugin->priv->dbus_proxy_cover,
166                             "Condition",
167                             G_TYPE_STRING,
168                             G_TYPE_STRING,
169                             G_TYPE_INVALID);
170     dbus_g_proxy_connect_signal(plugin->priv->dbus_proxy_cover,
171                                 "Condition",
172                                 G_CALLBACK(handle_cover_switch_changed), plugin, NULL);
173                                                          
174     plugin->priv->dbus_proxy_lock = dbus_g_proxy_new_for_name(plugin->priv->dbus_conn,
175                                                               MCE_SERVICE,
176                                                               MCE_SIGNAL_PATH,
177                                                               MCE_SIGNAL_IF);
178     dbus_g_proxy_add_signal(plugin->priv->dbus_proxy_lock,
179                             MCE_TKLOCK_MODE_SIG,
180                             G_TYPE_STRING,
181                             G_TYPE_INVALID);
182     dbus_g_proxy_connect_signal(plugin->priv->dbus_proxy_lock,
183                                 MCE_TKLOCK_MODE_SIG,
184                                 G_CALLBACK(handle_lock_mode_changed), plugin, NULL);
185
186   }
187 }
188
189
190 static void
191 lens_cover_status_plugin_class_finalize(LensCoverStatusPluginClass *klass)
192 {
193 }
194
195 static void
196 lens_cover_status_plugin_class_init(LensCoverStatusPluginClass *klass)
197 {
198   g_type_class_add_private(klass, sizeof(LensCoverStatusPluginPrivate));
199   G_OBJECT_CLASS(klass)->finalize = (GObjectFinalizeFunc)lens_cover_status_plugin_finalize;
200 }
201
202 static void
203 lens_cover_status_plugin_init(LensCoverStatusPlugin* plugin)
204 {
205   plugin->priv = LENS_COVER_STATUS_PLUGIN_GET_PRIVATE(plugin);
206   register_dbus_signal_on_cover_switch(plugin);
207   if(lens_cover_status_plugin_cover_is_open())
208   {
209     lens_cover_status_plugin_show_icon(plugin);
210   }
211   else
212   {
213     lens_cover_status_plugin_hide_icon(plugin);
214   }
215 }
216