2020-10-24 22:05:35 +00:00
|
|
|
using Dino.Entities;
|
2021-02-18 09:24:53 +00:00
|
|
|
using Dino.Plugins.WindowsNotification.Vapi;
|
2021-02-24 01:05:02 +00:00
|
|
|
using winrt.Windows.UI.Notifications;
|
2021-02-26 11:31:07 +00:00
|
|
|
using Xmpp;
|
2020-10-24 22:05:35 +00:00
|
|
|
|
|
|
|
namespace Dino.Plugins.WindowsNotification {
|
|
|
|
public class Plugin : RootInterface, Object {
|
|
|
|
|
2021-02-18 09:24:53 +00:00
|
|
|
private static string AUMID = "org.dino.Dino";
|
2021-02-24 01:05:02 +00:00
|
|
|
private ToastNotifier notifier;
|
|
|
|
private ToastNotification notification; // Notifications remove their actions when they get out of scope
|
2021-02-18 09:24:53 +00:00
|
|
|
|
2021-02-26 11:31:07 +00:00
|
|
|
private delegate void NodeFunction(StanzaNode node);
|
|
|
|
|
|
|
|
private StanzaNode Build(string xml) {
|
|
|
|
var reader = new Xmpp.StanzaReader.for_string(xml);
|
|
|
|
|
|
|
|
StanzaNode root_node = null;
|
|
|
|
var loop = new MainLoop();
|
|
|
|
reader.read_node
|
|
|
|
.begin((obj, res) => {
|
|
|
|
root_node = reader.read_node.end(res);
|
|
|
|
loop.quit();
|
|
|
|
});
|
|
|
|
loop.run();
|
|
|
|
|
|
|
|
ExecuteOnAllSubNodes(root_node, (node) => {
|
|
|
|
node.ns_uri = "";
|
|
|
|
foreach (var attr in node.attributes){
|
|
|
|
attr.ns_uri = "";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return root_node;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Gee.ArrayList<StanzaNode> FindRecursive(StanzaNode node, string tag_name, Gee.List<StanzaAttribute>? attributes) {
|
|
|
|
var ret = new Gee.ArrayList<StanzaNode>();
|
|
|
|
FindRecursiveInternal(node, tag_name, attributes, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void FindRecursiveInternal(StanzaNode root_node, string tag_name, Gee.List<StanzaAttribute>? attributes, Gee.List<StanzaNode> list) {
|
|
|
|
if (root_node.name == tag_name) {
|
|
|
|
if (attributes != null) {
|
|
|
|
foreach (var attr in attributes) {
|
|
|
|
var node_attr = root_node.get_attribute_raw(attr.name, attr.ns_uri);
|
|
|
|
if (node_attr != null && node_attr.equals(attr)) {
|
|
|
|
list.add(root_node);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
list.add(root_node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach (var node in root_node.get_all_subnodes()) {
|
|
|
|
FindRecursiveInternal(node, tag_name, attributes, list);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private string ToXml(StanzaNode node) {
|
|
|
|
var namespace_state = new NamespaceState();
|
|
|
|
namespace_state.set_current("");
|
|
|
|
return node.to_xml(namespace_state);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ExecuteOnAllSubNodes(StanzaNode root_node, NodeFunction func) {
|
|
|
|
func(root_node);
|
|
|
|
foreach (var node in root_node.get_all_subnodes()) {
|
|
|
|
ExecuteOnAllSubNodes(node, func);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-24 22:05:35 +00:00
|
|
|
public void registered(Dino.Application app) {
|
2021-02-21 20:03:24 +00:00
|
|
|
if (!winrt.InitApartment())
|
2021-03-26 11:22:55 +00:00
|
|
|
{
|
2021-02-18 09:24:53 +00:00
|
|
|
// log error, return
|
2020-10-24 22:30:57 +00:00
|
|
|
}
|
2021-03-26 11:22:55 +00:00
|
|
|
|
2021-02-18 09:24:53 +00:00
|
|
|
if (!Win32Api.SetAppModelID(AUMID))
|
|
|
|
{
|
|
|
|
// log error, return
|
|
|
|
}
|
2021-03-26 11:22:55 +00:00
|
|
|
|
2021-02-18 09:24:53 +00:00
|
|
|
if (!ShortcutCreator.TryCreateShortcut(AUMID))
|
2021-03-26 11:22:55 +00:00
|
|
|
{
|
2021-02-18 09:24:53 +00:00
|
|
|
// log error, return
|
2021-03-26 11:22:55 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 09:24:53 +00:00
|
|
|
{
|
2021-02-26 11:31:07 +00:00
|
|
|
var give_me_template = ToastTemplateType.ToastText02;
|
|
|
|
var template = ToastNotificationManager.GetTemplateContent(give_me_template);
|
|
|
|
var node = Build(template);
|
|
|
|
{
|
|
|
|
var attributes = new Gee.ArrayList<StanzaAttribute>();
|
|
|
|
attributes.add(new StanzaAttribute.build("", "id", "1"));
|
|
|
|
attributes.add(new StanzaAttribute.build("", "id", "2"));
|
2021-02-24 23:26:31 +00:00
|
|
|
|
2021-02-26 11:31:07 +00:00
|
|
|
var nodes = FindRecursive(node, "text", attributes);
|
|
|
|
foreach (var node_ in nodes) {
|
|
|
|
var attr = node_.get_attribute_raw("id", "");
|
|
|
|
if (attr != null) {
|
|
|
|
if (attr.val == "1") {
|
|
|
|
node_.put_node(new StanzaNode.text("Header!"));
|
|
|
|
} else if (attr.val == "2") {
|
|
|
|
node_.put_node(new StanzaNode.text("Text!"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-24 01:05:02 +00:00
|
|
|
this.notifier = new ToastNotifier(AUMID);
|
2021-02-26 11:31:07 +00:00
|
|
|
this.notification = new ToastNotification(ToXml(node));
|
2021-02-24 01:05:02 +00:00
|
|
|
var token = notification.Activated((c, d) => {
|
2021-02-26 11:31:07 +00:00
|
|
|
stdout.printf("\nYay! Activated!\n");
|
|
|
|
stdout.flush();
|
2021-02-24 23:26:31 +00:00
|
|
|
});
|
|
|
|
|
2021-02-24 01:05:02 +00:00
|
|
|
notifier.Show(notification);
|
2021-02-21 20:03:24 +00:00
|
|
|
}
|
2021-02-18 09:24:53 +00:00
|
|
|
|
|
|
|
// var provider = new WindowsNotificationProvider(app, Win32Api.SupportsModernNotifications());
|
|
|
|
// app.stream_interactor.get_module(NotificationEvents.IDENTITY).register_notification_provider(provider);
|
2020-10-24 22:05:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void shutdown() {
|
2020-10-25 11:07:31 +00:00
|
|
|
}
|
2020-10-24 22:05:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|