1
#!/usr/bin/env python2.5
2
# -*- coding: utf-8 -*-
3
""" Useful functions that shouldn't be in the UI code
4
5
fMMS - MMS for fremantle
6
Copyright (C) 2010 Nick Leppänen Larsson <frals@frals.se>
7
8
@license: GNU GPLv2, see COPYING file.
9
"""
10
import StringIO
11
import gettext
12
13
import gtk
14
import hildon
15
16
import controller
17
import heaboutdialog
18
19
import logging
20
log = logging.getLogger('fmms.%s' % __name__)
21
22
MSG_DIRECTION_IN = 0
23
MSG_DIRECTION_OUT = 1
24
MSG_UNREAD = 0
25
MSG_READ = 1
26
27
_ = gettext.gettext
28
29
class fMMS_controllerGTK(controller.fMMS_controller):
30
	
31
	def __init__(self):
32
		controller.fMMS_controller.__init__(self)
33
		self.ui = True
34
		self.config_label = gettext.ldgettext('rtcom-messaging-ui', "messaging_me_main_settings")
35
		self.reset_label = _('Reset settings')
36
		self.about_label = gettext.ldgettext('hildon-libs', "ecdg_ti_aboutdialog_title")
37
	
38
	def import_configdialog(self):
39
		""" This is used to import configdialog only when we need it
40
		    as its quite a hog """
41
		try:
42
			if not self.cdimported:
43
				import fmms_config_dialog as fMMSConfigDialog
44
				global fMMSConfigDialog
45
				self.cdimported = True
46
		except:
47
			import fmms_config_dialog as fMMSConfigDialog
48
			global fMMSConfigDialog
49
			self.cdimported = True
50
51
	def create_menu(self, parent=None):
52
		""" Creates the application menu. """
53
		menu = hildon.AppMenu()
54
55
		config = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
56
		config.set_label(self.config_label)
57
		config.connect('clicked', self.menu_button_clicked, parent)
58
		
59
		reset = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
60
		reset.set_label(self.reset_label)
61
		reset.connect('clicked', self.menu_button_clicked, parent)
62
63
		about = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
64
		about.set_label(self.about_label)
65
		about.connect('clicked', self.menu_button_clicked, parent)
66
		
67
		menu.append(config)
68
		menu.append(reset)
69
		menu.append(about)
70
71
		menu.show_all()
72
73
		return menu
74
75
	def menu_button_clicked(self, button, parent):
76
		""" Determine what button was clicked in the app menu. """
77
		buttontext = button.get_label()
78
		if buttontext == self.config_label:
79
			self.import_configdialog()
80
			fMMSConfigDialog.fMMS_ConfigDialog(parent)
81
		elif buttontext == self.about_label:
82
			self.create_about_dialog(parent)
83
		elif buttontext == self.reset_label:
84
			self.create_reset_dialog(parent)
85
86
	def create_reset_dialog(self, parent=None):
87
		dialog = gtk.Dialog()
88
		dialog.set_transient_for(parent)
89
		dialog.set_title(_('Reset settings'))
90
		dialog.add_button(gtk.STOCK_YES, 1)
91
		dialog.add_button(gtk.STOCK_NO, 0)
92
		label = gtk.Label(_('Are you sure you want to reset all settings?'))
93
		dialog.vbox.add(label)
94
		dialog.show_all()
95
		ret = dialog.run()
96
		if ret == 1:
97
			import osso
98
			self.osso_c = osso.Context("fMMS", "1.0", False)
99
			note = osso.SystemNote(self.osso_c)
100
			note.system_note_dialog(_('Application will close after settings are removed'), 'notice')
101
			self.reset_all_settings()
102
			import sys
103
			sys.exit(0)
104
		dialog.destroy()
105
106
	def create_about_dialog(self, parent=None):
107
		""" Create and display the About dialog. """
