Refactor windows-notification plugin

This commit is contained in:
LAGonauta 2020-10-24 19:30:57 -03:00
parent 1d4bb774a5
commit 65a26bce53
3 changed files with 54 additions and 41 deletions

View file

@ -16,11 +16,14 @@ public class Plugin : RootInterface, Object {
[CCode (has_target = false)] [CCode (has_target = false)]
private delegate void notification_callback(void* conv); private delegate void notification_callback(void* conv);
[CCode (cname = "init", cheader_filename = "wintoast.h")] [CCode (cname = "load_library", cheader_filename = "wintoast.h")]
private static extern int init(notification_callback callback); private static extern int load_library();
[CCode (cname = "uninit", cheader_filename = "wintoast.h")] [CCode (cname = "init_library", cheader_filename = "wintoast.h")]
private static extern void uninit(); private static extern int init_library(notification_callback callback);
[CCode (cname = "uninit_library", cheader_filename = "wintoast.h")]
private static extern void uninit_library();
[CCode (cname = "show_message", cheader_filename = "wintoast.h")] [CCode (cname = "show_message", cheader_filename = "wintoast.h")]
private static extern int show_message(char* sender, char* message, char* imagePath, char* protocolName, void *conv); private static extern int show_message(char* sender, char* message, char* imagePath, char* protocolName, void *conv);
@ -36,8 +39,10 @@ public class Plugin : RootInterface, Object {
public void registered(Dino.Application app) { public void registered(Dino.Application app) {
this.app = app; this.app = app;
if (load_library() != 1 ||
init(onclick_callback); init_library(onclick_callback) != 1) {
return;
}
app.stream_interactor.get_module(NotificationEvents.IDENTITY).notify_content_item.connect((item, conversation) => { app.stream_interactor.get_module(NotificationEvents.IDENTITY).notify_content_item.connect((item, conversation) => {
if (item.type_ == "message") { if (item.type_ == "message") {
@ -52,13 +57,15 @@ public class Plugin : RootInterface, Object {
//var message = item.encryption == Encryption.NONE ? message_item.message.body : "*encrypted*"; //var message = item.encryption == Encryption.NONE ? message_item.message.body : "*encrypted*";
var message = message_item.message.body; var message = message_item.message.body;
var sender = conversation.nickname != null ? conversation.nickname : conversation.counterpart.to_string(); var sender = conversation.nickname != null ? conversation.nickname : conversation.counterpart.to_string();
show_message(sender, message + "\n", "", "", this); if (show_message(sender, message, "", "", this) != 0) {
stderr.printf("Error sending notification.");
};
} }
}); });
} }
public void shutdown() { public void shutdown() {
uninit(); uninit_library();
} }
} }

View file

@ -3,47 +3,48 @@
#include "wintoast.h" #include "wintoast.h"
void(*callback)(void*) = NULL; static HMODULE library_handle = NULL;
static PidginWinToastLibInitType library_init = NULL;
static PidginWinToastLibShowMessageType library_show_message = NULL;
HMODULE library_handle = NULL; int load_library() {
typedef void(*ClickCallbackType)(void*);
typedef int(*PidginWinToastLibInitType)(ClickCallbackType);
typedef int(*PidginWinToastLibShowMessageType)(const char*, const char*, const char*, const char*, void*);
PidginWinToastLibInitType library_init = NULL;
PidginWinToastLibShowMessageType library_show_message = NULL;
void init(ClickCallbackType notification_click_callback) {
printf("Inicializando\n");
callback = notification_click_callback;
library_handle = LoadLibrary("PidginWinToastLib.dll"); library_handle = LoadLibrary("PidginWinToastLib.dll");
if (library_handle) { if (!library_handle) {
FARPROC function = GetProcAddress(library_handle, "pidginWinToastLibInit"); return FALSE;
if (function) {
library_init = (PidginWinToastLibInitType)function;
}
function = GetProcAddress(library_handle, "pidginWinToastLibShowMessage");
if (function) {
library_show_message = (PidginWinToastLibShowMessageType)function;
}
} }
if (library_init) { FARPROC function = GetProcAddress(library_handle, "pidginWinToastLibInit");
library_init(notification_click_callback); if (!function) {
return FALSE;
} }
library_init = (PidginWinToastLibInitType)function;
function = GetProcAddress(library_handle, "pidginWinToastLibShowMessage");
if (!function) {
return FALSE;
}
library_show_message = (PidginWinToastLibShowMessageType)function;
return TRUE;
} }
void uninit() { int init_library(ClickCallbackType notification_click_callback) {
if (!library_init) {
return FALSE;
}
library_init(notification_click_callback);
return TRUE;
}
void uninit_library() {
if (library_handle) { if (library_handle) {
FreeLibrary(library_handle); FreeLibrary(library_handle);
} }
} }
void show_message(const char * sender, const char * message, const char * imagePath, const char * protocolName, void *conv) { int show_message(const char * sender, const char * message, const char * imagePath, const char * protocolName, void *conv) {
if (library_show_message) { if (library_show_message) {
library_show_message(sender, message, imagePath, protocolName, conv); return library_show_message(sender, message, imagePath, protocolName, conv);
} }
return -1;
} }

View file

@ -1,8 +1,13 @@
#ifndef WINTOAST #ifndef WINTOAST
#define WINTOAST 1 #define WINTOAST 1
void init(void(*notification_click_callback)(void *conv)); typedef void(*ClickCallbackType)(void*);
void uninit(); typedef int(*PidginWinToastLibInitType)(ClickCallbackType);
void show_message(const char * sender, const char * message, const char * imagePath, const char * protocolName, void *conv); typedef int(*PidginWinToastLibShowMessageType)(const char*, const char*, const char*, const char*, void*);
int load_library();
int init_library(ClickCallbackType click_callback);
void uninit_library();
int show_message(const char * sender, const char * message, const char * imagePath, const char * protocolName, void *conv);
#endif #endif