initial commit
[fmms] / src / fmms_viewer.py
1 #!/usr/bin/env python2.5
2 # -*- coding: utf-8 -*-
3 """ Message-viewer UI for fMMS
4
5 @author: Nick Leppänen Larsson <frals@frals.se>
6 @license: GNU GPL
7 """
8 import os
9
10 import gtk
11 import hildon
12 import gobject
13 import osso
14 from gnome import gnomevfs
15
16 from wappushhandler import PushHandler
17 import fmms_config as fMMSconf
18 import controller as fMMSController
19
20
21 class fMMS_Viewer(hildon.Program):
22
23         def __init__(self, fname, standalone=False):
24                 self.cont = fMMSController.fMMS_controller()
25                 self.standalone = standalone
26                 self.config = fMMSconf.fMMS_config()
27                 self._mmsdir = self.config.get_mmsdir()
28                 self._pushdir = self.config.get_pushdir()
29                 self._outdir = self.config.get_outdir()
30                 self.osso_c = osso.Context("fMMS", "0.1.0", False)
31                 
32                 self.window = hildon.StackableWindow()
33                 self.window.set_title("Showing MMS: " + fname)
34                 self.window.connect("delete_event", self.quit)
35                 
36                 vbox = gtk.VBox()
37                 pan = hildon.PannableArea()
38                 pan.set_property("mov-mode", hildon.MOVEMENT_MODE_BOTH)
39
40                 self._parse_mms(fname, vbox)
41
42                 pan.add_with_viewport(vbox)
43                 self.window.add(pan)
44
45                 mms_menu = self.create_mms_menu(fname)
46                 self.window.set_app_menu(mms_menu)
47                 self.window.show_all()
48         
49         """ lets call it quits! """
50         def quit(self, *args):
51                 self.window.destroy()
52                 if self.standalone == True:
53                         gtk.main_quit()
54         
55         """ forces ui update, kinda... god this is AWESOME """
56         def force_ui_update(self):
57                 while gtk.events_pending():
58                         gtk.main_iteration(False)
59                                 
60         """ create app menu for mms viewing window """
61         def create_mms_menu(self, fname):
62                 menu = hildon.AppMenu()
63                 
64                 headers = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
65                 headers.set_label("Headers")
66                 headers.connect('clicked', self.mms_menu_button_clicked, fname)
67                 
68                 menu.append(headers)
69         
70                 menu.show_all()
71                 
72                 return menu             
73         
74         """ actions for mms menu """
75         def mms_menu_button_clicked(self, button, fname):
76                 buttontext = button.get_label()
77                 if buttontext == "Headers":
78                         ret = self.create_headers_dialog(fname)
79
80         """ show headers in a dialog """
81         def create_headers_dialog(self, fname):
82                 dialog = gtk.Dialog()
83                 dialog.set_title("Headers")
84                 
85                 dialogVBox = gtk.VBox()
86                 
87                 pan = hildon.PannableArea()
88                 #pan.set_property("mov-mode", hildon.MOVEMENT_MODE_BOTH)
89                 pan.set_property("size-request-policy", hildon.SIZE_REQUEST_CHILDREN)
90                 
91                 allVBox = gtk.VBox()
92                 #leftBox = gtk.VBox()
93                 #rightBox = gtk.VBox()
94                 headerlist = self.cont.get_mms_headers(fname)
95                 for line in headerlist:
96                         hbox = gtk.HBox()
97                         titel = gtk.Label(line)
98                         titel.set_alignment(0, 0)
99                         titel.set_width_chars(18)
100                         label = gtk.Label(headerlist[line])
101                         label.set_line_wrap(True)
102                         label.set_alignment(0, 0)
103                         hbox.pack_start(titel, False, False, 0)
104                         hbox.pack_start(label, False, False, 0)
105                         allVBox.pack_start(hbox)
106                         #leftBox.pack_start(titel, False, False, 0)
107                         #rightBox.pack_start(label, False, False, 0)
108                         #pan.add(label)
109                         
110                 #allHBox.pack_start(leftBox, False, False, 0)
111                 #allHBox.pack_start(rightBox, True, True, 0)
112                 allVBox.show_all()
113                 
114                 pan.add_with_viewport(allVBox)
115                 dialog.vbox.add(pan)
116                 dialog.vbox.show_all()
117                 ret = dialog.run()
118                 
119                 dialog.destroy()
120                 return ret
121         
122         """ parse mms and push each part to the container 
123             fetches the mms if its not downloaded         """
124         def _parse_mms(self, filename, container):
125                 hildon.hildon_gtk_window_set_progress_indicator(self.window, 1)
126                 self.force_ui_update()
127                 
128                 if not self.cont.is_fetched_push_by_transid(filename):  
129                         self.cont.get_mms_from_push(filename)
130                                 
131                 textview = gtk.TextView()
132                 textview.set_editable(False)
133                 textview.set_cursor_visible(False)
134                 textview.set_wrap_mode(gtk.WRAP_WORD)
135                 textbuffer = gtk.TextBuffer()
136                 direction = self.cont.get_direction_mms(filename)
137                 if direction == fMMSController.MSG_DIRECTION_OUT:
138                         path = self._outdir + filename
139                 else:
140                         path = self._mmsdir + filename
141                 filelist = self.cont.get_mms_attachments(filename)
142                 print "filelist:", filelist
143                 for fname in filelist:
144                         (name, ext) = os.path.splitext(fname)
145                         fnpath = os.path.join(path, fname)
146                         isText = False
147                         isImage = False
148                         try:
149                                 filetype = gnomevfs.get_mime_type(fnpath)
150                                 print "filetype:", filetype
151                                 if filetype != None:
152                                         if filetype.startswith("image") or filetype.startswith("sketch"):
153                                                 isImage = True
154                                         if filetype.startswith("text"):
155                                                 isText = True
156                         except Exception, e:
157                                 filetype = None
158                                 print type(e), e
159                         
160                         if isImage or ext == ".wbmp":
161                                 """ insert the image in an eventbox so we can get signals """
162                                 ebox = gtk.EventBox()
163                                 img = gtk.Image()
164                                 img.set_from_file(path + "/" + fname)
165                                 fullpath = path + "/" + fname
166                                 ebox.add(img)
167                                 ## TODO: make this menu proper without this ugly
168                                 # args passing
169                                 menu = self.mms_img_menu(fullpath)
170                                 ebox.tap_and_hold_setup(menu)
171                                 container.add(ebox)
172                         elif isText or ext.startswith(".txt"):
173                                 fp = open(path + "/" + fname, 'r')
174                                 contents = fp.read()
175                                 fp.close()
176                                 #print contents
177                                 textbuffer.insert(textbuffer.get_end_iter(), contents)
178                         elif name != "message" and name != "headers" and not ext.startswith(".smil") and filetype != "application/smil":
179                                 attachButton = hildon.Button(gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_HORIZONTAL, fname)
180                                 attachButton.connect('clicked', self.mms_img_clicked, fnpath)
181                                 container.pack_end(attachButton, False, False, 0)
182                                 
183                 textview.set_buffer(textbuffer)
184                 container.add(textview)
185                 hildon.hildon_gtk_window_set_progress_indicator(self.window, 0)
186                 
187                 
188         """ action on click on image/button """
189         def mms_img_clicked(self, widget, data):
190                 print widget, data
191                 path = "file://" + data
192                 # gnomevfs seems to be better than mimetype when guessing mimetype for us
193                 file_mimetype = gnomevfs.get_mime_type(path)
194                 if file_mimetype != None:
195                         if file_mimetype.startswith("video") or file_mimetype.startswith("audio"):
196                                 rpc = osso.Rpc(self.osso_c)
197                                 rpc.rpc_run("com.nokia.mediaplayer", "/com/nokia/mediaplayer", "com.nokia.mediaplayer", "mime_open", (str, path))       
198                         elif file_mimetype.startswith("image"):
199                                 rpc = osso.Rpc(self.osso_c)
200                                 rpc.rpc_run("com.nokia.image_viewer", "/com/nokia/image_viewer", "com.nokia.image_viewer", "mime_open", (str, path))
201                 else:
202                         # TODO: how to solve this?
203                         # move .mms to ~/MyDocs? change button to copy file to ~/MyDocs?
204                         #rpc = osso.Rpc(self.osso_c)
205                         #path = os.path.dirname(path).replace("file://", "")
206                         print path
207                         #rpc.rpc_run("com.nokia.osso_filemanager", "/com/nokia/osso_filemanager", "com.nokia.osso_filemanager", "open_folder", (str, path))
208
209
210         """ long press on image creates this """
211         def mms_img_menu(self, data=None):
212                 print "menu created"
213                 menu = gtk.Menu()
214                 menu.set_title("hildon-context-sensitive-menu")
215
216                 openItem = gtk.MenuItem("Open")
217                 menu.append(openItem)
218                 openItem.connect("activate", self.mms_img_clicked, data)
219                 openItem.show()
220                 menu.show_all()
221                 return menu
222
223         def run(self):
224                 self.window.show_all()
225                 gtk.main()
226                 
227 if __name__ == "__main__":
228         app = fMMS_Viewer("fname", True)
229         app.run()