summaryrefslogtreecommitdiff
path: root/tp-gtk-roster-model-filter.c
blob: d01cb44555a37a5fc2c91c824a627b1c3aeb0ede (plain)
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
/*
 * tp-gtk-roster-model-filter.c
 *
 * TpGtkRosterModelFilter
 *
 * (c) 2009, Collabora Ltd.
 *
 * Authors:
 *    Danielle Madeley <danielle.madeley@collabora.co.uk>
 */

#include <telepathy-glib/telepathy-glib.h>

#include "tp-gtk-roster-model.h"
#include "tp-gtk-roster-model-filter.h"

#define GET_PRIVATE(obj)	(G_TYPE_INSTANCE_GET_PRIVATE ((obj), TP_GTK_TYPE_ROSTER_MODEL_FILTER, TpGtkRosterModelFilterPrivate))

G_DEFINE_TYPE (TpGtkRosterModelFilter, tp_gtk_roster_model_filter, GTK_TYPE_TREE_MODEL_FILTER);

typedef struct _TpGtkRosterModelFilterPrivate TpGtkRosterModelFilterPrivate;
struct _TpGtkRosterModelFilterPrivate
{
  gboolean show_hidden;
};

enum
{
  PROP_0,
  PROP_SHOW_HIDDEN
};

static void
tp_gtk_roster_model_filter_get_property (GObject    *self,
                                         guint       prop_id,
                                         GValue     *value,
                                         GParamSpec *pspec)
{
  switch (prop_id)
    {
      case PROP_SHOW_HIDDEN:
        g_value_set_boolean (value,
            tp_gtk_roster_model_get_show_hidden (
              TP_GTK_ROSTER_MODEL_FILTER (self)));
        break;

      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
        break;
    }
}

static void
tp_gtk_roster_model_filter_set_property (GObject      *self,
                                         guint         prop_id,
                                         const GValue *value,
                                         GParamSpec   *pspec)
{
  switch (prop_id)
    {
      case PROP_SHOW_HIDDEN:
        tp_gtk_roster_model_set_show_hidden (TP_GTK_ROSTER_MODEL_FILTER (self),
            g_value_get_boolean (value));
        break;

      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
        break;
    }
}

static gboolean
tp_gtk_roster_model_filter_visible (GtkTreeModelFilter *self,
                                    GtkTreeModel       *child_model,
                                    GtkTreeIter        *iter)
{
  TpGtkRosterModelFilterPrivate *priv = GET_PRIVATE (self);
  TpGtkRosterModelColumnType type;
  TpConnectionPresenceType state;
  gboolean v;

  gtk_tree_model_get (child_model, iter,
      TP_GTK_ROSTER_MODEL_CONTACT, &type,
      TP_GTK_ROSTER_MODEL_STATE, &state,
      -1);

  v = (priv->show_hidden || type == TP_GTK_ROSTER_MODEL_COLUMN_TYPE_GROUP ||
      (state != TP_CONNECTION_PRESENCE_TYPE_UNSET &&
       state != TP_CONNECTION_PRESENCE_TYPE_OFFLINE &&
       state != TP_CONNECTION_PRESENCE_TYPE_UNKNOWN &&
       state != TP_CONNECTION_PRESENCE_TYPE_ERROR));

  /* FIXME: add filtering based on capabilities */

  return v &&
    GTK_TREE_MODEL_FILTER_CLASS (tp_gtk_roster_model_filter_parent_class)->visible (self, child_model, iter);
}

static void
tp_gtk_roster_model_filter_class_init (TpGtkRosterModelFilterClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  GtkTreeModelFilterClass *filter_class = GTK_TREE_MODEL_FILTER_CLASS (klass);

  gobject_class->get_property = tp_gtk_roster_model_filter_get_property;
  gobject_class->set_property = tp_gtk_roster_model_filter_set_property;

  filter_class->visible = tp_gtk_roster_model_filter_visible;

  g_object_class_install_property (gobject_class, PROP_SHOW_HIDDEN,
      g_param_spec_boolean ("show-hidden",
                            "Show Hidden",
                            "Show Hidden Contacts in Filter",
                            FALSE,
                            G_PARAM_READWRITE));

  g_type_class_add_private (gobject_class, sizeof (TpGtkRosterModelFilterPrivate));
}

static void
tp_gtk_roster_model_filter_init (TpGtkRosterModelFilter *self)
{
}

gboolean
tp_gtk_roster_model_get_show_hidden (TpGtkRosterModelFilter *self)
{
  g_return_if_fail (TP_GTK_IS_ROSTER_MODEL_FILTER (self));

  return GET_PRIVATE (self)->show_hidden;
}

void
tp_gtk_roster_model_set_show_hidden (TpGtkRosterModelFilter *self,
                                     gboolean                show_hidden)
{
  g_return_if_fail (TP_GTK_IS_ROSTER_MODEL_FILTER (self));

  GET_PRIVATE (self)->show_hidden = show_hidden;

  g_object_notify (G_OBJECT (self), "show-hidden");

  gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (self));
}

GtkTreeModel *
tp_gtk_roster_model_filter_new (TpGtkRosterModel *model)
{
  g_return_val_if_fail (TP_GTK_IS_ROSTER_MODEL (model), NULL);

  return g_object_new (TP_GTK_TYPE_ROSTER_MODEL_FILTER,
      "child-model", model,
      NULL);
}