-
Notifications
You must be signed in to change notification settings - Fork 2
/
plurk-plurk-list.vala
119 lines (92 loc) · 3 KB
/
plurk-plurk-list.vala
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
using Json;
public class Roguso.PlurkList : GLib.Object {
private static const string KEY_PLURKS = "plurks";
private static const string KEY_PLURK_USERS = "plurk_users";
private HashTable<string, Plurk> plurk_by_id = new HashTable<string, Plurk>(GLib.str_hash, GLib.str_equal);
private List<Plurk> plurk_list = new List<Plurk>();
/*
* Constructors
*/
public PlurkList() { }
public PlurkList.from_data(string buffer) {
this();
Parser parser = new Parser();
try {
parser.load_from_data(buffer);
build(parser.get_root());
} catch ( Error e) {
stdout.printf("Error: %s\n", e.message);
}
}
public PlurkList.from_node(Json.Node node) {
this();
build(node);
}
public void load_from_data(string buffer) {
clean();
Parser parser = new Parser();
try {
parser.load_from_data(buffer);
build(parser.get_root());
} catch ( Error e ) {
stdout.printf("Error: %s\n", e.message);
}
}
private void clean() {
plurk_by_id = new HashTable<string, Plurk>(GLib.str_hash, GLib.str_equal);
plurk_list = new List<Plurk>();
}
private void build(Json.Node node) {
if ( node.get_node_type() != NodeType.OBJECT ) {
return;
}
Json.Object object = node.get_object();
if ( !object.has_member(KEY_PLURKS) ) {
return;
}
build_plurks_only(object.get_member(KEY_PLURKS));
if ( !object.has_member(KEY_PLURK_USERS) ) {
return;
}
unowned Json.Node user_list_node = object.get_member(KEY_PLURK_USERS);
UserList user_list = new UserList.from_node(user_list_node);
foreach ( Plurk plurk in plurk_list ) {
User owner = user_list.get_id(plurk.owner_id);
if ( owner != null ) {
plurk.owner = owner;
}
}
}
private void build_plurks_only(Json.Node node) {
if ( node.get_node_type() != NodeType.ARRAY ) {
return;
}
foreach ( unowned Json.Node plurk_node in node.get_array().get_elements() ) {
if ( plurk_node.get_node_type() != NodeType.OBJECT ) {
continue;
}
Plurk plurk = new Plurk.from_node(plurk_node);
plurk_by_id.replace(plurk.plurk_id, plurk);
plurk_list.prepend(plurk);
}
plurk_list.reverse();
}
public uint get_count() {
return plurk_by_id.size();
}
public Plurk get_id(string plurk_id) {
return plurk_by_id.lookup(plurk_id);
}
public Plurk get_pos(uint index) {
if ( index >= 0 ) {
return plurk_list.nth_data(index);
} else {
uint roll = plurk_list.length();
roll += index;
return plurk_list.nth_data(roll);
}
}
public List<Plurk> get_all() {
return plurk_list.copy();
}
}