Example #1
0
    def __init__(self, session_bus, appid, dbus_name, dbus_path):
        dbus.service.Object.__init__(self, session_bus, dbus_path)

        self.appid = appid
        self.dbus_name = dbus_name
        self.dbus_path = dbus_path
        
        # WARING: only use once in one process
        DBusGMainLoop(set_as_default=True) 
        
        self.plug = gtk.Plug(0)

        self.webview = WebView(COOKIE_FILE)
        self.webview.connect('title-changed', self.webview_message_handler)
        self.webview.connect('script-alert', self.webview_message_handler)
        #self.webview.enable_inspector()
        self.paned_box = PanedBox(2, True, 2, True)
        self.paned_box.enter_bottom_win_callback = self.enter_bottom_notify
        self.paned_box.enter_top_win_callback = self.enter_top_notify
        self.paned_box.add_content_widget(self.webview)
        self.plug.add(self.paned_box)
        self.plug.connect('delete-event', self.plug_delete_event)
        
        # Handle signals.
        self.plug.connect("realize", self.flash_frame_realize)
        self.plug.connect("destroy", self.flash_frame_exit)

        glib.timeout_add(1000, self.is_exist)
        glib.timeout_add(200, self.connect_signal)

        def message_receiver(self, *message):
            message_type, contents = message
            if message_type == 'exit':
                self.exit()
            elif message_type == 'load_uri':
                self.load_flash(contents)
            elif message_type == 'load_loading_uri':
                self.webview.load_uri(contents)
                self.send_message('loading_uri_finish', '')
            elif message_type == 'load_string':
                self.webview.load_string(contents)
            elif message_type == 'get_plug_id':
                self.send_flash_info()
            elif message_type == 'app_info_download_finish':
                self.webview.execute_script("app_info=%s" %
                        json.dumps(str(contents), encoding="UTF-8", ensure_ascii=False))

        setattr(FlashFrame, 
                'message_receiver', 
                dbus.service.method(dbus_name)(message_receiver))
Example #2
0
    def init_ui(self):
        
        self.application = PlayerApplication(destroy_func=self.quit, max_callback=self.max_callback)
        if self.width+12 < 642:
            width = 642 + 220
        else:
            width = self.width + 12 + 220
        self.application.set_default_size(width, self.height+73)
        self.application.set_skin_preview(get_common_image("frame.png"))
        self.application.set_icon(get_common_image("logo48.png"))
        self.application.add_titlebar(
                ["mode", "min", "max", "close"],
                )
        player_title = _("Deepin Games - %s ") % self.game_name
        self.window = self.application.window
        self.window.set_title(player_title)
        self.application.titlebar.change_name(player_title)
        self.application.titlebar.mode_button.set_active(True)
        self.application.titlebar.mode_button.connect('toggled', self.change_view)
        self.application.titlebar.close_button.connect('clicked', self.quit)
        #self.application.window.connect('focus-out-event', self.window_out_focus_hander)
        self.application.window.set_position(gtk.WIN_POS_CENTER)
        self.application.window.connect('focus-in-event', self.window_in_focus_hander)
        self.application.window.connect('window-state-event', self.window_state_change_handler)
        self.application.window.connect('key-press-event', self.window_key_press_handler)

        self.page_box = gtk.HBox()
        self.content_page = ContentPage(self.appid)
        self.content_page.screenshot_box.connect('button-press-event', self.external_continue_action)
        self.content_page.play_button.connect('button-press-event', self.external_continue_action)

        self.guide_box = GuideBox()
        self.guide_box.set_size_request(220, -1)
        
        self.page_align = gtk.Alignment()
        self.page_align.set(0.5, 0.5, 1, 1)
        self.page_align.set_padding(0, 0, 2, 2)
        
        self.control_toolbar = self.create_toolbar(["star"])
        self.page_box.pack_start(self.content_page)
        self.page_box.pack_start(self.guide_box, False)

        self.inner_top_titlebar = self.create_top_titlebar()
        self.inner_top_titlebar.change_name(player_title)
        inner_widgets = ControlToolbar.widgets_id[:]
        inner_widgets.remove("star")
        self.inner_control_toolbar = self.create_toolbar(inner_widgets)

        self.paned_box = PanedBox()
        self.paned_box.add_content_widget(self.page_box)
        self.paned_box.add_bottom_widget(self.inner_control_toolbar)
        self.paned_box.add_top_widget(self.inner_top_titlebar)

        self.page_align.add(self.paned_box)
        self.application.main_box.pack_start(self.page_align)
        self.application.window.add_move_event(self.control_toolbar)
        self.show_bottom = False
        self.display_normal()
