|
| 1 | +const Desklet = imports.ui.desklet; |
| 2 | +const St = imports.gi.St; |
| 3 | +const Soup = imports.gi.Soup; |
| 4 | +const Settings = imports.ui.settings; |
| 5 | +const GLib = imports.gi.GLib; |
| 6 | +const Gio = imports.gi.Gio; |
| 7 | + |
| 8 | +class NewsFeedDesklet extends Desklet.Desklet { |
| 9 | + constructor(metadata, deskletId) { |
| 10 | + super(metadata, deskletId); |
| 11 | + |
| 12 | + this.settings = new Settings.DeskletSettings(this, "newsfeed@Paul163-ai", deskletId); |
| 13 | + |
| 14 | + // Bind settings — use .bind(this) to ensure correct context in callbacks |
| 15 | + this.settings.bindProperty(Settings.BindingDirection.IN, "source-google", "useGoogle", this.onSettingsChanged.bind(this), null); |
| 16 | + this.settings.bindProperty(Settings.BindingDirection.IN, "source-bbc", "useBBC", this.onSettingsChanged.bind(this), null); |
| 17 | + this.settings.bindProperty(Settings.BindingDirection.IN, "source-mint", "useMint", this.onSettingsChanged.bind(this), null); |
| 18 | + this.settings.bindProperty(Settings.BindingDirection.IN, "story-count", "storyCount", this.onSettingsChanged.bind(this), null); |
| 19 | + this.settings.bindProperty(Settings.BindingDirection.IN, "desklet-width", "deskletWidth", this.onVisualsChanged.bind(this), null); |
| 20 | + this.settings.bindProperty(Settings.BindingDirection.IN, "update-interval", "updateInterval", this.onSettingsChanged.bind(this), null); |
| 21 | + |
| 22 | + this.sourceMap = { |
| 23 | + "useGoogle": { name: "Google News", url: "https://news.google.com/rss" }, |
| 24 | + "useBBC": { name: "BBC World", url: "https://feeds.bbci.co.uk/news/world/rss.xml" }, |
| 25 | + "useMint": { name: "Linux Mint Blog", url: "https://blog.linuxmint.com/?feed=rss2" } |
| 26 | + }; |
| 27 | + |
| 28 | + // Reuse a single Soup session for all requests |
| 29 | + this.session = new Soup.Session({ user_agent: 'Cinnamon-NewsDesklet/1.0' }); |
| 30 | + |
| 31 | + this.timeoutId = null; |
| 32 | + this.setupUI(); |
| 33 | + this.updateAllFeeds(); |
| 34 | + } |
| 35 | + |
| 36 | + setupUI() { |
| 37 | + this.container = new St.BoxLayout({ vertical: true, style_class: "news-container" }); |
| 38 | + this.container.set_width(this.deskletWidth); |
| 39 | + this.setContent(this.container); |
| 40 | + } |
| 41 | + |
| 42 | + onVisualsChanged() { |
| 43 | + this.container.set_width(this.deskletWidth); |
| 44 | + } |
| 45 | + |
| 46 | + onSettingsChanged() { |
| 47 | + if (this.timeoutId) { |
| 48 | + GLib.source_remove(this.timeoutId); |
| 49 | + this.timeoutId = null; |
| 50 | + } |
| 51 | + this.updateAllFeeds(); |
| 52 | + } |
| 53 | + |
| 54 | + // Override destroy() to prevent the timer firing after desklet removal |
| 55 | + destroy() { |
| 56 | + if (this.timeoutId) { |
| 57 | + GLib.source_remove(this.timeoutId); |
| 58 | + this.timeoutId = null; |
| 59 | + } |
| 60 | + super.destroy(); |
| 61 | + } |
| 62 | + |
| 63 | + updateAllFeeds() { |
| 64 | + this.container.destroy_all_children(); |
| 65 | + this.container.add_child(new St.Label({ text: "Updating feeds...", style_class: "status-label" })); |
| 66 | + |
| 67 | + let activeSources = []; |
| 68 | + for (let key in this.sourceMap) { |
| 69 | + if (this[key]) activeSources.push(this.sourceMap[key]); |
| 70 | + } |
| 71 | + |
| 72 | + if (activeSources.length === 0) { |
| 73 | + this.displayError("No sources selected."); |
| 74 | + this._scheduleNextUpdate(); |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + // Track completion manually instead of Promise.all so one failed |
| 79 | + // feed does not prevent the others from rendering. |
| 80 | + let results = []; |
| 81 | + let completed = 0; |
| 82 | + let total = activeSources.length; |
| 83 | + |
| 84 | + const onDone = () => { |
| 85 | + completed++; |
| 86 | + if (completed < total) return; |
| 87 | + |
| 88 | + // All fetches finished (success or failure) — render what we have |
| 89 | + this.container.destroy_all_children(); |
| 90 | + let anySuccess = false; |
| 91 | + results.forEach(r => { |
| 92 | + if (r.xml) { |
| 93 | + this.parseAndDisplay(r.name, r.xml); |
| 94 | + anySuccess = true; |
| 95 | + } else { |
| 96 | + // Show a per-source error row instead of wiping everything |
| 97 | + let errHeader = new St.Label({ |
| 98 | + text: r.name.toUpperCase() + " — failed to load", |
| 99 | + style_class: "error-label" |
| 100 | + }); |
| 101 | + this.container.add_child(errHeader); |
| 102 | + } |
| 103 | + }); |
| 104 | + |
| 105 | + if (!anySuccess) { |
| 106 | + this.displayError("All feeds failed to load."); |
| 107 | + } |
| 108 | + |
| 109 | + this._scheduleNextUpdate(); |
| 110 | + }; |
| 111 | + |
| 112 | + activeSources.forEach(source => { |
| 113 | + let entry = { name: source.name, xml: null }; |
| 114 | + results.push(entry); |
| 115 | + |
| 116 | + this.fetchFeed(source.url, |
| 117 | + (xml) => { |
| 118 | + entry.xml = xml; |
| 119 | + onDone(); |
| 120 | + }, |
| 121 | + (err) => { |
| 122 | + // entry.xml stays null — error row will be shown |
| 123 | + global.logError("NewsFeedDesklet: fetch failed for " + source.name + ": " + err); |
| 124 | + onDone(); |
| 125 | + } |
| 126 | + ); |
| 127 | + }); |
| 128 | + } |
| 129 | + |
| 130 | + _scheduleNextUpdate() { |
| 131 | + if (this.timeoutId) { |
| 132 | + GLib.source_remove(this.timeoutId); |
| 133 | + this.timeoutId = null; |
| 134 | + } |
| 135 | + this.timeoutId = GLib.timeout_add_seconds( |
| 136 | + GLib.PRIORITY_DEFAULT, |
| 137 | + this.updateInterval * 60, |
| 138 | + () => { |
| 139 | + this.timeoutId = null; |
| 140 | + this.updateAllFeeds(); |
| 141 | + return GLib.SOURCE_REMOVE; |
| 142 | + } |
| 143 | + ); |
| 144 | + } |
| 145 | + |
| 146 | + fetchFeed(url, onSuccess, onError) { |
| 147 | + let message = Soup.Message.new('GET', url); |
| 148 | + |
| 149 | + this.session.send_and_read_async(message, GLib.PRIORITY_DEFAULT, null, (session, result) => { |
| 150 | + try { |
| 151 | + const bytes = session.send_and_read_finish(result); |
| 152 | + // Decode Uint8Array properly — .toString() produces "1,2,3,..." |
| 153 | + const decoder = new TextDecoder('utf-8'); |
| 154 | + const xml = decoder.decode(bytes.get_data()); |
| 155 | + onSuccess(xml); |
| 156 | + } catch (e) { |
| 157 | + onError(e.message || String(e)); |
| 158 | + } |
| 159 | + }); |
| 160 | + } |
| 161 | + |
| 162 | + displayError(message) { |
| 163 | + this.container.destroy_all_children(); |
| 164 | + this.container.add_child(new St.Label({ text: message, style_class: "error-label" })); |
| 165 | + } |
| 166 | + |
| 167 | + parseAndDisplay(sourceName, xml) { |
| 168 | + const itemRegex = /<item>([\s\S]*?)<\/item>/g; |
| 169 | + let match; |
| 170 | + let count = 0; |
| 171 | + |
| 172 | + let sourceHeader = new St.Label({ text: sourceName.toUpperCase(), style_class: "source-header" }); |
| 173 | + this.container.add_child(sourceHeader); |
| 174 | + |
| 175 | + while ((match = itemRegex.exec(xml)) !== null && count < this.storyCount) { |
| 176 | + let itemContent = match[1]; |
| 177 | + |
| 178 | + let title = itemContent.match(/<title>([\s\S]*?)<\/title>/)?.[1] || "Untitled"; |
| 179 | + let link = itemContent.match(/<link>([\s\S]*?)<\/link>/)?.[1] || ""; |
| 180 | + |
| 181 | + // Strip CDATA and decode HTML entities from both title and link |
| 182 | + title = this._cleanText(title); |
| 183 | + link = this._cleanText(link).trim(); |
| 184 | + |
| 185 | + // Skip items with empty or obviously invalid links |
| 186 | + if (!link || link === "#") { |
| 187 | + // Try <guid> as a fallback URL |
| 188 | + let guid = itemContent.match(/<guid[^>]*>([\s\S]*?)<\/guid>/)?.[1] || ""; |
| 189 | + guid = this._cleanText(guid).trim(); |
| 190 | + if (guid.startsWith("http")) link = guid; |
| 191 | + } |
| 192 | + |
| 193 | + let btn = new St.Button({ |
| 194 | + label: " • " + title, |
| 195 | + style_class: "news-button", |
| 196 | + reactive: true, |
| 197 | + x_align: St.Align.START |
| 198 | + }); |
| 199 | + |
| 200 | + // Capture link in closure |
| 201 | + const finalLink = link; |
| 202 | + btn.connect('clicked', () => { |
| 203 | + if (finalLink) { |
| 204 | + try { |
| 205 | + Gio.app_info_launch_default_for_uri(finalLink, null); |
| 206 | + } catch (e) { |
| 207 | + global.logError("NewsFeedDesklet: could not open link: " + e); |
| 208 | + } |
| 209 | + } |
| 210 | + }); |
| 211 | + |
| 212 | + this.container.add_child(btn); |
| 213 | + count++; |
| 214 | + } |
| 215 | + |
| 216 | + this.container.add_child(new St.Label({ text: " ", style_class: "section-spacer" })); |
| 217 | + } |
| 218 | + |
| 219 | + _cleanText(str) { |
| 220 | + return str |
| 221 | + .replace(/<!\[CDATA\[([\s\S]*?)\]\]>/g, "$1") |
| 222 | + .replace(/&/g, "&") |
| 223 | + .replace(/"/g, '"') |
| 224 | + .replace(/</g, "<") |
| 225 | + .replace(/>/g, ">") |
| 226 | + .replace(/&#(\d+);/g, (_, code) => String.fromCharCode(Number(code))) |
| 227 | + .trim(); |
| 228 | + } |
| 229 | +} |
| 230 | + |
| 231 | +function main(metadata, deskletId) { |
| 232 | + return new NewsFeedDesklet(metadata, deskletId); |
| 233 | +} |
0 commit comments