Exemplo n.º 1
0
class StockIconDialog(BaseDialog):
    def __init__(self, parent=None):
        BaseDialog.__init__(self, parent, title=_('Stock Icons'),
                            buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                                     gtk.STOCK_OK, gtk.RESPONSE_OK))
        self.set_size_request(260, 330)

        self._stockicons = ObjectList([Column('stock_id', use_stock=True),
                                       Column('name')])
        self._stockicons.set_headers_visible(False)
        self._stockicons.connect('row-activated',
                                 self._on_stockicons__row_activated)
        self._icons = {}
        for stock_label, stock_id in get_stock_icons():
            icon = Settable(stock_id=stock_id, name=stock_label)
            self._stockicons.append(icon)
            self._icons[stock_id] = icon

        self.vbox.pack_start(self._stockicons)
        self._stockicons.show()

    def select(self, value):
        icon = self._icons.get(value)
        if icon:
            self._stockicons.select(icon)

    def get_selected(self):
        icon = self._stockicons.get_selected()
        if icon:
            return icon.stock_id

    def _on_stockicons__row_activated(self, objectlist, icon):
        self.emit('response', gtk.RESPONSE_OK)
Exemplo n.º 2
0
    def _create_list(self, column_title):
        object_list = ObjectList([
            KanbanObjectListColumn('markup', title=column_title,
                                   data_type=str, use_markup=True,
                                   expand=True),
        ])
        object_list.connect('row-activated',
                            self._on_row_activated)
        object_list.connect('right-click',
                            self._on_right_click)
        sw = object_list.get_scrolled_window()
        sw.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
        sw.set_shadow_type(Gtk.ShadowType.NONE)

        treeview = object_list.get_treeview()
        treeview.set_name(column_title)
        treeview.connect(
            "button-press-event", self._on_button_press_event)
        treeview.set_rules_hint(False)

        column = object_list.get_column_by_name('markup')
        column.treeview_column.set_clickable(False)

        white = Gdk.color_parse('white')
        treeview.modify_base(Gtk.StateType.ACTIVE, white)
        treeview.modify_base(Gtk.StateType.SELECTED, white)

        object_list.set_cell_data_func(self._on_results__cell_data_func)
        return object_list
Exemplo n.º 3
0
    def _create_list(self, column_title):
        object_list = ObjectList([
            KanbanObjectListColumn('markup', title=column_title,
                                   data_type=str, use_markup=True,
                                   expand=True),
        ])
        object_list.connect('row-activated',
                            self._on_row_activated)
        object_list.connect('right-click',
                            self._on_right_click)
        sw = object_list.get_scrolled_window()
        sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        sw.set_shadow_type(gtk.SHADOW_NONE)

        treeview = object_list.get_treeview()
        treeview.set_name(column_title)
        treeview.connect(
            "button-press-event", self._on_button_press_event)
        treeview.set_rules_hint(False)

        column = object_list.get_column_by_name('markup')
        column.treeview_column.set_clickable(False)

        white = gtk.gdk.color_parse('white')
        treeview.modify_base(gtk.STATE_ACTIVE, white)
        treeview.modify_base(gtk.STATE_SELECTED, white)

        object_list.set_cell_data_func(self._on_results__cell_data_func)
        return object_list
Exemplo n.º 4
0
class ManView(PidaView):

    icon_name = 'gtk-library'
    label_text = 'Man'

    def create_ui(self):
        self._count = 0
        self.__vbox = gtk.VBox(spacing=3)
        self.__vbox.set_border_width(6)
        self.__hbox = gtk.HBox()
        self.__entry = gtk.Entry()
        self.__entry.connect('changed', self.cb_entry_changed)
        self.__check = gtk.CheckButton(label='-k')
        self.__check.connect('toggled', self.cb_entry_changed)
        self.__list = ObjectList([
                   Column('markup', title=_('Man page'), sorted=True,
                       use_markup=True),
                   Column('description', title=_('Description'),
                       use_markup=True),
               ])
        self.__list.connect('double-click', self._on_man_double_click)
        self.__list.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        self.__hbox.pack_start(self.__entry)
        self.__hbox.pack_start(self.__check, expand=False)
        self.__vbox.pack_start(self.__hbox, expand=False)
        self.__vbox.pack_start(self.__list)
        self.add_main_widget(self.__vbox)
        self.__vbox.show_all()

    def clear_items(self):
        self._count = 0
        self.__list.clear()

    def add_item(self, item):
        self._count += 1
        self.__list.append(item)

    def _on_man_double_click(self, olist, item):
        commandargs = ['/usr/bin/env', 'man', item.number, item.pattern]
        directory = os.path.dirname(commandargs[0])
        self.svc.boss.cmd('commander', 'execute',
                commandargs=commandargs,
                cwd=directory,
                icon='gnome-library',
                title='%(pattern)s(%(number)d)' % dict(
                    pattern=item.pattern,
                    number=int(item.number)
                ))

    def cb_entry_changed(self, w):
        options = '-f'
        if self.__check.get_active():
            options = '-k'
        self.svc.cmd_find(options=options, pattern=self.__entry.get_text())

    def can_be_closed(self):
        self.svc.get_action('show_man').set_active(False)
Exemplo n.º 5
0
 def create_objectlist(self, icon_name, text):
         l = ObjectList([Column('title', use_markup=True)])
         l.connect('row-activated', self._on_item_activated)
         l.connect('selection-changed', self._on_item_selected)
         l.set_headers_visible(False)
         l.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
         self._books.append_page(l,
                 tab_label=self.create_tab_label(icon_name, text))
         return l
Exemplo n.º 6
0
 def create_objectlist(self, icon_name, text):
     l = ObjectList([Column('title', use_markup=True)])
     l.connect('row-activated', self._on_item_activated)
     l.connect('selection-changed', self._on_item_selected)
     l.set_headers_visible(False)
     l.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
     self._books.append_page(l,
                             tab_label=self.create_tab_label(
                                 icon_name, text))
     return l
Exemplo n.º 7
0
	def __init__(self):
		listt = ObjectList(self.columns)
		listt.connect('selection-changed', self.selected)
		
		# selecting categories
		f = urllib.urlopen("http://pacnet.archlinux.pl/api/categories/").read()
		categories=json.loads(f)
		for category in categories:
			row = CategoryItem(category['fields']['name'])
			listt.append(row)

		SlaveView.__init__(self, listt)
Exemplo n.º 8
0
class PyflakeView(PidaView):
    
    icon_name = 'python-icon'
    label_text = _('Python Errors')

    def create_ui(self):
        self.errors_ol = ObjectList(
            Column('markup', use_markup=True)
        )
        self.errors_ol.set_headers_visible(False)
        self.errors_ol.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        self.add_main_widget(self.errors_ol)
        self.errors_ol.connect('double-click', self._on_errors_double_clicked)
        self.errors_ol.show_all()
        self.sort_combo = AttrSortCombo(
            self.errors_ol,
            [
                ('lineno', _('Line Number')),
                ('message_string', _('Message')),
                ('name', _('Type')),
            ],
            'lineno',
        )
        self.sort_combo.show()
        self.add_main_widget(self.sort_combo, expand=False)

    def clear_items(self):
        self.errors_ol.clear()

    def set_items(self, items):
        self.clear_items()
        for item in items:
            self.errors_ol.append(self.decorate_pyflake_message(item))

    def decorate_pyflake_message(self, msg):
        args = [('<b>%s</b>' % arg) for arg in msg.message_args]
        msg.message_string = msg.message % tuple(args)
        msg.name = msg.__class__.__name__
        msg.markup = ('<tt>%s </tt><i>%s</i>\n%s' % 
                      (msg.lineno, msg.name, msg.message_string))
        return msg

    def _on_errors_double_clicked(self, ol, item):
        self.svc.boss.editor.cmd('goto_line', line=item.lineno)

    def can_be_closed(self):
        self.svc.get_action('show_python_errors').set_active(False)
Exemplo n.º 9
0
class TestSignals(unittest.TestCase):
    def setUp(self):
        self.klist = ObjectList()
        self.klist.connect('has-rows', self._on_klist__has_rows)
        self.klist.connect('selection-changed',
                           self._on_klist__selection_changed)
        self.rows = None
        self.selected = None

    def _on_klist__has_rows(self, klist, rows):
        self.rows = rows

    def _on_klist__selection_changed(self, klist, selected):
        self.selected = selected

    def testHasRows(self):
        self.assertEqual(self.rows, None)
        self.assertEqual(len(self.klist), 0)

        # Add one
        self.klist.append(0)
        self.assertEqual(len(self.klist), 1)
        self.assertEqual(self.rows, True)
        self.klist.remove(0)
        self.assertEqual(self.rows, False)
        self.assertEqual(len(self.klist), 0)

        # Add several
        self.klist.extend((1, 2))
        self.assertEqual(len(self.klist), 2)
        self.assertEqual(self.rows, True)
        self.klist.remove(1)
        self.assertEqual(self.rows, True)
        self.klist.remove(2)
        self.assertEqual(self.rows, False)
        self.assertEqual(len(self.klist), 0)

    def testSelectionChanged(self):
        self.assertEqual(self.selected, None)
        self.assertEqual(len(self.klist), 0)
        self.klist.extend((0, 1))
        self.klist.select(0)
        self.assertEqual(self.selected, 0)
        self.klist.unselect_all()
        self.assertEqual(self.selected, None)
        self.assertRaises(ValueError, self.klist.select, 2)
Exemplo n.º 10
0
class TestSignals(unittest.TestCase):
    def setUp(self):
        self.klist = ObjectList()
        self.klist.connect('has-rows', self._on_klist__has_rows)
        self.klist.connect('selection-changed',
                           self._on_klist__selection_changed)
        self.rows = None
        self.selected = None

    def _on_klist__has_rows(self, klist, rows):
        self.rows = rows

    def _on_klist__selection_changed(self, klist, selected):
        self.selected = selected

    def testHasRows(self):
        self.assertEqual(self.rows, None)
        self.assertEqual(len(self.klist), 0)

        # Add one
        self.klist.append(0)
        self.assertEqual(len(self.klist), 1)
        self.assertEqual(self.rows, True)
        self.klist.remove(0)
        self.assertEqual(self.rows, False)
        self.assertEqual(len(self.klist), 0)

        # Add several
        self.klist.extend((1, 2))
        self.assertEqual(len(self.klist), 2)
        self.assertEqual(self.rows, True)
        self.klist.remove(1)
        self.assertEqual(self.rows, True)
        self.klist.remove(2)
        self.assertEqual(self.rows, False)
        self.assertEqual(len(self.klist), 0)

    def testSelectionChanged(self):
        self.assertEqual(self.selected, None)
        self.assertEqual(len(self.klist), 0)
        self.klist.extend((0, 1))
        self.klist.select(0)
        self.assertEqual(self.selected, 0)
        self.klist.unselect_all()
        self.assertEqual(self.selected, None)
        self.assertRaises(ValueError, self.klist.select, 2)
Exemplo n.º 11
0
class DeviceConstantsDialog(BasicDialog):
    size = (500, 300)

    def __init__(self, store, printer):
        self._constant_slave = None
        self.store = store
        self.printer = printer

        BasicDialog.__init__(self,
                             hide_footer=False,
                             title='edit',
                             size=self.size)
        self.main.set_border_width(6)

        self._create_ui()

    def _create_ui(self):
        hbox = Gtk.HBox()
        self.klist = ObjectList([Column('name')])
        self.klist.set_size_request(150, -1)
        self.klist.get_treeview().set_headers_visible(False)
        self.klist.connect('selection-changed',
                           self._on_klist__selection_changed)
        hbox.pack_start(self.klist, True, True, 0)
        hbox.show()

        for name, ctype in [(_(u'Units'), DeviceConstant.TYPE_UNIT),
                            (_(u'Tax'), DeviceConstant.TYPE_TAX),
                            (_(u'Payments'), DeviceConstant.TYPE_PAYMENT)]:
            self.klist.append(Settable(name=name, type=ctype))
        self.klist.show()

        self._constant_slave = _DeviceConstantsList(self.store, self.printer)
        self._constant_slave.switch(DeviceConstant.TYPE_UNIT)

        hbox.pack_start(self._constant_slave.get_toplevel(), True, True, 0)

        # FIXME: redesign BasicDialog
        self.main.remove(self.main_label)
        self.main.add(hbox)

        hbox.show_all()

    def _on_klist__selection_changed(self, klist, selected):
        self._constant_slave.switch(selected.type)
Exemplo n.º 12
0
class DeviceConstantsDialog(BasicDialog):
    size = (500, 300)

    def __init__(self, store, printer):
        self._constant_slave = None
        self.store = store
        self.printer = printer

        BasicDialog.__init__(self, hide_footer=False, title='edit',
                             size=self.size)
        self.main.set_border_width(6)

        self._create_ui()

    def _create_ui(self):
        hbox = gtk.HBox()
        self.klist = ObjectList([Column('name')])
        self.klist.set_size_request(150, -1)
        self.klist.get_treeview().set_headers_visible(False)
        self.klist.connect('selection-changed',
                           self._on_klist__selection_changed)
        hbox.pack_start(self.klist)
        hbox.show()

        for name, ctype in [(_(u'Units'), DeviceConstant.TYPE_UNIT),
                            (_(u'Tax'), DeviceConstant.TYPE_TAX),
                            (_(u'Payments'), DeviceConstant.TYPE_PAYMENT)]:
            self.klist.append(Settable(name=name, type=ctype))
        self.klist.show()

        self._constant_slave = _DeviceConstantsList(self.store, self.printer)
        self._constant_slave.switch(DeviceConstant.TYPE_UNIT)

        hbox.pack_start(self._constant_slave.get_toplevel())

        # FIXME: redesign BasicDialog
        self.main.remove(self.main_label)
        self.main.add(hbox)

        hbox.show_all()

    def _on_klist__selection_changed(self, klist, selected):
        self._constant_slave.switch(selected.type)
Exemplo n.º 13
0
Arquivo: news3.py Projeto: fuinha/kiwi
    def __init__(self):
        Delegate.__init__(self, gladefile="news_shell.ui",
                          delete_handler=self.quit_if_last)

        # paint header and footer; they are eventboxes that hold a
        # label and buttonbox respectively
        gadgets.set_background(self.header, "white")
        gadgets.set_background(self.footer, "#A0A0A0")
        gadgets.set_foreground(self.title, "blue")

        # Create the delegate and set it up
        objectlist = ObjectList(my_columns, news)
        objectlist.connect('selection-changed', self.news_selected)
        objectlist.connect('double-click', self.double_click)
        slave = SlaveDelegate(toplevel=objectlist)

        self.attach_slave("placeholder", slave)
        slave.focus_toplevel()  # Must be done after attach

        self.slave = slave
Exemplo n.º 14
0
    def __init__(self):
        Delegate.__init__(self,
                          gladefile="news_shell.ui",
                          delete_handler=self.quit_if_last)

        # paint header and footer; they are eventboxes that hold a
        # label and buttonbox respectively
        gadgets.set_background(self.header, "white")
        gadgets.set_background(self.footer, "#A0A0A0")
        gadgets.set_foreground(self.title, "blue")

        # Create the delegate and set it up
        objectlist = ObjectList(my_columns, news)
        objectlist.connect('selection-changed', self.news_selected)
        objectlist.connect('double-click', self.double_click)
        slave = SlaveDelegate(toplevel=objectlist)

        self.attach_slave("placeholder", slave)
        slave.focus_toplevel()  # Must be done after attach

        self.slave = slave
Exemplo n.º 15
0
class Package(SlaveView):
	
	columns = [
		Column("name", sorted=True),
		Column("version"),
		Column("installed"),
		Column("description")
	]
	
	def __init__(self):
		self.list = ObjectList(self.columns)
		self.list.connect('selection-changed', self.info)
		
		self.to_install = ""
			
		# selecting categories
		f = urllib.urlopen("http://pacnet.karbownicki.com/api/category/app-accessibility/").read()
		packages=json.loads(f)
		for category in packages:
			got = local.check(category['name'])
			row = PackageItem(category['name'], category['version'], got, category['description'])
			self.list.append(row)

		SlaveView.__init__(self, self.list)


	def info(self, the_list, item):
		command = ["pacman", "-Si", item.name]
		pkg_info, errors = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
		shell.get_widget("info").get_buffer().set_text(pkg_info)
		self.to_install = item.name
		
	def new_list(self, category):
		self.list.clear()
		f = urllib.urlopen("http://pacnet.karbownicki.com/api/category/%s/" % category).read()
		packages=json.loads(f)
		for category in packages:
			got = local.check(category['name'])
			row = PackageItem(category['name'], category['version'], got, category['description'])
			self.list.append(row)
Exemplo n.º 16
0
class TodoView(PidaView):

    label_text = _("TODO")
    icon_name = "accessories-text-editor"

    def create_ui(self):
        self.todo_list = ObjectList([Column("line", sorted=True), Column("todo"), Column("marker")])
        self.todo_list.connect("double-click", self._on_todo_double_click)
        self.add_main_widget(self.todo_list)
        self.todo_list.show_all()

    def clear_items(self):
        gcall(self.todo_list.clear)

    def add_item(self, todo, line, marker):
        self.todo_list.append(TodoItem(todo, line, marker))

    def _on_todo_double_click(self, olist, item):
        self.svc.boss.editor.cmd("goto_line", line=item.line)

    def can_be_closed(self):
        self.svc.get_action("show_todo").set_active(False)
Exemplo n.º 17
0
class PlayMediaWindow(Delegate):
    widgets = ["imgAddMediaFile"]

    def __init__(self, parent, recentPlayed=None):
        windowName = "PlayMediaWindow"

        Delegate.__init__(self,
                          gladefile=globals.gladeFile,
                          toplevel_name=windowName,
                          delete_handler=self.quit_if_last)

        # Create the delegate and set it up
        self.buildObjectList(recentPlayed)
        self.mediaList.grab_focus()
        slave = SlaveDelegate(toplevel=self.mediaList)

        self.attach_slave("labelX", slave)
        slave.focus_toplevel()  # Must be done after attach

        self.slave = slave

        self.set_transient_for(parent)

        # Set the image
        image = self.get_widget("imgAddMediaFile")
        image.set_from_file(
            os.path.join(globals.imageDir, "movie_track_add.png"))

    def buildObjectList(self, mediaList):
        """
        This method builds and initialize the ObjectList.
        """
        self.mediaList = ObjectList(media_list_columns)
        self.mediaList.connect('selection_changed', self.media_selected)
        self.mediaList.connect('double-click', self.double_click)

        for media in mediaList:
            mf = MediaFile(media.getFilename(),
                           uri=media.getURI(),
                           length=media.getLengthSec())
            mf.lastPlayed = media.getLastPlayed()
            self.mediaList.append(mf)

        # FIXME: Remove it. Only for testing
        #for i in [('test1.wmv', "2.34"),
        #         ('test2.wmv', "2.59"),
        #        ('test3.wmv', "2.59"),
        #        ('test4.wmv', "2.59")]:
        #  self.mediaList.append(MediaFile(i[0], i[1]))

    def media_selected(self, the_list, item):
        pass

    def double_click(self, the_list, selected_object):
        self.emit('result', selected_object.uri)
        self.hide_and_quit()

    def on_bCancel__clicked(self, *args):
        """
        This method is called when the user clicks the Cancel button. It closes
        the dialog.
        """
        self.hide_and_quit()

    def on_bQuit__clicked(self, *args):
        """
        This method is called when the user clicks the Quit button. It closes
        the dialog and quits Morphin.
        """
        self.emit('result', 'quit')
        self.hide_and_quit()

    def on_bPlayMedia__clicked(self, *args):
        """
        This method is called when the user clicks the Play Media button.
        """
        dialog = gtk.FileChooserDialog("Open Media...", None,
                                       gtk.FILE_CHOOSER_ACTION_OPEN,
                                       (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                                        gtk.STOCK_OPEN, gtk.RESPONSE_OK))

        dialog.set_default_response(gtk.RESPONSE_OK)

        filter = gtk.FileFilter()
        filter.set_name("Media files")
        for pattern in globals.compatibleFiles:
            filter.add_mime_type(pattern)
        dialog.add_filter(filter)

        filter = gtk.FileFilter()
        filter.set_name("All files")
        filter.add_pattern("*")
        dialog.add_filter(filter)

        response = dialog.run()
        if response == gtk.RESPONSE_OK:
            self.emit('result', dialog.get_filename())
            self.hide_and_quit()
        elif response == gtk.RESPONSE_CANCEL:
            pass

        dialog.destroy()

    def on_bPlayDisk__clicked(self, *args):
        """
        """
        pass

    def onKeyPressEvent(self, widget=None, event=None):
        """
        """
        pass