Example #3
0
class Player(dbus.service.Object):
    def __init__(self, session_bus, argv, dbus_name, dbus_path):
        dbus.service.Object.__init__(self, session_bus, dbus_path)

        (self.appid, self.game_name, self.width, self.height, self.swf_url,
         self.resizable) = argv
        self.game_name = urllib.unquote(self.game_name)
        self.width = int(self.width)
        self.height = int(self.height)
        self.plug_status = False
        self.conf_db = get_config_file("conf.db")
        self.p = None
        self.current_sink_index = None
        #self.sound_manager = SoundSetting(self.sound_sink_callback)
        self.loading = True
        self.hand_pause = False
        self.game_pause = False
        self.fullscreen_state = False
        self.guide_box_expand = True
        self.window_normal_info = {}
        self.maximize_state = False
        self.share_show = False

        self.call_flash_game(self.appid)
        self.init_ui()
        self.plug_id = None

        def unique(self):
            self.application.window.present()

        def message_receiver(self, *message):
            message_type, contents = message
            if message_type == 'send_plug_id':
                self.plug_id = int(str(contents[1]))
                self.content_page.add_plug_id(self.plug_id)
                self.plug_status = True
                self.start_loading()
            elif message_type == 'loading_uri_finish':
                fetch_service.add_missions([self.download_task])
            elif message_type == 'enter_bottom':
                if self.show_bottom:
                    self.paned_box.bottom_window.show()
            elif message_type == 'enter_top':
                if self.show_top:
                    self.paned_box.top_window.show()
            elif message_type == 'onload_loading':
                self.start_download_swf()
            elif message_type == 'game_action':
                if contents == 'pause':
                    self.command_pause_game()

        setattr(Player, 'unique', dbus.service.method(dbus_name)(unique))
        setattr(Player, 'message_receiver',
                dbus.service.method(dbus_name)(message_receiver))
        setattr(Player, 'update_signal',
                dbus.service.signal(dbus_name)(self.update_signal))

        self.send_message('get_plug_id', '')

    def sound_sink_callback(self, dt, index):
        sound_id = int(dt['proplist']['application.process.id'])
        if self.p and sound_id == self.p.pid:
            self.current_sink_index = index

    def update_signal(self, obj, data=None):
        pass

    def init_ui(self):

        self.application = PlayerApplication(destroy_func=self.quit,
                                             max_callback=self.max_callback)
        if self.width + 12 < 642:
            width = 642 + 220
        else:
            width = self.width + 12 + 220
        self.application.set_default_size(width, self.height + 73)
        self.application.set_skin_preview(get_common_image("frame.png"))
        self.application.set_icon(get_common_image("logo48.png"))
        self.application.add_titlebar(["mode", "min", "max", "close"], )
        player_title = _("Deepin Game - %s ") % self.game_name
        self.window = self.application.window
        self.window.set_title(player_title)
        self.application.titlebar.change_name(player_title)
        self.application.titlebar.mode_button.set_active(True)
        self.application.titlebar.mode_button.connect('toggled',
                                                      self.change_view)
        self.application.titlebar.close_button.connect('clicked', self.quit)
        #self.application.window.connect('focus-out-event', self.window_out_focus_hander)
        self.application.window.set_position(gtk.WIN_POS_CENTER)
        self.application.window.connect('focus-in-event',
                                        self.window_in_focus_hander)
        self.application.window.connect('window-state-event',
                                        self.window_state_change_handler)
        self.application.window.connect('key-press-event',
                                        self.window_key_press_handler)

        self.page_box = gtk.HBox()
        self.content_page = ContentPage(self.appid)
        self.content_page.screenshot_box.connect('button-press-event',
                                                 self.external_continue_action)
        self.content_page.play_button.connect('button-press-event',
                                              self.external_continue_action)

        self.guide_box = GuideBox()
        self.guide_box.set_size_request(220, -1)

        self.page_align = gtk.Alignment()
        self.page_align.set(0.5, 0.5, 1, 1)
        self.page_align.set_padding(0, 0, 2, 2)

        self.control_toolbar = self.create_toolbar(["star"])
        self.page_box.pack_start(self.content_page)
        self.page_box.pack_start(self.guide_box, False)

        self.inner_top_titlebar = self.create_top_titlebar()
        self.inner_top_titlebar.change_name(player_title)
        inner_widgets = ControlToolbar.widgets_id[:]
        inner_widgets.remove("star")
        self.inner_control_toolbar = self.create_toolbar(inner_widgets)

        self.paned_box = PanedBox()
        self.paned_box.add_content_widget(self.page_box)
        self.paned_box.add_bottom_widget(self.inner_control_toolbar)
        self.paned_box.add_top_widget(self.inner_top_titlebar)

        self.page_align.add(self.paned_box)
        self.application.main_box.pack_start(self.page_align)
        self.application.window.add_move_event(self.control_toolbar)
        self.show_bottom = False
        self.display_normal()

    def window_key_press_handler(self, widget, event):
        if self.fullscreen_state:
            from dtk.ui.keymap import get_keyevent_name
            if get_keyevent_name(event, True) == 'Escape':
                self.fullscreen_handler(self.control_toolbar.fullscreen_button,
                                        None)

    def create_top_titlebar(
        self,
        button_mask=['min', "close"],
        icon_path=None,
        app_name=None,
        title=None,
        add_separator=False,
        show_title=True,
        enable_gaussian=True,
        name_size=DEFAULT_FONT_SIZE,
        title_size=DEFAULT_FONT_SIZE,
    ):
        titlebar = Titlebar(
            button_mask,
            icon_path,
            app_name,
            title,
            add_separator,
            show_title=show_title,
            enable_gaussian=enable_gaussian,
            name_size=name_size,
            title_size=title_size,
        )
        if "min" in button_mask:
            titlebar.min_button.connect("clicked",
                                        lambda w: self.window.min_window())
        if "close" in button_mask:
            titlebar.close_button.connect("clicked", self.quit)

        return titlebar

    def create_toolbar(self, widget_display=[]):
        control_toolbar = ControlToolbar(self.appid, widget_display)
        control_toolbar.mute_button.connect('clicked', self.mute_handler)
        control_toolbar.pause_button.connect('button-press-event',
                                             self.pause_handler)
        control_toolbar.replay_button.connect('clicked', self.replay_action)
        control_toolbar.fullscreen_button.connect('button-release-event',
                                                  self.fullscreen_handler)
        control_toolbar.share_button.connect('clicked', self.share_action)
        control_toolbar.favorite_button.connect('button-release-event',
                                                self.favorite_action)
        control_toolbar.leave_callback = self.leave_callback

        if os.path.exists(self.conf_db):
            data = utils.load_db(self.conf_db)
            if data.get('favorite') and self.appid in data['favorite']:
                control_toolbar.favorite_button.set_active(True)

        return control_toolbar

    def favorite_action(self, widget, event):
        if not widget.get_active():
            record_info.record_favorite(self.appid, self.conf_db)
        else:
            record_info.remove_favorite(self.appid, self.conf_db)

    def leave_callback(self, widget, e):
        if self.fullscreen_state:
            if e.window == self.paned_box.bottom_window:
                self.paned_box.bottom_window.hide()
            elif e.window == self.paned_box.top_window:
                self.paned_box.top_window.hide()

    def window_state_change_handler(self, widget, event):
        if event.new_window_state == gtk.gdk.WINDOW_STATE_ICONIFIED:
            self.external_pause_action()

    def external_pause_action(self):
        if not self.loading and (not self.hand_pause and not self.game_pause):
            self.control_toolbar.pause_button.set_active(True)
            self.inner_control_toolbar.pause_button.set_active(False)
            self.toggle_pause_action(self.control_toolbar.pause_button)

    def external_continue_action(self, widget=None, event=None):
        if not self.loading and self.game_pause:
            self.control_toolbar.pause_button.set_active(False)
            self.inner_control_toolbar.pause_button.set_active(False)
            self.toggle_pause_action(self.control_toolbar.pause_button)

    def window_in_focus_hander(self, widget, event):
        #print "In: hand=>%s, pause=>%s" % (self.hand_pause, self.game_pause)
        if not self.loading and (not self.hand_pause and self.game_pause):
            self.control_toolbar.pause_button.set_active(False)
            self.toggle_pause_action(self.control_toolbar.pause_button)

    def window_out_focus_hander(self, widget, event):
        #print "Out: hand=>%s, pause=>%s" % (self.hand_pause, self.game_pause)
        min_state = self.window.get_state() == gtk.gdk.WINDOW_STATE_ICONIFIED
        if not self.loading and (not self.hand_pause
                                 and not self.game_pause) and min_state:
            self.control_toolbar.pause_button.set_active(True)
            self.toggle_pause_action(self.control_toolbar.pause_button)

    def change_view(self, widget):
        screen = gtk.gdk.screen_get_default()
        screen_w, screen_h = screen.get_width(), screen.get_height()
        width, height = self.window.get_size()

        DEFAULT_HEIGHT = height

        if not widget.get_active():
            self.guide_box_expand = False
            DEFAULT_WIDTH = width - 220
            self.guide_box.hide_all()
            self.guide_box.set_no_show_all(True)

        else:
            self.guide_box_expand = True
            DEFAULT_WIDTH = width + 220
            self.guide_box.set_no_show_all(False)
            self.guide_box.show_all()

        if self.maximize_state:
            DEFAULT_WIDTH = screen_w

        self.window.set_default_size(DEFAULT_WIDTH, DEFAULT_HEIGHT)
        self.window.set_geometry_hints(None, DEFAULT_WIDTH, DEFAULT_HEIGHT,
                                       screen_w, screen_h, -1, -1, -1, -1, -1,
                                       -1)
        self.window.resize(DEFAULT_WIDTH, DEFAULT_HEIGHT)

    def max_callback(self, widget):
        screen = gtk.gdk.screen_get_default()
        screen_w, screen_h = screen.get_width(), screen.get_height()

        window_state = self.window.window.get_state()
        if window_state & gtk.gdk.WINDOW_STATE_MAXIMIZED == gtk.gdk.WINDOW_STATE_MAXIMIZED:
            self.maximize_state = False
            self.window.unmaximize()

            if self.window_normal_info:
                if self.window_normal_info[
                        'guide_box_expand'] == self.guide_box_expand:
                    width, height = self.window_normal_info[
                        'width'], self.window_normal_info['height']
                else:
                    if self.window_normal_info['guide_box_expand']:
                        p_width, p_height = self.window_normal_info[
                            'width'], self.window_normal_info['height']
                        width = p_width - 220
                        height = p_height
                    else:
                        p_width, p_height = self.window_normal_info[
                            'width'], self.window_normal_info['height']
                        width = p_width + 220
                        height = p_height

                self.window.set_geometry_hints(None, width, height, screen_w,
                                               screen_h, -1, -1, -1, -1, -1,
                                               -1)
                self.window.resize(width, height)

        else:
            self.maximize_state = True
            self.window_normal_info['guide_box_expand'] = copy.deepcopy(
                self.guide_box_expand)
            self.window_normal_info['width'] = copy.deepcopy(
                self.window.get_size()[0])
            self.window_normal_info['height'] = copy.deepcopy(
                self.window.get_size()[1])
            self.window.maximize()

    def quit(self, widget=None, data=None):
        if self.game_pause:
            os.system('kill -9 %s' % self.p.pid)
        gtk.main_quit()

    def display_normal(self):
        self.application.show_titlebar()
        self.guide_box.show_all()
        self.application.main_box.pack_start(self.control_toolbar, False,
                                             False)
        self.application.window.show_window()
        if getattr(self.paned_box, "bottom_window"):
            self.paned_box.bottom_window.hide()
        if getattr(self.paned_box, 'top_window'):
            self.paned_box.top_window.hide()

    def fullscreen_handler(self, widget, data=None):
        self.control_toolbar.fullscreen_button.set_state(gtk.STATE_NORMAL)
        self.inner_control_toolbar.fullscreen_button.set_state(
            gtk.STATE_NORMAL)
        if self.fullscreen_state:
            self.fullscreen_state = False
            self.display_normal()
            self.show_bottom = False
            self.show_top = False
            self.page_align.set_padding(0, 0, 2, 2)
            self.application.window.unfullscreen()
            self.control_toolbar.fullscreen_button.set_active(False)
        else:
            self.fullscreen_state = True
            # for fullscreen mode
            self.paned_box.connect('leave-notify-event', self.leave_callback)
            self.paned_box.paint_bottom_window = self.__paint_bottom_toolbar_background
            self.paned_box.paint_top_window = self.__paint_top_titlebar_background
            self.show_bottom = True
            self.show_top = True

            self.application.hide_titlebar()
            self.application.main_box.remove(self.control_toolbar)
            self.control_toolbar.show_all()
            self.application.window.show_window()
            self.guide_box.hide_all()
            self.page_align.set_padding(0, 0, 0, 0)
            self.application.window.fullscreen()
            self.inner_control_toolbar.fullscreen_button.set_active(True)

    def mute_handler(self, widget, data=None):
        #if self.current_sink_index:
        #pypulse.PULSE.set_sink_input_mute(self.current_sink_index, widget.get_active())
        pass

    def replay_action(self, widget, data=None):
        if self.game_pause:
            self.control_toolbar.pause_button.set_active(False)
            self.toggle_pause_action(self.control_toolbar.pause_button)
        if not self.loading:
            self.update_signal([
                'load_uri',
                'file://%s,%s,%s' %
                (self.swf_save_path, self.width, self.height)
            ])

    def toggle_pause_action(self, widget):
        if widget.get_active():
            self.update_signal(['game_action', 'pause'])
        else:
            self.command_continue_game()
        self.game_pause = widget.get_active()

    def content_socket_press_handler(self, widget, event):
        print event

    def pause_handler(self, widget, data=None):
        if not self.game_pause:
            self.update_signal(['game_action', 'pause'])
            self.hand_pause = True
            self.game_pause = True
        else:
            self.command_continue_game()
            self.hand_pause = False
            self.game_pause = False

    def command_pause_game(self):
        screenshot_pixbuf = self.get_game_screenshot_pixbuf()
        self.content_page.remove_socket(screenshot_pixbuf)
        os.system('kill -STOP %s' % self.p.pid)

    def command_continue_game(self):
        os.system('kill -CONT %s' % self.p.pid)
        self.content_page.add_plug_id(self.plug_id)
        #self.update_signal(['game_action', 'continue'])

    def fullscreen_action(self, widget, data=None):
        pass

    def get_game_screenshot_pixbuf(self):
        if self.fullscreen_state:
            self.paned_box.bottom_window.hide()

        rect = self.content_page.get_allocation()
        #rect = self.window.get_allocation()
        width = rect.width
        height = rect.height
        pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, width,
                                height)
        pixbuf = pixbuf.get_from_drawable(
            self.content_page.socket_box.window,
            self.content_page.window.get_colormap(), rect.x, rect.y, 0, 0,
            width, height)
        return pixbuf

    def share_action(self, widget, data=None):
        if not self.share_show:
            #rect = self.content_page.get_allocation()
            rect = self.window.get_allocation()
            width = rect.width
            height = rect.height
            pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, width,
                                    height)
            pixbuf = pixbuf.get_from_drawable(self.window.window,
                                              self.window.get_colormap(),
                                              rect.x, rect.y, 0, 0, width,
                                              height)

            filename = self.save_to_tmp_file(pixbuf)
            share_path = os.path.join(get_parent_dir(__file__), 'share.py')

            order = ['python2', share_path, filename]
            try:
                self.share_p = subprocess.Popen(order,
                                                stderr=subprocess.STDOUT,
                                                shell=False)
            except OSError:
                order[0] = 'python'
                self.share_p = subprocess.Popen(order,
                                                stderr=subprocess.STDOUT,
                                                shell=False)
            self.share_show = True
            self.external_pause_action()
            self.hand_pause = True
            gtk.timeout_add(200, self.check_share_dialog_close)

    def check_share_dialog_close(self):
        if getattr(self, 'share_p'):
            if self.share_p.poll() == 0:
                self.share_show = False
                self.hand_pause = False
                return False
        return True

    def save_to_tmp_file(self, pixbuf):
        from tempfile import mkstemp
        import os
        tmp = mkstemp(".tmp", PROGRAM_NAME)
        os.close(tmp[0])
        filename = tmp[1]
        pixbuf.save(filename, "jpeg", {"quality": "100"})
        return filename

    def start_loading(self):
        global_event.register_event('download-app-info-finish',
                                    self.app_info_download_finish)
        FetchInfo(self.appid).start()
        self.swf_save_path = os.path.expanduser(
            "~/.cache/deepin-game-center/downloads/%s/%s" %
            (self.appid, os.path.split(self.swf_url)[1]))
        if os.path.exists(self.swf_save_path):
            gtk.timeout_add(200, lambda: self.load_game())
        else:
            gtk.timeout_add(200, self.show_loading_page)

    def show_loading_page(self):
        touch_file_dir(self.swf_save_path)
        print "Touch file dir:", self.swf_save_path
        self.load_html_path = os.path.join(static_dir, 'loading.html')
        self.send_message('load_loading_uri', 'file://' + self.load_html_path)
        print "Send show loading message:", self.load_html_path

    def start_download_swf(self):
        self.remote_path = urllib.basejoin(GAME_CENTER_DATA_ADDRESS,
                                           self.swf_url)
        utils.ThreadMethod(urllib.urlretrieve,
                           (self.remote_path, self.swf_save_path + '_tmp',
                            self.report_hook)).start()
        print "Downloading: %s" % self.remote_path

        #self.download_task = TaskObject(self.remote_path, self.swf_save_path, verbose=True)
        #self.download_task.connect("update", self.download_update)
        #self.download_task.connect("finish", self.download_finish)
        #self.download_task.connect("error",  self.download_failed)
        #self.download_task.connect("start",  self.download_start)

    def report_hook(self, block_count, block_size, total_size):
        current = block_count * block_size
        if current < total_size:
            percent = str(int(float(current) / total_size * 100))
            self.update_signal(['download_update', percent])
        else:
            self.update_signal(['download_update', '100'])
            os.system('mv %s %s' %
                      (self.swf_save_path + '_tmp', self.swf_save_path))
            self.load_game()
            #gtk.timeout_add(500, lambda:self.load_game())

    def load_game(self):
        self.loading = False
        self.control_toolbar.show_toolbar_button(
            ['pause', 'replay', 'favorite', 'fullscreen', 'share'])
        self.update_signal([
            'download_finish',
            'file://%s,%s,%s' % (self.swf_save_path, self.width, self.height)
        ])
        record_info.record_recent_play(self.appid, self.conf_db)

        #self.loading = False
        #self.send_message('load_uri', 'file://%s,%s,%s' % (self.swf_save_path, self.width, self.height))
        #record_info.record_recent_play(self.appid, self.conf_db)

    def run(self):
        self.application.window.show_window()
        #self.paned_box.bottom_window.set_composited(True)
        gtk.main()

    def app_info_download_finish(self, js):
        gtk.timeout_add(500, self.import_infos, js)

    def import_infos(self, infos):
        if not self.loading:
            self.send_message('app_info_download_finish', infos)
            return False
        return True

    def call_flash_game(self, local_path):
        flash_frame_path = os.path.join(get_parent_dir(__file__),
                                        'flash_frame.py')
        order = ['python2', flash_frame_path, self.appid]
        try:
            self.p = subprocess.Popen(order,
                                      stderr=subprocess.STDOUT,
                                      shell=False)
        except OSError:
            order[0] = 'python'
            self.p = subprocess.Popen(order,
                                      stderr=subprocess.STDOUT,
                                      shell=False)

    def send_message(self, message_type, contents):
        bus = dbus.SessionBus()
        flash_dbus_name = "com.deepin.game_flash_%s" % (self.appid)
        flash_dbus_path = "/com/deepin/game_flash_%s" % (self.appid)
        if is_dbus_name_exists(flash_dbus_name):
            bus_object = bus.get_object(flash_dbus_name, flash_dbus_path)
            method = bus_object.get_dbus_method("message_receiver")
            method(message_type,
                   contents,
                   reply_handler=handle_dbus_reply,
                   error_handler=handle_dbus_error)

    def download_update(self, task, data):
        #print "%s/s" % utils.get_human_size(data.speed)
        progress = "%d" % data.progress
        self.update_signal(['download_update', progress])

    def download_start(self, task, data):
        pass

    def download_finish(self, task, data):
        gtk.timeout_add(500, lambda: self.load_game())

    def download_failed(self, task, data):
        pass

    def __paint_bottom_toolbar_background(self, e):
        # 将皮肤的图片画在bottom toolbar上,作为背景.
        cr = e.window.cairo_create()
        bottom_size = e.window.get_size()
        # draw background.
        cr.set_source_rgba(*alpha_color_hex_to_cairo(("#ebebeb", 0.1)))
        cr.rectangle(0, 0, bottom_size[0], bottom_size[1])
        cr.fill()
        # draw background pixbuf.
        pixbuf = skin_config.background_pixbuf
        app_h = self.application.window.allocation.height
        app_w = self.application.window.allocation.width
        bottom_h = bottom_size[1]
        # 当图片的高度小雨窗口高度的时候,只拿出图片的最尾巴.
        if pixbuf.get_height() > app_h + bottom_h:
            h = app_h
        else:
            h = pixbuf.get_height() - bottom_h
        # 当图片小于窗口宽度的时候,拉伸图片.
        if pixbuf.get_width() < app_w:
            pixbuf = pixbuf.scale_simple(app_w, pixbuf.get_width(),
                                         gtk.gdk.INTERP_BILINEAR)

        draw_pixbuf(cr, pixbuf, 0, -(h))

    def __paint_top_titlebar_background(self, e):
        # 将皮肤的图片画在bottom toolbar上,作为背景.
        cr = e.window.cairo_create()
        bottom_size = e.window.get_size()
        # draw background.
        cr.set_source_rgba(*alpha_color_hex_to_cairo(("#ebebeb", 0.1)))
        cr.rectangle(0, 0, bottom_size[0], bottom_size[1])
        cr.fill()
        # draw background pixbuf.
        pixbuf = skin_config.background_pixbuf
        app_w = self.application.window.allocation.width
        # 当图片小于窗口宽度的时候,拉伸图片.
        if pixbuf.get_width() < app_w:
            pixbuf = pixbuf.scale_simple(app_w, pixbuf.get_width(),
                                         gtk.gdk.INTERP_BILINEAR)

        draw_pixbuf(cr, pixbuf, 0, 0)
