forked from wuman/PlurkVala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plurk-profile.vala
141 lines (115 loc) · 4.14 KB
/
plurk-profile.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using Json;
using Soup;
public class Roguso.Profile : GLib.Object {
private static const string KEY_FANS_COUNT = "fans_count";
private static const string KEY_ALERTS_COUNT = "alerts_count";
private static const string KEY_FRIENDS_COUNT = "friends_count";
private static const string KEY_UNREAD_COUNT = "unread_count";
private static const string KEY_PRIVACY = "privacy";
private static const string KEY_HAS_READ_PERMISSION = "has_read_permission";
private static const string KEY_USER_INFO = "user_info";
public int64 fans_count { get; set; default = 0; }
public int64 alerts_count { get; set; default = 0; }
public int64 friends_count { get; set; default = 0; }
public int64 unread_count { get; set; default = 0; }
public Privacy privacy { get; set; default = Privacy.WORLD; }
public bool has_read_permission { get; set; default = true; }
public User user { get; set; default = null; }
public static enum Privacy {
WORLD,
ONLY_FRIENDS,
ONLY_ME;
public static Privacy from_string(string val) {
switch (val) {
case "only_friends":
return ONLY_FRIENDS;
case "only_me":
return ONLY_ME;
default:
return WORLD;
}
}
public string to_string() {
switch (this) {
case ONLY_FRIENDS:
return "only_friends";
case ONLY_ME:
return "only_me";
default:
return "world";
}
}
}
/*
* Constructors
*/
public Profile() { }
public Profile.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 Profile.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() {
fans_count = 0;
alerts_count = 0;
friends_count = 0;
unread_count = 0;
privacy = Privacy.WORLD;
has_read_permission = true;
user = null;
}
private void build(Json.Node node) {
if ( node.get_node_type() != NodeType.OBJECT ) {
return;
}
Json.Object object = node.get_object();
unowned Json.Node node_fans_count = object.get_member(KEY_FANS_COUNT);
if ( node_fans_count != null ) {
fans_count = node_fans_count.get_int();
}
unowned Json.Node node_alerts_count = object.get_member(KEY_ALERTS_COUNT);
if ( node_alerts_count != null ) {
alerts_count = node_alerts_count.get_int();
}
unowned Json.Node node_friends_count = object.get_member(KEY_FRIENDS_COUNT);
if ( node_friends_count != null ) {
friends_count = node_friends_count.get_int();
}
unowned Json.Node node_unread_count = object.get_member(KEY_UNREAD_COUNT);
if ( node_unread_count != null ) {
unread_count = node_unread_count.get_int();
}
unowned Json.Node node_privacy = object.get_member(KEY_PRIVACY);
if ( node_privacy != null ) {
privacy = Privacy.from_string(node_privacy.get_string());
}
unowned Json.Node node_has_read_permission = object.get_member(KEY_HAS_READ_PERMISSION);
if ( node_has_read_permission != null ) {
has_read_permission = node_has_read_permission.get_boolean();
}
unowned Json.Node node_user = object.get_member(KEY_USER_INFO);
if ( node_user != null && node_user.get_node_type() == NodeType.OBJECT ) {
User userinfo = new User.from_node(node_user);
user = userinfo;
}
}
}