]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-live-search.c
Merge branch 'sasl'
[empathy.git] / libempathy-gtk / empathy-live-search.c
1 /*
2  * Copyright (C) 2010 Collabora Ltd.
3  * Copyright (C) 2007-2010 Nokia Corporation.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  * Authors: Felix Kaser <felix.kaser@collabora.co.uk>
20  *          Xavier Claessens <xavier.claessens@collabora.co.uk>
21  *          Claudio Saavedra <csaavedra@igalia.com>
22  */
23
24 #include <config.h>
25 #include <string.h>
26
27 #include <gtk/gtk.h>
28 #include <gdk/gdkkeysyms.h>
29
30 #include <libempathy/empathy-utils.h>
31
32 #include "empathy-live-search.h"
33 #include "empathy-gtk-marshal.h"
34
35 G_DEFINE_TYPE (EmpathyLiveSearch, empathy_live_search, GTK_TYPE_HBOX)
36
37 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyLiveSearch)
38
39 typedef struct
40 {
41   GtkWidget *search_entry;
42   GtkWidget *hook_widget;
43
44   GPtrArray *stripped_words;
45 } EmpathyLiveSearchPriv;
46
47 enum
48 {
49   PROP_0,
50   PROP_HOOK_WIDGET,
51   PROP_TEXT
52 };
53
54 enum
55 {
56   ACTIVATE,
57   KEYNAV,
58   LAST_SIGNAL
59 };
60
61 static guint signals[LAST_SIGNAL];
62
63 static void live_search_hook_widget_destroy_cb (GtkWidget *object,
64     gpointer user_data);
65
66 /**
67  * stripped_char:
68  *
69  * Returns a stripped version of @ch, removing any case, accentuation
70  * mark, or any special mark on it.
71  **/
72 static gunichar
73 stripped_char (gunichar ch)
74 {
75   gunichar retval = 0;
76   GUnicodeType utype;
77   gunichar *decomp;
78   gsize dlen;
79
80   utype = g_unichar_type (ch);
81
82   switch (utype)
83     {
84     case G_UNICODE_CONTROL:
85     case G_UNICODE_FORMAT:
86     case G_UNICODE_UNASSIGNED:
87     case G_UNICODE_NON_SPACING_MARK:
88     case G_UNICODE_COMBINING_MARK:
89     case G_UNICODE_ENCLOSING_MARK:
90       /* Ignore those */
91       break;
92     case G_UNICODE_PRIVATE_USE:
93     case G_UNICODE_SURROGATE:
94     case G_UNICODE_LOWERCASE_LETTER:
95     case G_UNICODE_MODIFIER_LETTER:
96     case G_UNICODE_OTHER_LETTER:
97     case G_UNICODE_TITLECASE_LETTER:
98     case G_UNICODE_UPPERCASE_LETTER:
99     case G_UNICODE_DECIMAL_NUMBER:
100     case G_UNICODE_LETTER_NUMBER:
101     case G_UNICODE_OTHER_NUMBER:
102     case G_UNICODE_CONNECT_PUNCTUATION:
103     case G_UNICODE_DASH_PUNCTUATION:
104     case G_UNICODE_CLOSE_PUNCTUATION:
105     case G_UNICODE_FINAL_PUNCTUATION:
106     case G_UNICODE_INITIAL_PUNCTUATION:
107     case G_UNICODE_OTHER_PUNCTUATION:
108     case G_UNICODE_OPEN_PUNCTUATION:
109     case G_UNICODE_CURRENCY_SYMBOL:
110     case G_UNICODE_MODIFIER_SYMBOL:
111     case G_UNICODE_MATH_SYMBOL:
112     case G_UNICODE_OTHER_SYMBOL:
113     case G_UNICODE_LINE_SEPARATOR:
114     case G_UNICODE_PARAGRAPH_SEPARATOR:
115     case G_UNICODE_SPACE_SEPARATOR:
116     default:
117       ch = g_unichar_tolower (ch);
118       decomp = g_unicode_canonical_decomposition (ch, &dlen);
119       if (decomp != NULL)
120         {
121           retval = decomp[0];
122           g_free (decomp);
123         }
124     }
125
126   return retval;
127 }
128
129 static void
130 append_word (GPtrArray **word_array,
131     GString **word)
132 {
133   if (*word != NULL)
134     {
135       if (*word_array == NULL)
136         *word_array = g_ptr_array_new_with_free_func (g_free);
137       g_ptr_array_add (*word_array, g_string_free (*word, FALSE));
138       *word = NULL;
139     }
140 }
141
142 static GPtrArray *
143 strip_utf8_string (const gchar *string)
144 {
145   GPtrArray *word_array = NULL;
146   GString *word = NULL;
147   const gchar *p;
148
149   if (EMP_STR_EMPTY (string))
150     return NULL;
151
152   for (p = string; *p != '\0'; p = g_utf8_next_char (p))
153     {
154       gunichar sc;
155
156       /* Make the char lower-case, remove its accentuation marks, and ignore it
157        * if it is just unicode marks */
158       sc = stripped_char (g_utf8_get_char (p));
159       if (sc == 0)
160         continue;
161
162       /* If it is not alpha-num, it is separator between words */
163       if (!g_unichar_isalnum (sc))
164         {
165           append_word (&word_array, &word);
166           continue;
167         }
168
169       /* It is alpha-num, append this char to current word, or start new word */
170       if (word == NULL)
171         word = g_string_new (NULL);
172       g_string_append_unichar (word, sc);
173     }
174
175   append_word (&word_array, &word);
176
177   return word_array;
178 }
179
180 static gboolean
181 live_search_match_prefix (const gchar *string,
182     const gchar *prefix)
183 {
184   const gchar *p;
185   const gchar *prefix_p;
186   gboolean next_word = FALSE;
187
188   if (prefix == NULL || prefix[0] == 0)
189     return TRUE;
190
191   if (EMP_STR_EMPTY (string))
192     return FALSE;
193
194   prefix_p = prefix;
195   for (p = string; *p != '\0'; p = g_utf8_next_char (p))
196     {
197       gunichar sc;
198
199       /* Make the char lower-case, remove its accentuation marks, and ignore it
200        * if it is just unicode marks */
201       sc = stripped_char (g_utf8_get_char (p));
202       if (sc == 0)
203         continue;
204
205       /* If we want to go to next word, ignore alpha-num chars */
206       if (next_word && g_unichar_isalnum (sc))
207         continue;
208       next_word = FALSE;
209
210       /* Ignore word separators */
211       if (!g_unichar_isalnum (sc))
212         continue;
213
214       /* If this char does not match prefix_p, go to next word and start again
215        * from the beginning of prefix */
216       if (sc != g_utf8_get_char (prefix_p))
217         {
218           next_word = TRUE;
219           prefix_p = prefix;
220           continue;
221         }
222
223       /* prefix_p match, verify to next char. If this was the last of prefix,
224        * it means it completely machted and we are done. */
225       prefix_p = g_utf8_next_char (prefix_p);
226       if (*prefix_p == '\0')
227         return TRUE;
228     }
229
230   return FALSE;
231 }
232
233 static gboolean
234 live_search_match_words (const gchar *string,
235     GPtrArray *words)
236 {
237   guint i;
238
239   if (words == NULL)
240     return TRUE;
241
242   for (i = 0; i < words->len; i++)
243     if (!live_search_match_prefix (string, g_ptr_array_index (words, i)))
244       return FALSE;
245
246   return TRUE;
247 }
248
249 static gboolean
250 live_search_entry_key_pressed_cb (GtkEntry *entry,
251     GdkEventKey *event,
252     gpointer user_data)
253 {
254   EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (user_data);
255   gboolean ret;
256
257   /* if esc key pressed, hide the search */
258   if (event->keyval == GDK_KEY_Escape)
259     {
260       gtk_widget_hide (GTK_WIDGET (self));
261       return TRUE;
262     }
263
264   /* emit key navigation signal, so other widgets can respond to it properly */
265   if (event->keyval == GDK_KEY_Up || event->keyval == GDK_KEY_Down
266       || event->keyval == GDK_KEY_Left || event->keyval == GDK_KEY_Right)
267      {
268        g_signal_emit (self, signals[KEYNAV], 0, event, &ret);
269        return ret;
270      }
271
272   return FALSE;
273 }
274
275 static void
276 live_search_text_changed (GtkEntry *entry,
277     gpointer user_data)
278 {
279   EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (user_data);
280   EmpathyLiveSearchPriv *priv = GET_PRIV (self);
281   const gchar *text;
282
283   text = gtk_entry_get_text (entry);
284
285   if (EMP_STR_EMPTY (text))
286     gtk_widget_hide (GTK_WIDGET (self));
287   else
288     gtk_widget_show (GTK_WIDGET (self));
289
290   if (priv->stripped_words != NULL)
291     g_ptr_array_unref (priv->stripped_words);
292
293   priv->stripped_words = strip_utf8_string (text);
294
295   g_object_notify (G_OBJECT (self), "text");
296 }
297
298 static void
299 live_search_close_pressed (GtkEntry *entry,
300     GtkEntryIconPosition icon_pos,
301     GdkEvent *event,
302     gpointer user_data)
303 {
304   EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (user_data);
305
306   gtk_widget_hide (GTK_WIDGET (self));
307 }
308
309 static gboolean
310 live_search_key_press_event_cb (GtkWidget *widget,
311     GdkEventKey *event,
312     gpointer user_data)
313 {
314   EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (user_data);
315   EmpathyLiveSearchPriv *priv = GET_PRIV (self);
316   GdkEvent *new_event;
317   gboolean ret;
318
319   /* dont forward this event to the entry, else the event is consumed by the
320    * entry and does not close the window */
321   if (!gtk_widget_get_visible (GTK_WIDGET (self)) &&
322       event->keyval == GDK_KEY_Escape)
323     return FALSE;
324
325   /* do not show the search if CTRL and/or ALT are pressed with a key
326    * this is needed, because otherwise the CTRL + F accel would not work,
327    * because the entry consumes it */
328   if (event->state & (GDK_MOD1_MASK | GDK_CONTROL_MASK) ||
329       event->keyval == GDK_KEY_Control_L ||
330       event->keyval == GDK_KEY_Control_R)
331     return FALSE;
332
333   /* dont forward the up and down arrow keys to the entry, they are needed for
334    * navigation in the treeview and are not needed in the search entry */
335    if (event->keyval == GDK_KEY_Up || event->keyval == GDK_KEY_Down)
336      return FALSE;
337
338   /* realize the widget if it is not realized yet */
339   gtk_widget_realize (priv->search_entry);
340   if (!gtk_widget_has_focus (priv->search_entry))
341     {
342       gtk_widget_grab_focus (priv->search_entry);
343       gtk_editable_set_position (GTK_EDITABLE (priv->search_entry), -1);
344     }
345
346   /* forward the event to the search entry */
347   new_event = gdk_event_copy ((GdkEvent *) event);
348   ret = gtk_widget_event (priv->search_entry, new_event);
349   gdk_event_free (new_event);
350
351   return ret;
352 }
353
354 static void
355 live_search_entry_activate_cb (GtkEntry *entry,
356     EmpathyLiveSearch *self)
357 {
358   g_signal_emit (self, signals[ACTIVATE], 0);
359 }
360
361 static void
362 live_search_release_hook_widget (EmpathyLiveSearch *self)
363 {
364   EmpathyLiveSearchPriv *priv = GET_PRIV (self);
365
366   /* remove old handlers if old source was not null */
367   if (priv->hook_widget != NULL)
368     {
369       g_signal_handlers_disconnect_by_func (priv->hook_widget,
370           live_search_key_press_event_cb, self);
371       g_signal_handlers_disconnect_by_func (priv->hook_widget,
372           live_search_hook_widget_destroy_cb, self);
373       g_object_unref (priv->hook_widget);
374       priv->hook_widget = NULL;
375     }
376 }
377
378 static void
379 live_search_hook_widget_destroy_cb (GtkWidget *object,
380     gpointer user_data)
381 {
382   EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (user_data);
383
384   /* unref the hook widget and hide search */
385   gtk_widget_hide (GTK_WIDGET (self));
386   live_search_release_hook_widget (self);
387 }
388
389 static void
390 live_search_dispose (GObject *obj)
391 {
392   EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (obj);
393
394   live_search_release_hook_widget (self);
395
396   if (G_OBJECT_CLASS (empathy_live_search_parent_class)->dispose != NULL)
397     G_OBJECT_CLASS (empathy_live_search_parent_class)->dispose (obj);
398 }
399
400 static void
401 live_search_finalize (GObject *obj)
402 {
403   EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (obj);
404   EmpathyLiveSearchPriv *priv = GET_PRIV (self);
405
406   if (priv->stripped_words != NULL)
407     g_ptr_array_unref (priv->stripped_words);
408
409   if (G_OBJECT_CLASS (empathy_live_search_parent_class)->finalize != NULL)
410     G_OBJECT_CLASS (empathy_live_search_parent_class)->finalize (obj);
411 }
412
413 static void
414 live_search_get_property (GObject *object,
415     guint param_id,
416     GValue *value,
417     GParamSpec *pspec)
418 {
419   EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (object);
420
421   switch (param_id)
422     {
423     case PROP_HOOK_WIDGET:
424       g_value_set_object (value, empathy_live_search_get_hook_widget (self));
425       break;
426     case PROP_TEXT:
427       g_value_set_string (value, empathy_live_search_get_text (self));
428       break;
429     default:
430       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
431       break;
432     }
433 }
434
435 static void
436 live_search_set_property (GObject *object,
437     guint param_id,
438     const GValue *value,
439     GParamSpec *pspec)
440 {
441   EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (object);
442
443   switch (param_id) {
444   case PROP_HOOK_WIDGET:
445     empathy_live_search_set_hook_widget (self, g_value_get_object (value));
446     break;
447   case PROP_TEXT:
448     empathy_live_search_set_text (self, g_value_get_string (value));
449     break;
450   default:
451     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
452     break;
453   };
454 }
455
456 static void
457 live_search_hide (GtkWidget *widget)
458 {
459   EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (widget);
460   EmpathyLiveSearchPriv *priv = GET_PRIV (self);
461
462   GTK_WIDGET_CLASS (empathy_live_search_parent_class)->hide (widget);
463
464   gtk_entry_set_text (GTK_ENTRY (priv->search_entry), "");
465   gtk_widget_grab_focus (priv->hook_widget);
466 }
467
468 static void
469 live_search_show (GtkWidget *widget)
470 {
471   EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (widget);
472   EmpathyLiveSearchPriv *priv = GET_PRIV (self);
473
474   if (!gtk_widget_has_focus (priv->search_entry))
475     gtk_widget_grab_focus (priv->search_entry);
476
477   GTK_WIDGET_CLASS (empathy_live_search_parent_class)->show (widget);
478 }
479
480 static void
481 live_search_grab_focus (GtkWidget *widget)
482 {
483   EmpathyLiveSearch *self = EMPATHY_LIVE_SEARCH (widget);
484   EmpathyLiveSearchPriv *priv = GET_PRIV (self);
485
486   if (!gtk_widget_has_focus (priv->search_entry))
487     {
488       gtk_widget_grab_focus (priv->search_entry);
489       gtk_editable_set_position (GTK_EDITABLE (priv->search_entry), -1);
490     }
491 }
492
493 static void
494 empathy_live_search_class_init (EmpathyLiveSearchClass *klass)
495 {
496   GObjectClass *object_class = (GObjectClass *) klass;
497   GtkWidgetClass *widget_class = (GtkWidgetClass *) klass;
498   GParamSpec *param_spec;
499
500   object_class->finalize = live_search_finalize;
501   object_class->dispose = live_search_dispose;
502   object_class->get_property = live_search_get_property;
503   object_class->set_property = live_search_set_property;
504
505   widget_class->hide = live_search_hide;
506   widget_class->show = live_search_show;
507   widget_class->grab_focus = live_search_grab_focus;
508
509   signals[ACTIVATE] = g_signal_new ("activate",
510       G_TYPE_FROM_CLASS (object_class),
511       G_SIGNAL_RUN_LAST,
512       0,
513       NULL, NULL,
514       g_cclosure_marshal_VOID__VOID,
515       G_TYPE_NONE, 0);
516
517   signals[KEYNAV] = g_signal_new ("key-navigation",
518       G_TYPE_FROM_CLASS (object_class),
519       G_SIGNAL_RUN_LAST,
520       0,
521       g_signal_accumulator_true_handled, NULL,
522       _empathy_gtk_marshal_BOOLEAN__BOXED,
523       G_TYPE_BOOLEAN, 1, GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
524
525   param_spec = g_param_spec_object ("hook-widget", "Live Search Hook Widget",
526       "The live search catches key-press-events on this widget",
527       GTK_TYPE_WIDGET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
528   g_object_class_install_property (object_class, PROP_HOOK_WIDGET,
529       param_spec);
530
531   param_spec = g_param_spec_string ("text", "Live Search Text",
532       "The text of the live search entry",
533       "", G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
534   g_object_class_install_property (object_class, PROP_TEXT, param_spec);
535
536   g_type_class_add_private (klass, sizeof (EmpathyLiveSearchPriv));
537 }
538
539 static void
540 empathy_live_search_init (EmpathyLiveSearch *self)
541 {
542   EmpathyLiveSearchPriv *priv =
543     G_TYPE_INSTANCE_GET_PRIVATE ((self), EMPATHY_TYPE_LIVE_SEARCH,
544         EmpathyLiveSearchPriv);
545
546   gtk_widget_set_no_show_all (GTK_WIDGET (self), TRUE);
547
548   priv->search_entry = gtk_entry_new ();
549   gtk_entry_set_icon_from_stock (GTK_ENTRY (priv->search_entry),
550       GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLOSE);
551   gtk_entry_set_icon_activatable (GTK_ENTRY (priv->search_entry),
552       GTK_ENTRY_ICON_SECONDARY, TRUE);
553   gtk_entry_set_icon_sensitive (GTK_ENTRY (priv->search_entry),
554       GTK_ENTRY_ICON_SECONDARY, TRUE);
555   gtk_widget_show (priv->search_entry);
556
557   gtk_box_pack_start (GTK_BOX (self), priv->search_entry, TRUE, TRUE, 0);
558
559   g_signal_connect (priv->search_entry, "icon_release",
560       G_CALLBACK (live_search_close_pressed), self);
561   g_signal_connect (priv->search_entry, "changed",
562       G_CALLBACK (live_search_text_changed), self);
563   g_signal_connect (priv->search_entry, "key-press-event",
564       G_CALLBACK (live_search_entry_key_pressed_cb), self);
565   g_signal_connect (priv->search_entry, "activate",
566       G_CALLBACK (live_search_entry_activate_cb), self);
567
568   priv->hook_widget = NULL;
569
570   self->priv = priv;
571 }
572
573 GtkWidget *
574 empathy_live_search_new (GtkWidget *hook)
575 {
576   g_return_val_if_fail (hook == NULL || GTK_IS_WIDGET (hook), NULL);
577
578   return g_object_new (EMPATHY_TYPE_LIVE_SEARCH,
579       "hook-widget", hook,
580       NULL);
581 }
582
583 /* public methods */
584
585 GtkWidget *
586 empathy_live_search_get_hook_widget (EmpathyLiveSearch *self)
587 {
588   EmpathyLiveSearchPriv *priv = GET_PRIV (self);
589
590   g_return_val_if_fail (EMPATHY_IS_LIVE_SEARCH (self), NULL);
591
592   return priv->hook_widget;
593 }
594
595 void
596 empathy_live_search_set_hook_widget (EmpathyLiveSearch *self,
597     GtkWidget *hook)
598 {
599   EmpathyLiveSearchPriv *priv;
600
601   g_return_if_fail (EMPATHY_IS_LIVE_SEARCH (self));
602   g_return_if_fail (hook == NULL || GTK_IS_WIDGET (hook));
603
604   priv = GET_PRIV (self);
605
606   /* release the actual widget */
607   live_search_release_hook_widget (self);
608
609   /* connect handlers if new source is not null */
610   if (hook != NULL)
611     {
612       priv->hook_widget = g_object_ref (hook);
613       g_signal_connect (priv->hook_widget, "key-press-event",
614           G_CALLBACK (live_search_key_press_event_cb),
615           self);
616       g_signal_connect (priv->hook_widget, "destroy",
617           G_CALLBACK (live_search_hook_widget_destroy_cb),
618           self);
619     }
620 }
621
622 const gchar *
623 empathy_live_search_get_text (EmpathyLiveSearch *self)
624 {
625   EmpathyLiveSearchPriv *priv = GET_PRIV (self);
626
627   g_return_val_if_fail (EMPATHY_IS_LIVE_SEARCH (self), NULL);
628
629   return gtk_entry_get_text (GTK_ENTRY (priv->search_entry));
630 }
631
632 void
633 empathy_live_search_set_text (EmpathyLiveSearch *self,
634     const gchar *text)
635 {
636   EmpathyLiveSearchPriv *priv = GET_PRIV (self);
637
638   g_return_if_fail (EMPATHY_IS_LIVE_SEARCH (self));
639   g_return_if_fail (text != NULL);
640
641   gtk_entry_set_text (GTK_ENTRY (priv->search_entry), text);
642 }
643
644 /**
645  * empathy_live_search_match:
646  * @self: a #EmpathyLiveSearch
647  * @string: a string where to search, must be valid UTF-8.
648  *
649  * Search if one of the words in @string string starts with the current text
650  * of @self.
651  *
652  * Searching for "aba" in "Abasto" will match, searching in "Moraba" will not,
653  * and searching in "A tool (abacus)" will do.
654  *
655  * The match is not case-sensitive, and regardless of the accentuation marks.
656  *
657  * Returns: %TRUE if a match is found, %FALSE otherwise.
658  *
659  **/
660 gboolean
661 empathy_live_search_match (EmpathyLiveSearch *self,
662     const gchar *string)
663 {
664   EmpathyLiveSearchPriv *priv;
665
666   g_return_val_if_fail (EMPATHY_IS_LIVE_SEARCH (self), FALSE);
667
668   priv = GET_PRIV (self);
669
670   return live_search_match_words (string, priv->stripped_words);
671 }
672
673 gboolean
674 empathy_live_search_match_string (const gchar *string,
675     const gchar *prefix)
676 {
677   GPtrArray *words;
678   gboolean match;
679
680   words = strip_utf8_string (prefix);
681   match = live_search_match_words (string, words);
682   if (words != NULL)
683     g_ptr_array_unref (words);
684
685   return match;
686 }
687