Exemplo n.º 18
0
class ListContainer(gtk.HBox):
    """A ListContainer is an L{ObjectList} with buttons to be able
    to modify the content of the list.
    Depending on the list_mode, @see L{set_list_mode} you will
    have add, remove and edit buttons.

    Signals
    =======
      - B{add-item} (returns item):
        - emitted when the add button is clicked, you're expected to
          return an object here
      - B{remove-item} (item, returns bool):
        - emitted when removing an item,
          you can block the removal from the list by returning False
      - B{edit-item} (item):
        - emitted when editing an item
          you can block the update afterwards by returning False

    @ivar add_button: add button
    @type add_button: L{gtk.Button}
    @ivar remove_button: remove button
    @type remove_button: L{gtk.Button}
    @ivar edit_button: edit button
    @type edit_button: L{gtk.Button}
    """

    gsignal('add-item', retval=object)
    gsignal('remove-item', object, retval=bool)
    gsignal('edit-item', object, retval=bool)
    gsignal('selection-changed', object)

    def __init__(self, columns, orientation=gtk.ORIENTATION_VERTICAL):
        """
        Create a new ListContainer object.
        @param columns: columns for the L{kiwi.ui.objectlist.ObjectList}
        @type columns: a list of L{kiwi.ui.objectlist.Columns}
        @param orientation: the position where the buttons will be
            placed: at the right (vertically) or at the bottom (horizontally)
            of the list. Defaults to the right of the list.
        @type: gtk.ORIENTATION_HORIZONTAL or gtk.ORIENTATION_VERTICAL
        """
        self._list_type = None

        gtk.HBox.__init__(self)

        self._orientation = orientation

        self._create_ui(columns)
        self.set_list_type(ListType.NORMAL)

    # Private API

    def _create_ui(self, columns):
        self.list = ObjectList(columns)
        self.list.connect('selection-changed',
                          self._on_list__selection_changed)
        self.list.connect('row-activated',
                          self._on_list__row_activated)

        self.add_button = gtk.Button(stock=gtk.STOCK_ADD)
        self.add_button.connect('clicked', self._on_add_button__clicked)

        self.remove_button = gtk.Button(stock=gtk.STOCK_REMOVE)
        self.remove_button.set_sensitive(False)
        self.remove_button.connect('clicked', self._on_remove_button__clicked)

        self.edit_button = gtk.Button(stock=gtk.STOCK_EDIT)
        self.edit_button.set_sensitive(False)
        self.edit_button.connect('clicked', self._on_edit_button__clicked)

        self._vbox = gtk.VBox(spacing=6)

        if self._orientation == gtk.ORIENTATION_VERTICAL:
            self.pack_start(self.list)
            self.list.show()
            self._add_buttons_to_box(self._vbox)
            self._pack_vbox()
        elif self._orientation == gtk.ORIENTATION_HORIZONTAL:
            self._vbox.pack_start(self.list)
            self.list.show()
            hbox = gtk.HBox(spacing=6)
            self._add_buttons_to_box(hbox)
            self._vbox.pack_start(hbox, expand=False)
            hbox.show()
            self._pack_vbox()
        else:
            raise TypeError(
                "buttons_orientation must be gtk.ORIENTATION_VERTICAL "
                " or gtk.ORIENTATION_HORIZONTAL")

    def _add_buttons_to_box(self, box):
        box.pack_start(self.add_button, expand=False)
        box.pack_start(self.remove_button, expand=False)
        box.pack_start(self.edit_button, expand=False)

    def _pack_vbox(self):
        self.pack_start(self._vbox, expand=False, padding=6)
        self._vbox.show()

    def _set_child_packing(self, padding):
        expand = self._orientation == gtk.ORIENTATION_HORIZONTAL

        self.set_child_packing(self._vbox, expand, True, padding,
                               gtk.PACK_START)

    def _add_item(self):
        retval = self.emit('add-item')
        if retval is None:
            return
        elif isinstance(retval, NotImplementedError):
            raise retval

        self.list.append(retval)

    def _remove_item(self, item):
        retval = self.emit('remove-item', item)
        if retval:
            self.list.remove(item)

    def _edit_item(self, item):
        retval = self.emit('edit-item', item)
        if retval:
            self.list.update(item)

    # Public API

    def add_item(self, item):
        """Appends an item to the list
        @param item: item to append
        """
        self.list.append(item)

    def add_items(self, items):
        """Appends a list of items to the list
        @param items: items to add
        @type items: a sequence of items
        """
        self.list.extend(items)

    def remove_item(self, item):
        """Removes an item from the list
        @param item: item to remove
        """
        self.list.remove(item)

    def update_item(self, item):
        """Updates an item in the list.
        You should call this if you change the object
        @param item: item to update
        """
        self.list.update(item)

    def default_remove(self, item):
        """Asks the user confirmation for removal of an item.
        @param item: a description of the item that will be removed
        @returns: True if the user confirm the removal, False otherwise
        """
        response = yesno(_('Do you want to remove %s ?') % (quote(str(item)),),
                         parent=None,
                         default=gtk.RESPONSE_OK,
                         buttons=((gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL),
                                  (gtk.STOCK_REMOVE, gtk.RESPONSE_OK)))
        return response == gtk.RESPONSE_OK

    def set_list_type(self, list_type):
        """Sets the kind of list type.
        @param list_type:
        """
        if not isinstance(list_type, ListType):
            raise TypeError("list_type must be a ListType enum")

        self.add_button.set_property(
            'visible',
            (list_type != ListType.READONLY and
             list_type != ListType.REMOVEONLY and
             list_type != ListType.UNADDABLE))
        self.remove_button.set_property(
            'visible',
            (list_type != ListType.READONLY and
             list_type != ListType.UNREMOVABLE))
        self.edit_button.set_property(
            'visible',
            (list_type != ListType.READONLY and
             list_type != ListType.UNEDITABLE and
             list_type != ListType.REMOVEONLY))
        if list_type in [ListType.READONLY, ListType.REMOVEONLY]:
            padding = 0
        else:
            padding = 6
        self._set_child_packing(padding)
        self._list_type = list_type

    def clear(self):
        """Removes all the items in the list"""
        self.list.clear()

    # Callbacks

    def _on_list__selection_changed(self, list, selection):
        object_selected = selection is not None
        self.remove_button.set_sensitive(object_selected)
        self.edit_button.set_sensitive(object_selected)
        self.emit('selection-changed', selection)

    def _on_list__row_activated(self, list, item):
        if (self._list_type != ListType.READONLY and
            self._list_type != ListType.UNEDITABLE):
            self._edit_item(item)

    def _on_add_button__clicked(self, button):
        self._add_item()

    def _on_remove_button__clicked(self, button):
        self._remove_item(self.list.get_selected())

    def _on_edit_button__clicked(self, button):
        self._edit_item(self.list.get_selected())
Exemplo n.º 19
0
class ListContainer(Gtk.Box):
    """A ListContainer is an :class:`ObjectList` with buttons to be able
    to modify the content of the list.
    Depending on the list_mode, @see :class:`set_list_mode` you will
    have add, remove and edit buttons.

    Signals
    =======
      - B{add-item} (returns item):
        - emitted when the add button is clicked, you're expected to
          return an object here
      - B{remove-item} (item, returns bool):
        - emitted when removing an item,
          you can block the removal from the list by returning False
      - B{edit-item} (item):
        - emitted when editing an item
          you can block the update afterwards by returning False

    :ivar add_button: add button
    :type add_button: :class:`Gtk.Button`
    :ivar remove_button: remove button
    :type remove_button: :class:`Gtk.Button`
    :ivar edit_button: edit button
    :type edit_button: :class:`Gtk.Button`
    """

    __gtype_name__ = 'ListContainer'
    gsignal('add-item', retval=object)
    gsignal('remove-item', object, retval=bool)
    gsignal('edit-item', object, retval=bool)
    gsignal('selection-changed', object)

    def __init__(self, columns, orientation=Gtk.Orientation.VERTICAL):
        """
        Create a new ListContainer object.
        :param columns: columns for the :class:`kiwi.ui.objectlist.ObjectList`
        :type columns: a list of :class:`kiwi.ui.objectlist.Columns`
        :param orientation: the position where the buttons will be
            placed: at the right (vertically) or at the bottom (horizontally)
            of the list. Defaults to the right of the list.
        :type: Gtk.Orientation.HORIZONTAL or Gtk.Orientation.VERTICAL
        """
        self._list_type = None

        super(ListContainer, self).__init__(orientation=Gtk.Orientation.HORIZONTAL)

        self._orientation = orientation

        self._create_ui(columns)
        self.set_list_type(ListType.NORMAL)

    # Private API

    def _create_ui(self, columns):
        self.list = ObjectList(columns)
        self.list.connect('selection-changed',
                          self._on_list__selection_changed)
        self.list.connect('row-activated',
                          self._on_list__row_activated)

        self.add_button = Gtk.Button(stock=Gtk.STOCK_ADD)
        self.add_button.connect('clicked', self._on_add_button__clicked)

        self.remove_button = Gtk.Button(stock=Gtk.STOCK_REMOVE)
        self.remove_button.set_sensitive(False)
        self.remove_button.connect('clicked', self._on_remove_button__clicked)

        self.edit_button = Gtk.Button(stock=Gtk.STOCK_EDIT)
        self.edit_button.set_sensitive(False)
        self.edit_button.connect('clicked', self._on_edit_button__clicked)

        self._vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)

        if self._orientation == Gtk.Orientation.VERTICAL:
            self.pack_start(self.list, True, True, 0)
            self.list.show()
            self._add_buttons_to_box(self._vbox)
            self._pack_vbox()
        elif self._orientation == Gtk.Orientation.HORIZONTAL:
            self._vbox.pack_start(self.list, True, True, 0)
            self.list.show()
            hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=6)
            self._add_buttons_to_box(hbox)
            self._vbox.pack_start(hbox, False, True, 0)
            hbox.show()
            self._pack_vbox()
        else:
            raise TypeError(
                "buttons_orientation must be Gtk.Orientation.VERTICAL "
                " or Gtk.Orientation.HORIZONTAL")

    def _add_buttons_to_box(self, box):
        box.pack_start(self.add_button, False, True, 0)
        box.pack_start(self.remove_button, False, True, 0)
        box.pack_start(self.edit_button, False, True, 0)

    def _pack_vbox(self):
        self.pack_start(self._vbox, False, True, 6)
        self._vbox.show()

    def _set_child_packing(self, padding):
        expand = self._orientation == Gtk.Orientation.HORIZONTAL

        self.set_child_packing(self._vbox, expand, True, padding,
                               Gtk.PackType.START)

    def _add_item(self):
        retval = self.emit('add-item')
        if retval is None:
            return
        elif isinstance(retval, NotImplementedError):
            raise retval

        self.list.append(retval)
        self.list.refresh()

    def _remove_item(self, item):
        retval = self.emit('remove-item', item)
        if retval:
            self.list.remove(item)

    def _edit_item(self, item):
        retval = self.emit('edit-item', item)
        if retval:
            self.list.update(item)

    # Public API

    def add_item(self, item):
        """Appends an item to the list
        :param item: item to append
        """
        self.list.append(item)

    def add_items(self, items):
        """Appends a list of items to the list
        :param items: items to add
        :type items: a sequence of items
        """
        self.list.extend(items)

    def remove_item(self, item):
        """Removes an item from the list
        :param item: item to remove
        """
        self.list.remove(item)

    def update_item(self, item):
        """Updates an item in the list.
        You should call this if you change the object
        :param item: item to update
        """
        self.list.update(item)

    def default_remove(self, item):
        """Asks the user confirmation for removal of an item.
        :param item: a description of the item that will be removed
        :returns: True if the user confirm the removal, False otherwise
        """
        response = yesno(_('Do you want to remove %s ?') %
                        (GLib.markup_escape_text(str(item)),),
                         parent=None,
                         default=Gtk.ResponseType.OK,
                         buttons=((Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL),
                                  (Gtk.STOCK_REMOVE, Gtk.ResponseType.OK)))
        return response == Gtk.ResponseType.OK

    def set_list_type(self, list_type):
        """Sets the kind of list type.
        :param list_type:
        """
        if not isinstance(list_type, ListType):
            raise TypeError("list_type must be a ListType enum")

        self.add_button.set_property(
            'visible',
            list_type not in [ListType.READONLY, ListType.REMOVEONLY,
                              ListType.UNADDABLE])
        self.remove_button.set_property(
            'visible',
            list_type not in [ListType.READONLY, ListType.ADDONLY,
                              ListType.UNREMOVABLE])
        self.edit_button.set_property(
            'visible',
            list_type not in [ListType.READONLY, ListType.ADDONLY,
                              ListType.UNEDITABLE, ListType.REMOVEONLY])
        if list_type in [ListType.READONLY, ListType.REMOVEONLY]:
            padding = 0
        else:
            padding = 6
        self._set_child_packing(padding)
        self._list_type = list_type

    def clear(self):
        """Removes all the items in the list"""
        self.list.clear()

    # Callbacks

    def _on_list__selection_changed(self, list, selection):
        object_selected = selection is not None
        self.remove_button.set_sensitive(object_selected)
        self.edit_button.set_sensitive(object_selected)
        self.emit('selection-changed', selection)

    def _on_list__row_activated(self, list, item):
        if self._list_type not in [ListType.READONLY, ListType.ADDONLY,
                                   ListType.UNEDITABLE]:
            self._edit_item(item)

    def _on_add_button__clicked(self, button):
        self._add_item()

    def _on_remove_button__clicked(self, button):
        self._remove_item(self.list.get_selected())

    def _on_edit_button__clicked(self, button):
        self._edit_item(self.list.get_selected())
Exemplo n.º 20
0
class PluginManagerDialog(BasicDialog):
    size = (500, 350)
    title = _(u'Plugin Manager')
    help_section = 'plugin'

    def __init__(self, store):
        header = _(u'Select the plugin you want to activate and click in '
                    'the apply button.')
        BasicDialog.__init__(self, hide_footer=False,
                             size=PluginManagerDialog.size,
                             title=PluginManagerDialog.title,
                             header_text=header)
        self.store = store
        self._manager = get_plugin_manager()
        self._setup_widgets()

    def _update_widgets(self):
        selected = self.klist.get_selected()
        assert selected

        self.ok_button.set_sensitive(selected.can_activate())

    def _setup_widgets(self):
        self.set_ok_label(_(u'Activate'), gtk.STOCK_APPLY)
        self.ok_button.set_sensitive(False)
        plugins = []

        for name in sorted(self._manager.available_plugins_names):
            # FIXME: Remove when magento plugin is functional for end users
            if not is_developer_mode() and name == 'magento':
                continue
            if platform.system() == 'Windows':
                if name in ['ecf', 'tef']:
                    continue

            desc = self._manager.get_description_by_name(name)
            plugins.append(_PluginModel(name, name in
                                        self._manager.installed_plugins_names,
                                        desc))

        self.klist = ObjectList(self._get_columns(), plugins,
                                gtk.SELECTION_BROWSE)
        self.klist.set_headers_visible(False)
        self.klist.connect("selection-changed",
                           self._on_klist__selection_changed)
        self.main.remove(self.main.get_child())
        self.main.add(self.klist)
        self.klist.show()

    def _get_columns(self):
        return [Column('is_active', title=_('Active'), width=20, data_type=bool),
                Column('icon', data_type=str, width=24, use_stock=True,
                       icon_size=gtk.ICON_SIZE_BUTTON),
                Column('description', data_type=str, expand=True,
                       use_markup=True)]

    def _enable_plugin(self, plugin_model):
        plugin_name = plugin_model.name
        # This should not really be necessary, but there may be deadlocks when
        # activating the plugin. See bug 5272
        default_store = get_default_store()
        default_store.commit()
        self._manager.install_plugin(plugin_name)
        self._manager.activate_plugin(plugin_name)

    #
    # BasicDialog
    #

    def confirm(self):
        msg = _("Are you sure you want activate this plugin?\n"
                "Please note that, once activated you will not "
                "be able to disable it.")
        response = yesno(msg, gtk.RESPONSE_NO,
                         _("Activate plugin"), _("Not now"))

        if response:
            self._enable_plugin(self.klist.get_selected())
            self.close()

    #
    # Callbacks
    #

    def _on_klist__selection_changed(self, list, data):
        self._update_widgets()
Exemplo n.º 21
0
class PluginManagerDialog(BasicDialog):
    size = (500, 350)
    title = _(u'Plugin Manager')
    help_section = 'plugin'

    def __init__(self, store):
        header = _(u'Select the plugin you want to activate and click in '
                   'the apply button.')
        BasicDialog.__init__(self, hide_footer=False,
                             size=PluginManagerDialog.size,
                             title=PluginManagerDialog.title,
                             header_text=header)
        self.store = store
        self._manager = get_plugin_manager()
        self._setup_widgets()

    def _update_widgets(self):
        selected = self.klist.get_selected()
        assert selected

        self.ok_button.set_sensitive(selected.can_activate())

    def _setup_widgets(self):
        self.set_ok_label(_(u'Activate'), Gtk.STOCK_APPLY)
        self.ok_button.set_sensitive(False)
        plugins = []

        for name in sorted(self._manager.available_plugins_names):
            desc = self._manager.get_description_by_name(name)
            plugins.append(_PluginModel(name, name in
                                        self._manager.installed_plugins_names,
                                        desc))

        self.klist = ObjectList(self._get_columns(), plugins,
                                Gtk.SelectionMode.BROWSE)
        self.klist.set_headers_visible(False)
        self.klist.connect("selection-changed",
                           self._on_klist__selection_changed)
        self.main.remove(self.main.get_child())
        self.main.add(self.klist)
        self.klist.show()

    def _get_columns(self):
        return [Column('is_active', title=_('Active'), width=20, data_type=bool),
                Column('icon', data_type=str, width=24, use_stock=True,
                       icon_size=Gtk.IconSize.BUTTON),
                Column('description', data_type=str, expand=True,
                       use_markup=True)]

    def _enable_plugin(self, plugin_model):
        plugin_name = plugin_model.name
        # This should not really be necessary, but there may be deadlocks when
        # activating the plugin. See bug 5272
        default_store = get_default_store()
        default_store.commit()
        with new_store() as store:
            self._manager.install_plugin(store, plugin_name)
        self._manager.activate_plugin(plugin_name)

        info(_("The plugin %s was successfully activated. Please, restart all "
               "Stoq instances connected to this installation.") % (plugin_name, ))

    #
    # BasicDialog
    #

    def confirm(self):
        msg = _("Are you sure you want activate this plugin?\n"
                "Please note that, once activated you will not "
                "be able to disable it.")
        response = yesno(msg, Gtk.ResponseType.NO,
                         _("Activate plugin"), _("Not now"))

        if response:
            self._enable_plugin(self.klist.get_selected())
            self.close()

    #
    # Callbacks
    #

    def _on_klist__selection_changed(self, list, data):
        self._update_widgets()
