|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2011 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to add RSS feeds. |
|
8 """ |
|
9 |
|
10 import functools |
|
11 |
|
12 from PyQt6.QtCore import QUrl |
|
13 from PyQt6.QtWidgets import QDialog, QPushButton, QLabel |
|
14 |
|
15 from .Ui_FeedsDialog import Ui_FeedsDialog |
|
16 |
|
17 import UI.PixmapCache |
|
18 from UI.NotificationWidget import NotificationTypes |
|
19 |
|
20 |
|
21 class FeedsDialog(QDialog, Ui_FeedsDialog): |
|
22 """ |
|
23 Class implementing a dialog to add RSS feeds. |
|
24 """ |
|
25 def __init__(self, availableFeeds, browser, parent=None): |
|
26 """ |
|
27 Constructor |
|
28 |
|
29 @param availableFeeds list of available RSS feeds (list of tuple of |
|
30 two strings) |
|
31 @param browser reference to the browser widget (WebBrowserView) |
|
32 @param parent reference to the parent widget (QWidget) |
|
33 """ |
|
34 super().__init__(parent) |
|
35 self.setupUi(self) |
|
36 |
|
37 self.iconLabel.setPixmap(UI.PixmapCache.getPixmap("rss48")) |
|
38 |
|
39 self.__browser = browser |
|
40 |
|
41 self.__availableFeeds = availableFeeds[:] |
|
42 for row in range(len(self.__availableFeeds)): |
|
43 feed = self.__availableFeeds[row] |
|
44 button = QPushButton(self) |
|
45 button.setText(self.tr("Add")) |
|
46 button.feed = feed |
|
47 label = QLabel(self) |
|
48 label.setText(feed[0]) |
|
49 self.feedsLayout.addWidget(label, row, 0) |
|
50 self.feedsLayout.addWidget(button, row, 1) |
|
51 button.clicked.connect( |
|
52 functools.partial(self.__addFeed, button)) |
|
53 |
|
54 msh = self.minimumSizeHint() |
|
55 self.resize(max(self.width(), msh.width()), msh.height()) |
|
56 |
|
57 def __addFeed(self, button): |
|
58 """ |
|
59 Private slot to add a RSS feed. |
|
60 |
|
61 @param button reference to the feed button |
|
62 @type QPushButton |
|
63 """ |
|
64 urlString = button.feed[1] |
|
65 url = QUrl(urlString) |
|
66 if url.isRelative(): |
|
67 url = self.__browser.url().resolved(url) |
|
68 urlString = url.toDisplayString( |
|
69 QUrl.ComponentFormattingOption.FullyDecoded) |
|
70 |
|
71 if not url.isValid(): |
|
72 return |
|
73 |
|
74 title = (button.feed[0] if button.feed[0] |
|
75 else self.__browser.url().host()) |
|
76 |
|
77 from WebBrowser.WebBrowserWindow import WebBrowserWindow |
|
78 feedsManager = WebBrowserWindow.feedsManager() |
|
79 if feedsManager.addFeed(urlString, title, self.__browser.icon()): |
|
80 WebBrowserWindow.showNotification( |
|
81 UI.PixmapCache.getPixmap("rss48"), |
|
82 self.tr("Add RSS Feed"), |
|
83 self.tr("""The feed was added successfully.""")) |
|
84 else: |
|
85 WebBrowserWindow.showNotification( |
|
86 UI.PixmapCache.getPixmap("rss48"), |
|
87 self.tr("Add RSS Feed"), |
|
88 self.tr("""The feed was already added before."""), |
|
89 kind=NotificationTypes.WARNING, |
|
90 timeout=0) |
|
91 |
|
92 self.close() |