Example #4
0
    def init_ui(self):

        self.application = PlayerApplication(destroy_func=self.quit,
                                             max_callback=self.max_callback)
        if self.width + 12 < 642:
            width = 642 + 220
        else:
            width = self.width + 12 + 220
        self.application.set_default_size(width, self.height + 73)
        self.application.set_skin_preview(get_common_image("frame.png"))
        self.application.set_icon(get_common_image("logo48.png"))
        self.application.add_titlebar(["mode", "min", "max", "close"], )
        player_title = _("Deepin Game - %s ") % self.game_name
        self.window = self.application.window
        self.window.set_title(player_title)
        self.application.titlebar.change_name(player_title)
        self.application.titlebar.mode_button.set_active(True)
        self.application.titlebar.mode_button.connect('toggled',
                                                      self.change_view)
        self.application.titlebar.close_button.connect('clicked', self.quit)
        #self.application.window.connect('focus-out-event', self.window_out_focus_hander)
        self.application.window.set_position(gtk.WIN_POS_CENTER)
        self.application.window.connect('focus-in-event',
                                        self.window_in_focus_hander)
        self.application.window.connect('window-state-event',
                                        self.window_state_change_handler)
        self.application.window.connect('key-press-event',
                                        self.window_key_press_handler)

        self.page_box = gtk.HBox()
        self.content_page = ContentPage(self.appid)
        self.content_page.screenshot_box.connect('button-press-event',
                                                 self.external_continue_action)
        self.content_page.play_button.connect('button-press-event',
                                              self.external_continue_action)

        self.guide_box = GuideBox()
        self.guide_box.set_size_request(220, -1)

        self.page_align = gtk.Alignment()
        self.page_align.set(0.5, 0.5, 1, 1)
        self.page_align.set_padding(0, 0, 2, 2)

        self.control_toolbar = self.create_toolbar(["star"])
        self.page_box.pack_start(self.content_page)
        self.page_box.pack_start(self.guide_box, False)

        self.inner_top_titlebar = self.create_top_titlebar()
        self.inner_top_titlebar.change_name(player_title)
        inner_widgets = ControlToolbar.widgets_id[:]
        inner_widgets.remove("star")
        self.inner_control_toolbar = self.create_toolbar(inner_widgets)

        self.paned_box = PanedBox()
        self.paned_box.add_content_widget(self.page_box)
        self.paned_box.add_bottom_widget(self.inner_control_toolbar)
        self.paned_box.add_top_widget(self.inner_top_titlebar)

        self.page_align.add(self.paned_box)
        self.application.main_box.pack_start(self.page_align)
        self.application.window.add_move_event(self.control_toolbar)
        self.show_bottom = False
        self.display_normal()