Exemplo n.º 22
0
class ChecklistView(PidaView):

    key = 'checklist.view'

    icon_name = 'gtk-todo'
    label_text = _('Check list')

    def create_ui(self):
        self._vbox = gtk.VBox(spacing=3)
        self._vbox.set_border_width(3)
        self.create_toolbar()
        self.create_newitem()
        self.create_list()
        self.add_main_widget(self._vbox)
        self._vbox.show_all()

    def create_tab_label(self, icon_name, text):
        if None in [icon_name, text]:
            return None
        label = gtk.Label(text)
        b_factory = gtk.HBox
        b = b_factory(spacing=2)
        icon = gtk.image_new_from_stock(icon_name, gtk.ICON_SIZE_MENU)
        b.pack_start(icon)
        b.pack_start(label)
        b.show_all()
        return b

    def create_list(self):
        self._list = ObjectList([
            Column('done', title=_('Done'), data_type=bool, editable=True),
            Column('title',
                   title=_('Title'),
                   data_type=str,
                   editable=True,
                   expand=True),
            Column('priority',
                   title=_('Priority'),
                   data_type=ChecklistStatus,
                   editable=True)
        ])
        self._list.connect('cell-edited', self._on_item_edit)
        self._list.connect('selection-changed', self._on_item_selected)
        self._list.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        self._vbox.add(self._list)

        self._sort_combo = AttrSortCombo(self._list, [
            ('done', _('Done')),
            ('title', _('Title')),
            ('priority', _('Priority')),
        ], 'title')

        self._vbox.pack_start(self._sort_combo, expand=False)
        self._list.show_all()
        self._sort_combo.show_all()

    def create_newitem(self):
        self._hbox = gtk.HBox(spacing=3)
        self._newitem_title = gtk.Entry()
        self._newitem_title.connect('changed', self._on_newitem_changed)
        self._newitem_ok = gtk.Button(stock=gtk.STOCK_ADD)
        self._newitem_ok.connect('clicked', self._on_item_add)
        self._newitem_ok.set_sensitive(False)
        self._hbox.pack_start(self._newitem_title, expand=True)
        self._hbox.pack_start(self._newitem_ok, expand=False)
        self._vbox.pack_start(self._hbox, expand=False)
        self._hbox.show_all()

    def create_toolbar(self):
        self._uim = gtk.UIManager()
        self._uim.insert_action_group(self.svc.get_action_group(), 0)
        uim_data = pkgutil.get_data(__name__, 'uidef/checklist-toolbar.xml')
        self._uim.add_ui_from_string(uim_data)
        self._uim.ensure_update()
        self._toolbar = self._uim.get_toplevels('toolbar')[0]
        self._toolbar.set_style(gtk.TOOLBAR_ICONS)
        self._toolbar.set_icon_size(gtk.ICON_SIZE_SMALL_TOOLBAR)
        self._vbox.pack_start(self._toolbar, expand=False)
        self.svc.get_action('checklist_del').set_sensitive(False)
        self._toolbar.show_all()

    def add_item(self, item):
        self._list.append(item, select=True)
        self.svc.save()

    def update_item(self, item):
        self._list.update(item)
        self.svc.save()

    def remove_item(self, item):
        self._list.remove(item)
        self.svc.save()

    def clear(self):
        self._list.clear()

    def _on_item_selected(self, olist, item):
        self.svc.get_action('checklist_del').set_sensitive(item is not None)
        self.svc.set_current(item)

    def _on_item_edit(self, olist, item, value):
        self.svc.save()

    def _on_item_add(self, w):
        title = self._newitem_title.get_text()
        self.svc.add_item(ChecklistItem(title=title))
        self._newitem_title.set_text('')

    def _on_newitem_changed(self, w):
        self._newitem_ok.set_sensitive(self._newitem_title.get_text() != '')

    def can_be_closed(self):
        self.svc.get_action('show_checklist').set_active(False)
Exemplo n.º 23
0
class PaymentMethodsDialog(BasicDialog):
    # TODO Bug 2406 will avoid duplicating code here
    size = (400, 400)
    title = _("Payment Method Settings")

    # TODO: implement editor for 'multiple' payment method.
    METHOD_EDITORS = {u'card': CardPaymentMethodEditor,
                      u'money': PaymentMethodEditor,
                      u'check': PaymentMethodEditor,
                      u'credit': PaymentMethodEditor,
                      u'bill': PaymentMethodEditor,
                      u'deposit': PaymentMethodEditor,
                      u'store_credit': PaymentMethodEditor}

    def __init__(self, store):
        BasicDialog.__init__(self,
                             hide_footer=True, size=PaymentMethodsDialog.size,
                             title=PaymentMethodsDialog.title)
        self._can_edit = False
        self.store = store
        self._setup_list()
        self._setup_slaves()

    def _setup_slaves(self):
        self._toolbar_slave = SearchEditorToolBar()
        self._toolbar_slave.connect("edit", self._on_edit_button__clicked)
        self._toolbar_slave.new_button.hide()
        self._toolbar_slave.edit_button.set_sensitive(False)
        self.attach_slave("extra_holder", self._toolbar_slave)

    def _setup_list(self):
        methods = PaymentMethod.get_editable_methods(self.store)
        self.klist = ObjectList(self._get_columns(), methods,
                                Gtk.SelectionMode.BROWSE)
        self.klist.connect("selection-changed",
                           self._on_klist__selection_changed)
        self.klist.connect("row-activated", self._on_klist__row_activated)
        self.klist.connect("cell-edited", self.on_cell_edited)
        self.main.remove(self.main.get_child())
        self.main.add(self.klist)
        self.klist.show()

    def _get_columns(self):
        return [Column('description', title=_('Payment Method'), data_type=str,
                       expand=True),
                Column('is_active', title=_('Active'), data_type=bool,
                       editable=True)]

    def _edit_item(self, item):
        editor = self.METHOD_EDITORS.get(item.method_name, None)

        if not editor:
            raise TypeError('Invalid payment method adapter: %s'
                            % item.method_name)

        store = api.new_store()
        item = store.fetch(item)
        retval = run_dialog(editor, self, store, item)
        store.confirm(retval)
        store.close()

    #
    # Callbacks
    #

    def on_cell_edited(self, klist, obj, attr):
        # All the payment methods could be (de)activate, except the 'money'
        # payment method.
        if obj.method_name != u'money':
            store = obj.store
            store.commit()
        else:
            obj.is_active = True

    def _on_klist__selection_changed(self, list, data):
        self._can_edit = (data and
                          data.method_name in self.METHOD_EDITORS.keys())
        self._toolbar_slave.edit_button.set_sensitive(self._can_edit)

    def _on_edit_button__clicked(self, toolbar_slave):
        assert self._can_edit
        self._edit_item(self.klist.get_selected())

    def _on_klist__row_activated(self, list, data):
        if not self._can_edit:
            return

        self._edit_item(data)
Exemplo n.º 24
0
class ChartDialog(gtk.Window):
    def __init__(self):
        self._js_data = None
        self._js_options = None
        self._current = None

        gtk.Window.__init__(self)
        self.set_size_request(800, 480)

        self.vbox = gtk.VBox()
        self.add(self.vbox)
        self.vbox.show()

        hbox = gtk.HBox()
        self.vbox.pack_start(hbox, False, False, 6)
        hbox.show()

        label = gtk.Label('Period:')
        hbox.pack_start(label, False, False, 6)
        label.show()

        self.chart_type = ProxyComboBox()
        self.chart_type.connect(
            'content-changed',
            self._on_chart_type__content_changed)
        hbox.pack_start(self.chart_type, False, False, 6)
        self.chart_type.show()

        self.period_values = ProxyComboBox()
        self.period_values.connect(
            'content-changed',
            self._on_period_values__content_changed)
        hbox.pack_start(self.period_values, False, False, 6)
        self.period_values.show()

        self._view = WebView()
        self._view.get_view().connect(
            'load-finished',
            self._on_view__document_load_finished)
        self.vbox.pack_start(self._view, True, True)

        self.results = ObjectList()
        self.results.connect(
            'row-activated',
            self._on_results__row_activated)
        self.vbox.pack_start(self.results, True, True)

        self._setup_daemon()

    @api.async
    def _setup_daemon(self):
        daemon = yield start_daemon()
        self._daemon_uri = daemon.base_uri

        proxy = daemon.get_client()
        yield proxy.callRemote('start_webservice')

        self.chart_type.prefill([
            ('Year', 'YearlyPayments'),
            ('Month', 'MonthlyPayments'),
            ('Day', 'DailyPayments'),
        ])

    @api.async
    def _invoke_chart(self, chart_type_name, **report_kwargs):
        def _get_chart_url(**kwargs):
            params = []
            for key, value in kwargs.items():
                params.append(key + '=' + str(value))
            return '%s/web/chart.json?%s' % (
                self._daemon_uri, '&'.join(params))

        url = _get_chart_url(type=chart_type_name, **report_kwargs)
        page = yield getPage(url)
        data = json.loads(page)
        api.asyncReturn(data)

    def _render_chart(self, chart_class, response):
        self._render_javascript(chart_class, response)
        self._render_objectlist(chart_class, response)

    def _render_javascript(self, chart_class, response):
        ticks = [item['short_title'] for item in response['items']]

        self._js_data = response['data']

        options = {}
        options['description'] = response['description']
        options['series'] = [dict(label=c['title']) for c in chart_class.columns][1:]
        options['xaxis_ticks'] = ticks
        self._js_options = options

        self._view.load_uri('%s/web/static/chart.html' % (
                            self._daemon_uri,))

    def _render_objectlist(self, chart_class, response):
        columns = []
        for kwargs in chart_class.columns:
            kwargs = kwargs.copy()
            name = kwargs.pop('name')
            columns.append(Column(name, **kwargs))
        self.results.set_columns(columns)

        items = []
        for item in response['items']:
            settable = Settable(**item)
            settable.chart_class = chart_class
            items.append(settable)
        self.results.add_list(items, clear=True)
        self.results.show()

    def _load_finished(self):
        self._view.js_function_call(
            "plot", self._js_data, self._js_options)

    @api.async
    def _show_one(self, chart_type_name, start, end):
        chart_class = get_chart_class(chart_type_name)
        report_kwargs = dict(start=start.strftime('%Y-%m-%d'),
                             end=end.strftime('%Y-%m-%d'))

        # Get chart datab
        response = yield self._invoke_chart(chart_type_name, **report_kwargs)
        self._render_chart(chart_class, response)

    def _update_period_values(self):
        chart_type_name = self.chart_type.get_selected()
        chart_class = get_chart_class(chart_type_name)
        values = chart_class.get_combo_labels()
        self.period_values.prefill(values)

    #
    # Callbacks
    #

    def _on_view__document_load_finished(self, view, frame):
        self._load_finished()

    def _on_chart_type__content_changed(self, combo):
        self._update_period_values()

    def _on_period_values__content_changed(self, combo):
        kind = self.chart_type.get_selected()
        value = self.period_values.get_selected()
        if not value:
            return
        start, end = value
        if self._current == (kind, start, end):
            return
        self._show_one(kind, start, end)
        self._current = kind, start, end

    def _on_results__row_activated(self, results, item):
        chart_type_name = item.chart_class.__name__
        if chart_type_name == 'YearlyPayments':
            start = localdate(item.year, 1, 1).date()
            end = localdate(item.year, 12, 31).date()
            chart_type_name = 'MonthlyPayments'
        elif chart_type_name == 'MonthlyPayments':
            start = localdate(item.year, item.month, 1).date()
            end = start + relativedelta(days=31)
            chart_type_name = 'DailyPayments'
        else:
            return
        self._show_one(chart_type_name, start, end)
Exemplo n.º 25
0
class ListContainer(Gtk.Box):
    """A ListContainer is an :class:`ObjectList` with buttons to be able
    to modify the content of the list.
    Depending on the list_mode, @see :class:`set_list_mode` you will
    have add, remove and edit buttons.

    Signals
    =======
      - B{add-item} (returns item):
        - emitted when the add button is clicked, you're expected to
          return an object here
      - B{remove-item} (item, returns bool):
        - emitted when removing an item,
          you can block the removal from the list by returning False
      - B{edit-item} (item):
        - emitted when editing an item
          you can block the update afterwards by returning False

    :ivar add_button: add button
    :type add_button: :class:`Gtk.Button`
    :ivar remove_button: remove button
    :type remove_button: :class:`Gtk.Button`
    :ivar edit_button: edit button
    :type edit_button: :class:`Gtk.Button`
    """

    __gtype_name__ = 'ListContainer'
    gsignal('add-item', retval=object)
    gsignal('remove-item', object, retval=bool)
    gsignal('edit-item', object, retval=bool)
    gsignal('selection-changed', object)

    def __init__(self, columns, orientation=Gtk.Orientation.VERTICAL):
        """
        Create a new ListContainer object.
        :param columns: columns for the :class:`kiwi.ui.objectlist.ObjectList`
        :type columns: a list of :class:`kiwi.ui.objectlist.Columns`
        :param orientation: the position where the buttons will be
            placed: at the right (vertically) or at the bottom (horizontally)
            of the list. Defaults to the right of the list.
        :type: Gtk.Orientation.HORIZONTAL or Gtk.Orientation.VERTICAL
        """
        self._list_type = None

        super(ListContainer,
              self).__init__(orientation=Gtk.Orientation.HORIZONTAL)

        self._orientation = orientation

        self._create_ui(columns)
        self.set_list_type(ListType.NORMAL)

    # Private API

    def _create_ui(self, columns):
        self.list = ObjectList(columns)
        self.list.connect('selection-changed',
                          self._on_list__selection_changed)
        self.list.connect('row-activated', self._on_list__row_activated)

        self.add_button = Gtk.Button(stock=Gtk.STOCK_ADD)
        self.add_button.connect('clicked', self._on_add_button__clicked)

        self.remove_button = Gtk.Button(stock=Gtk.STOCK_REMOVE)
        self.remove_button.set_sensitive(False)
        self.remove_button.connect('clicked', self._on_remove_button__clicked)

        self.edit_button = Gtk.Button(stock=Gtk.STOCK_EDIT)
        self.edit_button.set_sensitive(False)
        self.edit_button.connect('clicked', self._on_edit_button__clicked)

        self._vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)

        if self._orientation == Gtk.Orientation.VERTICAL:
            self.pack_start(self.list, True, True, 0)
            self.list.show()
            self._add_buttons_to_box(self._vbox)
            self._pack_vbox()
        elif self._orientation == Gtk.Orientation.HORIZONTAL:
            self._vbox.pack_start(self.list, True, True, 0)
            self.list.show()
            hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=6)
            self._add_buttons_to_box(hbox)
            self._vbox.pack_start(hbox, False, True, 0)
            hbox.show()
            self._pack_vbox()
        else:
            raise TypeError(
                "buttons_orientation must be Gtk.Orientation.VERTICAL "
                " or Gtk.Orientation.HORIZONTAL")

    def _add_buttons_to_box(self, box):
        box.pack_start(self.add_button, False, True, 0)
        box.pack_start(self.remove_button, False, True, 0)
        box.pack_start(self.edit_button, False, True, 0)

    def _pack_vbox(self):
        self.pack_start(self._vbox, False, True, 6)
        self._vbox.show()

    def _set_child_packing(self, padding):
        expand = self._orientation == Gtk.Orientation.HORIZONTAL

        self.set_child_packing(self._vbox, expand, True, padding,
                               Gtk.PackType.START)

    def _add_item(self):
        retval = self.emit('add-item')
        if retval is None:
            return
        elif isinstance(retval, NotImplementedError):
            raise retval

        self.list.append(retval)
        self.list.refresh()

    def _remove_item(self, item):
        retval = self.emit('remove-item', item)
        if retval:
            self.list.remove(item)

    def _edit_item(self, item):
        retval = self.emit('edit-item', item)
        if retval:
            self.list.update(item)

    # Public API

    def add_item(self, item):
        """Appends an item to the list
        :param item: item to append
        """
        self.list.append(item)

    def add_items(self, items):
        """Appends a list of items to the list
        :param items: items to add
        :type items: a sequence of items
        """
        self.list.extend(items)

    def remove_item(self, item):
        """Removes an item from the list
        :param item: item to remove
        """
        self.list.remove(item)

    def update_item(self, item):
        """Updates an item in the list.
        You should call this if you change the object
        :param item: item to update
        """
        self.list.update(item)

    def default_remove(self, item):
        """Asks the user confirmation for removal of an item.
        :param item: a description of the item that will be removed
        :returns: True if the user confirm the removal, False otherwise
        """
        response = yesno(_('Do you want to remove %s ?') %
                         (GLib.markup_escape_text(str(item)), ),
                         parent=None,
                         default=Gtk.ResponseType.OK,
                         buttons=((Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL),
                                  (Gtk.STOCK_REMOVE, Gtk.ResponseType.OK)))
        return response == Gtk.ResponseType.OK

    def set_list_type(self, list_type):
        """Sets the kind of list type.
        :param list_type:
        """
        if not isinstance(list_type, ListType):
            raise TypeError("list_type must be a ListType enum")

        self.add_button.set_property(
            'visible', list_type not in [
                ListType.READONLY, ListType.REMOVEONLY, ListType.UNADDABLE
            ])
        self.remove_button.set_property(
            'visible', list_type
            not in [ListType.READONLY, ListType.ADDONLY, ListType.UNREMOVABLE])
        self.edit_button.set_property(
            'visible', list_type not in [
                ListType.READONLY, ListType.ADDONLY, ListType.UNEDITABLE,
                ListType.REMOVEONLY
            ])
        if list_type in [ListType.READONLY, ListType.REMOVEONLY]:
            padding = 0
        else:
            padding = 6
        self._set_child_packing(padding)
        self._list_type = list_type

    def clear(self):
        """Removes all the items in the list"""
        self.list.clear()

    # Callbacks

    def _on_list__selection_changed(self, list, selection):
        object_selected = selection is not None
        self.remove_button.set_sensitive(object_selected)
        self.edit_button.set_sensitive(object_selected)
        self.emit('selection-changed', selection)

    def _on_list__row_activated(self, list, item):
        if self._list_type not in [
                ListType.READONLY, ListType.ADDONLY, ListType.UNEDITABLE
        ]:
            self._edit_item(item)

    def _on_add_button__clicked(self, button):
        self._add_item()

    def _on_remove_button__clicked(self, button):
        self._remove_item(self.list.get_selected())

    def _on_edit_button__clicked(self, button):
        self._edit_item(self.list.get_selected())
