2013-04-21 19:17:06 +00:00
|
|
|
/*
|
|
|
|
* notifier.c
|
2019-11-13 11:11:05 +00:00
|
|
|
* vim: expandtab:ts=4:sts=4:sw=4
|
2013-04-21 19:17:06 +00:00
|
|
|
*
|
2019-01-22 10:31:45 +00:00
|
|
|
* Copyright (C) 2012 - 2019 James Booth <boothj5@gmail.com>
|
2013-04-21 19:17:06 +00:00
|
|
|
*
|
|
|
|
* This file is part of Profanity.
|
|
|
|
*
|
|
|
|
* Profanity 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 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Profanity 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
|
2016-07-24 00:14:49 +00:00
|
|
|
* along with Profanity. If not, see <https://www.gnu.org/licenses/>.
|
2013-04-21 19:17:06 +00:00
|
|
|
*
|
2014-08-24 19:57:39 +00:00
|
|
|
* In addition, as a special exception, the copyright holders give permission to
|
|
|
|
* link the code of portions of this program with the OpenSSL library under
|
|
|
|
* certain conditions as described in each individual source file, and
|
|
|
|
* distribute linked combinations including the two.
|
|
|
|
*
|
|
|
|
* You must obey the GNU General Public License in all respects for all of the
|
|
|
|
* code used other than OpenSSL. If you modify file(s) with this exception, you
|
|
|
|
* may extend this exception to your version of the file(s), but you are not
|
|
|
|
* obligated to do so. If you do not wish to do so, delete this exception
|
|
|
|
* statement from your version. If you delete this exception statement from all
|
|
|
|
* source files in the program, then also delete it here.
|
|
|
|
*
|
2013-04-21 19:17:06 +00:00
|
|
|
*/
|
2016-03-31 20:05:02 +00:00
|
|
|
#include "config.h"
|
2013-04-21 19:17:06 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
2020-07-07 07:43:28 +00:00
|
|
|
#include <string.h>
|
2020-07-07 11:53:30 +00:00
|
|
|
#include <stdlib.h>
|
2013-04-21 19:17:06 +00:00
|
|
|
|
|
|
|
#include <glib.h>
|
2016-07-24 14:43:51 +00:00
|
|
|
|
2016-03-31 20:05:02 +00:00
|
|
|
#ifdef HAVE_LIBNOTIFY
|
2013-04-21 19:17:06 +00:00
|
|
|
#include <libnotify/notify.h>
|
|
|
|
#endif
|
2016-07-24 14:43:51 +00:00
|
|
|
|
2016-03-31 20:05:02 +00:00
|
|
|
#ifdef PLATFORM_CYGWIN
|
2013-04-21 22:39:16 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
2013-04-21 19:17:06 +00:00
|
|
|
|
2020-07-07 07:43:28 +00:00
|
|
|
#include "log.h"
|
2020-07-07 11:53:30 +00:00
|
|
|
#include "config/preferences.h"
|
2016-07-24 14:43:51 +00:00
|
|
|
#include "ui/ui.h"
|
|
|
|
#include "ui/window_list.h"
|
2020-07-07 07:43:28 +00:00
|
|
|
#include "xmpp/xmpp.h"
|
2020-07-07 11:53:30 +00:00
|
|
|
#include "xmpp/muc.h"
|
2013-04-21 19:17:06 +00:00
|
|
|
|
2020-07-07 12:18:57 +00:00
|
|
|
static GTimer* remind_timer;
|
2015-01-15 00:42:40 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
notifier_initialise(void)
|
|
|
|
{
|
|
|
|
remind_timer = g_timer_new();
|
|
|
|
}
|
|
|
|
|
2014-12-22 22:13:42 +00:00
|
|
|
void
|
|
|
|
notifier_uninit(void)
|
2013-04-21 19:17:06 +00:00
|
|
|
{
|
2016-03-31 20:05:02 +00:00
|
|
|
#ifdef HAVE_LIBNOTIFY
|
2013-04-21 19:17:06 +00:00
|
|
|
if (notify_is_initted()) {
|
|
|
|
notify_uninit();
|
|
|
|
}
|
|
|
|
#endif
|
2015-01-15 00:42:40 +00:00
|
|
|
g_timer_destroy(remind_timer);
|
2013-04-21 19:17:06 +00:00
|
|
|
}
|
|
|
|
|
2014-12-22 22:13:42 +00:00
|
|
|
void
|
2020-07-07 12:18:57 +00:00
|
|
|
notify_typing(const char* const name)
|
2013-04-21 19:17:06 +00:00
|
|
|
{
|
2021-10-05 08:01:27 +00:00
|
|
|
gchar* message = g_strdup_printf("%s: typing...", name);
|
2016-02-14 22:28:55 +00:00
|
|
|
notify(message, 10000, "Incoming message");
|
2021-09-09 07:48:04 +00:00
|
|
|
g_free(message);
|
2013-04-21 19:17:06 +00:00
|
|
|
}
|
|
|
|
|
2014-12-22 22:13:42 +00:00
|
|
|
void
|
2020-07-07 12:18:57 +00:00
|
|
|
notify_invite(const char* const from, const char* const room, const char* const reason)
|
2013-04-22 22:48:23 +00:00
|
|
|
{
|
2020-07-07 12:18:57 +00:00
|
|
|
GString* message = g_string_new("Room invite\nfrom: ");
|
2013-04-22 22:48:23 +00:00
|
|
|
g_string_append(message, from);
|
|
|
|
g_string_append(message, "\nto: ");
|
|
|
|
g_string_append(message, room);
|
2015-05-04 22:06:35 +00:00
|
|
|
if (reason) {
|
2013-04-24 22:50:47 +00:00
|
|
|
g_string_append_printf(message, "\n\"%s\"", reason);
|
|
|
|
}
|
2013-04-22 22:48:23 +00:00
|
|
|
|
2016-02-14 22:28:55 +00:00
|
|
|
notify(message->str, 10000, "Incoming message");
|
2013-04-22 22:48:23 +00:00
|
|
|
|
2013-08-03 11:38:38 +00:00
|
|
|
g_string_free(message, TRUE);
|
2013-04-22 22:48:23 +00:00
|
|
|
}
|
|
|
|
|
2014-12-22 22:13:42 +00:00
|
|
|
void
|
2020-07-07 12:18:57 +00:00
|
|
|
notify_message(const char* const name, int num, const char* const text)
|
2013-04-21 19:17:06 +00:00
|
|
|
{
|
2016-02-04 00:41:53 +00:00
|
|
|
int ui_index = num;
|
|
|
|
if (ui_index == 10) {
|
|
|
|
ui_index = 0;
|
|
|
|
}
|
|
|
|
|
2020-07-07 12:18:57 +00:00
|
|
|
GString* message = g_string_new("");
|
2016-02-04 00:41:53 +00:00
|
|
|
g_string_append_printf(message, "%s (win %d)", name, ui_index);
|
|
|
|
if (text && prefs_get_boolean(PREF_NOTIFY_CHAT_TEXT)) {
|
2015-11-24 23:03:52 +00:00
|
|
|
g_string_append_printf(message, "\n%s", text);
|
2014-05-24 21:14:26 +00:00
|
|
|
}
|
|
|
|
|
2016-02-14 22:28:55 +00:00
|
|
|
notify(message->str, 10000, "incoming message");
|
2015-11-24 23:03:52 +00:00
|
|
|
g_string_free(message, TRUE);
|
2013-05-22 23:10:55 +00:00
|
|
|
}
|
|
|
|
|
2014-12-22 22:13:42 +00:00
|
|
|
void
|
2020-07-07 12:18:57 +00:00
|
|
|
notify_room_message(const char* const nick, const char* const room, int num, const char* const text)
|
2013-05-22 23:10:55 +00:00
|
|
|
{
|
2016-02-04 00:45:37 +00:00
|
|
|
int ui_index = num;
|
|
|
|
if (ui_index == 10) {
|
|
|
|
ui_index = 0;
|
|
|
|
}
|
|
|
|
|
2020-07-07 12:18:57 +00:00
|
|
|
GString* message = g_string_new("");
|
2016-02-04 00:45:37 +00:00
|
|
|
g_string_append_printf(message, "%s in %s (win %d)", nick, room, ui_index);
|
|
|
|
if (text && prefs_get_boolean(PREF_NOTIFY_ROOM_TEXT)) {
|
2014-05-24 21:14:26 +00:00
|
|
|
g_string_append_printf(message, "\n%s", text);
|
|
|
|
}
|
2013-05-22 23:10:55 +00:00
|
|
|
|
2016-02-14 22:28:55 +00:00
|
|
|
notify(message->str, 10000, "incoming message");
|
2013-05-22 23:10:55 +00:00
|
|
|
|
2014-05-24 21:14:26 +00:00
|
|
|
g_string_free(message, TRUE);
|
2013-04-21 19:17:06 +00:00
|
|
|
}
|
|
|
|
|
2014-12-22 22:13:42 +00:00
|
|
|
void
|
2020-07-07 12:18:57 +00:00
|
|
|
notify_subscription(const char* const from)
|
2013-04-27 22:46:49 +00:00
|
|
|
{
|
2020-07-07 12:18:57 +00:00
|
|
|
GString* message = g_string_new("Subscription request: \n");
|
2013-04-27 22:46:49 +00:00
|
|
|
g_string_append(message, from);
|
2016-09-27 14:20:37 +00:00
|
|
|
notify(message->str, 10000, "Incoming message");
|
2013-08-03 11:38:38 +00:00
|
|
|
g_string_free(message, TRUE);
|
2013-04-27 22:46:49 +00:00
|
|
|
}
|
|
|
|
|
2014-12-22 22:13:42 +00:00
|
|
|
void
|
|
|
|
notify_remind(void)
|
2013-04-21 19:17:06 +00:00
|
|
|
{
|
2015-01-15 00:42:40 +00:00
|
|
|
gdouble elapsed = g_timer_elapsed(remind_timer, NULL);
|
|
|
|
gint remind_period = prefs_get_notify_remind();
|
|
|
|
if (remind_period > 0 && elapsed >= remind_period) {
|
2016-02-14 22:28:55 +00:00
|
|
|
gboolean donotify = wins_do_notify_remind();
|
2015-11-02 21:14:55 +00:00
|
|
|
gint unread = wins_get_total_unread();
|
2015-01-15 00:42:40 +00:00
|
|
|
gint open = muc_invites_count();
|
|
|
|
gint subs = presence_sub_request_count();
|
2013-04-22 23:18:56 +00:00
|
|
|
|
2020-07-07 12:18:57 +00:00
|
|
|
GString* text = g_string_new("");
|
2013-04-21 19:17:06 +00:00
|
|
|
|
2016-02-14 22:28:55 +00:00
|
|
|
if (donotify && unread > 0) {
|
2015-01-15 00:42:40 +00:00
|
|
|
if (unread == 1) {
|
|
|
|
g_string_append(text, "1 unread message");
|
|
|
|
} else {
|
|
|
|
g_string_append_printf(text, "%d unread messages", unread);
|
|
|
|
}
|
2013-04-22 23:18:56 +00:00
|
|
|
}
|
2015-01-15 00:42:40 +00:00
|
|
|
if (open > 0) {
|
|
|
|
if (unread > 0) {
|
|
|
|
g_string_append(text, "\n");
|
|
|
|
}
|
|
|
|
if (open == 1) {
|
|
|
|
g_string_append(text, "1 room invite");
|
|
|
|
} else {
|
|
|
|
g_string_append_printf(text, "%d room invites", open);
|
|
|
|
}
|
2013-04-22 23:18:56 +00:00
|
|
|
}
|
2015-01-15 00:42:40 +00:00
|
|
|
if (subs > 0) {
|
|
|
|
if ((unread > 0) || (open > 0)) {
|
|
|
|
g_string_append(text, "\n");
|
|
|
|
}
|
|
|
|
if (subs == 1) {
|
|
|
|
g_string_append(text, "1 subscription request");
|
|
|
|
} else {
|
|
|
|
g_string_append_printf(text, "%d subscription requests", subs);
|
|
|
|
}
|
2013-04-27 22:57:51 +00:00
|
|
|
}
|
2015-01-15 00:42:40 +00:00
|
|
|
|
2016-02-14 22:28:55 +00:00
|
|
|
if ((donotify && unread > 0) || (open > 0) || (subs > 0)) {
|
|
|
|
notify(text->str, 5000, "Incoming message");
|
2013-04-27 22:57:51 +00:00
|
|
|
}
|
2013-04-22 23:18:56 +00:00
|
|
|
|
2015-01-15 00:42:40 +00:00
|
|
|
g_string_free(text, TRUE);
|
2013-04-22 23:18:56 +00:00
|
|
|
|
2015-01-15 00:42:40 +00:00
|
|
|
g_timer_start(remind_timer);
|
|
|
|
}
|
2013-04-21 19:17:06 +00:00
|
|
|
}
|
|
|
|
|
2016-02-14 22:28:55 +00:00
|
|
|
void
|
2020-07-07 12:18:57 +00:00
|
|
|
notify(const char* const message, int timeout, const char* const category)
|
2013-04-21 19:17:06 +00:00
|
|
|
{
|
2016-03-31 20:05:02 +00:00
|
|
|
#ifdef HAVE_LIBNOTIFY
|
2014-06-29 18:19:08 +00:00
|
|
|
log_debug("Attempting notification: %s", message);
|
|
|
|
if (notify_is_initted()) {
|
2014-06-29 18:33:18 +00:00
|
|
|
log_debug("Reinitialising libnotify");
|
|
|
|
notify_uninit();
|
2014-06-29 18:19:08 +00:00
|
|
|
} else {
|
2014-06-29 18:33:18 +00:00
|
|
|
log_debug("Initialising libnotify");
|
2014-06-29 18:19:08 +00:00
|
|
|
}
|
2020-12-09 10:01:16 +00:00
|
|
|
notify_init("Profanity");
|
|
|
|
|
2013-04-21 19:17:06 +00:00
|
|
|
if (notify_is_initted()) {
|
2020-07-07 12:18:57 +00:00
|
|
|
NotifyNotification* notification;
|
2013-04-21 19:17:06 +00:00
|
|
|
notification = notify_notification_new("Profanity", message, NULL);
|
|
|
|
notify_notification_set_timeout(notification, timeout);
|
|
|
|
notify_notification_set_category(notification, category);
|
|
|
|
notify_notification_set_urgency(notification, NOTIFY_URGENCY_NORMAL);
|
|
|
|
|
2020-07-07 12:18:57 +00:00
|
|
|
GError* error = NULL;
|
2013-04-21 19:17:06 +00:00
|
|
|
gboolean notify_success = notify_notification_show(notification, &error);
|
|
|
|
|
|
|
|
if (!notify_success) {
|
|
|
|
log_error("Error sending desktop notification:");
|
|
|
|
log_error(" -> Message : %s", message);
|
|
|
|
log_error(" -> Error : %s", error->message);
|
2020-04-14 00:24:02 +00:00
|
|
|
g_error_free(error);
|
2014-06-29 18:19:08 +00:00
|
|
|
} else {
|
2020-04-14 00:24:02 +00:00
|
|
|
log_debug("Notification sent.");
|
|
|
|
}
|
2013-04-21 19:17:06 +00:00
|
|
|
} else {
|
2014-06-29 18:19:08 +00:00
|
|
|
log_error("Libnotify not initialised.");
|
2013-04-21 19:17:06 +00:00
|
|
|
}
|
|
|
|
#endif
|
2016-03-31 20:05:02 +00:00
|
|
|
#ifdef PLATFORM_CYGWIN
|
2013-04-21 19:17:06 +00:00
|
|
|
NOTIFYICONDATA nid;
|
2020-06-24 00:09:53 +00:00
|
|
|
memset(&nid, 0, sizeof(nid));
|
2013-04-21 19:17:06 +00:00
|
|
|
nid.cbSize = sizeof(NOTIFYICONDATA);
|
2022-01-27 14:49:44 +00:00
|
|
|
// nid.hWnd = hWnd;
|
2013-04-21 19:17:06 +00:00
|
|
|
nid.uID = 100;
|
|
|
|
nid.uVersion = NOTIFYICON_VERSION;
|
2022-01-27 14:49:44 +00:00
|
|
|
// nid.uCallbackMessage = WM_MYMESSAGE;
|
2013-04-21 19:17:06 +00:00
|
|
|
nid.hIcon = LoadIcon(NULL, IDI_APPLICATION);
|
2020-06-24 00:09:53 +00:00
|
|
|
strcpy(nid.szTip, "Tray Icon");
|
2013-04-21 19:17:06 +00:00
|
|
|
nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
|
|
|
|
Shell_NotifyIcon(NIM_ADD, &nid);
|
|
|
|
|
|
|
|
// For a Ballon Tip
|
|
|
|
nid.uFlags = NIF_INFO;
|
2020-07-07 12:18:57 +00:00
|
|
|
strcpy(nid.szInfoTitle, "Profanity"); // Title
|
2020-06-24 00:09:53 +00:00
|
|
|
strncpy(nid.szInfo, message, sizeof(nid.szInfo) - 1); // Copy Tip
|
2020-07-07 12:18:57 +00:00
|
|
|
nid.uTimeout = timeout; // 3 Seconds
|
2013-04-21 19:17:06 +00:00
|
|
|
nid.dwInfoFlags = NIIF_INFO;
|
|
|
|
|
|
|
|
Shell_NotifyIcon(NIM_MODIFY, &nid);
|
|
|
|
#endif
|
2016-03-31 20:05:02 +00:00
|
|
|
#ifdef HAVE_OSXNOTIFY
|
2020-07-07 12:18:57 +00:00
|
|
|
GString* notify_command = g_string_new("terminal-notifier -title \"Profanity\" -message '");
|
2014-05-24 21:31:42 +00:00
|
|
|
|
2020-07-07 12:18:57 +00:00
|
|
|
char* escaped_single = str_replace(message, "'", "'\\''");
|
2014-08-04 21:00:43 +00:00
|
|
|
|
|
|
|
if (escaped_single[0] == '<') {
|
|
|
|
g_string_append(notify_command, "\\<");
|
|
|
|
g_string_append(notify_command, &escaped_single[1]);
|
|
|
|
} else if (escaped_single[0] == '[') {
|
|
|
|
g_string_append(notify_command, "\\[");
|
|
|
|
g_string_append(notify_command, &escaped_single[1]);
|
|
|
|
} else if (escaped_single[0] == '(') {
|
|
|
|
g_string_append(notify_command, "\\(");
|
|
|
|
g_string_append(notify_command, &escaped_single[1]);
|
|
|
|
} else if (escaped_single[0] == '{') {
|
|
|
|
g_string_append(notify_command, "\\{");
|
|
|
|
g_string_append(notify_command, &escaped_single[1]);
|
|
|
|
} else {
|
|
|
|
g_string_append(notify_command, escaped_single);
|
|
|
|
}
|
|
|
|
|
2015-02-14 16:20:15 +00:00
|
|
|
g_string_append(notify_command, "'");
|
2014-08-04 20:41:17 +00:00
|
|
|
free(escaped_single);
|
2014-03-04 20:01:19 +00:00
|
|
|
|
2020-07-07 12:18:57 +00:00
|
|
|
char* term_name = getenv("TERM_PROGRAM");
|
|
|
|
char* app_id = NULL;
|
2014-03-04 20:01:19 +00:00
|
|
|
if (g_strcmp0(term_name, "Apple_Terminal") == 0) {
|
|
|
|
app_id = "com.apple.Terminal";
|
|
|
|
} else if (g_strcmp0(term_name, "iTerm.app") == 0) {
|
|
|
|
app_id = "com.googlecode.iterm2";
|
|
|
|
}
|
|
|
|
|
2015-05-04 22:06:35 +00:00
|
|
|
if (app_id) {
|
2014-03-04 20:01:19 +00:00
|
|
|
g_string_append(notify_command, " -sender ");
|
|
|
|
g_string_append(notify_command, app_id);
|
|
|
|
}
|
|
|
|
|
2014-03-04 20:16:47 +00:00
|
|
|
int res = system(notify_command->str);
|
|
|
|
if (res == -1) {
|
|
|
|
log_error("Could not send desktop notificaion.");
|
|
|
|
}
|
|
|
|
|
2014-03-04 20:01:19 +00:00
|
|
|
g_string_free(notify_command, TRUE);
|
2014-03-04 22:59:09 +00:00
|
|
|
#endif
|
2015-02-14 16:20:15 +00:00
|
|
|
}
|