Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/journalactivity.py
blob: 2777bd5fb8c1ef5abe13f20a6acfa2e5dc3e3e22 (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# Copyright (C) 2006, Red Hat, Inc.
# Copyright (C) 2007, One Laptop Per Child
#
# 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 logging
from gettext import gettext as _

import gobject
import gtk
import dbus

from sugar.activity import activity
from sugar.activity.registry import get_registry as get_activity_registry
from sugar.bundle.bundle import ZipExtractException, RegistrationException
from sugar.datastore import datastore

from journaltoolbox import MainToolbox, DetailToolbox
from listview import ListView
from detailview import DetailView
from volumestoolbar import VolumesToolbar
import backup
import misc

DS_DBUS_SERVICE = 'org.laptop.sugar.DataStore'
DS_DBUS_INTERFACE = 'org.laptop.sugar.DataStore'
DS_DBUS_PATH = '/org/laptop/sugar/DataStore'

J_DBUS_SERVICE = 'org.laptop.Journal'
J_DBUS_INTERFACE = 'org.laptop.Journal'
J_DBUS_PATH = '/org/laptop/Journal'

class JournalActivityDBusService(dbus.service.Object):
    def __init__(self, parent):
        self._parent = parent
        session_bus = dbus.SessionBus()
        bus_name = dbus.service.BusName(J_DBUS_SERVICE,
            bus=session_bus, replace_existing=False, allow_replacement=False)
        logging.debug('bus_name: %r', bus_name)
        dbus.service.Object.__init__(self, bus_name, J_DBUS_PATH)

    @dbus.service.method(J_DBUS_INTERFACE,
        in_signature='a{sv}', out_signature='')
    def FocusSearch(self, search_dict):
        """Set search parameters and grab focus
        search_dict can contain:
            -query: string
            -activity: string that can contain bundle id
            -mimetype: list of strings
            -mtime: a dict of the form:
                 {'start': 1193917107.9856629, 'end': 1193917107.9856629}
            """
        self._parent.present()
        self._parent._show_main_view()
        self._parent.set_search(search_dict)
        self._parent.search_grab_focus()

class JournalActivity(activity.Activity):
    def __init__(self, handle):
        activity.Activity.__init__(self, handle, create_jobject=False)

        self.set_title(_('Journal'))

        self._can_close = False
        self.iconify()

        self._setup_main_view()
        self._setup_secondary_view()
        self.connect('key-press-event', self._key_press_event_cb)
        self.connect('focus-in-event', self._focus_in_event_cb)

        bus = dbus.SessionBus()
        data_store = dbus.Interface(
            bus.get_object(DS_DBUS_SERVICE, DS_DBUS_PATH), DS_DBUS_INTERFACE)
        data_store.connect_to_signal('Created', self._data_store_created_cb)
        data_store.connect_to_signal('Updated', self._data_store_updated_cb)

        self._dbus_service = JournalActivityDBusService(self)

    def _setup_main_view(self):
        self._main_toolbox = MainToolbox()
        self._main_view = gtk.VBox()

        self._list_view = ListView()
        self._list_view.connect('entry-activated', self._entry_activated_cb)
        self._main_view.pack_start(self._list_view)
        self._list_view.show()

        volumes_toolbar = VolumesToolbar()
        volumes_toolbar.connect('volume-changed', self._volume_changed_cb)
        self._main_view.pack_start(volumes_toolbar, expand=False)

        search_toolbar = self._main_toolbox.search_toolbar
        search_toolbar.connect('query-changed', self._query_changed_cb)
        search_toolbar.set_volume_id(datastore.mounts()[0]['id'])

    def _setup_secondary_view(self):
        self._secondary_view = gtk.VBox()

        self._detail_toolbox = DetailToolbox()
        entry_toolbar = self._detail_toolbox.entry_toolbar
        entry_toolbar.connect('entry-erased', self._entry_erased_cb)
        entry_toolbar.connect('go-back-clicked', self._go_back_clicked_cb)

        self._detail_view = DetailView()
        self._secondary_view.pack_end(self._detail_view)
        self._detail_view.show()

    def _key_press_event_cb(self, widget, event):
        keyname = gtk.gdk.keyval_name(event.keyval)
        logging.info(keyname)
        logging.info(event.state)
        if keyname == 'Escape':
            self._show_main_view()

        if event.state & gtk.gdk.MOD1_MASK:
            if gtk.gdk.keyval_name(event.keyval) == 'b':
                backup.start()

    def _entry_activated_cb(self, list_view, entry):
        self._show_secondary_view(entry.jobject)
    
    def _go_back_clicked_cb(self, detail_view):
        self._show_main_view()

    def _query_changed_cb(self, toolbar, query):
        self._list_view.update_with_query(query)
        self._show_main_view()

    def _show_main_view(self):
        self.set_toolbox(self._main_toolbox)
        self._main_toolbox.show()

        self.set_canvas(self._main_view)
        self._main_view.show()

    def _show_secondary_view(self, jobject):
        try:
            self._detail_toolbox.entry_toolbar.set_jobject(jobject)
        except Exception, e:
            logging.error('Exception while displaying entry: %r' % e)

        self.set_toolbox(self._detail_toolbox)
        self._detail_toolbox.show()

        try:
            self._detail_view.set_jobject(jobject)
        except Exception, e:
            logging.error('Exception while displaying entry: %r' % e)

        self.set_canvas(self._secondary_view)
        self._secondary_view.show()

    def _entry_erased_cb(self, toolbar):
        self._show_main_view()

    def _volume_changed_cb(self, volume_toolbar, volume_id):
        logging.debug('Selected volume: %r.' % volume_id)
        self._main_toolbox.search_toolbar.set_volume_id(volume_id)
        self._main_toolbox.set_current_toolbar(0)

    def _data_store_created_cb(self, uid):
        jobject = datastore.get(uid)
        if jobject is None:
            return
        try:
            self._check_for_bundle(jobject)
        finally:
            jobject.destroy()
        self._main_toolbox.search_toolbar.refresh_filters()
        
    def _data_store_updated_cb(self, uid):
        jobject = datastore.get(uid)
        if jobject is None:
            return
        try:
            self._check_for_bundle(jobject)
        finally:
            jobject.destroy()

    def _focus_in_event_cb(self, window, event):
        self.search_grab_focus()

    def _check_for_bundle(self, jobject):
        bundle = misc.get_bundle(jobject)
        if bundle is None:
            return

        if bundle.is_installed():
            return
        try:
            bundle.install()
        except (ZipExtractException, RegistrationException), e:
            logging.warning('Could not install bundle %s: %r' %
                            (jobject.file_path, e))
            return

    def set_search(self, search_dict):
        search_toolbar = self._main_toolbox.search_toolbar
        if 'query' in search_dict:
            search_toolbar._search_entry.set_text(search_dict['query'])
            
    def search_grab_focus(self):
        search_toolbar = self._main_toolbox.search_toolbar
        search_toolbar._search_entry.grab_focus()