示例#1
0
class XOCom:
    # Constructor gives full XPCom access by default
    # This should be improved for future apps that may not need/want full access
    cometPort = 8889
    ajaxPort = 8890
    handler = None
    return_value = None

    def __init__(self, control_sending_text, uri=None):
        self.cond = Condition()
        #h = hash(Instance.instanceId)
        self.__class__.cometPort = 10
        self.__class__.ajaxPort = 11
        self.cometLogic = ServerLogic(self)
        #self.ajaxServer = ServerThread(self.__class__.ajaxPort, self.cometLogic)
        #self.ajaxServer.start()
        #self.cometServer = ServerThread(self.__class__.cometPort, self.cometLogic)
        #self.cometServer.start()
        if uri:
            self.uri = uri
        else:
            self.uri = 'file://' + get_bundle_path(
            ) + '/web/index.html?ajax=' + str(
                self.__class__.ajaxPort) + '&comet=' + str(
                    self.__class__.cometPort)
        self.control_sending_text = control_sending_text
        self.give_full_xpcom_access()
        self.set_observer()
        ##self.send_to_browser_localize(['initlocalize'])  ## to initialize the localized strings in socialcalc

    # Give the browser permission to use XPCom interfaces
    # This is necessary for XPCom communication to work
    # Note: Not all of these preferences may be required - requires further
    #       investigation
    def give_full_xpcom_access(self):
        pass
        '''pref_class = components.classes["@mozilla.org/preferences-service;1"]
        prefs = pref_class.getService(components.interfaces.nsIPrefService)
        prefs.getBranch('signed.applets.').setBoolPref('codebase_principal_support',
                True);
        prefs.getBranch('capability.principal.').setCharPref(
                        'socialcalc.granted', 'UniversalXPConnect')
        prefs.getBranch('capability.principal.').setCharPref(
                        'socialcalc.id', self.uri)'''

    # Wrapper method to create a new webview embedded browser component
    # Uses hulahop's WebView.  Assumes that you'll want to serve
    # web/index.html relative to your activity directory.
    def create_webview(self):
        self.web_view = WebView()
        self.web_view.connect("navigation-requested",
                              self.on_navigation_requested)
        ##self.uri = 'file://' + get_bundle_path() + '/web/index.html';
        self.web_view.load_uri(self.uri)
        self.web_view.show()
        return self.web_view

    def on_navigation_requested(self, view, frame, req, data=None):
        uri = req.get_uri()
        try:
            scheme, data = uri.split(':#', 1)
        except:
            return False
        if scheme == 'xo-message2':
            if handler is not None:
                data = json.loads(data)
                self.handler(data, 'xo-message2', data[1])
            return True
        elif scheme == 'return-value':
            self.return_value = json.loads(data)
            return True
        else:
            return False

    def set_observer(self):
        #try:
        print 'enter: set_observer'
        self.handler = self.control_sending_text
        print 'exit: set_observer'
    #except:
    #print 'error is there, remove try and except thing'

    # Use XPCom to execute a javascript callback registered with XO.js
    # The command will execute a javascript method registered with the same name,
    # and return any value received from the javascript

    def send_to_browser(self, command, parameter=''):
        if ((command == "read") and (parameter != '')):
            self.web_view.execute_script(
                "XO.observer.setSheet('" + parameter.replace(
                    '\\n', 'DECNEWLINE').replace('\n', 'NEWLINE').replace(
                        "\\", "B_SLASH").replace("'", "\\'") + "');")
            return

        self.web_view.execute_script("XO.observer.observe(['" + parameter +
                                     "'], 'xo-message', '" + command + "');")
        self.handler = self.control_sending_text

        # check if the browser returned anything
        while self.return_value == None:
            while Gtk.events_pending():
                Gtk.main_iteration()
        if self.return_value != '':
            value = self.return_value
            self.return_value = None
            return value
        return None

    def send_to_browser_shared(self, command):
        if command[0] == 'execute':
            self.web_view.execute_script('XO.observe(' + str(command[1:3]) +
                                         ', "xo-message", "execute");')

    def send_to_browser_localize(self, command):
        print 'sending to javascript part to localize\n'
        localstr = "XO.lang=["
        for i, j in localized_strings.iteritems():
            localstr = localstr + "'" + i.replace(
                "'", "\\'") + "','" + j.replace("'", "\\'") + "',"
        localstr = localstr + "'xv'];XO.observe();"
        self.web_view.execute_script(localstr)
        return