108
		heaboutdialog.HeAboutDialog.present(parent,
109
					'fMMS',
110
					'fmms',
111
					self.config.get_version(),
112
					_('Send and receive MMS on your N900.'),
113
					'Copyright (C) 2010 Nick Leppänen Larsson.\n' +
114
					'This program is free software; you can redistribute it and/or' +
115
					 ' modify it under the terms of the GNU General Public License' +
116
					 ' as published by the Free Software Foundation; either version 2' +
117
					 ' of the License, or (at your option) any later version.\n' +
118
					 'This program is distributed in the hope that it will be useful,' +
119
					 ' but WITHOUT ANY WARRANTY; without even the implied warranty of' +
120
					 ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the' +
121
					 ' GNU General Public License for more details.',
122
					'http://mms.frals.se/',
123
					'http://bugs.maemo.org/enter_bug.cgi?product=fMMS',
124
					'https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=AZKC7ZRYKEY76&lc=SE&item_name=frals_mms&item_number=fmms_app&currency_code=EUR&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted')
125
		
126
		
127
	def get_primary_font(self):
128
		return self.get_font_desc('SystemFont')
129
130
	def get_secondary_font(self):
131
		return self.get_font_desc('SmallSystemFont')
132
133
	def get_active_color(self):
134
		return self.get_color('ActiveTextColor')
135
136
	def get_primary_color(self):
137
		return self.get_color('ButtonTextColor')
138
139
	def get_secondary_color(self):
140
		return self.get_color('SecondaryTextColor')
141
142
	# credits to gpodder for this
143
	def get_font_desc(self, logicalfontname):
144
		settings = gtk.settings_get_default()
145
		font_style = gtk.rc_get_style_by_paths(settings, logicalfontname, \
146
							None, None)
147
		font_desc = font_style.font_desc
148
		return font_desc
149
150
	# credits to gpodder for this
151
	def get_color(self, logicalcolorname):
152
		settings = gtk.settings_get_default()
153
		color_style = gtk.rc_get_style_by_paths(settings, 'GtkButton', \
154
							'osso-logical-colors', gtk.Button)
155
		return color_style.lookup_color(logicalcolorname)
156
		
157
	""" from http://snippets.dzone.com/posts/show/655 """
158
	def image2pixbuf(self, im):
159
		file1 = StringIO.StringIO()
160
		try:
161
			im.save(file1, "ppm")
162
			contents = file1.getvalue()
163
			file1.close()
164
			loader = gtk.gdk.PixbufLoader("pnm")
165
			loader.write(contents, len(contents))
166
			pixbuf = loader.get_pixbuf()
167
			loader.close()
168
			return pixbuf
169
		except IOError, e:
170
			log.info("Failed to convert, trying as gif.")
171
			try:
172
				im.save(file1, "gif")
173
				contents = file1.getvalue()
174
				file1.close()
175
				loader = gtk.gdk.PixbufLoader()
176
				loader.write(contents, len(contents))
177
				pixbuf = loader.get_pixbuf()
178
				loader.close()
179
				return pixbuf
180
			except:
181
				log.exception("Failed to convert")
182
				raise
183
	
184
	""" ask user about download while roaming """
185
	def continue_download_roaming(self):
186
		dialog = gtk.Dialog()
187
		dialog.set_title(gettext.ldgettext('osso-connectivity-ui', 'conn_fi_phone_network_data_roam'))
188
		#dialog.set_transient_for(self.window)
189
		label = gtk.Label(_("To retrieve the MMS your active connection you need to connect to the internet, proceed?"))
190
		label.set_line_wrap(True)
191
		dialog.vbox.add(label)
192
		dialog.add_button(gtk.STOCK_YES, 1)
193
		dialog.add_button(gtk.STOCK_NO, 0)
194
		dialog.vbox.show_all()
195
		ret = dialog.run()
196
		switch = False
197
		if ret == 1:
198
			switch = True
199
		dialog.destroy()
200
		while gtk.events_pending():
201
			gtk.main_iteration(False)
202
		return switch