aboutsummaryrefslogtreecommitdiffstats
path: root/libempathy-gtk/gossip-presence-chooser.c
blob: 46b275456ae7d79e763380b5912aa96501854620 (plain) (blame)
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2005-2007 Imendio AB
 *
 * 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., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Authors: Richard Hult <richard@imendio.com>
 *          Martyn Russell <martyn@imendio.com>
 */

#include "config.h"

#include <string.h>
#include <stdlib.h>

#include <glib/gi18n.h>
#include <gtk/gtk.h>
#include <glade/glade.h>

#include <libempathy/gossip-utils.h>
#include <libempathy/empathy-marshal.h>

#include "gossip-ui-utils.h"
#include "empathy-images.h"
#include "gossip-presence-chooser.h"
#include "gossip-status-presets.h"

#define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GOSSIP_TYPE_PRESENCE_CHOOSER, GossipPresenceChooserPriv))

typedef struct {
    GtkWidget  *hbox;
    GtkWidget  *image;
    GtkWidget  *label;
    GtkWidget  *menu;

    McPresence  last_state;

    guint       flash_interval;
    McPresence  flash_state_1;
    McPresence  flash_state_2;
    guint       flash_timeout_id;

    /* The handle the kind of unnessecary scroll support. */
    guint       scroll_timeout_id;
    McPresence  scroll_state;
    gchar      *scroll_status;
} GossipPresenceChooserPriv;

/* States to be listed in the menu */
static McPresence states[] = {MC_PRESENCE_AVAILABLE,
                  MC_PRESENCE_DO_NOT_DISTURB,
                  MC_PRESENCE_AWAY};

static void     presence_chooser_finalize               (GObject               *object);
static void     presence_chooser_reset_scroll_timeout   (GossipPresenceChooser *chooser);
static void     presence_chooser_set_state              (GossipPresenceChooser *chooser,
                             McPresence             state,
                             const gchar           *status,
                             gboolean               save);
static void     presence_chooser_dialog_response_cb     (GtkWidget             *dialog,
                             gint                   response,
                             GossipPresenceChooser *chooser);
static void     presence_chooser_show_dialog            (GossipPresenceChooser *chooser,
                             McPresence             state);
static void     presence_chooser_custom_activate_cb     (GtkWidget             *item,
                             GossipPresenceChooser *chooser);
static void     presence_chooser_clear_response_cb      (GtkWidget             *widget,
                             gint                   response,
                             gpointer               user_data);
static void     presence_chooser_clear_activate_cb      (GtkWidget             *item,
                             GossipPresenceChooser *chooser);
static void     presence_chooser_menu_add_item          (GossipPresenceChooser *chooser,
                             GtkWidget             *menu,
                             const gchar           *str,
                             McPresence             state,
                             gboolean               custom);
static void     presence_chooser_menu_align_func        (GtkMenu               *menu,
                             gint                  *x,
                             gint                  *y,
                             gboolean              *push_in,
                             GossipPresenceChooser *chooser);
static void     presence_chooser_menu_selection_done_cb (GtkMenuShell          *menushell,
                             GossipPresenceChooser *chooser);
static void     presence_chooser_menu_detach            (GtkWidget             *attach_widget,
                             GtkMenu               *menu);
static void     presence_chooser_menu_popup             (GossipPresenceChooser *chooser);
static void     presence_chooser_menu_popdown           (GossipPresenceChooser *chooser);
static void     presence_chooser_toggled_cb             (GtkWidget             *chooser,
                             gpointer               user_data);
static gboolean presence_chooser_button_press_event_cb  (GtkWidget             *chooser,
                             GdkEventButton        *event,
                             gpointer               user_data);
static gboolean presence_chooser_scroll_event_cb        (GtkWidget             *chooser,
                             GdkEventScroll        *event,
                             gpointer               user_data);
static gboolean presence_chooser_flash_timeout_cb       (GossipPresenceChooser *chooser);

G_DEFINE_TYPE (GossipPresenceChooser, gossip_presence_chooser, GTK_TYPE_TOGGLE_BUTTON);

enum {
    CHANGED,
    LAST_SIGNAL
};

static guint signals[LAST_SIGNAL];

