~ubuntu-branches/ubuntu/precise/screenlets/precise

« back to all changes in this revision

Viewing changes to src/share/screenlets/MyIp/MyIpScreenlet.py

  • Committer: Bazaar Package Importer
  • Author(s): Julien Lavergne
  • Date: 2011-02-24 00:51:35 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110224005135-p65cxwmpfwwy3woi
Tags: 0.1.2+bzr616-0ubuntu1
* New upstream snapshot.
 - Move individual screenlets into another source package.
 - Many bug fixes since 0.1.2.
* Convert to source format 3.0 (quilt).
* debian/patches
 - Removed all patches, merged upstream.
* debian/rules:
 - Convert to dh7.
 - Don't remove copies of feedparser, not in this package.
 - Don't remove empty directory for the individual screenlets.
* debian/screenlets.install:
 - Update installed files.
* debian/compat
 - Bump to level 7.
* debian/control:
 - Rewrite Depends and build-depends.
* README.Debian :
 - Mention mutter as a composite manager.
* debian/README.source:
 - Remove the README.source.
* debian/screenlets.manpages:
 - Add the manpages.
* debian/copyright:
 - Rewrite with dep5 style.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
#MyIpScreenlet (c) Whise <helder.fraga@hotmail.com>
11
 
 
12
 
import screenlets
13
 
from screenlets.options import FloatOption, BoolOption, StringOption, FontOption, ColorOption, IntOption
14
 
from screenlets import DefaultMenuItem
15
 
import cairo
16
 
import pango
17
 
import gobject
18
 
from urllib import urlopen
19
 
from screenlets import Plugins
20
 
proxy = Plugins.importAPI('Proxy')
21
 
 
22
 
 
23
 
 
24
 
class MyIpScreenlet(screenlets.Screenlet):
25
 
        """A Screenlet that displays Internet Ip"""
26
 
        
27
 
        # default meta-info for Screenlets
28
 
        __name__ = 'MyIpScreenlet'
29
 
        __version__ = '0.3'
30
 
        __author__ = 'Helder Fraga aka Whise (c) 2007'
31
 
        __desc__ = 'A Screenlet that displays Internet Ip'
32
 
 
33
 
        font_color = (1,1,1, 0.8)
34
 
        background_color = (0,0,0, 0.8)
35
 
        myip = ""
36
 
        def __init__(self, **keyword_args):
37
 
                screenlets.Screenlet.__init__(self, width=200, height=50,uses_theme=True, **keyword_args) 
38
 
 
39
 
                self.theme_name = "default"
40
 
                self.add_default_menuitems(DefaultMenuItem.XML)
41
 
                self.add_options_group('Options',
42
 
                        'The Options widget settings')
43
 
 
44
 
 
45
 
                self.add_option(ColorOption('Options','font_color', 
46
 
                        self.font_color, 'Text color', 'font_color'))
47
 
                self.add_option(ColorOption('Options','background_color', 
48
 
                        self.background_color, 'Back color(only with default theme)', 'only works with default theme'))
49
 
        
50
 
                self.gen()
51
 
                self.__timeout = gobject.timeout_add(24 * 60 * 60 * 1000, self.update)
52
 
 
53
 
        def __setattr__(self, name, value):
54
 
                # call Screenlet.__setattr__ in baseclass (ESSENTIAL!!!!)
55
 
                screenlets.Screenlet.__setattr__(self, name, value)
56
 
 
57
 
        def on_init (self):
58
 
                print "Screenlet has been initialized."
59
 
                # add default menuitems
60
 
                self.add_default_menuitems()
61
 
 
62
 
        def update (self):
63
 
                self.gen()
64
 
                self.redraw_canvas()
65
 
                return True # keep running this event   
66
 
 
67
 
                
68
 
 
69
 
        def gen(self):
70
 
                try:
71
 
                        proxies = proxy.Proxy().get_proxy()
72
 
                        self.myip = str(urlopen("http://www.whatismyip.com/automation/n09230945.asp",proxies=proxies).read())
73
 
 
74
 
                except:
75
 
                        from screenlets import sensors
76
 
                        self.myip = str(sensors.net_get_ip())
77
 
 
78
 
        def on_draw(self, ctx):
79
 
                ctx.scale(self.scale, self.scale)
80
 
                ctx.set_operator(cairo.OPERATOR_OVER)
81
 
                if self.theme:
82
 
                        self.theme.render(ctx,'background')
83
 
 
84
 
                        ctx.set_source_rgba(*self.background_color)
85
 
                        if self.theme_name == 'default':self.draw_rounded_rectangle(ctx,0,0,6,200,40)
86
 
                        ctx.set_source_rgba(1, 1, 1, 1)
87
 
                        ctx.set_source_rgba(*self.font_color)
88
 
                        self.draw_text(ctx,'IP - ' + self.myip,0,10, 'FreeSans',16, self.width,allignment = pango.ALIGN_CENTER)
89
 
                        try:self.theme.render(ctx,'glass')
90
 
                        except:pass
91
 
 
92
 
        def on_menuitem_select (self, id):
93
 
                """handle MenuItem-events in right-click menu"""
94
 
                if id[:4] == "upda":
95
 
                        self.gen()
96
 
                        # TODO: use DBus-call for this
97
 
                        #self.switch_hide_show()
98
 
                        self.redraw_canvas()
99
 
 
100
 
        def on_draw_shape(self,ctx):
101
 
                ctx.rectangle(0,0,self.width,self.height)
102
 
                ctx.fill()
103
 
                self.on_draw(ctx)
104
 
 
105
 
if __name__ == "__main__":
106
 
        import screenlets.session
107
 
        screenlets.session.create_session(MyIpScreenlet)