Example #5
0
class Player(dbus.service.Object):
    def __init__(self, session_bus, argv, dbus_name, dbus_path):
        dbus.service.Object.__init__(self, session_bus, dbus_path)

        (self.appid, self.game_name, self.width, self.height, self.swf_url, 
                self.resizable) = argv
        self.game_name = urllib.unquote(self.game_name)
        self.width = int(self.width)
        self.height = int(self.height)
        self.plug_status = False
        self.conf_db = get_config_file("conf.db")
        self.p = None
        self.current_sink_index = None
        #self.sound_manager = SoundSetting(self.sound_sink_callback)
        self.loading = True
        self.hand_pause = False
        self.game_pause = False
        self.fullscreen_state = False
        self.guide_box_expand = True
        self.window_normal_info = {}
        self.maximize_state = False
        self.share_show = False

        self.call_flash_game(self.appid)
        self.init_ui()
        self.plug_id = None

        def unique(self):
            self.application.window.present()

        def message_receiver(self, *message):
            message_type, contents = message
            if message_type == 'send_plug_id':
                self.plug_id = int(str(contents[1]))
                self.content_page.add_plug_id(self.plug_id)
                self.plug_status = True
                self.start_loading()
            elif message_type == 'loading_uri_finish':
                fetch_service.add_missions([self.download_task])
            elif message_type == 'enter_bottom':
                if self.show_bottom:
                    self.paned_box.bottom_window.show()
            elif message_type == 'enter_top':
                if self.show_top:
                    self.paned_box.top_window.show()
            elif message_type == 'onload_loading':
                self.start_download_swf()
            elif message_type == 'game_action':
                if contents == 'pause':
                    self.command_pause_game()

        setattr(Player, 
                'unique', 
                dbus.service.method(dbus_name)(unique))
        setattr(Player, 
                'message_receiver', 
                dbus.service.method(dbus_name)(message_receiver))
        setattr(Player,
                'update_signal',
                dbus.service.signal(dbus_name)(self.update_signal))

        self.send_message('get_plug_id', '')

    def sound_sink_callback(self, dt, index):
        sound_id = int(dt['proplist']['application.process.id'])
        if self.p and sound_id == self.p.pid:
            self.current_sink_index = index

    def update_signal(self, obj, data=None):
        pass

    def init_ui(self):
        
        self.application = PlayerApplication(destroy_func=self.quit, max_callback=self.max_callback)
        if self.width+12 < 642:
            width = 642 + 220
        else:
            width = self.width + 12 + 220
        self.application.set_default_size(width, self.height+73)
        self.application.set_skin_preview(get_common_image("frame.png"))
        self.application.set_icon(get_common_image("logo48.png"))
        self.application.add_titlebar(
                ["mode", "min", "max", "close"],
                )
        player_title = _("Deepin Games - %s ") % self.game_name
        self.window = self.application.window
        self.window.set_title(player_title)
        self.application.titlebar.change_name(player_title)
        self.application.titlebar.mode_button.set_active(True)
        self.application.titlebar.mode_button.connect('toggled', self.change_view)
        self.application.titlebar.close_button.connect('clicked', self.quit)
        #self.application.window.connect('focus-out-event', self.window_out_focus_hander)
        self.application.window.set_position(gtk.WIN_POS_CENTER)
        self.application.window.connect('focus-in-event', self.window_in_focus_hander)
        self.application.window.connect('window-state-event', self.window_state_change_handler)
        self.application.window.connect('key-press-event', self.window_key_press_handler)

        self.page_box = gtk.HBox()
        self.content_page = ContentPage(self.appid)
        self.content_page.screenshot_box.connect('button-press-event', self.external_continue_action)
        self.content_page.play_button.connect('button-press-event', self.external_continue_action)

        self.guide_box = GuideBox()
        self.guide_box.set_size_request(220, -1)
        
        self.page_align = gtk.Alignment()
        self.page_align.set(0.5, 0.5, 1, 1)
        self.page_align.set_padding(0, 0, 2, 2)
        
        self.control_toolbar = self.create_toolbar(["star"])
        self.page_box.pack_start(self.content_page)
        self.page_box.pack_start(self.guide_box, False)

        self.inner_top_titlebar = self.create_top_titlebar()
        self.inner_top_titlebar.change_name(player_title)
        inner_widgets = ControlToolbar.widgets_id[:]
        inner_widgets.remove("star")
        self.inner_control_toolbar = self.create_toolbar(inner_widgets)

        self.paned_box = PanedBox()
        self.paned_box.add_content_widget(self.page_box)
        self.paned_box.add_bottom_widget(self.inner_control_toolbar)
        self.paned_box.add_top_widget(self.inner_top_titlebar)

        self.page_align.add(self.paned_box)
        self.application.main_box.pack_start(self.page_align)
        self.application.window.add_move_event(self.control_toolbar)
        self.show_bottom = False
        self.display_normal()

    def window_key_press_handler(self, widget, event):
        if self.fullscreen_state:
            from dtk.ui.keymap import get_keyevent_name
            if get_keyevent_name(event, True) == 'Escape':
                self.fullscreen_handler(self.control_toolbar.fullscreen_button, None)

    def create_top_titlebar(self,
                     button_mask=['min', "close"],
                     icon_path=None, 
                     app_name=None, 
                     title=None, 
                     add_separator=False, 
                     show_title=True, 
                     enable_gaussian=True, 
                     name_size=DEFAULT_FONT_SIZE,
                     title_size=DEFAULT_FONT_SIZE,
                     ):
        titlebar = Titlebar(
                    button_mask, 
                    icon_path, 
                    app_name, 
                    title, 
                    add_separator, 
                    show_title=show_title, 
                    enable_gaussian=enable_gaussian,
                    name_size=name_size,
                    title_size=title_size,
                    )
        if "min" in button_mask:
            titlebar.min_button.connect("clicked", lambda w: self.window.min_window())
        if "close" in button_mask:
            titlebar.close_button.connect("clicked", self.quit)

        return titlebar

    def create_toolbar(self, widget_display=[]):
        control_toolbar = ControlToolbar(self.appid, widget_display)
        control_toolbar.mute_button.connect('clicked', self.mute_handler)
        control_toolbar.pause_button.connect('button-press-event', self.pause_handler)
        control_toolbar.replay_button.connect('clicked', self.replay_action)
        control_toolbar.fullscreen_button.connect('button-release-event', self.fullscreen_handler)
        control_toolbar.share_button.connect('clicked', self.share_action)
        control_toolbar.favorite_button.connect('button-release-event', self.favorite_action)
        control_toolbar.leave_callback = self.leave_callback
        
        if os.path.exists(self.conf_db):
            data = utils.load_db(self.conf_db)
            if data.get('favorite') and self.appid in data['favorite']:
                control_toolbar.favorite_button.set_active(True)

        return control_toolbar

    def favorite_action(self, widget, event):
        if not widget.get_active():
            record_info.record_favorite(self.appid, self.conf_db)
        else:
            record_info.remove_favorite(self.appid, self.conf_db)

    def leave_callback(self, widget, e):
        if self.fullscreen_state:
            if e.window == self.paned_box.bottom_window:
                self.paned_box.bottom_window.hide()
            elif e.window == self.paned_box.top_window:
                self.paned_box.top_window.hide()

    def window_state_change_handler(self, widget, event):
        if event.new_window_state == gtk.gdk.WINDOW_STATE_ICONIFIED:
            self.external_pause_action()

    def external_pause_action(self):
        if not self.loading and (not self.hand_pause and not self.game_pause):
            self.control_toolbar.pause_button.set_active(True)
            self.inner_control_toolbar.pause_button.set_active(False)
            self.toggle_pause_action(self.control_toolbar.pause_button)

    def external_continue_action(self, widget=None, event=None):
        if not self.loading and self.game_pause:
            self.control_toolbar.pause_button.set_active(False)
            self.inner_control_toolbar.pause_button.set_active(False)
            self.toggle_pause_action(self.control_toolbar.pause_button)

    def window_in_focus_hander(self, widget, event):
        #print "In: hand=>%s, pause=>%s" % (self.hand_pause, self.game_pause)
        if not self.loading and (not self.hand_pause and self.game_pause):
            self.control_toolbar.pause_button.set_active(False)
            self.toggle_pause_action(self.control_toolbar.pause_button)

    def window_out_focus_hander(self, widget, event):
        #print "Out: hand=>%s, pause=>%s" % (self.hand_pause, self.game_pause)
        min_state = self.window.get_state() == gtk.gdk.WINDOW_STATE_ICONIFIED
        if not self.loading and (not self.hand_pause and not self.game_pause) and min_state:
            self.control_toolbar.pause_button.set_active(True)
            self.toggle_pause_action(self.control_toolbar.pause_button)

    def change_view(self, widget):
        screen = gtk.gdk.screen_get_default()
        screen_w, screen_h = screen.get_width(), screen.get_height()
        width, height = self.window.get_size()

        DEFAULT_HEIGHT = height

        if not widget.get_active():
            self.guide_box_expand = False
            DEFAULT_WIDTH = width - 220
            self.guide_box.hide_all()
            self.guide_box.set_no_show_all(True)


        else:
            self.guide_box_expand = True
            DEFAULT_WIDTH = width + 220
            self.guide_box.set_no_show_all(False)
            self.guide_box.show_all()

        if self.maximize_state:
            DEFAULT_WIDTH = screen_w

        self.window.set_default_size(DEFAULT_WIDTH, DEFAULT_HEIGHT)
        self.window.set_geometry_hints(None, DEFAULT_WIDTH, DEFAULT_HEIGHT, 
                                        screen_w, screen_h,  -1, -1, -1, -1, -1, -1)
        self.window.resize(DEFAULT_WIDTH, DEFAULT_HEIGHT)

    def max_callback(self, widget):
        screen = gtk.gdk.screen_get_default()
        screen_w, screen_h = screen.get_width(), screen.get_height()

        window_state = self.window.window.get_state()
        if window_state & gtk.gdk.WINDOW_STATE_MAXIMIZED == gtk.gdk.WINDOW_STATE_MAXIMIZED:
            self.maximize_state = False
            self.window.unmaximize()

            if self.window_normal_info:
                if self.window_normal_info['guide_box_expand'] == self.guide_box_expand:
                    width, height = self.window_normal_info['width'], self.window_normal_info['height']
                else:
                    if self.window_normal_info['guide_box_expand']:
                        p_width, p_height = self.window_normal_info['width'], self.window_normal_info['height']
                        width = p_width - 220
                        height = p_height
                    else:
                        p_width, p_height = self.window_normal_info['width'], self.window_normal_info['height']
                        width = p_width + 220
                        height = p_height

                self.window.set_geometry_hints(None, width, height, 
                                            screen_w, screen_h,  -1, -1, -1, -1, -1, -1)
                self.window.resize(width, height)

        else:
            self.maximize_state = True
            self.window_normal_info['guide_box_expand'] = copy.deepcopy(self.guide_box_expand)
            self.window_normal_info['width'] = copy.deepcopy(self.window.get_size()[0])
            self.window_normal_info['height'] = copy.deepcopy(self.window.get_size()[1])
            self.window.maximize()

    def quit(self, widget=None, data=None):
        if self.game_pause:
            os.system('kill -9 %s' % self.p.pid)
        gtk.main_quit()

    def display_normal(self):
        self.application.show_titlebar()
        self.guide_box.show_all()
        self.application.main_box.pack_start(self.control_toolbar, False, False)
        self.application.window.show_window()
        if getattr(self.paned_box, "bottom_window"):
            self.paned_box.bottom_window.hide()
        if getattr(self.paned_box, 'top_window'):
            self.paned_box.top_window.hide()

    def fullscreen_handler(self, widget, data=None):
        self.control_toolbar.fullscreen_button.set_state(gtk.STATE_NORMAL)
        self.inner_control_toolbar.fullscreen_button.set_state(gtk.STATE_NORMAL)
        if self.fullscreen_state:
            self.fullscreen_state = False
            self.display_normal()
            self.show_bottom = False
            self.show_top = False
            self.page_align.set_padding(0, 0, 2, 2)
            self.application.window.unfullscreen()
            self.control_toolbar.fullscreen_button.set_active(False)
        else:
            self.fullscreen_state = True
            # for fullscreen mode
            self.paned_box.connect('leave-notify-event', self.leave_callback)
            self.paned_box.paint_bottom_window = self.__paint_bottom_toolbar_background
            self.paned_box.paint_top_window = self.__paint_top_titlebar_background
            self.show_bottom = True
            self.show_top = True

            self.application.hide_titlebar()
            self.application.main_box.remove(self.control_toolbar)
            self.control_toolbar.show_all()
            self.application.window.show_window()
            self.guide_box.hide_all()
            self.page_align.set_padding(0, 0, 0, 0)
            self.application.window.fullscreen()
            self.inner_control_toolbar.fullscreen_button.set_active(True)

    def mute_handler(self, widget, data=None):
        #if self.current_sink_index:
            #pypulse.PULSE.set_sink_input_mute(self.current_sink_index, widget.get_active())
        pass

    def replay_action(self, widget, data=None):
        if self.game_pause:
            self.control_toolbar.pause_button.set_active(False)
            self.toggle_pause_action(self.control_toolbar.pause_button)
        if not self.loading:
            self.update_signal(['load_uri', 'file://%s,%s,%s' % (self.swf_save_path, self.width, self.height)])

    def toggle_pause_action(self, widget):
        if widget.get_active():
            self.update_signal(['game_action', 'pause'])
        else:
            self.command_continue_game()
        self.game_pause = widget.get_active() 

    def content_socket_press_handler(self, widget, event):
        print event

    def pause_handler(self, widget, data=None):
        if not self.game_pause:
            self.update_signal(['game_action', 'pause'])
            self.hand_pause = True
            self.game_pause = True
        else:
            self.command_continue_game()
            self.hand_pause = False
            self.game_pause = False

    def command_pause_game(self):
        screenshot_pixbuf = self.get_game_screenshot_pixbuf()
        self.content_page.remove_socket(screenshot_pixbuf)
        os.system('kill -STOP %s' % self.p.pid)

    def command_continue_game(self):
        os.system('kill -CONT %s' % self.p.pid)
        self.content_page.add_plug_id(self.plug_id)
        #self.update_signal(['game_action', 'continue'])

    def fullscreen_action(self, widget, data=None):
        pass

    def get_game_screenshot_pixbuf(self):
        if self.fullscreen_state:
            self.paned_box.bottom_window.hide()

        rect = self.content_page.get_allocation()
        #rect = self.window.get_allocation()
        width = rect.width
        height = rect.height
        pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, width, height)
        pixbuf = pixbuf.get_from_drawable(self.content_page.socket_box.window, self.content_page.window.get_colormap(), 
                                            rect.x, rect.y, 0, 0, width, height)
        return pixbuf

    def share_action(self, widget, data=None):
        if not self.share_show:
            #rect = self.content_page.get_allocation()
            rect = self.window.get_allocation()
            width = rect.width
            height = rect.height
            pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, width, height)
            pixbuf = pixbuf.get_from_drawable(self.window.window, self.window.get_colormap(), 
                                                rect.x, rect.y, 0, 0, width, height)

            filename = self.save_to_tmp_file(pixbuf)
            share_path = os.path.join(get_parent_dir(__file__), 'share.py')
            
            order = ['python2', share_path, filename]
            try:
                self.share_p = subprocess.Popen(order, stderr=subprocess.STDOUT, shell=False)
            except OSError:    
                order[0] = 'python'
                self.share_p = subprocess.Popen(order, stderr=subprocess.STDOUT, shell=False)
            self.share_show = True
            self.external_pause_action()
            self.hand_pause = True
            gtk.timeout_add(200, self.check_share_dialog_close)

    def check_share_dialog_close(self):
        if getattr(self, 'share_p'):
            if self.share_p.poll() == 0:
                self.share_show = False
                self.hand_pause = False
                return False
        return True

    def save_to_tmp_file(self, pixbuf):
        from tempfile import mkstemp
        import os
        tmp = mkstemp(".tmp", PROGRAM_NAME)
        os.close(tmp[0])
        filename = tmp[1]
        pixbuf.save(filename, "jpeg", {"quality":"100"})
        return filename

    def start_loading(self):
        global_event.register_event('download-app-info-finish', 
                self.app_info_download_finish)
        FetchInfo(self.appid).start()
        self.swf_save_path = os.path.expanduser(
                "~/.cache/deepin-game-center/downloads/%s/%s" % (
                    self.appid, 
                    os.path.split(self.swf_url)[1])
                )
        if os.path.exists(self.swf_save_path):
            gtk.timeout_add(200, lambda:self.load_game())
        else:
            gtk.timeout_add(200, self.show_loading_page)
            
    def show_loading_page(self):
        touch_file_dir(self.swf_save_path)
        print "Touch file dir:", self.swf_save_path
        self.load_html_path = os.path.join(static_dir, 'loading.html')
        self.send_message('load_loading_uri', 'file://' + self.load_html_path)
        print "Send show loading message:", self.load_html_path

    def start_download_swf(self):
        self.remote_path = urllib.basejoin(GAME_CENTER_DATA_ADDRESS, self.swf_url)
        utils.ThreadMethod(urllib.urlretrieve, (self.remote_path, self.swf_save_path + '_tmp', self.report_hook)).start()
        print "Downloading: %s" % self.remote_path

        #self.download_task = TaskObject(self.remote_path, self.swf_save_path, verbose=True)
        #self.download_task.connect("update", self.download_update)
        #self.download_task.connect("finish", self.download_finish)
        #self.download_task.connect("error",  self.download_failed)
        #self.download_task.connect("start",  self.download_start)

    def report_hook(self, block_count, block_size, total_size):
        current = block_count * block_size
        if current < total_size:
            percent = str(int(float(current)/total_size * 100))
            self.update_signal(['download_update', percent])
        else:
            self.update_signal(['download_update', '100'])
            os.system('mv %s %s' % (self.swf_save_path + '_tmp', self.swf_save_path))
            self.load_game()
            #gtk.timeout_add(500, lambda:self.load_game())

    def load_game(self):
        self.loading = False
        self.control_toolbar.show_toolbar_button(['pause', 'replay', 'favorite', 'fullscreen', 'share'])
        self.update_signal(['download_finish', 'file://%s,%s,%s' % (self.swf_save_path, self.width, self.height)])
        record_info.record_recent_play(self.appid, self.conf_db)

        #self.loading = False
        #self.send_message('load_uri', 'file://%s,%s,%s' % (self.swf_save_path, self.width, self.height))
        #record_info.record_recent_play(self.appid, self.conf_db)
            
    def run(self):
        self.application.window.show_window()
        #self.paned_box.bottom_window.set_composited(True)
        gtk.main()

    def app_info_download_finish(self, js):
        gtk.timeout_add(500, self.import_infos, js)

    def import_infos(self, infos):
        if not self.loading:
            self.send_message('app_info_download_finish', infos)
            return False
        return True

    def call_flash_game(self, local_path):
        flash_frame_path = os.path.join(get_parent_dir(__file__), 'flash_frame.py')
        order = ['python2', flash_frame_path, self.appid]
        try:
            self.p = subprocess.Popen(order, stderr=subprocess.STDOUT, shell=False)
        except OSError:    
            order[0] = 'python'
            self.p = subprocess.Popen(order, stderr=subprocess.STDOUT, shell=False)

    def send_message(self, message_type, contents):
        bus = dbus.SessionBus()
        flash_dbus_name = "com.deepin.game_flash_%s" % (self.appid)
        flash_dbus_path = "/com/deepin/game_flash_%s" % (self.appid)
        if is_dbus_name_exists(flash_dbus_name):
            bus_object = bus.get_object(flash_dbus_name, flash_dbus_path)
            method = bus_object.get_dbus_method("message_receiver")
            method(
                message_type, 
                contents,
                reply_handler=handle_dbus_reply,
                error_handler=handle_dbus_error
            )

    def download_update(self, task, data):
        #print "%s/s" % utils.get_human_size(data.speed)
        progress = "%d" % data.progress
        self.update_signal(['download_update', progress])

    def download_start(self, task, data):
        pass

    def download_finish(self, task, data):
        gtk.timeout_add(500, lambda:self.load_game())

    def download_failed(self, task, data):
        pass

    def __paint_bottom_toolbar_background(self, e):
        # 将皮肤的图片画在bottom toolbar上,作为背景.
        cr = e.window.cairo_create()
        bottom_size = e.window.get_size()
        # draw background.
        cr.set_source_rgba(*alpha_color_hex_to_cairo(("#ebebeb", 0.1)))
        cr.rectangle(0, 0, bottom_size[0], bottom_size[1])
        cr.fill()
        # draw background pixbuf.
        pixbuf = skin_config.background_pixbuf
        app_h = self.application.window.allocation.height
        app_w = self.application.window.allocation.width
        bottom_h = bottom_size[1]
        # 当图片的高度小雨窗口高度的时候,只拿出图片的最尾巴.
        if pixbuf.get_height() > app_h + bottom_h:
            h = app_h
        else:
            h = pixbuf.get_height() - bottom_h
        # 当图片小于窗口宽度的时候,拉伸图片.
        if pixbuf.get_width() < app_w:
            pixbuf = pixbuf.scale_simple(app_w,
                                pixbuf.get_width(),
                                gtk.gdk.INTERP_BILINEAR)

        draw_pixbuf(cr, 
                    pixbuf, 
                    0, 
                    -(h))

    def __paint_top_titlebar_background(self, e):
        # 将皮肤的图片画在bottom toolbar上,作为背景.
        cr = e.window.cairo_create()
        bottom_size = e.window.get_size()
        # draw background.
        cr.set_source_rgba(*alpha_color_hex_to_cairo(("#ebebeb", 0.1)))
        cr.rectangle(0, 0, bottom_size[0], bottom_size[1])
        cr.fill()
        # draw background pixbuf.
        pixbuf = skin_config.background_pixbuf
        app_w = self.application.window.allocation.width
        # 当图片小于窗口宽度的时候,拉伸图片.
        if pixbuf.get_width() < app_w:
            pixbuf = pixbuf.scale_simple(app_w,
                                pixbuf.get_width(),
                                gtk.gdk.INTERP_BILINEAR)

        draw_pixbuf(cr, 
                    pixbuf, 
                    0, 
                    0)
    def init_ui(self):
        self.application = Application()
        self.application.set_default_size(1000, 660)
        self.application.set_skin_preview(get_common_image("frame.png"))
        self.application.set_icon(get_common_image("logo48.png"))
        self.application.add_titlebar(
                ["theme", "menu", "max","min", "close"],
                show_title=False
                )
        self.application.window.set_title(_("Deepin Games"))

        # Init page box.
        self.page_box = gtk.VBox()
        self.page_box.connect('expose-event', self.page_box_render)
        
        # Init page align.
        self.page_align = gtk.Alignment()
        self.page_align.set(0.5, 0.5, 1, 1)
        self.page_align.set_padding(0, 0, 2, 2)
        
        # Append page to switcher.
        self.paned_box = PanedBox(24)
        self.paned_box.add_content_widget(self.page_box)
        self.bottom_tip_bar = BottomTipBar()
        self.bottom_tip_bar.close_button.connect('clicked', lambda w: self.paned_box.bottom_window.hide())
        self.paned_box.add_bottom_widget(self.bottom_tip_bar)

        self.page_align.add(self.paned_box)
        self.application.main_box.pack_start(self.page_align, True, True)
        
        # Init status bar.
        self.statusbar = Statusbar(30)
        status_box = gtk.HBox()

        self.statusbar.status_box.pack_start(status_box, True, True)
        self.application.main_box.pack_start(self.statusbar, False, False)

        self.webview = WebView(COOKIE_FILE)
        webkit.set_web_database_directory_path(CACHE_DIR)
        web_settings = self.webview.get_settings()
        web_settings.set_property("enable-page-cache", True)
        web_settings.set_property("enable-offline-web-application-cache", True)
        #web_settings.set_property("enable-file-access-from-file-uris", True)
        web_settings.set_property("enable-xss-auditor", False)
        web_settings.set_property('enable-universal-access-from-file-uris', True)
        web_settings.set_property("enable-default-context-menu", False)
        self.webview.set_settings(web_settings)
        #self.webview.enable_inspector()
        self.webview.connect('new-window-policy-decision-requested', self.navigation_policy_decision_requested_cb)
        #self.webview.connect('notify::load-status', self.webview_load_status_handler)
        self.webview.connect('notify::title', self.webview_title_changed_handler)
        self.webview.connect('script-alert', self.webview_script_alert_handler)
        self.webview.connect('window-object-cleared', self.webview_window_object_cleared)
        #self.webview.connect('load-progress-changed', self.load_progress)
        
        self.home_url = urllib.basejoin(GAME_CENTER_SERVER_ADDRESS, 'game/?hl=%s' % LANGUAGE)
        self.network_failed_box = NetworkConnectFailed(self.check_network_connection)
        self.check_network_connection()
        #self.page_box.add(self.network_failed_box)

        self.navigatebar = Navigatebar(
                [
                (None, _("Home"), self.show_home_page),
                (None, _("Topics"), self.show_subject_page),
                (None, _("My Games"), self.show_mygame_page),
                ],
                font_size = 11,
                padding_x = 5,
                padding_y = 16,
                vertical=False,
                item_normal_pixbuf=DynamicPixbuf(get_common_image('top/nav_normal.png')),
                item_hover_pixbuf=DynamicPixbuf(get_common_image('top/nav_hover.png')),
                item_press_pixbuf=DynamicPixbuf(get_common_image('top/nav_press.png')),
                )
        self.navigatebar.set_size_request(-1, 56)
        self.navigatebar_align = gtk.Alignment(0, 0, 1, 1)
        self.navigatebar_align.set_padding(0, 0, 4, 0)
        self.navigatebar_align.add(self.navigatebar)
        self.application.titlebar.set_size_request(-1, 56)
        self.application.titlebar.left_box.pack_start(self.navigatebar_align, True, True)
        self.application.window.add_move_event(self.navigatebar)

        self.about_dialog = AboutDialog(_('About us'))
        self.about_dialog.set_transient_for(self.application.window)

        # Init menu.
        #if LANGUAGE == 'en_US':
            #menu_min_width = 185
        #else:
            #menu_min_width = 150

        menu = Menu(
            [
             (None, _("Clear all cached data"), self.clean_download_cache),
             (None, _("See what's new"), lambda : self.show_wizard_win()),
             (None, _("About us"), self.show_about_dialog),
             (None, _("Quit"), lambda: gtk.main_quit()),
             ],
            is_root_menu=True,
            #menu_min_width=menu_min_width,
            )
        self.application.set_menu_callback(
            lambda button:
                menu.show(
                get_widget_root_coordinate(button, WIDGET_POS_BOTTOM_LEFT),
                (button.get_allocation().width, 0)))
        
        self.no_favorite_html_path = os.path.join(static_dir, "error-no-favorite.html")
        self.no_recent_html_path = os.path.join(static_dir, "error-no-recent.html")
        self.mygame_frame_path = os.path.join(static_dir, "mygame-frame.html")
        self.gallery_html_path = os.path.join(static_dir, 'game-mygame.html')

        skin_config.connect('theme-changed', self.theme_changed_handler)
        global_event.register_event('show-message', self.update_message)