Exemplo n.º 26
0
class DetailsTab(gtk.VBox):
    details_dialog_class = None

    def __init__(self, model, parent):
        super(DetailsTab, self).__init__()

        self.model = model
        self._parent = parent

        self.set_spacing(6)
        self.set_border_width(6)

        self.klist = ObjectList(self.get_columns())
        self.klist.add_list(self.populate())

        self.pack_start(self.klist)
        self.klist.show()

        if len(self.klist) and self.get_details_dialog_class():
            self.button_box = gtk.HButtonBox()
            self.button_box.set_layout(gtk.BUTTONBOX_START)

            details_button = gtk.Button(self.details_lbl)
            self.button_box.pack_start(details_button)
            details_button.set_sensitive(bool(self.klist.get_selected()))
            details_button.show()

            self.pack_end(self.button_box, False, False)
            self.button_box.show()

            self.button_box.details_button = details_button
            details_button.connect('clicked', self._on_details_button__clicked)

            self.klist.connect('row-activated', self._on_klist__row_activated)
            self.klist.connect('selection-changed',
                               self._on_klist__selection_changed)

        self.setup_widgets()

    def refresh(self):
        """Refreshes the list of respective tab."""
        self.klist.clear()
        self.klist.add_list(self.populate())

    def get_columns(self):
        """Returns a list of columns this tab should show."""
        raise NotImplementedError

    def show_details(self):
        """Called when the details button is clicked. Displays the details of
        the selected object in the list."""
        model = self.get_details_model(self.klist.get_selected())
        run_dialog(self.get_details_dialog_class(),
                   parent=self._parent,
                   store=self._parent.store,
                   model=model,
                   visual_mode=True)

    def get_label(self):
        """Returns the name of the tab."""
        label = gtk.Label(self.labels[1])
        return label

    def get_details_model(self, model):
        """Subclassses can overwrite this method if the details dialog class
        needs a model different than the one on the list."""
        return model

    def get_details_dialog_class(self):
        """Subclasses must return the dialog that should be displayed for more
        information about the item on the list"""
        return self.details_dialog_class

    def setup_widgets(self):
        """Override this if tab needs to do some custom widget setup."""

    #
    # Callbacks
    #

    def _on_details_button__clicked(self, button):
        self.show_details()

    def _on_klist__row_activated(self, klist, item):
        self.show_details()

    def _on_klist__selection_changed(self, klist, data):
        self.button_box.details_button.set_sensitive(bool(data))
Exemplo n.º 27
0
class DetailsTab(gtk.VBox):
    details_dialog_class = None

    def __init__(self, model, parent):
        super(DetailsTab, self).__init__()

        self.model = model
        self._parent = parent

        self.set_spacing(6)
        self.set_border_width(6)

        self.klist = ObjectList(self.get_columns())
        self.klist.add_list(self.populate())

        self.pack_start(self.klist)
        self.klist.show()

        if len(self.klist) and self.get_details_dialog_class():
            self.button_box = gtk.HButtonBox()
            self.button_box.set_layout(gtk.BUTTONBOX_START)

            details_button = gtk.Button(self.details_lbl)
            self.button_box.pack_start(details_button)
            details_button.set_sensitive(bool(self.klist.get_selected()))
            details_button.show()

            self.pack_end(self.button_box, False, False)
            self.button_box.show()

            self.button_box.details_button = details_button
            details_button.connect('clicked', self._on_details_button__clicked)

            self.klist.connect('row-activated', self._on_klist__row_activated)
            self.klist.connect('selection-changed',
                               self._on_klist__selection_changed)

        self.setup_widgets()

    def refresh(self):
        """Refreshes the list of respective tab."""
        self.klist.clear()
        self.klist.add_list(self.populate())

    def get_columns(self):
        """Returns a list of columns this tab should show."""
        raise NotImplementedError

    def show_details(self):
        """Called when the details button is clicked. Displays the details of
        the selected object in the list."""
        model = self.get_details_model(self.klist.get_selected())
        run_dialog(self.get_details_dialog_class(),
                   parent=self._parent,
                   store=self._parent.store,
                   model=model,
                   visual_mode=True)

    def get_label(self):
        """Returns the name of the tab."""
        label = gtk.Label(self.labels[1])
        return label

    def get_details_model(self, model):
        """Subclassses can overwrite this method if the details dialog class
        needs a model different than the one on the list."""
        return model

    def get_details_dialog_class(self):
        """Subclasses must return the dialog that should be displayed for more
        information about the item on the list"""
        return self.details_dialog_class

    def setup_widgets(self):
        """Override this if tab needs to do some custom widget setup."""

    #
    # Callbacks
    #

    def _on_details_button__clicked(self, button):
        self.show_details()

    def _on_klist__row_activated(self, klist, item):
        self.show_details()

    def _on_klist__selection_changed(self, klist, data):
        self.button_box.details_button.set_sensitive(bool(data))
Exemplo n.º 28
0
class PluginManagerDialog(BasicDialog):
    size = (500, 350)
    title = _(u'Plugin Manager')
    help_section = 'plugin'

    def __init__(self, store):
        header = _(u'Select the plugin you want to activate and click in '
                   'the apply button.')
        BasicDialog.__init__(self,
                             hide_footer=False,
                             size=PluginManagerDialog.size,
                             title=PluginManagerDialog.title,
                             header_text=header)
        self.store = store
        self._manager = get_plugin_manager()
        self._setup_widgets()

    def _update_widgets(self):
        selected = self.klist.get_selected()
        assert selected

        self.ok_button.set_sensitive(selected.can_activate())

    def _setup_widgets(self):
        self.set_ok_label(_(u'Activate'), gtk.STOCK_APPLY)
        self.ok_button.set_sensitive(False)
        plugins = []

        for name in sorted(self._manager.available_plugins_names):
            # FIXME: Remove when magento plugin is functional for end users
            if not is_developer_mode() and name == 'magento':
                continue
            if platform.system() == 'Windows':
                if name in ['ecf', 'tef']:
                    continue

            desc = self._manager.get_description_by_name(name)
            plugins.append(
                _PluginModel(name, name
                             in self._manager.installed_plugins_names, desc))

        self.klist = ObjectList(self._get_columns(), plugins,
                                gtk.SELECTION_BROWSE)
        self.klist.set_headers_visible(False)
        self.klist.connect("selection-changed",
                           self._on_klist__selection_changed)
        self.main.remove(self.main.get_child())
        self.main.add(self.klist)
        self.klist.show()

    def _get_columns(self):
        return [
            Column('is_active', title=_('Active'), width=20, data_type=bool),
            Column('icon',
                   data_type=str,
                   width=24,
                   use_stock=True,
                   icon_size=gtk.ICON_SIZE_BUTTON),
            Column('description', data_type=str, expand=True, use_markup=True)
        ]

    def _enable_plugin(self, plugin_model):
        plugin_name = plugin_model.name
        # This should not really be necessary, but there may be deadlocks when
        # activating the plugin. See bug 5272
        default_store = get_default_store()
        default_store.commit()
        self._manager.install_plugin(plugin_name)
        self._manager.activate_plugin(plugin_name)

    #
    # BasicDialog
    #

    def confirm(self):
        msg = _("Are you sure you want activate this plugin?\n"
                "Please note that, once activated you will not "
                "be able to disable it.")
        response = yesno(msg, gtk.RESPONSE_NO, _("Activate plugin"),
                         _("Not now"))

        if response:
            self._enable_plugin(self.klist.get_selected())
            self.close()

    #
    # Callbacks
    #

    def _on_klist__selection_changed(self, list, data):
        self._update_widgets()
Exemplo n.º 29
0
class ChecklistView(PidaView):
    
    key = 'checklist.view'

    icon_name = 'gtk-todo'
    label_text = _('Check list')

    def create_ui(self):
        self._vbox = gtk.VBox(spacing=3)
        self._vbox.set_border_width(3)
        self.create_toolbar()
        self.create_newitem()
        self.create_list()
        self.add_main_widget(self._vbox)
        self._vbox.show_all()

    def create_tab_label(self, icon_name, text):
        if None in [icon_name, text]:
            return None
        label = gtk.Label(text)
        b_factory = gtk.HBox
        b = b_factory(spacing=2)
        icon = gtk.image_new_from_stock(icon_name, gtk.ICON_SIZE_MENU)
        b.pack_start(icon)
        b.pack_start(label)
        b.show_all()
        return b

    def create_list(self):
        self._list = ObjectList([
                Column('done', title=_('Done'), data_type=bool, editable=True),
                Column('title', title=_('Title'), data_type=str,
                    editable=True, expand=True),
                Column('priority', title=_('Priority'),
                    data_type=ChecklistStatus, editable=True)
                ])
        self._list.connect('cell-edited', self._on_item_edit)
        self._list.connect('selection-changed', self._on_item_selected)
        self._list.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        self._vbox.add(self._list)

        self._sort_combo = AttrSortCombo(self._list,
            [
                ('done', _('Done')),
                ('title', _('Title')),
                ('priority', _('Priority')),
            ],
            'title')

        self._vbox.pack_start(self._sort_combo, expand=False)
        self._list.show_all()
        self._sort_combo.show_all()

    def create_newitem(self):
        self._hbox = gtk.HBox(spacing=3)
        self._newitem_title = gtk.Entry()
        self._newitem_title.connect('changed', self._on_newitem_changed)
        self._newitem_ok = gtk.Button(stock=gtk.STOCK_ADD)
        self._newitem_ok.connect('clicked', self._on_item_add)
        self._newitem_ok.set_sensitive(False)
        self._hbox.pack_start(self._newitem_title, expand=True)
        self._hbox.pack_start(self._newitem_ok, expand=False)
        self._vbox.pack_start(self._hbox, expand=False)
        self._hbox.show_all()

    def create_toolbar(self):
        self._uim = gtk.UIManager()
        self._uim.insert_action_group(self.svc.get_action_group(), 0)
        uim_data = pkgutil.get_data(__name__, 'uidef/checklist-toolbar.xml')
        self._uim.add_ui_from_string(uim_data)
        self._uim.ensure_update()
        self._toolbar = self._uim.get_toplevels('toolbar')[0]
        self._toolbar.set_style(gtk.TOOLBAR_ICONS)
        self._toolbar.set_icon_size(gtk.ICON_SIZE_SMALL_TOOLBAR)
        self._vbox.pack_start(self._toolbar, expand=False)
        self.svc.get_action('checklist_del').set_sensitive(False)
        self._toolbar.show_all()

    def add_item(self, item):
        self._list.append(item, select=True)
        self.svc.save()

    def update_item(self, item):
        self._list.update(item)
        self.svc.save()

    def remove_item(self, item):
        self._list.remove(item)
        self.svc.save()

    def clear(self):
        self._list.clear()

    def _on_item_selected(self, olist, item):
        self.svc.get_action('checklist_del').set_sensitive(item is not None)
        self.svc.set_current(item)

    def _on_item_edit(self, olist, item, value):
        self.svc.save()

    def _on_item_add(self, w):
        title = self._newitem_title.get_text()
        self.svc.add_item(ChecklistItem(title=title))
        self._newitem_title.set_text('')

    def _on_newitem_changed(self, w):
        self._newitem_ok.set_sensitive(self._newitem_title.get_text() != '')

    def can_be_closed(self):
        self.svc.get_action('show_checklist').set_active(False)
Exemplo n.º 30
0
class ShowLastPackets(InformationWindow):
    ## @var win
    #  window
    ## @var visualizer
    #  visualizer
    ## @var viz_node
    #  visualizer node
    ## @var node
    #  the node
    ## @var tx_list
    #  packet transmit list
    ## @var rx_list
    #  packet receive list
    ## @var drop_list
    #  packet drop list
    ## @var packet_capture_options
    #  packet capture options
    ## @var packet_filter_widget
    #  packet filter widget
    ## @var packet_filter_list
    #  list of TypeIdConfig instances
    ## @var op_AND_button
    #  AND button
    ## @var op_OR_button
    #  OR button
    class PacketList(Gtk.ScrolledWindow):
        """
        PacketList class
        """
        ## @var table_model
        #  table model
        (
            COLUMN_TIME,
            COLUMN_INTERFACE,
            COLUMN_SIZE,
            COLUMN_CONTENTS,
            ) = range(4)

        def __init__(self):
            """
            Initializer
            @param self this object
            """
            super(ShowLastPackets.PacketList, self).__init__()
            self.set_properties(hscrollbar_policy=Gtk.PolicyType.AUTOMATIC,
                                vscrollbar_policy=Gtk.PolicyType.AUTOMATIC)
            self.table_model = Gtk.ListStore(*([str]*4))
            treeview = Gtk.TreeView(self.table_model)
            treeview.show()
            self.add(treeview)

            def add_column(descr, colid):
                column = Gtk.TreeViewColumn(descr, Gtk.CellRendererText(), text=colid)
                treeview.append_column(column)

            add_column("Time", self.COLUMN_TIME)
            add_column("Interface", self.COLUMN_INTERFACE)
            add_column("Size", self.COLUMN_SIZE)
            add_column("Contents", self.COLUMN_CONTENTS)

        def update(self, node, packet_list):
            """!
            Update function
            @param self this object
            @param node the node
            @param packet_list packet list
            @return none
            """
            self.table_model.clear()
            for sample in packet_list:
                tree_iter = self.table_model.append()
                if sample.device is None:
                    interface_name = "(unknown)"
                else:
                    interface_name = ns.core.Names.FindName(sample.device)
                    if not interface_name:
                        interface_name = "(interface %i)" % sample.device.GetIfIndex()
                self.table_model.set(tree_iter,
                                     self.COLUMN_TIME, str(sample.time.GetSeconds()),
                                     self.COLUMN_INTERFACE, interface_name,
                                     self.COLUMN_SIZE, str(sample.packet.GetSize ()),
                                     self.COLUMN_CONTENTS, str(sample.packet)
                                     )


    def __init__(self, visualizer, node_index):
        """
        Initializer
        @param self this object
        @param visualizer the visualizer object
        @param node_index the node index
        """
        InformationWindow.__init__(self)
        self.win = Gtk.Dialog(parent=visualizer.window,
                              flags=Gtk.DialogFlags.DESTROY_WITH_PARENT|Gtk.DialogFlags.NO_SEPARATOR,
                              buttons=(Gtk.STOCK_CLOSE, Gtk.ResponseType.CLOSE))
        self.win.connect("response", self._response_cb)
        self.win.set_title("Last packets for node %i" % node_index)
        self.visualizer = visualizer
        self.viz_node = visualizer.get_node(node_index)
        self.node = ns.network.NodeList.GetNode(node_index)

        def smart_expand(expander, vbox):
            if expander.get_expanded():
                vbox.set_child_packing(expander, expand=True, fill=True, padding=0, pack_type=Gtk.PACK_START)
            else:
                vbox.set_child_packing(expander, expand=False, fill=False, padding=0, pack_type=Gtk.PACK_START)

        main_hbox = Gtk.HBox(False, 4)
        main_hbox.show()
        main_vbox = Gtk.VBox(False, 4)
        main_vbox.show()
        self.win.vbox.add(main_hbox)
        main_hbox.add(main_vbox)

        self.tx_list = self.PacketList()
        self.tx_list.show()
        group = Gtk.Expander("Last transmitted packets")
        group.show()
        group.add(self.tx_list)
        main_vbox.pack_start(group, expand=False, fill=False)
        group.connect_after("activate", smart_expand, main_vbox)

        self.rx_list = self.PacketList()
        self.rx_list.show()
        group = Gtk.Expander("Last received packets")
        group.show()
        group.add(self.rx_list)
        main_vbox.pack_start(group, expand=False, fill=False)
        group.connect_after("activate", smart_expand, main_vbox)

        self.drop_list = self.PacketList()
        self.drop_list.show()
        group = Gtk.Expander("Last dropped packets")
        group.show()
        group.add(self.drop_list)
        main_vbox.pack_start(group, expand=False, fill=False)
        group.connect_after("activate", smart_expand, main_vbox)


        # Packet Filter

        # - options
        self.packet_capture_options = ns.visualizer.PyViz.PacketCaptureOptions()
        self.packet_capture_options.numLastPackets = 100

        packet_filter_vbox = Gtk.VBox(False, 4)
        packet_filter_vbox.show()
        main_hbox.add(packet_filter_vbox)

        sel_buttons_box = Gtk.HButtonBox()
        sel_buttons_box.show()
        packet_filter_vbox.pack_start(sel_buttons_box, False, False, 4)
        select_all_button = GObject.new(Gtk.Button, label="Sel. All", visible=True)
        select_none_button = GObject.new(Gtk.Button, label="Sel. None", visible=True)
        sel_buttons_box.add(select_all_button)
        sel_buttons_box.add(select_none_button)

        self.packet_filter_widget = ObjectList([
                Column('selected', title="Sel.", data_type=bool, editable=True),
                Column('name', title="Header"),
                ], sortable=True)
        self.packet_filter_widget.show()
        packet_filter_vbox.pack_start(self.packet_filter_widget, True, True, 4)

        class TypeIdConfig(object):
            __slots__ = ['name', 'selected', 'typeid']

        self.packet_filter_list = [] # list of TypeIdConfig instances

        Header = ns.core.TypeId.LookupByName("ns3::Header")
        Trailer = ns.core.TypeId.LookupByName("ns3::Trailer")
        for typeid_i in range(ns.core.TypeId.GetRegisteredN()):
            typeid = ns.core.TypeId.GetRegistered(typeid_i)
            # check if this is a header or trailer subtype
            typeid_tmp = typeid
            type_is_good = False
            while 1:
                if typeid_tmp == Header or typeid_tmp == Trailer:
                    type_is_good = True
                    break
                if typeid_tmp.HasParent():
                    typeid_tmp = typeid_tmp.GetParent()
                else:
                    break
            if not type_is_good:
                continue
            if typeid in [Header, Trailer]:
                continue
            c = TypeIdConfig()
            c.selected = True
            c.name = typeid.GetName()
            c.typeid = typeid
            self.packet_filter_list.append(c)
        self.packet_filter_widget.add_list(self.packet_filter_list)

        def update_capture_options():
            if self.op_AND_button.props.active:
                self.packet_capture_options.mode = ns.visualizer.PyViz.PACKET_CAPTURE_FILTER_HEADERS_AND
            else:
                self.packet_capture_options.mode = ns.visualizer.PyViz.PACKET_CAPTURE_FILTER_HEADERS_OR
            self.packet_capture_options.numLastPackets = 100
            self.packet_capture_options.headers = [c.typeid for c in self.packet_filter_list if c.selected]
            self.visualizer.simulation.lock.acquire()
            try:
                self.visualizer.simulation.sim_helper.SetPacketCaptureOptions(
                    self.node.GetId(), self.packet_capture_options)
            finally:
                self.visualizer.simulation.lock.release()

        def sel_all_cb(bt):
            for c in self.packet_filter_list:
                c.selected = True
            self.packet_filter_widget.refresh()
            update_capture_options()

        def sel_none_cb(bt):
            for c in self.packet_filter_list:
                c.selected = False
            self.packet_filter_widget.refresh()
            update_capture_options()

        select_all_button.connect("clicked", sel_all_cb)
        select_none_button.connect("clicked", sel_none_cb)

        op_buttons_box = Gtk.HButtonBox()
        op_buttons_box.show()
        packet_filter_vbox.pack_start(op_buttons_box, False, False, 4)
        self.op_AND_button = GObject.new(Gtk.RadioButton, label="AND", visible=True)
        self.op_OR_button = GObject.new(Gtk.RadioButton, label="OR", visible=True, group=self.op_AND_button)
        op_buttons_box.add(self.op_AND_button)
        op_buttons_box.add(self.op_OR_button)
        self.op_OR_button.props.active = True

        self.op_AND_button.connect("toggled", lambda b: update_capture_options())

        def cell_edited(l, obj, attribute):
            update_capture_options()
        self.packet_filter_widget.connect("cell-edited", cell_edited)

        update_capture_options()

        self.visualizer.add_information_window(self)
        self.win.set_default_size(600, 300)
        self.win.show()

    def _response_cb(self, win, response):
        """!
        Response callback function
        @param self this object
        @param win the window
        @param response the response
        @return none
        """
        self.win.destroy()
        self.visualizer.remove_information_window(self)

    def update(self):
        """!
        Update function
        @param self this object
        @return none
        """
        last_packets = self.visualizer.simulation.sim_helper.GetLastPackets(self.node.GetId())

        self.tx_list.update(self.node, last_packets.lastTransmittedPackets)
        self.rx_list.update(self.node, last_packets.lastReceivedPackets)
        self.drop_list.update(self.node, last_packets.lastDroppedPackets)