static void
gossip_presence_chooser_class_init (GossipPresenceChooserClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);

    object_class->finalize = presence_chooser_finalize;

    signals[CHANGED] =
        g_signal_new ("changed",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  0,
                  NULL, NULL,
                  empathy_marshal_VOID__INT_STRING,
                  G_TYPE_NONE, 2,
                  G_TYPE_INT, G_TYPE_STRING);

    g_type_class_add_private (object_class, sizeof (GossipPresenceChooserPriv));
}

static void
gossip_presence_chooser_init (GossipPresenceChooser *chooser)
{
    GossipPresenceChooserPriv *priv;
    GtkWidget                 *arrow;
    GtkWidget                 *alignment;

    priv = GET_PRIV (chooser);

    /* Default to 1/2 a second flash interval */
    priv->flash_interval = 500;

    gtk_button_set_relief (GTK_BUTTON (chooser), GTK_RELIEF_NONE);
    gtk_button_set_focus_on_click (GTK_BUTTON (chooser), FALSE);

    alignment = gtk_alignment_new (0.5, 0.5, 1, 1);
    gtk_widget_show (alignment);
    gtk_container_add (GTK_CONTAINER (chooser), alignment);
    gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 0, 0, 1, 0);

    priv->hbox = gtk_hbox_new (FALSE, 1);
    gtk_widget_show (priv->hbox);
    gtk_container_add (GTK_CONTAINER (alignment), priv->hbox);

    priv->image = gtk_image_new ();
    gtk_widget_show (priv->image);
    gtk_box_pack_start (GTK_BOX (priv->hbox), priv->image, FALSE, TRUE, 0);

    priv->label = gtk_label_new (NULL);
    gtk_widget_show (priv->label);
    gtk_box_pack_start (GTK_BOX (priv->hbox), priv->label, TRUE, TRUE, 0);
    gtk_label_set_ellipsize (GTK_LABEL (priv->label), PANGO_ELLIPSIZE_END);
    gtk_misc_set_alignment (GTK_MISC (priv->label), 0, 0.5);
    gtk_misc_set_padding (GTK_MISC (priv->label), 4, 1);

    alignment = gtk_alignment_new (0.5, 0.5, 1, 1);
    gtk_widget_show (alignment);
    gtk_box_pack_start (GTK_BOX (priv->hbox), alignment, FALSE, FALSE, 0);

    arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_OUT);
    gtk_widget_show (arrow);
    gtk_container_add (GTK_CONTAINER (alignment), arrow);

    g_signal_connect (chooser, "toggled",
              G_CALLBACK (presence_chooser_toggled_cb),
              NULL);
    g_signal_connect (chooser, "button-press-event",
              G_CALLBACK (presence_chooser_button_press_event_cb),
              NULL);
    g_signal_connect (chooser, "scroll-event",
              G_CALLBACK (presence_chooser_scroll_event_cb),
              NULL);
}

static void
presence_chooser_finalize (GObject *object)
{
    GossipPresenceChooserPriv *priv;

    priv = GET_PRIV (object);

    if (priv->flash_timeout_id) {
        g_source_remove (priv->flash_timeout_id);
    }

    if (priv->scroll_timeout_id) {
        g_source_remove (priv->scroll_timeout_id);
    }

    G_OBJECT_CLASS (gossip_presence_chooser_parent_class)->finalize (object);
}

static void
presence_chooser_reset_scroll_timeout (GossipPresenceChooser *chooser)
{
    GossipPresenceChooserPriv *priv;

    priv = GET_PRIV (chooser);

    if (priv->scroll_timeout_id) {
        g_source_remove (priv->scroll_timeout_id);
        priv->scroll_timeout_id = 0;
    }

    g_free (priv->scroll_status);
    priv->scroll_status = NULL;
}

static void
presence_chooser_set_state (GossipPresenceChooser *chooser,
                McPresence             state,
                const gchar           *status,
                gboolean               save)
{
    GossipPresenceChooserPriv *priv;
    const gchar               *default_status;

    priv = GET_PRIV (chooser);

    default_status = gossip_presence_state_get_default_status (state);

    if (G_STR_EMPTY (status)) {
        status = default_status;
    } else {
        /* Only store the value if it differs from the default ones. */
        if (save && strcmp (status, default_status) != 0) {
            gossip_status_presets_set_last (state, status);
        }
    }

    priv->last_state = state;

    presence_chooser_reset_scroll_timeout (chooser);
    g_signal_emit (chooser, signals[CHANGED], 0, state, status);
}

