Imported Upstream version 0.2.5
[debian/pino.git] / src / timeline_list.vala
1 /* timeline_list.vala
2  *
3  * Copyright (C) 2009-2010  troorl
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  * Author:
19  *      troorl <troorl@gmail.com>
20  */
21
22 using Gtk;
23 using Auth;
24 using RestAPI;
25 using WebKit;
26 using Gee;
27
28 /* for home timeline, mentions, public timeline, user's timeline */
29 public class TimelineList : TimelineListAbstract {
30         
31         public Icon icon_fresh;
32         
33         //focus of the main window
34         protected bool _parent_focus = false;
35         public override bool parent_focus {
36                 get { return _parent_focus; }
37                 set {
38                         _parent_focus = value;
39                         if(_parent_focus && act.active) {
40                                 have_fresh = false;
41                                 
42                                 if(lst.size > 0)
43                                         last_focused = (int)lst.get(0).created_at.mktime();
44                         }
45                         
46                         if(!value)
47                                 more.hide();
48                 }
49         }
50         
51         private bool _have_fresh = false;
52         public bool have_fresh {
53                 get { return _have_fresh; }
54                 set {
55                         _have_fresh = value;
56                         if(_have_fresh) {
57                                 act.set_gicon(icon_fresh);
58                                 fresh();
59                         } else {
60                                 act.set_gicon(icon);
61                                 no_fresh();
62                         }
63                 }
64         }
65         
66         public signal void fresh();
67         public signal void no_fresh();
68         
69         public TimelineList(Window _parent, Accounts _accounts, TimelineType timeline_type,
70                 Template _template, int __items_count = 20, Icon? _icon = null,
71                 Icon? _icon_fresh = null, string fname = "", string icon_name = "",
72                 string icon_desc = "", bool _active = false) {
73                 
74                 base(_parent, _accounts, timeline_type, _template, __items_count, 
75                         _icon, fname, icon_name, icon_desc, _active);
76                 
77                 icon_fresh = _icon_fresh;
78         }
79         
80         public override void show_smart() {
81                 show();
82                 
83                 if(lst.size > 0) {
84                         last_focused = (int)lst.get(0).created_at.mktime();
85                         have_fresh = false;
86                 }
87                 
88                 act.set_gicon(icon);
89         }
90         
91         /* get new statuses and update the list */
92         public override ArrayList<Status>? update() {
93                 ArrayList<Status> result = null;
94                 string since_id = "";
95                 bool first_time = true;
96                 
97                 //clearing temporary statuses
98                 for(int i = 0; i < lst.size; i++) {
99                         if(lst.get(i).tmp) {
100                                 lst.remove_at(i);
101                                 i--;
102                         }
103                 }
104                 
105                 if(lst.size > 0) {
106                         since_id = lst.get(0).id;
107                         first_time = false;
108                 }
109                 
110                 try {
111                         result = api.get_timeline(_items_count, null, since_id);
112                 } catch(RestError e) {
113                         updating_error(e.message);
114                         return result;
115                 }
116                 
117                 warning("SIZE: %d", result.size);
118                 
119                 if(result.size > 0) { //if we got some statuses
120                         if((!_parent_focus || !act.active) && lst.size > 0) { //if this list is not visible and we have updates
121                                 int self_count = 0;
122                                 foreach(Status status in result) {
123                                         if(status.user_screen_name == api.account.login)
124                                                 self_count++;
125                                 }
126                                 
127                                 if(self_count < result.size) //if we got not only our tweets
128                                         have_fresh = true;
129                         }
130                         
131                         if(lst.size == 0) {
132                                 lst.add_all(result);
133                         } else {
134                                 int i = 0;
135                                 foreach(Status status in result) { //insert statuses at the start of the list
136                                         lst.insert(i, status);
137                                         i++;
138                                 }
139                         }
140                 }
141                 
142                 if(!parent_focus) //delete only if main window out of focus
143                         delete_extra();
144                 
145                 refresh(true);
146                 
147                 if((act.active && _parent_focus) || last_focused == 0) {
148                         if(lst.size > 0)
149                                 last_focused = (int)lst.get(0).created_at.mktime();
150                 }
151                 
152                 finish_update(); //send signal
153                 
154                 if(first_time)
155                         result.clear();
156                 
157                 return result;
158         }
159         
160         /* insert new status */
161         public void insert_status(Status status) {
162                 lst.insert(0, status);
163                 //tmp_lst.add(status);
164                 //delete_extra();
165                 
166                 //last_focused = (int)lst.get(0).created_at.mktime();
167                 
168                 refresh(true);
169         }
170         
171         /* delete status with some id */
172         protected override void destroy_status(string id) {
173                 try {
174                         api.destroy_status(id);
175                 } catch(RestError e) {
176                         warning(e.message);
177                         updating_error(e.message);
178                         return;
179                 }
180                 
181                 //delete status from the list
182                 foreach(Status status in lst) {
183                         if(status.id == id) {
184                                 lst.remove(status);
185                                 break;
186                         }
187                 }
188                 
189                 last_focused = (int)lst.get(0).created_at.mktime();
190                 
191                 refresh(true);
192                 
193                 deleted(_("Your status has been deleted successfully")); //signal
194         }
195         
196         /* get older statuses */
197         protected override void get_older() {
198                 warning("more");
199                 if(lst.size < 1)
200                         return;
201                 
202                 more.set_enabled(false);
203                 
204                 ArrayList<RestAPI.Status> result;
205                 string max_id = lst.get(lst.size - 1).id;
206                 
207                 try {
208                         result = api.get_timeline(_items_count, null, "", max_id);
209                 } catch(RestError e) {
210                         more.set_enabled(true);
211                         updating_error(e.message);
212                         return;
213                 }
214                 
215                 if(result.size < 2) {
216                         more.set_enabled(true);
217                         return;
218                 }
219                 
220                 lst.add_all(result.slice(1, result.size -1));
221                 
222                 finish_update(); //send signal
223                 
224                 more.set_enabled(true);
225                 
226                 refresh(true);
227         }
228 }