2 * test-gsm-receive - Play with receiving GSM data
4 * Copyright (C) 2010 Enrico Zini <enrico@enricozini.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 //string VERSION = "0.1";
25 public class GSMReceive: Object, zavai.Resource
27 public dynamic DBus.Object network;
28 public dynamic DBus.Object call;
32 zavai.gsm.gsm.request("GSMReceive");
34 network = zavai.registry.sbus.get_object(
35 "org.freesmartphone.ogsmd",
36 "/org/freesmartphone/GSM/Device",
37 "org.freesmartphone.GSM.Network");
39 call = zavai.registry.sbus.get_object(
40 "org.freesmartphone.ogsmd",
41 "/org/freesmartphone/GSM/Device",
42 "org.freesmartphone.GSM.Call");
44 network.Status += on_network_Status;
45 network.SignalStrength += on_network_SignalStrength;
46 call.CallStatus += on_call_Status;
49 public void shutdown()
51 zavai.gsm.gsm.release("GSMReceive");
54 public void on_network_SignalStrength(int strength)
56 stderr.printf("SIGNAL STRENGTH %d\n", strength);
59 public void on_network_Status(GLib.HashTable<string, GLib.Value?> status)
61 stderr.printf("NETWORK STATUS\n");
63 // dbg("cbNetworkStatus %s" % formatDict(status))
66 public void on_call_Status(int index, string status, GLib.HashTable<string, GLib.Value?> properties)
68 stderr.printf("CALL STATUS %d %s\n", index, status);
70 dbg("cbCallStatus %d, %s, %s" % (id, status, formatDict(properties)))
72 if status == "incoming":
73 if "peer" in properties and properties["peer"] == "+358942832031":
74 self.gsm_call_iface.ReleaseAll()
75 os.system("kapula-debug-call-handler &")
77 os.system("alsactl restore -f /etc/alsa-scenarios/stereoout-maxvolume.state")
78 os.system("vibrator-start")
79 os.system("ringtone-start")
80 os.system("ledctrl --on-time 400 --off-time 200 gta02-aux:red");
81 if status == "release":
82 os.system("ringtone-stop")
83 os.system("vibrator-stop")
84 os.system("ledctrl --off gta02-aux:red");
90 def onIncomingUssd( self, mode, message ):
91 logger.info( "USSD Message: %s" % message )
92 self.main.groups["alert"].activate( "<title>Operator Message</title>%s" % message, [("OK")] )
93 def onCallStatus( self, id, status, properties ):
95 self.update_status(status)
96 if not status in ["release"]:
97 if "peer" in properties:
98 self.part_text_set( "label", self.main.groups["contacts"].tryNumberToName( properties[ "peer" ] ) )
99 self.part_text_set( "sublabel", properties[ "peer" ] )
101 self.part_text_set( "label", _("unknown number") )
102 self.part_text_set( "sublabel", "" )
104 @edje.decorators.signal_callback( "mouse,clicked,1", "button_*" )
105 def on_edje_signal_dialer_button_pressed(self, emission, source):
106 key = source.split("_", 1)[1]
107 if key in ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9"):
108 dbus_object.gsm_call_iface.SendDtmf( key )
109 self.text.append(key)
111 dbus_object.gsm_call_iface.SendDtmf( "*" )
112 self.text.append("*")
114 dbus_object.gsm_call_iface.SendDtmf( "#" )
116 # The trailing whitespace is a workaround for the one char invisible
117 # bug due to some problems with scaling of text parts.
118 self.part_text_set("label", "".join(self.text)+" ")
120 @edje.decorators.signal_callback("call_button_pressed", "button_right")
121 def on_edje_signal_call_button_right_pressed(self, emission, source):
122 self.main.transition_to("call")
124 def sendMessage( self, index ):
125 logger.info( "trying to send message w/ index %d..." % index )
126 dbus_object.gsm_sim_iface.SendStoredMessage( index, reply_handler=self.cbSendReply, error_handler=self.cbSendError )
128 def cbSendReply( self, reference, timestamp ):
129 logger.info( "sent message successfully w/ reference number %d (timestamp: %s)" % ( reference, timestamp ) )
131 def cbSendError( self, e ):
132 logger.error( "could not send message. leaving in outgoing. error was: %s" % e )
134 def cbStoreReply( self, result ):
135 logger.info( "stored message lives at SIM position %d" % result )
136 self.sendMessage( result )
138 dbus_object.gsm_sim_iface.RetrieveMessagebook(
140 reply_handler=self.cbMessagebookReply,
141 error_handler=self.cbMessagebookError
145 def cbStoreError( self, e ):
146 logger.warning( "error while storing message - %s" % e.get_dbus_name() )
147 if e.get_dbus_name() == "org.freesmartphone.GSM.SIM.MemoryFull":
148 self.main.groups["alert"].activate( "<title>Failed to send message</title>SIM Memory Full", [("OK")] )
150 def cbSend1( self, selected, cb_data ):
151 self.main.groups["text_edit"].setup(
155 selected[1], # reference
159 def cbSend2( self, text, number ):
160 if dbus_object.gsm_device_obj:
161 dbus_object.gsm_sim_iface.StoreMessage(
163 reply_handler=self.cbStoreReply,
164 error_handler=self.cbStoreError
167 def cbDelete( self, result, reference ):
168 if result == "abort":
170 if dbus_object.gsm_device_obj:
171 dbus_object.gsm_sim_iface.DeleteMessage(
174 for i in range( len( self.messagebook ) ):
175 if self.messagebook[i][0] == reference:
176 del self.messagebook[i]
180 def cbForward( self, selected, text ):
181 if dbus_object.gsm_device_obj:
182 dbus_object.gsm_sim_iface.StoreMessage(
183 selected[1], text, {},
184 reply_handler=self.cbStoreReply,
185 error_handler=self.cbStoreError
188 def cbReply( self, text, number):
189 if dbus_object.gsm_device_obj:
190 dbus_object.gsm_sim_iface.StoreMessage(
192 reply_handler=self.cbStoreReply,
193 error_handler=self.cbStoreError
196 def cbMenu( self, result ):
197 if result == _("send"):
198 self.main.groups["contacts"].prepare()
199 if self.main.groups["contacts"].ready:
200 self.main.groups["list_choose"].setup(
202 [ (x[1], x[2]) for x in self.main.groups["contacts"].phonebook],
206 elif result == _("delete"):
207 self.main.groups["alert"].activate(
209 (_("abort"), _("delete")),
210 self.current[self.selected][0], # reference
213 elif result == _("forward"):
214 self.main.groups["contacts"].prepare()
215 if self.main.groups["contacts"].ready:
216 self.main.groups["list_choose"].setup(
218 [ (x[1], x[2] ) for x in self.main.groups["contacts"].phonebook],
219 self.current[self.selected][3],
222 elif result == _("reply"):
223 self.main.groups["text_edit"].setup(
226 self.current[self.selected][2], # title = number
227 self.current[self.selected][2], # reference = number
231 def cbMessagebookReply( self, result ):
232 logger.info( "retrieved messagebook: %s" % result )
234 self.messagebook = result
237 self.main.groups["main"].targets["sms"] = True
238 self.main.groups["main"].update()
239 if not self.newindex is None:
240 message = [x for x in self.messagebook if x[0] == self.newindex]
242 if message and self.main.current_group == self.main.groups["main"]:
244 if "read" in message[1]:
246 timestamp = message[4]["timestamp"]
249 timestamp = _("Unknown")
250 from_text = self.main.groups["contacts"].tryNumberToName( message[2] )
251 self.main.groups["text_show"].setup(
253 _("%s: %s<br>Date: %s<p>%s") % (
257 textblock_escape( message[3] ).replace( '\n', '<br>' )
260 if dbus_object.gsm_device_obj:
261 messagebookInfo = dbus_object.gsm_sim_iface.GetMessagebookInfo()
262 if messagebookInfo["used"] == messagebookInfo["last"]:
263 self.main.groups["alert"].activate( "<title>Warning</title>SIM Memory Full", [("OK")] )
265 def cbMessagebookError( self, e ):
266 logger.warning( "error while retrieving messagebook" )
269 def onIncomingMessage( self, index ):
270 logger.info( "new message! Retrieving messagebook..." )
271 self.newindex = index
273 dbus_object.gsm_sim_iface.RetrieveMessagebook(
275 reply_handler=self.cbMessagebookReply,
276 error_handler=self.cbMessagebookError
281 if not self.ready and not self.busy:
282 if dbus_object.gsm_device_obj:
283 logger.info( "retrieving messagebook..." )
284 dbus_object.gsm_sim_iface.RetrieveMessagebook(
286 reply_handler=self.cbMessagebookReply,
287 error_handler=self.cbMessagebookError
291 # Fake messagebook...
292 self.cbMessagebookReply( [
293 (0, "read", "+4544555", "Hello World!"),
294 (1, "read", "+456663443", "Zhone!"),
295 (2, "read", "+456663443", "Hi Guy\nGuess what, I now "+
296 "know to write multi-line SMSs.\nIsn't that "+
297 "nice?\n\nSome Buddy"),
298 (3, "read", "Flash SMS", "An SMS without digits. Strange, isn't it?"),
301 def onReadyStatus( self, status ):
302 logger.debug( "SIM is ready: %s" % status )
312 def updateList( self):
313 self.main.groups["contacts"].prepare()
314 self.pages = max( ( len( self.messagebook ) - 1 ) / 6 + 1, 1 )
315 if self.page >= self.pages:
316 self.page = self.pages - 1
319 self.current = self.messagebook[self.page*6:(self.page+1)*6]
320 text = u"".join( [u"□"]*self.page+[u"▣"]+[u"□"]*(self.pages-self.page-1) )
321 self.part_text_set( "pager", text )
322 for i in range( 0, len( self.current ) ):
323 main_text = self.main.groups["contacts"].tryNumberToName( self.current[i][2] )
324 self.part_text_set( "label_main_list_%i" % i, main_text )
325 sub_text = " ".join(self.current[i][3].splitlines())
326 self.part_text_set( "label_sub_list_%i" % i, u"(%s) %s" % ( self.current[i][1], sub_text ) )
327 for i in range( len( self.current ), 6):
328 self.part_text_set( "label_main_list_%i" % i, u"" )
329 self.part_text_set( "label_sub_list_%i" % i, u"" )
332 self.signal_emit( "deactivate_target_list_%i" % i, "" )
335 @edje.decorators.signal_callback( "mouse,clicked,1", "target_list_*" )
336 def on_edje_signal_button_list_pressed( self, emission, source ):
337 id = int( source.split( "_" )[-1] )
338 if self.selected == id:
340 if self.selected is not None:
341 self.signal_emit( "deactivate_target_list_%i" % self.selected, "" )
342 self.signal_emit( "activate_target_list_%i" % id, "" )
345 @edje.decorators.signal_callback( "mouse,clicked,1", "button_action_left" )
346 def on_edje_signal_button_action_left_pressed( self, emission, source ):
350 @edje.decorators.signal_callback( "mouse,clicked,1", "button_action_right" )
351 def on_edje_signal_button_action_right_pressed( self, emission, source ):
355 @edje.decorators.signal_callback( "mouse,clicked,1", "button_action_open" )
356 def on_edje_signal_button_action_open_pressed( self, emission, source ):
357 if self.selected is not None:
358 if "read" in self.current[self.selected][1]:
360 timestamp = self.current[self.selected][4]["timestamp"]
363 timestamp = _("Unknown")
364 from_text = self.main.groups["contacts"].tryNumberToName( self.current[self.selected][2] )
365 self.main.groups["text_show"].setup(
367 _("%s: %s<br>Date: %s<p>%s") % (
371 textblock_escape( self.current[self.selected][3] ).replace( '\n', '<br>' )
375 @edje.decorators.signal_callback( "mouse,clicked,1", "button_bottom_middle" )
376 def on_edje_signal_button_bottom_middle_pressed( self, emission, source ):
377 self.main.groups["menu"].activate( ( _("send"), _("delete"), _("forward"), _("reply") ), self.cbMenu )
378 self.main.groups["menu"].part_text_set( "target_label_cancel", _("cancel") )
382 static int main (string[] args) {
388 // Core infrastructure
389 zavai.config = new zavai.Config();
390 zavai.config.argv0 = args[0];
391 zavai.registry = new zavai.Registry();
393 // Additional infrastructure
401 // zavai.info("Shutting down")
402 zavai.registry.shutdown();