1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

Tidy regular chat and room notifications

This commit is contained in:
James Booth 2015-11-24 23:03:52 +00:00
parent 00a735ece5
commit 9c8b137a51
8 changed files with 128 additions and 60 deletions

View File

@ -202,6 +202,47 @@ prefs_reset_room_trigger_ac(void)
autocomplete_reset(room_trigger_ac); autocomplete_reset(room_trigger_ac);
} }
gboolean
prefs_get_notify_chat(gboolean current_win)
{
gboolean notify_message = prefs_get_boolean(PREF_NOTIFY_MESSAGE);
gboolean notify_window = FALSE;
if (!current_win || (current_win && prefs_get_boolean(PREF_NOTIFY_MESSAGE_CURRENT)) ) {
notify_window = TRUE;
}
return (notify_message && notify_window);
}
gboolean
prefs_get_notify_room(gboolean current_win, const char *const nick, const char *const message)
{
gboolean notify_message = FALSE;
gboolean notify_window = FALSE;
char *room_setting = prefs_get_string(PREF_NOTIFY_ROOM);
if (g_strcmp0(room_setting, "on") == 0) {
notify_message = TRUE;
}
if (g_strcmp0(room_setting, "mention") == 0) {
char *message_lower = g_utf8_strdown(message, -1);
char *nick_lower = g_utf8_strdown(nick, -1);
if (g_strrstr(message_lower, nick_lower)) {
notify_message = TRUE;
}
g_free(message_lower);
g_free(nick_lower);
}
prefs_free_string(room_setting);
if (!current_win || (current_win && prefs_get_boolean(PREF_NOTIFY_ROOM_CURRENT)) ) {
notify_window = TRUE;
}
return (notify_message && notify_window);
}
gboolean gboolean
prefs_get_boolean(preference_t pref) prefs_get_boolean(preference_t pref)
{ {

View File

@ -213,4 +213,7 @@ char* prefs_get_string(preference_t pref);
void prefs_free_string(char *pref); void prefs_free_string(char *pref);
void prefs_set_string(preference_t pref, char *value); void prefs_set_string(preference_t pref, char *value);
gboolean prefs_get_notify_chat(gboolean current_win);
gboolean prefs_get_notify_room(gboolean current_win, const char *const nick, const char *const message);
#endif #endif

View File

@ -274,8 +274,32 @@ chatwin_incoming_msg(ProfChatWin *chatwin, const char *const resource, const cha
beep(); beep();
} }
if (prefs_get_boolean(PREF_NOTIFY_MESSAGE)) { if (!prefs_get_boolean(PREF_NOTIFY_MESSAGE)) {
notify_message(window, display_name, message); free(display_name);
return;
}
gboolean notify = FALSE;
gboolean is_current = wins_is_current(window);
if (!is_current || (is_current && prefs_get_boolean(PREF_NOTIFY_MESSAGE_CURRENT)) ) {
notify = TRUE;
}
if (!notify) {
free(display_name);
return;
}
int ui_index = num;
if (ui_index == 10) {
ui_index = 0;
}
if (prefs_get_boolean(PREF_NOTIFY_MESSAGE_TEXT)) {
notify_message(display_name, ui_index, message);
} else {
notify_message(display_name, ui_index, NULL);
} }
free(display_name); free(display_name);

View File

@ -390,11 +390,6 @@ mucwin_message(ProfMucWin *mucwin, const char *const nick, const char *const mes
mucwin->unread++; mucwin->unread++;
} }
int ui_index = num;
if (ui_index == 10) {
ui_index = 0;
}
// don't notify self messages // don't notify self messages
if (strcmp(nick, my_nick) == 0) { if (strcmp(nick, my_nick) == 0) {
return; return;
@ -404,34 +399,24 @@ mucwin_message(ProfMucWin *mucwin, const char *const nick, const char *const mes
beep(); beep();
} }
gboolean notify = FALSE;
char *room_setting = prefs_get_string(PREF_NOTIFY_ROOM);
if (g_strcmp0(room_setting, "on") == 0) {
notify = TRUE;
}
if (g_strcmp0(room_setting, "mention") == 0) {
char *message_lower = g_utf8_strdown(message, -1);
char *nick_lower = g_utf8_strdown(nick, -1);
if (g_strrstr(message_lower, nick_lower)) {
notify = TRUE;
}
g_free(message_lower);
g_free(nick_lower);
}
prefs_free_string(room_setting);
if (notify) {
gboolean is_current = wins_is_current(window); gboolean is_current = wins_is_current(window);
if ( !is_current || (is_current && prefs_get_boolean(PREF_NOTIFY_ROOM_CURRENT)) ) { gboolean notify = prefs_get_notify_room(is_current, my_nick, message);
if (!notify) {
return;
}
Jid *jidp = jid_create(mucwin->roomjid); Jid *jidp = jid_create(mucwin->roomjid);
int ui_index = num;
if (ui_index == 10) {
ui_index = 0;
}
if (prefs_get_boolean(PREF_NOTIFY_ROOM_TEXT)) { if (prefs_get_boolean(PREF_NOTIFY_ROOM_TEXT)) {
notify_room_message(nick, jidp->localpart, ui_index, message); notify_room_message(nick, jidp->localpart, ui_index, message);
} else { } else {
notify_room_message(nick, jidp->localpart, ui_index, NULL); notify_room_message(nick, jidp->localpart, ui_index, NULL);
} }
jid_destroy(jidp); jid_destroy(jidp);
}
}
} }
void void

