def __init__(self, daddy, title=_("My Inventories")):
        gtk.Notebook.__init__(self)

        self.daddy = daddy
        self.running_scans = {}
        self.scans_timer = -1
        self.invdataload = InventoryLoad()
        self.invdata = None
        self.show_hosts_by = HOSTNAME
        # the following dict will have a format like this when data loads:
        # {inventoryname: {somedisplay:someip_or_info, ...}, ...}
        # it is a way to make a relation between what is been displayed and
        # its correspondent ipv4 in database. This is used to display
        # hostnames instead of ipv4 address for example.
        self.show_related_to = {}

        self.tooltips = gtk.Tooltips()
        self.control_title = gtk.Label("%s (0)" % title)
        self.treestore = gtk.TreeStore(gtk.gdk.Pixbuf, str)
        self.treeview = gtk.TreeView(self.treestore)
        self.tcolumn = gtk.TreeViewColumn("%s (0)" % title)

        # icon
        render_pixbuf = gtk.CellRendererPixbuf()
        self.tcolumn.pack_start(render_pixbuf, expand=False)
        self.tcolumn.add_attribute(render_pixbuf, 'pixbuf', 0)
        #self.tcolumn.add_attribute(render_pixbuf, 'pixbuf-expander-closed', 0)
        #self.tcolumn.add_attribute(render_pixbuf, 'pixbuf-expander-open', 1)

        # inventory/host/info names
        render_text = gtk.CellRendererText()
        self.tcolumn.pack_start(render_text, expand=True)
        self.tcolumn.add_attribute(render_text, 'text', 1)

        self.treeview.append_column(self.tcolumn)

        self.treeview.connect('row-activated', self._row_activated)
        self.treeview.connect('button-press-event', self._row_clicked)

        self.__set_props()
        self.__do_layout()
    def __init__(self, daddy, title=_("My Inventories")):
        gtk.Notebook.__init__(self)

        self.daddy = daddy
        self.running_scans = { }
        self.scans_timer = -1
        self.invdataload = InventoryLoad()
        self.invdata = None
        self.show_hosts_by = HOSTNAME
        # the following dict will have a format like this when data loads:
        # {inventoryname: {somedisplay:someip_or_info, ...}, ...}
        # it is a way to make a relation between what is been displayed and
        # its correspondent ipv4 in database. This is used to display
        # hostnames instead of ipv4 address for example.
        self.show_related_to = { }

        self.tooltips = gtk.Tooltips()
        self.control_title = gtk.Label("%s (0)" % title)
        self.treestore = gtk.TreeStore(gtk.gdk.Pixbuf, str)
        self.treeview = gtk.TreeView(self.treestore)
        self.tcolumn = gtk.TreeViewColumn("%s (0)" % title)

        # icon
        render_pixbuf = gtk.CellRendererPixbuf()
        self.tcolumn.pack_start(render_pixbuf, expand=False)
        self.tcolumn.add_attribute(render_pixbuf, 'pixbuf', 0)
        #self.tcolumn.add_attribute(render_pixbuf, 'pixbuf-expander-closed', 0)
        #self.tcolumn.add_attribute(render_pixbuf, 'pixbuf-expander-open', 1)

        # inventory/host/info names
        render_text = gtk.CellRendererText()
        self.tcolumn.pack_start(render_text, expand=True)
        self.tcolumn.add_attribute(render_text, 'text', 1)

        self.treeview.append_column(self.tcolumn)

        self.treeview.connect('row-activated', self._row_activated)
        self.treeview.connect('button-press-event', self._row_clicked)

        self.__set_props()
        self.__do_layout()