static void
presence_chooser_dialog_response_cb (GtkWidget             *dialog,
                     gint                   response,
                     GossipPresenceChooser *chooser)
{
    if (response == GTK_RESPONSE_OK) {
        GtkWidget           *entry;
        GtkWidget           *checkbutton;
        GtkListStore        *store;
        GtkTreeModel        *model;
        GtkTreeIter          iter;
        McPresence           state;
        const gchar         *status;
        gboolean             save;
        gboolean             duplicate = FALSE;
        gboolean             has_next;

        entry = g_object_get_data (G_OBJECT (dialog), "entry");
        status = gtk_entry_get_text (GTK_ENTRY (entry));
        store = g_object_get_data (G_OBJECT (dialog), "store");
        model = GTK_TREE_MODEL (store);

        has_next = gtk_tree_model_get_iter_first (model, &iter);
        while (has_next) {
            gchar *str;

            gtk_tree_model_get (model, &iter,
                        0, &str,
                        -1);

            if (strcmp (status, str) == 0) {
                g_free (str);
                duplicate = TRUE;
                break;
            }

            g_free (str);

            has_next = gtk_tree_model_iter_next (model, &iter);
        }

        if (!duplicate) {
            gtk_list_store_append (store, &iter);
            gtk_list_store_set (store, &iter, 0, status, -1);
        }

        checkbutton = g_object_get_data (G_OBJECT (dialog), "checkbutton");
        save = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (checkbutton));
        state = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (dialog), "state"));

        presence_chooser_set_state (chooser, state, status, save);
    }

    gtk_widget_destroy (dialog);
}

static void
presence_chooser_show_dialog (GossipPresenceChooser *chooser,
                  McPresence             state)
{
    GossipPresenceChooserPriv *priv;
    static GtkWidget          *dialog = NULL;
    static GtkListStore       *store[LAST_MC_PRESENCE];
    GladeXML                  *glade;
    GtkWidget                 *image;
    GtkWidget                 *combo;
    GtkWidget                 *entry;
    GtkWidget                 *checkbutton;
    const gchar               *default_status;

    priv = GET_PRIV (chooser);

    if (dialog) {
        gtk_widget_destroy (dialog);
        dialog = NULL;
    } else {
        guint i;

        for (i = 0; i < LAST_MC_PRESENCE; i++) {
            store[i] = NULL;
        }
    }

    glade = gossip_glade_get_file ("gossip-presence-chooser.glade",
                       "status_message_dialog",
                       NULL,
                       "status_message_dialog", &dialog,
                       "comboentry_status", &combo,
                       "image_status", &image,
                       "checkbutton_add", &checkbutton,
                       NULL);

    g_object_unref (glade);

    g_signal_connect (dialog, "destroy",
              G_CALLBACK (gtk_widget_destroyed),
              &dialog);
    g_signal_connect (dialog, "response",
              G_CALLBACK (presence_chooser_dialog_response_cb),
              chooser);

    gtk_image_set_from_icon_name (GTK_IMAGE (image),
                      gossip_icon_name_for_presence_state (state),
                      GTK_ICON_SIZE_MENU);

    if (!store[state]) {
        GList       *presets, *l;
        GtkTreeIter  iter;

        store[state] = gtk_list_store_new (1, G_TYPE_STRING);

        presets = gossip_status_presets_get (state, -1);
        for (l = presets; l; l = l->next) {
            gtk_list_store_append (store[state], &iter);
            gtk_list_store_set (store[state], &iter, 0, l->data, -1);
        }

        g_list_free (presets);
    }

    default_status = gossip_presence_state_get_default_status (state);

    entry = GTK_BIN (combo)->child;
    gtk_entry_set_text (GTK_ENTRY (entry), default_status);
    gtk_entry_set_activates_default (GTK_ENTRY (entry), TRUE);
    gtk_entry_set_width_chars (GTK_ENTRY (entry), 25);

    gtk_combo_box_set_model (GTK_COMBO_BOX (combo), GTK_TREE_MODEL (store[state]));
    gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (combo), 0);

    /* FIXME: Set transian for a window ? */

    g_object_set_data (G_OBJECT (dialog), "store", store[state]);
    g_object_set_data (G_OBJECT (dialog), "entry", entry);
    g_object_set_data (G_OBJECT (dialog), "checkbutton", checkbutton);
    g_object_set_data (G_OBJECT (dialog), "state", GINT_TO_POINTER (state));

    gtk_widget_show_all (dialog);
}

