class FootBox(gtk.HBox):
    def __init__(self):
        gtk.HBox.__init__(self)

        self.set_size_request(-1, 35)

        self.apply_method = None
        self.init_ui()

        self.timer = Timer(3000, self.__clear_tips)

        Dispatcher.connect("button-change", self.set_button)
        Dispatcher.connect("set-tip", self.set_tip)

        event_manager.add_callback("update-delete-button",
                                   self.__on_update_delete_button)

    def __on_update_delete_button(self, name, obj, data):
        #self.btn_delete.set_child_visible(data)
        self.queue_draw()

    def expose_line(self, widget, event):
        cr = widget.window.cairo_create()
        rect = widget.allocation
        style.draw_out_line(cr, rect)

    def init_ui(self):
        self.tip_align = gtk.Alignment(0, 0.5, 0, 1)
        self.tip = Label("", text_x_align=pango.ALIGN_CENTER, label_width=500)
        self.tip_align.set_padding(5, 5, 20, 0)
        self.tip_align.add(self.tip)

        self.btn_delete = Button(_("Delete"))
        self.btn_delete.connect("clicked", self.delete_click)
        self.btn_delete.set_no_show_all(True)
        self.btn_save = Button()
        self.btn_save.connect("clicked", self.button_click)

        button_box = gtk.HBox(spacing=10)
        button_box.pack_start(self.btn_delete)
        button_box.pack_start(self.btn_save)

        self.buttons_align = gtk.Alignment(1, 0.5, 0, 0)
        self.buttons_align.set_padding(0, 0, 0, 10)
        self.buttons_align.add(button_box)
        self.pack(self, [self.tip_align], True, True)
        self.pack_end(self.buttons_align, False, False)

    def pack(self, parent, widgets, expand=False, fill=False):
        for widget in widgets:
            parent.pack_start(widget, expand, fill)

    def set_lock(self, state):
        self.__setting_module.set_lock(state)

    def get_lock(self):
        return self.__setting_module.get_lock()

    def set_button(self, widget, content, state):
        self.btn_save.set_label(_("Save"))
        self.btn_save.set_sensitive(state)

    def delete_click(self, widget):
        if self.focus_connection:
            Dispatcher.delete_setting(self.focus_connection)
            Dispatcher.to_main_page()

    def show_delete(self, connection):
        self.btn_delete.show()
        self.focus_connection = connection

    def hide_delete(self):
        self.btn_delete.hide()
        self.focus_connection = None

        #if content == "save":
        #if state and not self.get_lock():
        #Dispatcher.emit("setting-saved")
        #else:
        #self.btn_save.set_label(_("connect"))
        #self.btn_save.set_sensitive(False)
        #else:
        #self.btn_save.set_label(_("connect"))
        #self.btn_save.set_sensitive(True)

    def get_button(self):
        return self.__setting_module.get_button_state()

    def set_setting(self, module):
        self.__setting_module = module

    def button_click(self, widget):
        #if self.btn_save.label == "save":
        Dispatcher.emit("setting-saved")
        #elif self.btn_save.label == _("connect"):
        #Dispatcher.set_tip("setting saved")
        #Dispatcher.emit("setting-appled")

    def __clear_tips(self):
        self.tip.set_text("")

    def set_tip(self, widget, new_tip):
        self.tip.set_text(_("Tip:") + new_tip)
        self.timer.restart()
Example #2
0
    def init_share_box(self):
        '''get weibo info, and create button'''
        self.to_share_weibo = {}
        self.to_share_weibo_res = {}
        self.deepin_info = {}
        self.is_get_user_info = {}
        self.__weibo_check_button_list = []
        self.__weibo_image_button_list = []

        # create Thumbnail
        if exists(self.upload_image):
            pixbuf = gtk.gdk.pixbuf_new_from_file(self.upload_image)
            pix_w = pixbuf.get_width()
            pix_h = pixbuf.get_height()
            if pix_w > pix_h:
                pix_s_w = self.thumb_width
                pix_s_h = int(pix_h / (float(pix_w) / self.thumb_width))
            else:
                pix_s_h = self.thumb_height
                pix_s_w = int(pix_w / (float(pix_h) / self.thumb_height))
            pixbuf = pixbuf.scale_simple(pix_s_w, pix_s_h, gtk.gdk.INTERP_TILES)
            thumb = gtk.image_new_from_pixbuf(pixbuf)
        else:
            thumb = gtk.Image()
        thumb.set_size_request(self.thumb_width, self.thumb_height)

        # weibo context input
        text_box = gtk.HBox(False, 2)
        text_vbox = gtk.VBox(False, 2)
        text_bg_vbox = gtk.VBox(False)
        text_bg_align = gtk.Alignment()
        text_bg_align.set(0.5, 0.5, 0, 0)
        text_bg_align.set_padding(5, 5, 16, 5)
        text_bg_align.connect("expose-event", self.text_view_bg_expose)
        text_scrolled_win = ScrolledWindow()
        text_scrolled_win.set_size_request(340, 157)

        text_view = gtk.TextView()
        text_view.set_left_margin(10)
        text_view.set_right_margin(10)
        text_view.set_pixels_above_lines(5)
        text_view.set_pixels_below_lines(5)
        text_view.set_wrap_mode(gtk.WRAP_WORD| gtk.WRAP_CHAR)
        text_view.connect("expose-event", self.text_view_expose)
        buf = text_view.get_buffer()
        text_scrolled_win.add(text_view)
        text_bg_vbox.pack_start(text_scrolled_win)
        text_bg_align.add(text_bg_vbox)

        text_align = gtk.Alignment() 
        text_align.set(0.5, 0.5, 0, 0)
        text_align.set_padding(25, 30, 10, 10)

        text_box.pack_start(thumb, False, False, 10)
        text_box.pack_start(text_bg_align)
        text_vbox.pack_start(text_box, False, False, 10)

        text_align.add(text_vbox)
        #tmp_align = gtk.Alignment()
        #tmp_align.set(0.5, 0, 0, 1)
        #self.share_box.pack_start(tmp_align, False, False)
        self.share_box.pack_start(text_align, False, False)

        # dialog button box
        left_box = self.window.left_button_box
        right_box = self.window.right_button_box

        # input tip label
        self.input_num_label = Label("%d" % self.MAX_CHAR,
            text_size=16, text_x_align=pango.ALIGN_CENTER, label_width=50, enable_select=False)
        self.input_num_label.text_color = app_theme.get_color("left_char_num")

        # login box
        #weibo_box = gtk.HBox(False, 1)
        #weibo_box.set_size_request(-1, 50)
        weibo_box_list = []
        self.loading_label = Label("%s..." % _("Loading"), text_size=12,
            label_width=70, enable_select=False)
        weibo_box_list.append(self.loading_label)

        for weibo in self.__weibo_list:
            box = gtk.HBox(False, 2)
            weibo.set_box(box)
            weibo_box_list.append(box)
        left_box.set_buttons(weibo_box_list)

        # share button
        button = Button(_("Share"))
        #button.set_size_request(75, 25)
        button.connect("clicked", self.share_button_clicked, text_view)
        buf.connect("changed", self.text_view_changed, button)  # check char num

        tmp_vbox = gtk.VBox(False)
        tmp_align = gtk.Alignment()
        tmp_align.set(0.5, 0.5, 0, 0)
        tmp_vbox.pack_start(button, False, False)
        #tmp_vbox.pack_start(tmp_align)
        tmp_align.add(tmp_vbox)
        right_box.set_buttons([self.input_num_label, tmp_align])

        # at first, set widget insensitive
        button.set_sensitive(False)
        text_view.set_editable(False)
        t = threading.Thread(target=self.init_user_info_thread, args=(button, text_view))
        t.setDaemon(True)
        t.start()
