~ubuntu-branches/ubuntu/saucy/screenlets/saucy

21 by Chris Coulson
* Port from python-gtkmozembed to python-webkit. GtkMozEmbed has been
1
#!/usr/bin/env python
2
3
# This application is released under the GNU General Public License 
4
# v3 (or, at your option, any later version). You can find the full 
5
# text of the license under http://www.gnu.org/licenses/gpl.txt. 
6
# By using, editing and/or distributing this software you agree to 
7
# the terms and conditions of this license. 
8
# Thank you for using free software!
9
10
# WidgetScreenlet (c) 2007 bu Helder Fraga aka Whise
11
12
13
14
import screenlets
15
from screenlets import DefaultMenuItem
16
from screenlets.options import BoolOption, IntOption, ColorOption
17
import cairo
18
import gtk
19
import gobject
20
import commands
21
import sys
22
import os
23
from screenlets import sensors
24
25
26
27
#########WORKARROUND FOR GTKOZEMBED BUG BY WHISE################
28
myfile = 'WidgetScreenlet.py'
29
mypath = sys.argv[0][:sys.argv[0].find(myfile)].strip()
30
31
if sys.argv[0].endswith(myfile): # Makes Shure its not the manager running...
32
		# First workarround
33
		c = None
34
		workarround =  "python "+ sys.argv[0] + " &"
35
		a = str(commands.getoutput('whereis firefox')).replace('firefox: ','').split(' ')
36
		for b in a:
37
			if os.path.isfile(b + '/run-mozilla.sh'):
38
				c = b + '/run-mozilla.sh'
39
				workarround = c + " " + sys.argv[0] + " &"
40
41
42
		if c == None:
43
			# Second workarround
44
			print 'First workarround didnt work let run a second manual workarround'
45
			if str(sensors.sys_get_distrib_name()).lower().find('ubuntu') != -1: # Works for ubuntu 32
46
				workarround = "export LD_LIBRARY_PATH=/usr/lib/firefox \n export MOZILLA_FIVE_HOME=/usr/lib/firefox \n python "+ sys.argv[0] + " &"
47
			elif str(sensors.sys_get_distrib_name()).lower().find('debian') != -1: # Works for debian 32 with iceweasel installed
48
				workarround = "export LD_LIBRARY_PATH=/usr/lib/iceweasel \n export MOZILLA_FIVE_HOME=/usr/lib/iceweasel \n python " + sys.argv[0] + " &"
49
			elif str(sensors.sys_get_distrib_name()).lower().find('suse') != -1: # Works for suse 32 with seamonkey installed
50
				workarround = "export LD_LIBRARY_PATH=/usr/lib/seamonkey \n export MOZILLA_FIVE_HOME=/usr/lib/seamonkey \n python "+ sys.argv[0] + " &"
51
				print 'Your running suse , make shure you have seamonkey installed'
52
			elif str(sensors.sys_get_distrib_name()).lower().find('fedora') != -1: # Works for fedora 32 with seamonkey installed
53
				workarround = "export LD_LIBRARY_PATH=/usr/lib/seamonkey \n export MOZILLA_FIVE_HOME=/usr/lib/seamonkey \n python "+ sys.argv[0] + " &"
54
				print 'Your running fedora , make shure you have seamonkey installed'
55
56
57
		if os.path.isfile("/tmp/"+ myfile+"running"):
58
			os.system("rm -f " + "/tmp/"+ myfile+"running")
59
		
60
		else:
61
			if workarround == "python "+ sys.argv[0] + " &":
62
				print 'No workarround will be applied to your sistem , this screenlet will probably not work properly'
63
			os.system (workarround)
64
			fileObj = open("/tmp/"+ myfile+"running","w") #// open for for write
65
			fileObj.write('gtkmozembed bug workarround')
66
		
67
			fileObj.close()
68
			sys.exit()
69
70
71
else:
72
	pass
73
try:
74
	import gtkmozembed
75
except:
76
	if sys.argv[0].endswith(myfile):screenlets.show_error(None,"You need Gtkmozembed to run this Screenlet , please install it")
77
	else: print "You need Gtkmozembed to run this Screenlet , please install it"
78
#########WORKARROUND FOR GTKOZEMBED BUG BY WHISE################
79
#Check for internet connection required for web widgets
80
81
if sys.argv[0].endswith(myfile):# Makes Shure its not the manager running...
82
	#os.system('wget www.google.com -O/tmp/index.html &')
83
	a = commands.getoutput('wget www.google.com -O/tmp/index.html')
84
	if a.find('text/html') == -1:
85
		screenlets.show_error(None,"Internet connection is required to use this Screenlet")
86
		os.system('rm /tmp/index.html')
87
		sys.exit()
88
	os.system('rm /tmp/index.html')
89
90
class WidgetScreenlet (screenlets.Screenlet):
91
	"""Converted widgets to screenlets engine"""
92
	
93
	# default meta-info for Screenlets
94
	__name__		= 'WidgetScreenlet'
95
	__version__		= '0.3'
96
	__author__		= 'Helder Fraga aka Whise'
97
	__desc__		= __doc__
98
99
	started = False
