Changeset View
Changeset View
Standalone View
Standalone View
plugins/welcomepage/qml/NewsFeed.qml
- This file was added.
1 | /* KDevelop | ||||
---|---|---|---|---|---|
2 | * | ||||
3 | * Copyright 2017 Kevin Funk <kfunk@kde.org> | ||||
4 | * | ||||
5 | * This program is free software; you can redistribute it and/or | ||||
6 | * modify it under the terms of the GNU General Public License | ||||
7 | * as published by the Free Software Foundation; either version 2 | ||||
8 | * of the License, or (at your option) any later version. | ||||
9 | * | ||||
10 | * This program is distributed in the hope that it will be useful, | ||||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
13 | * GNU General Public License for more details. | ||||
14 | * | ||||
15 | * You should have received a copy of the GNU General Public License | ||||
16 | * along with this program; if not, write to the Free Software | ||||
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||||
18 | * 02110-1301, USA. | ||||
19 | */ | ||||
20 | | ||||
21 | import QtQuick 2.0 | ||||
22 | import QtQuick.Controls 1.3 | ||||
23 | import QtQuick.Layouts 1.2 | ||||
24 | import QtQuick.XmlListModel 2.0 | ||||
25 | | ||||
26 | import "storage.js" as Storage | ||||
27 | | ||||
28 | ListView { | ||||
29 | id: root | ||||
30 | | ||||
31 | /// Update interval (in minutes) in which the news feed is polled | ||||
32 | property int updateInterval: 24 * 60 * 1000 // 24 hours | ||||
33 | /// Max age (in minutes) of a news entry so it is shown in the list view | ||||
34 | /// TODO: Implement me | ||||
35 | property int maxNewsAge: 3 * 30 * 24 * 60 // 3 months | ||||
36 | /// Max age (in minutes) of a news entry so it is considered 'new' (thus highlighted with a bold font) | ||||
37 | property int maxHighlightedNewsAge: 30 * 24 * 60 // a month | ||||
38 | | ||||
39 | readonly property string feedUrl: "https://www.kdevelop.org/news/feed" | ||||
40 | readonly property bool loading: newsFeedSyncModel.status === XmlListModel.Loading | ||||
41 | | ||||
42 | /// Returns a date parsed from the pubDate | ||||
43 | function parsePubDate(pubDate) { | ||||
44 | // We need to modify the pubDate read from the RSS feed | ||||
45 | // so the JavaScript Date object can interpret it | ||||
46 | var d = pubDate.replace(',','').split(' '); | ||||
47 | if (d.length != 6) | ||||
48 | return new Date(NaN); | ||||
49 | | ||||
50 | return new Date([d[0], d[2], d[1], d[3], d[4], 'GMT' + d[5]].join(' ')); | ||||
51 | } | ||||
52 | | ||||
53 | // there's no builtin function for this(?) | ||||
54 | function toMap(obj) { | ||||
55 | var map = {}; | ||||
56 | for (var k in obj) { | ||||
57 | map[k] = obj[k]; | ||||
58 | } | ||||
59 | return map; | ||||
60 | } | ||||
61 | | ||||
62 | function minutesSince(date) { | ||||
63 | return !isNaN(date) ? Math.floor(Number((new Date() - date)) / 60000) : -1; | ||||
64 | } | ||||
65 | | ||||
66 | function loadFromCache() { | ||||
67 | newsFeedOfflineModel.clear() | ||||
68 | | ||||
69 | var data = Storage.get("newsFeedOfflineModelData", null); | ||||
70 | if (data) { | ||||
71 | var newsEntries = JSON.parse(data); | ||||
72 | for (var i = 0; i < newsEntries.length; ++i) { | ||||
73 | newsFeedOfflineModel.append(newsEntries[i]); | ||||
74 | } | ||||
75 | } | ||||
76 | root.positionViewAtBeginning() | ||||
77 | } | ||||
78 | function saveToCache() { | ||||
79 | var newsEntries = []; | ||||
80 | for (var i = 0; i < newsFeedSyncModel.count; ++i) { | ||||
81 | var entry = newsFeedSyncModel.get(i); | ||||
82 | newsEntries.push(toMap(entry)); | ||||
83 | } | ||||
84 | Storage.set("newsFeedOfflineModelData", JSON.stringify(newsEntries)); | ||||
85 | Storage.set("newsFeedLastFetchDate", JSON.stringify(new Date())); | ||||
86 | } | ||||
87 | | ||||
88 | spacing: 10 | ||||
89 | | ||||
90 | // Note: this model is *not* attached to the the view -- it's merely used for fetching the RSS feed | ||||
91 | XmlListModel { | ||||
92 | id: newsFeedSyncModel | ||||
93 | | ||||
94 | property bool active: false | ||||
95 | | ||||
96 | source: active ? feedUrl : "" | ||||
97 | query: "/rss/channel/item" | ||||
98 | | ||||
99 | XmlRole { name: "title"; query: "title/string()" } | ||||
100 | XmlRole { name: "link"; query: "link/string()" } | ||||
101 | XmlRole { name: "pubDate"; query: "pubDate/string()" } | ||||
102 | | ||||
103 | onStatusChanged: { | ||||
104 | if (status == XmlListModel.Ready) { | ||||
105 | saveToCache(); | ||||
106 | loadFromCache(); | ||||
107 | } else if (status == XmlListModel.Error) { | ||||
108 | console.log("Failed to fetch news feed: " + errorString()); | ||||
109 | } | ||||
110 | } | ||||
111 | } | ||||
112 | | ||||
113 | model: ListModel { | ||||
114 | id: newsFeedOfflineModel | ||||
115 | } | ||||
116 | | ||||
117 | delegate: Column { | ||||
118 | id: feedDelegate | ||||
119 | | ||||
120 | readonly property date publicationDate: parsePubDate(model.pubDate) | ||||
121 | readonly property int ageInMinutes: minutesSince(publicationDate) | ||||
122 | readonly property bool isNew: ageInMinutes != -1 && ageInMinutes < maxHighlightedNewsAge | ||||
123 | readonly property string dateString: isNaN(publicationDate.getDate()) ? model.pubDate : publicationDate.toLocaleDateString() | ||||
124 | | ||||
125 | x: 10 | ||||
126 | width: parent.width - 2*x | ||||
127 | | ||||
128 | Link { | ||||
129 | width: parent.width | ||||
130 | | ||||
131 | text: model.title | ||||
132 | | ||||
133 | onClicked: Qt.openUrlExternally(model.link) | ||||
134 | } | ||||
135 | | ||||
136 | Label { | ||||
137 | width: parent.width | ||||
138 | | ||||
139 | font.bold: isNew | ||||
140 | font.pointSize: 8 | ||||
141 | color: disabledPalette.windowText | ||||
142 | | ||||
143 | text: isNew ? i18nc("Example: Tue, 03 Jan 2017 10:00:00 (new)", "%1 (new)", dateString) : dateString | ||||
144 | } | ||||
145 | } | ||||
146 | | ||||
147 | BusyIndicator { | ||||
148 | id: busyIndicator | ||||
149 | | ||||
150 | height: newsHeading.height | ||||
151 | | ||||
152 | running: newsFeed.loading | ||||
153 | } | ||||
154 | | ||||
155 | Label { | ||||
156 | id: placeHolderLabel | ||||
157 | | ||||
158 | x: 10 | ||||
159 | width: parent.width - 2*x | ||||
160 | | ||||
161 | text: i18n("No recent news") | ||||
162 | color: disabledPalette.windowText | ||||
163 | visible: root.count === 0 && !root.loading | ||||
164 | | ||||
165 | Behavior on opacity { NumberAnimation {} } | ||||
166 | } | ||||
167 | | ||||
168 | SystemPalette { | ||||
169 | id: disabledPalette | ||||
170 | colorGroup: SystemPalette.Disabled | ||||
171 | } | ||||
172 | | ||||
173 | function fetchFeed() { | ||||
174 | console.log("Fetching news feed") | ||||
175 | | ||||
176 | newsFeedSyncModel.active = true | ||||
177 | newsFeedSyncModel.reload() | ||||
178 | } | ||||
179 | | ||||
180 | Timer { | ||||
181 | id: delayedStartupTimer | ||||
182 | | ||||
183 | // delay loading a bit so it has no effect on the KDevelop startup | ||||
184 | interval: 3000 | ||||
185 | running: true | ||||
186 | | ||||
187 | onTriggered: { | ||||
188 | // only fetch feed if items are out of date | ||||
189 | var lastFetchDate = new Date(JSON.parse(Storage.get("newsFeedLastFetchDate", null))); | ||||
190 | if (minutesSince(lastFetchDate) > root.updateInterval) { | ||||
191 | console.log("Last fetch of news feed was on " + lastFetchDate + ", updating now"); | ||||
192 | root.fetchFeed(); | ||||
193 | } | ||||
194 | } | ||||
195 | } | ||||
196 | | ||||
197 | Timer { | ||||
198 | id: reloadFeedTimer | ||||
199 | | ||||
200 | interval: root.updateInterval | ||||
201 | running: true | ||||
202 | repeat: true | ||||
203 | | ||||
204 | onTriggered: root.fetchFeed() | ||||
205 | } | ||||
206 | | ||||
207 | Component.onCompleted: loadFromCache() | ||||
208 | } |