Exemplo n.º 31
0
class PluginManagerDialog(BasicDialog):
    size = (500, 350)
    title = _(u'Plugin Manager')
    help_section = 'plugin'

    def __init__(self, store):
        header = _(u'Select the plugin you want to activate and click in '
                   'the apply button.')
        BasicDialog.__init__(self,
                             hide_footer=False,
                             size=PluginManagerDialog.size,
                             title=PluginManagerDialog.title,
                             header_text=header)
        self.store = store
        self._manager = get_plugin_manager()
        self._setup_widgets()

    def _update_widgets(self):
        selected = self.klist.get_selected()
        self.ok_button.set_sensitive(selected and selected.can_activate())

    def _setup_widgets(self):
        self.set_ok_label(_(u'Activate'), Gtk.STOCK_APPLY)
        self.ok_button.set_sensitive(False)
        plugins = []

        for name in sorted(self._manager.available_plugins_names):
            desc = self._manager.get_description_by_name(name)
            plugins.append(
                _PluginModel(name, name
                             in self._manager.installed_plugins_names, desc))

        self.klist = ObjectList(self._get_columns(), plugins,
                                Gtk.SelectionMode.BROWSE)
        self.klist.set_headers_visible(False)
        self.klist.connect("selection-changed",
                           self._on_klist__selection_changed)
        self.main.remove(self.main.get_child())
        self.main.add(self.klist)
        self.klist.show()

    def _get_columns(self):
        return [
            Column('is_active', title=_('Active'), width=20, data_type=bool),
            Column('icon',
                   data_type=str,
                   width=24,
                   use_stock=True,
                   icon_size=Gtk.IconSize.BUTTON),
            Column('description', data_type=str, expand=True, use_markup=True)
        ]

    def _enable_plugin(self, plugin_model):
        plugin_name = plugin_model.name
        # This should not really be necessary, but there may be deadlocks when
        # activating the plugin. See bug 5272
        default_store = get_default_store()
        default_store.commit()
        with new_store() as store:
            self._manager.install_plugin(store, plugin_name)
        self._manager.activate_plugin(plugin_name)

        info(
            _("The plugin %s was successfully activated. Please, restart all "
              "Stoq instances connected to this installation.") %
            (plugin_name, ))

    #
    # BasicDialog
    #

    def confirm(self):
        msg = _("Are you sure you want activate this plugin?\n"
                "Please note that, once activated you will not "
                "be able to disable it.")
        response = yesno(msg, Gtk.ResponseType.NO, _("Activate plugin"),
                         _("Not now"))

        if response:
            self._enable_plugin(self.klist.get_selected())
            self.close()

    #
    # Callbacks
    #

    def _on_klist__selection_changed(self, list, data):
        self._update_widgets()
Exemplo n.º 32
0
class ShortcutsEditor(BasicDialog):
    size = (700, 400)
    title = _("Keyboard shortcuts")

    def __init__(self):
        BasicDialog.__init__(self,
                             size=ShortcutsEditor.size,
                             title=ShortcutsEditor.title)
        self._create_ui()

    def _create_ui(self):
        self.cancel_button.hide()

        hbox = gtk.HBox(spacing=6)
        self.main.remove(self.main.get_child())
        self.main.add(hbox)
        hbox.show()

        self.categories = ObjectList(
            [Column('label', sorted=True, expand=True)],
            get_binding_categories(), gtk.SELECTION_BROWSE)
        self.categories.connect('selection-changed',
                                self._on_categories__selection_changed)
        self.categories.set_headers_visible(False)
        self.categories.set_size_request(200, -1)
        hbox.pack_start(self.categories, False, False)
        self.categories.show()

        box = gtk.VBox(spacing=6)
        hbox.pack_start(box)
        box.show()

        self.shortcuts = ObjectList(self._get_columns(), [],
                                    gtk.SELECTION_BROWSE)
        box.pack_start(self.shortcuts)
        self.shortcuts.show()

        self._label = gtk.Label(
            _("You need to restart Stoq for the changes to take effect"))
        box.pack_start(self._label, False, False, 6)

        box.show()

        defaults_button = gtk.Button(_("Reset defaults"))
        defaults_button.connect('clicked', self._on_defaults_button__clicked)
        self.action_area.pack_start(defaults_button, False, False, 6)
        self.action_area.reorder_child(defaults_button, 0)
        defaults_button.show()

    def _on_categories__selection_changed(self, categories, category):
        if not category:
            return
        self.shortcuts.add_list(get_bindings(category.name), clear=True)

    def _on_defaults_button__clicked(self, button):
        old = self.categories.get_selected()
        api.user_settings.remove('shortcuts')
        remove_user_bindings()
        self._label.show()
        self.categories.refresh()
        self.categories.select(old)

    def _get_columns(self):
        return [
            Column('description',
                   _("Description"),
                   data_type=str,
                   expand=True,
                   sorted=True),
            ShortcutColumn('shortcut', _("Shortcut"), self)
        ]

    def set_binding(self, binding):
        set_user_binding(binding.name, binding.shortcut)
        d = api.user_settings.get('shortcuts', {})
        d[binding.name] = binding.shortcut
        self._label.show()

    def remove_binding(self, binding):
        remove_user_binding(binding.name)
        d = api.user_settings.get('shortcuts', {})
        try:
            del d[binding.name]
        except KeyError:
            pass
        self._label.show()
Exemplo n.º 33
0
class FormFieldEditor(BasicDialog):
    size = (700, 400)
    title = _("Form fields")

    def __init__(self, store):
        self.store = store
        BasicDialog.__init__(self, size=FormFieldEditor.size,
                             title=FormFieldEditor.title)
        self._create_ui()

    def _create_ui(self):
        hbox = gtk.HBox()
        self.main.remove(self.main.get_child())
        self.main.add(hbox)
        hbox.show()

        self.forms = ObjectList(
            [Column('description', title=_('Description'), sorted=True,
                    expand=True, format_func=stoqlib_gettext)],
            self.store.find(UIForm),
            gtk.SELECTION_BROWSE)
        self.forms.connect('selection-changed',
                           self._on_forms__selection_changed)
        self.forms.set_headers_visible(False)
        self.forms.set_size_request(200, -1)
        hbox.pack_start(self.forms, False, False)
        self.forms.show()

        box = gtk.VBox()
        hbox.pack_start(box)
        box.show()

        self.fields = ObjectList(self._get_columns(), [],
                                 gtk.SELECTION_BROWSE)
        box.pack_start(self.fields)
        self.fields.show()

        box.show()

    def _on_forms__selection_changed(self, forms, form):
        if not form:
            return
        self.fields.add_list(self.store.find(UIField,
                                             ui_form=form), clear=True)

    def _get_columns(self):
        return [Column('description', title=_('Description'), data_type=str,
                       expand=True, sorted=True,
                       format_func=stoqlib_gettext),
                Column('visible', title=_('Visible'), data_type=bool,
                       width=120, editable=True),
                Column('mandatory', title=_('Mandatory'), data_type=bool,
                       width=120, editable=True)]

    def confirm(self, *args):
        self.store.confirm(True)
        BasicDialog.confirm(self, *args)

    def cancel(self, *args):
        self.store.rollback(close=False)
        BasicDialog.confirm(self, *args)
Exemplo n.º 34
0
class FormFieldEditor(BasicDialog):
    size = (700, 400)
    title = _("Form fields")

    def __init__(self, store):
        self.store = store
        BasicDialog.__init__(self, size=FormFieldEditor.size,
                             title=FormFieldEditor.title)
        self._create_ui()

    def _create_ui(self):
        hbox = gtk.HBox()
        self.main.remove(self.main.get_child())
        self.main.add(hbox)
        hbox.show()

        self.forms = ObjectList(
            [Column('description', title=_('Description'), sorted=True,
                    expand=True, format_func=stoqlib_gettext)],
            self.store.find(UIForm),
            gtk.SELECTION_BROWSE)
        self.forms.connect('selection-changed',
                           self._on_forms__selection_changed)
        self.forms.set_headers_visible(False)
        self.forms.set_size_request(200, -1)
        hbox.pack_start(self.forms, False, False)
        self.forms.show()

        box = gtk.VBox()
        hbox.pack_start(box)
        box.show()

        self.fields = ObjectList(self._get_columns(), [],
                                 gtk.SELECTION_BROWSE)
        box.pack_start(self.fields)
        self.fields.show()

        box.show()

    def _on_forms__selection_changed(self, forms, form):
        if not form:
            return
        self.fields.add_list(self.store.find(UIField,
                                             ui_form=form), clear=True)

    def _get_columns(self):
        return [Column('description', title=_('Description'), data_type=str,
                       expand=True, sorted=True,
                       format_func=stoqlib_gettext),
                Column('visible', title=_('Visible'), data_type=bool,
                       width=120, editable=True),
                Column('mandatory', title=_('Mandatory'), data_type=bool,
                       width=120, editable=True)]

    def confirm(self, *args):
        self.store.confirm(True)
        BasicDialog.confirm(self, *args)
        info(_("Changes will be applied after all instances of Stoq are restarted."))

    def cancel(self, *args):
        self.store.rollback(close=False)
        BasicDialog.confirm(self, *args)
Exemplo n.º 35
0
class FormFieldEditor(BasicDialog):
    size = (700, 400)
    title = _("Form fields")

    def __init__(self, store):
        self.store = store
        BasicDialog.__init__(self, size=FormFieldEditor.size,
                             title=FormFieldEditor.title)
        self._create_ui()

    def _create_ui(self):
        hbox = Gtk.HBox()
        self.main.remove(self.main.get_child())
        self.main.add(hbox)
        hbox.show()

        self.forms = ObjectList(
            [Column('description', title=_('Description'), sorted=True,
                    expand=True, format_func=stoqlib_gettext)],
            self.store.find(UIForm),
            Gtk.SelectionMode.BROWSE)
        self.forms.connect('selection-changed',
                           self._on_forms__selection_changed)
        self.forms.set_headers_visible(False)
        self.forms.set_size_request(200, -1)
        hbox.pack_start(self.forms, False, False, 0)
        self.forms.show()

        box = Gtk.VBox()
        hbox.pack_start(box, True, True, 0)
        box.show()

        self.fields = ObjectList(self._get_columns(), [],
                                 Gtk.SelectionMode.BROWSE)
        box.pack_start(self.fields, True, True, 0)
        self.fields.show()

        box.show()

    def _on_forms__selection_changed(self, forms, form):
        if not form:
            return
        self.fields.add_list(self.store.find(UIField,
                                             ui_form=form), clear=True)
        self.fields.set_cell_data_func(self._uifield__cell_data_func)

    def _uifield__cell_data_func(self, column, renderer, obj, text):
        if isinstance(renderer, Gtk.CellRendererText):
            return text

        manager = get_plugin_manager()
        if manager.is_any_active(['nfe', 'nfce']):
            is_editable = obj.field_name not in [u'street', u'district',
                                                 u'city', u'state',
                                                 u'country', u'street_number']

            renderer.set_property('sensitive', is_editable)
            renderer.set_property('activatable', is_editable)
        return text

    def _get_columns(self):
        return [Column('description', title=_('Description'), data_type=str,
                       expand=True, sorted=True,
                       format_func=stoqlib_gettext),
                Column('visible', title=_('Visible'), data_type=bool,
                       width=120, editable=True),
                Column('mandatory', title=_('Mandatory'), data_type=bool,
                       width=120, editable=True)]

    def confirm(self, *args):
        self.store.confirm(True)
        BasicDialog.confirm(self, *args)
        info(_("Changes will be applied after all instances of Stoq are restarted."))

    def cancel(self, *args):
        self.store.rollback(close=False)
        BasicDialog.confirm(self, *args)
Exemplo n.º 36
0
class PaymentMethodsDialog(BasicDialog):
    # TODO Bug 2406 will avoid duplicating code here
    size = (400, 400)
    title = _("Payment Method Settings")

    # TODO: implement editor for 'multiple' payment method.
    METHOD_EDITORS = {
        u'card': CardPaymentMethodEditor,
        u'money': PaymentMethodEditor,
        u'check': PaymentMethodEditor,
        u'credit': PaymentMethodEditor,
        u'bill': PaymentMethodEditor,
        u'deposit': PaymentMethodEditor,
        u'store_credit': PaymentMethodEditor
    }

    def __init__(self, store):
        BasicDialog.__init__(self,
                             hide_footer=True,
                             size=PaymentMethodsDialog.size,
                             title=PaymentMethodsDialog.title)
        self._can_edit = False
        self.store = store
        self._setup_list()
        self._setup_slaves()

    def _setup_slaves(self):
        self._toolbar_slave = SearchEditorToolBar()
        self._toolbar_slave.connect("edit", self._on_edit_button__clicked)
        self._toolbar_slave.new_button.hide()
        self._toolbar_slave.edit_button.set_sensitive(False)
        self.attach_slave("extra_holder", self._toolbar_slave)

    def _setup_list(self):
        methods = PaymentMethod.get_editable_methods(self.store)
        self.klist = ObjectList(self._get_columns(), methods,
                                Gtk.SelectionMode.BROWSE)
        self.klist.connect("selection-changed",
                           self._on_klist__selection_changed)
        self.klist.connect("row-activated", self._on_klist__row_activated)
        self.klist.connect("cell-edited", self.on_cell_edited)
        self.main.remove(self.main.get_child())
        self.main.add(self.klist)
        self.klist.show()

    def _get_columns(self):
        return [
            Column('description',
                   title=_('Payment Method'),
                   data_type=str,
                   expand=True),
            Column('is_active',
                   title=_('Active'),
                   data_type=bool,
                   editable=True)
        ]

    def _edit_item(self, item):
        editor = self.METHOD_EDITORS.get(item.method_name, None)

        if not editor:
            raise TypeError('Invalid payment method adapter: %s' %
                            item.method_name)

        store = api.new_store()
        item = store.fetch(item)
        retval = run_dialog(editor, self, store, item)
        store.confirm(retval)
        store.close()

    #
    # Callbacks
    #

    def on_cell_edited(self, klist, obj, attr):
        # All the payment methods could be (de)activate, except the 'money'
        # payment method.
        if obj.method_name != u'money':
            store = obj.store
            store.commit()
        else:
            obj.is_active = True

    def _on_klist__selection_changed(self, list, data):
        self._can_edit = (data
                          and data.method_name in self.METHOD_EDITORS.keys())
        self._toolbar_slave.edit_button.set_sensitive(self._can_edit)

    def _on_edit_button__clicked(self, toolbar_slave):
        assert self._can_edit
        self._edit_item(self.klist.get_selected())

    def _on_klist__row_activated(self, list, data):
        if not self._can_edit:
            return

        self._edit_item(data)
Exemplo n.º 37
0
class ListContainer(gtk.HBox):
    """A ListContainer is an L{ObjectList} with buttons to be able
    to modify the content of the list.
    Depending on the list_mode, @see L{set_list_mode} you will
    have add, remove and edit buttons.

    Signals
    =======
      - B{add-item} (returns item):
        - emitted when the add button is clicked, you're expected to
          return an object here
      - B{remove-item} (item, returns bool):
        - emitted when removing an item,
          you can block the removal from the list by returning False
      - B{edit-item} (item):
        - emitted when editing an item
          you can block the update afterwards by returning False

    @ivar add_button: add button
    @type add_button: L{gtk.Button}
    @ivar remove_button: remove button
    @type remove_button: L{gtk.Button}
    @ivar edit_button: edit button
    @type edit_button: L{gtk.Button}
    """

    gsignal('add-item', retval=object)
    gsignal('remove-item', object, retval=bool)
    gsignal('edit-item', object, retval=bool)
    gsignal('selection-changed', object)

    def __init__(self, columns, orientation=gtk.ORIENTATION_VERTICAL):
        """
        Create a new ListContainer object.
        @param columns: columns for the L{kiwi.ui.objectlist.ObjectList}
        @type columns: a list of L{kiwi.ui.objectlist.Columns}
        @param orientation: the position where the buttons will be
            placed: at the right (vertically) or at the bottom (horizontally)
            of the list. Defaults to the right of the list.
        @type: gtk.ORIENTATION_HORIZONTAL or gtk.ORIENTATION_VERTICAL
        """
        self._list_type = None

        gtk.HBox.__init__(self)

        self._orientation = orientation

        self._create_ui(columns)
        self.set_list_type(ListType.NORMAL)

    # Private API

    def _create_ui(self, columns):
        self.list = ObjectList(columns)
        self.list.connect('selection-changed',
                          self._on_list__selection_changed)
        self.list.connect('row-activated', self._on_list__row_activated)

        self.add_button = gtk.Button(stock=gtk.STOCK_ADD)
        self.add_button.connect('clicked', self._on_add_button__clicked)

        self.remove_button = gtk.Button(stock=gtk.STOCK_REMOVE)
        self.remove_button.set_sensitive(False)
        self.remove_button.connect('clicked', self._on_remove_button__clicked)

        self.edit_button = gtk.Button(stock=gtk.STOCK_EDIT)
        self.edit_button.set_sensitive(False)
        self.edit_button.connect('clicked', self._on_edit_button__clicked)

        self._vbox = gtk.VBox(spacing=6)

        if self._orientation == gtk.ORIENTATION_VERTICAL:
            self.pack_start(self.list)
            self.list.show()
            self._add_buttons_to_box(self._vbox)
            self._pack_vbox()
        elif self._orientation == gtk.ORIENTATION_HORIZONTAL:
            self._vbox.pack_start(self.list)
            self.list.show()
            hbox = gtk.HBox(spacing=6)
            self._add_buttons_to_box(hbox)
            self._vbox.pack_start(hbox, expand=False)
            hbox.show()
            self._pack_vbox()
        else:
            raise TypeError(
                "buttons_orientation must be gtk.ORIENTATION_VERTICAL "
                " or gtk.ORIENTATION_HORIZONTAL")

    def _add_buttons_to_box(self, box):
        box.pack_start(self.add_button, expand=False)
        box.pack_start(self.remove_button, expand=False)
        box.pack_start(self.edit_button, expand=False)

    def _pack_vbox(self):
        self.pack_start(self._vbox, expand=False, padding=6)
        self._vbox.show()

    def _set_child_packing(self, padding):
        expand = self._orientation == gtk.ORIENTATION_HORIZONTAL

        self.set_child_packing(self._vbox, expand, True, padding,
                               gtk.PACK_START)

    def _add_item(self):
        retval = self.emit('add-item')
        if retval is None:
            return
        elif isinstance(retval, NotImplementedError):
            raise retval

        self.list.append(retval)

    def _remove_item(self, item):
        retval = self.emit('remove-item', item)
        if retval:
            self.list.remove(item)

    def _edit_item(self, item):
        retval = self.emit('edit-item', item)
        if retval:
            self.list.update(item)

    # Public API

    def add_item(self, item):
        """Appends an item to the list
        @param item: item to append
        """
        self.list.append(item)

    def add_items(self, items):
        """Appends a list of items to the list
        @param items: items to add
        @type items: a sequence of items
        """
        self.list.extend(items)

    def remove_item(self, item):
        """Removes an item from the list
        @param item: item to remove
        """
        self.list.remove(item)

    def update_item(self, item):
        """Updates an item in the list.
        You should call this if you change the object
        @param item: item to update
        """
        self.list.update(item)

    def default_remove(self, item):
        """Asks the user confirmation for removal of an item.
        @param item: a description of the item that will be removed
        @returns: True if the user confirm the removal, False otherwise
        """
        response = yesno(_('Do you want to remove %s ?') %
                         (quote(str(item)), ),
                         parent=None,
                         default=gtk.RESPONSE_OK,
                         buttons=((gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL),
                                  (gtk.STOCK_REMOVE, gtk.RESPONSE_OK)))
        return response == gtk.RESPONSE_OK

    def set_list_type(self, list_type):
        """Sets the kind of list type.
        @param list_type:
        """
        if not isinstance(list_type, ListType):
            raise TypeError("list_type must be a ListType enum")

        self.add_button.set_property('visible',
                                     (list_type != ListType.READONLY
                                      and list_type != ListType.REMOVEONLY
                                      and list_type != ListType.UNADDABLE))
        self.remove_button.set_property(
            'visible', (list_type != ListType.READONLY
                        and list_type != ListType.UNREMOVABLE))
        self.edit_button.set_property('visible',
                                      (list_type != ListType.READONLY
                                       and list_type != ListType.UNEDITABLE
                                       and list_type != ListType.REMOVEONLY))
        if list_type in [ListType.READONLY, ListType.REMOVEONLY]:
            padding = 0
        else:
            padding = 6
        self._set_child_packing(padding)
        self._list_type = list_type

    def clear(self):
        """Removes all the items in the list"""
        self.list.clear()

    # Callbacks

    def _on_list__selection_changed(self, list, selection):
        object_selected = selection is not None
        self.remove_button.set_sensitive(object_selected)
        self.edit_button.set_sensitive(object_selected)
        self.emit('selection-changed', selection)

    def _on_list__row_activated(self, list, item):
        if (self._list_type != ListType.READONLY
                and self._list_type != ListType.UNEDITABLE):
            self._edit_item(item)

    def _on_add_button__clicked(self, button):
        self._add_item()

    def _on_remove_button__clicked(self, button):
        self._remove_item(self.list.get_selected())

    def _on_edit_button__clicked(self, button):
        self._edit_item(self.list.get_selected())
