-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
129 lines (109 loc) · 3.63 KB
/
extension.js
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
const Gio = imports.gi.Gio;
const Gtk = imports.gi.Gtk;
const Main = imports.ui.main;
const Mainloop = imports.mainloop;
const MessageTray = imports.ui.messageTray;
const PopupMenu = imports.ui.popupMenu;
const St = imports.gi.St;
const Lang = imports.lang;
const Extension = imports.misc.extensionUtils.getCurrentExtension();
const RssLocal = Extension.imports.rssLocal;
const RssItemStore = RssLocal.RssItemStore;
const RssRemote = Extension.imports.rssRemote;
const RssLoader = RssRemote.RssLoader;
const RssConverter = RssRemote.RssConverter;
function init() {
return new CernHotNewsExtension();
}
const CernHotNewsExtension = new Lang.Class({
Name: 'CernHotNewsExtension',
FEED_URL: 'http://cernalerts.web.cern.ch/cernalerts/?feed=cern%20hot%20news',
STORE_LOCATION: '.cern_hot_news',
REFRESH_TIME: 900,
MAX_NEW_ITEMS: 3,
_init: function() {
let theme = Gtk.IconTheme.get_default();
theme.append_search_path(Extension.path + '/icons');
this._newsStore = new RssItemStore(this.STORE_LOCATION);
this._newsLoader = new RssLoader(this.FEED_URL);
this._newsLoader.onSuccess = Lang.bind(this, function(newsXML) {
var news = new RssConverter(newsXML).extractRSSItems();
this._onNewsLoaded(news)
});
},
enable: function() {
this._loadNews();
},
disable: function() {
if (this._newsNotificationSource)
this._newsNotificationSource._remove();
Mainloop.source_remove(this._timeoutSourceId);
},
_loadNews: function() {
this._newsLoader.load();
this._scheduleNextLoad();
},
_scheduleNextLoad: function () {
if (this._timeoutSourceId != null)
Mainloop.source_remove(this._timeoutSourceId);
this._timeoutSourceId = Mainloop.timeout_add_seconds(this.REFRESH_TIME, Lang.bind(this, this._loadNews));
},
_onNewsLoaded: function(loadedNews) {
if (loadedNews.length > 0)
{
var newsNotInStore = this._newsStore.compareAndGetNewsNotInStore(loadedNews);
this._newsStore.storeNews(loadedNews);
if (newsNotInStore.length > 0)
{
this._createAndAttachNewsNotificationSourceIfRequired();
for (let index = 0; index < newsNotInStore.length && index < this.MAX_NEW_ITEMS; index++) {
this._newsNotificationSource.sendNewsNotification(newsNotInStore[index]);
}
}
}
},
_createAndAttachNewsNotificationSourceIfRequired: function() {
if (!this._newsNotificationSource)
{
this._newsNotificationSource = new NewsNotificationSource();
this._newsNotificationSource.onRemove = Lang.bind(this, function() {
this._newsNotificationSource = null
});
Main.messageTray.add(this._newsNotificationSource);
}
}
});
const NewsNotificationSource = new Lang.Class({
Name: 'NewsNotificationSource',
Extends: MessageTray.Source,
_init: function() {
this.parent('CERN Hot News', 'cern-white');
this.setTransient(true);
this.onRemove = function() {};
},
sendNewsNotification: function(newsItem) {
var notification = new MessageTray.Notification(
this,
newsItem.title,
newsItem.description + '\n' + newsItem.link,
{gicon: Gio.icon_new_for_string('c-white')}
);
this.notify(notification);
},
buildRightClickMenu: function() {
let item;
let rightClickMenu = new St.BoxLayout({
name: 'summary-right-click-menu',
vertical: true
});
item = new PopupMenu.PopupMenuItem(_("Remove"));
item.connect('activate', Lang.bind(this, this._remove));
rightClickMenu.add(item.actor);
return rightClickMenu;
},
_remove: function() {
this.destroy();
this.emit('done-displaying-content', false);
this.onRemove();
}
});