Update copyright information
[debian/pino.git] / src / twitter_stream_abstract.vala
1 using Gee;
2 using PinoEnums;
3 using Rest;
4
5 namespace Twitter {
6
7 public abstract class StreamAbstract : AStream {
8         
9         protected Rest.Proxy proxy;
10         protected string own_name = "";
11         //protected string auth_header;
12         protected Rest.ProxyCall call;
13         
14         private unowned GLib.Thread thread;
15         
16         private abstract string func {get; set;}
17         
18         protected virtual void set_call_params(bool more = false) {}
19         
20         protected abstract void parse_stream(string data);
21         protected abstract void parse_more_stream(string data);
22         
23         public virtual void set_proxy(Rest.Proxy proxy, string own_name, string password = "") {
24                 this.proxy = proxy;
25                 this.own_name = own_name;
26                 
27                 debug("proxy was added");
28                 
29                 //setup call
30                 call = proxy.new_call();
31                 call.set_function(func);
32                 call.set_method("GET");
33                 
34                 if(password != "") { //creating basic http auth header
35                         uchar[] b_chars = Utils.string_to_uchar_array("%s:%s".printf(own_name, password));
36                         string http_auth = GLib.Base64.encode(b_chars);
37                         call.add_header("Authorization", "Basic %s".printf(http_auth));
38                 }
39                 
40                 sync();
41         }
42         
43         public virtual void sync(bool more = false) {
44                 if(status == StreamStatus.UPDATING || status == StreamStatus.PARSING)
45                         return;
46                 /*
47                 try {
48                         thread = Thread.create(sync_thread, true);
49                 } catch(ThreadError e) {
50                         debug(e.message);
51                 }*/
52                 debug("start updating");
53                 status = StreamStatus.UPDATING;
54                 
55                 Rest.ProxyCallAsyncCallback callback;
56                 
57                 if(more)
58                         callback = get_more_response;
59                 else
60                         callback = get_response;
61                 
62                 set_call_params(more);
63                 call.run_async(callback, this);
64         }
65         
66         public void get_response(Rest.ProxyCall call, Error? error, Object? obj) {
67                 debug("finish updating");
68                 status = StreamStatus.READY;
69                 //debug(call.get_payload());
70                 parse_stream(call.get_payload());
71                 
72                 updated(); //emit signal
73         }
74         
75         public void get_more_response(Rest.ProxyCall call, Error? error, Object? obj) {
76                 debug("finish updating");
77                 status = StreamStatus.READY;
78                 //debug(call.get_payload());
79                 parse_more_stream(call.get_payload());
80                 
81                 updated(); //emit signal
82         }
83         
84         public override void menu_refresh() {
85                 debug("twitter sync");
86                 sync();
87         }
88 }
89
90 }