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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* Copyright (C) 2005-2007 Imendio AB
* Copyright (C) 2009 Collabora Ltd.
*
* This program 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 2 of the
* License, or (at your option) any later version.
*
* This program 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 this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* Authors: Richard Hult <richard@imendio.com>
* Martyn Russell <martyn@imendio.com>
* Xavier Claessens <xclaesse@gmail.com>
* Davyd Madeley <davyd.madeley@collabora.co.uk>
*/
#include "config.h"
#include <string.h>
#include <stdlib.h>
#include <glib/gi18n-lib.h>
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include <telepathy-glib/util.h>
#include <libempathy/empathy-connectivity.h>
#include <libempathy/empathy-idle.h>
#include <libempathy/empathy-utils.h>
#include <libempathy/empathy-status-presets.h>
#include <libempathy/empathy-account-manager.h>
#define DEBUG_FLAG EMPATHY_DEBUG_OTHER
#include <libempathy/empathy-debug.h>
#include "empathy-ui-utils.h"
#include "empathy-images.h"
#include "empathy-presence-chooser.h"
#include "empathy-status-preset-dialog.h"
/**
* SECTION:empathy-presence-chooser
* @title:EmpathyPresenceChooser
* @short_description: A widget used to change presence
* @include: libempathy-gtk/empathy-presence-chooser.h
*
* #EmpathyPresenceChooser is a widget which extends #GtkComboBoxEntry
* to change presence.
*/
/**
* EmpathyAccountChooser:
* @parent: parent object
*
* Widget which extends #GtkComboBoxEntry to change presence.
*/
/* Flashing delay for icons (milliseconds). */
#define FLASH_TIMEOUT 500
#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyPresenceChooser)
/* For custom message dialog */
enum {
COL_ICON,
COL_LABEL,
COL_PRESENCE,
COL_COUNT
};
/* For combobox's model */
enum {
COL_STATUS_TEXT,
COL_STATE_ICON_NAME,
COL_STATE,
COL_DISPLAY_MARKUP,
COL_STATUS_CUSTOMISABLE,
COL_TYPE,
N_COLUMNS
};
typedef enum {
ENTRY_TYPE_BUILTIN,
ENTRY_TYPE_SAVED,
ENTRY_TYPE_CUSTOM,
ENTRY_TYPE_SEPARATOR,
ENTRY_TYPE_EDIT_CUSTOM,
} PresenceChooserEntryType;
typedef struct {
EmpathyIdle *idle;
EmpathyConnectivity *connectivity;
gboolean editing_status;
int block_set_editing;
int block_changed;
guint focus_out_idle_source;
TpConnectionPresenceType state;
PresenceChooserEntryType previous_type;
TpConnectionPresenceType flash_state_1;
TpConnectionPresenceType flash_state_2;
guint flash_timeout_id;
EmpathyAccountManager *account_manager;
} EmpathyPresenceChooserPriv;
/* States to be listed in the menu.
* Each state has a boolean telling if it can have custom message */
static struct { TpConnectionPresenceType state;
gboolean customisable;
} states[] = { { TP_CONNECTION_PRESENCE_TYPE_AVAILABLE, TRUE } ,
{ TP_CONNECTION_PRESENCE_TYPE_BUSY, TRUE },
{ TP_CONNECTION_PRESENCE_TYPE_AWAY, TRUE },
{ TP_CONNECTION_PRESENCE_TYPE_HIDDEN, FALSE },
{ TP_CONNECTION_PRESENCE_TYPE_OFFLINE, FALSE},
{ TP_CONNECTION_PRESENCE_TYPE_UNSET, },
};
static void presence_chooser_finalize (GObject *object);
static void presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser);
static gboolean presence_chooser_flash_timeout_cb (EmpathyPresenceChooser *chooser);
static void presence_chooser_flash_start (EmpathyPresenceChooser *chooser,
TpConnectionPresenceType state_1,
TpConnectionPresenceType state_2);
static void presence_chooser_flash_stop (EmpathyPresenceChooser *chooser,
TpConnectionPresenceType state);
static void presence_chooser_menu_add_item (GtkWidget *menu,
const gchar *str,
TpConnectionPresenceType state);
static void presence_chooser_noncustom_activate_cb (GtkWidget *item,
gpointer user_data);
static void presence_chooser_set_state (TpConnectionPresenceType state,
const gchar *status);
static void presence_chooser_custom_activate_cb (GtkWidget *item,
gpointer user_data);
G_DEFINE_TYPE (EmpathyPresenceChooser, empathy_presence_chooser, GTK_TYPE_COMBO_BOX_ENTRY);
static void
empathy_presence_chooser_class_init (EmpathyPresenceChooserClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = presence_chooser_finalize;
g_type_class_add_private (object_class, sizeof (EmpathyPresenceChooserPriv));
}
static void
presence_chooser_create_model (EmpathyPresenceChooser *self)
{
GtkListStore *store;
char *custom_message;
int i;
store = gtk_list_store_new (N_COLUMNS,
G_TYPE_STRING, /* COL_STATUS_TEXT */
G_TYPE_STRING, /* COL_STATE_ICON_NAME */
G_TYPE_UINT, /* COL_STATE */
G_TYPE_STRING, /* COL_DISPLAY_MARKUP */
G_TYPE_BOOLEAN, /* COL_STATUS_CUSTOMISABLE */
G_TYPE_INT); /* COL_TYPE */
custom_message = g_strdup_printf ("<i>%s</i>", _("Custom Message..."));
for (i = 0; states[i].state != TP_CONNECTION_PRESENCE_TYPE_UNSET; i++) {
GList *list, *l;
const char *status, *icon_name;
status = empathy_presence_get_default_message (states[i].state);
icon_name = empathy_icon_name_for_presence (states[i].state);
gtk_list_store_insert_with_values (store, NULL, -1,
COL_STATUS_TEXT, status,
COL_STATE_ICON_NAME, icon_name,
COL_STATE, states[i].state,
COL_DISPLAY_MARKUP, status,
COL_STATUS_CUSTOMISABLE, states[i].customisable,
COL_TYPE, ENTRY_TYPE_BUILTIN,
-1);
if (states[i].customisable) {
/* Set custom messages if wanted */
list = empathy_status_presets_get (states[i].state, -1);
list = g_list_sort (list, (GCompareFunc) g_utf8_collate);
for (l = list; l; l = l->next) {
gtk_list_store_insert_with_values (store,
NULL, -1,
COL_STATUS_TEXT, l->data,
COL_STATE_ICON_NAME, icon_name,
COL_STATE, states[i].state,
COL_DISPLAY_MARKUP, l->data,
COL_STATUS_CUSTOMISABLE, TRUE,
COL_TYPE, ENTRY_TYPE_SAVED,
-1);
}
g_list_free (list);
gtk_list_store_insert_with_values (store, NULL, -1,
COL_STATUS_TEXT, _("Custom Message..."),
COL_STATE_ICON_NAME, icon_name,
COL_STATE, states[i].state,
COL_DISPLAY_MARKUP, custom_message,
COL_STATUS_CUSTOMISABLE, TRUE,
COL_TYPE, ENTRY_TYPE_CUSTOM,
-1);
}
}
/* add a separator */
gtk_list_store_insert_with_values (store, NULL, -1,
COL_TYPE, ENTRY_TYPE_SEPARATOR,
-1);
gtk_list_store_insert_with_values (store, NULL, -1,
COL_STATUS_TEXT, _("Edit Custom Messages..."),
COL_STATE_ICON_NAME, GTK_STOCK_EDIT,
COL_DISPLAY_MARKUP, _("Edit Custom Messages..."),
COL_TYPE, ENTRY_TYPE_EDIT_CUSTOM,
-1);
g_free (custom_message);
gtk_combo_box_set_model (GTK_COMBO_BOX (self), GTK_TREE_MODEL (store));
g_object_unref (store);
}
static void
presence_chooser_popup_shown_cb (GObject *self,
GParamSpec *pspec,
gpointer user_data)
{
EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
gboolean shown;
g_object_get (self, "popup-shown", &shown, NULL);
if (!shown) {
return;
}
/* see presence_chooser_entry_focus_out_cb () for what this does */
if (priv->focus_out_idle_source != 0) {
g_source_remove (priv->focus_out_idle_source);
priv->focus_out_idle_source = 0;
}
presence_chooser_create_model (EMPATHY_PRESENCE_CHOOSER (self));
}
static PresenceChooserEntryType
presence_chooser_get_entry_type (EmpathyPresenceChooser *self)
{
GtkTreeIter iter;
PresenceChooserEntryType type = -1;
if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (self), &iter)) {
type = ENTRY_TYPE_CUSTOM;
}
else {
GtkTreeModel *model;
model = gtk_combo_box_get_model (GTK_COMBO_BOX (self));
gtk_tree_model_get (model, &iter,
COL_TYPE, &type,
-1);
}
return type;
}
static gboolean
presence_chooser_is_preset (EmpathyPresenceChooser *self)
{
EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
TpConnectionPresenceType state;
const char *status;
GList *presets, *l;
gboolean match = FALSE;
state = empathy_idle_get_state (priv->idle);
status = empathy_idle_get_status (priv->idle);
presets = empathy_status_presets_get (state, -1);
for (l = presets; l; l = l->next) {
char *preset = (char *) l->data;
if (!tp_strdiff (status, preset)) {
match = TRUE;
break;
}
}
g_list_free (presets);
DEBUG ("is_preset(%i, %s) = %i", state, status, match);
return match;
}
static void
presence_chooser_set_favorite_icon (EmpathyPresenceChooser *self)
{
GtkWidget *entry;
PresenceChooserEntryType type;
entry = gtk_bin_get_child (GTK_BIN (self));
type = presence_chooser_get_entry_type (self);
if (type == ENTRY_TYPE_CUSTOM || type == ENTRY_TYPE_SAVED) {
if (presence_chooser_is_preset (self)) {
/* saved entries can be removed from the list */
gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
GTK_ENTRY_ICON_SECONDARY,
"empathy-starred");
gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
GTK_ENTRY_ICON_SECONDARY,
_("Click to remove this status as a favorite"));
}
else {
/* custom entries can be favorited */
gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
GTK_ENTRY_ICON_SECONDARY,
"empathy-unstarred");
gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
GTK_ENTRY_ICON_SECONDARY,
_("Click to make this status a favorite"));
}
}
else {
/* built-in entries cannot be favorited */
gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
GTK_ENTRY_ICON_SECONDARY,
NULL);
gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
GTK_ENTRY_ICON_SECONDARY,
NULL);
}
}
static void
presence_chooser_set_status_editing (EmpathyPresenceChooser *self,
gboolean editing)
{
EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
GtkWidget *entry;
if (priv->block_set_editing) {
return;
}
entry = gtk_bin_get_child (GTK_BIN (self));
if (editing) {
priv->editing_status = TRUE;
gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
GTK_ENTRY_ICON_SECONDARY,
GTK_STOCK_OK);
gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
GTK_ENTRY_ICON_SECONDARY,
_("Set status"));
gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
GTK_ENTRY_ICON_PRIMARY,
FALSE);
} else {
GtkWidget *window;
presence_chooser_set_favorite_icon (self);
gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
GTK_ENTRY_ICON_PRIMARY,
TRUE);
/* attempt to get the toplevel for this widget */
window = gtk_widget_get_toplevel (GTK_WIDGET (self));
if (GTK_WIDGET_TOPLEVEL (window) && GTK_IS_WINDOW (window)) {
/* unset the focus */
gtk_window_set_focus (GTK_WINDOW (window), NULL);
}
/* see presence_chooser_entry_focus_out_cb ()
* for what this does */
if (priv->focus_out_idle_source != 0) {
g_source_remove (priv->focus_out_idle_source);
priv->focus_out_idle_source = 0;
}
gtk_editable_set_position (GTK_EDITABLE (entry), 0);
priv->editing_status = FALSE;
}
}
static void
mc_set_custom_state (EmpathyPresenceChooser *self)
{
EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
GtkWidget *entry;
const char *status;
entry = gtk_bin_get_child (GTK_BIN (self));
/* update the status with MC */
status = gtk_entry_get_text (GTK_ENTRY (entry));
DEBUG ("Sending state to MC-> %d (%s)", priv->state, status);
empathy_idle_set_presence (priv->idle, priv->state, status);
}
static void
ui_set_custom_state (EmpathyPresenceChooser *self,
TpConnectionPresenceType state,
const char *status)
{
EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
GtkWidget *entry;
const char *icon_name;
entry = gtk_bin_get_child (GTK_BIN (self));
priv->block_set_editing++;
priv->block_changed++;
icon_name = empathy_icon_name_for_presence (state);
gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
GTK_ENTRY_ICON_PRIMARY,
icon_name);
gtk_entry_set_text (GTK_ENTRY (entry), status == NULL ? "" : status);
presence_chooser_set_favorite_icon (self);
priv->block_changed--;
priv->block_set_editing--;
}
static void
presence_chooser_reset_status (EmpathyPresenceChooser *self)
{
/* recover the status that was unset */
presence_chooser_set_status_editing (self, FALSE);
presence_chooser_presence_changed_cb (self);
}
static void
presence_chooser_entry_icon_release_cb (EmpathyPresenceChooser *self,
GtkEntryIconPosition icon_pos,
GdkEvent *event,
GtkEntry *entry)
{
EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
if (priv->editing_status) {
presence_chooser_set_status_editing (self, FALSE);
mc_set_custom_state (self);
}
else {
PresenceChooserEntryType type;
TpConnectionPresenceType state;
const char *status;
type = presence_chooser_get_entry_type (self);
state = empathy_idle_get_state (priv->idle);
status = empathy_idle_get_status (priv->idle);
if (!empathy_status_presets_is_valid (state))
/* It doesn't make sense to add such presence as favorite */
return;
if (presence_chooser_is_preset (self)) {
/* remove the entry */
DEBUG ("REMOVING PRESET (%i, %s)", state, status);
empathy_status_presets_remove (state, status);
}
else {
/* save the entry */
DEBUG ("SAVING PRESET (%i, %s)", state, status);
empathy_status_presets_set_last (state, status);
}
/* update the icon */
presence_chooser_set_favorite_icon (self);
}
}
static void
presence_chooser_entry_activate_cb (EmpathyPresenceChooser *self,
GtkEntry *entry)
{
presence_chooser_set_status_editing (self, FALSE);
mc_set_custom_state (self);
}
static gboolean
presence_chooser_entry_key_press_event_cb (EmpathyPresenceChooser *self,
GdkEventKey *event,
GtkWidget *entry)
{
EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
if (priv->editing_status && event->keyval == GDK_Escape) {
/* the user pressed Escape, undo the editing */
presence_chooser_reset_status (self);
return TRUE;
}
else if (event->keyval == GDK_Up || event->keyval == GDK_Down) {
/* ignore */
return TRUE;
}
return FALSE; /* send this event elsewhere */
}
static gboolean
presence_chooser_entry_button_press_event_cb (EmpathyPresenceChooser *self,
GdkEventButton *event,
GtkWidget *entry)
{
EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
if (!priv->editing_status &&
event->button == 1 &&
!GTK_WIDGET_HAS_FOCUS (entry)) {
gtk_widget_grab_focus (entry);
gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
return TRUE;
}
return FALSE;
}
static void
presence_chooser_entry_changed_cb (EmpathyPresenceChooser *self,
GtkEntry *entry)
{
EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
if (priv->block_changed){
return;
}
/* the combo is being edited to a custom entry */
if (!priv->editing_status) {
presence_chooser_set_status_editing (self, TRUE);
}
}
static void
presence_chooser_changed_cb (GtkComboBox *self, gpointer user_data)
{
EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
GtkTreeIter iter;
char *icon_name;
TpConnectionPresenceType new_state;
gboolean customisable = TRUE;
PresenceChooserEntryType type = -1;
GtkWidget *entry;
GtkTreeModel *model;
if (priv->block_changed ||
!gtk_combo_box_get_active_iter (self, &iter)) {
return;
}
model = gtk_combo_box_get_model (self);
gtk_tree_model_get (model, &iter,
COL_STATE_ICON_NAME, &icon_name,
COL_STATE, &new_state,
COL_STATUS_CUSTOMISABLE, &customisable,
COL_TYPE, &type,
-1);
entry = gtk_bin_get_child (GTK_BIN (self));
/* some types of status aren't editable, set the editability of the
* entry appropriately. Unless we're just about to reset it anyway,
* in which case, don't fiddle with it */
if (type != ENTRY_TYPE_EDIT_CUSTOM) {
gtk_editable_set_editable (GTK_EDITABLE (entry), customisable);
priv->state = new_state;
}
if (type == ENTRY_TYPE_EDIT_CUSTOM) {
GtkWidget *window, *dialog;
presence_chooser_reset_status (EMPATHY_PRESENCE_CHOOSER (self));
/* attempt to get the toplevel for this widget */
window = gtk_widget_get_toplevel (GTK_WIDGET (self));
if (!GTK_WIDGET_TOPLEVEL (window) || !GTK_IS_WINDOW (window)) {
window = NULL;
}
dialog = empathy_status_preset_dialog_new (GTK_WINDOW (window));
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
}
else if (type == ENTRY_TYPE_CUSTOM) {
gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
GTK_ENTRY_ICON_PRIMARY,
icon_name);
/* preseed the status */
if (priv->previous_type == ENTRY_TYPE_BUILTIN) {
/* if their previous entry was a builtin, don't
* preseed */
gtk_entry_set_text (GTK_ENTRY (entry), "");
} else {
/* else preseed the text of their currently entered
* status message */
const char *status;
status = empathy_idle_get_status (priv->idle);
gtk_entry_set_text (GTK_ENTRY (entry), status);
}
/* grab the focus */
gtk_widget_grab_focus (entry);
} else {
char *status;
/* just in case we were setting a new status when
* things were changed */
presence_chooser_set_status_editing (
EMPATHY_PRESENCE_CHOOSER (self),
FALSE);
gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
GTK_ENTRY_ICON_PRIMARY,
icon_name);
gtk_tree_model_get (model, &iter,
COL_STATUS_TEXT, &status,
-1);
empathy_idle_set_presence (priv->idle, priv->state, status);
g_free (status);
}
if (type != ENTRY_TYPE_EDIT_CUSTOM) {
priv->previous_type = type;
}
g_free (icon_name);
}
static gboolean
combo_row_separator_func (GtkTreeModel *model,
GtkTreeIter *iter,
gpointer data)
{
PresenceChooserEntryType type;
gtk_tree_model_get (model, iter,
COL_TYPE, &type,
-1);
return (type == ENTRY_TYPE_SEPARATOR);
}
static gboolean
presence_chooser_entry_focus_out_idle_cb (gpointer user_data)
{
EmpathyPresenceChooser *chooser;
GtkWidget *entry;
DEBUG ("Autocommiting status message");
chooser = EMPATHY_PRESENCE_CHOOSER (user_data);
entry = gtk_bin_get_child (GTK_BIN (chooser));
presence_chooser_entry_activate_cb (chooser, GTK_ENTRY (entry));
return FALSE;
}
static gboolean
presence_chooser_entry_focus_out_cb (EmpathyPresenceChooser *chooser,
GdkEventFocus *event,
GtkEntry *entry)
{
EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
if (priv->editing_status) {
/* this seems a bit evil and maybe it will be fragile,
* someone should think of a better way to do it.
*
* The entry has focused out, but we don't know where the focus
* has gone. If it goes to the combo box, we don't want to
* do anything. If it's gone anywhere else, we want to commit
* the result.
*
* Thus we install this idle handler and store its source.
* If the source is scheduled when the popup handler runs,
* it will remove it, else the callback will commit the result.
*/
priv->focus_out_idle_source = g_idle_add (
presence_chooser_entry_focus_out_idle_cb,
chooser);
}
gtk_editable_set_position (GTK_EDITABLE (entry), 0);
return FALSE;
}
static void
presence_chooser_update_sensitivity (EmpathyPresenceChooser *chooser)
{
EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
gboolean sensitive = FALSE;
GList *accounts, *l;
accounts = empathy_account_manager_dup_accounts (priv->account_manager);
for (l = accounts ; l != NULL ; l = g_list_next (l)) {
EmpathyAccount *a = EMPATHY_ACCOUNT (l->data);
if (empathy_account_is_enabled (a))
sensitive = TRUE;
g_object_unref (a);
}
g_list_free (accounts);
if (!empathy_connectivity_is_online (priv->connectivity))
sensitive = FALSE;
gtk_widget_set_sensitive (GTK_WIDGET (chooser), sensitive);
}
static void
presence_chooser_account_manager_account_changed_cb (
EmpathyAccountManager *manager,
EmpathyAccount *account,
EmpathyPresenceChooser *chooser)
{
presence_chooser_update_sensitivity (chooser);
}
static void
presence_chooser_connectivity_state_change (EmpathyConnectivity *connectivity,
gboolean new_online,
EmpathyPresenceChooser *chooser)
{
presence_chooser_update_sensitivity (chooser);
}
static void
empathy_presence_chooser_init (EmpathyPresenceChooser *chooser)
{
EmpathyPresenceChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
EMPATHY_TYPE_PRESENCE_CHOOSER, EmpathyPresenceChooserPriv);
GtkWidget *entry;
GtkCellRenderer *renderer;
chooser->priv = priv;
presence_chooser_create_model (chooser);
gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (chooser),
COL_STATUS_TEXT);
gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
combo_row_separator_func,
NULL, NULL);
entry = gtk_bin_get_child (GTK_BIN (chooser));
gtk_entry_set_icon_activatable (GTK_ENTRY (entry),
GTK_ENTRY_ICON_PRIMARY,
FALSE);
g_signal_connect_swapped (entry, "icon-release",
G_CALLBACK (presence_chooser_entry_icon_release_cb),
chooser);
g_signal_connect_swapped (entry, "activate",
G_CALLBACK (presence_chooser_entry_activate_cb),
chooser);
g_signal_connect_swapped (entry, "key-press-event",
G_CALLBACK (presence_chooser_entry_key_press_event_cb),
chooser);
g_signal_connect_swapped (entry, "button-press-event",
G_CALLBACK (presence_chooser_entry_button_press_event_cb),
chooser);
gtk_cell_layout_clear (GTK_CELL_LAYOUT (chooser));
renderer = gtk_cell_renderer_pixbuf_new ();
gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, FALSE);
gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
"icon-name", COL_STATE_ICON_NAME,
NULL);
g_object_set (renderer, "stock-size", GTK_ICON_SIZE_MENU, NULL);
renderer = gtk_cell_renderer_text_new ();
gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, TRUE);
gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
"markup", COL_DISPLAY_MARKUP,
NULL);
g_signal_connect (chooser, "notify::popup-shown",
G_CALLBACK (presence_chooser_popup_shown_cb), NULL);
g_signal_connect (chooser, "changed",
G_CALLBACK (presence_chooser_changed_cb), NULL);
g_signal_connect_swapped (entry, "changed",
G_CALLBACK (presence_chooser_entry_changed_cb),
chooser);
g_signal_connect_swapped (entry, "focus-out-event",
G_CALLBACK (presence_chooser_entry_focus_out_cb),
chooser);
priv->idle = empathy_idle_dup_singleton ();
presence_chooser_presence_changed_cb (chooser);
g_signal_connect_swapped (priv->idle, "notify",
G_CALLBACK (presence_chooser_presence_changed_cb),
chooser);
priv->account_manager = empathy_account_manager_dup_singleton ();
empathy_signal_connect_weak (priv->account_manager, "account-created",
G_CALLBACK (presence_chooser_account_manager_account_changed_cb),
G_OBJECT (chooser));
empathy_signal_connect_weak (priv->account_manager, "account-deleted",
G_CALLBACK (presence_chooser_account_manager_account_changed_cb),
G_OBJECT (chooser));
empathy_signal_connect_weak (priv->account_manager, "account-enabled",
G_CALLBACK (presence_chooser_account_manager_account_changed_cb),
G_OBJECT (chooser));
empathy_signal_connect_weak (priv->account_manager, "account-disabled",
G_CALLBACK (presence_chooser_account_manager_account_changed_cb),
G_OBJECT (chooser));
/* FIXME: this string sucks */
gtk_widget_set_tooltip_text (GTK_WIDGET (chooser),
_("Set your presence and current status"));
priv->connectivity = empathy_connectivity_dup_singleton ();
empathy_signal_connect_weak (priv->connectivity,
"state-change",
G_CALLBACK (presence_chooser_connectivity_state_change),
G_OBJECT (chooser));
presence_chooser_update_sensitivity (chooser);
}
static void
presence_chooser_finalize (GObject *object)
{
EmpathyPresenceChooserPriv *priv;
priv = GET_PRIV (object);
if (priv->flash_timeout_id) {
g_source_remove (priv->flash_timeout_id);
}
if (priv->focus_out_idle_source) {
g_source_remove (priv->focus_out_idle_source);
}
if (priv->account_manager != NULL)
g_object_unref (priv->account_manager);
g_signal_handlers_disconnect_by_func (priv->idle,
presence_chooser_presence_changed_cb,
object);
g_object_unref (priv->idle);
g_object_unref (priv->connectivity);
G_OBJECT_CLASS (empathy_presence_chooser_parent_class)->finalize (object);
}
/**
* empathy_presence_chooser_new:
*
* Creates a new #EmpathyPresenceChooser widget.
*
* Return value: A new #EmpathyPresenceChooser widget
*/
GtkWidget *
empathy_presence_chooser_new (void)
{
GtkWidget *chooser;
chooser = g_object_new (EMPATHY_TYPE_PRESENCE_CHOOSER, NULL);
return chooser;
}
static void
presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser)
{
EmpathyPresenceChooserPriv *priv;
TpConnectionPresenceType state;
TpConnectionPresenceType flash_state;
const gchar *status;
GtkTreeModel *model;
GtkTreeIter iter;
gboolean valid, match_state = FALSE, match = FALSE;
GtkWidget *entry;
priv = GET_PRIV (chooser);
if (priv->editing_status) {
return;
}
priv->state = state = empathy_idle_get_state (priv->idle);
status = empathy_idle_get_status (priv->idle);
flash_state = empathy_idle_get_flash_state (priv->idle);
/* An unset presence here doesn't make any sense. Force it to appear as
* offline. */
if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET) {
state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
}
/* look through the model and attempt to find a matching state */
model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
for (valid = gtk_tree_model_get_iter_first (model, &iter);
valid;
valid = gtk_tree_model_iter_next (model, &iter)) {
int m_type;
TpConnectionPresenceType m_state;
char *m_status;
gtk_tree_model_get (model, &iter,
COL_STATE, &m_state,
COL_TYPE, &m_type,
-1);
if (m_type == ENTRY_TYPE_CUSTOM ||
m_type == ENTRY_TYPE_SEPARATOR ||
m_type == ENTRY_TYPE_EDIT_CUSTOM) {
continue;
}
else if (!match_state && state == m_state) {
/* we are now in the section that can contain our
* match */
match_state = TRUE;
}
else if (match_state && state != m_state) {
/* we have passed the section that can contain our
* match */
break;
}
gtk_tree_model_get (model, &iter,
COL_STATUS_TEXT, &m_status,
-1);
match = !tp_strdiff (status, m_status);
g_free (m_status);
if (match) break;
}
if (match) {
priv->block_changed++;
gtk_combo_box_set_active_iter (GTK_COMBO_BOX (chooser), &iter);
presence_chooser_set_favorite_icon (chooser);
priv->block_changed--;
}
else {
ui_set_custom_state (chooser, state, status);
}
if (flash_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
presence_chooser_flash_start (chooser, state, flash_state);
}
else {
presence_chooser_flash_stop (chooser, state);
}
entry = gtk_bin_get_child (GTK_BIN (chooser));
gtk_editable_set_editable (GTK_EDITABLE (entry),
state != TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
}
static gboolean
presence_chooser_flash_timeout_cb (EmpathyPresenceChooser *chooser)
{
EmpathyPresenceChooserPriv *priv;
TpConnectionPresenceType state;
static gboolean on = FALSE;
GtkWidget *entry;
priv = GET_PRIV (chooser);
if (on) {
state = priv->flash_state_1;
}
else {
state = priv->flash_state_2;
}
entry = gtk_bin_get_child (GTK_BIN (chooser));
gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
GTK_ENTRY_ICON_PRIMARY,
empathy_icon_name_for_presence (state));
on = !on;
return TRUE;
}
static void
presence_chooser_flash_start (EmpathyPresenceChooser *chooser,
TpConnectionPresenceType state_1,
TpConnectionPresenceType state_2)
{
EmpathyPresenceChooserPriv *priv;
g_return_if_fail (EMPATHY_IS_PRESENCE_CHOOSER (chooser));
priv = GET_PRIV (chooser);
priv->flash_state_1 = state_1;
priv->flash_state_2 = state_2;
if (!priv->flash_timeout_id) {
priv->flash_timeout_id = g_timeout_add (FLASH_TIMEOUT,
(GSourceFunc) presence_chooser_flash_timeout_cb,
chooser);
}
}
static void
presence_chooser_flash_stop (EmpathyPresenceChooser *chooser,
TpConnectionPresenceType state)
{
EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
GtkWidget *entry;
if (priv->flash_timeout_id) {
g_source_remove (priv->flash_timeout_id);
priv->flash_timeout_id = 0;
}
entry = gtk_bin_get_child (GTK_BIN (chooser));
gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
GTK_ENTRY_ICON_PRIMARY,
empathy_icon_name_for_presence (state));
}
/**
* empathy_presence_chooser_create_menu:
*
* Creates a new #GtkMenu allowing users to change their presence from a menu.
*
* Return value: a new #GtkMenu for changing presence in a menu.
*/
GtkWidget *
empathy_presence_chooser_create_menu (void)
{
const gchar *status;
GtkWidget *menu;
GtkWidget *item;
GtkWidget *image;
guint i;
menu = gtk_menu_new ();
for (i = 0; states[i].state != TP_CONNECTION_PRESENCE_TYPE_UNSET; i++) {
GList *list, *l;
status = empathy_presence_get_default_message (states[i].state);
presence_chooser_menu_add_item (menu,
status,
states[i].state);
if (states[i].customisable) {
/* Set custom messages if wanted */
list = empathy_status_presets_get (states[i].state, 5);
for (l = list; l; l = l->next) {
presence_chooser_menu_add_item (menu,
l->data,
states[i].state);
}
g_list_free (list);
}
}
/* Separator */
item = gtk_menu_item_new ();
gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
gtk_widget_show (item);
/* Custom messages */
item = gtk_image_menu_item_new_with_label (_("Custom messages..."));
image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_MENU);
gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
gtk_widget_show (image);
gtk_widget_show (item);
g_signal_connect (item,
"activate",
G_CALLBACK (presence_chooser_custom_activate_cb),
NULL);
return menu;
}
static void
presence_chooser_menu_add_item (GtkWidget *menu,
const gchar *str,
TpConnectionPresenceType state)
{
GtkWidget *item;
GtkWidget *image;
const gchar *icon_name;
item = gtk_image_menu_item_new_with_label (str);
icon_name = empathy_icon_name_for_presence (state);
g_signal_connect (item, "activate",
G_CALLBACK (presence_chooser_noncustom_activate_cb),
NULL);
image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
gtk_widget_show (image);
gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (item), TRUE);
gtk_widget_show (item);
g_object_set_data_full (G_OBJECT (item),
"status", g_strdup (str),
(GDestroyNotify) g_free);
g_object_set_data (G_OBJECT (item), "state", GINT_TO_POINTER (state));
gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
}
static void
presence_chooser_noncustom_activate_cb (GtkWidget *item,
gpointer user_data)
{
TpConnectionPresenceType state;
const gchar *status;
status = g_object_get_data (G_OBJECT (item), "status");
state = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (item), "state"));
presence_chooser_set_state (state, status);
}
static void
presence_chooser_set_state (TpConnectionPresenceType state,
const gchar *status)
{
EmpathyIdle *idle;
idle = empathy_idle_dup_singleton ();
empathy_idle_set_presence (idle, state, status);
g_object_unref (idle);
}
static void
presence_chooser_custom_activate_cb (GtkWidget *item,
gpointer user_data)
{
GtkWidget *dialog;
dialog = empathy_status_preset_dialog_new (NULL);
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
}
|