1# Copyright (c) 2017-2021 Cedric Bellegarde <cedric.bellegarde@adishatz.org>
2# This program is free software: you can redistribute it and/or modify
3# it under the terms of the GNU General Public License as published by
4# the Free Software Foundation, either version 3 of the License, or
5# (at your option) any later version.
6# This program is distributed in the hope that it will be useful,
7# but WITHOUT ANY WARRANTY; without even the implied warranty of
8# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9# GNU General Public License for more details.
10# You should have received a copy of the GNU General Public License
11# along with this program. If not, see <http://www.gnu.org/licenses/>.
12
13from gi.repository import Gtk, GObject, Pango, GLib
14
15from eolie.helper_passwords import PasswordsHelper
16from eolie.define import App, MARGIN_SMALL
17from eolie.utils import emit_signal
18from eolie.logger import Logger
19
20
21class Row(Gtk.ListBoxRow):
22    """
23        A row
24    """
25
26    def __init__(self, scheme, host):
27        """
28            Init Row
29            @param scheme as str
30            @param host as str
31        """
32        Gtk.ListBoxRow.__init__(self)
33        self.__scheme = scheme
34        self.__host = host
35        label = Gtk.Label.new("%s://%s" % (scheme, host))
36        label.show()
37        label.set_halign(Gtk.Align.START)
38        label.set_property("margin", MARGIN_SMALL)
39        label.set_ellipsize(Pango.EllipsizeMode.END)
40        self.add(label)
41
42    @property
43    def scheme(self):
44        """
45            Get scheme
46            @return str
47        """
48        return self.__scheme
49
50    @property
51    def host(self):
52        """
53            Get host
54            @return str
55        """
56        return self.__host
57
58
59class NotificationsDialog(Gtk.Bin):
60    """
61        Show notifications authorizations
62    """
63
64    __gsignals__ = {
65        "destroy-me": (GObject.SignalFlags.RUN_FIRST, None, ())
66    }
67
68    def __init__(self):
69        """
70            Init dialog
71        """
72        Gtk.Bin.__init__(self)
73        self.__filter = ""
74        self.__helper = PasswordsHelper()
75        builder = Gtk.Builder()
76        builder.add_from_resource('/org/gnome/Eolie/DialogNotifications.ui')
77        builder.connect_signals(self)
78        self.__search_bar = builder.get_object("search_bar")
79        self.__remove_button = builder.get_object("remove_button")
80        self.__listbox = builder.get_object("listbox")
81        self.__listbox.set_filter_func(self.__filter_func)
82        self.__listbox.set_sort_func(self.__sort_func)
83        self.add(builder.get_object('widget'))
84        self.__populate()
85
86#######################
87# PROTECTED           #
88#######################
89    def _on_back_clicked(self, button):
90        """
91            Ask to be destroyed
92            @param button as Gtk.Button
93        """
94        emit_signal(self, "destroy-me")
95
96    def _on_search_changed(self, entry):
97        """
98            Update filter
99            @param entry as Gtk.Entry
100        """
101        self.__filter = entry.get_text()
102        self.__listbox.invalidate_filter()
103
104    def _on_remove_clicked(self, button):
105        """
106            Remove all passwords
107            @param button as Gtk.Button
108        """
109        values = list(App().settings.get_value("notification-domains"))
110        for row in self.__listbox.get_selected_rows():
111            values.remove("%s;%s" % (row.scheme, row.host))
112            row.destroy()
113        App().settings.set_value("notification-domains",
114                                 GLib.Variant("as", values))
115
116    def _on_row_selected(self, listbox, row):
117        """
118            Update clear button state
119            @param listbox as Gtk.ListBox
120            @param row as Gtk.ListBoxRow
121        """
122        self.__remove_button.set_sensitive(
123            len(listbox.get_selected_rows()) != 0)
124
125    def _on_search_toggled(self, button):
126        """
127            Show entry
128            @param button as Gtk.Button
129        """
130        self.__search_bar.set_search_mode(button.get_active())
131
132#######################
133# PRIVATE             #
134#######################
135    def __filter_func(self, row):
136        """
137            Filter rows
138            @param row as Row
139        """
140        return self.__filter in row.host
141
142    def __sort_func(self, row1, row2):
143        """
144            Sort rows
145            @param row1 as Row
146            @param row2 as Row
147        """
148        return row2.host < row1.host
149
150    def __populate(self):
151        """
152            Populate view
153        """
154        values = App().settings.get_value("notification-domains")
155        try:
156            for value in values:
157                (scheme, host) = value.split(";")
158                row = Row(scheme, host)
159                row.show()
160                self.__listbox.add(row)
161        except Exception as e:
162            Logger.error("NotificationsDialog::__populate(): %s", e)
163