~tribaal/txaws/xss-hardening

« back to all changes in this revision

Viewing changes to txaws/client/gui/gtk.py

  • Committer: Duncan McGreggor
  • Date: 2009-11-22 02:20:42 UTC
  • mto: (44.3.2 484858-s3-scripts)
  • mto: This revision was merged to the branch mainline in revision 52.
  • Revision ID: duncan@canonical.com-20091122022042-4zi231hxni1z53xd
* Updated the LICENSE file with copyright information.
* Updated the README with license information.

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
import gnomekeyring
8
8
import gobject
9
9
import gtk
10
 
import sys
11
 
    
12
 
try:
13
 
    import appindicator
14
 
    have_appindicator = True
15
 
except:
16
 
    have_appindicator = False
17
10
 
18
11
# DO NOT IMPORT twisted.internet, or things that import
19
12
# twisted.internet.
21
14
 
22
15
from txaws.credentials import AWSCredentials
23
16
 
 
17
 
24
18
__all__ = ["main"]
25
19
 
26
 
class AWSStatusIndicator(object):
 
20
 
 
21
class AWSStatusIcon(gtk.StatusIcon):
 
22
    """A status icon shown when instances are running."""
 
23
 
27
24
    def __init__(self, reactor):
28
 
        # Even though we have appindicator, we may still need the status
29
 
        # icon because we're on something that does not show them.
30
 
        self.status_icon = gtk.StatusIcon()
31
 
        self.status_icon.set_from_stock(gtk.STOCK_NETWORK)
32
 
        self.status_icon.set_visible(True)
33
 
        self.status_icon.connect("activate", self.on_activate)
34
 
        if have_appindicator:
35
 
            self.indicator = appindicator.Indicator("aws-status","stock_weather-cloudy",appindicator.CATEGORY_OTHER)
36
 
            self.indicator.set_status(appindicator.STATUS_PASSIVE)
 
25
        from txaws.service import AWSServiceRegion
 
26
        gtk.StatusIcon.__init__(self)
 
27
        self.set_from_stock(gtk.STOCK_NETWORK)
 
28
        self.set_visible(True)
37
29
        self.reactor = reactor
 
30
        self.connect("activate", self.on_activate)
38
31
        self.probing = False
39
32
        # Nested import because otherwise we get "reactor already installed".
40
33
        self.password_dialog = None
41
 
        self.region = None
42
34
        try:
43
35
            creds = AWSCredentials()
44
36
        except ValueError:
45
37
            creds = self.from_gnomekeyring()
46
 
        if self.region is None:
47
 
            self.set_region(creds)
 
38
        self.region = AWSServiceRegion(creds)
48
39
        self.create_client(creds)
49
40
        menu = """
50
41
            <ui>
51
42
             <menubar name="Menubar">
52
43
              <menu action="Menu">
53
 
               <menuitem action="Refresh"/>
54
44
               <menuitem action="Stop instances"/>
55
 
               <menuitem action="Quit"/>
56
45
              </menu>
57
46
             </menubar>
58
47
            </ui>
59
48
        """
60
49
        actions = [
61
50
            ("Menu",  None, "Menu"),
62
 
            ("Refresh", gtk.STOCK_REFRESH, "_Refresh...", None,
63
 
                "Refresh", self.on_activate),
64
51
            ("Stop instances", gtk.STOCK_STOP, "_Stop instances...", None,
65
52
                "Stop instances", self.on_stop_instances),
66
 
            ("Quit", gtk.STOCK_QUIT, "_Quit...", None,
67
 
                "Quit", self.on_quit),
68
53
            ]
69
54
        ag = gtk.ActionGroup("Actions")
70
55
        ag.add_actions(actions)
73
58
        self.manager.add_ui_from_string(menu)
74
59
        self.menu = self.manager.get_widget(
75
60
            "/Menubar/Menu/Stop instances").props.parent
76
 
        self.status_icon.connect("popup-menu", self.on_popup_menu)
