~entertainer-releases/entertainer/trunk

« back to all changes in this revision

Viewing changes to entertainerlib/gui/screens/rss.py

  • Committer: Matt Layman
  • Date: 2010-04-03 15:10:28 UTC
  • mfrom: (403.1.12 bye-feedz)
  • Revision ID: laymansterms.dev@gmail.com-20100403151028-fxbe7g64z129ohu6
All feed code is now removed. Lots of pylint cleanup too. (Matt Layman)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (c) 2009 Entertainer Developers - See COPYING - GPLv2
2
 
'''Rss - Screen displays RSS feeds'''
3
 
 
4
 
import pango
5
 
 
6
 
from entertainerlib.gui.screens.screen import Screen
7
 
from entertainerlib.gui.widgets.label import Label
8
 
from entertainerlib.gui.widgets.texture import Texture
9
 
from entertainerlib.gui.widgets.text_menu import TextMenu
10
 
from entertainerlib.gui.widgets.list_indicator import ListIndicator
11
 
from entertainerlib.backend.components.feeds.feed_utils import FeedEntryParser
12
 
 
13
 
class Rss(Screen):
14
 
    '''Screen displays RSS-feed titles and number of entries.'''
15
 
 
16
 
    def __init__(self, feed_library, move_to_new_screen_callback):
17
 
        Screen.__init__(self, 'Rss', move_to_new_screen_callback)
18
 
 
19
 
        self.feed_order = 'UNREAD' # Feeds ordered by number of unread entries
20
 
        self.theme = self.config.theme
21
 
        self.library = feed_library
22
 
        self.parser = FeedEntryParser()
23
 
 
24
 
        # Screen Title (Displayed at the bottom left corner)
25
 
        screen_title = Label(0.13, "screentitle", 0, 0.87, _("Headlines"))
26
 
        self.add(screen_title)
27
 
 
28
 
        # RSS Icon
29
 
        icon = Texture(self.theme.getImage("rss_icon"), 0.05, 0.07)
30
 
        self.add(icon)
31
 
 
32
 
        # RSS Title
33
 
        title = Label(0.0833, "title", 0.13, 0.07, _("RSS Feeds"))
34
 
        self.add(title)
35
 
 
36
 
        #We set the menus to None here so we can check if they exist later on
37
 
        self.menu = None
38
 
        self.context_menu = None
39
 
 
40
 
        # Display information box if there are not feeds available
41
 
        if self.library.is_empty() or len(self.library.get_feeds()) == 0:
42
 
            self._create_no_feeds_information()
43
 
        else:
44
 
            # Menus
45
 
            self._create_context_menu()
46
 
            self.menu = self._create_feed_menu()
47
 
            self.add(self.menu)
48
 
            self.li = None
49
 
            self._create_feed_menu_indicator()
50
 
            self.menu.active = True
51
 
            self.context_menu.active = False
52
 
 
53
 
            # Feeds Title
54
 
            feeds_title = Label(0.04167, "title", 0.3953, 0.2, _("Feeds"))
55
 
            self.add(feeds_title)
56
 
 
57
 
            # Unread Title
58
 
            unread_title = Label(0.04167, "title", 0.8053, 0.2, _("Unread"))
59
 
            self.add(unread_title)
60
 
 
61
 
            # Feed description text
62
 
            desc_text = self.parser.strip_tags(
63
 
                self.menu.selected_userdata.description)
64
 
 
65
 
            self.description = Label(0.03646, "text", 0.3953, 0.9, desc_text)
66
 
            self.description.set_ellipsize(pango.ELLIPSIZE_END)
67
 
            self.description.set_use_markup(True)
68
 
            self.description.set_size(0.5088, 0.0651)
69
 
            self.add(self.description)
70
 
 
71
 
            self.menu.connect('selected', self._handle_select)
72
 
            self.menu.connect('moved', self._display_selected_feed)
73
 
            self.menu.connect('activated', self._menu_activation)
74
 
            self.context_menu.connect('selected', self._handle_select)
75
 
            self.context_menu.connect('activated',
76
 
                self._context_menu_activation)
77
 
 
78
 
    def _create_feed_menu(self):
79
 
        """
80
 
        Create Feed-menu. This menu contains list of Feeds. It also displays
81
 
        number of unread entries per feed.
82
 
        """
83
 
        menu = TextMenu(0.3807, 0.2604, 0.5124, 0.0781)
84
 
 
85
 
        feeds = self.library.get_feeds(self.feed_order)
86
 
        feeds_list = [[self.parser.strip_tags(feed.title), \
87
 
            str(feed.get_number_of_unread()), feed] \
88
 
            for feed in feeds]
89
 
        menu.async_add(feeds_list)
90
 
 
91
 
        return menu
92
 
 
93
 
    def _create_feed_menu_indicator(self):
94
 
        '''Create a ListIndicator for feed menu.'''
95
 
        self.li = ListIndicator(0.7, 0.9, 0.2, 0.045, ListIndicator.VERTICAL)
96
 
        self.li.set_maximum(len(self.library.get_feeds(self.feed_order)))
97
 
        self.add(self.li)
98
 
 
99
 
    def _create_context_menu(self):
100
 
        """
101
 
        Create RSS-feed context menu and add it to the screen.
102
 
        This menu contains buttons for update and sorting.
103
 
        """
104
 
        self.context_menu = TextMenu(0.0732, 0.2604, 0.2196, 0.0781)