static void
presence_chooser_custom_activate_cb (GtkWidget             *item,
                     GossipPresenceChooser *chooser)
{
    McPresence state;

    state = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (item), "state"));

    presence_chooser_show_dialog (chooser, state);
}

static void
presence_chooser_noncustom_activate_cb (GtkWidget             *item,
                    GossipPresenceChooser *chooser)
{
    McPresence   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_reset_scroll_timeout (chooser);
    g_signal_emit (chooser, signals[CHANGED], 0, state, status);
}

static void
presence_chooser_clear_response_cb (GtkWidget *widget,
                    gint       response,
                    gpointer   user_data)
{
    if (response == GTK_RESPONSE_OK) {
        gossip_status_presets_reset ();
    }

    gtk_widget_destroy (widget);
}

static void
presence_chooser_clear_activate_cb (GtkWidget             *item,
                    GossipPresenceChooser *chooser)
{
    GtkWidget *dialog;
    GtkWidget *toplevel;
    GtkWindow *parent = NULL;

    toplevel = gtk_widget_get_toplevel (GTK_WIDGET (chooser));
    if (GTK_WIDGET_TOPLEVEL (toplevel) &&
        GTK_IS_WINDOW (toplevel)) {
        GtkWindow *window;
        gboolean   visible;

        window = GTK_WINDOW (toplevel);
        visible = gossip_window_get_is_visible (window);

        if (visible) {
            parent = window;
        }
    }

    dialog = gtk_message_dialog_new (GTK_WINDOW (parent),
                     0,
                     GTK_MESSAGE_QUESTION,
                     GTK_BUTTONS_NONE,
                     _("Are you sure you want to clear the list?"));

    gtk_message_dialog_format_secondary_text (
        GTK_MESSAGE_DIALOG (dialog),
        _("This will remove any custom messages you have "
          "added to the list of preset status messages."));

    gtk_dialog_add_buttons (GTK_DIALOG (dialog),
                GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                _("Clear List"), GTK_RESPONSE_OK,
                NULL);

    gtk_window_set_skip_taskbar_hint (GTK_WINDOW (dialog), FALSE);

    g_signal_connect (dialog, "response",
              G_CALLBACK (presence_chooser_clear_response_cb),
              NULL);

    gtk_widget_show (dialog);
}

static void
presence_chooser_menu_add_item (GossipPresenceChooser *chooser,
                GtkWidget             *menu,
                const gchar           *str,
                McPresence             state,
                gboolean               custom)
{
    GtkWidget   *item;
    GtkWidget   *image;
    const gchar *icon_name;

    item = gtk_image_menu_item_new_with_label (str);
    icon_name = gossip_icon_name_for_presence_state (state);

    if (custom) {
        g_signal_connect (
            item,
            "activate",
            G_CALLBACK (presence_chooser_custom_activate_cb),
            chooser);
    } else {
        g_signal_connect (
            item,
            "activate",
            G_CALLBACK (presence_chooser_noncustom_activate_cb),
            chooser);
    }

    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_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_menu_align_func (GtkMenu               *menu,
                  gint                  *x,
                  gint                  *y,
                  gboolean              *push_in,
                  GossipPresenceChooser *chooser)
{
    GtkWidget      *widget;
    GtkRequisition  req;
    GdkScreen      *screen;
    gint            screen_height;

    widget = GTK_WIDGET (chooser);

    gtk_widget_size_request (GTK_WIDGET (menu), &req);

    gdk_window_get_origin (widget->window, x, y);

    *x += widget->allocation.x + 1;
    *y += widget->allocation.y;

    screen = gtk_widget_get_screen (GTK_WIDGET (menu));
    screen_height = gdk_screen_get_height (screen);

    if (req.height > screen_height) {
        /* Too big for screen height anyway. */
        *y = 0;
        return;
    }

    if ((*y + req.height + widget->allocation.height) > screen_height) {
        /* Can't put it below the button. */
        *y -= req.height;
        *y += 1;
    } else {
        /* Put menu below button. */
        *y += widget->allocation.height;
        *y -= 1;
    }

    *push_in = FALSE;
}

static void
presence_chooser_menu_selection_done_cb (GtkMenuShell          *menushell,
                     GossipPresenceChooser *chooser)
{
    gtk_widget_destroy (GTK_WIDGET (menushell));

    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (chooser), FALSE);
}