77
 
        if have_appindicator:
78
 
            self.indicator.set_menu(self.menu)
79
 
        # kickstart things
80
 
        self.on_activate(None)
81
 
        self.queue_check()
82
 
 
83
 
    def set_region(self, creds):
84
 
        from txaws.service import AWSServiceRegion
85
 
        self.region = AWSServiceRegion(creds)
 
61
        self.connect("popup-menu", self.on_popup_menu)
86
62
 
87
63
    def create_client(self, creds):
88
64
        if creds is not None:
89
 
            if self.region is None:
90
 
                self.set_region(creds)
91
65
            self.client = self.region.get_ec2_client()
92
66
            self.on_activate(None)
93
67
        else:
96
70
 
97
71
    def from_gnomekeyring(self):
98
72
        # Try for gtk gui specific credentials.
 
73
        creds = None
99
74
        try:
100
75
            items = gnomekeyring.find_items_sync(
101
76
                gnomekeyring.ITEM_GENERIC_SECRET,
117
92
            gtk.STOCK_CANCEL,
118
93
            gtk.RESPONSE_REJECT))
119
94
        content = self.password_dialog.get_content_area()
120
 
 
121
95
        def add_entry(name):
122
96
            box = gtk.HBox()
123
97
            box.show()
146
120
        deferred = self.client.describe_instances()
147
121
        deferred.addCallbacks(self.showhide, self.describe_error)
148
122
 
149
 
    def on_quit(self, data):
150
 
        self.reactor.stop()
151
 
 
152
123
    def on_popup_menu(self, status, button, time):
153
124
        self.menu.popup(None, None, None, button, time)
154
125
 
158
129
        deferred.addCallbacks(self.shutdown_instances, self.show_error)
159
130
 
160
131
    def save_key(self, response_id, data):
 
132
        # handle the dialog
 
133
        dialog = self.password_dialog
161
134
        try:
162
135
            if data != gtk.RESPONSE_ACCEPT:
163
136
                # User cancelled. They can ask for the password again somehow.
183
156
        for instance in reservation:
184
157
            if instance.instance_state == "running":
185
158
                active += 1
186
 
        if active == 0:
187
 
            self.status_icon.set_visible(False)
188
 
            if have_appindicator:
189
 
                self.indicator.set_label("")
190
 
                self.indicator.set_status(appindicator.STATUS_PASSIVE)
191
 
        else:
192
 
            if active == 1:
193
 
                word = "instance"
194
 
            else:
195
 
                word = "instances"
196
 
            self.status_icon.set_tooltip(
197
 
                    "AWS Status - %d %s" % (active,word))
198
 
            self.status_icon.set_visible(True)
199
 
            if have_appindicator:
200
 
                self.indicator.set_label("%d %s" % (active,word), "10 instances")
201
 
                self.indicator.set_status(appindicator.STATUS_ACTIVE)
 
159
        self.set_tooltip("AWS Status - %d instances" % active)
 
160
        self.set_visible(active != 0)
202
161
        self.queue_check()
203
162
 
204
163
    def shutdown_instances(self, reservation):
208
167
 
209
168
    def queue_check(self):
210
169
        self.probing = False
211
 
        self.reactor.callLater(29, self.on_activate, None)
 
170
        self.reactor.callLater(60, self.on_activate, None)
212
171
 
213
172
    def show_error(self, error):
214
173
        # debugging output for now.
245
204
        from twisted.internet import gtk2reactor
246
205
        gtk2reactor.install()
247
206
        from twisted.internet import reactor
248
 
    try:
249
 
        AWSStatusIndicator(reactor)
250
 
        gobject.set_application_name("aws-status")
251
 
        reactor.run()
252
 
    except ValueError:
253
 
        # In this case, the user cancelled, and the exception bubbled to here.
254
 
        pass
 
207
    status = AWSStatusIcon(reactor)
 
208
    gobject.set_application_name("aws-status")
 
209
    reactor.run()