From 642ed6ab1b997ac307370083a6506d21ceba72c5 Mon Sep 17 00:00:00 2001 From: LAGonauta Date: Sun, 25 Oct 2020 09:06:23 -0300 Subject: [PATCH] Convert C code to Vala --- plugins/windows-notification/CMakeLists.txt | 26 +------ plugins/windows-notification/src/plugin.vala | 27 ++----- plugins/windows-notification/src/wintoast.c | 50 ------------- plugins/windows-notification/src/wintoast.h | 13 ---- .../windows-notification/src/wintoast.vala | 70 +++++++++++++++++++ 5 files changed, 76 insertions(+), 110 deletions(-) delete mode 100644 plugins/windows-notification/src/wintoast.c delete mode 100644 plugins/windows-notification/src/wintoast.h create mode 100644 plugins/windows-notification/src/wintoast.vala diff --git a/plugins/windows-notification/CMakeLists.txt b/plugins/windows-notification/CMakeLists.txt index d28fcaf7..3be2a506 100644 --- a/plugins/windows-notification/CMakeLists.txt +++ b/plugins/windows-notification/CMakeLists.txt @@ -11,6 +11,7 @@ SOURCES src/plugin.vala src/register_plugin.vala src/helper.vala + src/wintoast.vala CUSTOM_VAPIS ${CMAKE_BINARY_DIR}/exports/xmpp-vala.vapi ${CMAKE_BINARY_DIR}/exports/dino.vapi @@ -19,34 +20,9 @@ PACKAGES ${WINDOWS_NOTIFICATION_PACKAGES} ) -add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/exports/wintoast.h -COMMAND - cp ${CMAKE_CURRENT_SOURCE_DIR}/src/wintoast.h ${CMAKE_BINARY_DIR}/exports/wintoast.h -DEPENDS - ${CMAKE_CURRENT_SOURCE_DIR}/src/wintoast.h -COMMENT - Copy header file wintoast.h -) - -add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/exports/wintoast.c -COMMAND - cp ${CMAKE_CURRENT_SOURCE_DIR}/src/wintoast.c ${CMAKE_BINARY_DIR}/exports/wintoast.c -DEPENDS - ${CMAKE_CURRENT_SOURCE_DIR}/src/wintoast.c -COMMENT - Copy header file wintoast.c -) - -add_custom_target(wintoast -DEPENDS - ${CMAKE_BINARY_DIR}/exports/wintoast.h - ${CMAKE_BINARY_DIR}/exports/wintoast.c -) - add_definitions(${VALA_CFLAGS}) add_library(windows-notification SHARED ${WINDOWS_NOTIFICATION_VALA_C} src/wintoast.c) target_link_libraries(windows-notification libdino ${WINDOWS_NOTIFICATION_PACKAGES}) -add_dependencies(windows-notification wintoast) set_target_properties(windows-notification PROPERTIES PREFIX "") set_target_properties(windows-notification PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/plugins/) diff --git a/plugins/windows-notification/src/plugin.vala b/plugins/windows-notification/src/plugin.vala index 90bb9ac2..7dba54fa 100644 --- a/plugins/windows-notification/src/plugin.vala +++ b/plugins/windows-notification/src/plugin.vala @@ -6,21 +6,7 @@ public class Plugin : RootInterface, Object { private Dino.Application app; private ulong signal_handler = 0; - - [CCode (has_target = false)] - private delegate void notification_callback(void* conv); - - [CCode (cname = "load_library", cheader_filename = "wintoast.h")] - private static extern int load_library(); - - [CCode (cname = "init_library", cheader_filename = "wintoast.h")] - 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")] - private static extern int show_message(char* sender, char* message, char* imagePath, char* protocolName, void *conv); + private WinToast toaster; private void onclick_callback() { // TODO: @@ -33,19 +19,16 @@ public class Plugin : RootInterface, Object { public void registered(Dino.Application app) { this.app = app; - if (load_library() != 1 || - init_library(onclick_callback) != 1) { - return; + this.toaster = new WinToast(onclick_callback); + if (toaster.valid) { + signal_handler = app.stream_interactor.get_module(NotificationEvents.IDENTITY).notify_content_item.connect(on_notify); } - - signal_handler = app.stream_interactor.get_module(NotificationEvents.IDENTITY).notify_content_item.connect(on_notify); } public void shutdown() { if (signal_handler > 0) { app.stream_interactor.get_module(NotificationEvents.IDENTITY).notify_content_item.disconnect(on_notify); } - uninit_library(); } private void on_notify(ContentItem content_item, Conversation conversation) { @@ -81,7 +64,7 @@ public class Plugin : RootInterface, Object { } var avatar_manager = app.stream_interactor.get_module(AvatarManager.IDENTITY); var avatar = avatar_manager.get_avatar_filepath(conversation.account, conversation.counterpart); - if (show_message(display_name, text, avatar, null, this) != 0) { + if (!toaster.show_message(display_name, text, avatar, this)) { stderr.printf("Error sending notification."); }; } diff --git a/plugins/windows-notification/src/wintoast.c b/plugins/windows-notification/src/wintoast.c deleted file mode 100644 index 262d8ed3..00000000 --- a/plugins/windows-notification/src/wintoast.c +++ /dev/null @@ -1,50 +0,0 @@ -#include -#include - -#include "wintoast.h" - -static HMODULE library_handle = NULL; -static PidginWinToastLibInitType library_init = NULL; -static PidginWinToastLibShowMessageType library_show_message = NULL; - -int load_library() { - library_handle = LoadLibrary("PidginWinToastLib.dll"); - if (!library_handle) { - return FALSE; - } - - FARPROC function = GetProcAddress(library_handle, "pidginWinToastLibInit"); - if (!function) { - return FALSE; - } - library_init = (PidginWinToastLibInitType)function; - - function = GetProcAddress(library_handle, "pidginWinToastLibShowMessage"); - if (!function) { - return FALSE; - } - library_show_message = (PidginWinToastLibShowMessageType)function; - return TRUE; -} - -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) { - FreeLibrary(library_handle); - } -} - -int show_message(const char * sender, const char * message, const char * imagePath, const char * protocolName, void *conv) { - if (library_show_message) { - return library_show_message(sender, message, imagePath, protocolName, conv); - } - - return -1; -} \ No newline at end of file diff --git a/plugins/windows-notification/src/wintoast.h b/plugins/windows-notification/src/wintoast.h deleted file mode 100644 index 73d2a436..00000000 --- a/plugins/windows-notification/src/wintoast.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef WINTOAST -#define WINTOAST 1 - -typedef void(*ClickCallbackType)(void*); -typedef int(*PidginWinToastLibInitType)(ClickCallbackType); -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 \ No newline at end of file diff --git a/plugins/windows-notification/src/wintoast.vala b/plugins/windows-notification/src/wintoast.vala new file mode 100644 index 00000000..013fc50b --- /dev/null +++ b/plugins/windows-notification/src/wintoast.vala @@ -0,0 +1,70 @@ +namespace Dino.Plugins.WindowsNotification { + public class WinToast { + [CCode (has_target = false)] + private delegate void FunctionPointer(); + + [CCode (has_target = false)] + public delegate void NotificationCallback(void* conv); + + [CCode (has_target = false)] + private delegate int PidginWinToastLibInitType(NotificationCallback callback); + + [CCode (has_target = false)] + private delegate int PidginWinToastLibShowMessageType(char* sender, char* message, char* image_path, char* protocolName, void *conv); + + [CCode (cname = "LoadLibrary", cheader_filename = "libloaderapi.h")] + private static extern void* load_library(char* lib_name); + + [CCode (cname = "FreeLibrary", cheader_filename = "libloaderapi.h")] + private static extern int free_library(void* handle); + + [CCode (cname = "GetProcAddress", cheader_filename = "libloaderapi.h")] + private static extern FunctionPointer get_proc_address(void* lib_handle, char* func_name); + + private void* library_handle = null; + private PidginWinToastLibInitType library_init = null; + private PidginWinToastLibShowMessageType library_show_message = null; + + public bool valid { get; private set; } + + public WinToast(NotificationCallback callback) { + valid = load(); + if (valid) { + valid = library_init(callback) == 0; + } + } + + ~WinToast() { + if (library_handle != null) { + free_library(library_handle); + } + } + + public bool show_message(string sender, string message, string image_path, void *conv) { + if (valid && library_show_message != null) { + return library_show_message(sender, message, image_path, null, conv) == 0; + } + return false; + } + + private bool load() { + library_handle = load_library("PidginWinToastLib.dll"); + if (library_handle == null) { + return false; + } + + FunctionPointer function = get_proc_address(library_handle, "pidginWinToastLibInit"); + if (function == null) { + return false; + } + library_init = (PidginWinToastLibInitType)function; + + function = get_proc_address(library_handle, "pidginWinToastLibShowMessage"); + if (function == null) { + return false; + } + library_show_message = (PidginWinToastLibShowMessageType)function; + return true; + } + } +} \ No newline at end of file