class InventoryTree(gtk.Notebook):
    """
    A Notebook that holds a ScrolledWindow that holds a TreeView that holds
    all inventories in database.
    """

    def __init__(self, daddy, title=_("My Inventories")):
        gtk.Notebook.__init__(self)

        self.daddy = daddy
        self.running_scans = { }
        self.scans_timer = -1
        self.invdataload = InventoryLoad()
        self.invdata = None
        self.show_hosts_by = HOSTNAME
        # the following dict will have a format like this when data loads:
        # {inventoryname: {somedisplay:someip_or_info, ...}, ...}
        # it is a way to make a relation between what is been displayed and
        # its correspondent ipv4 in database. This is used to display
        # hostnames instead of ipv4 address for example.
        self.show_related_to = { }

        self.tooltips = gtk.Tooltips()
        self.control_title = gtk.Label("%s (0)" % title)
        self.treestore = gtk.TreeStore(gtk.gdk.Pixbuf, str)
        self.treeview = gtk.TreeView(self.treestore)
        self.tcolumn = gtk.TreeViewColumn("%s (0)" % title)

        # icon
        render_pixbuf = gtk.CellRendererPixbuf()
        self.tcolumn.pack_start(render_pixbuf, expand=False)
        self.tcolumn.add_attribute(render_pixbuf, 'pixbuf', 0)
        #self.tcolumn.add_attribute(render_pixbuf, 'pixbuf-expander-closed', 0)
        #self.tcolumn.add_attribute(render_pixbuf, 'pixbuf-expander-open', 1)

        # inventory/host/info names
        render_text = gtk.CellRendererText()
        self.tcolumn.pack_start(render_text, expand=True)
        self.tcolumn.add_attribute(render_text, 'text', 1)

        self.treeview.append_column(self.tcolumn)

        self.treeview.connect('row-activated', self._row_activated)
        self.treeview.connect('button-press-event', self._row_clicked)

        self.__set_props()
        self.__do_layout()


    def set_title(self, title):
        """
        Set new title.
        """
        self.tcolumn.set_title(title)
        self.control_title.set_label(title)


    def set_show_hosts_by(self, event, opt):
        """
        Set new way to show hosts.
        """
        if event and not event.get_active():
            return

        self.show_hosts_by = opt
        self.fill_tree()


    def expand_tree(self, event):
        """
        Expand tree.
        """
        self.treeview.expand_all()


    def collapse_tree(self, event):
        """
        Collapse tree.
        """
        self.treeview.collapse_all()


    def fill_tree(self):
        """
        Fill inventory tree list and return the inventories that filled it.
        """
        self.invdata = self.invdataload.load_from_db()

        # need to clean these because tree is refilled several times.
        tr = self.treestore
        tr.clear()
        self.show_related_to = { }

        invs = [ ] # temporary place for inventory names
        for inventory, addrs in self.invdata.items():
            invs.append(inventory)

            root = tr.append(None, [self.render_icon(NI_INVENTORY_ROOT_ICON,
                gtk.ICON_SIZE_MENU), '%s' % inventory])

            relations = { }
            if addrs:
                tr.append(root, [self.render_icon(
                    gtk.STOCK_INFO, gtk.ICON_SIZE_MENU), INVENTORY_INFO])
                relations[INVENTORY_INFO] = INVENTORY_INFO

            for addr in addrs: # append hosts to the tree
                # addr[4] holds os short text for current host

                append_now = addr[0] # fallback to ipv4 in case something else
                                     # is selected but not present

                if self.show_hosts_by == HOSTNAME: # default mode
                    if addr[3] and addr[3][0]: # check for hostnames existence
                        append_now = addr[3][0] # show first hostname

                elif self.show_hosts_by == IPV6:
                    if addr[1]: # check for ipv6 existence
                        append_now = addr[1]

                elif self.show_hosts_by == MAC:
                    if addr[2]: # check for mac existence
                        append_now =  addr[2]

                #if len(append_now) > 21:
                #    append_now = append_now[:20] + '..'

                relations[append_now] = addr[0]
                tr.append(root, [self.render_icon(
                    get_os_icon(addr[4]), gtk.ICON_SIZE_MENU), append_now])

            self.show_related_to[inventory] = relations

        self.set_title(_("My Inventories") + (" (%d)" % len(tr)))
        self.treeview.expand_all()

        return invs


    def _edit_inv(self, widget, inv):
        """
        Open Inventory for editing.
        """
        # checking for inventory existence in schemas file.
        schemas = ConfigParser()
        schemas.read(Path.sched_schemas)

        if not schemas.has_section(inv):
            dlg = NoScheduleDlg()
            dlg.run()
            dlg.destroy()
            return

        w = NewInventory(inv, edit_mode=True)
        w.show_all()


    def _get_command_from_schemas(self, inventory):
        """
        Get scan command from schemas profiles.
        """
        schemas = ConfigParser()
        schemas.read(Path.sched_schemas)

        try:
            scan_args = schemas.get(inventory, "command")
            return scan_args

        except NoSectionError, err:
            dlg = GenericAlert(_("Scan will not run!"),
                _("You tried running scan for Inventory") +
                (" %r,\n" % inventory) +
                _("but it has no data in Scheduler schemas file, neither in "
                    "database.\n\nError returned:") + (" %s" % err),
                buttons={1: (gtk.RESPONSE_OK, gtk.STOCK_OK)})
            dlg.run()
            dlg.destroy()