class FootBox(gtk.HBox):
    def __init__(self):
        gtk.HBox.__init__(self)

        self.set_size_request(-1, 35)

        self.apply_method = None
        self.init_ui()

        self.timer = Timer(3000, self.__clear_tips)

        Dispatcher.connect("button-change", self.set_button)
        Dispatcher.connect("set-tip", self.set_tip)

        event_manager.add_callback("update-delete-button", self.__on_update_delete_button)

    def __on_update_delete_button(self, name, obj, data):
        #self.btn_delete.set_child_visible(data)
        self.queue_draw()

    def expose_line(self, widget, event):
        cr = widget.window.cairo_create()
        rect = widget.allocation
        style.draw_out_line(cr, rect)

    def init_ui(self):
        self.tip_align = gtk.Alignment(0, 0.5, 0, 1)
        self.tip = Label("",
                         text_x_align=pango.ALIGN_CENTER,
                         label_width=500)
        self.tip_align.set_padding(5, 5, 20, 0)
        self.tip_align.add(self.tip)


        self.btn_delete = Button(_("Delete"))
        self.btn_delete.connect("clicked", self.delete_click)
        self.btn_delete.set_no_show_all(True)
        self.btn_save = Button()
        self.btn_save.connect("clicked", self.button_click)

        button_box = gtk.HBox(spacing=10)
        button_box.pack_start(self.btn_delete)
        button_box.pack_start(self.btn_save)

        self.buttons_align = gtk.Alignment(1, 0.5, 0, 0)
        self.buttons_align.set_padding(0, 0, 0, 10)
        self.buttons_align.add(button_box)
        self.pack(self, [self.tip_align], True, True)
        self.pack_end(self.buttons_align, False, False)
    
    def pack(self, parent, widgets, expand=False, fill=False):
        for widget in widgets:
            parent.pack_start(widget, expand, fill)

    def set_lock(self, state):
        self.__setting_module.set_lock(state)

    def get_lock(self):
        return self.__setting_module.get_lock()

    def set_button(self, widget, content, state):
        self.btn_save.set_label(_("Save"))
        self.btn_save.set_sensitive(state)

    def delete_click(self, widget):
        if self.focus_connection:
            Dispatcher.delete_setting(self.focus_connection)
            Dispatcher.to_main_page()

    def show_delete(self, connection):
        self.btn_delete.show()
        self.focus_connection = connection

    def hide_delete(self):
        self.btn_delete.hide()
        self.focus_connection = None
            

        #if content == "save":
            #if state and not self.get_lock():
                #Dispatcher.emit("setting-saved")
            #else:
                #self.btn_save.set_label(_("connect"))
                #self.btn_save.set_sensitive(False)
        #else:
            #self.btn_save.set_label(_("connect"))
            #self.btn_save.set_sensitive(True)

    def get_button(self):
        return self.__setting_module.get_button_state()

    def set_setting(self, module):
        self.__setting_module = module

    def button_click(self, widget):
        #if self.btn_save.label == "save":
        Dispatcher.emit("setting-saved")
        #elif self.btn_save.label == _("connect"):
            #Dispatcher.set_tip("setting saved")
            #Dispatcher.emit("setting-appled")

    def __clear_tips(self):
        self.tip.set_text("")

    def set_tip(self, widget, new_tip):
        self.tip.set_text(_("Tip:") + new_tip)
        self.timer.restart()
Example #4
0
    def init_share_box(self):
        '''get weibo info, and create button'''
        self.to_share_weibo = {}
        self.to_share_weibo_res = {}
        self.deepin_info = {}
        self.is_get_user_info = {}
        self.__weibo_check_button_list = []
        self.__weibo_image_button_list = []

        # create Thumbnail
        if exists(self.upload_image):
            pixbuf = gtk.gdk.pixbuf_new_from_file(self.upload_image)
            pix_w = pixbuf.get_width()
            pix_h = pixbuf.get_height()
            if pix_w > pix_h:
                pix_s_w = self.thumb_width
                pix_s_h = int(pix_h / (float(pix_w) / self.thumb_width))
            else:
                pix_s_h = self.thumb_height
                pix_s_w = int(pix_w / (float(pix_h) / self.thumb_height))
            pixbuf = pixbuf.scale_simple(pix_s_w, pix_s_h, gtk.gdk.INTERP_TILES)
            thumb = gtk.image_new_from_pixbuf(pixbuf)
        else:
            thumb = gtk.Image()
        thumb.set_size_request(self.thumb_width, self.thumb_height)

        # weibo context input
        text_box = gtk.HBox(False, 2)
        text_vbox = gtk.VBox(False, 2)
        text_bg_vbox = gtk.VBox(False)
        text_bg_align = gtk.Alignment()
        text_bg_align.set(0.5, 0.5, 0, 0)
        text_bg_align.set_padding(5, 5, 16, 5)
        text_bg_align.connect("expose-event", self.text_view_bg_expose)
        text_scrolled_win = gtk.ScrolledWindow()
        text_scrolled_win.set_policy(gtk.POLICY_NEVER, gtk.POLICY_NEVER)
        text_scrolled_win.set_size_request(340, 157)

        text_view = gtk.TextView()
        text_view.set_left_margin(10)
        text_view.set_right_margin(10)
        text_view.set_pixels_above_lines(5)
        text_view.set_pixels_below_lines(5)
        text_view.set_wrap_mode(gtk.WRAP_WORD| gtk.WRAP_CHAR)
        text_view.connect("expose-event", self.text_view_expose)
        buf = text_view.get_buffer()
        text_scrolled_win.add(text_view)
        text_bg_vbox.pack_start(text_scrolled_win)
        text_bg_align.add(text_bg_vbox)

        text_align = gtk.Alignment()
        text_align.set(0.5, 0.5, 0, 0)
        text_align.set_padding(25, 30, 10, 10)

        text_box.pack_start(thumb, False, False, 10)
        text_box.pack_start(text_bg_align)
        text_vbox.pack_start(text_box, False, False, 10)

        text_align.add(text_vbox)
        #tmp_align = gtk.Alignment()
        #tmp_align.set(0.5, 0, 0, 1)
        #self.share_box.pack_start(tmp_align, False, False)
        self.share_box.pack_start(text_align, False, False)

        # dialog button box
        left_box = self.window.left_button_box
        right_box = self.window.right_button_box

        # input tip label
        self.input_num_label = Label("%d" % self.MAX_CHAR,
            text_size=16, text_x_align=pango.ALIGN_CENTER, label_width=50, enable_select=False)
        self.input_num_label.text_color = ui_theme.get_color("label_select_text")

        # login box
        #weibo_box = gtk.HBox(False, 1)
        #weibo_box.set_size_request(-1, 50)
        weibo_box_list = []
        self.loading_label = Label("%s..." % _("Loading"), text_size=12,
            label_width=70, enable_select=False)
        weibo_box_list.append(self.loading_label)

        for weibo in self.__weibo_list:
            box = gtk.HBox(False, 2)
            weibo.set_box(box)
            weibo_box_list.append(box)
        left_box.set_buttons(weibo_box_list)

        # share button
        button = Button(_("Share"))
        #button.set_size_request(75, 25)
        button.connect("clicked", self.share_button_clicked, text_view)
        buf.connect("changed", self.text_view_changed, button)  # check char num

        tmp_vbox = gtk.VBox(False)
        tmp_align = gtk.Alignment()
        tmp_align.set(0.5, 0.5, 0, 0)
        tmp_vbox.pack_start(button, False, False)
        #tmp_vbox.pack_start(tmp_align)
        tmp_align.add(tmp_vbox)
        right_box.set_buttons([self.input_num_label, tmp_align])

        # at first, set widget insensitive
        button.set_sensitive(False)
        text_view.set_editable(False)
        t = threading.Thread(target=self.init_user_info_thread, args=(button, text_view))
        t.setDaemon(True)
        t.start()