Exemplo n.º 38
0
class PlayMediaWindow(Delegate):
    widgets = ["imgAddMediaFile"]

    def __init__(self, parent, recentPlayed=None):
        windowName = "PlayMediaWindow"

        Delegate.__init__(self, gladefile=globals.gladeFile,
                          toplevel_name=windowName,  
                          delete_handler=self.quit_if_last)

        # Create the delegate and set it up
        self.buildObjectList(recentPlayed)
        self.mediaList.grab_focus()
        slave = SlaveDelegate(toplevel=self.mediaList)

        self.attach_slave("labelX", slave)
        slave.focus_toplevel() # Must be done after attach

        self.slave = slave

        self.set_transient_for(parent)

        # Set the image
        image = self.get_widget("imgAddMediaFile")
        image.set_from_file(os.path.join(globals.imageDir, "movie_track_add.png"))


    def buildObjectList(self, mediaList):
        """
        This method builds and initialize the ObjectList.
        """
        self.mediaList = ObjectList(media_list_columns)
        self.mediaList.connect('selection_changed', self.media_selected)
        self.mediaList.connect('double-click', self.double_click)

        for media in mediaList:
            mf = MediaFile(media.getFilename(), uri=media.getURI(), length=media.getLengthSec())
            mf.lastPlayed = media.getLastPlayed()
            self.mediaList.append(mf)
            
        # FIXME: Remove it. Only for testing
        #for i in [('test1.wmv', "2.34"),
         #         ('test2.wmv', "2.59"),
          #        ('test3.wmv', "2.59"),
          #        ('test4.wmv', "2.59")]:
          #  self.mediaList.append(MediaFile(i[0], i[1]))


    def media_selected(self, the_list, item):
        pass


    def double_click(self, the_list, selected_object):
        self.emit('result', selected_object.uri)
        self.hide_and_quit()


    def on_bCancel__clicked(self, *args):
        """
        This method is called when the user clicks the Cancel button. It closes
        the dialog.
        """
        self.hide_and_quit()


    def on_bQuit__clicked(self, *args):
        """
        This method is called when the user clicks the Quit button. It closes
        the dialog and quits Morphin.
        """
        self.emit('result', 'quit')
        self.hide_and_quit()


    def on_bPlayMedia__clicked(self, *args):
        """
        This method is called when the user clicks the Play Media button.
        """
        dialog = gtk.FileChooserDialog("Open Media...",
                                        None,
                                        gtk.FILE_CHOOSER_ACTION_OPEN,
                                        (gtk.STOCK_CANCEL,
                                         gtk.RESPONSE_CANCEL,
                                         gtk.STOCK_OPEN,
                                         gtk.RESPONSE_OK))

        dialog.set_default_response(gtk.RESPONSE_OK)

        filter = gtk.FileFilter()
        filter.set_name("Media files")
        for pattern in globals.compatibleFiles:
            filter.add_mime_type(pattern)
        dialog.add_filter(filter)    
    
        filter = gtk.FileFilter()
        filter.set_name("All files")
        filter.add_pattern("*")
        dialog.add_filter(filter)


        response = dialog.run()
        if response == gtk.RESPONSE_OK:
            self.emit('result', dialog.get_filename())
            self.hide_and_quit()
        elif response == gtk.RESPONSE_CANCEL:
            pass

        dialog.destroy()


    def on_bPlayDisk__clicked(self, *args):
        """
        """
        pass
    
    
    def onKeyPressEvent(self, widget=None, event=None):
        """
        """
        pass
Exemplo n.º 39
0
class ShortcutsEditor(BasicDialog):
    size = (700, 400)
    title = _("Keyboard shortcuts")

    def __init__(self):
        BasicDialog.__init__(self, size=ShortcutsEditor.size,
                             title=ShortcutsEditor.title)
        self._create_ui()

    def _create_ui(self):
        self.cancel_button.hide()

        hbox = gtk.HBox(spacing=6)
        self.main.remove(self.main.get_child())
        self.main.add(hbox)
        hbox.show()

        self.categories = ObjectList(
            [Column('label', sorted=True, expand=True)],
            get_binding_categories(),
            gtk.SELECTION_BROWSE)
        self.categories.connect('selection-changed',
                                self._on_categories__selection_changed)
        self.categories.set_headers_visible(False)
        self.categories.set_size_request(200, -1)
        hbox.pack_start(self.categories, False, False)
        self.categories.show()

        box = gtk.VBox(spacing=6)
        hbox.pack_start(box)
        box.show()

        self.shortcuts = ObjectList(self._get_columns(), [],
                                    gtk.SELECTION_BROWSE)
        box.pack_start(self.shortcuts)
        self.shortcuts.show()

        self._label = gtk.Label(
            _("You need to restart Stoq for the changes to take effect"))
        box.pack_start(self._label, False, False, 6)

        box.show()

        defaults_button = gtk.Button(_("Reset defaults"))
        defaults_button.connect('clicked', self._on_defaults_button__clicked)
        self.action_area.pack_start(defaults_button, False, False, 6)
        self.action_area.reorder_child(defaults_button, 0)
        defaults_button.show()

    def _on_categories__selection_changed(self, categories, category):
        if not category:
            return
        self.shortcuts.add_list(get_bindings(category.name), clear=True)

    def _on_defaults_button__clicked(self, button):
        old = self.categories.get_selected()
        api.user_settings.remove('shortcuts')
        remove_user_bindings()
        self._label.show()
        self.categories.refresh()
        self.categories.select(old)

    def _get_columns(self):
        return [Column('description', _("Description"), data_type=str,
                       expand=True, sorted=True),
                ShortcutColumn('shortcut', _("Shortcut"), self)]

    def set_binding(self, binding):
        set_user_binding(binding.name, binding.shortcut)
        d = api.user_settings.get('shortcuts', {})
        d[binding.name] = binding.shortcut
        self._label.show()

    def remove_binding(self, binding):
        remove_user_binding(binding.name)
        d = api.user_settings.get('shortcuts', {})
        try:
            del d[binding.name]
        except KeyError:
            pass
        self._label.show()
Exemplo n.º 40
0
class PylintView(GladeDelegate, BaseReporter):
    """
    A dokuwiki editor window
    """
    mapping = {'E':'error',
               'W':'warning',
               'C':'low'}
    def __init__(self):
        self.icons = {}
        GladeDelegate.__init__(self, gladefile="lintgtk",
                          delete_handler=self.quit_if_last)
        self.setup_sourceview()
        self.setup_side()
        self.throbber_icon = Throbber(self.view.throbber)
        self.show_all()

    def setup_config(self):
        """
        Setup configuration file.
        """
        nomondir = os.path.join(user.home,'.pylintgtk')
        if not os.path.exists(nomondir):
            os.mkdir(nomondir)
        cfg = FileDataSource(file=os.path.join(nomondir,'config.caf'))
        cfg.save()
        return cfg

    # quit override to work with twisted
    def quit_if_last(self, *_):
        """
        Quit if we're the last window.
        """
        windows = [toplevel
               for toplevel in gtk.window_list_toplevels()
                   if toplevel.get_property('type') == gtk.WINDOW_TOPLEVEL]
        if len(windows) == 1:
            reactor.stop()

    def setup_sourceview(self):
        """
        Setup the source editor.
        """
        self.buffer = gtksourceview.Buffer()
        tagtable = self.buffer.get_tag_table()
        setup_tags(tagtable)
        lang_manager = gtksourceview.LanguageManager()
        lang = lang_manager.get_language('python')
        self.buffer.set_language(lang)
        self.editor = gtksourceview.View(self.buffer)
        accel_group = gtk.AccelGroup()
        self.get_toplevel().add_accel_group(accel_group)
        self.editor.add_accelerator("paste-clipboard",
                                               accel_group,
                                               ord('v'),
                                               gtk.gdk.CONTROL_MASK,
                                               0)
        self.editor.add_accelerator("copy-clipboard",
                                               accel_group,
                                               ord('c'),
                                               gtk.gdk.CONTROL_MASK,
                                               0)
        self.editor.add_accelerator("cut-clipboard",
                                               accel_group,
                                               ord('x'),
                                               gtk.gdk.CONTROL_MASK,
                                               0)
        self.editor.set_left_margin(5)
        self.editor.set_right_margin(5)
        self.editor.set_show_line_marks(True)
        self.editor.set_show_line_numbers(True)
        self.editor.set_auto_indent(True)
        self.editor.set_insert_spaces_instead_of_tabs(True)
        self.editor.set_highlight_current_line(True)        
        self.editor.set_indent_width(4)
        self.editor.set_indent_on_tab(True)
        path = environ.find_resource('images', 'red-warning.png')
        high = gtk.gdk.pixbuf_new_from_file(path)
        path = environ.find_resource('images', 'violet-warning.png')
        medium = gtk.gdk.pixbuf_new_from_file(path)
        path = environ.find_resource('images', 'yellow-warning.png')
        low = gtk.gdk.pixbuf_new_from_file(path)
        self.icons["low"] = low
        self.icons["warning"] = medium
        self.icons["error"] = high
        self.editor.set_mark_category_pixbuf('error', high)
        self.editor.set_mark_category_pixbuf('warning', medium)
        self.editor.set_mark_category_pixbuf('low', low)                
        self.view.scrolledwindow1.add(self.editor)        

    # setup functions
    def format_icon(self, value):
        """
        return appropriate icon for kiwi.
        """
        priority = self.mapping.get(value[0], 'low')
        return self.icons[priority]

    def setup_side(self):
        """
        Setup the side statistics.
        """
        columns = [Column('code', data_type=gtk.gdk.Pixbuf, format_func=self.format_icon, title=""),
                   Column('line', sorted=True),
                   Column('message'),
                   Column('object'),
                   Column('code')]
        self.problemlist = ObjectList(columns)

        self.view.side_vbox.pack_start(gtk.Label('Violations:'), False, False)
        self.view.side_vbox.add(self.problemlist)
        self.problemlist.connect("selection-changed", self.problem_selected)

        self.view.side_vbox.pack_start(gtk.Label('Stats:'), False, False)
        self.statslist = ObjectList([Column('name'), Column('value')])
        #self.backlinks.connect("selection-changed", self.change_selected)
        self.view.side_vbox.add(self.statslist)

    def problem_selected(self, _, violation):
        """
        A violation was selected on the objectlist.
        """
        if not violation:
            return
        start = self.buffer.get_iter_at_line(violation.line-1)
        end = self.buffer.get_iter_at_line(violation.line-1)
        end.forward_to_line_end()
        self.buffer.place_cursor(start)
        self.editor.scroll_to_iter(start, 0.1)
        self.buffer.select_range(start, end)

    def load_file(self, filename):
        """
        Load and check a file.
        """
        self.linter = lint.PyLinter()
        self.linter.set_reporter(self)
        checkers.initialize(self.linter)
        source_file = open(filename, 'r')
        text = source_file.read()
        source_file.close()
        self.buffer.set_property('text', text)
        self.linecount = self.buffer.get_line_count()
        self.text = text
        self.lastfilename = filename
        self.check()

    def set_score(self, value):
        """
        Set the score widget value.
        """
        scoretext = "<big><big><big>%s</big></big></big>" % (value,)
        self.view.score.set_markup(scoretext)

    def _check_done(self, _):
        """
        A pylint check has finished.
        """
        self.set_score("%.1f"%(self.linter.stats['global_note'],))
        self.view.comments.set_value(self.linter.stats['comment_lines'])
        self.view.lines.set_value(self.linter.stats['code_lines'])
        for key, value in self.linter.stats.iteritems():
            if value.__class__ in [int, str, float]:
                self.statslist.append(Stat(key.replace("_"," "), value))
        self.throbber_icon.stop()
        self.view.progressbar.set_fraction(1.0)

    def _check_error(self, failure):
        """
        There was an error doing the pylint check.
        """
        print self, failure

    def check(self):
        """
        Perform a check on current file.
        """
        self.view.progressbar.set_fraction(0.0)
        self.set_score('?')
        self.throbber_icon.start()
        self.idx = 0
        self.linter = lint.PyLinter()
        self.linter.set_reporter(self)
        checkers.initialize(self.linter)
        deferred = threads.deferToThread(self.linter.check, self.lastfilename)
        deferred.addCallback(self._check_done)
        deferred.addErrback(self._check_error)

    def on_button_save__clicked(self, _):
        """
        Kiwi callback for the save button.
        """
        text = self.buffer.get_property('text')
        sourcefile = open(self.lastfilename, 'w')
        sourcefile.write(text)
        sourcefile.close()
        self.linecount = self.buffer.get_line_count()

        while self.idx > 0:
            mark = self.buffer.get_mark(str(self.idx-1))
            if mark:
                self.buffer.delete_mark(mark)
            self.idx -= 1
        self.idx = 0
        self.problemlist.clear()
        self.statslist.clear()
        start, end = self.buffer.get_bounds()
        self.buffer.remove_source_marks(start, end, 'violation')
        self.buffer.remove_tag_by_name('violation', start, end)
        self.check()

    def _display(self, layout):
        """
        Necessary for pylint.BaseReporter
        """
        pass

    def add_message(self, msg_id, opts, msg):
        """
        A message arrived from the checker thread.
        """
        reactor.callFromThread(self.add_message_main, msg_id, opts, msg)

    def add_message_main(self, msg_id, opts, msg):
        """
        Main thread callback for a message arriving from pylint.
        """
        _, _, obj, line = opts
        self.problemlist.append(ViolationReport(code=msg_id,
                                                icon=msg_id,
                                                line=line, 
                                                object=obj, 
                                                message=msg))
        startiter = self.buffer.get_iter_at_line(line-1)
        enditer = self.buffer.get_iter_at_line(line-1)
        enditer.forward_to_line_end()
        self.buffer.apply_tag_by_name('violation', startiter, enditer)
        fraction = self.linter.stats['statement'] / float(self.linecount)
        self.view.progressbar.set_fraction(fraction)
        priority = self.mapping.get(msg_id[0], 'low')
        self.buffer.create_source_mark(str(self.idx), priority, startiter)
        self.idx += 1
