anotherim-desktop/plugins/windows-notification/src/plugin.vala

69 lines
2.7 KiB
Vala
Raw Normal View History

2020-10-24 22:05:35 +00:00
using Gee;
using Dino.Entities;
namespace Dino.Plugins.WindowsNotification {
public class Plugin : RootInterface, Object {
2020-10-25 11:07:31 +00:00
private Dino.Application app;
private ulong signal_handler = 0;
2020-10-25 12:06:23 +00:00
private WinToast toaster;
2020-10-24 22:05:35 +00:00
2020-10-25 14:09:46 +00:00
private void onclick_callback(int conv_id) {
this.app.activate_action("open-conversation", conv_id);
2020-10-24 22:05:35 +00:00
}
public void registered(Dino.Application app) {
this.app = app;
2020-10-25 14:09:46 +00:00
this.toaster = new WinToast();
2020-10-25 12:06:23 +00:00
if (toaster.valid) {
signal_handler = app.stream_interactor.get_module(NotificationEvents.IDENTITY).notify_content_item.connect(on_notify);
2020-10-24 22:30:57 +00:00
}
2020-10-24 22:05:35 +00:00
}
public void shutdown() {
2020-10-25 11:07:31 +00:00
if (signal_handler > 0) {
app.stream_interactor.get_module(NotificationEvents.IDENTITY).notify_content_item.disconnect(on_notify);
}
2020-10-24 22:05:35 +00:00
}
2020-10-25 11:07:31 +00:00
private void on_notify(ContentItem content_item, Conversation conversation) {
string display_name = Dino.Ui.Util.get_conversation_display_name(app.stream_interactor, conversation);
string text = "";
switch (content_item.type_) {
case MessageItem.TYPE:
var message_item = (content_item as MessageItem);
if (message_item != null) {
Message message = message_item.message;
if (message != null) {
text = message.body;
}
}
break;
case FileItem.TYPE:
FileItem file_item = content_item as FileItem;
if (file_item != null) {
FileTransfer transfer = file_item.file_transfer;
bool file_is_image = transfer.mime_type != null && transfer.mime_type.has_prefix("image");
if (transfer.direction == Message.DIRECTION_SENT) {
text = file_is_image ? "Image sent" : "File sent";
} else {
text = file_is_image ? "Image received" : "File received";
}
}
break;
}
if (app.stream_interactor.get_module(MucManager.IDENTITY).is_groupchat(conversation.counterpart, conversation.account)) {
string muc_occupant = Dino.Ui.Util.get_participant_display_name(app.stream_interactor, conversation, content_item.jid);
text = @"$muc_occupant: $text";
}
var avatar_manager = app.stream_interactor.get_module(AvatarManager.IDENTITY);
var avatar = avatar_manager.get_avatar_filepath(conversation.account, conversation.counterpart);
if (!toaster.show_message(display_name, text, avatar, conversation.id, onclick_callback)) {
2020-10-25 11:07:31 +00:00
stderr.printf("Error sending notification.");
};
}
2020-10-24 22:05:35 +00:00
}
}