1# -*- coding: utf-8 -*-
2#
3# Copyright (C) 2007 Andrew Resch <andrewresch@gmail.com>
4# Copyright (C) 2008 Martijn Voncken <mvoncken@gmail.com>
5#
6# This file is part of Deluge and is licensed under GNU General Public License 3.0, or later, with
7# the additional special exception to link portions of this program with the OpenSSL library.
8# See LICENSE for more details.
9#
10
11from __future__ import unicode_literals
12
13import logging
14
15from gi.repository.Gtk import Label, PolicyType, ScrolledWindow
16
17import deluge.component as component
18from deluge.configmanager import ConfigManager
19
20log = logging.getLogger(__name__)
21
22
23class SideBar(component.Component):
24    """
25    manages the sidebar-tabs.
26    purpose : plugins
27    """
28
29    def __init__(self):
30        component.Component.__init__(self, 'SideBar')
31        main_builder = component.get('MainWindow').get_builder()
32        self.notebook = main_builder.get_object('sidebar_notebook')
33        self.config = ConfigManager('gtk3ui.conf')
34
35        # Tabs holds references to the Tab widgets by their name
36        self.tabs = {}
37
38        # Hide if necessary
39        self.visible(self.config['show_sidebar'])
40
41    def visible(self, visible):
42        self.notebook.show() if visible else self.notebook.hide()
43        self.config['show_sidebar'] = visible
44
45    def add_tab(self, widget, tab_name, label):
46        """Adds a tab object to the notebook."""
47        log.debug('add tab: %s', tab_name)
48        self.tabs[tab_name] = widget
49        scrolled = ScrolledWindow()
50        scrolled.set_policy(PolicyType.AUTOMATIC, PolicyType.AUTOMATIC)
51        scrolled.add(widget)
52        self.notebook.insert_page(scrolled, Label(label=label), -1)
53        scrolled.show_all()
54
55        self.after_update()
56
57    def remove_tab(self, tab_name):
58        """Removes a tab by name."""
59        self.notebook.remove_page(self.notebook.page_num(self.tabs[tab_name]))
60        del self.tabs[tab_name]
61
62        self.after_update()
63
64    def after_update(self):
65        # If there are no tabs visible, then do not show the notebook
66        if len(self.tabs) == 0:
67            self.visible(False)
68
69        # If there is 1 tab, hide the tab-headers
70        if len(self.tabs) == 1:
71            self.notebook.set_show_tabs(False)
72        else:
73            self.notebook.set_show_tabs(True)
74