summaryrefslogtreecommitdiff
path: root/liszt/main.c
blob: 2ca75c7ef7afc9142e1161d8c773251a20ebd952 (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
#include <mx/mx.h>
#include <folks/folks.h>

#include "factory.h"

enum
{
  COLUMN_INDIVIDUAL,
  N_COLUMNS
};

static gboolean
model_delete_cb (ClutterModel *model,
    ClutterModelIter *iter,
    gpointer user_data)
{
  gpointer current_individual;
  clutter_model_iter_get (iter, COLUMN_INDIVIDUAL, &current_individual, -1);
  if (current_individual == user_data)
    {
      clutter_model_remove (model, clutter_model_iter_get_row (iter));
      return FALSE;
    }

  return TRUE;
}

static void
aggregator_individuals_changed_cb (FolksIndividualAggregator *aggregator,
    GList *added,
    GList *removed,
    const char *message,
    FolksPersona *actor,
    guint reason,
    ClutterModel *model)
{
  GList *l;

  for (l = removed; l != NULL; l = l->next)
    {
      FolksIndividual *individual = l->data;

      if (folks_individual_get_is_user (individual))
        continue;

      clutter_model_foreach (model, model_delete_cb, individual);
    }
  for (l = added; l != NULL; l = l->next)
    {
      FolksIndividual *individual = l->data;

      if (folks_individual_get_is_user (individual))
        continue;

      clutter_model_append (model,
          COLUMN_INDIVIDUAL, individual,
          -1);
    }
}

static gint
model_sort_func (ClutterModel *model,
    const GValue *a,
    const GValue *b,
    gpointer user_data)
{
  FolksIndividual *one = g_value_get_object (a);
  FolksIndividual *two = g_value_get_object (b);
  const gchar *alias1;
  const gchar *alias2;
  gint ret;

  ret = - folks_has_presence_typecmp (
      folks_has_presence_get_presence_type (FOLKS_HAS_PRESENCE (one)),
      folks_has_presence_get_presence_type (FOLKS_HAS_PRESENCE (two)));

  if (ret != 0)
    return ret;

  alias1 = folks_aliasable_get_alias (FOLKS_ALIASABLE (one));
  alias2 = folks_aliasable_get_alias (FOLKS_ALIASABLE (two));
  ret = strcmp (alias1, alias2);

  return ret;
}

static ClutterModel *
create_model (FolksIndividualAggregator *aggregator)
{
  ClutterModel *model;

  model = clutter_list_model_new (N_COLUMNS,
      FOLKS_TYPE_INDIVIDUAL, "individual");

  clutter_model_set_sort (model, COLUMN_INDIVIDUAL, model_sort_func,
      NULL, NULL);

  g_signal_connect (aggregator, "individuals-changed",
      G_CALLBACK (aggregator_individuals_changed_cb), model);

  folks_individual_aggregator_prepare (aggregator, NULL, NULL);

  return model;
}

int
main (int argc, char **argv)
{
  MxApplication *application;
  FolksIndividualAggregator *aggregator;
  MxWindow *window;
  ClutterActor *stage, *toolbar, *scroll, *view;

  application = mx_application_new (&argc, &argv, "Contact list",
      MX_APPLICATION_SINGLE_INSTANCE);

  aggregator = folks_individual_aggregator_new ();

  window = mx_application_create_window (application);
  stage = (ClutterActor *) mx_window_get_clutter_stage (window);

  /* toolbar */
  toolbar = (ClutterActor *) mx_window_get_toolbar (window);
  mx_bin_set_fill (MX_BIN (toolbar), TRUE, FALSE);

  /* main stage */
  scroll = mx_scroll_view_new ();
  mx_window_set_child (window, scroll);

  view = mx_list_view_new ();
  mx_list_view_add_attribute (MX_LIST_VIEW (view), "individual", COLUMN_INDIVIDUAL);

  mx_list_view_set_factory (MX_LIST_VIEW (view), MX_ITEM_FACTORY (lol_factory_new ()));
  mx_list_view_set_model (MX_LIST_VIEW (view), create_model (aggregator));

  clutter_container_add_actor (CLUTTER_CONTAINER (scroll), view);

  clutter_actor_show (stage);

  mx_application_run (application);

  g_object_unref (aggregator);

  return 0;
}