static void
presence_chooser_menu_destroy_cb (GtkWidget             *menu,
                  GossipPresenceChooser *chooser)
{
    GossipPresenceChooserPriv *priv;

    priv = GET_PRIV (chooser);

    priv->menu = NULL;
}

static void
presence_chooser_menu_detach (GtkWidget *attach_widget,
                  GtkMenu   *menu)
{
    /* We don't need to do anything, but attaching the menu means
     * we don't own the ref count and it is cleaned up properly.
     */
}

static void
presence_chooser_menu_popup (GossipPresenceChooser *chooser)
{
    GossipPresenceChooserPriv *priv;
    GtkWidget                 *menu;

    priv = GET_PRIV (chooser);

    if (priv->menu) {
        return;
    }

    menu = gossip_presence_chooser_create_menu (chooser);

    g_signal_connect_after (menu, "selection-done",
                G_CALLBACK (presence_chooser_menu_selection_done_cb),
                chooser);

    g_signal_connect (menu, "destroy",
              G_CALLBACK (presence_chooser_menu_destroy_cb),
              chooser);

    gtk_menu_attach_to_widget (GTK_MENU (menu),
                   GTK_WIDGET (chooser),
                   presence_chooser_menu_detach);

    gtk_menu_popup (GTK_MENU (menu),
            NULL, NULL,
            (GtkMenuPositionFunc) presence_chooser_menu_align_func,
            chooser,
            1,
            gtk_get_current_event_time ());

    priv->menu = menu;
}

static void
presence_chooser_menu_popdown (GossipPresenceChooser *chooser)
{
    GossipPresenceChooserPriv *priv;

    priv = GET_PRIV (chooser);

    if (priv->menu) {
        gtk_widget_destroy (priv->menu);
    }
}

static void
presence_chooser_toggled_cb (GtkWidget *chooser,
                 gpointer   user_data)
{
    if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (chooser))) {
        presence_chooser_menu_popup (GOSSIP_PRESENCE_CHOOSER (chooser));
    } else {
        presence_chooser_menu_popdown (GOSSIP_PRESENCE_CHOOSER (chooser));
    }
}

static gboolean
presence_chooser_button_press_event_cb (GtkWidget      *chooser,
                    GdkEventButton *event,
                    gpointer        user_data)
{
    if (event->button != 1 || event->type != GDK_BUTTON_PRESS) {
        return FALSE;
    }

    if (!gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (chooser))) {
            gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (chooser), TRUE);
            return TRUE;
        }

    return FALSE;
}

typedef struct {
    McPresence   state;
    const gchar *status;
} StateAndStatus;

static StateAndStatus *
presence_chooser_state_and_status_new (McPresence   state,
                       const gchar *status)
{
    StateAndStatus *sas;

    sas = g_new0 (StateAndStatus, 1);

    sas->state = state;
    sas->status = status;

    return sas;
}

static GList *
presence_chooser_get_presets (GossipPresenceChooser *chooser)
{
    GList      *list = NULL;
    guint       i;

    for (i = 0; i < G_N_ELEMENTS (states); i++) {
        GList          *presets, *p;
        StateAndStatus *sas;
        const gchar    *status;

        status = gossip_presence_state_get_default_status (states[i]);
        sas = presence_chooser_state_and_status_new (states[i], status);
        list = g_list_append (list, sas);
    
        presets = gossip_status_presets_get (states[i], 5);
        for (p = presets; p; p = p->next) {
            sas = presence_chooser_state_and_status_new (states[i], p->data);
            list = g_list_append (list, sas);
        }
        g_list_free (presets);
    }

    return list;
}