class InventoryTree(gtk.Notebook):
    """
    A Notebook that holds a ScrolledWindow that holds a TreeView that holds
    all inventories in database.
    """
    def __init__(self, daddy, title=_("My Inventories")):
        gtk.Notebook.__init__(self)

        self.daddy = daddy
        self.running_scans = {}
        self.scans_timer = -1
        self.invdataload = InventoryLoad()
        self.invdata = None
        self.show_hosts_by = HOSTNAME
        # the following dict will have a format like this when data loads:
        # {inventoryname: {somedisplay:someip_or_info, ...}, ...}
        # it is a way to make a relation between what is been displayed and
        # its correspondent ipv4 in database. This is used to display
        # hostnames instead of ipv4 address for example.
        self.show_related_to = {}

        self.tooltips = gtk.Tooltips()
        self.control_title = gtk.Label("%s (0)" % title)
        self.treestore = gtk.TreeStore(gtk.gdk.Pixbuf, str)
        self.treeview = gtk.TreeView(self.treestore)
        self.tcolumn = gtk.TreeViewColumn("%s (0)" % title)

        # icon
        render_pixbuf = gtk.CellRendererPixbuf()
        self.tcolumn.pack_start(render_pixbuf, expand=False)
        self.tcolumn.add_attribute(render_pixbuf, 'pixbuf', 0)
        #self.tcolumn.add_attribute(render_pixbuf, 'pixbuf-expander-closed', 0)
        #self.tcolumn.add_attribute(render_pixbuf, 'pixbuf-expander-open', 1)

        # inventory/host/info names
        render_text = gtk.CellRendererText()
        self.tcolumn.pack_start(render_text, expand=True)
        self.tcolumn.add_attribute(render_text, 'text', 1)

        self.treeview.append_column(self.tcolumn)

        self.treeview.connect('row-activated', self._row_activated)
        self.treeview.connect('button-press-event', self._row_clicked)

        self.__set_props()
        self.__do_layout()

    def set_title(self, title):
        """
        Set new title.
        """
        self.tcolumn.set_title(title)
        self.control_title.set_label(title)

    def set_show_hosts_by(self, event, opt):
        """
        Set new way to show hosts.
        """
        if event and not event.get_active():
            return

        self.show_hosts_by = opt
        self.fill_tree()

    def expand_tree(self, event):
        """
        Expand tree.
        """
        self.treeview.expand_all()

    def collapse_tree(self, event):
        """
        Collapse tree.
        """
        self.treeview.collapse_all()

    def fill_tree(self):
        """
        Fill inventory tree list and return the inventories that filled it.
        """
        self.invdata = self.invdataload.load_from_db()

        # need to clean these because tree is refilled several times.
        tr = self.treestore
        tr.clear()
        self.show_related_to = {}

        invs = []  # temporary place for inventory names
        for inventory, addrs in self.invdata.items():
            invs.append(inventory)

            root = tr.append(None, [
                self.render_icon(NI_INVENTORY_ROOT_ICON, gtk.ICON_SIZE_MENU),
                '%s' % inventory
            ])

            relations = {}
            if addrs:
                tr.append(root, [
                    self.render_icon(gtk.STOCK_INFO, gtk.ICON_SIZE_MENU),
                    INVENTORY_INFO
                ])
                relations[INVENTORY_INFO] = INVENTORY_INFO

            for addr in addrs:  # append hosts to the tree
                # addr[4] holds os short text for current host

                append_now = addr[0]  # fallback to ipv4 in case something else
                # is selected but not present

                if self.show_hosts_by == HOSTNAME:  # default mode
                    if addr[3] and addr[3][0]:  # check for hostnames existence
                        append_now = addr[3][0]  # show first hostname

                elif self.show_hosts_by == IPV6:
                    if addr[1]:  # check for ipv6 existence
                        append_now = addr[1]

                elif self.show_hosts_by == MAC:
                    if addr[2]:  # check for mac existence
                        append_now = addr[2]

                #if len(append_now) > 21:
                #    append_now = append_now[:20] + '..'

                relations[append_now] = addr[0]
                tr.append(root, [
                    self.render_icon(get_os_icon(addr[4]), gtk.ICON_SIZE_MENU),
                    append_now
                ])

            self.show_related_to[inventory] = relations

        self.set_title(_("My Inventories") + (" (%d)" % len(tr)))
        self.treeview.expand_all()

        return invs

    def _edit_inv(self, widget, inv):
        """
        Open Inventory for editing.
        """
        # checking for inventory existence in schemas file.
        schemas = ConfigParser()
        schemas.read(Path.sched_schemas)

        if not schemas.has_section(inv):
            dlg = NoScheduleDlg()
            dlg.run()
            dlg.destroy()
            return

        w = NewInventory(inv, edit_mode=True)
        w.show_all()

    def _get_command_from_schemas(self, inventory):
        """
        Get scan command from schemas profiles.
        """
        schemas = ConfigParser()
        schemas.read(Path.sched_schemas)

        try:
            scan_args = schemas.get(inventory, "command")
            return scan_args

        except NoSectionError, err:
            dlg = GenericAlert(
                _("Scan will not run!"),
                _("You tried running scan for Inventory") +
                (" %r,\n" % inventory) +
                _("but it has no data in Scheduler schemas file, neither in "
                  "database.\n\nError returned:") + (" %s" % err),
                buttons={1: (gtk.RESPONSE_OK, gtk.STOCK_OK)})
            dlg.run()
            dlg.destroy()