100
	moz = None
101
	box = None
102
	mypath = sys.argv[0][:sys.argv[0].find('WidgetScreenlet.py')].strip()
103
	url = mypath + 'index.html'
104
	color_back = 0.3,0.3,0.3,0.7
105
	rgba_color = (1,1,1,0.2)
106
	border_width = 8
107
	show_frame = True
108
	widget_width = 300
109
	widget_height = 330
110
	engine = ''
111
112
	width = 325
113
	height = 370
114
	set_width = 0
115
	set_height = 0
116
	def __init__ (self, **keyword_args):
117
		# init stuff
118
		screenlets.Screenlet.__init__(self, width=325, height=370,uses_theme=True, 
119
			is_widget=False, is_sticky=True,is_sizable=False, **keyword_args)
120
121
122
		self.add_options_group('Options', 'CPU-Graph specific options')
123
		self.add_option(BoolOption('Options', 'show_frame',
124
			self.show_frame, 'Show frame border', 'Show frame border arround the widget ...'))	
125
		self.add_option(ColorOption('Options','rgba_color', 
126
			self.rgba_color , 'Frame color', 'The color of the frame border'))
127
        	self.add_option(IntOption('Options', 'set_width', self.set_width, 'Width',  'Custom width', min=10, max=gtk.gdk.screen_width()))
128
        	self.add_option(IntOption('Options', 'set_height', self.set_height, 'Height', 'Custom height', min=10, max=gtk.gdk.screen_height()))
129
        	#self.add_option(IntOption('Options', 'border_width', self.border_width, 'Frame border width', 'The width of the frame border', min=1, max=8))
130
		self.disable_option('scale')
131
		self.theme_name = 'default'
132
		self.box = gtk.VBox(False, 0)
133
		if hasattr(gtkmozembed, 'set_profile_path'):
134
			gtkmozembed.set_profile_path(self.mypath,'mozilla')
135
		else:
136
			gtkmozembed.gtk_moz_embed_set_profile_path(self.mypath ,'mozilla')
137
		self.moz = gtkmozembed.MozEmbed()
138
    		self.box.pack_start(self.moz, False, False, 0)
139
140
		self.window.add(self.box)		
141
			
142
		self.window.show_all()
143
			
144
145
146
	def __setattr__(self, name, value):
147
		# call Screenlet.__setattr__ in baseclass (ESSENTIAL!!!!)
148
		screenlets.Screenlet.__setattr__(self, name, value)
149
		if name == 'border_width' or name == 'rgba_color' or name == 'show_frame':
150
			self.redraw_canvas()
151
		if name == 'set_width' and value  != self.widget_width:
152
			self.widget_width = value
153
			self.width = int(value)+30
154
			self.redraw_canvas()
155
		if name == 'set_height' and value != self.widget_height:
156
			self.widget_height = value
157
			self.height = int(value)+30	
158
			self.redraw_canvas()
159
160
161
	def on_focus (self, event):
162
		"""Called when the Screenlet's window receives focus."""
163
		self.redraw_canvas()
164
165
	def on_unfocus (self, event):
166
		"""Called when the Screenlet's window receives focus."""
167
		self.redraw_canvas()
168
169
	def on_draw (self, ctx):
170
171
172
		ctx.scale(self.scale , self.scale )
173
		ctx.set_operator(cairo.OPERATOR_OVER)
174
175
		ctx.set_source_rgba(0, 0, 0, 0)
176
 		
177
		bgcolor = self.box.get_style().bg[gtk.STATE_NORMAL]
178
179
		r = bgcolor.red/65535.0
180
		g = bgcolor.green/65535.0
181
		b = bgcolor.blue/65535.0
182
		ctx.set_source_rgba(r, g, b, 1)	
183
	
184
		if self.theme and self.window:
185
			ctx.set_source_rgba(self.color_back[0],self.color_back[1],self.color_back[2],self.color_back[3])
186
			if self.has_focus == True:
187
				if not self.show_frame:
188
					self.draw_rounded_rectangle(ctx,int((self.width - 64)),0,5,64,12)
189
			ctx.set_source_rgba(self.rgba_color[0], self.rgba_color[1], self.rgba_color[2], self.rgba_color[3])	
190
		
191
			
192
			if self.show_frame:
193
				self.draw_rounded_rectangle(ctx,0,0,5,self.width,self.height)
194
				ctx.set_source_rgba(1-self.rgba_color[0], 1-self.rgba_color[1], 1- self.rgba_color[2], 0.15)
195
				self.draw_rounded_rectangle(ctx,0,0,5,self.width,self.height,fill=False)
196
	
197
			if self.engine == 'google':		
198
				self.bgpb = gtk.gdk.pixbuf_new_from_file(self.mypath + 'icon.png').scale_simple(int(self.width),int(self.widget_height),gtk.gdk.INTERP_HYPER)
199
				self.bgpbim, self.bgpbms = self.bgpb.render_pixmap_and_mask(alpha_threshold=128)
200
			
201
				if not self.window.is_composited():
202
					#ctx.translate(0,10)
203
					self.draw_scaled_image(ctx,0,0,self.width,self.height,self.mypath + 'icon.png')
