eb78be6229fbb2878f8b2501e81f9a4c40710fab
[debian/pino.git] / src / twitter_stream_home.vala
1 using Gee;
2 using PinoEnums;
3 using Rest;
4
5 namespace Twitter {
6
7 public class StreamHome : Twitter.StreamAbstract {
8
9         public override StreamEnum stream_type {get { return StreamEnum.HOME; } }
10
11         public override MenuItems[] popup_items {
12                 owned get {
13                         return {
14                                 MenuItems.REFRESH,
15                                 MenuItems.SETTINGS,
16                                 MenuItems.MORE,
17                                 MenuItems.REMOVE
18                         };
19                 }
20         }
21         
22         private override string func {get; set; default = "statuses/home_timeline.xml";}
23         
24         public int64 s_last_id {get; set; default = 0;}
25         
26         public override string id {get; set; default = "home";}
27         
28         protected delegate ArrayList<Status> ParsingDelegate(string data, string own_name);
29         
30         protected ParsingDelegate? parsing_delegate = null;
31         
32         construct {
33                 debug("twitter home stream was created");
34                 
35                 parsing_delegate = Parser.get_timeline;
36         }
37         
38         protected override void set_call_params(bool more = false) {
39                 if(s_last_id > 0) {
40                         call.remove_param("max_id");
41                         call.remove_param("since_id");
42                                 
43                         if(!more) {
44                                 call.add_param("since_id", s_last_id.to_string());
45                         } else {
46                                 call.add_param("max_id", model.get(model.size - 1).id);
47                         }
48                 }
49         }
50         
51         protected override void parse_stream(string data) {
52                 ArrayList<Status> result_lst = parsing_delegate(data, own_name);//Parser.get_timeline(data, own_name);
53                 
54                 if(result_lst.size == 0) {
55                         fresh_items = 0;
56                         fresh_to_normal();
57                         return;
58                 }
59                 
60                 if(s_last_id == 0) {
61                         model.add_all(result_lst);
62                         s_last_id = model.get(0).id.to_int64();
63                         
64                         return;
65                 }
66                 
67                 fresh_items = result_lst.size;
68                 
69                 fresh_to_normal();
70                 
71                 foreach(Status status in result_lst) {
72                         status.fresh = true;
73                 }
74                 
75                 //getting fresh statuses
76                 model.add_all(result_lst);
77                 
78                 s_last_id = model.get(0).id.to_int64();
79                 
80                 /*
81                 int own_statuses = 0;
82                 foreach(Status status in statuses_fresh) {
83                         if(status.own)
84                                 own_statuses += 1;
85                 }
86                 
87                 fresh_items = statuses_fresh.size - own_statuses;
88                 */
89                 debug("What we got: %d", result_lst.size);
90         }
91         
92         protected override void parse_more_stream(string data) {
93                 ArrayList<Status> result_lst = parsing_delegate(data, own_name);
94                 
95                 if(result_lst.size > 1) {
96                         //model.add_all(result_lst.slice(2, result_lst.size - 1));
97                         foreach(Status status in result_lst) {
98                                 model.add(status);
99                         }
100                 }
101         }
102         
103         /* Moving fresh statuses to the normal statuses */
104         protected void fresh_to_normal() {
105                 foreach(Status status in model) {
106                         status.fresh = false;
107                 }
108         }
109         
110         public override void menu_more() {
111                 debug("more");
112                 sync(true);
113         }
114 }
115
116 }