105
 
 
106
 
        self.context_menu.add_item(_("Update feeds"))
107
 
        self.context_menu.add_item(_("Mark all as read"))
108
 
        self.context_menu.add_item(_("Sort by name"))
109
 
        self.context_menu.add_item(_("Sort by unread"))
110
 
 
111
 
        self.context_menu.active = False
112
 
        self.add(self.context_menu)
113
 
 
114
 
    def _create_no_feeds_information(self):
115
 
        """
116
 
        Create textures and labels for information screen. This is displayed
117
 
        instead of feeds list if there are no feeds available. This screen helps
118
 
        users to add new feeds to the system.
119
 
        """
120
 
        # Create warning icon
121
 
        warning_icon = Texture(self.theme.getImage("warning_icon"), 0.28, 0.27)
122
 
        self.add(warning_icon)
123
 
 
124
 
        # Create warning title
125
 
        info_title = Label(0.0625, "title", 0.3367, 0.2709,
126
 
            _("No headlines available!"))
127
 
        self.add(info_title)
128
 
 
129
 
        # Create warning help text
130
 
        message = _(
131
 
            "To use headlines you have to add some RSS feeds with the Content "
132
 
            "management tool. To do this, start the Content management tool, "
133
 
            "open the 'RSS Feeds' tab and click the 'Add' button. Now you can "
134
 
            "write an RSS feed URL in the text entry. You can get RSS feed "
135
 
            "URLs from webpages that support RSS.")
136
 
        info = Label(0.0417, "menuitem_inactive", 0.2804, 0.45, message)
137
 
        info.set_size(0.5, 0.5208)
138
 
        self.add(info)
139
 
 
140
 
    def update(self, event=None):
141
 
        """
142
 
        Update screen widgets. This is called always when screen is poped from
143
 
        the screen history.
144
 
        """
145
 
        feeds = self.library.get_feeds(self.feed_order)
146
 
        for idx, item in enumerate(self.menu.items):
147
 
            feed = feeds[idx]
148
 
            item.userdata = feed
149
 
            item.update(self.parser.strip_tags(feed.title),
150
 
                str(feed.get_number_of_unread()))
151
 
 
152
 
    def _handle_up(self):
153
 
        '''Handle UserEvent.NAVIGATE_UP.'''
154
 
        if self.menu.active:
155
 
            self.menu.up()
156
 
        else:
157
 
            self.context_menu.up()
158
 
 
159
 
    def _handle_down(self):
160
 
        '''Handle UserEvent.NAVIGATE_DOWN.'''
161
 
        if self.menu.active:
162
 
            self.menu.down()
163
 
        else:
164
 
            self.context_menu.down()
165
 
 
166
 
    def _handle_left(self):
167
 
        '''Handle UserEvent.NAVIGATE_LEFT.'''
168
 
        if self.menu.active:
169
 
            self._context_menu_activation()
170
 
 
171
 
    def _handle_right(self):
172
 
        '''Handle UserEvent.NAVIGATE_RIGHT.'''
173
 
        if not self.menu.active:
174
 
            self._menu_activation()
175
 
 
176
 
    def _handle_select(self, event=None):
177
 
        '''Handle UserEvent.NAVIGATE_SELECT.'''
178
 
        if not self.menu.active:
179
 
            index = self.context_menu.selected_index
180
 
            if index == 0:
181
 
                # Send message to bus and update screen
182
 
                self.library.request_feed_update()
183
 
                self.update()
184
 
            elif index == 1:
185
 
                # Tell feedlibrary to mark all as read and then update screen
186
 
                self.library.mark_all_as_read()
187
 
                self.update()
188
 
            elif index == 2:
189
 
                # Sort feeds by title ASC and update screen
190
 
                self.feed_order = 'TITLE'
191
 
                self.update()
192
 
            elif index == 3:
193
 
                # Sort feeds by UNREAD DESC and update screen
194
 
                self.feed_order = 'UNREAD'
195
 
                self.update()
196
 
        else:
197
 
            # Feed selected from the menu. Change screen.
198
 
            feed = self.menu.selected_userdata
199
 
            kwargs = { 'feed' : feed }
200
 
            self.callback("feed", kwargs)
201
 
 
202
 
    def handle_user_event(self, event=None):
203
 
        '''Handle screen specific user events unless the library is empty.'''
204
 
        if self.library.is_empty() or len(self.library.get_feeds()) == 0:
205
 
            return
206
 
        else:
207
 
            Screen.handle_user_event(self, event)
208
 
 
209
 
    def _display_selected_feed(self, actor=None):
210
 
        '''Update of the list indicator and the description label'''
211
 
        self.li.set_current(self.menu.selected_index + 1)
212
 
        feed = self.menu.selected_userdata
213
 
        self.description.set_text(self.parser.strip_tags(feed.description))
214
 
 
215
 
    def _menu_activation(self, actor=None):
216
 
        '''Handle the menu activation'''
217
 
        self.menu.active = True
218
 
        self.context_menu.active = False
219
 
        self.li.show_position()
220
 
        self.description.show()
221
 
 
222
 
    def _context_menu_activation(self, actor=None):
223
 
        '''Handle the context menu activation'''
224
 
        self.menu.active = False
225
 
        self.context_menu.active = True
226
 
        self.li.hide_position()
227
 
        self.description.hide()
228