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 Gio, GLib, Gtk
14
15from gettext import gettext as _
16
17from eolie.define import App
18
19
20class SitesMenu(Gtk.Grid):
21    """
22        Menu linked to a SitesManagerChild
23    """
24
25    def __init__(self, webviews, window):
26        """
27            Init menu
28            @param webviews as [WebView]
29            @param window as Window
30        """
31        self.__window = window
32        self.__webviews = webviews
33        Gtk.Grid.__init__(self)
34        self.set_margin_start(5)
35        self.set_margin_end(5)
36        self.set_margin_top(5)
37        self.set_margin_bottom(5)
38        self.set_orientation(Gtk.Orientation.VERTICAL)
39        # Page switcher
40        action = Gio.SimpleAction.new("switch_page",
41                                      GLib.VariantType.new("s"))
42        self.__window.add_action(action)
43        action.connect("activate",
44                       self.__on_action_activate)
45        for webview in webviews:
46            uri = webview.uri
47            if uri is None:
48                continue
49            title = webview.title
50            item = Gtk.ModelButton.new()
51            item.set_hexpand(True)
52            item.set_property("text", title)
53            item.set_action_name("win.switch_page")
54            item.set_action_target_value(GLib.Variant("s", str(webview)))
55            item.show()
56            self.add(item)
57        # Bottom section
58        separator = Gtk.Separator.new(Gtk.Orientation.HORIZONTAL)
59        separator.show()
60        self.add(separator)
61        # Move to
62        item = Gtk.ModelButton.new()
63        item.set_property("text", _("Move to"))
64        item.set_property("menu-name", "moveto")
65        item.show()
66        self.add(item)
67        # Modify UA
68        if App().settings.get_value("developer-extras"):
69            action = Gio.SimpleAction.new("user_agent")
70            action.connect("activate", self.__on_modify_ua_activate)
71            self.__window.add_action(action)
72            item = Gtk.ModelButton.new()
73            item.set_property("text", _("Modify user agent"))
74            item.set_action_name("win.user_agent")
75            item.show()
76            self.add(item)
77        # Pinned state
78        uri = webviews[0].uri
79        pinned = App().websettings.get("pinned", uri)
80        action = Gio.SimpleAction.new_stateful(
81                    "pinned",
82                    None,
83                    GLib.Variant.new_boolean(pinned))
84        action.connect("change-state",
85                       self.__on_pinned_change_state,
86                       uri)
87        self.__window.add_action(action)
88        item = Gtk.ModelButton.new()
89        item.set_property("text", _("Pinned site"))
90        item.set_action_name("win.pinned")
91        item.show()
92        self.add(item)
93        # Close site
94        action = Gio.SimpleAction.new("close_site")
95        action.connect("activate", self.__on_close_activate)
96        self.__window.add_action(action)
97        item = Gtk.ModelButton.new()
98        item.set_property("text", _("Close site"))
99        item.set_action_name("win.close_site")
100        item.show()
101        self.add(item)
102
103#######################
104# PRIVATE             #
105#######################
106    def __on_pinned_change_state(self, action, param, uri):
107        """
108            Set option value
109            @param action as Gio.SimpleAction
110            @param param as GLib.Variant
111            @param uri as str
112        """
113        action.set_state(param)
114        App().websettings.set("pinned", uri, param.get_boolean())
115
116    def __on_close_activate(self, action, param):
117        """
118            Close wanted page
119            @param action as Gio.SimpleAction
120            @param param as GLib.Variant
121        """
122        for webview in self.__webviews:
123            self.__window.container.try_close_webview(webview)
124
125    def __on_modify_ua_activate(self, action, param):
126        """
127            Show a dialog allowing user to update User Agent
128            @param action as Gio.SimpleAction
129            @param param as GLib.Variant
130        """
131        if self.__webviews:
132            from eolie.dialog_modify_ua import ModifyUADialog
133            dialog = ModifyUADialog(self.__webviews[0].uri, self.__window)
134            dialog.run()
135
136    def __on_action_activate(self, action, variant):
137        """
138            Switch view
139            @param action as Gio.SimpleAction
140            @param variant as GLib.Variant
141            @param view as View
142        """
143        webview_str = variant.get_string()
144        for webview in self.__window.container.webviews:
145            if webview_str == str(webview):
146                self.__window.container.set_visible_webview(webview)
147                break
148