class BlueToothView(gtk.VBox):
    '''
    class docs
    '''

    def __init__(self, module_frame):
        '''
        init docs
        '''
        gtk.VBox.__init__(self)

        self.module_frame = module_frame

        self.my_bluetooth = MyBluetooth(self.__on_adapter_removed,
                                        self.__on_default_adapter_changed,
                                        self.__device_found)
        self.periodic_timer = None
        self.is_discoverable = False
        self.is_searching = False
        '''
        enable open
        '''
        if self.my_bluetooth.adapter:
            self.my_bluetooth.adapter.connect("property-changed", self.__on_property_changed)
            if self.my_bluetooth.adapter.get_powered():
                self.title_align, self.title_label = self.__setup_title_align(
                    app_theme.get_pixbuf("bluetooth/enable_open.png"), _("Bluetooth"))
                self.title_label.set_sensitive(True)
            else:
                self.title_align, self.title_label = self.__setup_title_align(
                    app_theme.get_pixbuf("bluetooth/enable_open_disable.png"), _("Bluetooth"))
        else:
            self.title_align, self.title_label = self.__setup_title_align(
                app_theme.get_pixbuf("bluetooth/enable_open_disable.png"), _("Bluetooth"))
            self.title_label.set_sensitive(False)
        self.enable_align = self.__setup_align()
        self.enable_box = gtk.HBox(spacing=WIDGET_SPACING)
        self.enable_open_label = self.__setup_label(_("Enable bluetooth"))
        if self.my_bluetooth.adapter:
            self.my_bluetooth.adapter.set_powered(permanent_settings.get_powered())
            self.enable_open_label.set_sensitive(self.my_bluetooth.adapter.get_powered())
        else:
            self.enable_open_label.set_sensitive(False)
        self.enable_open_toggle_align = self.__setup_align(padding_top = 4, padding_left = 158)
        self.enable_open_toggle = self.__setup_toggle()
        if self.my_bluetooth.adapter:
            self.enable_open_toggle.set_active(self.my_bluetooth.adapter.get_powered())
        self.enable_open_toggle.connect("toggled", self.__toggled, "enable_open")
        self.enable_open_toggle_align.add(self.enable_open_toggle)
        self.__widget_pack_start(self.enable_box,
                                 [self.enable_open_label,
                                  self.enable_open_toggle_align])
        self.enable_align.add(self.enable_box)
        '''
        display
        '''
        self.display_align = self.__setup_align()
        self.display_box = gtk.HBox(spacing=WIDGET_SPACING)
        self.display_device_label = self.__setup_label(_("Device name"))
        if self.my_bluetooth.adapter:
            self.display_device_label.set_sensitive(self.my_bluetooth.adapter.get_powered())
        else:
            self.display_device_label.set_sensitive(False)
        self.display_device_entry = InputEntry()
        if self.my_bluetooth.adapter:
            self.display_device_entry.set_text(self.my_bluetooth.adapter.get_name())
            self.display_device_entry.set_sensitive(self.my_bluetooth.adapter.get_powered())
        else:
            self.display_device_entry.set_sensitive(False)
        self.display_device_entry.set_size(HSCALEBAR_WIDTH, WIDGET_HEIGHT)
        self.display_device_entry.entry.connect("changed", self.__display_device_changed)
        self.__widget_pack_start(self.display_box,
                                 [self.display_device_label, self.display_device_entry])
        self.display_align.add(self.display_box)
        '''
        enable searchable
        '''
        self.search_align = self.__setup_align()
        self.search_box = gtk.HBox(spacing=WIDGET_SPACING)
        self.search_label = self.__setup_label(_("Discoverable"))
        if self.my_bluetooth.adapter:
            self.search_label.set_sensitive(self.my_bluetooth.adapter.get_powered())
        else:
            self.search_label.set_sensitive(False)
        self.search_timeout_align = self.__setup_align(padding_top = 0, padding_left = 0)
        self.search_timeout_label = self.__setup_label("", width = 110, align = ALIGN_START)
        self.search_timeout_align.add(self.search_timeout_label)
        self.search_toggle_align = self.__setup_align(padding_top = 4, padding_left = 18)
        self.search_toggle = self.__setup_toggle()
        if self.my_bluetooth.adapter:
            self.search_toggle.set_active(self.my_bluetooth.adapter.get_discoverable())
        self.search_toggle.connect("toggled", self.__toggled, "search")
        self.search_toggle_align.add(self.search_toggle)
        self.__widget_pack_start(self.search_box,
                                 [self.search_label,
                                  self.search_timeout_align,
                                  self.search_toggle_align
                                 ])
        self.search_align.add(self.search_box)
        '''
        device iconview
        '''
        self.device_align = self.__setup_align()
        self.device_iconview = DeviceIconView()
        self.device_iconview.set_size_request(690, 228)
        self.device_align.add(self.device_iconview)
        '''
        operation
        '''
        self.oper_align = self.__setup_align()
        self.oper_box = gtk.HBox(spacing = WIDGET_SPACING)
        self.notice_label = Label("", text_x_align = ALIGN_START, label_width = 610)
        self.search_button = Button(_("Search"))
        if self.my_bluetooth.adapter:
            self.search_button.set_sensitive(self.my_bluetooth.adapter.get_powered())
        else:
            self.search_button.set_sensitive(False)
        self.search_button.connect("clicked", self.__on_search)
        self.__widget_pack_start(self.oper_box,
                [self.notice_label,
                 self.search_button,
                ])
        self.oper_align.add(self.oper_box)
        '''
        this->gtk.VBox pack_start
        '''
        self.__widget_pack_start(self,
                                 [self.title_align,
                                  self.enable_align,
                                  self.display_align,
                                  self.search_align,
                                  self.device_align,
                                  self.oper_align])

        if self.my_bluetooth.adapter == None:
            self.set_sensitive(False)

        self.connect("expose-event", self.__expose)

        if self.my_bluetooth.adapter and self.my_bluetooth.adapter.get_powered():
            self.__get_devices()

    def __on_property_changed(self, adapter, key, value):
        if key == "Powered":
            if value == 1:
                self.enable_open_toggle.set_active(True)
                # removed by hualet, this will cause devices are added twice.
                # self.__set_enable_open(True)
            else:
                self.enable_open_toggle.set_active(False)
                # self.__set_enable_open(False)
        if key == "Devices":    # fixbug: iconview didn't update accordingly 
                                # while adapter paired other devices in system tray. 
            self.device_iconview.clear()
            self.__get_devices()

    def sendfile(self, device_name):
        event_manager.emit("send-file", device_name)

    def cancel(self):
        event_manager.emit("cancel", None)

    def __on_adapter_removed(self):
        self.set_sensitive(False)

    def __on_default_adapter_changed(self):
        self.set_sensitive(True)
        self.display_device_entry.set_text(self.my_bluetooth.adapter.get_name())

    def __display_device_changed(self, widget, event):
        self.my_bluetooth.adapter.set_name(widget.get_text())

    def __setup_separator(self):
        hseparator = HSeparator(app_theme.get_shadow_color("hSeparator").get_color_info(), 0, 0)
        hseparator.set_size_request(500, HSEPARATOR_HEIGHT)
        return hseparator

    def __setup_title_label(self,
                            text="",
                            text_color=app_theme.get_color("globalTitleForeground"),
                            text_size=TITLE_FONT_SIZE,
                            text_x_align=ALIGN_START,
                            label_width=180):
        return Label(text = text,
                     text_color = text_color,
                     text_size = text_size,
                     text_x_align = text_x_align,
                     label_width = label_width,
                     enable_select = False,
                     enable_double_click = False)

    def __setup_title_align(self,
                            pixbuf,
                            text,
                            padding_top=TEXT_WINDOW_TOP_PADDING,
                            padding_left=TEXT_WINDOW_LEFT_PADDING):
        align = self.__setup_align(padding_top = padding_top, padding_left = padding_left)
        align_box = gtk.VBox(spacing = WIDGET_SPACING)
        title_box = gtk.HBox(spacing = WIDGET_SPACING)
        image = ImageBox(pixbuf)
        label = self.__setup_title_label(text)
        separator = self.__setup_separator()
        self.__widget_pack_start(title_box, [image, label])
        self.__widget_pack_start(align_box, [title_box, separator])
        align.add(align_box)
        return align, label

    def __get_devices(self):
        devices = self.my_bluetooth.get_devices()
        items = []
        i = 0

        while i < len(devices):
            items.append(DeviceItem(devices[i].get_name(),
                         app_theme.get_pixbuf("bluetooth/%s.png" % bluetooth_class_to_type(devices[i].get_class())).get_pixbuf(),
                         devices[i],
                         self.my_bluetooth.adapter,
                         devices[i].get_paired(),
                         self.module_frame))
            i += 1

        self.device_iconview.add_items(items)

    def __refresh_notice_label(self):
        searching_str = _("Discovering device")

        if self.notice_label.get_text().count(".") == 3:
            self.notice_label.set_text(searching_str + ".")
        else:
            self.notice_label.set_text(self.notice_label.get_text() + ".")

        return True

    def __on_search(self, widget):
        if not self.is_searching:
            self.my_bluetooth.adapter.start_discovery()
            self.notice_label.set_text(_("Discovering device"))
            self.refresh_lable_timeout = gobject.timeout_add_seconds(1, self.__refresh_notice_label)
            self.my_bluetooth.adapter.connect("property-changed", self.on_adapter_property_changed)
            self.is_searching = True
            
    def on_adapter_property_changed(self, obj, key, value):
        if key == "Discovering" and value == False:
            gobject.source_remove(self.refresh_lable_timeout)
            if self.is_searching:
                self.my_bluetooth.adapter.stop_discovery()
            self.is_searching = False
            self.notice_label.set_text("")

    def __device_found(self, adapter, address, values):
        print "address", address
        if address not in adapter.get_address_records():
            device_path = adapter.create_device(address)
            if device_path == "None":
                return 
            
            device = Device(device_path)
            items = []
        
            if not values.has_key("Name"):
                return
        
            print bluetooth_class_to_type(device.get_class())
            items.append(DeviceItem(values['Name'],
                                    app_theme.get_pixbuf("bluetooth/%s.png" 
                                                         % bluetooth_class_to_type(device.get_class())).get_pixbuf(), 
                                    device, 
                                    adapter))
            self.device_iconview.add_items(items)
                
    def __set_enable_open(self, is_open=True):
        self.enable_open_label.set_sensitive(is_open)
        self.display_device_label.set_sensitive(is_open)
        self.search_label.set_sensitive(is_open)
        self.display_align.set_sensitive(is_open)
        self.display_device_entry.set_sensitive(is_open)
        # self.device_iconview.set_sensitive(is_open)
        self.search_align.set_sensitive(is_open)
        self.search_timeout_label.set_child_visible(False)
        self.search_button.set_sensitive(is_open)
        # changed by hualet, to fix the bug that device icons stay while disabling the iconview widget
        if is_open:
            self.__get_devices()
        else:
            self.device_iconview.clear()

    def __toggled(self, widget, object):
        if self.my_bluetooth.adapter == None:
            return

        if object == "enable_open":
            self.__set_enable_open(widget.get_active())
            permanent_settings.set_powered(widget.get_active())
            self.my_bluetooth.adapter.set_powered(widget.get_active())
            return

        if object == "search":
            self.is_discoverable = widget.get_active()
            self.my_bluetooth.adapter.set_discoverable(self.is_discoverable)
            self.search_timeout_label.set_child_visible(self.is_discoverable)
            if self.is_discoverable:
                self.periodic_timer = PerodicTimer(self, 1)
            else:
                self.periodic_timer.stop()
            return

    def __expose(self, widget, event):
        cr = widget.window.cairo_create()
        rect = widget.allocation

        cr.set_source_rgb(*color_hex_to_cairo(MODULE_BG_COLOR))
        cr.rectangle(rect.x, rect.y, rect.width, rect.height)
        cr.fill()

    def __setup_label(self, text="", width=180, align=ALIGN_END):
        return Label(text, None, TITLE_FONT_SIZE, align, width, False, False, False)

    def __setup_combo(self, items=[], width=HSCALEBAR_WIDTH):
        combo = ComboBox(items, None, 0, width, width)
        combo.set_size_request(width, WIDGET_HEIGHT)
        return combo

    def __setup_toggle(self):
        return ToggleButton(
                app_theme.get_pixbuf("toggle_button/inactive_normal.png"),
                app_theme.get_pixbuf("toggle_button/active_normal.png"),
                inactive_disable_dpixbuf = app_theme.get_pixbuf("toggle_button/inactive_normal.png"),
                active_disable_dpixbuf = app_theme.get_pixbuf("toggle_button/inactive_normal.png"))

    def __setup_align(self, xalign=0, yalign=0, xscale=0, yscale=0,
                      padding_top=BETWEEN_SPACING,
                      padding_bottom=0,
                      padding_left=TEXT_WINDOW_LEFT_PADDING,
                      padding_right=20):
        align = gtk.Alignment()
        align.set(xalign, yalign, xscale, yscale)
        align.set_padding(padding_top, padding_bottom, padding_left, padding_right)
        return align

    def __widget_pack_start(self, parent_widget, widgets=[], expand=False, fill=False):
        if parent_widget == None:
            return
        for item in widgets:
            parent_widget.pack_start(item, expand, fill)