Exemplo n.º 41
0
class ShowLastPackets(InformationWindow):
    ## @var win
    #  window
    ## @var visualizer
    #  visualizer
    ## @var viz_node
    #  visualizer node
    ## @var node
    #  the node
    ## @var tx_list
    #  packet transmit list
    ## @var rx_list
    #  packet receive list
    ## @var drop_list
    #  packet drop list
    ## @var packet_capture_options
    #  packet capture options
    ## @var packet_filter_widget
    #  packet filter widget
    ## @var packet_filter_list
    #  list of TypeIdConfig instances
    ## @var op_AND_button
    #  AND button
    ## @var op_OR_button
    #  OR button
    class PacketList(gtk.ScrolledWindow):
        """
        PacketList class
        """
        ## @var table_model
        #  table model
        (
            COLUMN_TIME,
            COLUMN_INTERFACE,
            COLUMN_SIZE,
            COLUMN_CONTENTS,
        ) = range(4)

        def __init__(self):
            """
            Initializer
            @param self this object
            """
            super(ShowLastPackets.PacketList, self).__init__()
            self.set_properties(hscrollbar_policy=gtk.POLICY_AUTOMATIC,
                                vscrollbar_policy=gtk.POLICY_AUTOMATIC)
            self.table_model = gtk.ListStore(*([str] * 4))
            treeview = gtk.TreeView(self.table_model)
            treeview.show()
            self.add(treeview)

            def add_column(descr, colid):
                column = gtk.TreeViewColumn(descr,
                                            gtk.CellRendererText(),
                                            text=colid)
                treeview.append_column(column)

            add_column("Time", self.COLUMN_TIME)
            add_column("Interface", self.COLUMN_INTERFACE)
            add_column("Size", self.COLUMN_SIZE)
            add_column("Contents", self.COLUMN_CONTENTS)

        def update(self, node, packet_list):
            """!
            Update function
            @param self this object
            @param node the node
            @param packet_list packet list
            @return none
            """
            self.table_model.clear()
            for sample in packet_list:
                tree_iter = self.table_model.append()
                if sample.device is None:
                    interface_name = "(unknown)"
                else:
                    interface_name = ns.core.Names.FindName(sample.device)
                    if not interface_name:
                        interface_name = "(interface %i)" % sample.device.GetIfIndex(
                        )
                self.table_model.set(tree_iter, self.COLUMN_TIME,
                                     str(sample.time.GetSeconds()),
                                     self.COLUMN_INTERFACE, interface_name,
                                     self.COLUMN_SIZE,
                                     str(sample.packet.GetSize()),
                                     self.COLUMN_CONTENTS, str(sample.packet))

    def __init__(self, visualizer, node_index):
        """
        Initializer
        @param self this object
        @param visualizer the visualizer object
        @param node_index the node index
        """
        InformationWindow.__init__(self)
        self.win = gtk.Dialog(parent=visualizer.window,
                              flags=gtk.DIALOG_DESTROY_WITH_PARENT
                              | gtk.DIALOG_NO_SEPARATOR,
                              buttons=(gtk.STOCK_CLOSE, gtk.RESPONSE_CLOSE))
        self.win.connect("response", self._response_cb)
        self.win.set_title("Last packets for node %i" % node_index)
        self.visualizer = visualizer
        self.viz_node = visualizer.get_node(node_index)
        self.node = ns.network.NodeList.GetNode(node_index)

        def smart_expand(expander, vbox):
            if expander.get_expanded():
                vbox.set_child_packing(expander,
                                       expand=True,
                                       fill=True,
                                       padding=0,
                                       pack_type=gtk.PACK_START)
            else:
                vbox.set_child_packing(expander,
                                       expand=False,
                                       fill=False,
                                       padding=0,
                                       pack_type=gtk.PACK_START)

        main_hbox = gtk.HBox(False, 4)
        main_hbox.show()
        main_vbox = gtk.VBox(False, 4)
        main_vbox.show()
        self.win.vbox.add(main_hbox)
        main_hbox.add(main_vbox)

        self.tx_list = self.PacketList()
        self.tx_list.show()
        group = gtk.Expander("Last transmitted packets")
        group.show()
        group.add(self.tx_list)
        main_vbox.pack_start(group, expand=False, fill=False)
        group.connect_after("activate", smart_expand, main_vbox)

        self.rx_list = self.PacketList()
        self.rx_list.show()
        group = gtk.Expander("Last received packets")
        group.show()
        group.add(self.rx_list)
        main_vbox.pack_start(group, expand=False, fill=False)
        group.connect_after("activate", smart_expand, main_vbox)

        self.drop_list = self.PacketList()
        self.drop_list.show()
        group = gtk.Expander("Last dropped packets")
        group.show()
        group.add(self.drop_list)
        main_vbox.pack_start(group, expand=False, fill=False)
        group.connect_after("activate", smart_expand, main_vbox)

        # Packet Filter

        # - options
        self.packet_capture_options = ns.visualizer.PyViz.PacketCaptureOptions(
        )
        self.packet_capture_options.numLastPackets = 100

        packet_filter_vbox = gtk.VBox(False, 4)
        packet_filter_vbox.show()
        main_hbox.add(packet_filter_vbox)

        sel_buttons_box = gtk.HButtonBox()
        sel_buttons_box.show()
        packet_filter_vbox.pack_start(sel_buttons_box, False, False, 4)
        select_all_button = gobject.new(gtk.Button,
                                        label="Sel. All",
                                        visible=True)
        select_none_button = gobject.new(gtk.Button,
                                         label="Sel. None",
                                         visible=True)
        sel_buttons_box.add(select_all_button)
        sel_buttons_box.add(select_none_button)

        self.packet_filter_widget = ObjectList([
            Column('selected', title="Sel.", data_type=bool, editable=True),
            Column('name', title="Header"),
        ],
                                               sortable=True)
        self.packet_filter_widget.show()
        packet_filter_vbox.pack_start(self.packet_filter_widget, True, True, 4)

        class TypeIdConfig(object):
            __slots__ = ['name', 'selected', 'typeid']

        self.packet_filter_list = []  # list of TypeIdConfig instances

        Header = ns.core.TypeId.LookupByName("ns3::Header")
        Trailer = ns.core.TypeId.LookupByName("ns3::Trailer")
        for typeid_i in range(ns.core.TypeId.GetRegisteredN()):
            typeid = ns.core.TypeId.GetRegistered(typeid_i)
            # check if this is a header or trailer subtype
            typeid_tmp = typeid
            type_is_good = False
            while 1:
                if typeid_tmp == Header or typeid_tmp == Trailer:
                    type_is_good = True
                    break
                if typeid_tmp.HasParent():
                    typeid_tmp = typeid_tmp.GetParent()
                else:
                    break
            if not type_is_good:
                continue
            if typeid in [Header, Trailer]:
                continue
            c = TypeIdConfig()
            c.selected = True
            c.name = typeid.GetName()
            c.typeid = typeid
            self.packet_filter_list.append(c)
        self.packet_filter_widget.add_list(self.packet_filter_list)

        def update_capture_options():
            if self.op_AND_button.props.active:
                self.packet_capture_options.mode = ns.visualizer.PyViz.PACKET_CAPTURE_FILTER_HEADERS_AND
            else:
                self.packet_capture_options.mode = ns.visualizer.PyViz.PACKET_CAPTURE_FILTER_HEADERS_OR
            self.packet_capture_options.numLastPackets = 100
            self.packet_capture_options.headers = [
                c.typeid for c in self.packet_filter_list if c.selected
            ]
            self.visualizer.simulation.lock.acquire()
            try:
                self.visualizer.simulation.sim_helper.SetPacketCaptureOptions(
                    self.node.GetId(), self.packet_capture_options)
            finally:
                self.visualizer.simulation.lock.release()

        def sel_all_cb(bt):
            for c in self.packet_filter_list:
                c.selected = True
            self.packet_filter_widget.refresh()
            update_capture_options()

        def sel_none_cb(bt):
            for c in self.packet_filter_list:
                c.selected = False
            self.packet_filter_widget.refresh()
            update_capture_options()

        select_all_button.connect("clicked", sel_all_cb)
        select_none_button.connect("clicked", sel_none_cb)

        op_buttons_box = gtk.HButtonBox()
        op_buttons_box.show()
        packet_filter_vbox.pack_start(op_buttons_box, False, False, 4)
        self.op_AND_button = gobject.new(gtk.RadioButton,
                                         label="AND",
                                         visible=True)
        self.op_OR_button = gobject.new(gtk.RadioButton,
                                        label="OR",
                                        visible=True,
                                        group=self.op_AND_button)
        op_buttons_box.add(self.op_AND_button)
        op_buttons_box.add(self.op_OR_button)
        self.op_OR_button.props.active = True

        self.op_AND_button.connect("toggled",
                                   lambda b: update_capture_options())

        def cell_edited(l, obj, attribute):
            update_capture_options()

        self.packet_filter_widget.connect("cell-edited", cell_edited)

        update_capture_options()

        self.visualizer.add_information_window(self)
        self.win.set_default_size(600, 300)
        self.win.show()

    def _response_cb(self, win, response):
        """!
        Response callback function
        @param self this object
        @param win the window
        @param response the response
        @return none
        """
        self.win.destroy()
        self.visualizer.remove_information_window(self)

    def update(self):
        """!
        Update function
        @param self this object
        @return none
        """
        last_packets = self.visualizer.simulation.sim_helper.GetLastPackets(
            self.node.GetId())

        self.tx_list.update(self.node, last_packets.lastTransmittedPackets)
        self.rx_list.update(self.node, last_packets.lastReceivedPackets)
        self.drop_list.update(self.node, last_packets.lastDroppedPackets)
Exemplo n.º 42
0
class ChartDialog(gtk.Window):
    def __init__(self):
        self._js_data = None
        self._js_options = None
        self._current = None

        gtk.Window.__init__(self)
        self.set_size_request(800, 480)

        self.vbox = gtk.VBox()
        self.add(self.vbox)
        self.vbox.show()

        hbox = gtk.HBox()
        self.vbox.pack_start(hbox, False, False, 6)
        hbox.show()

        label = gtk.Label('Period:')
        hbox.pack_start(label, False, False, 6)
        label.show()

        self.chart_type = ProxyComboBox()
        self.chart_type.connect('content-changed',
                                self._on_chart_type__content_changed)
        hbox.pack_start(self.chart_type, False, False, 6)
        self.chart_type.show()

        self.period_values = ProxyComboBox()
        self.period_values.connect('content-changed',
                                   self._on_period_values__content_changed)
        hbox.pack_start(self.period_values, False, False, 6)
        self.period_values.show()

        self._view = WebView()
        self._view.get_view().connect('load-finished',
                                      self._on_view__document_load_finished)
        self.vbox.pack_start(self._view, True, True)

        self.results = ObjectList()
        self.results.connect('row-activated', self._on_results__row_activated)
        self.vbox.pack_start(self.results, True, True)

        self._setup_daemon()

    @api. async
    def _setup_daemon(self):
        daemon = yield start_daemon()
        self._daemon_uri = daemon.base_uri

        proxy = daemon.get_client()
        yield proxy.callRemote('start_webservice')

        self.chart_type.prefill([
            ('Year', 'YearlyPayments'),
            ('Month', 'MonthlyPayments'),
            ('Day', 'DailyPayments'),
        ])

    @api. async
    def _invoke_chart(self, chart_type_name, **report_kwargs):
        def _get_chart_url(**kwargs):
            params = []
            for key, value in kwargs.items():
                params.append(key + '=' + str(value))
            return '%s/web/chart.json?%s' % (self._daemon_uri,
                                             '&'.join(params))

        url = _get_chart_url(type=chart_type_name, **report_kwargs)
        page = yield getPage(url)
        data = json.loads(page)
        api.asyncReturn(data)

    def _render_chart(self, chart_class, response):
        self._render_javascript(chart_class, response)
        self._render_objectlist(chart_class, response)

    def _render_javascript(self, chart_class, response):
        ticks = [item['short_title'] for item in response['items']]

        self._js_data = response['data']

        options = {}
        options['description'] = response['description']
        options['series'] = [
            dict(label=c['title']) for c in chart_class.columns
        ][1:]
        options['xaxis_ticks'] = ticks
        self._js_options = options

        self._view.load_uri('%s/web/static/chart.html' % (self._daemon_uri, ))

    def _render_objectlist(self, chart_class, response):
        columns = []
        for kwargs in chart_class.columns:
            kwargs = kwargs.copy()
            name = kwargs.pop('name')
            columns.append(Column(name, **kwargs))
        self.results.set_columns(columns)

        items = []
        for item in response['items']:
            settable = Settable(**item)
            settable.chart_class = chart_class
            items.append(settable)
        self.results.add_list(items, clear=True)
        self.results.show()

    def _load_finished(self):
        self._view.js_function_call("plot", self._js_data, self._js_options)

    @api. async
    def _show_one(self, chart_type_name, start, end):
        chart_class = get_chart_class(chart_type_name)
        report_kwargs = dict(start=start.strftime('%Y-%m-%d'),
                             end=end.strftime('%Y-%m-%d'))

        # Get chart datab
        response = yield self._invoke_chart(chart_type_name, **report_kwargs)
        self._render_chart(chart_class, response)

    def _update_period_values(self):
        chart_type_name = self.chart_type.get_selected()
        chart_class = get_chart_class(chart_type_name)
        values = chart_class.get_combo_labels()
        self.period_values.prefill(values)

    #
    # Callbacks
    #

    def _on_view__document_load_finished(self, view, frame):
        self._load_finished()

    def _on_chart_type__content_changed(self, combo):
        self._update_period_values()

    def _on_period_values__content_changed(self, combo):
        kind = self.chart_type.get_selected()
        value = self.period_values.get_selected()
        if not value:
            return
        start, end = value
        if self._current == (kind, start, end):
            return
        self._show_one(kind, start, end)
        self._current = kind, start, end

    def _on_results__row_activated(self, results, item):
        chart_type_name = item.chart_class.__name__
        if chart_type_name == 'YearlyPayments':
            start = localdate(item.year, 1, 1).date()
            end = localdate(item.year, 12, 31).date()
            chart_type_name = 'MonthlyPayments'
        elif chart_type_name == 'MonthlyPayments':
            start = localdate(item.year, item.month, 1).date()
            end = start + relativedelta(days=31)
            chart_type_name = 'DailyPayments'
        else:
            return
        self._show_one(chart_type_name, start, end)
Exemplo n.º 43
0
class CreateDeliveryEditor(BaseEditor):
    """A fake delivery editor implementation.

    This is used to get get information for creating a delivery,
    without really creating a it.
    """

    model_name = _('Delivery')
    model_type = CreateDeliveryModel
    form_holder_name = 'forms'
    gladefile = 'CreateDeliveryEditor'
    title = _('New Delivery')
    form_columns = 2
    size = (750, 550)

    @cached_property()
    def fields(self):
        # Only users with admin or purchase permission can modify transporters
        user = api.get_current_user(self.store)
        can_modify_transporter = any((
            user.profile.check_app_permission(u'admin'),
            user.profile.check_app_permission(u'purchase'),
        ))

        return collections.OrderedDict(
            client=PersonQueryField(_("Client"), proxy=True, mandatory=True,
                                    person_type=Client),
            transporter_id=PersonField(_("Transporter"), proxy=True,
                                       person_type=Transporter,
                                       can_add=can_modify_transporter,
                                       can_edit=can_modify_transporter),
            address=AddressField(_("Address"), proxy=True, mandatory=True),
            price=PriceField(_("Delivery cost"), proxy=True),
            estimated_fix_date=DateField(_("Estimated delivery date"), proxy=True),
        )

    def __init__(self, store, model=None, sale_items=None):
        self.sale_items = sale_items
        self._deliver_items = []

        if not model:
            for sale_item in sale_items:
                sale_item.deliver = True
        else:
            model.create_savepoint()
            # Store this information for later rollback.
            for sale_item in sale_items:
                self._deliver_items.append(sale_item.deliver)

        BaseEditor.__init__(self, store, model)
        self._setup_widgets()

    def _setup_widgets(self):
        self.additional_info_label.set_size('small')
        self.additional_info_label.set_color('red')
        self.register_validate_function(self._validate_widgets)
        self.set_description(self.model_name)

        self._update_widgets()

    def _validate_widgets(self, validation_value):
        if validation_value:
            validation_value = any(item.deliver for item in self.items)
        self.refresh_ok(validation_value)

    def _update_widgets(self):
        if self.model.notes:
            self.additional_info_label.show()
        else:
            self.additional_info_label.hide()

    def _get_sale_items_columns(self):
        return [Column('code', title=_('Code'),
                       data_type=str),
                Column('description', title=_('Description'),
                       data_type=str, expand=True),
                Column('quantity', title=_('Quantity'),
                       data_type=decimal.Decimal, format_func=format_quantity),
                Column('deliver', title=_('Deliver'),
                       data_type=bool, editable=True)]

    #
    # Callbacks
    #

    def on_additional_info_button__clicked(self, button):
        if run_dialog(NoteEditor, self, self.store, self.model, 'notes',
                      title=_('Delivery Instructions')):
            self._update_widgets()

    def on_estimated_fix_date__validate(self, widget, date):
        if date < localtoday().date():
            return ValidationError(_("Expected delivery date must "
                                     "be set to a future date"))

    def on_price__validate(self, widget, price):
        if price < 0:
            return ValidationError(
                _("The Delivery cost must be a positive value."))

    def on_client__content_changed(self, entry):
        client = entry.read()
        if client is None:
            return
        self.fields['address'].set_from_client(client)

    def on_transporter_id__validate(self, widget, transporter_id):
        transporter = self.store.get(Transporter, transporter_id)
        return StockOperationPersonValidationEvent.emit(transporter.person, type(transporter))

    def on_client__validate(self, widget, client):
        return StockOperationPersonValidationEvent.emit(client.person, type(client))

    def _on_items__cell_edited(self, items, item, attribute):
        self.force_validation()

    #
    # BaseEditor hooks
    #

    def create_model(self, store):
        price = sysparam.get_object(store, 'DELIVERY_SERVICE').sellable.price
        return CreateDeliveryModel(price=price)

    def setup_slaves(self):
        self.items = ObjectList(columns=self._get_sale_items_columns(),
                                objects=self.sale_items)
        self.items.connect('cell-edited', self._on_items__cell_edited)
        self.addition_list_holder.add(self.items)
        self.items.show()

    def on_cancel(self):
        # FIXME: When Kiwi allows choosing proxies to save upon confirm, apply
        # that here instead of making this rollback by hand. Bug 5415.
        self.model.rollback_to_savepoint()
        if self._deliver_items:
            for sale_item, deliver in zip(self.sale_items, self._deliver_items):
                sale_item.deliver = deliver

    def on_confirm(self):
        estimated_fix_date = self.estimated_fix_date.read()
        for sale_item in self.sale_items:
            sale_item.estimated_fix_date = estimated_fix_date
Exemplo n.º 44
0
class Connections(GladeWidget):
    gladeFile = "connections.glade"

    gsignal("have-connection", bool)
    gsignal("connection-activated", object)
    gsignal("connections-cleared")

    def __init__(self):
        GladeWidget.__init__(self)

        columns = [
            Column("host", title=_("Hostname"), searchable=True),
            Column(
                "timestamp", title=_("Last used"), sorted=True, order=gtk.SORT_DESCENDING, format_func=format_timestamp
            ),
        ]
        self._connections = ObjectList(columns, objects=getRecentConnections(), mode=gtk.SELECTION_SINGLE)
        self._connections.connect("row-activated", self._on_objectlist_row_activated)
        self._connections.connect("selection-changed", self._on_objectlist_selection_changed)
        self._connections.set_size_request(-1, 160)
        self.page.pack_start(self._connections)
        self.page.reorder_child(self._connections, 0)
        self._connections.get_treeview().set_search_equal_func(self._searchEqual)
        self._connections.show()
        self._updateButtons()

    def _updateButtons(self):
        canClear = hasRecentConnections()
        self.button_clear.set_sensitive(canClear)
        self.button_clear_all.set_sensitive(canClear)
        if not canClear:
            self.emit("connections-cleared")

    def _searchEqual(self, model, column, key, iter):
        connection = model.get(iter, column)[0]
        if key in connection.name:
            return False

        # True means doesn't match
        return True

    def _clear_all(self):
        for conn in self._connections:
            os.unlink(conn.filename)
        self._connections.clear()

    def _clear(self, conn):
        self._connections.remove(conn)
        os.unlink(conn.filename)

    # Public API

    def grab_focus(self):
        if len(self._connections):
            self._connections.select(self._connections[0])
        self._connections.grab_focus()

    def get_selected(self):
        return self._connections.get_selected()

    def update(self, connection):
        os.utime(connection.filename, None)

    # Callbacks

    def on_button_clear_clicked(self, button):
        conn = self._connections.get_selected()
        if conn:
            self._clear(conn)
        self._updateButtons()

    def on_button_clear_all_clicked(self, button):
        self._clear_all()
        self._updateButtons()

    def _on_objectlist_row_activated(self, connections, connection):
        self.emit("connection-activated", connection)

    def _on_objectlist_selection_changed(self, connections, connection):
        self.emit("have-connection", bool(connection))
Exemplo n.º 45
0
class BookmarkView(PidaView):

    icon_name = 'gtk-library'
    label_text = _('Bookmarks')

    def create_ui(self):
        self._vbox = gtk.VBox()
        self.create_toolbar()
        self.create_ui_list()
        self.add_main_widget(self._vbox)
        self._vbox.show_all()

    def create_tab_label(self, icon_name, text):
        if None in [icon_name, text]:
            return None
        label = gtk.Label(text)
        b_factory = gtk.HBox
        b = b_factory(spacing=2)
        icon = gtk.image_new_from_stock(icon_name, gtk.ICON_SIZE_MENU)
        b.pack_start(icon)
        b.pack_start(label)
        b.show_all()
        return b

    def create_ui_list(self):
        self._books = gtk.Notebook()
        self._books.set_border_width(6)
        self._list_dirs = ObjectList([Column('markup', data_type=str, use_markup=True)])
        self._list_dirs.connect('row-activated', self._on_item_activated)
        self._list_dirs.connect('selection-changed', self._on_item_selected)
        self._list_dirs.set_headers_visible(False)
        self._list_dirs.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        self._books.append_page(self._list_dirs,
                tab_label=self.create_tab_label('stock_folder', _('Dirs')))
        self._list_files = ObjectList([Column('markup', data_type=str, use_markup=True)])
        self._list_files.connect('row-activated', self._on_item_activated)
        self._list_files.connect('selection-changed', self._on_item_selected)
        self._list_files.set_headers_visible(False)
        self._list_files.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        self._books.append_page(self._list_files,
                tab_label=self.create_tab_label('text-x-generic', _('Files')))
        """
        self._list_url = ObjectList([Column('markup', data_type=str, use_markup=True)])
        self._list_url.set_headers_visible(False)
        self._books.add(self._list_url)
        """
        self._vbox.add(self._books)
        self._books.show_all()

    def create_toolbar(self):
        self._uim = gtk.UIManager()
        self._uim.insert_action_group(self.svc.get_action_group(), 0)
        self._uim.add_ui_from_file(get_uidef_path('bookmark-toolbar.xml'))
        self._uim.ensure_update()
        self._toolbar = self._uim.get_toplevels('toolbar')[0]
        self._toolbar.set_style(gtk.TOOLBAR_ICONS)
        self._toolbar.set_icon_size(gtk.ICON_SIZE_SMALL_TOOLBAR)
        self._vbox.pack_start(self._toolbar, expand=False)
        self._toolbar.show_all()

    def add_item(self, item):
        if item.group == 'file':
            self._list_files.append(item)
        elif item.group == 'path':
            self._list_dirs.append(item)
        elif item.group == 'url':
            self._list_urls.append(item)

    def remove_item(self, item):
        if item.group == 'file':
            self._list_files.remove(item)
        elif item.group == 'path':
            self._list_dirs.remove(item)
        elif item.group == 'url':
            self._list_urls.remove(item)

    def clear_all(self):
        self._list_files.clear()
        self._list_dirs.clear()
        #self._list_urls.clear()

    def can_be_closed(self):
        self.svc.get_action('show_bookmark').set_active(False)

    def _on_item_selected(self, olist, item):
        self.svc.set_current(item)

    def _on_item_activated(self, olist, item):
        item.run(self.svc)
