From f63f68630b84d9b94f1238e8c2d8e9c2e6ca73ab Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 21 Apr 2013 20:17:06 +0100 Subject: [PATCH] Added notifier module --- Makefile.am | 2 +- src/profanity.c | 1 + src/ui/core.c | 137 ++++++---------------------------------------- src/ui/notifier.c | 136 +++++++++++++++++++++++++++++++++++++++++++++ src/ui/notifier.h | 28 ++++++++++ src/ui/ui.h | 2 +- 6 files changed, 183 insertions(+), 123 deletions(-) create mode 100644 src/ui/notifier.c create mode 100644 src/ui/notifier.h diff --git a/Makefile.am b/Makefile.am index edcdbb83..61aace12 100644 --- a/Makefile.am +++ b/Makefile.am @@ -10,7 +10,7 @@ profanity_SOURCES = src/contact.c src/contact.h src/log.c src/common.c \ src/xmpp/capabilities.h src/xmpp/connection.h \ src/ui/ui.h src/ui/window.c src/ui/window.h src/ui/core.c \ src/ui/titlebar.c src/ui/statusbar.c src/ui/inputwin.c \ - src/ui/console.c \ + src/ui/console.c src/ui/notifier.c src/ui/notifier.h \ src/command/command.h src/command/command.c src/command/history.c \ src/command/history.h src/command/parser.c \ src/command/parser.h \ diff --git a/src/profanity.c b/src/profanity.c index a0c917f6..052575a9 100644 --- a/src/profanity.c +++ b/src/profanity.c @@ -42,6 +42,7 @@ #include "log.h" #include "muc.h" #include "resource.h" +#include "ui/notifier.h" #include "ui/ui.h" #include "xmpp/xmpp.h" diff --git a/src/ui/core.c b/src/ui/core.c index 6bdc3fa0..777ce98a 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -28,9 +28,6 @@ #include #endif #include -#ifdef HAVE_LIBNOTIFY -#include -#endif #ifdef PLATFORM_CYGWIN #include #endif @@ -50,6 +47,7 @@ #include "jid.h" #include "log.h" #include "muc.h" +#include "ui/notifier.h" #include "ui/ui.h" #include "ui/window.h" @@ -86,17 +84,10 @@ static void _show_status_string(ProfWin *window, const char * const from, static void _win_handle_switch(const wint_t * const ch); static void _win_handle_page(const wint_t * const ch); static void _win_resize_all(void); -static gint _win_get_unread(void); static void _win_show_history(WINDOW *win, int win_index, const char * const contact); static void _ui_draw_win_title(void); -static void _notify(const char * const message, int timeout, - const char * const category); -static void _notify_remind(gint unread); -static void _notify_message(const char * const short_from); -static void _notify_typing(const char * const from); - void ui_init(void) { @@ -119,6 +110,7 @@ ui_init(void) console = windows[0]; current = console; cons_about(); + notifier_init(); #ifdef HAVE_LIBXSS display = XOpenDisplay(0); #endif @@ -171,11 +163,7 @@ ui_reset_idle_time(void) void ui_close(void) { -#ifdef HAVE_LIBNOTIFY - if (notify_is_initted()) { - notify_uninit(); - } -#endif + notifier_uninit(); endwin(); } @@ -239,7 +227,7 @@ ui_contact_typing(const char * const from) } if (prefs_get_boolean(PREF_NOTIFY_TYPING)) - _notify_typing(from); + notify_typing(from); } void @@ -392,7 +380,7 @@ ui_incoming_msg(const char * const from, const char * const message, if (prefs_get_boolean(PREF_BEEP)) beep(); if (prefs_get_boolean(PREF_NOTIFY_MESSAGE)) - _notify_message(display_from); + notify_message(display_from); g_free(display_from); } @@ -990,7 +978,7 @@ ui_room_message(const char * const room_jid, const char * const nick, beep(); } if (prefs_get_boolean(PREF_NOTIFY_MESSAGE)) { - _notify_message(nick); + notify_message(nick); } } } @@ -1082,13 +1070,17 @@ ui_status_room(const char * const contact) } } -void -notify_remind(void) +gint +ui_unread(void) { - gint unread = _win_get_unread(); - if (unread > 0) { - _notify_remind(unread); + int i; + gint result = 0; + for (i = 0; i < NUM_WINS; i++) { + if (windows[i] != NULL) { + result += windows[i]->unread; + } } + return result; } static void @@ -1110,7 +1102,7 @@ _ui_draw_win_title(void) if (status == JABBER_CONNECTED) { const char * const jid = jabber_get_jid(); - gint unread = _win_get_unread(); + gint unread = ui_unread(); if (unread != 0) { snprintf(new_win_title, sizeof(new_win_title), "%c]0;%s%s (%d) - %s%c", '\033', "Profanity", version_str->str, unread, jid, '\007'); @@ -1133,90 +1125,6 @@ _ui_draw_win_title(void) } } -static void -_notify(const char * const message, int timeout, - const char * const category) -{ -#ifdef HAVE_LIBNOTIFY - gboolean notify_initted = notify_is_initted(); - - if (!notify_initted) { - notify_initted = notify_init("Profanity"); - } - - if (notify_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); - } - } else { - log_error("Libnotify initialisation error."); - } -#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); - strcpy(nid.szTip, "Tray Icon"); - nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP; - Shell_NotifyIcon(NIM_ADD, &nid); - - // For a Ballon Tip - nid.uFlags = NIF_INFO; - strcpy(nid.szInfoTitle, "Profanity"); // Title - strcpy(nid.szInfo, message); // Copy Tip - nid.uTimeout = timeout; // 3 Seconds - nid.dwInfoFlags = NIIF_INFO; - - Shell_NotifyIcon(NIM_MODIFY, &nid); -#endif -} - -static void -_notify_remind(gint unread) -{ - char message[20]; - if (unread == 1) { - sprintf(message, "1 unread message"); - } else { - snprintf(message, sizeof(message), "%d unread messages", unread); - } - - _notify(message, 5000, "Incoming message"); -} - -static void -_notify_message(const char * const short_from) -{ - char message[strlen(short_from) + 1 + 10]; - sprintf(message, "%s: message.", short_from); - - _notify(message, 10000, "Incoming message"); -} - -static void -_notify_typing(const char * const from) -{ - char message[strlen(from) + 1 + 11]; - sprintf(message, "%s: typing...", from); - - _notify(message, 10000, "Incoming message"); -} - static int _find_prof_win_index(const char * const contact) { @@ -1493,19 +1401,6 @@ _win_handle_page(const wint_t * const ch) } } -static gint -_win_get_unread(void) -{ - int i; - gint result = 0; - for (i = 0; i < NUM_WINS; i++) { - if (windows[i] != NULL) { - result += windows[i]->unread; - } - } - return result; -} - static void _win_show_history(WINDOW *win, int win_index, const char * const contact) { diff --git a/src/ui/notifier.c b/src/ui/notifier.c new file mode 100644 index 00000000..2cd9ad73 --- /dev/null +++ b/src/ui/notifier.c @@ -0,0 +1,136 @@ +/* + * notifier.c + * + * Copyright (C) 2012, 2013 James Booth + * + * 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 . + * + */ +#include "config.h" + +#include +#include + +#include +#ifdef HAVE_LIBNOTIFY +#include +#endif + +#include "log.h" +#include "ui/ui.h" + +static void _notify(const char * const message, int timeout, + const char * const category); + +void +notifier_init(void) +{ +#ifdef HAVE_LIBNOTIFY + notify_init("Profanity"); +#endif +} + +void +notifier_uninit(void) +{ +#ifdef HAVE_LIBNOTIFY + if (notify_is_initted()) { + notify_uninit(); + } +#endif +} + +void +notify_typing(const char * const from) +{ + char message[strlen(from) + 1 + 11]; + sprintf(message, "%s: typing...", from); + + _notify(message, 10000, "Incoming message"); +} + +void +notify_message(const char * const short_from) +{ + char message[strlen(short_from) + 1 + 10]; + sprintf(message, "%s: message.", short_from); + + _notify(message, 10000, "Incoming message"); +} + +void +notify_remind(void) +{ + gint unread = ui_unread(); + if (unread > 0) { + char message[20]; + if (unread == 1) { + sprintf(message, "1 unread message"); + } else { + snprintf(message, sizeof(message), "%d unread messages", unread); + } + + _notify(message, 5000, "Incoming message"); + } +} + +static void +_notify(const char * const message, int timeout, + const char * const category) +{ +#ifdef HAVE_LIBNOTIFY + + 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); + } + } else { + log_error("Libnotify initialisation error."); + } +#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); + strcpy(nid.szTip, "Tray Icon"); + nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP; + Shell_NotifyIcon(NIM_ADD, &nid); + + // For a Ballon Tip + nid.uFlags = NIF_INFO; + strcpy(nid.szInfoTitle, "Profanity"); // Title + strcpy(nid.szInfo, message); // Copy Tip + nid.uTimeout = timeout; // 3 Seconds + nid.dwInfoFlags = NIIF_INFO; + + Shell_NotifyIcon(NIM_MODIFY, &nid); +#endif +} diff --git a/src/ui/notifier.h b/src/ui/notifier.h new file mode 100644 index 00000000..a12dfa54 --- /dev/null +++ b/src/ui/notifier.h @@ -0,0 +1,28 @@ +/* + * notifier.h + * + * Copyright (C) 2012, 2013 James Booth + * + * 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 . + * + */ + +void notifier_init(void); +void notifier_uninit(void); + +void notify_typing(const char * const from); +void notify_message(const char * const short_from); +void notify_remind(void); diff --git a/src/ui/ui.h b/src/ui/ui.h index 47c6fe82..ab4eaa35 100644 --- a/src/ui/ui.h +++ b/src/ui/ui.h @@ -62,6 +62,7 @@ void ui_reset_idle_time(void); void ui_new_chat_win(const char * const to); void ui_print_error_from_recipient(const char * const from, const char *err_msg); void ui_print_system_msg_from_recipient(const char * const from, const char *message); +gint ui_unread(void); // current window actions void ui_close_current(void); @@ -188,5 +189,4 @@ void inp_block(void); void inp_get_password(char *passwd); void inp_replace_input(char *input, const char * const new_input, int *size); -void notify_remind(void); #endif