class GameCenterApp(dbus.service.Object):

    def __init__(self, session_bus):
        dbus.service.Object.__init__(self, session_bus, GAME_CENTER_DBUS_PATH)
        self.conf_db = get_config_file("conf.db")
        self.in_wizard_showing = False

        self.init_ui()

    def init_ui(self):
        self.application = Application()
        self.application.set_default_size(1000, 660)
        self.application.set_skin_preview(get_common_image("frame.png"))
        self.application.set_icon(get_common_image("logo48.png"))
        self.application.add_titlebar(
                ["theme", "menu", "max","min", "close"],
                show_title=False
                )
        self.application.window.set_title(_("Deepin Games"))

        # Init page box.
        self.page_box = gtk.VBox()
        self.page_box.connect('expose-event', self.page_box_render)
        
        # Init page align.
        self.page_align = gtk.Alignment()
        self.page_align.set(0.5, 0.5, 1, 1)
        self.page_align.set_padding(0, 0, 2, 2)
        
        # Append page to switcher.
        self.paned_box = PanedBox(24)
        self.paned_box.add_content_widget(self.page_box)
        self.bottom_tip_bar = BottomTipBar()
        self.bottom_tip_bar.close_button.connect('clicked', lambda w: self.paned_box.bottom_window.hide())
        self.paned_box.add_bottom_widget(self.bottom_tip_bar)

        self.page_align.add(self.paned_box)
        self.application.main_box.pack_start(self.page_align, True, True)
        
        # Init status bar.
        self.statusbar = Statusbar(30)
        status_box = gtk.HBox()

        self.statusbar.status_box.pack_start(status_box, True, True)
        self.application.main_box.pack_start(self.statusbar, False, False)

        self.webview = WebView(COOKIE_FILE)
        webkit.set_web_database_directory_path(CACHE_DIR)
        web_settings = self.webview.get_settings()
        web_settings.set_property("enable-page-cache", True)
        web_settings.set_property("enable-offline-web-application-cache", True)
        #web_settings.set_property("enable-file-access-from-file-uris", True)
        web_settings.set_property("enable-xss-auditor", False)
        web_settings.set_property('enable-universal-access-from-file-uris', True)
        web_settings.set_property("enable-default-context-menu", False)
        self.webview.set_settings(web_settings)
        #self.webview.enable_inspector()
        self.webview.connect('new-window-policy-decision-requested', self.navigation_policy_decision_requested_cb)
        #self.webview.connect('notify::load-status', self.webview_load_status_handler)
        self.webview.connect('notify::title', self.webview_title_changed_handler)
        self.webview.connect('script-alert', self.webview_script_alert_handler)
        self.webview.connect('window-object-cleared', self.webview_window_object_cleared)
        #self.webview.connect('load-progress-changed', self.load_progress)
        
        self.home_url = urllib.basejoin(GAME_CENTER_SERVER_ADDRESS, 'game/?hl=%s' % LANGUAGE)
        self.network_failed_box = NetworkConnectFailed(self.check_network_connection)
        self.check_network_connection()
        #self.page_box.add(self.network_failed_box)

        self.navigatebar = Navigatebar(
                [
                (None, _("Home"), self.show_home_page),
                (None, _("Topics"), self.show_subject_page),
                (None, _("My Games"), self.show_mygame_page),
                ],
                font_size = 11,
                padding_x = 5,
                padding_y = 16,
                vertical=False,
                item_normal_pixbuf=DynamicPixbuf(get_common_image('top/nav_normal.png')),
                item_hover_pixbuf=DynamicPixbuf(get_common_image('top/nav_hover.png')),
                item_press_pixbuf=DynamicPixbuf(get_common_image('top/nav_press.png')),
                )
        self.navigatebar.set_size_request(-1, 56)
        self.navigatebar_align = gtk.Alignment(0, 0, 1, 1)
        self.navigatebar_align.set_padding(0, 0, 4, 0)
        self.navigatebar_align.add(self.navigatebar)
        self.application.titlebar.set_size_request(-1, 56)
        self.application.titlebar.left_box.pack_start(self.navigatebar_align, True, True)
        self.application.window.add_move_event(self.navigatebar)

        self.about_dialog = AboutDialog(_('About us'))
        self.about_dialog.set_transient_for(self.application.window)

        # Init menu.
        #if LANGUAGE == 'en_US':
            #menu_min_width = 185
        #else:
            #menu_min_width = 150

        menu = Menu(
            [
             (None, _("Clear all cached data"), self.clean_download_cache),
             (None, _("See what's new"), lambda : self.show_wizard_win()),
             (None, _("About us"), self.show_about_dialog),
             (None, _("Quit"), lambda: gtk.main_quit()),
             ],
            is_root_menu=True,
            #menu_min_width=menu_min_width,
            )
        self.application.set_menu_callback(
            lambda button:
                menu.show(
                get_widget_root_coordinate(button, WIDGET_POS_BOTTOM_LEFT),
                (button.get_allocation().width, 0)))
        
        self.no_favorite_html_path = os.path.join(static_dir, "error-no-favorite.html")
        self.no_recent_html_path = os.path.join(static_dir, "error-no-recent.html")
        self.mygame_frame_path = os.path.join(static_dir, "mygame-frame.html")
        self.gallery_html_path = os.path.join(static_dir, 'game-mygame.html')

        skin_config.connect('theme-changed', self.theme_changed_handler)
        global_event.register_event('show-message', self.update_message)

    def page_box_render(self, widget, event):
        cr = widget.window.cairo_create()
        rect = widget.get_allocation()
        cr.set_source_rgb(1, 1, 1)
        cr.rectangle(*rect)
        cr.fill()

    def check_network_connection(self):
        if is_network_connected():
            self.network_connected_flag = True
            self.webview.load_uri(self.home_url)
            self.switch_page_view(self.webview)
        else:    
            self.network_connected_flag = False
            self.switch_page_view(self.network_failed_box)
    
    def switch_page_view(self, box):
        container_remove_all(self.page_box)
        self.page_box.add(box)

    def load_progress(self, webview, progress):
        print progress

    def webview_window_object_cleared(self, webview, frame, context, window_object):
        ctx = jswebkit.JSContext(frame.get_global_context())
        window = ctx.EvaluateScript("window")
        window.css_color = skin_config.theme_name
        location = window.location.href
        parse_result = urlparse.urlparse(location)

        frame.get_web_view().execute_script('var global_l18n_str = %s' % json.dumps({
                'my_favorites': _('My favorites'),
                'my_recent': _('My recents'),
                },
                encoding='UTF-8',
                ensure_ascii=False,
                ))
        frame.get_web_view().execute_script('var global_language = %s' %
                json.dumps(LANGUAGE, encoding='UTF-8', ensure_ascii=False))

        if parse_result.path == self.no_favorite_html_path or parse_result.path == self.no_recent_html_path:
            window.css_language = LANGUAGE

    def update_message(self, message, hide_timeout=0):
        if not self.paned_box.bottom_window.is_visible():
            self.paned_box.bottom_window.show()
        if isinstance(message, list) and len(message) == 3:
            self.bottom_tip_bar.update_info(*message)
        else:
            self.bottom_tip_bar.update_info(message)
        if hide_timeout != 0:
            gtk.timeout_add(hide_timeout, lambda:self.paned_box.bottom_window.hide())
    
    def ready_show(self):    
        if not utils.is_wizard_showed():
            self.in_wizard_showing = True
            self.show_wizard_win(True, callback=self.wizard_callback)
            utils.set_wizard_showed()
        else:    
            self.application.window.show_all()
        gtk.main()

    def show_wizard_win(self, show_button=False, callback=None):    

        self.wizard_win = Wizard(
            [get_common_locale_image('wizard', '%s.png' % i) for i in range(3)],
            (
                get_common_image('wizard/dot_normal.png'), 
                get_common_image('wizard/dot_active.png'),
            ),
            (
                get_common_locale_image('wizard', 'start_normal.png'), 
                get_common_locale_image('wizard', 'start_press.png'), 
            ),
            show_button,
            callback
            )
        self.wizard_win.show_all()
        
    def wizard_callback(self):
        self.in_wizard_showing = False
        self.application.window.show_all()
        gtk.timeout_add(200, self.application.raise_to_top)

    def clean_download_cache(self):
        info = {
                'file_num':0,
                'total_size':0
                }
        downloads_dir = os.path.join(CACHE_DIR, 'downloads')
        appids =  os.listdir(downloads_dir)
        for appid in appids:
            files = os.listdir(os.path.join(downloads_dir, appid))
            for f in files:
                if f.endswith('.swf'):
                    swf_path = os.path.join(downloads_dir, appid, f)
                    info['file_num'] += 1
                    info['total_size'] += os.path.getsize(swf_path)
                    os.remove(os.path.join(downloads_dir, appid, f))
        
        if info['file_num']:
            cache_cleaned_message = _('%s files are deleted and %s disk space is freed.') % (
                    info['file_num'], utils.get_human_size(info['total_size']))
        else:
            cache_cleaned_message = _('Your game cache is empty.')
        global_event.emit('show-message', cache_cleaned_message, 5000)

    def show_about_dialog(self):
        self.about_dialog.show_all()

    def theme_changed_handler(self, widget, name):
        self.webview.execute_script('change_color_theme(%s)' %
            json.dumps(name, encoding="UTF-8", ensure_ascii=False))
        self.webview.execute_script('$("#game-gallery").get(0).contentWindow.change_color_theme(%s)' %
            json.dumps(name, encoding="UTF-8", ensure_ascii=False))
        self.webview.execute_script('alert("scroll_top://" + $($("#game-gallery").get(0).contentWindow.document.body).scrollTop())')

    def send_event(self, data=0):
        press_event = gtk.gdk.Event(gtk.gdk.BUTTON_PRESS)
        press_event.window = self.webview.window
        press_event.x = 1062.0
        press_event.y = 35.0 + int(data) + 20.0
        press_event.button = 1

        release_event = gtk.gdk.Event(gtk.gdk.BUTTON_RELEASE)
        release_event.window = self.webview.window
        release_event.x = 1062.0
        release_event.y = 35.0 + int(data) + 20.0
        release_event.button = 1

        self.webview.event(press_event)
        self.webview.event(release_event)
        self.webview.window.invalidate_rect(self.webview.allocation, True)

    def webview_message_handler(self, info):
        info = info.split('://')
        if len(info) == 2:
            order, data = info

            if order == 'play':
                self.show_play(data)

            elif order == 'star':
                self.toggle_favorite(data)

            elif order == 'local':
                if data == 'recent':
                    self.show_recent_page()
                elif data == 'star':
                    self.show_favorite_page()

            elif order == 'document_ready' and data == 'game_gallery':
                self.document_ready()
                
            elif order == 'onload' and data == 'game_gallery':
                gtk.timeout_add(200, self.fresh_favotite_status)

            elif order == 'onload' and data == 'local_game_gallery':
                self.webview.execute_script('$("#game-gallery").get(0).contentWindow.set_right_menu()')
                gtk.timeout_add(200, self.fresh_favotite_status)

            elif order == 'onload' and data == 'main_frame':
                gtk.timeout_add(200, self.show_favorite_page)

            elif order == 'onload' and data == 'footer':
                self.webview.execute_script('if(infos){append_data_to_gallery(infos);}')

            elif order == 'favorite':
                record_info.record_favorite(data, self.conf_db)
                FetchInfo(data).start()
                favorite_animation(self.application.window)

            elif order == 'unfavorite':
                record_info.remove_favorite(data, self.conf_db)

            elif order == 'local_action':
                info = data.split('-')
                if len(info) == 2:
                    action_type= info[0]
                    appid = info[1]
                    if action_type == 'favorite':
                        record_info.remove_favorite(appid, self.conf_db)
                        utils.ThreadMethod(utils.send_analytics, ('unfavorite', appid)).start()
                        #var favorite_url = 'http://' + location.host + '/game/analytics/?type=unfavorite&appid=' + id;
                        self.webview.execute_script('if(infos){infos_remove(%s);}else{gallery_change(%s);}' % (
                                json.dumps(appid, encoding="UTF-8", ensure_ascii=False),
                                json.dumps(self.no_favorite_html_path, encoding="UTF-8", ensure_ascii=False),
                                ))
                    elif action_type == 'recent':
                        record_info.remove_recent_play(appid, self.conf_db)
                        self.webview.execute_script('if(infos){infos_remove(%s);}else{gallery_change(%s);}' % (
                                json.dumps(appid, encoding="UTF-8", ensure_ascii=False),
                                json.dumps(self.no_recent_html_path, encoding="UTF-8", ensure_ascii=False),
                                ))

            elif order == 'scroll_top':
                self.send_event(data)

    def navigation_policy_decision_requested_cb(self, web_view, frame, request, navigation_action, policy_decision):
        uri = request.get_uri()
        if uri.startswith('http://') or uri.startswith('https://'):
            return False
        else:
            self.webview_message_handler(uri)
            return True

    def webview_script_alert_handler(self, widget, frame, uri, data=None):
        self.webview_message_handler(uri)
        return True

    def webview_title_changed_handler(self, webview, data=None):
        title = webview.get_title()
        if title:
            self.webview_message_handler(title)

    def fresh_favotite_status(self):
        if os.path.exists(self.conf_db):
            data = utils.load_db(self.conf_db)
            if data.get('favorite'):
                for id in data['favorite']:
                    self.webview.execute_script("change_favorite_status(%s, 'ilike')" %
                        json.dumps(id, encoding="UTF-8", ensure_ascii=False))

    def webview_load_status_handler(self, widget=None, status=None):
        load_status = widget.get_load_status()
        if load_status == webkit.LOAD_FINISHED:
            self.webview.execute_script("$('#game-gallery').contents().find('#grid span span').removeClass('ilike')")
            self.webview.execute_script("$('#game-gallery').contents().find('#grid span span').addClass('like')")

    def document_ready(self):
        self.webview.execute_script("$('#game-gallery').contents().find('#grid span span').removeClass('ilike')")
        self.webview.execute_script("$('#game-gallery').contents().find('#grid span span').addClass('like')")

    def show_play(self, data):
        data = data.split(',')
        player_path = os.path.join(get_parent_dir(__file__), 'deepin-game-center.py')
        order = ['python2', player_path]
        order.append('-p')
        order.append(','.join(data))
        try:
            self.p = subprocess.Popen(order, stderr=subprocess.STDOUT, shell=False)
        except OSError:    
            order[0] = 'python'
            self.p = subprocess.Popen(order, stderr=subprocess.STDOUT, shell=False)


    #def mute_handler(self, widget, data=None):
        #active = widget.get_active()
        #current_sink = pypulse.get_fallback_sink_index()
        #if current_sink is not None:
            #pypulse.PULSE.set_output_mute(current_sink, active)

    def print_info(self, info_type, info):
        if info:
            print info_type, info

    def toggle_favorite(self, data):
        print "toggle favorite"

    def show_home_page(self):
        self.webview.load_uri(self.home_url)

    def show_subject_page(self):
        self.subject_url = urllib.basejoin(GAME_CENTER_SERVER_ADDRESS, 'game/subjects/?hl=%s' % LANGUAGE)
        self.webview.load_uri(self.subject_url)

    def show_mygame_page(self):
        self.webview.open('file://' + self.mygame_frame_path)
        
    def show_favorite_page(self):
        downloads_dir = os.path.join(CACHE_DIR, 'downloads')
        if os.path.exists(self.conf_db):
            data = utils.load_db(self.conf_db)
            if data.get('favorite'):
                infos = []
                favorite_list = data['favorite']
                for id in favorite_list:
                    try:
                        info_js_path = os.path.join(downloads_dir, str(id), 'info.json')
                        info = json.load(open(info_js_path))
                        info['index_pic_url'] = os.path.join(downloads_dir, str(id), info['index_pic_url'].split('/')[-1])
                        #info['swf_game'] = os.path.join(downloads_dir, str(id), info['swf_game'].split('/')[-1])
                        info['swf_game'] = urlparse.urlparse(info['swf_game']).path[1:]
                        info['type'] = 'favorite'
                        infos.append(info)
                    except Exception, e:
                        print "Load favorite page error:", e
                if infos:
                    self.webview.execute_script('var infos=%s' % 
                            json.dumps(infos, encoding="UTF-8", ensure_ascii=False))
                    self.webview.execute_script("gallery_change(%s)" %
                            json.dumps(self.gallery_html_path, encoding="UTF-8", ensure_ascii=False))
                    return

        self.webview.execute_script("gallery_change(%s)" %
                json.dumps(self.no_favorite_html_path, encoding="UTF-8", ensure_ascii=False))