Exemplo n.º 46
0
class DokuwikiView(GladeDelegate):
    """
    A dokuwiki editor window
    """
    def __init__(self):
        GladeDelegate.__init__(self, gladefile="pydoku",
                          delete_handler=self.quit_if_last)
        self._icons = {}
        self.throbber_icon = Throbber(self.view.throbber)
        self.setup_wikitree()
        self.setup_wikislist()
        self.setup_attachments()
        self.setup_lastchanges()
        self.setup_side()
        self.setup_sourceview()
        self.setup_htmlview()
        self.page_edit = self.view.notebook1.get_nth_page(0)
        self.page_view = self.view.notebook1.get_nth_page(1)
        self.page_attach = self.view.notebook1.get_nth_page(2)
        self.show_all()
        if len(cfg.getChildren()):
            wiki = cfg.getChildren()[0]
            self.connect(wiki.url, wiki.user, wiki.password)
            self.wiki = wiki
            if wiki.current:
                self.load_page(wiki.current)

    # quit override to work with twisted
    def quit_if_last(self, *args):
        self.htmlview.destroy() # for some reason has to be deleted explicitly
        windows = [toplevel
               for toplevel in gtk.window_list_toplevels()
                   if toplevel.get_property('type') == gtk.WINDOW_TOPLEVEL]
        if len(windows) == 1:
            reactor.stop()

    # general interface functions
    def post(self, text):
        id = self.view.statusbar.get_context_id("zap")
        self.view.statusbar.push(id, text)

    # setup functions
    def setup_wikislist(self):
        columns = [Column('url',format_func=self.get_favicon,data_type=gtk.gdk.Pixbuf,icon_size=gtk.ICON_SIZE_SMALL_TOOLBAR)]
        self.wikislist = ObjectList(columns)
        columns.append(Column('url', title='Wiki', column='url'))
        self.wikislist.set_columns(columns)
        self.view.vbox2.pack_start(self.wikislist)
        self.view.vbox2.reorder_child(self.wikislist, 0)
        self.wikislist.add_list(cfg.getChildren())
        self.wikislist.connect("selection-changed", self.wiki_selected)
        threads.deferToThread(self.download_favicons, cfg)

    def download_favicons(self, cfg):
        for wiki in cfg.getChildren():
            self.download_favicon(wiki)

    def download_favicon(self, wiki):
        import urllib
        icon_url = wiki.url+"/lib/tpl/sidebar/images/favicon.ico"
        filename, headers = urllib.urlretrieve(icon_url, "/tmp/ico.ico")
        if headers["Content-Type"] == 'image/x-icon':
            self.add_favicon(wiki, filename)

    def get_favicon(self, wiki_url):
        return self._icons.get(wiki_url, page_icon)

    def add_favicon(self, wiki, filename):
        pixbuf = gtk.gdk.pixbuf_new_from_file(filename)
        pixbuf = pixbuf.scale_simple(16,16,gtk.gdk.INTERP_BILINEAR)
        self._icons[wiki.url] = pixbuf
        self.objectlist.refresh()

    def setup_side(self):
        columns = ['sum', 'user', 'type', 'version', 'ip']
        columns = [Column(s) for s in columns]
        self.versionlist = ObjectList(columns)

        self.view.side_vbox.pack_start(gtk.Label('Version Log:'), False, False)
        self.view.side_vbox.add(self.versionlist)
        self.versionlist.connect("selection-changed", self.version_selected)

        self.view.side_vbox.pack_start(gtk.Label('BackLinks:'), False, False)
        self.backlinks = ObjectList([Column('name')])
        self.backlinks.connect("selection-changed", self.change_selected)
        self.view.side_vbox.add(self.backlinks)

    def setup_attachments(self):
        columns = ['id', 'size', 'lastModified', 'writable', 'isimg', 'perms']
        columns = [Column(s) for s in columns]
        self.attachmentlist = ObjectList(columns)

        self.view.attachments_vbox.add(self.attachmentlist)

    def setup_lastchanges(self):
        columns = ['name', 'author', 'lastModified', 'perms', 'version', 'size']
        columns = [Column(s) for s in columns]
        columns.append(Column('lastModified', sorted=True, order=gtk.SORT_DESCENDING))
        self.lastchangeslist = ObjectList(columns)
        self.lastchangeslist.connect("selection-changed", self.change_selected)

        self.view.side_vbox.add(self.lastchangeslist)


    def setup_wikitree(self):
        columns = ['id', 'lastModified', 'perms', 'size']
        columns = [Column(s) for s in columns]
        columns.insert(0, Column('icon', title='name', data_type=gtk.gdk.Pixbuf))
        self.objectlist = ObjectTree(columns)
        columns.insert(1, Column('name', column='icon'))
        self.objectlist.set_columns(columns)

        self.objectlist.connect("selection-changed", self.selected)
        self.view.vbox2.add(self.objectlist)

    def html_realized(self, widget):
        if self.wiki and self.wiki.current:
            self.get_htmlview(self.wiki.current)

    def setup_htmlview(self):
        self.htmlview = gtkmozembed.MozEmbed()
        self.view.html_scrolledwindow.add_with_viewport(self.htmlview)
        self.htmlview.connect('realize', self.html_realized)
        #self.htmlview.set_size_request(800,600)
        #self.htmlview.realize()
        #self.view.html_scrolledwindow.show_all()
        #self.htmlview.show()

    def setup_sourceview(self):
        self.buffer = DokuwikiBuffer(table)
        self.editor = gtksourceview.SourceView(self.buffer)
        #self.editor.set_show_line_numbers(True)
        accel_group = gtk.AccelGroup()
        self.get_toplevel().add_accel_group(accel_group)
        self.editor.add_accelerator("paste-clipboard", accel_group, ord('v'), gtk.gdk.CONTROL_MASK, 0)
        self.editor.add_accelerator("copy-clipboard", accel_group, ord('c'), gtk.gdk.CONTROL_MASK, 0)
        self.editor.add_accelerator("cut-clipboard", accel_group, ord('x'), gtk.gdk.CONTROL_MASK, 0)
        #self.editor = gtk.TextView(self.buffer)
        self.editor.set_left_margin(5)
        self.editor.set_right_margin(5)
        self.editor.set_wrap_mode(gtk.WRAP_WORD_CHAR)
        self.view.scrolledwindow1.add(self.editor)

        lm = gtksourceview.SourceLanguagesManager()
        langs = lm.get_available_languages()
        lang_diffs = filter(lambda s: s.get_name() == 'Diff', langs)
        if lang_diffs:
            self.buffer.set_language(lang_diffs[0])

    # dokuwiki operations
    def _getVersion(self):
        return self._rpc.dokuwiki.getVersion()

    def get_version(self):
        return threads.deferToThread(self._getVersion)

    def get_pagelist(self):
        print "getpagelist1"
        pages = self._rpc.wiki.getAllPages()
        self._sections = {}
        self.objectlist.clear()
        print "getpagelist1.5"
        print "PAGES",pages
        for page in pages:
            self.add_page(page)
        print "getpagelist2"
        self.view.new_page.set_sensitive(True)
        self.view.delete_page.set_sensitive(True)
        if self.wiki.current:
            self.set_selection(self.wiki.current)
        print "getpagelist3"
        # XXX
        self.get_recent_changes()
        print "getpagelist2"

    def _getRecentChanges(self):
        return self._rpc.wiki.getRecentChanges(int(time.time()-(60*60*24*7*12)))

    def _gotRecentChanges(self, changes):
        changes = [DictWrapper(s) for s in changes]
        self.lastchangeslist.add_list(changes)

    def get_recent_changes(self):
        self.callDeferred(self._getRecentChanges, self._gotRecentChanges)

    def get_attachments(self, ns):
        attachments = self._rpc.wiki.getAttachments(ns, {})
        attachments = [DictWrapper(s) for s in attachments]
        self.attachmentlist.add_list(attachments)

    def _getBackLinks(self, pagename):
        return self._rpc.wiki.getBackLinks(pagename)

    def _gotBackLinks(self, backlinks):
        backlinks = [Section(s) for s in backlinks]
        self.backlinks.add_list(backlinks)

    def get_backlinks(self, pagename):
        self.callDeferred(self._getBackLinks, self._gotBackLinks, pagename)

    def _getVersions(self, pagename):
        return self._rpc.wiki.getPageVersions(pagename, 0)

    def _gotVersions(self, versionlist):
        versionlist = [DictWrapper(s) for s in versionlist]
        self.versionlist.add_list(versionlist)

    def get_versions(self, pagename):
        self.callDeferred(self._getVersions, self._gotVersions, pagename)

    def _getHtmlData(self, pagename):
        text = self._rpc.wiki.getPageHTML(pagename)
        return text

    def _gotHtmlData(self, text):
        self.throbber_icon.stop()
        if not self.htmlview.window:
            return
        text = """<head><meta http-equiv="Content-Type" content="text/html;  charset=utf-8" />
        </head><body>"""+text+"</body>"
        self.htmlview.render_data(text, len(text), self.wiki.url, 'text/html')
        self.htmlview.realize()
        self.htmlview.show()

    def get_htmlview(self, pagename):
        self.throbber_icon.start()
        self.callDeferred(self._getHtmlData, self._gotHtmlData, pagename)
        #d.addErrback(self.someError)

        # XXX following is for gtkhtml (not used)
        #self.document.clear()
        #self.document.open_stream('text/html')
        #self.document.write_stream(text)
        #self.document.close_stream()

    def callDeferred(self, get_func, got_func, *args):
        d = threads.deferToThread(get_func, *args)
        d.addCallback(got_func)

    def _getEditText(self, pagename):
        return self._rpc.wiki.getPage(pagename)

    def _gotEditText(self, text):
        self.throbber_icon.stop()
        self.buffer.set_highlight(False)
        self.editor.set_editable(True)
        self.buffer.add_text(text)

    def _getDiffText(self, pagename, version, idx):
        return self._rpc.wiki.getPageVersion(pagename, version), idx

    def _gotDiffText(self, data):
        import difflib
        import StringIO
        text, idx = data
        self.textstack[idx] = text
        if not None in self.textstack:
            fromlines = self.textstack[0].split("\n")
            tolines = self.textstack[1].split("\n")
            diff_text = difflib.unified_diff(fromlines, tolines, "a", "b",
                                        self.versions[0],
                                        self.versions[1])
            self.throbber_icon.stop()
            self.buffer.clear()
            str_buffer = StringIO.StringIO()
            for line in diff_text:
                str_buffer.write(line+'\n')
            str_buffer.seek(0)
            self.buffer.add_text(str_buffer.read())
            self.buffer.set_highlight(True)
            self.editor.set_editable(False)

    def get_difftext(self, pagename, version, prev_version):
        self.throbber_icon.start()
        self.textstack = [None, None]
        self.versions = [version, prev_version]
        self.callDeferred(self._getDiffText, self._gotDiffText,
                                  pagename, prev_version, 0)
        self.callDeferred(self._getDiffText, self._gotDiffText,
                                  pagename, version, 1)


    def get_edittext(self, pagename):
        self.throbber_icon.start()
        self.callDeferred(self._getEditText, self._gotEditText, pagename)

    def put_page(self, text, summary, minor):
        pars = {}
        if summary:
            pars['sum'] = summary
        if minor:
            pars['minor'] = minor
        d = threads.deferToThread(self._rpc.wiki.putPage, self.wiki.current, text, pars)
        return d

    # put a page into the page tree
    def add_page(self, page):
      print page
      try:
        name = page["id"]
        path = name.split(":")
        prev = None
        for i, pathm in enumerate(path):
            if i == len(path)-1: # a page
                new = DictWrapper(page, pathm)
                self._sections[name] = new
                self.objectlist.append(prev, new, False)
            else: # a namespace
                part_path = ":".join(path[:i+1])
                if not part_path in self._sections:
                    new = Section(pathm, part_path)
                    self._sections[part_path] = new
                    self.objectlist.append(prev, new, False)
                else:
                    new = self._sections[part_path]
            prev = new
      except:
        traceback.print_exc()

    def expand_to(self, pagename):
        path = pagename.split(":")
        for i, pathm in enumerate(path):
            if not i == len(path)-1:
                section = self._sections[":".join(path[:i+1])]
                self.view.objectlist.expand(section)

    def set_selection(self, pagename):
        obj = self._sections[pagename]
        self.expand_to(pagename)
        self.view.objectlist.select(obj, True)
        #self.selected(widget, obj)

    # page selected callback
    def wiki_selected(self, widget, wiki):
        self.connect(wiki.url, wiki.user, wiki.password)
        self.objectlist.clear()
        self.versionlist.clear()
        self.lastchangeslist.clear()
        self.backlinks.clear()
        self._sections = {}
        self.wiki = wiki
        self.buffer.clear()
        if wiki.current:
            self.load_page(wiki.current)

    def version_selected(self, widget, object):
        # yes, the previous item is the next in the widget
        if object == None:
            return
        previous = widget.get_next(object)
        if not previous:
            return
        prev_version = previous.version
        self.get_difftext(self.wiki.current, int(object.version),
                          int(prev_version))

    def change_selected(self, widget, object):
        if not object:
            return
        self.set_selection(object.name)

    def selected(self, widget, object):
        if not object:
            return
        if isinstance(object, Section):
            self.get_attachments(object.id)
        if not isinstance(object, DictWrapper):
            return
        self.wiki.current = object.id
        cfg.save()
        self.load_page(object.id)

    def load_page(self, pagename):
        self.get_edittext(pagename)
        self.get_htmlview(pagename)
        self.get_backlinks(pagename)
        self.get_versions(pagename)

    # kiwi interface callbacks
    def on_view_edit__toggled(self, widget):
        if widget.get_active():
            self.notebook1.insert_page(self.page_edit, gtk.Label('edit'), 0)
        else:
            self.notebook1.remove_page(self.notebook1.page_num(self.page_edit))

    def on_view_view__toggled(self, widget):
        if widget.get_active():
            self.notebook1.insert_page(self.page_view, gtk.Label('view'), 1)
        else:
            self.notebook1.remove_page(self.notebook1.page_num(self.page_view))

    def on_view_attachments__toggled(self, widget):
        if widget.get_active():
            self.notebook1.insert_page(self.page_attach, gtk.Label('attach'))
        else:
            self.notebook1.remove_page(self.notebook1.page_num(self.page_attach))

    def on_view_extra__toggled(self, widget):
        if widget.get_active():
            self.backlinks.show()
            self.versionlist.show()
            self.view.hpaned2.set_position(self._prevpos)
        else:
            self.backlinks.hide()
            self.versionlist.hide()
            self._prevpos = self.view.hpaned2.get_position()
            self.view.hpaned2.set_position(self.view.hpaned2.allocation.width)

    def on_button_add__clicked(self, *args):
        dialog = ModalDialog("User Details")
        # prepare
        widgets = {}
        items = ["url","user", "password"]
        for i, item in enumerate(items):
            widgets[item] = gtk.Entry()
            if i == 2:
                widgets[item].set_visibility(False)
            hbox = gtk.HBox()
            hbox.pack_start(gtk.Label(item+': '))
            hbox.add(widgets[item])
            dialog.vbox.add(hbox)
        dialog.show_all()
        # run
        response = dialog.run()
        user = widgets['user'].get_text()
        password = widgets['password'].get_text()
        url = widgets['url'].get_text()
        dialog.destroy()
        if not response == gtk.RESPONSE_ACCEPT:
            return

        self.wiki = cfg.new(Dokuwiki, 
                    url=url,
                    user=user,
                    password=password)

        cfg.addChild(self.wiki)
        cfg.save()
        self.connect(url, user, password)

    def get_full_url(self, url, user, password):
      try:
        if user and password:
            split_url = url.split('://')
            proto = split_url[0]
            base_url = split_url[1]
            return proto + '://' + user + ':' + password + '@' + base_url
        return url
      except:
        traceback.print_exc()

    def connect(self, url, user, password):
        # following commented line is for gtkhtml (not used)
        #simplebrowser.currentUrl = self.view.url.get_text()
        # handle response
        self.post("Connecting to " + url)
        params = urlencode({'u':user, 'p':password})
        print self.get_full_url(url, user, password)
        fullurl = self.get_full_url(url, user, password) + "/lib/exe/xmlrpc.php?"+ params
        print "serverproxy1"
        self._rpc = ServerProxy(fullurl)
        print "serverproxy1"
        d = self.get_version()
        d.addCallback(self.connected)
        d.addErrback(self.error_connecting)

    def error_connecting(self, failure):
        self.post("Error connecting to " + self.wiki.url)
        print failure.getErrorMessage()

    def connected(self, version):
        print "connected1"
        self.view.version.set_text(version)
        print "connected1.5"
        self.get_pagelist()
        print "connected2"
        self.post("Connected")

    def on_delete_page__clicked(self, *args):
        dialog = ModalDialog("Are you sure?")
        response = dialog.run()
        if response == gtk.RESPONSE_ACCEPT:
            value = self._sections[self.wiki.current]
            sel = self.objectlist.remove(value)
            self._rpc.wiki.putPage(self.wiki.current, "", {})
            self.wiki.current = ''
            cfg.save()
        dialog.destroy()

    def on_new_page__clicked(self, *args):
        dialog = ModalDialog("Name for the new page")
        text_w = gtk.Entry()
        text_w.show()
        response = []
        dialog.vbox.add(text_w)
        response = dialog.run()
        if response == gtk.RESPONSE_ACCEPT:
            text = text_w.get_text()
            if text:
                self.wiki.current = text
                cfg.save()
                self.buffer.clear()
        dialog.destroy()

    def on_button_h1__clicked(self, *args):
        self.buffer.set_style('h1')

    def on_button_h2__clicked(self, *args):
        self.buffer.set_style('h2')

    def on_button_h3__clicked(self, *args):
        self.buffer.set_style('h3')

    def on_button_h4__clicked(self, *args):
        self.buffer.set_style('h4')

    def on_button_h5__clicked(self, *args):
        self.buffer.set_style('h5')

    def on_button_h6__clicked(self, *args):
        self.buffer.set_style('h6')

    def on_button_bold__clicked(self, *args):
        self.buffer.set_style('bold')

    def on_button_italic__clicked(self, *args):
        self.buffer.set_style('italic')

    def on_button_clear_style__clicked(self, *args):
        self.buffer.clear_style()

    def _pagePut(self, *args):
        if not self.wiki.current in self._sections:
            self.add_page({"id":self.wiki.current})
        self.get_htmlview(self.wiki.current)
        self.get_versions(self.wiki.current)
        self.post("Saved")

    def on_button_save__clicked(self, *args):
        """ Save button callback """
        dialog = ModalDialog("Commit message")
        entry = gtk.Entry()
        minor = gtk.CheckButton("Minor")
        dialog.vbox.add(gtk.Label("Your attention to detail\nis greatly appreciated"))
        dialog.vbox.add(entry)
        dialog.vbox.add(minor)
        dialog.show_all()
        response = dialog.run()
        if response == gtk.RESPONSE_ACCEPT:
            self.post("Saving...")
            text = self.buffer.process_text()
            self.throbber_icon.start()
            d = self.put_page(text, entry.get_text(), minor.get_active())
            d.addCallback(self._pagePut)
        dialog.destroy()

    # unused stuff
    def request_url(self, document, url, stream):
        f = simplebrowser.open_url(url)
        stream.write(f.read())

    def setup_htmlview_gtkhtml(self):
        # XXX not used now
        self.document = gtkhtml2.Document()
        self.document.connect('request_url', self.request_url)
        self.htmlview = gtkhtml2.View()
        self.htmlview.set_document(self.document)