class BlueToothView(gtk.VBox):
    '''
    class docs
    '''
    def __init__(self, module_frame):
        '''
        init docs
        '''
        gtk.VBox.__init__(self)

        self.module_frame = module_frame

        self.my_bluetooth = MyBluetooth(self.__on_adapter_removed,
                                        self.__on_default_adapter_changed,
                                        self.__device_found)
        self.periodic_timer = None
        self.is_discoverable = False
        self.is_searching = False
        '''
        enable open
        '''
        if self.my_bluetooth.adapter:
            self.my_bluetooth.adapter.connect("property-changed",
                                              self.__on_property_changed)
            if self.my_bluetooth.adapter.get_powered():
                self.title_align, self.title_label = self.__setup_title_align(
                    app_theme.get_pixbuf("bluetooth/enable_open.png"),
                    _("Bluetooth"))
                self.title_label.set_sensitive(True)
            else:
                self.title_align, self.title_label = self.__setup_title_align(
                    app_theme.get_pixbuf("bluetooth/enable_open_disable.png"),
                    _("Bluetooth"))
        else:
            self.title_align, self.title_label = self.__setup_title_align(
                app_theme.get_pixbuf("bluetooth/enable_open_disable.png"),
                _("Bluetooth"))
            self.title_label.set_sensitive(False)
        self.enable_align = self.__setup_align()
        self.enable_box = gtk.HBox(spacing=WIDGET_SPACING)
        self.enable_open_label = self.__setup_label(_("Enable bluetooth"))
        if self.my_bluetooth.adapter:
            self.my_bluetooth.adapter.set_powered(
                permanent_settings.get_powered())
            self.enable_open_label.set_sensitive(
                self.my_bluetooth.adapter.get_powered())
        else:
            self.enable_open_label.set_sensitive(False)
        self.enable_open_toggle_align = self.__setup_align(padding_top=4,
                                                           padding_left=158)
        self.enable_open_toggle = self.__setup_toggle()
        if self.my_bluetooth.adapter:
            self.enable_open_toggle.set_active(
                self.my_bluetooth.adapter.get_powered())
        self.enable_open_toggle.connect("toggled", self.__toggled,
                                        "enable_open")
        self.enable_open_toggle_align.add(self.enable_open_toggle)
        self.__widget_pack_start(
            self.enable_box,
            [self.enable_open_label, self.enable_open_toggle_align])
        self.enable_align.add(self.enable_box)
        '''
        display
        '''
        self.display_align = self.__setup_align()
        self.display_box = gtk.HBox(spacing=WIDGET_SPACING)
        self.display_device_label = self.__setup_label(_("Device name"))
        if self.my_bluetooth.adapter:
            self.display_device_label.set_sensitive(
                self.my_bluetooth.adapter.get_powered())
        else:
            self.display_device_label.set_sensitive(False)
        self.display_device_entry = InputEntry()
        if self.my_bluetooth.adapter:
            self.display_device_entry.set_text(
                self.my_bluetooth.adapter.get_name())
            self.display_device_entry.set_sensitive(
                self.my_bluetooth.adapter.get_powered())
        else:
            self.display_device_entry.set_sensitive(False)
        self.display_device_entry.set_size(HSCALEBAR_WIDTH, WIDGET_HEIGHT)
        self.display_device_entry.entry.connect("changed",
                                                self.__display_device_changed)
        self.__widget_pack_start(
            self.display_box,
            [self.display_device_label, self.display_device_entry])
        self.display_align.add(self.display_box)
        '''
        enable searchable
        '''
        self.search_align = self.__setup_align()
        self.search_box = gtk.HBox(spacing=WIDGET_SPACING)
        self.search_label = self.__setup_label(_("Discoverable"))
        if self.my_bluetooth.adapter:
            self.search_label.set_sensitive(
                self.my_bluetooth.adapter.get_powered())
        else:
            self.search_label.set_sensitive(False)
        self.search_timeout_align = self.__setup_align(padding_top=0,
                                                       padding_left=0)
        self.search_timeout_label = self.__setup_label("",
                                                       width=110,
                                                       align=ALIGN_START)
        self.search_timeout_align.add(self.search_timeout_label)
        self.search_toggle_align = self.__setup_align(padding_top=4,
                                                      padding_left=18)
        self.search_toggle = self.__setup_toggle()
        if self.my_bluetooth.adapter:
            self.search_toggle.set_active(
                self.my_bluetooth.adapter.get_discoverable())
        self.search_toggle.connect("toggled", self.__toggled, "search")
        self.search_toggle_align.add(self.search_toggle)
        self.__widget_pack_start(self.search_box, [
            self.search_label, self.search_timeout_align,
            self.search_toggle_align
        ])
        self.search_align.add(self.search_box)
        '''
        device iconview
        '''
        self.device_align = self.__setup_align()
        self.device_iconview = DeviceIconView()
        self.device_iconview.set_size_request(690, 228)
        self.device_align.add(self.device_iconview)
        '''
        operation
        '''
        self.oper_align = self.__setup_align()
        self.oper_box = gtk.HBox(spacing=WIDGET_SPACING)
        self.notice_label = Label("",
                                  text_x_align=ALIGN_START,
                                  label_width=610)
        self.search_button = Button(_("Search"))
        if self.my_bluetooth.adapter:
            self.search_button.set_sensitive(
                self.my_bluetooth.adapter.get_powered())
        else:
            self.search_button.set_sensitive(False)
        self.search_button.connect("clicked", self.__on_search)
        self.__widget_pack_start(self.oper_box, [
            self.notice_label,
            self.search_button,
        ])
        self.oper_align.add(self.oper_box)
        '''
        this->gtk.VBox pack_start
        '''
        self.__widget_pack_start(self, [
            self.title_align, self.enable_align, self.display_align,
            self.search_align, self.device_align, self.oper_align
        ])

        if self.my_bluetooth.adapter == None:
            self.set_sensitive(False)

        self.connect("expose-event", self.__expose)

        if self.my_bluetooth.adapter and self.my_bluetooth.adapter.get_powered(
        ):
            self.__get_devices()

    def __on_property_changed(self, adapter, key, value):
        if key == "Powered":
            if value == 1:
                self.enable_open_toggle.set_active(True)
                # removed by hualet, this will cause devices are added twice.
                # self.__set_enable_open(True)
            else:
                self.enable_open_toggle.set_active(False)
                # self.__set_enable_open(False)
        if key == "Devices":  # fixbug: iconview didn't update accordingly
            # while adapter paired other devices in system tray.
            self.device_iconview.clear()
            self.__get_devices()

    def sendfile(self, device_name):
        event_manager.emit("send-file", device_name)

    def cancel(self):
        event_manager.emit("cancel", None)

    def __on_adapter_removed(self):
        self.set_sensitive(False)

    def __on_default_adapter_changed(self):
        self.set_sensitive(True)
        self.display_device_entry.set_text(
            self.my_bluetooth.adapter.get_name())

    def __display_device_changed(self, widget, event):
        self.my_bluetooth.adapter.set_name(widget.get_text())

    def __setup_separator(self):
        hseparator = HSeparator(
            app_theme.get_shadow_color("hSeparator").get_color_info(), 0, 0)
        hseparator.set_size_request(500, HSEPARATOR_HEIGHT)
        return hseparator

    def __setup_title_label(
            self,
            text="",
            text_color=app_theme.get_color("globalTitleForeground"),
            text_size=TITLE_FONT_SIZE,
            text_x_align=ALIGN_START,
            label_width=180):
        return Label(text=text,
                     text_color=text_color,
                     text_size=text_size,
                     text_x_align=text_x_align,
                     label_width=label_width,
                     enable_select=False,
                     enable_double_click=False)

    def __setup_title_align(self,
                            pixbuf,
                            text,
                            padding_top=TEXT_WINDOW_TOP_PADDING,
                            padding_left=TEXT_WINDOW_LEFT_PADDING):
        align = self.__setup_align(padding_top=padding_top,
                                   padding_left=padding_left)
        align_box = gtk.VBox(spacing=WIDGET_SPACING)
        title_box = gtk.HBox(spacing=WIDGET_SPACING)
        image = ImageBox(pixbuf)
        label = self.__setup_title_label(text)
        separator = self.__setup_separator()
        self.__widget_pack_start(title_box, [image, label])
        self.__widget_pack_start(align_box, [title_box, separator])
        align.add(align_box)
        return align, label

    def __get_devices(self):
        devices = self.my_bluetooth.get_devices()
        items = []
        i = 0

        while i < len(devices):
            items.append(
                DeviceItem(
                    devices[i].get_name(),
                    app_theme.get_pixbuf(
                        "bluetooth/%s.png" % bluetooth_class_to_type(
                            devices[i].get_class())).get_pixbuf(), devices[i],
                    self.my_bluetooth.adapter, devices[i].get_paired(),
                    self.module_frame))
            i += 1

        self.device_iconview.add_items(items)

    def __refresh_notice_label(self):
        searching_str = _("Discovering device")

        if self.notice_label.get_text().count(".") == 3:
            self.notice_label.set_text(searching_str + ".")
        else:
            self.notice_label.set_text(self.notice_label.get_text() + ".")

        return True

    def __on_search(self, widget):
        if not self.is_searching:
            self.my_bluetooth.adapter.start_discovery()
            self.notice_label.set_text(_("Discovering device"))
            self.refresh_lable_timeout = gobject.timeout_add_seconds(
                1, self.__refresh_notice_label)
            self.my_bluetooth.adapter.connect("property-changed",
                                              self.on_adapter_property_changed)
            self.is_searching = True

    def on_adapter_property_changed(self, obj, key, value):
        if key == "Discovering" and value == False:
            gobject.source_remove(self.refresh_lable_timeout)
            if self.is_searching:
                self.my_bluetooth.adapter.stop_discovery()
            self.is_searching = False
            self.notice_label.set_text("")

    def __device_found(self, adapter, address, values):
        print "address", address
        if address not in adapter.get_address_records():
            device_path = adapter.create_device(address)
            if device_path == "None":
                return

            device = Device(device_path)
            items = []

            if not values.has_key("Name"):
                return

            print bluetooth_class_to_type(device.get_class())
            items.append(
                DeviceItem(
                    values['Name'],
                    app_theme.get_pixbuf(
                        "bluetooth/%s.png" % bluetooth_class_to_type(
                            device.get_class())).get_pixbuf(), device,
                    adapter))
            self.device_iconview.add_items(items)

    def __set_enable_open(self, is_open=True):
        self.enable_open_label.set_sensitive(is_open)
        self.display_device_label.set_sensitive(is_open)
        self.search_label.set_sensitive(is_open)
        self.display_align.set_sensitive(is_open)
        self.display_device_entry.set_sensitive(is_open)
        # self.device_iconview.set_sensitive(is_open)
        self.search_align.set_sensitive(is_open)
        self.search_timeout_label.set_child_visible(False)
        self.search_button.set_sensitive(is_open)
        # changed by hualet, to fix the bug that device icons stay while disabling the iconview widget
        if is_open:
            self.__get_devices()
        else:
            self.device_iconview.clear()

    def __toggled(self, widget, object):
        if self.my_bluetooth.adapter == None:
            return

        if object == "enable_open":
            self.__set_enable_open(widget.get_active())
            permanent_settings.set_powered(widget.get_active())
            self.my_bluetooth.adapter.set_powered(widget.get_active())
            return

        if object == "search":
            self.is_discoverable = widget.get_active()
            self.my_bluetooth.adapter.set_discoverable(self.is_discoverable)
            self.search_timeout_label.set_child_visible(self.is_discoverable)
            if self.is_discoverable:
                self.periodic_timer = PerodicTimer(self, 1)
            else:
                self.periodic_timer.stop()
            return

    def __expose(self, widget, event):
        cr = widget.window.cairo_create()
        rect = widget.allocation

        cr.set_source_rgb(*color_hex_to_cairo(MODULE_BG_COLOR))
        cr.rectangle(rect.x, rect.y, rect.width, rect.height)
        cr.fill()

    def __setup_label(self, text="", width=180, align=ALIGN_END):
        return Label(text, None, TITLE_FONT_SIZE, align, width, False, False,
                     False)

    def __setup_combo(self, items=[], width=HSCALEBAR_WIDTH):
        combo = ComboBox(items, None, 0, width, width)
        combo.set_size_request(width, WIDGET_HEIGHT)
        return combo

    def __setup_toggle(self):
        return ToggleButton(
            app_theme.get_pixbuf("toggle_button/inactive_normal.png"),
            app_theme.get_pixbuf("toggle_button/active_normal.png"),
            inactive_disable_dpixbuf=app_theme.get_pixbuf(
                "toggle_button/inactive_normal.png"),
            active_disable_dpixbuf=app_theme.get_pixbuf(
                "toggle_button/inactive_normal.png"))

    def __setup_align(self,
                      xalign=0,
                      yalign=0,
                      xscale=0,
                      yscale=0,
                      padding_top=BETWEEN_SPACING,
                      padding_bottom=0,
                      padding_left=TEXT_WINDOW_LEFT_PADDING,
                      padding_right=20):
        align = gtk.Alignment()
        align.set(xalign, yalign, xscale, yscale)
        align.set_padding(padding_top, padding_bottom, padding_left,
                          padding_right)
        return align

    def __widget_pack_start(self,
                            parent_widget,
                            widgets=[],
                            expand=False,
                            fill=False):
        if parent_widget == None:
            return
        for item in widgets:
            parent_widget.pack_start(item, expand, fill)
