-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathNotification.js
92 lines (85 loc) · 3.11 KB
/
Notification.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
/*
* Copyright (c) 2012-2017 Gnome Email Notifications contributors
*
* Gnome Email Notifications is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* Gnome Email Notifications is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with Gnome Documents; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Authors:
* Adam Jabłoński <jablona123@gmail.com>
* Shuming Chan <shuming0207@gmail.com>
*
*/
"use strict";
const Me = imports.misc.extensionUtils.getCurrentExtension();
const Console = Me.imports.console.Console;
const MessageTray = imports.ui.messageTray;
const {Gio, GLib, GObject} = imports.gi;
const escaped_one_to_xml_special_map = {
'&': '&',
''': "'",
'"': '"',
'<': '<',
'>': '>'
};
const unescape_regex = /("|'|<|>|&)/g;
/**
* A single notification in the message tray.
* @class Notification
*/
var Notification = GObject.registerClass(
class extends MessageTray.Notification {
/**
* Creates a notification in the specified source
* @param {Source} source - the source to create the notification in
* @param content - information to display in notification
* @param iconName - the name of the icon to display in the notification
*/
_init(source, content, iconName) {
try {
const date = new Date(content.date);
const title = Notification._unescapeXML(content.subject);
const gicon = new Gio.ThemedIcon({name: iconName});
let banner = Notification._unescapeXML(content.from);
const params = {
gicon: gicon
};
Notification._addDateTimeToParams(date, params);
super._init(source, title, banner, params);
} catch (err) {
const console = new Console();
console.error(err);
}
}
/**
* Unescapes special characters found in XML
* @param {?string} xmlString - the string to unescape
* @returns {string} - the unescaped string
* @private
*/
static _unescapeXML(xmlString) {
if (xmlString === null) return "";
return xmlString.replace(unescape_regex,
(str, item) => escaped_one_to_xml_special_map[item]);
}
/**
* Adds date and time to the params object as unix local time
* @param {Date} date - the date to add
* @param params - parameters for creating a {@link MessageTray.Notification}
* @private
*/
static _addDateTimeToParams(date, params) {
const unix_local = date.getTime() / 1000;
params.datetime = GLib.DateTime.new_from_unix_local(unix_local);
}
});