static gboolean
presence_chooser_scroll_timeout_cb (GossipPresenceChooser *chooser)
{
    GossipPresenceChooserPriv *priv;

    priv = GET_PRIV (chooser);

    g_signal_emit (chooser, signals[CHANGED], 0,
               priv->scroll_state,
               priv->scroll_status);

    priv->scroll_timeout_id = 0;

    g_free (priv->scroll_status);
    priv->scroll_status = NULL;

    return FALSE;
}

static gboolean
presence_chooser_scroll_event_cb (GtkWidget      *chooser,
                  GdkEventScroll *event,
                  gpointer        user_data)
{
    GossipPresenceChooserPriv *priv;
    GList                     *list, *l;
    const gchar               *current_status;
    StateAndStatus            *sas;
    gboolean                   match;

    priv = GET_PRIV (chooser);

    switch (event->direction) {
    case GDK_SCROLL_UP:
        break;
    case GDK_SCROLL_DOWN:
        break;
    default:
        return FALSE;
    }

    current_status = gtk_label_get_text (GTK_LABEL (priv->label));

    /* Get the list of presets, which in this context means all the items
     * without a trailing "...".
     */
    list = presence_chooser_get_presets (GOSSIP_PRESENCE_CHOOSER (chooser));
    sas = NULL;
    match = FALSE;
    for (l = list; l; l = l->next) {
        sas = l->data;

        if (sas->state == priv->last_state &&
            strcmp (sas->status, current_status) == 0) {
            sas = NULL;
            match = TRUE;
            if (event->direction == GDK_SCROLL_UP) {
                if (l->prev) {
                    sas = l->prev->data;
                }
            }
            else if (event->direction == GDK_SCROLL_DOWN) {
                if (l->next) {
                    sas = l->next->data;
                }
            }
            break;
        }

        sas = NULL;
    }

    if (sas) {
        presence_chooser_reset_scroll_timeout (GOSSIP_PRESENCE_CHOOSER (chooser));

        priv->scroll_status = g_strdup (sas->status);
        priv->scroll_state = sas->state;

        priv->scroll_timeout_id =
            g_timeout_add (500,
                       (GSourceFunc) presence_chooser_scroll_timeout_cb,
                       chooser);

        gossip_presence_chooser_set_status (GOSSIP_PRESENCE_CHOOSER (chooser),
                            sas->status);
        gossip_presence_chooser_set_state (GOSSIP_PRESENCE_CHOOSER (chooser),
                           sas->state);
    }
    else if (!match) {
        /* If we didn't get any match at all, it means the last state
         * was a custom one. Just switch to the first one.
         */
        presence_chooser_reset_scroll_timeout (GOSSIP_PRESENCE_CHOOSER (chooser));
        g_signal_emit (chooser, signals[CHANGED], 0,
                   MC_PRESENCE_AVAILABLE,
                   _("Available"));
    }

    g_list_foreach (list, (GFunc) g_free, NULL);
    g_list_free (list);

    return TRUE;
}

GtkWidget *
gossip_presence_chooser_new (void)
{
    GtkWidget *chooser;

    chooser = g_object_new (GOSSIP_TYPE_PRESENCE_CHOOSER, NULL);

    return chooser;
}