class SessionView(gtk.VBox):
    def __init__(self):
        gtk.VBox.__init__(self)
        self.open_dialog = False
        self.tmp_editing_session = None

        # UI style
        style.draw_background_color(self)
        self.tree = TreeView(
            [],
            enable_drag_drop=False,
            enable_hover=True,
            enable_multiple_select=False,
        )
        self.tree.set_expand_column(3)
        self.tree.set_column_titles(
            (_("Active"), _("Application"), _("Description"), _("Exec")), )

        self.tree.set_size_request(800, -1)
        self.tree.connect("right-press-items", self.right_press_item)

        self.tree.draw_mask = self.draw_mask
        self.tree.add_items(self.get_list())
        align = gtk.Alignment(0, 0, 0, 1)
        align.set_padding(15, 0, 20, 20)
        align.add(self.tree)
        align.connect("expose-event", self.expose_line)

        add_button = Button(_("New"))
        self.delete_button = Button(_("Delete"))
        add_button.connect("clicked", self.add_autostart)
        self.delete_button.connect("clicked", self.delete_autostart)
        self.delete_button.set_sensitive(False)

        foot_box = FootBox(adjustment=15)
        foot_box.set_buttons([add_button, self.delete_button])
        self.pack_start(align, True, True)

        self.pack_end(foot_box, False, False)
        #self.pack_end(self.new_box, False, False)

        self.show_all()

        self._init_monitor()

    def disable_delete_button(self, value):
        self.delete_button.set_sensitive(not value)

    def _init_monitor(self):
        self.library_monitor = LibraryMonitor(get_user_config_dir())
        self.library_monitor.set_property("monitored", True)
        self.library_monitor.connect("file-added", self.refresh_list)
        self.library_monitor.connect("location-removed", self.refresh_list)

    def right_press_item(self, widget, x_root, y_root, current_item,
                         select_items):
        self.tmp_editing_session = current_item.item
        for item in select_items:
            item.unselect()
        if current_item != None:
            current_item.select()
            if self.open_dialog == False:
                dialog = NewSessionDialog(confirm_callback=self.edit_done,
                                          cancel_callback=self.cancel_callback)
                dialog.name_entry.set_text(current_item.item.name)
                dialog.exec_entry.set_text(current_item.item.exec_)
                dialog.desc_entry.set_text(current_item.item.comment)
                dialog.place_center()
                dialog.show_all()
                self.open_dialog = True

    def create_session_item(self, dialog):
        name = dialog.name_entry.get_text()
        exec_ = dialog.exec_entry.get_text()
        comment = dialog.desc_entry.get_text()
        session_manager.add(name, exec_, comment)
        self.open_dialog = False

    def expose_line(self, widget, event):
        cr = widget.window.cairo_create()
        rect = widget.allocation
        style.draw_out_line(cr, rect, exclude=["left", "right", "top"])

    def sort_method(self):
        pass

    def add_autostart(self, widget):
        if self.open_dialog == False:
            dialog = NewSessionDialog(
                confirm_callback=self.create_session_item,
                cancel_callback=self.cancel_callback)
            dialog.show_all()
            self.open_dialog = True

    def delete_autostart(self, widget):
        items = map(lambda row: self.tree.visible_items[row],
                    self.tree.select_rows)
        item = items[0].item
        item.delete()
        self.tree.delete_select_items()
        if self.tree.visible_items == []:
            self.tree.add_items([NothingItem()])

    def edit_done(self, dialog):
        self.tmp_editing_session.set_name(dialog.name_entry.get_text())
        self.tmp_editing_session.set_exec(dialog.exec_entry.get_text())
        self.tmp_editing_session.set_comment(dialog.desc_entry.get_text())
        self.tmp_editing_session.save()
        self.tmp_editing_session = None
        items = map(lambda row: self.tree.visible_items[row],
                    self.tree.select_rows)
        self.tree.redraw_request(items, True)
        self.open_dialog = False

    def cancel_callback(self):
        self.tmp_editing_session = None
        self.open_dialog = False

    def pack(self, parent, widget_list, expand=False, fill=False):
        for w in widget_list:
            parent.pack_start(w, expand, fill)

    def draw_mask(self, cr, x, y, w, h):
        cr.set_source_rgb(1, 1, 1)
        cr.rectangle(x, y, w, h)
        cr.fill()

    def get_list(self):
        usr_list = session_manager.list_autostart_items()
        if usr_list:
            return map(lambda w: SessionItem(self, w), usr_list)
        else:
            return [NothingItem()]

    def refresh_list(self, widget, gfile):
        self.tree.clear()
        self.tree.add_items(self.get_list())
        self.tree.show()