示例#2
0
class T2Activity(activity.Activity):
    def xois_clicked(self, widget, data=None):
        self.webview.load_uri('http://teach-teacher.ep.io/slides/1')

    def guide_clicked(self, widget, data=None):
        self.webview.load_uri('http://teach-teacher.ep.io/slides/17')

    def back_clicked(self, widget, data=None):
        if self.webview.get_web_navigation().canGoBack:
            self.webview.get_web_navigation().goBack()

    def home_clicked(self, widget, data=None):
        self.webview.load_uri('http://147.47.120.20/~tsquare/menu.php')

    def __init__(self, handle):
        print "running activity init", handle
        activity.Activity.__init__(self, handle)
        print "activity running"

        self.set_title('Teach Teacher')

        toolbarbox = ToolbarBox()
        self.set_toolbar_box(toolbarbox)

        toolbar = Gtk.Toolbar()

        button = ActivityToolbarButton(self)
        toolbarbox.toolbar.insert(button, -1)

        self.goBack = ToolButton('go-left')
        self.goBack.set_tooltip("Go Back")
        self.goBack.connect('clicked', self.back_clicked)
        toolbar.insert(self.goBack, -1)

        self.home = ToolButton('go-home')
        self.home.set_tooltip("Home")
        self.home.connect('clicked', self.home_clicked)
        toolbar.insert(self.home, -1)

        self.xois = ToolButton('computer-xo')
        self.xois.set_tooltip("T's XO")
        self.xois.connect('clicked', self.xois_clicked)
        # toolbar.insert(self.xois, -1)
        # self.xois.show()

        self.guide = ToolButton('go-next')
        self.guide.set_tooltip("T's Guide")
        self.guide.connect('clicked', self.guide_clicked)
        # toolbar.insert(self.guide, -1)
        # self.guide.show()

        toolbarbox.toolbar.insert(
            ToolbarButton(page=toolbar, icon_name='toolbar-edit'), -1)

        separator = Gtk.SeparatorToolItem()
        separator.props.draw = False
        separator.set_expand(True)
        toolbarbox.toolbar.insert(separator, -1)

        stop_button = StopButton(self)
        toolbarbox.toolbar.insert(stop_button, -1)

        scroll = Gtk.ScrolledWindow()
        self.set_canvas(scroll)

        self.webview = WebView()
        self.webview.load_uri('http://147.47.120.20/~tsquare/menu.php')
        scroll.add(self.webview)

        toolbar.show_all()
        toolbarbox.show_all()
        toolbarbox.toolbar.show_all()
        self.show_all()

        print "AT END OF THE CLASS"
示例#3
0
class T2Activity(activity.Activity):
    def xois_clicked(self, widget, data=None):
        self.webview.load_uri('http://teach-teacher.ep.io/slides/1')

    def guide_clicked(self, widget, data=None):
        self.webview.load_uri('http://teach-teacher.ep.io/slides/17')

    def back_clicked(self, widget, data=None):
        if self.webview.get_web_navigation().canGoBack:
            self.webview.get_web_navigation().goBack()

    def home_clicked(self, widget, data=None):
        self.webview.load_uri('http://147.47.120.20/~tsquare/menu.php')

    def __init__(self, handle):
        print "running activity init", handle
        activity.Activity.__init__(self, handle)
        print "activity running"

        self.set_title('Teach Teacher')

        toolbarbox = ToolbarBox()
        self.set_toolbar_box(toolbarbox)

        toolbar = Gtk.Toolbar()

        button = ActivityToolbarButton(self)
        toolbarbox.toolbar.insert(button, -1)

        self.goBack = ToolButton('go-left')
        self.goBack.set_tooltip("Go Back")
        self.goBack.connect('clicked', self.back_clicked)
        toolbar.insert(self.goBack, -1)

        self.home = ToolButton('go-home')
        self.home.set_tooltip("Home")
        self.home.connect('clicked', self.home_clicked)
        toolbar.insert(self.home, -1)

        self.xois = ToolButton('computer-xo')
        self.xois.set_tooltip("T's XO")
        self.xois.connect('clicked', self.xois_clicked)
        # toolbar.insert(self.xois, -1)
        # self.xois.show()

        self.guide = ToolButton('go-next')
        self.guide.set_tooltip("T's Guide")
        self.guide.connect('clicked', self.guide_clicked)
        # toolbar.insert(self.guide, -1)
        # self.guide.show()

        toolbarbox.toolbar.insert(ToolbarButton(page=toolbar, icon_name='toolbar-edit'), -1)

        separator = Gtk.SeparatorToolItem()
        separator.props.draw = False
        separator.set_expand(True)
        toolbarbox.toolbar.insert(separator, -1)

        stop_button = StopButton(self)
        toolbarbox.toolbar.insert(stop_button, -1)

        scroll = Gtk.ScrolledWindow()
        self.set_canvas(scroll)

        self.webview = WebView()
        self.webview.load_uri('http://147.47.120.20/~tsquare/menu.php')
        scroll.add(self.webview)

        toolbar.show_all()
        toolbarbox.show_all()
        toolbarbox.toolbar.show_all()
        self.show_all()

        print "AT END OF THE CLASS"
