Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/addons/openactivity.py
blob: 924e2823500a730ab8ad2ddfe9d3151d6c3d0354 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# Copyright (C) 2009, Tutorius.org
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import gtk, gtk.gdk

import dbus

from sugar.tutorius.filters import EventFilter
from sugar.tutorius.properties import TStringProperty
from sugar.tutorius import overlayer
from sugar.tutorius.services import ObjectStore

from sugar.activity import activityfactory
from sugar.bundle.activitybundle import ActivityBundle
from jarabe.model import bundleregistry

class OpenActivity(EventFilter):
    """
    OpenActivity
    """
    message = TStringProperty("Message")
    activity_name = TStringProperty("ActivityName")

    def __init__(self, message=None, activity_name=None):
        """Constructor.

        @param message message to display
        @param activity_name name of activity to open
        """
        super(OpenActivity,self).__init__()

        if message:
            self.message = message

        # temporary activity name
        #activity_name = "org.laptop.Terminal"

        if activity_name:
            self.activity_name = activity_name

        self.overlay = None
        self.msgnext = None

    def install_handlers(self, callback, **kwargs):
        """install_handlers creates the open activity message and shows it"""
        super(OpenActivity,self).install_handlers(callback, **kwargs)
        
        # get or inject overlayer
        self.overlay = ObjectStore().activity._overlayer

        if not self.overlay:
            self.overlay = ObjectStore().activity._overlayer

        self.msgnext = None

        self.btntext = "OPEN"

        self.msgnext = overlayer.MsgNext(text=self.message,btntext=self.btntext)

        self.msgnext._btnnext.connect("clicked", self.btnnext_clicked)

        # add space around minimum need size
        wid_width, wid_height = self.msgnext.size_request()
        self.msgnext.set_size_request(wid_width+40, wid_height+40)

        self.msgnext.show()

        # set position
        wid_width, wid_height = self.msgnext.size_request()

        # get position (center of screen)
        screen = gtk.gdk.Screen()
        scr_width_half = screen.get_width()/2
        scr_height_half = screen.get_height()/2

        self.position = (scr_width_half-wid_width/2, scr_height_half-wid_height/2)
        x, y = self.position

        self.overlay.put(self.msgnext, x, y)
        
        self.overlay.queue_draw()
        
    def remove_handlers(self):
        """remove handler removes the open activity message"""
        super(OpenActivity,self).remove_handlers()

        if self.msgnext:
            self.msgnext.destroy()
            self.msgnext = None

    def btnnext_clicked(self, widget):
        if self.activity_name:
            bus = dbus.SessionBus()
            proxy = bus.get_object('org.laptop.Shell', '/org/laptop/Shell')
            path = dbus.Interface(proxy, 'org.laptop.Shell').GetBundlePath(self.activity_name)
            if not path:
                print 'Cannot find %s bundle.' % self.activity_name
            else:
                activity_bundle = ActivityBundle(path)
                activityfactory.create(activity_bundle)
            self.do_callback()
        else:
            self.open_activity_list()

    def open_activity_list(self):
        bus = dbus.SessionBus()
        proxy = bus.get_object('org.laptop.Shell', '/org/laptop/Shell')

        bregistry = bundleregistry.get_registry()
        self.bundles = bregistry._bundles

        self.dlg = gtk.Dialog()

        combobox = gtk.combo_box_new_text()
        self.dlg.add_action_widget(combobox, 0)

        # for each bundle (activity)
        for bundle in self.bundles:
            combobox.append_text(bundle.get_name())

        combobox.connect('changed', self.changed_cb)
        
        self.dlg.show_all()

        return

    def changed_cb(self, combobox):
        model = combobox.get_model()
        index = combobox.get_active()

        if index:
            #print 'About to start ', model[index][0], ' activity'
            activityfactory.create(self.bundles[index])
        
        self.dlg.destroy()

        self.do_callback()

        return

__event__ = {
    "name" : "OpenActivity",
    "display_name" : "Open activity message",
    "icon" : "message-bubble",
    "class" : OpenActivity,
    "mandatory_props" : ["message"]
}