View File

@ -73,17 +73,16 @@ notifier_uninit(void)
} }
void void
notify_typing(const char *const handle) notify_typing(const char *const name)
{ {
char message[strlen(handle) + 1 + 11]; char message[strlen(name) + 1 + 11];
sprintf(message, "%s: typing...", handle); sprintf(message, "%s: typing...", name);
_notify(message, 10000, "Incoming message"); _notify(message, 10000, "Incoming message");
} }
void void
notify_invite(const char *const from, const char *const room, notify_invite(const char *const from, const char *const room, const char *const reason)
const char *const reason)
{ {
GString *message = g_string_new("Room invite\nfrom: "); GString *message = g_string_new("Room invite\nfrom: ");
g_string_append(message, from); g_string_append(message, from);
@ -99,32 +98,24 @@ notify_invite(const char *const from, const char *const room,
} }
void void
notify_message(ProfWin *window, const char *const name, const char *const text) notify_message(const char *const name, int win, const char *const text)
{ {
int num = wins_get_num(window);
if (num == 10) {
num = 0;
}
gboolean is_current = wins_is_current(window);
if (!is_current || (is_current && prefs_get_boolean(PREF_NOTIFY_MESSAGE_CURRENT)) ) {
GString *message = g_string_new(""); GString *message = g_string_new("");
g_string_append_printf(message, "%s (win %d)", name, num); g_string_append_printf(message, "%s (win %d)", name, win);
if (text) {
if (prefs_get_boolean(PREF_NOTIFY_MESSAGE_TEXT) && text) {
g_string_append_printf(message, "\n%s", text); g_string_append_printf(message, "\n%s", text);
} }
_notify(message->str, 10000, "incoming message"); _notify(message->str, 10000, "incoming message");
g_string_free(message, TRUE); g_string_free(message, TRUE);
}
} }
void void
notify_room_message(const char *const handle, const char *const room, int win, const char *const text) notify_room_message(const char *const nick, const char *const room, int win, const char *const text)
{ {
GString *message = g_string_new(""); GString *message = g_string_new("");
g_string_append_printf(message, "%s in %s (win %d)", handle, room, win); g_string_append_printf(message, "%s in %s (win %d)", nick, room, win);
if (text) { if (text) {
g_string_append_printf(message, "\n%s", text); g_string_append_printf(message, "\n%s", text);
} }

View File

@ -75,8 +75,32 @@ privwin_incoming_msg(ProfPrivateWin *privatewin, const char *const message, GDat
beep(); beep();
} }
if (prefs_get_boolean(PREF_NOTIFY_MESSAGE)) { if (!prefs_get_boolean(PREF_NOTIFY_MESSAGE)) {
notify_message(window, display_from, message); free(display_from);
return;
}
gboolean notify = FALSE;
gboolean is_current = wins_is_current(window);
if (!is_current || (is_current && prefs_get_boolean(PREF_NOTIFY_MESSAGE_CURRENT)) ) {
notify = TRUE;
}
if (!notify) {
free(display_from);
return;
}
int ui_index = num;
if (ui_index == 10) {
ui_index = 0;
}
if (prefs_get_boolean(PREF_NOTIFY_MESSAGE_TEXT)) {
notify_message(display_from, ui_index, message);
} else {
notify_message(display_from, ui_index, NULL);
} }
free(display_from); free(display_from);

View File

@ -339,9 +339,9 @@ void win_clear(ProfWin *window);
// desktop notifications // desktop notifications
void notifier_initialise(void); void notifier_initialise(void);
void notifier_uninit(void); void notifier_uninit(void);
void notify_typing(const char *const handle); void notify_typing(const char *const name);
void notify_message(ProfWin *window, const char *const name, const char *const text); void notify_message(const char *const name, int win, const char *const text);
void notify_room_message(const char *const handle, const char *const room, int win, const char *const text); void notify_room_message(const char *const nick, const char *const room, int win, const char *const text);
void notify_remind(void); void notify_remind(void);
void notify_invite(const char *const from, const char *const room, const char *const reason); void notify_invite(const char *const from, const char *const room, const char *const reason);
void notify_subscription(const char *const from); void notify_subscription(const char *const from);

View File

@ -514,7 +514,7 @@ void win_clear(ProfWin *window) {}
void notifier_uninit(void) {} void notifier_uninit(void) {}
void notify_typing(const char * const handle) {} void notify_typing(const char * const handle) {}
void notify_message(ProfWin *window, const char * const name, const char * const text) {} void notify_message(const char *const name, int win, const char *const text) {}
void notify_room_message(const char * const handle, const char * const room, void notify_room_message(const char * const handle, const char * const room,
int win, const char * const text) {} int win, const char * const text) {}
void notify_remind(void) {} void notify_remind(void) {}