示例#4
0
class XOCom:
    # Constructor gives full XPCom access by default
    # This should be improved for future apps that may not need/want full access
    cometPort = 8889
    ajaxPort = 8890
    handler = None
    return_value = None

    def __init__(self, control_sending_text,uri=None):
        self.cond = Condition()
        #h = hash(Instance.instanceId)
        self.__class__.cometPort = 10
        self.__class__.ajaxPort = 11
        self.cometLogic = ServerLogic(self)
        #self.ajaxServer = ServerThread(self.__class__.ajaxPort, self.cometLogic)
        #self.ajaxServer.start()
        #self.cometServer = ServerThread(self.__class__.cometPort, self.cometLogic)
        #self.cometServer.start()
        if uri:
            self.uri = uri
        else:
            self.uri = 'file://' + get_bundle_path() + '/web/index.html?ajax='+str(self.__class__.ajaxPort)+'&comet='+str(self.__class__.cometPort);
        self.control_sending_text=control_sending_text
        self.give_full_xpcom_access()
        self.set_observer()
        ##self.send_to_browser_localize(['initlocalize'])  ## to initialize the localized strings in socialcalc
	
    # Give the browser permission to use XPCom interfaces
    # This is necessary for XPCom communication to work
    # Note: Not all of these preferences may be required - requires further
    #       investigation
    def give_full_xpcom_access(self):
        pass
        '''pref_class = components.classes["@mozilla.org/preferences-service;1"]
        prefs = pref_class.getService(components.interfaces.nsIPrefService)
        prefs.getBranch('signed.applets.').setBoolPref('codebase_principal_support',
                True);
        prefs.getBranch('capability.principal.').setCharPref(
                        'socialcalc.granted', 'UniversalXPConnect')
        prefs.getBranch('capability.principal.').setCharPref(
                        'socialcalc.id', self.uri)'''

    # Wrapper method to create a new webview embedded browser component
    # Uses hulahop's WebView.  Assumes that you'll want to serve
    # web/index.html relative to your activity directory.
    def create_webview(self):
        self.web_view = WebView()
        self.web_view.connect("navigation-requested", self.on_navigation_requested)
        ##self.uri = 'file://' + get_bundle_path() + '/web/index.html';
        self.web_view.load_uri(self.uri)
        self.web_view.show()
        return self.web_view

    def on_navigation_requested(self, view, frame, req, data=None):
        uri = req.get_uri()
        try:
            scheme, data = uri.split(':#', 1)
        except:
            return False
        if scheme == 'xo-message2':
            if handler is not None:
                data = json.loads(data)
                self.handler(data, 'xo-message2', data[1])
            return True
        elif scheme == 'return-value':
            self.return_value = json.loads(data)
            return True
        else:
            return False

    def set_observer(self):
        #try:
            print 'enter: set_observer'
            self.handler = self.control_sending_text;
            print 'exit: set_observer'
        #except:
            #print 'error is there, remove try and except thing'
        
        
    # Use XPCom to execute a javascript callback registered with XO.js
    # The command will execute a javascript method registered with the same name,
    # and return any value received from the javascript
    def send_to_browser(self, command, parameter=''):
        if((command == "read") and (parameter != '')):
            self.web_view.execute_script("XO.observer.setSheet('"+parameter.replace('\\n','DECNEWLINE').replace('\n','NEWLINE').replace("\\","B_SLASH").replace("'","\\'")+"');")
            return

        self.web_view.execute_script("XO.observer.observe(['"+parameter+"'], 'xo-message', '"+command+"');");
        self.handler = self.control_sending_text;

        # check if the browser returned anything
        while self.return_value == None:
            while Gtk.events_pending():
                Gtk.main_iteration()
        if self.return_value != '':
            value = self.return_value
            self.return_value = None
            return value
        return None
    
    def send_to_browser_shared(self,command):
        if command[0]=='execute':
            self.web_view.execute_script('XO.observe(' + str(command[1:3]) + ', "xo-message", "execute");');

    def send_to_browser_localize(self,command):
        print 'sending to javascript part to localize\n'
        localstr = "XO.lang=["
        for i,j in localized_strings.iteritems():
            localstr = localstr+"'"+i.replace("'","\\'")+"','"+j.replace("'","\\'")+"',"
        localstr = localstr+"'xv'];XO.observe();"
        self.web_view.execute_script(localstr)
        return