File: main_leaflet.py

package info (click to toggle)
gnome-feeds 2.2.0-5
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,520 kB
  • sloc: python: 5,369; sh: 93; xml: 28; makefile: 2
file content (154 lines) | stat: -rw-r--r-- 5,892 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from gettext import ngettext
from functools import reduce
from typing import Optional
from gfeeds.filter_view import FilterView
from gfeeds.feed_item import FeedItem
from gfeeds.stack_with_empty_state import StackWithEmptyState
from operator import or_
from subprocess import Popen
from gi.repository import GObject, Gtk, Adw, Gio
from gfeeds.sidebar import GFeedsSidebar
from gfeeds.webview import GFeedsWebView
from gfeeds.headerbar import LeftHeaderbar, RightHeaderbar
from gfeeds.confManager import ConfManager
from gfeeds.feeds_manager import FeedsManager


@Gtk.Template(resource_path='/org/gabmus/gfeeds/ui/main_leaflet.ui')
class MainLeaflet(Adw.Bin):
    __gtype_name__ = 'MainLeaflet'
    left_box = Gtk.Template.Child()
    right_box = Gtk.Template.Child()
    leaflet = Gtk.Template.Child()
    connection_bar: Gtk.InfoBar = Gtk.Template.Child()
    left_headerbar: LeftHeaderbar = Gtk.Template.Child()
    filter_view: FilterView = Gtk.Template.Child()
    searchbar: Gtk.SearchBar = Gtk.Template.Child()
    searchbar_entry: Gtk.SearchEntry = Gtk.Template.Child()
    filter_flap: Adw.Flap = Gtk.Template.Child()
    sidebar_stack: StackWithEmptyState = Gtk.Template.Child()
    sidebar: GFeedsSidebar = Gtk.Template.Child()
    webview: GFeedsWebView = Gtk.Template.Child()
    right_headerbar: RightHeaderbar = Gtk.Template.Child()

    def __init__(self):
        super().__init__()
        self.confman = ConfManager()
        self.feedman = FeedsManager()

        self.sidebar.listview_sw.connect_activate(
            self.on_sidebar_row_activated
        )
        self.filter_flap.bind_property(
            'reveal-flap', self.left_headerbar.filter_btn, 'active',
            GObject.BindingFlags.BIDIRECTIONAL
        )

        self.confman.connect(
            'gfeeds_filter_changed', self.on_filter_changed
        )
        self.searchbar_entry.connect(
            'changed',
            lambda entry: self.sidebar.set_search(entry.get_text())
        )
        self.searchbar.bind_property(
            'search-mode-enabled', self.left_headerbar.search_btn,
            'active', GObject.BindingFlags.BIDIRECTIONAL
        )

        self.feedman.connect(
            'feedmanager_refresh_end', self.on_refresh_end
        )
        self.feedman.connect(
            'feedmanager_online_changed',
            lambda _, value: self.connection_bar.set_revealed(not value)
        )

        self.on_leaflet_folded()

    def on_filter_changed(self, *_):
        self.left_headerbar.filter_btn.set_active(False)
        # reset vertical scroll position to 0
        adjustment = self.sidebar.listview_sw.get_vadjustment()
        adjustment.set_value(0)
        self.sidebar.listview_sw.set_vadjustment(adjustment)

    @Gtk.Template.Callback()
    def on_leaflet_folded(self, *_):
        rh = self.right_headerbar.right_headerbar
        lh = self.left_headerbar.left_headerbar
        if self.leaflet.get_folded():
            self.right_headerbar.back_btn.set_visible(True)
            rh.set_show_start_title_buttons(True)
            rh.set_show_end_title_buttons(True)
            lh.set_show_start_title_buttons(True)
            lh.set_show_end_title_buttons(True)
        else:
            self.right_headerbar.back_btn.set_visible(False)
            rh.set_show_start_title_buttons(False)
            rh.set_show_end_title_buttons(True)
            lh.set_show_start_title_buttons(True)
            lh.set_show_end_title_buttons(False)

    @Gtk.Template.Callback()
    def on_back_btn_clicked(self, *_):
        self.leaflet.set_visible_child(self.left_box)
        self.on_leaflet_folded()

    def on_view_mode_change(self, target):
        self.right_headerbar.on_view_mode_change(target)
        self.webview.change_view_mode(target)

    def on_refresh_end(self, *_):
        self.left_headerbar.errors_btn.set_visible(
            len(self.feedman.errors) > 0
        )
        self.sidebar.listview_sw.all_items_changed()
        self.sidebar.loading_revealer.set_running(False)
        if (
                self.confman.nconf.notify_new_articles and
                not self.get_root().is_active() and  # window is not focused
                self.feedman.new_items_num > 0
        ):
            notif_text = ngettext(
                '{0} new article', '{0} new articles',
                self.feedman.new_items_num
            ).format(self.feedman.new_items_num)
            notif = Gio.Notification.new(notif_text)
            notif.set_icon(Gio.ThemedIcon.new(
                'org.gabmus.gfeeds-symbolic'
            ))
            self.get_root().app.send_notification('new_articles', notif)

    def on_sidebar_row_activated(self, feed_item: Optional[FeedItem]):
        self.feedman.article_store.set_selected_article(feed_item)
        if not feed_item:
            return
        feed_item.read = True
        if (
                self.confman.nconf.open_youtube_externally and
                reduce(or_, [
                    f'://{pfx}' in feed_item.link  # type: ignore
                    for pfx in [
                        p + 'youtube.com'
                        for p in ('', 'www.', 'm.')
                    ]
                ])
        ):
            cmd_parts = [
                self.confman.nconf.media_player, f'"{feed_item.link}"'
            ]
            if self.confman.is_flatpak:
                cmd_parts.insert(0, 'flatpak-spawn --host')
            cmd = ' '.join(cmd_parts)
            Popen(cmd, shell=True)
            return
        self.webview.load_feeditem(feed_item)
        self.right_headerbar.set_article_title(
            feed_item.title
        )
        self.right_headerbar.extra_menu_btn.set_sensitive(True)
        self.leaflet.set_visible_child(self.right_box)
        self.on_leaflet_folded()
        self.sidebar.listview_sw.invalidate_filter()
        feed_item.emit('changed', '')