class SessionView(gtk.VBox):

    def __init__(self):
        gtk.VBox.__init__(self)
        self.open_dialog = False
        self.tmp_editing_session = None
        
        # UI style
        style.draw_background_color(self)
        self.tree = TreeView([],enable_drag_drop=False,
                             enable_hover=True,
                             enable_multiple_select=False,
                             )
        self.tree.set_expand_column(3)
        self.tree.set_column_titles((_("Active"), _("Application"), _("Description"), _("Exec")),)

        self.tree.set_size_request(800, -1)
        self.tree.connect("right-press-items", self.right_press_item)

        self.tree.draw_mask = self.draw_mask
        self.tree.add_items(self.get_list())
        align = gtk.Alignment(0, 0, 0, 1)
        align.set_padding(15, 0, 20, 20)
        align.add(self.tree)
        align.connect("expose-event", self.expose_line)


        add_button = Button(_("New"))
        self.delete_button = Button(_("Delete"))
        add_button.connect("clicked", self.add_autostart)
        self.delete_button.connect("clicked", self.delete_autostart)
        self.delete_button.set_sensitive(False)
        
        foot_box = FootBox(adjustment=15)
        foot_box.set_buttons([add_button, self.delete_button])
        self.pack_start(align, True, True)
        
        self.pack_end(foot_box, False, False)
        #self.pack_end(self.new_box, False, False)

        self.show_all()

        self._init_monitor()

    def disable_delete_button(self, value):
        self.delete_button.set_sensitive(not value)
    def _init_monitor(self):
        self.library_monitor = LibraryMonitor(get_user_config_dir())
        self.library_monitor.set_property("monitored", True)
        self.library_monitor.connect("file-added", self.refresh_list)
        self.library_monitor.connect("location-removed", self.refresh_list)

    def right_press_item(self, widget,  x_root, y_root, current_item, select_items):
        self.tmp_editing_session = current_item.item
        for item in select_items:
            item.unselect()
        if current_item != None:
            current_item.select()
            if self.open_dialog == False:
                dialog = NewSessionDialog(confirm_callback = self.edit_done, cancel_callback = self.cancel_callback)
                dialog.name_entry.set_text(current_item.item.name)
                dialog.exec_entry.set_text(current_item.item.exec_)
                dialog.desc_entry.set_text(current_item.item.comment)
                dialog.place_center()
                dialog.show_all()
                self.open_dialog = True

    def create_session_item(self, dialog):
        name = dialog.name_entry.get_text()
        exec_ = dialog.exec_entry.get_text()
        comment = dialog.desc_entry.get_text()
        session_manager.add(name, exec_, comment)
        self.open_dialog = False

    def expose_line(self, widget, event):
        cr = widget.window.cairo_create()
        rect = widget.allocation
        style.draw_out_line(cr, rect, exclude=["left", "right", "top"])
    
    def sort_method(self):
        pass

    def add_autostart(self, widget):
        if self.open_dialog == False:
            dialog = NewSessionDialog(confirm_callback= self.create_session_item, cancel_callback = self.cancel_callback)
            dialog.show_all()
            self.open_dialog = True

    def delete_autostart(self, widget):
        items = map(lambda row: self.tree.visible_items[row], self.tree.select_rows)
        item = items[0].item
        item.delete()
        self.tree.delete_select_items()
        if self.tree.visible_items == []:
            self.tree.add_items([NothingItem()])

    def edit_done(self, dialog):
        self.tmp_editing_session.set_name(dialog.name_entry.get_text())
        self.tmp_editing_session.set_exec(dialog.exec_entry.get_text())
        self.tmp_editing_session.set_comment(dialog.desc_entry.get_text())
        self.tmp_editing_session.save()
        self.tmp_editing_session = None
        items = map(lambda row: self.tree.visible_items[row], self.tree.select_rows)
        self.tree.redraw_request(items, True)
        self.open_dialog = False

    def cancel_callback(self):
        self.tmp_editing_session = None
        self.open_dialog = False

    def pack(self, parent, widget_list, expand=False, fill=False):
        for w in widget_list:
            parent.pack_start(w, expand, fill)

    def draw_mask(self, cr, x, y, w, h):
        cr.set_source_rgb(1, 1, 1)
        cr.rectangle(x, y, w, h)
        cr.fill()

    def get_list(self):
        usr_list = session_manager.list_autostart_items()
        if usr_list:
            return map(lambda w: SessionItem(self, w), usr_list)
        else:
            return [NothingItem()]

    def refresh_list(self, widget, gfile):
        self.tree.clear()
        self.tree.add_items(self.get_list())
        self.tree.show()
