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 eolie.define import App
14
15
16class WebViewStateStruct:
17    def __init__(self):
18        self.uri = ""
19        self.title = ""
20        self.atime = 0
21        self.is_ephemeral = False
22        self.session = None
23
24
25class WebViewState:
26    """
27        Web view state allowing to restore a webview from disk
28    """
29
30    def new_from_state(state, window):
31        """
32            New webview from state
33            @param state as WebViewStateStruct
34            @param window as Window
35        """
36        from eolie.webview import WebView
37        if state.is_ephemeral:
38            webview = WebView.new_ephemeral(window)
39        else:
40            webview = WebView.new(window)
41        webview.set_uri(state.uri)
42        webview.set_title(state.title)
43        webview.set_atime(state.atime)
44        # TODO session
45        return webview
46
47    def __init__(self):
48        """
49            Init state
50        """
51        pass
52
53    @property
54    def state(self):
55        """
56            Get state
57            @return WebViewStateStruct
58        """
59        if App().settings.get_value("remember-session"):
60            state = WebViewStateStruct()
61            state.uri = self.uri
62            state.title = self.title
63            state.atime = self.atime
64            state.is_ephemeral = self.is_ephemeral
65            state.session = self.get_session_state().serialize().get_data()
66            return state
67        else:
68            return None
69