GtkWidget *
gossip_presence_chooser_create_menu (GossipPresenceChooser *chooser)
{
    const gchar *status;
    GtkWidget *menu;
    GtkWidget *item;
    GtkWidget *image;
    guint      i;

    menu = gtk_menu_new ();

    for (i = 0; i < G_N_ELEMENTS (states); i++) {
        GList       *list, *l;

        status = gossip_presence_state_get_default_status (states[i]);
        presence_chooser_menu_add_item (chooser,
                        menu,
                        status,
                        states[i],
                        FALSE);

        list = gossip_status_presets_get (states[i], 5);
        for (l = list; l; l = l->next) {
            presence_chooser_menu_add_item (chooser,
                            menu,
                            l->data,
                            states[i],
                            FALSE);
        }
        g_list_free (list);

        presence_chooser_menu_add_item (chooser,
                        menu,
                        _("Custom message..."),
                        states[i],
                        TRUE);

        /* Separator. */
        item = gtk_menu_item_new ();
        gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
        gtk_widget_show (item);
    }

    /* Offline to disconnect */
    status = gossip_presence_state_get_default_status (MC_PRESENCE_OFFLINE);
    presence_chooser_menu_add_item (chooser,
                    menu,
                    status,
                    MC_PRESENCE_OFFLINE,
                    FALSE);
    /* Separator. */
    item = gtk_menu_item_new ();
    gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
    gtk_widget_show (item);

    /* Clear list */
    item = gtk_image_menu_item_new_with_label (_("Clear List..."));
    image = gtk_image_new_from_stock (GTK_STOCK_CLEAR, 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_clear_activate_cb),
              chooser);

    return menu;
}

void
gossip_presence_chooser_set_state (GossipPresenceChooser *chooser,
                   McPresence             state)
{
    GossipPresenceChooserPriv *priv;

    g_return_if_fail (GOSSIP_IS_PRESENCE_CHOOSER (chooser));

    priv = GET_PRIV (chooser);

    gossip_presence_chooser_flash_stop (chooser, state);
}

void
gossip_presence_chooser_set_status (GossipPresenceChooser *chooser,
                    const gchar           *status)
{
    GossipPresenceChooserPriv *priv;

    g_return_if_fail (GOSSIP_IS_PRESENCE_CHOOSER (chooser));

    priv = GET_PRIV (chooser);

    gtk_label_set_text (GTK_LABEL (priv->label), status);
}

void
gossip_presence_chooser_set_flash_interval (GossipPresenceChooser *chooser,
                        guint                  ms)
{
    GossipPresenceChooserPriv *priv;

    g_return_if_fail (GOSSIP_IS_PRESENCE_CHOOSER (chooser));
    g_return_if_fail (ms > 1 && ms < 30000);

    priv = GET_PRIV (chooser);

    priv->flash_interval = ms;
}

static gboolean
presence_chooser_flash_timeout_cb (GossipPresenceChooser *chooser)
{
    GossipPresenceChooserPriv *priv;
    McPresence                 state;
    static gboolean            on = FALSE;

    priv = GET_PRIV (chooser);

    if (on) {
        state = priv->flash_state_1;
    } else {
        state = priv->flash_state_2;
    }

    gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
                      gossip_icon_name_for_presence_state (state),
                      GTK_ICON_SIZE_MENU);

    on = !on;

    return TRUE;
}

void
gossip_presence_chooser_flash_start (GossipPresenceChooser *chooser,
                     McPresence             state_1,
                     McPresence             state_2)
{
    GossipPresenceChooserPriv *priv;

    g_return_if_fail (GOSSIP_IS_PRESENCE_CHOOSER (chooser));

    priv = GET_PRIV (chooser);

    if (priv->flash_timeout_id != 0) {
        return;
    }

    priv->flash_state_1 = state_1;
    priv->flash_state_2 = state_2;

    priv->flash_timeout_id = g_timeout_add (priv->flash_interval,
                        (GSourceFunc) presence_chooser_flash_timeout_cb,
                        chooser);
}

void
gossip_presence_chooser_flash_stop (GossipPresenceChooser *chooser,
                    McPresence             state)
{
    GossipPresenceChooserPriv *priv;

    g_return_if_fail (GOSSIP_IS_PRESENCE_CHOOSER (chooser));

    priv = GET_PRIV (chooser);

    if (priv->flash_timeout_id) {
        g_source_remove (priv->flash_timeout_id);
        priv->flash_timeout_id = 0;
    }

    gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
                      gossip_icon_name_for_presence_state (state),
                      GTK_ICON_SIZE_MENU);

    priv->last_state = state;
}

gboolean
gossip_presence_chooser_is_flashing (GossipPresenceChooser *chooser)
{
    GossipPresenceChooserPriv *priv;

    g_return_val_if_fail (GOSSIP_IS_PRESENCE_CHOOSER (chooser), FALSE);

    priv = GET_PRIV (chooser);

    if (priv->flash_timeout_id) {
        return TRUE;
    }

    return FALSE;
}