204
205
				self.moz.shape_combine_mask(self.bgpbms,0,0)	
206
			else:
207
				self.bgpb = gtk.gdk.pixbuf_new_from_file(self.mypath + 'icon.png').scale_simple(int(self.widget_width),int(self.widget_height),gtk.gdk.INTERP_HYPER)
208
				self.bgpbim, self.bgpbms = self.bgpb.render_pixmap_and_mask(alpha_threshold=128)
209
			
210
				if not self.window.is_composited():
211
					ctx.translate(0,10)
212
					self.draw_image(ctx,0,0,self.mypath + 'icon.png')
213
214
				self.moz.shape_combine_mask(self.bgpbms,8,8)	
215
 	
216
217
218
	def on_init(self):
219
		self.load_widget()
220
		self.add_default_menuitems(DefaultMenuItem.WINDOW_MENU | DefaultMenuItem.PROPERTIES |
221
			DefaultMenuItem.DELETE | DefaultMenuItem.QUIT | DefaultMenuItem.QUIT_ALL)
222
		if self.set_width == 0: self.set_width = int(self.widget_width)
223
		else:
224
			self.width = self.set_width + 30
225
			self.redraw_canvas()
226
			
227
		if self.set_height == 0: self.set_height = int(self.widget_height)
228
		else:
229
			self.height = self.set_height + 30
230
			self.redraw_canvas()
231
	def load_widget(self):
232
233
		self.widget  = open(self.url,'r')
234
		self.widget_info = self.widget.read()
235
		self.widget_info = self.widget_info.replace('width=' +chr(34) + '100%','')
236
237
238
		if self.widget_info.find("width=" + chr(34)) != -1:
239
		# Search for width="
240
			self.widget_width = self.widget_info[self.widget_info.find("width=" + chr(34)):]
241
			self.widget_width = self.widget_width[7:]
242
			self.widget_width = self.widget_width[:self.widget_width.find(chr(34)) ].strip()
243
			self.widget_height = self.widget_info[self.widget_info.find("height=" + chr(34)):]
244
			self.widget_height = self.widget_height[8:]
245
			self.widget_height = self.widget_height[:self.widget_height.find(chr(34)) ].strip()
246
247
		
248
		elif self.widget_info.find("width=") != -1:
249
		# Search for width=
250
			self.widget_width = self.widget_info[self.widget_info.find("width="):]
251
			self.widget_width = self.widget_width[6:]
252
	
253
			self.widget_width = self.widget_width[:self.widget_width.find('&') ].strip()
254
				
255
256
			# widget height for scaling porpuse ( the height of the html frame)
257
			self.widget_height = self.widget_info[self.widget_info.find("height=" ):]
258
			self.widget_height = self.widget_height[7:]
259
			self.widget_height = self.widget_height[:self.widget_height.find('&') ].strip()
260
		elif self.widget_info.find("w=") != -1:
261
		# Search for w=
262
			self.widget_width = self.widget_info[self.widget_info.find("w="):]
263
			self.widget_width = self.widget_width[2:]
264
			self.widget_width = self.widget_width[:self.widget_width.find('&') ].strip()
265
			self.widget_height = self.widget_info[self.widget_info.find("h=" ):]
266
			self.widget_height = self.widget_height[2:]
267
			self.widget_height = self.widget_height[:self.widget_height.find('&') ].strip()
268
		
269
		self.widget_width = str(self.widget_width).replace('px','') 
270
		self.widget_height = str(self.widget_height).replace('px','') 
271
272
		self.widget.close()
273
274
275
		if self.widget_info.startswith('<script src='):
276
			self.url = self.widget_info[13:][:(len(self.widget_info)-24)]
277
			
278
			self.engine = 'google'
279
		self.moz.load_url(self.url)
280
		print 'loading ' + self.url
281
		
282
		self.width = int(self.widget_width)+30
283
		self.height = int(self.widget_height)+30
284
		#print self.width,self.height
285
		#print self.widget_width, self.widget_height
286
		#	self.width = 325
287
		#	self.height = 370
288
		self.box.set_border_width(7)
289
		if self.engine == 'google':
290
			#self.box.set_border_width(10)
291
292
			self.box.set_uposition(-1,7)
293
		
294
			self.moz.set_size_request(-1,int(self.widget_height))
295
		else:
296
			self.moz.set_size_request(-1,int(self.height))
297
		self.redraw_canvas()
298
299
	def on_mouse_down(self,event):
300
		pass
301
	def on_draw_shape (self, ctx):
302
		if self.theme:
303
			ctx.scale(1, 1)
304
			ctx.set_operator(cairo.OPERATOR_OVER)
305
306
			ctx.set_source_rgba(0, 0, 0, 1)
307
			#a = self.window.get_size()
308
	 		
309
	 		ctx.rectangle(0, 0, self.width,self.height)
310
			
311
			ctx.fill()
312
		
313
		
314
			
315
316
317
	
318
319
			
320
321
if __name__ == "__main__":
322
	# create new session
323
	import screenlets.session
324
	screenlets.session.create_session(WidgetScreenlet)
325