Example #8
0
class FlashFrame(dbus.service.Object):
    '''
    class docs
    '''
	
    def __init__(self, session_bus, appid, dbus_name, dbus_path):
        dbus.service.Object.__init__(self, session_bus, dbus_path)

        self.appid = appid
        self.dbus_name = dbus_name
        self.dbus_path = dbus_path
        
        # WARING: only use once in one process
        DBusGMainLoop(set_as_default=True) 
        
        self.plug = gtk.Plug(0)

        self.webview = WebView(COOKIE_FILE)
        self.webview.connect('title-changed', self.webview_message_handler)
        self.webview.connect('script-alert', self.webview_message_handler)
        #self.webview.enable_inspector()
        self.paned_box = PanedBox(2, True, 2, True)
        self.paned_box.enter_bottom_win_callback = self.enter_bottom_notify
        self.paned_box.enter_top_win_callback = self.enter_top_notify
        self.paned_box.add_content_widget(self.webview)
        self.plug.add(self.paned_box)
        self.plug.connect('delete-event', self.plug_delete_event)
        
        # Handle signals.
        self.plug.connect("realize", self.flash_frame_realize)
        self.plug.connect("destroy", self.flash_frame_exit)

        glib.timeout_add(1000, self.is_exist)
        glib.timeout_add(200, self.connect_signal)

        def message_receiver(self, *message):
            message_type, contents = message
            if message_type == 'exit':
                self.exit()
            elif message_type == 'load_uri':
                self.load_flash(contents)
            elif message_type == 'load_loading_uri':
                self.webview.load_uri(contents)
                self.send_message('loading_uri_finish', '')
            elif message_type == 'load_string':
                self.webview.load_string(contents)
            elif message_type == 'get_plug_id':
                self.send_flash_info()
            elif message_type == 'app_info_download_finish':
                self.webview.execute_script("app_info=%s" %
                        json.dumps(str(contents), encoding="UTF-8", ensure_ascii=False))

        setattr(FlashFrame, 
                'message_receiver', 
                dbus.service.method(dbus_name)(message_receiver))

    def plug_delete_event(self, widget, event):
        return True

    def webview_message_handler(self, webview, frame, message):
        if message == 'finish_load':
            self.webview.execute_script('loading_flash(%s)' %
                json.dumps(self.swf_info, encoding="UTF-8", ensure_ascii=False))
        elif message == 'onload_loading':
            self.send_message('onload_loading', '')
            self.webview.execute_script('change_color_theme(%s)' %
                json.dumps(skin_config.theme_name, encoding="UTF-8", ensure_ascii=False))
        return True

    def load_flash(self, contents):
        flash_html_path = os.path.join(static_dir, 'flash.html')
        self.webview.load_uri("file://" + flash_html_path)
        self.swf_info = str(contents).split(',')

    def enter_bottom_notify(self):
        self.send_message('enter_bottom', '')

    def enter_top_notify(self):
        self.send_message('enter_top', '')

    def is_exist(self):
        if dbus.SessionBus().name_has_owner("com.deepin.game_player_%s" % self.appid):
            return True
        else:
            glib.timeout_add(0, gtk.main_quit)
            return False

    def run(self):    
        self.plug.show_all()
        self.paned_box.bottom_window.set_composited(True)
        self.paned_box.top_window.set_composited(True)
        gtk.main()

    def do_delete_event(self, w):
        #a trick to prevent plug destroyed!.  the  better way is recreate an GtkPlug when need reuse it's content
        return True

    def flash_frame_realize(self, widget):
        # Send module information.
        self.send_flash_info()

    def exit(self):
        gtk.main_quit()

    def flash_frame_exit(self, widget):
        gtk.main_quit()
        
    def send_message(self, message_type, message_content):
        session_bus = dbus.SessionBus()
        GAME_PLAYER_DBUS_NAME = "com.deepin.game_player_%s" % self.appid
        GAME_PLAYER_DBUS_PATH = "/com/deepin/game_player_%s" % self.appid
        if is_dbus_name_exists(GAME_PLAYER_DBUS_NAME):

            bus_object = session_bus.get_object(GAME_PLAYER_DBUS_NAME, GAME_PLAYER_DBUS_PATH)
            method = bus_object.get_dbus_method("message_receiver")
            method(message_type, 
                   message_content,
                   reply_handler=self.handle_dbus_reply, # add reply handler
                   error_handler=self.handle_dbus_error  # add error handler
                   )

    def connect_signal(self):
        session_bus = dbus.SessionBus()
        GAME_PLAYER_DBUS_NAME = "com.deepin.game_player_%s" % self.appid
        GAME_PLAYER_DBUS_PATH = "/com/deepin/game_player_%s" % self.appid
        if is_dbus_name_exists(GAME_PLAYER_DBUS_NAME):
            session_bus.add_signal_receiver(
                    self.signal_receiver, 
                    signal_name="update_signal", 
                    dbus_interface=GAME_PLAYER_DBUS_NAME, 
                    path=GAME_PLAYER_DBUS_PATH)
            return False
        else:
            return True

    def signal_receiver(self, message):
        message_type, contents = message
        if message_type == 'download_update':
            self.webview.execute_script('if (fresh_loading){fresh_loading(%s);}' % 
                    json.dumps(str(contents), encoding="UTF-8", ensure_ascii=False))
        elif message_type == 'download_finish':
            self.load_flash(str(contents))
        elif message_type == 'load_uri':
            self.webview.execute_script("window.location.href = %s" %
                    json.dumps(str(contents).split(',')[0], encoding="UTF-8", ensure_ascii=False))
        elif message_type == 'load_loading_uri':
            self.webview.load_uri(contents)
            self.send_message('loading_uri_finish', '')
        elif message_type == 'game_action':
            if contents == 'pause':
                self.webview.execute_script('$("#mask").hide()')
                self.send_message('game_action', 'pause')
            elif contents == 'continue':
                self.webview.execute_script('$("#mask").hide()')
            
    def handle_dbus_reply(self, *reply):
        # print "%s (reply): %s" % (self.module_dbus_name, str(reply))
        pass
        
    def handle_dbus_error(self, *error):
        #print "%s (error): %s" % (self.module_dbus_name, str(error))
        pass
        
    def send_flash_info(self):
        self.send_message("send_plug_id", (self.appid, self.plug.get_id()))
    def init_ui(self):
        self.loginfo("Init ui")
        # Init application.
        self.application = Application(
            resizable=False,
            destroy_func=self.application_close_window,
            )
        self.application.set_default_size(888, 634)
        self.application.set_skin_preview(utils.get_common_image("frame.png"))
        self.application.set_icon(utils.get_common_image("logo48.png"))
        self.application.add_titlebar(
                ["theme", "menu", "min", "close"],
                show_title=False
                )
        self.application.window.set_title(_("Deepin Store"))
        self.application.window.connect("delete-event", self.application_close_window)

        # Init page box.
        self.page_box = gtk.VBox()

        # Init page switcher.
        self.page_switcher = HSlider(200)
        self.page_switcher.append_page(self.page_box)
        self.page_switcher.set_to_page(self.page_box)

        # Init page align.
        self.page_align = gtk.Alignment()
        self.page_align.set(0.5, 0.5, 1, 1)
        self.page_align.set_padding(0, 0, 2, 2)

        # Append page to switcher.
        self.paned_box = PanedBox(24)
        self.paned_box.add_content_widget(self.page_switcher)
        self.bottom_tip_bar = BottomTipBar()
        self.bottom_tip_bar.close_button.connect('clicked', lambda w: self.paned_box.bottom_window.hide())
        self.paned_box.add_bottom_widget(self.bottom_tip_bar)
        self.page_align.add(self.paned_box)
        self.application.main_box.pack_start(self.page_align, True, True)

        # Init status bar.
        self.statusbar = Statusbar(24)
        status_box = gtk.HBox()
        self.message_box = gtk.HBox()

        self.message_label = Label("", enable_gaussian=True)
        label_align = gtk.Alignment()
        label_align.set(0.0, 0.5, 0, 0)
        label_align.set_padding(0, 0, 10, 0)
        label_align.add(self.message_label)
        self.message_box.pack_start(label_align)

        join_us_button = LinkButton(_("Join us"), "http://www.linuxdeepin.com/joinus/job")
        join_us_button_align = gtk.Alignment()
        join_us_button_align.set(0.5, 0.5, 0, 0)
        join_us_button_align.set_padding(0, 3, 0, 10)
        join_us_button_align.add(join_us_button)
        status_box.pack_start(self.message_box, True, True)
        status_box.pack_start(join_us_button_align, False, False)
        self.statusbar.status_box.pack_start(status_box, True, True)
        self.application.main_box.pack_start(self.statusbar, False, False)

        # Init navigatebar.
        self.detail_page = None
        self.home_page = None
        self.upgrade_page = None
        self.uninstall_page = None
        self.install_page = None

        self.navigatebar = Navigatebar(
                [
                (DynamicPixbuf(utils.get_common_image("navigatebar/nav_home.png")), _("Home"), self.show_home_page),
                (DynamicPixbuf(utils.get_common_image("navigatebar/nav_update.png")), _("Upgrade"), self.show_upgrade_page),
                (DynamicPixbuf(utils.get_common_image("navigatebar/nav_uninstall.png")), _("Uninstall"), self.show_uninstall_page),
                (DynamicPixbuf(utils.get_common_image("navigatebar/nav_download.png")), _("Installation"), self.show_install_page),
                ],
                font_size = 11,
                padding_x = 2,
                padding_y = 2,
                vertical=False,
                item_hover_pixbuf=DynamicPixbuf(utils.get_common_image("navigatebar/nav_hover.png")),
                item_press_pixbuf=DynamicPixbuf(utils.get_common_image("navigatebar/nav_press.png")),
                )
        self.navigatebar.set_size_request(-1, 56)
        self.navigatebar_align = gtk.Alignment(0, 0, 1, 1)
        self.navigatebar_align.set_padding(0, 0, 4, 0)
        self.navigatebar_align.add(self.navigatebar)
        self.application.titlebar.set_size_request(-1, 56)
        self.application.titlebar.left_box.pack_start(self.navigatebar_align, True, True)
        self.application.window.add_move_event(self.navigatebar)

        # Init menu.
        if LANGUAGE == 'en_US':
            menu_min_width = 185
        else:
            menu_min_width = 150
        menu = Menu(
            [
             (None, _("Refresh package lists"), lambda:global_event.emit('start-update-list')),
             (None, _("Open download directory"), self.open_download_directory),
             (None, _("Clear up cached packages"), self.clean_download_cache),
             (None, _("View new features"), lambda : self.show_wizard_win()),
             (self.get_pixbuf_group("menu", "setting"), _("Preferences"), self.show_preference_dialog),
             (self.get_pixbuf_group("menu", "close"), _("Quit"), self.exit),
             ],
            is_root_menu=True,
            menu_min_width=menu_min_width,
            )
        self.application.set_menu_callback(
            lambda button:
                menu.show(
                get_widget_root_coordinate(button, WIDGET_POS_BOTTOM_LEFT),
                (button.get_allocation().width, 0)))

        self.preference_dialog = DscPreferenceDialog()

        if hasattr(self, 'recommend_status'):
            self.init_home_page(self.recommend_status)
        else:
            self.init_home_page()
