about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2015-11-24 23:03:52 +0000
committerJames Booth <boothj5@gmail.com>2015-11-24 23:03:52 +0000
commit9c8b137a515e90f55606177c75847493fa02f556 (patch)
tree2bd84de203d85679dc908d57c2dfd758e19b174d
parent00a735ece58421a6d4d3f7a161684b033aea828a (diff)
downloadprofani-tty-9c8b137a515e90f55606177c75847493fa02f556.tar.gz
Tidy regular chat and room notifications
-rw-r--r--src/config/preferences.c41
-rw-r--r--src/config/preferences.h3
-rw-r--r--src/ui/chatwin.c28
-rw-r--r--src/ui/mucwin.c43
-rw-r--r--src/ui/notifier.c35
-rw-r--r--src/ui/privwin.c28
-rw-r--r--src/ui/ui.h6
-rw-r--r--tests/unittests/ui/stub_ui.c2
8 files changed, 127 insertions, 59 deletions
diff --git a/src/config/preferences.c b/src/config/preferences.c
index 2051e66b..205d47df 100644
--- a/src/config/preferences.c
+++ b/src/config/preferences.c
@@ -203,6 +203,47 @@ prefs_reset_room_trigger_ac(void)
 }
 
 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
 prefs_get_boolean(preference_t pref)
 {
     const char *group = _get_group(pref);
diff --git a/src/config/preferences.h b/src/config/preferences.h
index 9038591d..face1811 100644
--- a/src/config/preferences.h
+++ b/src/config/preferences.h
@@ -213,4 +213,7 @@ char* prefs_get_string(preference_t pref);
 void prefs_free_string(char *pref);
 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
diff --git a/src/ui/chatwin.c b/src/ui/chatwin.c
index c34cc1fc..2efe82e1 100644
--- a/src/ui/chatwin.c
+++ b/src/ui/chatwin.c
@@ -274,8 +274,32 @@ chatwin_incoming_msg(ProfChatWin *chatwin, const char *const resource, const cha
         beep();
     }
 
-    if (prefs_get_boolean(PREF_NOTIFY_MESSAGE)) {
-        notify_message(window, display_name, message);
+    if (!prefs_get_boolean(PREF_NOTIFY_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);
diff --git a/src/ui/mucwin.c b/src/ui/mucwin.c
index 58fef4d1..aa825d16 100644
--- a/src/ui/mucwin.c
+++ b/src/ui/mucwin.c
@@ -390,11 +390,6 @@ mucwin_message(ProfMucWin *mucwin, const char *const nick, const char *const mes
         mucwin->unread++;
     }
 
-    int ui_index = num;
-    if (ui_index == 10) {
-        ui_index = 0;
-    }
-
     // don't notify self messages
     if (strcmp(nick, my_nick) == 0) {
         return;
@@ -404,34 +399,24 @@ mucwin_message(ProfMucWin *mucwin, const char *const nick, const char *const mes
         beep();
     }
 
-    gboolean notify = FALSE;
-    char *room_setting = prefs_get_string(PREF_NOTIFY_ROOM);
-    if (g_strcmp0(room_setting, "on") == 0) {
-        notify = TRUE;
+    gboolean is_current = wins_is_current(window);
+    gboolean notify = prefs_get_notify_room(is_current, my_nick, message);
+    if (!notify) {
+        return;
     }
-    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);
+
+    Jid *jidp = jid_create(mucwin->roomjid);
+    int ui_index = num;
+    if (ui_index == 10) {
+        ui_index = 0;
     }
-    prefs_free_string(room_setting);
 
-    if (notify) {
-        gboolean is_current = wins_is_current(window);
-        if ( !is_current || (is_current && prefs_get_boolean(PREF_NOTIFY_ROOM_CURRENT)) ) {
-            Jid *jidp = jid_create(mucwin->roomjid);
-            if (prefs_get_boolean(PREF_NOTIFY_ROOM_TEXT)) {
-                notify_room_message(nick, jidp->localpart, ui_index, message);
-            } else {
-                notify_room_message(nick, jidp->localpart, ui_index, NULL);
-            }
-            jid_destroy(jidp);
-        }
+    if (prefs_get_boolean(PREF_NOTIFY_ROOM_TEXT)) {
+        notify_room_message(nick, jidp->localpart, ui_index, message);
+    } else {
+        notify_room_message(nick, jidp->localpart, ui_index, NULL);
     }
+    jid_destroy(jidp);
 }
 
 void
diff --git a/src/ui/notifier.c b/src/ui/notifier.c
index 9127b1e7..c0861009 100644
--- a/src/ui/notifier.c
+++ b/src/ui/notifier.c
@@ -73,17 +73,16 @@ notifier_uninit(void)
 }
 
 void
-notify_typing(const char *const handle)
+notify_typing(const char *const name)
 {
-    char message[strlen(handle) + 1 + 11];
-    sprintf(message, "%s: typing...", handle);
+    char message[strlen(name) + 1 + 11];
+    sprintf(message, "%s: typing...", name);
 
     _notify(message, 10000, "Incoming message");
 }
 
 void
-notify_invite(const char *const from, const char *const room,
-    const char *const reason)
+notify_invite(const char *const from, const char *const room, const char *const reason)
 {
     GString *message = g_string_new("Room invite\nfrom: ");
     g_string_append(message, from);
@@ -99,32 +98,24 @@ notify_invite(const char *const from, const char *const room,
 }
 
 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;
+    GString *message = g_string_new("");
+    g_string_append_printf(message, "%s (win %d)", name, win);
+    if (text) {
+        g_string_append_printf(message, "\n%s", text);
     }
 
-    gboolean is_current = wins_is_current(window);
-    if (!is_current || (is_current && prefs_get_boolean(PREF_NOTIFY_MESSAGE_CURRENT)) ) {
-        GString *message = g_string_new("");
-        g_string_append_printf(message, "%s (win %d)", name, num);
-
-        if (prefs_get_boolean(PREF_NOTIFY_MESSAGE_TEXT) && 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
-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("");
-    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) {
         g_string_append_printf(message, "\n%s", text);
     }
diff --git a/src/ui/privwin.c b/src/ui/privwin.c
index 6031a2c0..10ce1dbe 100644
--- a/src/ui/privwin.c
+++ b/src/ui/privwin.c
@@ -75,8 +75,32 @@ privwin_incoming_msg(ProfPrivateWin *privatewin, const char *const message, GDat
         beep();
     }
 
-    if (prefs_get_boolean(PREF_NOTIFY_MESSAGE)) {
-        notify_message(window, display_from, message);
+    if (!prefs_get_boolean(PREF_NOTIFY_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);
diff --git a/src/ui/ui.h b/src/ui/ui.h
index 032a1161..3cc40001 100644
--- a/src/ui/ui.h
+++ b/src/ui/ui.h
@@ -339,9 +339,9 @@ void win_clear(ProfWin *window);
 // desktop notifications
 void notifier_initialise(void);
 void notifier_uninit(void);
-void notify_typing(const char *const handle);
-void notify_message(ProfWin *window, const char *const name, const char *const text);
-void notify_room_message(const char *const handle, const char *const room, int win, const char *const text);
+void notify_typing(const char *const name);
+void notify_message(const char *const name, 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_invite(const char *const from, const char *const room, const char *const reason);
 void notify_subscription(const char *const from);
diff --git a/tests/unittests/ui/stub_ui.c b/tests/unittests/ui/stub_ui.c
index 084b36be..4cab2ea9 100644
--- a/tests/unittests/ui/stub_ui.c
+++ b/tests/unittests/ui/stub_ui.c
@@ -514,7 +514,7 @@ void win_clear(ProfWin *window) {}
 void notifier_uninit(void) {}
 
 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,
     int win, const char * const text) {}
 void notify_remind(void) {}
'Blame the previous revision' href='/akkartik/mu/blame/html/apps/braces.subx.html?h=hlt&id=0ca3aa4acce4f5a1378f87754761197673cdd6db'>^
a844bd05 ^
8aeb85f0 ^
a844bd05 ^
4a4a392d ^


8fa32599 ^
4a4a392d ^

a844bd05 ^
4a4a392d ^
6070c23e ^
4a4a392d ^







6070c23e ^
4a4a392d ^


1008059f ^
8aeb85f0 ^

6070c23e ^
4a4a392d ^
8fa32599 ^
4a4a392d ^
a844bd05 ^
8aeb85f0 ^
4a4a392d ^






1008059f ^
8aeb85f0 ^

6070c23e ^
4a4a392d ^
8fa32599 ^
4a4a392d ^
a844bd05 ^
8aeb85f0 ^
4a4a392d ^




1008059f ^
a844bd05 ^
4a4a392d ^



a844bd05 ^
4a4a392d ^


999c529c ^
4a4a392d ^









b1635a5c ^
4a4a392d ^




b1635a5c ^
4a4a392d ^


8aeb85f0 ^
de54d473 ^
8aeb85f0 ^

999c529c ^
8aeb85f0 ^

999c529c ^
de54d473 ^
999c529c ^
8aeb85f0 ^
999c529c ^
















4a4a392d ^
999c529c ^



8aeb85f0 ^
de54d473 ^
8aeb85f0 ^

999c529c ^
8aeb85f0 ^

999c529c ^
de54d473 ^
999c529c ^
8aeb85f0 ^
999c529c ^

























8aeb85f0 ^
de54d473 ^
8aeb85f0 ^

999c529c ^
8aeb85f0 ^

999c529c ^
de54d473 ^
999c529c ^
8aeb85f0 ^
999c529c ^



4a4a392d ^



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411