class UpdateManager(dbus.service.Object):
    def __init__(self, session_bus):
        dbus.service.Object.__init__(self, session_bus, DSC_UPDATE_MANAGER_PATH)

        self.in_update_list = False
        self.in_upgrade_packages = False
        self.upgrade_pkg_infos = []

        self.application = Application()
        self.application.set_default_size(400, 250)
        self.application.add_titlebar(
                button_mask=['min', 'close'],
                app_name='Software Update Manager',
                )

        self.application.window.set_title("Software Update Manager")
        self.application.set_icon(get_common_image('update.png'))


        # Init page box.
        self.page_box = gtk.VBox()
        
        # 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)
        
        self.page_align.add(self.page_box)
        self.application.main_box.pack_start(self.page_align, True, True)
        
        # Init status bar.
        self.statusbar = Statusbar(28)
        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.background = BackgroundBox()
        self.background.draw_mask = self.draw_mask
        self.page_box.pack_start(self.background)

        self.upgrade_button = Button('更新软件')
        self.upgrade_button.set_sensitive(False)

        button_box = gtk.HBox()
        button_box.pack_start(self.upgrade_button, False, False)

        button_box_align = gtk.Alignment(0.5, 0.5, 0, 0)
        button_box_align.set_padding(3, 8, 4, 4)
        button_box_align.add(button_box)

        self.statusbar.status_item_box.pack_start(button_box_align)

        self.update_info_label = Label("初始化...")
        self.update_info_label_align = create_align((0.5, 0.5, 0, 0))
        self.update_info_label_align.add(self.update_info_label)
        
        self.upgrade_button.connect('clicked', self.upgrade_packages)


    def draw_mask(self, cr, x, y, w, h):
        sidebar_color = ui_theme.get_color("menu_select_font").get_color()
        draw_vlinear(cr, x, y, w, h,
                     [(0, (sidebar_color, 0.9)),
                      (1, (sidebar_color, 0.9)),]
                     )

    def start_dsc_backend(self):
        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)
        self.system_bus.add_signal_receiver(
                self.backend_signal_receiver, 
                signal_name="update_signal", 
                dbus_interface=DSC_SERVICE_NAME, 
                path=DSC_SERVICE_PATH)

    def backend_signal_receiver(self, messages):
        for message in messages:
            (signal_type, action_content) = message
            
            if signal_type == "update-list-update":
                self.in_update_list = True
                message_str = "正在检查更新,请稍等...(%s%%)" % int(float(action_content[0]))
                self.update_info_label.set_text(message_str)
                self.upgrade_button.set_sensitive(False)
            elif signal_type == 'update-list-finish':
                message_str = "正在检查更新,请稍等..."
                self.update_info_label.set_text(message_str)
                self.in_update_list = False

                self.bus_interface.request_upgrade_pkgs(
                        reply_handler=self.render_upgrade_info, 
                        error_handler=lambda e:handle_dbus_error("request_upgrade_pkgs", e))
            elif signal_type == 'update-list-failed':
                message_str = '检查更新失败!'
                self.update_info_label.set_text(message_str)

            elif signal_type == 'upgrade-commit-update':
                pkg_names, action_type, percent, status = action_content
                message_str = "[%s%%]%s" % (percent, status)
                self.update_info_label.set_text(message_str)
                self.upgrade_button.set_sensitive(False)
                self.in_upgrade_packages = True

            elif signal_type == 'upgrade-commit-finish':
                self.in_upgrade_packages = False
                message_str = '软件更新完成!'
                self.update_info_label.set_text(message_str)

    def upgrade_packages(self, widget):
        if self.in_update_list:
            print 'Check update, please wait...'
        elif self.in_upgrade_packages:
            print 'Upgrade packages, please wait...'
        else:
            self.upgrade_button.set_sensitive(False)
            self.in_upgrade_packages = True
            all_upgrade_pkgs = []
            for info in self.upgrade_pkg_infos:
                all_upgrade_pkgs.append(str(eval(info)[0]))
            self.bus_interface.upgrade_pkgs_with_new_policy(
                    all_upgrade_pkgs,
                    reply_handler=lambda :handle_dbus_reply("upgrade_pkgs_with_new_policy"), 
                    error_handler=lambda e:handle_dbus_error("upgrade_pkgs_with_new_policy", e),
                    )

    def render_upgrade_info(self, pkg_infos):
        self.upgrade_pkg_infos = pkg_infos
        if len(pkg_infos) > 0:
            msg_str = '您的系统有%s个更新!' % (len(pkg_infos),)
            self.upgrade_button.set_sensitive(True)
        else:
            msg_str = '您的系统已经最新状态了~'
            self.upgrade_button.set_sensitive(False)

        self.update_info_label.set_text(msg_str)
        self.application.window.show_all()

    def run(self):
        self.start_dsc_backend()
        container_remove_all(self.background)
        self.background.pack_start(self.update_info_label_align)

        gtk.timeout_add(1000, lambda: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)))

        self.application.run()
        self.bus_interface.request_quit(
                reply_handler=lambda :handle_dbus_reply("request_quit"), 
                error_handler=lambda e:handle_dbus_error("request_quit", e))

    def quit(self):
        gtk.main_quit()

    @dbus.service.method(DSC_UPDATE_MANAGER_NAME, in_signature="", out_signature="")    
    def hello(self):
        self.application.window.present()