Example #10
0
class DeepinSoftwareCenter(dbus.service.Object, Logger):
    '''
    class docs
    '''

    pages = ['home', 'upgrade', 'uninstall', 'install']

    def __init__(self, session_bus, arguments):
        '''
        init docs
        '''
        dbus.service.Object.__init__(self, session_bus, DSC_FRONTEND_PATH)
        Logger.__init__(self)

        self.simulate = "--simulate" in arguments

        global debug_flag
        debug_flag = "--debug" in arguments
        self.in_wizard_showing = False
        self.init_hide = False

    def exit(self):
        gtk.main_quit()

    def open_download_directory(self):
        run_command("xdg-open %s" % get_software_download_dir())

    def switch_page(self, page):
        switch_page(self.page_switcher, self.page_box, page, self.detail_page)

    def show_home_page(self):
        if self.detail_page and self.home_page:
            self.switch_page(self.home_page)

    def show_upgrade_page(self):
        if self.detail_page and self.upgrade_page:
            self.switch_page(self.upgrade_page)

    def show_uninstall_page(self):
        if self.detail_page and self.uninstall_page:
            self.switch_page(self.uninstall_page)

    def show_install_page(self):
        if self.detail_page and self.install_page:
            self.switch_page(self.install_page)

    @dbus.service.method(DSC_FRONTEND_NAME, in_signature="s", out_signature="")
    def show_page(self, key):
        try:
            index = self.pages.index(key)
            if index != self.navigatebar.get_index():
                method = "show_%s_page" % key
                getattr(self, method)()
                self.navigatebar.set_index(index)
        except:
            print "Unknow page:", key

    def init_ui(self):
        self.loginfo("Init ui")
        # Init application.
        self.application = Application(
            resizable=False,
            destroy_func=self.application_close_window,
            )
        self.application.set_default_size(888, 634)
        self.application.set_skin_preview(utils.get_common_image("frame.png"))
        self.application.set_icon(utils.get_common_image("logo48.png"))
        self.application.add_titlebar(
                ["theme", "menu", "min", "close"],
                show_title=False
                )
        self.application.window.set_title(_("Deepin Store"))
        self.application.window.connect("delete-event", self.application_close_window)

        # Init page box.
        self.page_box = gtk.VBox()

        # Init page switcher.
        self.page_switcher = HSlider(200)
        self.page_switcher.append_page(self.page_box)
        self.page_switcher.set_to_page(self.page_box)

        # Init page align.
        self.page_align = gtk.Alignment()
        self.page_align.set(0.5, 0.5, 1, 1)
        self.page_align.set_padding(0, 0, 2, 2)

        # Append page to switcher.
        self.paned_box = PanedBox(24)
        self.paned_box.add_content_widget(self.page_switcher)
        self.bottom_tip_bar = BottomTipBar()
        self.bottom_tip_bar.close_button.connect('clicked', lambda w: self.paned_box.bottom_window.hide())
        self.paned_box.add_bottom_widget(self.bottom_tip_bar)
        self.page_align.add(self.paned_box)
        self.application.main_box.pack_start(self.page_align, True, True)

        # Init status bar.
        self.statusbar = Statusbar(24)
        status_box = gtk.HBox()
        self.message_box = gtk.HBox()

        self.message_label = Label("", enable_gaussian=True)
        label_align = gtk.Alignment()
        label_align.set(0.0, 0.5, 0, 0)
        label_align.set_padding(0, 0, 10, 0)
        label_align.add(self.message_label)
        self.message_box.pack_start(label_align)

        join_us_button = LinkButton(_("Join us"), "http://www.linuxdeepin.com/joinus/job")
        join_us_button_align = gtk.Alignment()
        join_us_button_align.set(0.5, 0.5, 0, 0)
        join_us_button_align.set_padding(0, 3, 0, 10)
        join_us_button_align.add(join_us_button)
        status_box.pack_start(self.message_box, True, True)
        status_box.pack_start(join_us_button_align, False, False)
        self.statusbar.status_box.pack_start(status_box, True, True)
        self.application.main_box.pack_start(self.statusbar, False, False)

        # Init navigatebar.
        self.detail_page = None
        self.home_page = None
        self.upgrade_page = None
        self.uninstall_page = None
        self.install_page = None

        self.navigatebar = Navigatebar(
                [
                (DynamicPixbuf(utils.get_common_image("navigatebar/nav_home.png")), _("Home"), self.show_home_page),
                (DynamicPixbuf(utils.get_common_image("navigatebar/nav_update.png")), _("Upgrade"), self.show_upgrade_page),
                (DynamicPixbuf(utils.get_common_image("navigatebar/nav_uninstall.png")), _("Uninstall"), self.show_uninstall_page),
                (DynamicPixbuf(utils.get_common_image("navigatebar/nav_download.png")), _("Installation"), self.show_install_page),
                ],
                font_size = 11,
                padding_x = 2,
                padding_y = 2,
                vertical=False,
                item_hover_pixbuf=DynamicPixbuf(utils.get_common_image("navigatebar/nav_hover.png")),
                item_press_pixbuf=DynamicPixbuf(utils.get_common_image("navigatebar/nav_press.png")),
                )
        self.navigatebar.set_size_request(-1, 56)
        self.navigatebar_align = gtk.Alignment(0, 0, 1, 1)
        self.navigatebar_align.set_padding(0, 0, 4, 0)
        self.navigatebar_align.add(self.navigatebar)
        self.application.titlebar.set_size_request(-1, 56)
        self.application.titlebar.left_box.pack_start(self.navigatebar_align, True, True)
        self.application.window.add_move_event(self.navigatebar)

        # Init menu.
        if LANGUAGE == 'en_US':
            menu_min_width = 185
        else:
            menu_min_width = 150
        menu = Menu(
            [
             (None, _("Refresh package lists"), lambda:global_event.emit('start-update-list')),
             (None, _("Open download directory"), self.open_download_directory),
             (None, _("Clear up cached packages"), self.clean_download_cache),
             (None, _("View new features"), lambda : self.show_wizard_win()),
             (self.get_pixbuf_group("menu", "setting"), _("Preferences"), self.show_preference_dialog),
             (self.get_pixbuf_group("menu", "close"), _("Quit"), self.exit),
             ],
            is_root_menu=True,
            menu_min_width=menu_min_width,
            )
        self.application.set_menu_callback(
            lambda button:
                menu.show(
                get_widget_root_coordinate(button, WIDGET_POS_BOTTOM_LEFT),
                (button.get_allocation().width, 0)))

        self.preference_dialog = DscPreferenceDialog()

        if hasattr(self, 'recommend_status'):
            self.init_home_page(self.recommend_status)
        else:
            self.init_home_page()

    def get_pixbuf_group(self, folder, name):
        return (app_theme.get_pixbuf("%s/%s_normal.png" % (folder, name)),
                app_theme.get_pixbuf("%s/%s_hover.png" % (folder, name)),
                app_theme.get_pixbuf("%s/%s_disable.png" % (folder, name)),
                )

    def application_close_window(self, widget=None, event=None):
        self.application.window.hide_all()
        gtk.main_quit()

        return True

    def upgrade_finish_action(self, pkg_info_list):
        return
        """
        if len(pkg_info_list) > 0:
            # Delete items from treeview.
            upgraded_items = []

            for (pkg_name, marked_delete, marked_install, marked_upgrade) in pkg_info_list:
                for item in self.upgrade_page.upgrade_treeview.visible_items:
                    if item.pkg_name == pkg_name:
                        upgraded_items.append(item)
                        break

            print upgraded_items
            self.upgrade_page.upgrade_treeview.delete_items(upgraded_items)
            print len(self.upgrade_page.upgrade_treeview.visible_items)
        """

    def show_preference_dialog(self):
        self.preference_dialog.show_all()

    def ready_show(self):
        if utils.is_first_started():
            utils.set_first_started()
            self.in_wizard_showing = True
            self.show_wizard_win(True, callback=self.wizard_callback)
            self.init_ui()
        else:
            self.init_ui()
            if not self.init_hide:
                self.application.window.show_all()
        #self.paned_box.bottom_window.set_composited(True)

    def show_wizard_win(self, show_button=False, callback=None):
        program_dir = get_parent_dir(__file__, 2)
        wizard_dir = os.path.join(program_dir, 'wizard', LANGUAGE)
        if not os.path.exists(wizard_dir):
            wizard_dir = os.path.join(program_dir, 'wizard', 'en_US')
        wizard_root_dir = os.path.dirname(wizard_dir)

        self.wizard = Wizard(
            [os.path.join(wizard_dir, "%d.png" % i) for i in range(3)],
            (os.path.join(wizard_root_dir, "dot_normal.png"),
             os.path.join(wizard_root_dir, "dot_active.png"),
             ),
            (os.path.join(wizard_dir, "start_normal.png"),
             os.path.join(wizard_dir, "start_press.png"),
             ),
            show_button,
            callback
            )
        self.wizard.set_icon(utils.get_common_image_pixbuf("logo48.png"))
        if not self.init_hide:
            self.wizard.show_all()

    def wizard_callback(self):
        self.in_wizard_showing = False
        self.application.window.show_all()
        gtk.timeout_add(200, self.application.raise_to_top)

    def init_home_page(self, recommend_status="publish"):

        # Init DBus.
        self.system_bus = dbus.SystemBus()
        bus_object = self.system_bus.get_object(DSC_SERVICE_NAME, DSC_SERVICE_PATH)
        self.bus_interface = dbus.Interface(bus_object, DSC_SERVICE_NAME)
        # Say hello to backend.
        #self.bus_interface.say_hello(self.simulate)
        self.set_software_download_dir()
        self.inhibit_obj = InhibitObject()

        self.loginfo("Init data manager")

        # Init data manager.
        self.data_manager = DataManager(self.bus_interface, debug_flag)

        # Init packages status
        self.packages_status = {}

        # Init home page.
        self.home_page = HomePage(self.data_manager, recommend_status)

        # Init switch page.
        self.switch_page(self.home_page)

        self.in_update_list = False

        self.init_backend()

    def init_backend(self):

        # Init detail view.
        self.detail_page = DetailPage(self.data_manager)

        self.page_switcher.append_page(self.detail_page)

        log("Init pages.")

        self.loginfo("Init pages")
        self.upgrade_page = UpgradePage(self.bus_interface, self.data_manager, self.preference_dialog)
        self.uninstall_page = UninstallPage(self.bus_interface, self.data_manager)
        self.install_page = InstallPage(self.bus_interface, self.data_manager)


        log("Handle global event.")

        # Handle global event.
        global_event.register_event("install-pkg", lambda pkg_names: install_pkg(
            self.bus_interface, self.install_page, pkg_names, self.application.window))
        global_event.register_event("upgrade-pkg", self.upgrade_pkgs)
        global_event.register_event("uninstall-pkg", lambda pkg_name, purge_flag: self.uninstall_pkg(pkg_name, purge_flag))
        global_event.register_event("stop-download-pkg", self.bus_interface.stop_download_pkg)
        global_event.register_event("switch-to-detail-page", lambda pkg_name : switch_to_detail_page(self.page_switcher, self.detail_page, pkg_name))
        global_event.register_event("switch-from-detail-page", lambda : switch_from_detail_page(self.page_switcher, self.detail_page, self.page_box))
        global_event.register_event("remove-wait-action", self.bus_interface.RemoveWaitMissions)
        global_event.register_event("remove-wait-download", self.bus_interface.remove_wait_downloads)
        global_event.register_event("request-clear-action-pages", request_clear_action_pages)
        global_event.register_event("request-stop-install-actions", request_stop_install_actions)
        global_event.register_event("request-clear-failed-action", request_clear_failed_action)
        global_event.register_event("update-upgrade-notify-number", lambda number: update_navigatebar_number(self.navigatebar, 1, number))
        global_event.register_event("update-install-notify-number", lambda number: update_navigatebar_number(self.navigatebar, 3, number))
        global_event.register_event("jump-to-category",
                                    lambda first_category_name, second_category_name:
                                    jump_to_category(self.page_switcher,
                                                     self.page_box,
                                                     self.home_page,
                                                     self.detail_page,
                                                     first_category_name,
                                                     second_category_name))
        global_event.register_event("grade-pkg", lambda pkg, star: grade_pkg(self.application.window, pkg, star))
        global_event.register_event("set-cursor", lambda cursor: set_cursor(self.application.window, cursor))
        global_event.register_event("show-message", self.update_status_bar_message)
        global_event.register_event("start-pkg", lambda alias_name, desktop_infos, offset: start_pkg(
            alias_name, desktop_infos, offset, self.application.window))
        global_event.register_event("show-pkg-name-tooltip", lambda pkg_name: show_tooltip(self.application.window, pkg_name))
        global_event.register_event("hide-pkg-name-tooltip", lambda :tool_tip.hide())
        global_event.register_event("update-current-status-pkg-page", update_current_status_pkg_page)
        global_event.register_event('start-change-mirror', self.change_mirror_action)
        global_event.register_event('download-directory-changed', self.set_software_download_dir)
        global_event.register_event('vote-send-success', lambda p: vote_send_success_callback(p, self.application.window))
        global_event.register_event('vote-send-failed', lambda p: vote_send_failed_callback(p, self.application.window))
        global_event.register_event('max-download-number-changed', self.init_download_manager)
        global_event.register_event('update-list-finish', self.update_list_finish)
        global_event.register_event('start-update-list', self.update_list_handler)
        global_event.register_event("upgrade-finish-action", self.upgrade_finish_action)
        global_event.register_event("upload-error-log", self.exec_upload_error_log)

        self.bus_interface.connect_to_signal(
                signal_name="update_signal",
                handler_function=lambda messages: message_handler(messages,
                                         self.bus_interface,
                                         self.upgrade_page,
                                         self.uninstall_page,
                                         self.install_page,
                                         self.home_page,
                                         self.inhibit_obj,
                                         ),
                )
        glib.timeout_add(1000, lambda : clear_action_pages(self.bus_interface, self.upgrade_page, self.uninstall_page, self.install_page))
        glib.timeout_add(1000, lambda : clear_install_stop_list(self.install_page))
        glib.timeout_add(1000, lambda : clear_failed_action(self.install_page, self.upgrade_page))

        #self.init_download_manager()

        #self.request_update_list()
        self.upgrade_page.fetch_upgrade_info(utils.get_backend_running())

    def change_mirror_action(self, mirror):
        repo_urls = mirror.get_repo_urls()
        self.bus_interface.change_source_list(
            repo_urls,
            reply_handler=lambda :self.handle_mirror_change_reply(mirror),
            error_handler=lambda e:handle_dbus_error("change_source_list", e)
            )

    def exec_upload_error_log(self):
        SendErrorLog().start()

    @dbus.service.method(DSC_FRONTEND_NAME, in_signature="sb", out_signature="")
    def uninstall_pkg(self, pkg_name, purge_flag):
        self.bus_interface.uninstall_pkg(pkg_name, purge_flag,
                reply_handler=lambda :handle_dbus_reply("uninstall_pkg"),
                error_handler=lambda e:handle_dbus_error("uninstall_pkg", e))
        SendUninstallCount(pkg_name).start()

        self.install_page.delete_item_match_pkgname(pkg_name)

    def init_download_manager(self, v=5):
        self.bus_interface.init_download_manager(
                v,
                reply_handler=lambda :self.init_download_manager_handler(),
                error_handler=lambda e:handle_dbus_error("init_download_manager", e))

    def init_download_manager_handler(self):
        self.dbus_request_status()
        self.loginfo("Init download manager")

    def dbus_request_status(self):
        self.bus_interface.request_status(
                reply_handler=lambda reply: request_status_reply_hander(reply, self.install_page, self.upgrade_page, self.uninstall_page),
                error_handler=lambda e:handle_dbus_error("request_status", e),
                )

    def set_software_download_dir(self):
        self.bus_interface.set_download_dir(
                get_software_download_dir(),
                reply_handler=lambda :handle_dbus_reply("set_download_dir"),
                error_handler=lambda e:handle_dbus_error("set_download_dir", e))

    def update_list_handler(self):
        self.show_page("upgrade")
        if not self.in_update_list:
            self.request_update_list()
            global_event.emit('show-updating-view')

    def update_list_finish(self):
        try:
            self.hide_dialog('update_list_dialog')
        except:
            pass
        self.in_update_list = False
        self.data_manager.init_cache_soft_db()

    def hide_dialog(self, name):
        getattr(self, name).hide_all()

    def show_dialog(self, name):
        getattr(self, name).show_all()

    def handle_mirror_change_reply(self, mirror):
        global_event.emit("mirror-backend-changed", mirror)

    def update_status_bar_message(self, message, hide_timeout=0):
        if not self.paned_box.bottom_window.is_visible():
            self.paned_box.bottom_window.show()
        if isinstance(message, list) and len(message) == 4:
            self.bottom_tip_bar.update_info(message[0], message[2], message[3])
            self.bottom_tip_bar.update_end_info(message[1])
        elif isinstance(message, list) and len(message) == 3:
            self.bottom_tip_bar.update_info(*message)
            self.bottom_tip_bar.update_end_info("")
        elif isinstance(message, list) and len(message) == 2:
            self.bottom_tip_bar.update_info(message[0])
            self.bottom_tip_bar.update_end_info(message[1])
        else:
            self.bottom_tip_bar.update_info(message)
            self.bottom_tip_bar.update_end_info("")
        if hide_timeout != 0:
            gtk.timeout_add(hide_timeout, lambda:self.paned_box.bottom_window.hide())

    def request_update_list(self):
        self.in_update_list = True
        self.bus_interface.start_update_list(
                reply_handler=lambda :handle_dbus_reply("start_update_list"),
                error_handler=lambda e:handle_dbus_error("start_update_list", e),)

    def upgrade_pkgs(self, pkg_names):
        self.bus_interface.upgrade_pkgs_with_new_policy(
                pkg_names,
                reply_handler=lambda :handle_dbus_reply("upgrade_pkgs"),
                error_handler=lambda e:handle_dbus_error("upgrade_pkgs", e))
        return False

    def clean_download_cache(self):
        self.bus_interface.clean_download_cache(
                reply_handler=self.clean_download_cache_reply,
                error_handler=lambda e:handle_dbus_error("clean_download_cache", e),
                )

    def clean_download_cache_reply(obj, result):
        num, size = result
        if num != 0:
            message = _("You have cleared up %s packages and saved %s of space.") % (num, bit_to_human_str(size))
        else:
            message = _("Your system cache is empty.")
        global_event.emit("show-message", message, 5000)

    def run(self):
        self.ready_show()
        gtk.main()

        # Send exit request to backend when frontend exit.
        self.bus_interface.request_quit(
                reply_handler=lambda :handle_dbus_reply("request_quit"),
                error_handler=lambda e:handle_dbus_error("request_quit", e))

        # Remove id from config file.
        data_exit()
        self.loginfo('Data id removed')

    @dbus.service.method(DSC_FRONTEND_NAME, in_signature="", out_signature="")
    def request_exit(self):
        self.exit()
        self.bus_interface.request_quit(
                reply_handler=lambda :handle_dbus_reply("request_quit"),
                error_handler=lambda e:handle_dbus_error("request_quit", e))
        data_exit()
        self.loginfo('Data id removed')

    @dbus.service.method(DSC_FRONTEND_NAME, in_signature="as", out_signature="")
    def install_pkgs(self, pkg_names):
        for pkg_name in pkg_names:
            self.install_page.download_wait(pkg_name)
        create_thread(lambda : self.bus_interface.install_pkg(
                                    pkg_names,
                                    reply_handler=lambda :handle_dbus_reply("install_pkg"),
                                    error_handler=lambda e:handle_dbus_error("install_pkg", e))).start()
        for pkg_name in pkg_names:
            SendDownloadCount(pkg_name).start()

    @dbus.service.method(DSC_FRONTEND_NAME, in_signature="", out_signature="")
    def raise_to_top(self):
        if not self.in_wizard_showing:
            self.application.window.show_all()
            self.application.raise_to_top()
        else:
            self.wizard.present()

    @dbus.service.signal(DSC_FRONTEND_NAME)
    def update_signal(self, message):
        pass