public
Description: A Clutter-based Twitter client
Homepage: http://live.gnome.org/Tweet
Clone URL: git://github.com/ebassi/tweet.git
Search Repo:
Emmanuele Bassi (author)
Sat Apr 19 07:29:41 -0700 2008
commit  a643edaaa7ba328cf4f24356dbeb5c2faa5837e3
tree    a002d1ab1d3439fcbbd514ce3b803f4d8e216866
parent  455bcf01d7cb8a5c84630024c693349a9c06dafe
tweet / twitter-glib / twitter-timeline.c
100644 225 lines (178 sloc) 5.739 kb
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
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
 
#include <string.h>
#include <glib.h>
 
#include <json-glib/json-glib.h>
 
#include "twitter-common.h"
#include "twitter-enum-types.h"
#include "twitter-private.h"
#include "twitter-status.h"
#include "twitter-timeline.h"
#include "twitter-user.h"
 
#define TWITTER_TIMELINE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TWITTER_TYPE_TIMELINE, TwitterTimelinePrivate))
 
struct _TwitterTimelinePrivate
{
  GHashTable *status_by_id;
  GList *status_list;
};
 
G_DEFINE_TYPE (TwitterTimeline, twitter_timeline, G_TYPE_OBJECT);
 
static void
twitter_timeline_finalize (GObject *gobject)
{
  TwitterTimelinePrivate *priv = TWITTER_TIMELINE (gobject)->priv;
 
  g_hash_table_destroy (priv->status_by_id);
  g_list_free (priv->status_list);
 
  G_OBJECT_CLASS (twitter_timeline_parent_class)->finalize (gobject);
}
 
static void
twitter_timeline_class_init (TwitterTimelineClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
 
  g_type_class_add_private (klass, sizeof (TwitterTimelinePrivate));
 
  gobject_class->finalize = twitter_timeline_finalize;
}
 
static void
twitter_timeline_init (TwitterTimeline *timeline)
{
  TwitterTimelinePrivate *priv;
 
  timeline->priv = priv = TWITTER_TIMELINE_GET_PRIVATE (timeline);
 
  priv->status_by_id = g_hash_table_new_full (NULL, NULL,
                                              NULL,
                                              g_object_unref);
}
 
static void
twitter_timeline_clean (TwitterTimeline *timeline)
{
  TwitterTimelinePrivate *priv = timeline->priv;
 
  if (priv->status_list)
    {
      g_list_free (priv->status_list);
      priv->status_list = NULL;
    }
 
  if (priv->status_by_id)
    {
      g_hash_table_destroy (priv->status_by_id);
      priv->status_by_id = g_hash_table_new_full (NULL, NULL,
                                                  NULL,
                                                  g_object_unref);
    }
}
 
static void
twitter_timeline_build (TwitterTimeline *timeline,
                        JsonNode *node)
{
  TwitterTimelinePrivate *priv = timeline->priv;
  JsonArray *array;
  GList *elements, *l;
  GList *status_list = NULL;
 
  if (!node || JSON_NODE_TYPE (node) != JSON_NODE_ARRAY)
    return;
 
  array = json_node_get_array (node);
  elements = json_array_get_elements (array);
 
  for (l = elements; l != NULL; l = l->next)
    {
      JsonNode *element = l->data;
 
      if (JSON_NODE_TYPE (element) == JSON_NODE_OBJECT)
        {
          TwitterStatus *status;
          guint status_id;
 
          status = twitter_status_new_from_node (element);
          status_id = twitter_status_get_id (status);
          if (status_id == 0)
            {
              g_object_unref (status);
              continue;
            }
 
          g_hash_table_replace (priv->status_by_id,
                                GUINT_TO_POINTER (status_id),
                                g_object_ref_sink (status));
          status_list = g_list_prepend (status_list, status);
        }
    }
 
  priv->status_list = g_list_reverse (status_list);
}
 
TwitterTimeline *
twitter_timeline_new (void)
{
  return g_object_new (TWITTER_TYPE_TIMELINE, NULL);
}
 
TwitterTimeline *
twitter_timeline_new_from_data (const gchar *buffer)
{
  TwitterTimeline *retval;
  JsonParser *parser;
  GError *parse_error;
 
  g_return_val_if_fail (buffer != NULL, NULL);
 
  retval = twitter_timeline_new ();
 
  parser = json_parser_new ();
  parse_error = NULL;
  json_parser_load_from_data (parser, buffer, -1, &parse_error);
  if (parse_error)
    {
      g_warning ("Unable to parse data into a timeline: %s",
                 parse_error->message);
      g_error_free (parse_error);
    }
  else
    twitter_timeline_build (retval, json_parser_get_root (parser));
 
  g_object_unref (parser);
 
  return retval;
}
 
void
twitter_timeline_load_from_data (TwitterTimeline *timeline,
                                 const gchar *buffer)
{
  JsonParser *parser;
  GError *parse_error;
 
  g_return_if_fail (TWITTER_IS_TIMELINE (timeline));
  g_return_if_fail (buffer != NULL);
 
  twitter_timeline_clean (timeline);
 
  parser = json_parser_new ();
  parse_error = NULL;
  json_parser_load_from_data (parser, buffer, -1, &parse_error);
  if (parse_error)
    {
      g_warning ("Unable to parse data into a timeline: %s",
                 parse_error->message);
      g_error_free (parse_error);
    }
  else
    twitter_timeline_build (timeline, json_parser_get_root (parser));
 
  g_object_unref (parser);
}
 
guint
twitter_timeline_get_count (TwitterTimeline *timeline)
{
  g_return_val_if_fail (TWITTER_IS_TIMELINE (timeline), 0);
 
  return g_hash_table_size (timeline->priv->status_by_id);
}
 
TwitterStatus *
twitter_timeline_get_id (TwitterTimeline *timeline,
                         guint id)
{
  g_return_val_if_fail (TWITTER_IS_TIMELINE (timeline), NULL);
 
  return g_hash_table_lookup (timeline->priv->status_by_id,
                              GUINT_TO_POINTER (id));
}
 
TwitterStatus *
twitter_timeline_get_pos (TwitterTimeline *timeline,
                          gint index_)
{
  g_return_val_if_fail (TWITTER_IS_TIMELINE (timeline), NULL);
  g_return_val_if_fail (ABS (index_) < twitter_timeline_get_count (timeline), NULL);
 
  if (index_ >= 0)
    return g_list_nth_data (timeline->priv->status_list, index_);
  else
    {
      guint roll = g_list_length (timeline->priv->status_list);
 
      roll += index_;
      return g_list_nth_data (timeline->priv->status_list, roll);
    }
}
 
GList *
twitter_timeline_get_all (TwitterTimeline *timeline)
{
  g_return_val_if_fail (TWITTER_IS_TIMELINE (timeline), NULL);
 
  return g_list_copy (timeline->priv->status_list);
}