Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/help/help.py
blob: 207e3495e599ea5360b78e540b5afc3be618df37 (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# Copyright (C) 2006, Red Hat, Inc.
#
# 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 os, sys
from gettext import gettext as _
from subprocess import Popen, PIPE
import shutil

import gtk
import gobject
import wnck
#from time import time
from sugar import util

from sugar.activity import activity
from sugar.graphics.window import Window
from sugar.graphics.toolbox import Toolbox
from sugar.activity.activityhandle import ActivityHandle
from sugar import wm, env
#from IPython.Debugger import Tracer
from pdb import *
from sugar.graphics.toolbutton import ToolButton

import hulahop
#hulahop.startup(os.path.join(activity.get_activity_root(), 'data/gecko'))
hulahop.startup(os.path.join(os.environ['SUGAR_ACTIVITY_ROOT'], 'data/gecko'))
import xpcom
from xpcom.nsError import *
#from xpcom import components
#from xpcom.components import interfaces
from browser import Browser
from xpcom.components import interfaces

"""#from hulahop.webview import WebView
from browser import Browser
import xpcom
from xpcom.components import interfaces
"""
gobject.threads_init()

HELP_PANE = 1

# Initialize logging.
import logging
_logger = logging.getLogger('HelpTemplate')

class Help(Window):
    def __init__(self, parent):
        global HELP_PANE
        self.parent_obj = parent
        
        if version < 0.838: 
            from hulahop.webview import WebView as Browser
        else:
            from browser import Browser
        
        self.help_id = None
        self.handle = ActivityHandle()
        self.handle.activity_id = util.unique_id()
        Window.__init__(self)
        self.connect('realize',self.realize_cb)

        self._web_view = Browser()

        #determine which language we are going to be using
        help_root = self.get_help_root()
        self.HOME = os.path.join(help_root, 'HelpApi.htm')
    
        self.toolbox = Toolbox()
        self.toolbox.connect('current_toolbar_changed',self.goto_cb)
        self.set_toolbox(self.toolbox)
        self.toolbox.show()
        
        activitybar = gtk.Toolbar()
        self.toolbox.add_toolbar(_('Activity'), activitybar)
        activitybar.show_all()
        
        activitybar = gtk.Toolbar()
        self.toolbox.add_toolbar(_('Edit'), activitybar)
        activitybar.show_all()
        
        activitybar = gtk.Toolbar()
        self.toolbox.add_toolbar(_('View'), activitybar)
        activitybar.show_all()
        
        activitybar = gtk.Toolbar()
        self.toolbox.add_toolbar(_('Tab'), activitybar)
        activitybar.show_all()
        
        self.help_toolbar = Toolbar(self._web_view, self)
        self.help_toolbar.show()
        self.toolbox.add_toolbar(_('Help'), self.help_toolbar)
        #template user, please set following
        HELP_PANE = 4
        self.toolbox._notebook.set_current_page(HELP_PANE)

        self.set_canvas(self._web_view)
        self._web_view.show()

        self.toolbox.set_current_toolbar(HELP_PANE)


    def get_help_toolbar(self):
        return self.help_toolbar

    def realize_help(self):

        #trial and error suggest the following parent activation is necesssary to return reliably to parent window
        if version > 0.839:
            self.pywin = self.get_wnck_window_from_activity_id(str(self.parent_obj.handle.activity_id))
            if self.pywin:
                self.pywin.activate(gtk.get_current_event_time())
                _logger.debug('pywin.activate called')
        self.show_all()
        self.toolbox._notebook.set_current_page(HELP_PANE)
        return self
    
    def realize_cb(self, window):
        self.help_id = util.unique_id()
        wm.set_activity_id(window.window, self.help_id)
        self.help_window = window
        self._web_view.load_uri(self.HOME)
            
    def activate_help(self):
        _logger.debug('activate_help called')
        self.help_window.show()
        if version < 0.838: return
        window = self.get_wnck_window_from_activity_id(self.help_id)
        self.toolbox._notebook.set_current_page(HELP_PANE)
        if window:
            window.activate(gtk.get_current_event_time())
        else:
            _logger.debug('failed to get window')
            
    def close (self):
        self.goto_cb(None, 0)
        self.parent_obj.py_stop()
            
    def goto_cb(self, page, tab):
        _logger.debug('current_toolbar_changed event called goto_cb. tab: %s'%tab)
        if tab == HELP_PANE: return
        if not self.help_id: return
        self.parent_obj.set_toolbar(tab)
        self.help_window.hide()
        if version < 0.838: return
        self.pywin = self.get_wnck_window_from_activity_id(str(self.parent_obj.handle.activity_id))
        if self.pywin:
            self.pywin.activate(gtk.get_current_event_time())

    def get_wnck_window_from_activity_id(self, activity_id):
        """Use shell model to look up the wmck window associated with activity_id
           --the home_model code changed between .82 and .84 sugar
           --so do the lookup differently depending on sugar version
        """
        _logger.debug('get_wnck_window_from_activity_id. id:%s'%activity_id)
        _logger.debug('sugar version %s'%version)
        if version and version >= 0.839:
            home_model = shell.get_model()
            activity = home_model.get_activity_by_id(activity_id)
        else:
            instance = view.Shell.get_instance()
            home_model = instance.get_model().get_home()
            activity = home_model._get_activity_by_id(activity_id)
        if activity:
            return activity.get_window()
        else:
            _logger.debug('wnck_window was none')
            return None
        
    def get_help_root(self):
        lang = os.environ.get('LANGUAGE')
        if not lang:
            lang = os.environ.get('LANG')
        if not lang:
            lang = 'en_US'
        if len(lang) > 1:
            two_char = lang[:2]
        else:
            two_char = ''
        root = os.path.join(os.environ['SUGAR_BUNDLE_PATH'],'help',two_char)
        if os.path.isdir(root):
            return root
        root = os.path.join(os.environ['SUGAR_ACTIVITY_ROOT'],'help',two_char)
        if os.path.isdir(root):
            return root
        #default to a non localized root
        root = os.path.join(os.environ['SUGAR_BUNDLE_PATH'],'help')
        return root
    
class Toolbar(gtk.Toolbar):
    def __init__(self, web_view, _help):
        gobject.GObject.__init__(self)

        self._web_view = web_view
        self._help = _help

        self._back = ToolButton('go-previous-paired')
        self._back.set_tooltip(_('Back'))
        self._back.props.sensitive = False
        self._back.connect('clicked', self._go_back_cb)
        self.insert(self._back, -1)
        self._back.show()

        self._forward = ToolButton('go-next-paired')
        self._forward.set_tooltip(_('Forward'))
        self._forward.props.sensitive = False
        self._forward.connect('clicked', self._go_forward_cb)
        self.insert(self._forward, -1)
        self._forward.show()

        home = ToolButton('zoom-home')
        home.set_tooltip(_('Home'))
        home.connect('clicked', self._go_home_cb)
        self.insert(home, -1)
        home.show()

        separator = gtk.SeparatorToolItem()
        separator.set_draw(False)
        separator.set_expand(True)
        self.insert(separator, -1)
        separator.show()

        stop_button = ToolButton('activity-stop')
        stop_button.set_tooltip(_('Stop'))
        #stop_button.props.accelerator = '<Ctrl>Q'
        stop_button.connect('clicked', self.__stop_clicked_cb)
        self.insert(stop_button, -1)
        stop_button.show()
        
        if version < 0.838:
            self._listener = xpcom.server.WrapObject(EarlyListener(self),
                                                     interfaces.nsIWebProgressListener)
            weak_ref = xpcom.client.WeakReference(self._listener)
    
            mask = interfaces.nsIWebProgress.NOTIFY_STATE_NETWORK | \
                   interfaces.nsIWebProgress.NOTIFY_LOCATION
            self._web_view.web_progress.addProgressListener(self._listener, mask)
        else:
        
            progress_listener = self._web_view.progress
            progress_listener.connect('location-changed',
                                      self._location_changed_cb)
            progress_listener.connect('loading-stop', self._loading_stop_cb)

        

    def __stop_clicked_cb(self, button):
        self._help.close()
        
    def _location_changed_cb(self, progress_listener, uri):
        self.update_navigation_buttons()

    def _loading_stop_cb(self, progress_listener):
        self.update_navigation_buttons()
        
    def update_navigation_buttons(self):
        can_go_back = self._web_view.web_navigation.canGoBack
        self._back.props.sensitive = can_go_back

        can_go_forward = self._web_view.web_navigation.canGoForward
        self._forward.props.sensitive = can_go_forward

    def _go_back_cb(self, button):
        self._web_view.web_navigation.goBack()
    
    def _go_forward_cb(self, button):
        self._web_view.web_navigation.goForward()

    def _go_home_cb(self, button):
        self._web_view.load_uri(HOME)

class EarlyListener(object):
    #from xpcom.components import interfaces

    _com_interfaces_ = interfaces.nsIWebProgressListener

    def __init__(self, toolbar):
        self._toolbar = toolbar
    
    def onLocationChange(self, webProgress, request, location):
        self._toolbar.update_navigation_buttons()
        
    def onProgressChange(self, webProgress, request, curSelfProgress,
                         maxSelfProgress, curTotalProgress, maxTotalProgress):
        pass
    
    def onSecurityChange(self, webProgress, request, state):
        pass
        
    def onStateChange(self, webProgress, request, stateFlags, status):
        if stateFlags & interfaces.nsIWebProgressListener.STATE_IS_NETWORK:
            self._toolbar.update_navigation_buttons()

    def onStatusChange(self, webProgress, request, status, message):
        pass
  

def command_line(cmd):
    _logger.debug('command_line cmd:%s'%cmd)
    p1 = Popen(cmd,stdout=PIPE, shell=True)
    output = p1.communicate()
    if p1.returncode != 0:
        return None
    return output[0]
    
def sugar_version():
    cmd = '/bin/rpm -q sugar'
    reply = command_line(cmd)
    if reply and reply.find('sugar') > -1:
        version = reply.split('-')[1]
        version_chunks = version.split('.')
        major_minor = version_chunks[0] + '.' + version_chunks[1]
        return float(major_minor) 
    return None

version = 0.0
version = sugar_version() 
if version and version >= 0.839:
    from jarabe.model import shell
else:
    if not '/usr/share/sugar/shell/' in sys.path:
        sys.path.append('/usr/share/sugar/shell/')
    import view.Shell