2013-04-21 15:17:06 -04:00
|
|
|
/*
|
|
|
|
* notifier.c
|
|
|
|
*
|
2014-03-08 20:18:19 -05:00
|
|
|
* Copyright (C) 2012 - 2014 James Booth <boothj5@gmail.com>
|
2013-04-21 15:17:06 -04: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
|
|
|
|
* along with Profanity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2014-03-04 15:01:19 -05:00
|
|
|
#include <stdlib.h>
|
2013-04-21 15:17:06 -04:00
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
#ifdef HAVE_LIBNOTIFY
|
|
|
|
#include <libnotify/notify.h>
|
|
|
|
#endif
|
2013-04-21 18:39:16 -04:00
|
|
|
#ifdef PLATFORM_CYGWIN
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
2013-04-21 15:17:06 -04:00
|
|
|
|
|
|
|
#include "log.h"
|
2013-04-24 18:50:47 -04:00
|
|
|
#include "muc.h"
|
2013-04-21 15:17:06 -04:00
|
|
|
#include "ui/ui.h"
|
|
|
|
|
|
|
|
static void _notify(const char * const message, int timeout,
|
|
|
|
const char * const category);
|
|
|
|
|
2013-12-22 17:14:15 -05:00
|
|
|
static void
|
|
|
|
_notifier_uninit(void)
|
2013-04-21 15:17:06 -04:00
|
|
|
{
|
|
|
|
#ifdef HAVE_LIBNOTIFY
|
|
|
|
if (notify_is_initted()) {
|
|
|
|
notify_uninit();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-12-22 17:14:15 -05:00
|
|
|
static void
|
|
|
|
_notify_typing(const char * const handle)
|
2013-04-21 15:17:06 -04:00
|
|
|
{
|
2013-05-20 17:51:35 -04:00
|
|
|
char message[strlen(handle) + 1 + 11];
|
|
|
|
sprintf(message, "%s: typing...", handle);
|
2013-04-21 15:17:06 -04:00
|
|
|
|
|
|
|
_notify(message, 10000, "Incoming message");
|
|
|
|
}
|
|
|
|
|
2013-12-22 17:14:15 -05:00
|
|
|
static void
|
|
|
|
_notify_invite(const char * const from, const char * const room,
|
2013-04-24 18:50:47 -04:00
|
|
|
const char * const reason)
|
2013-04-22 18:48:23 -04:00
|
|
|
{
|
|
|
|
GString *message = g_string_new("Room invite\nfrom: ");
|
|
|
|
g_string_append(message, from);
|
|
|
|
g_string_append(message, "\nto: ");
|
|
|
|
g_string_append(message, room);
|
2013-04-24 18:50:47 -04:00
|
|
|
if (reason != NULL) {
|
|
|
|
g_string_append_printf(message, "\n\"%s\"", reason);
|
|
|
|
}
|
2013-04-22 18:48:23 -04:00
|
|
|
|
|
|
|
_notify(message->str, 10000, "Incoming message");
|
|
|
|
|
2013-08-03 07:38:38 -04:00
|
|
|
g_string_free(message, TRUE);
|
2013-04-22 18:48:23 -04:00
|
|
|
}
|
|
|
|
|
2013-12-22 17:14:15 -05:00
|
|
|
static void
|
2014-05-24 17:14:26 -04:00
|
|
|
_notify_message(const char * const handle, int win, const char * const text)
|
2013-04-21 15:17:06 -04:00
|
|
|
{
|
2014-05-24 17:14:26 -04:00
|
|
|
GString *message = g_string_new("");
|
|
|
|
g_string_append_printf(message, "%s (win %d)", handle, win);
|
|
|
|
if (text != NULL) {
|
|
|
|
g_string_append_printf(message, "\n%s", text);
|
|
|
|
}
|
|
|
|
|
|
|
|
_notify(message->str, 10000, "incoming message");
|
2013-04-21 15:17:06 -04:00
|
|
|
|
2014-05-24 17:14:26 -04:00
|
|
|
g_string_free(message, TRUE);
|
2013-05-22 19:10:55 -04:00
|
|
|
}
|
|
|
|
|
2013-12-22 17:14:15 -05:00
|
|
|
static void
|
2014-05-24 17:14:26 -04:00
|
|
|
_notify_room_message(const char * const handle, const char * const room, int win, const char * const text)
|
2013-05-22 19:10:55 -04:00
|
|
|
{
|
2014-05-24 17:14:26 -04:00
|
|
|
GString *message = g_string_new("");
|
|
|
|
g_string_append_printf(message, "%s in %s (win %d)", handle, room, win);
|
|
|
|
if (text != NULL) {
|
|
|
|
g_string_append_printf(message, "\n%s", text);
|
|
|
|
}
|
2013-05-22 19:10:55 -04:00
|
|
|
|
2014-05-24 17:14:26 -04:00
|
|
|
_notify(message->str, 10000, "incoming message");
|
2013-05-22 19:10:55 -04:00
|
|
|
|
2014-05-24 17:14:26 -04:00
|
|
|
g_string_free(message, TRUE);
|
2013-04-21 15:17:06 -04:00
|
|
|
}
|
|
|
|
|
2013-12-22 17:14:15 -05:00
|
|
|
static void
|
|
|
|
_notify_subscription(const char * const from)
|
2013-04-27 18:46:49 -04:00
|
|
|
{
|
|
|
|
GString *message = g_string_new("Subscription request: \n");
|
|
|
|
g_string_append(message, from);
|
|
|
|
_notify(message->str, 10000, "Incomming message");
|
2013-08-03 07:38:38 -04:00
|
|
|
g_string_free(message, TRUE);
|
2013-04-27 18:46:49 -04:00
|
|
|
}
|
|
|
|
|
2013-12-22 17:14:15 -05:00
|
|
|
static void
|
|
|
|
_notify_remind(void)
|
2013-04-21 15:17:06 -04:00
|
|
|
{
|
|
|
|
gint unread = ui_unread();
|
2013-04-24 18:50:47 -04:00
|
|
|
gint open = muc_invite_count();
|
2013-04-27 18:57:51 -04:00
|
|
|
gint subs = presence_sub_request_count();
|
2013-04-22 19:18:56 -04:00
|
|
|
|
|
|
|
GString *text = g_string_new("");
|
|
|
|
|
2013-04-21 15:17:06 -04:00
|
|
|
if (unread > 0) {
|
|
|
|
if (unread == 1) {
|
2013-04-22 19:18:56 -04:00
|
|
|
g_string_append(text, "1 unread message");
|
2013-04-21 15:17:06 -04:00
|
|
|
} else {
|
2013-04-22 19:18:56 -04:00
|
|
|
g_string_append_printf(text, "%d unread messages", unread);
|
2013-04-21 15:17:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2013-04-22 19:18:56 -04:00
|
|
|
if (open > 0) {
|
|
|
|
if (unread > 0) {
|
|
|
|
g_string_append(text, "\n");
|
|
|
|
}
|
|
|
|
if (open == 1) {
|
2013-04-24 18:50:47 -04:00
|
|
|
g_string_append(text, "1 room invite");
|
2013-04-22 19:18:56 -04:00
|
|
|
} else {
|
2013-04-24 18:50:47 -04:00
|
|
|
g_string_append_printf(text, "%d room invites", open);
|
2013-04-22 19:18:56 -04:00
|
|
|
}
|
|
|
|
}
|
2013-04-27 18:57:51 -04: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-22 19:18:56 -04:00
|
|
|
|
2013-04-27 18:57:51 -04:00
|
|
|
if ((unread > 0) || (open > 0) || (subs > 0)) {
|
2013-04-22 19:18:56 -04:00
|
|
|
_notify(text->str, 5000, "Incoming message");
|
|
|
|
}
|
|
|
|
|
|
|
|
g_string_free(text, TRUE);
|
2013-04-21 15:17:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_notify(const char * const message, int timeout,
|
|
|
|
const char * const category)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_LIBNOTIFY
|
2014-06-29 14:19:08 -04:00
|
|
|
log_debug("Attempting notification: %s", message);
|
|
|
|
if (notify_is_initted()) {
|
2014-06-29 14:33:18 -04:00
|
|
|
log_debug("Reinitialising libnotify");
|
|
|
|
notify_uninit();
|
|
|
|
notify_init("Profanity");
|
2014-06-29 14:19:08 -04:00
|
|
|
} else {
|
2014-06-29 14:33:18 -04:00
|
|
|
log_debug("Initialising libnotify");
|
|
|
|
notify_init("Profanity");
|
2014-06-29 14:19:08 -04:00
|
|
|
}
|
2013-04-21 15:17:06 -04:00
|
|
|
if (notify_is_initted()) {
|
|
|
|
NotifyNotification *notification;
|
|
|
|
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);
|
|
|
|
|
|
|
|
GError *error = NULL;
|
|
|
|
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);
|
2014-06-29 14:19:08 -04:00
|
|
|
} else {
|
|
|
|
log_debug("Notification sent.");
|
|
|
|
}
|
2013-04-21 15:17:06 -04:00
|
|
|
} else {
|
2014-06-29 14:19:08 -04:00
|
|
|
log_error("Libnotify not initialised.");
|
2013-04-21 15:17:06 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef PLATFORM_CYGWIN
|
|
|
|
NOTIFYICONDATA nid;
|
|
|
|
nid.cbSize = sizeof(NOTIFYICONDATA);
|
|
|
|
//nid.hWnd = hWnd;
|
|
|
|
nid.uID = 100;
|
|
|
|
nid.uVersion = NOTIFYICON_VERSION;
|
|
|
|
//nid.uCallbackMessage = WM_MYMESSAGE;
|
|
|
|
nid.hIcon = LoadIcon(NULL, IDI_APPLICATION);
|
2014-04-02 16:01:20 -04:00
|
|
|
strncpy(nid.szTip, "Tray Icon", 10);
|
2013-04-21 15:17:06 -04:00
|
|
|
nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
|
|
|
|
Shell_NotifyIcon(NIM_ADD, &nid);
|
|
|
|
|
|
|
|
// For a Ballon Tip
|
|
|
|
nid.uFlags = NIF_INFO;
|
2014-04-02 16:01:20 -04:00
|
|
|
strncpy(nid.szInfoTitle, "Profanity", 10); // Title
|
|
|
|
strncpy(nid.szInfo, message, 256); // Copy Tip
|
2013-04-21 15:17:06 -04:00
|
|
|
nid.uTimeout = timeout; // 3 Seconds
|
|
|
|
nid.dwInfoFlags = NIIF_INFO;
|
|
|
|
|
|
|
|
Shell_NotifyIcon(NIM_MODIFY, &nid);
|
|
|
|
#endif
|
2014-03-04 17:59:09 -05:00
|
|
|
#ifdef HAVE_OSXNOTIFY
|
2014-05-24 17:31:42 -04:00
|
|
|
GString *notify_command = g_string_new("terminal-notifier -title \"Profanity\" -message \"");
|
|
|
|
|
2014-08-04 16:41:17 -04:00
|
|
|
char *escaped_double = str_replace(message, "\"", "\\\"");
|
|
|
|
char *escaped_single = str_replace(escaped_double, "`", "\\`");
|
2014-08-04 17:00:43 -04: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);
|
|
|
|
}
|
|
|
|
|
2014-05-24 17:31:42 -04:00
|
|
|
g_string_append(notify_command, "\"");
|
2014-08-04 16:41:17 -04:00
|
|
|
free(escaped_double);
|
|
|
|
free(escaped_single);
|
2014-03-04 15:01:19 -05:00
|
|
|
|
|
|
|
char *term_name = getenv("TERM_PROGRAM");
|
|
|
|
char *app_id = NULL;
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (app_id != NULL) {
|
|
|
|
g_string_append(notify_command, " -sender ");
|
|
|
|
g_string_append(notify_command, app_id);
|
|
|
|
}
|
|
|
|
|
2014-03-04 15:16:47 -05:00
|
|
|
int res = system(notify_command->str);
|
|
|
|
if (res == -1) {
|
|
|
|
log_error("Could not send desktop notificaion.");
|
|
|
|
}
|
|
|
|
|
2014-03-04 15:01:19 -05:00
|
|
|
g_string_free(notify_command, TRUE);
|
2014-03-04 17:59:09 -05:00
|
|
|
#endif
|
2013-04-21 15:17:06 -04:00
|
|
|
}
|
2013-12-22 19:23:22 -05:00
|
|
|
|
|
|
|
void
|
|
|
|
notifier_init_module(void)
|
|
|
|
{
|
|
|
|
notifier_uninit = _notifier_uninit;
|
|
|
|
notify_typing = _notify_typing;
|
|
|
|
notify_invite = _notify_invite;
|
|
|
|
notify_message = _notify_message;
|
|
|
|
notify_room_message = _notify_room_message;
|
|
|
|
notify_subscription = _notify_subscription;
|
|
|
|
notify_remind = _notify_remind;
|
|
|
|
}
|
|
|
|
|