Exemple #1
0
class Community_Information_GUI:
    """The Class can be used to create new peer/personal communities."""
    
    def __init__(self, title, gui):
        self.main_gui = gui
        self.personal_community = False

        self.icon_fname = None
        self.icon_changed = False
        self.notification = get_plugin_by_type(PLUGIN_TYPE_NOTIFICATION)
        self.community = get_plugin_by_type(PLUGIN_TYPE_COMMUNITY)

        self.main_window = gui.get_main_window()

        new_title = title + ' Community'
        self.dialog = gtk.Dialog(new_title, self.main_window, \
                                 gtk.DIALOG_DESTROY_WITH_PARENT | gtk.DIALOG_MODAL, \
                                 (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, \
                                  title, gtk.RESPONSE_OK))
        # this should be better for toplevel windows than set_size_request()
        self.dialog.set_default_size(400, 300)

        self.initialize_widgets()
        self.dialog.connect("response", self.response_handler)
        self.dialog.show_all()

        self.pic_dialog = Picture_Choose_Dialog(self.main_gui, self.got_picture)

    def response_handler(self, widget, event):
        if event == gtk.RESPONSE_OK:
            self.community_name = self.name_entry.get_text()
            if not valid_community(self.community_name):
                self.notification.ok_dialog('Error', 'Invalid community name.',
                    parent=self.dialog, modal=True)
                return True

            self.desc = self.description_tbuffer.get_text(self.description_tbuffer.get_start_iter(), self.description_tbuffer.get_end_iter())
            if self.desc == "<<Insert description>>":
                self.desc = None
                
            self.save_community_information(self.personal_community == False)

        self.pic_dialog.close()
        self.dialog.destroy()
        return True

    def save_community_information(self, peer=True):
        if peer:
            com = self.community.create_community(self.community_name, desc=self.desc)
            self.community.join_community(com)

            if self.icon_changed and not self.community.set_community_icon(com, self.icon_fname):
                self.notification.notify('Could not set community icon', True)
        else:
            # personal, public
            com = self.community.create_community(self.community_name, False, True, desc=self.desc)

            if self.icon_changed and not self.community.set_community_icon(com, self.icon_fname):
                self.notification.notify('Could not set community icon', True)
                
    def initialize_widgets(self):
        fwindow = new_scrollarea()
        viewport = gtk.Viewport()

        self.widgets_to_hide = []
        self.main_vbox = gtk.VBox()
        self.main_vbox.set_size_request(-1, 500)
        self.image_hbox = gtk.HBox()
        self.main_vbox.pack_start(self.image_hbox, False, False)

        # Community name, personal cbox, area size and image
        self.name_vbox = gtk.VBox()
        hbox = gtk.HBox()
        self.name_label = gtk.Label('Community\'s name:')
        hbox.pack_start(self.name_label, False, True)
        self.name_vbox.pack_start(hbox, False, True)
        self.name_entry = gtk.Entry()
        self.name_entry.set_size_request(-1, 32)
        self.name_vbox.pack_start(self.name_entry, False, True)

        hbox = gtk.HBox()
        self.personal_label = gtk.Label('Personal Community:')
        hbox.pack_start(self.personal_label, False, True)
        if self.community.personal_communities:
            self.name_vbox.pack_start(hbox, False, True)
        self.personal_cbutton = gtk.CheckButton()
        self.personal_cbutton.set_active(False)
        self.personal_cbutton.set_size_request(-1, 32)
        self.personal_cbutton.connect("clicked", self.checkbutton_clicked)
        hbox = gtk.HBox()
        hbox.pack_start(self.personal_cbutton, True, True)
        if self.community.personal_communities:
            self.name_vbox.pack_start(hbox, False, True)

        self.image_hbox.pack_start(self.name_vbox, False, True)
        self.image_eb = gtk.EventBox()
        self.image_eb.connect("button-press-event", self.image_clicked)
        self.image = gtk.Image()
        self.image.set_size_request(MAX_FACE_DIMENSION+10, MAX_FACE_DIMENSION+10)
        self.image.set_from_file(get_path(DEFAULT_COMMUNITY_ICON))
        self.image_eb.add(self.image)
        vbox = gtk.VBox()
        vbox.pack_start(self.image_eb, True, True)
        vbox.pack_start(gtk.Label('Click community\nicon to change it'), True, False)
        self.image_hbox.pack_end(vbox, False, False)
        
        hbox = gtk.HBox()
        desc_label = gtk.Label('Description: ')
        desc_label.set_size_request(-1, 32)
        hbox.pack_start(desc_label, False, True)
        self.main_vbox.pack_start(hbox, False, True)
        self.description_tview = gtk.TextView()
        self.description_tview.set_property("wrap-mode", gtk.WRAP_CHAR)
        self.description_tbuffer = gtk.TextBuffer()
        self.description_tbuffer.set_text("<<Insert description>>")
        self.description_tview.set_buffer(self.description_tbuffer)
        self.description_tview.set_size_request(300, 100)
        
        self.main_vbox.pack_start(self.description_tview, False, True)
        self.main_vbox.show_all()
        viewport.add(self.main_vbox)
        fwindow.add(viewport)
        self.dialog.vbox.pack_start(fwindow)

    def initialize_date_and_time_widgets(self):
        # community activation
        hbox = gtk.HBox()
        hbox.pack_start(gtk.Label('Activation Date & Time:'), False, True)
        self.widgets_to_hide.append(hbox)
        self.main_vbox.pack_start(hbox, False, True)

        hbox = gtk.HBox()
        self.start_date_entry = gtk.Entry()
        self.start_date_entry.set_size_request(-1, 32)
        self.start_date_button = gtk.Button("Date")
        self.start_date_button.set_size_request(80, -1)
        self.start_date_button.connect("clicked", self.sdate_clicked)
        hbox.pack_start(self.start_date_entry, False, True)
        hbox.pack_start(self.start_date_button, False, True)
        self.widgets_to_hide.append(hbox)
        self.main_vbox.pack_start(hbox, False, True)
        self.start_time_entry = gtk.Entry()
        self.start_time_entry.set_size_request(-1, 32)
        self.start_time_button = gtk.Button("Time")
        self.start_time_button.set_size_request(80, -1)
        self.start_time_button.connect("clicked", self.stime_clicked)
        hbox = gtk.HBox()
        hbox.pack_start(self.start_time_entry, False, True)
        hbox.pack_start(self.start_time_button, False, True)
        self.widgets_to_hide.append(hbox)
        self.main_vbox.pack_start(hbox, False, True)

        # community deactivation
        hbox = gtk.HBox()
        hbox.pack_start(gtk.Label('Deactivation Date & Time:'), False, True)
        self.widgets_to_hide.append(hbox)
        self.main_vbox.pack_start(hbox, False, True)

        hbox = gtk.HBox()
        self.end_date_entry = gtk.Entry()
        self.end_date_entry.set_size_request(-1, 32)
        self.end_date_button = gtk.Button("Date")
        self.end_date_button.set_size_request(80, -1)
        self.end_date_button.connect("clicked", self.sdate_clicked, False)
        hbox.pack_start(self.end_date_entry, False, True)
        hbox.pack_start(self.end_date_button, False, True)
        self.widgets_to_hide.append(hbox)
        self.main_vbox.pack_start(hbox, False, True)
        self.end_time_entry = gtk.Entry()
        self.end_time_entry.set_size_request(-1, 32)
        self.end_time_button = gtk.Button("Time")
        self.end_time_button.set_size_request(80, -1)
        self.end_time_button.connect("clicked", self.stime_clicked, False)
        hbox = gtk.HBox()
        hbox.pack_start(self.end_time_entry, False, True)
        hbox.pack_start(self.end_time_button, False, True)
        self.widgets_to_hide.append(hbox)
        self.main_vbox.pack_start(hbox, False, True)

    def checkbutton_clicked(self, widget):
        self.personal_community = self.personal_cbutton.get_active()

        if self.personal_community:
            for widget in self.widgets_to_hide:
                widget.hide_all()
        else:
            for widget in self.widgets_to_hide:
                widget.show_all()

    def image_clicked(self, widget, event=None):
        self.pic_dialog.set_picture(self.icon_fname)
        self.pic_dialog.show()

    def got_picture(self, fname):
        self.icon_fname = fname
        self.icon_changed = True
        if fname != None:
            self.image.set_from_file(fname)
        else:
            self.image.set_from_pixbuf(get_default_community_icon(self.com))

    def sdate_clicked(self, widget, start = True):
        fields = time.localtime()
        dialog = hildon.CalendarPopup(self.main_window, fields[0], fields[2], \
                                      fields[3])
        dialog.run()
        date = dialog.get_date()
        dialog.destroy()
        (year, month, day) = date
        if start:
            self.start_date_entry.set_text('%.4d-%.2d-%.2d' %(year, month, day))
        else:
            self.end_date_entry.set_text('%.4d-%.2d-%.2d' %(year, month, day))

    def stime_clicked(self, widget, start = True):
        time_picker = hildon.TimePicker(self.main_window)
        time_picker.run()
        selected_time = time_picker.get_time()
        time_picker.destroy()
        (hours, minutes) = selected_time

        str_hours = '%.2d' %(hours)
        str_mins = '%.2d' %(minutes)

        if start:
            self.start_time_entry.set_text('%s:%s' %(str_hours, str_mins))
        else:
            self.end_time_entry.set_text('%s:%s' %(str_hours, str_mins))