Add description to the previously committed patch
[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                 } catch(ParseError e) {
116                         updating_error(e.message);
117                         return result;
118                 }
119                 
120                 debug("SIZE: %d", result.size);
121                 
122                 if(result.size > 0) { //if we got some statuses
123                         if((!_parent_focus || !act.active) && lst.size > 0) { //if this list is not visible and we have updates
124                                 int self_count = 0;
125                                 foreach(Status status in result) {
126                                         if(status.user_screen_name == api.account.login)
127                                                 self_count++;
128                                 }
129                                 
130                                 if(self_count < result.size) //if we got not only our tweets
131                                         have_fresh = true;
132                         }
133                         
134                         if(lst.size == 0) {
135                                 lst.add_all(result);
136                         } else {
137                                 int i = 0;
138                                 foreach(Status status in result) { //insert statuses at the start of the list
139                                         lst.insert(i, status);
140                                         i++;
141                                 }
142                         }
143                 }
144                 
145                 if(!parent_focus) //delete only if main window out of focus
146                         delete_extra();
147                 
148                 refresh(true);
149                 
150                 if((act.active && _parent_focus) || last_focused == 0) {
151                         if(lst.size > 0)
152                                 last_focused = (int)lst.get(0).created_at.mktime();
153                 }
154                 
155                 finish_update(); //send signal
156                 
157                 if(first_time)
158                         result.clear();
159                 
160                 return result;
161         }
162         
163         /* insert new status */
164         public void insert_status(Status status) {
165                 lst.insert(0, status);
166                 //tmp_lst.add(status);
167                 //delete_extra();
168                 
169                 //last_focused = (int)lst.get(0).created_at.mktime();
170                 
171                 refresh(true);
172         }
173         
174         /* delete status with some id */
175         protected override void destroy_status(string id) {
176                 try {
177                         api.destroy_status(id);
178                 } catch(RestError e) {
179                         debug(e.message);
180                         updating_error(e.message);
181                         return;
182                 }
183                 
184                 //delete status from the list
185                 foreach(Status status in lst) {
186                         if(status.id == id) {
187                                 lst.remove(status);
188                                 break;
189                         }
190                 }
191                 
192                 last_focused = (int)lst.get(0).created_at.mktime();
193                 
194                 refresh(true);
195                 
196                 deleted(_("Your status has been deleted successfully")); //signal
197         }
198         
199         /* get older statuses */
200         protected override void get_older() {
201                 debug("more");
202                 if(lst.size < 1)
203                         return;
204                 
205                 more.set_enabled(false);
206                 
207                 ArrayList<RestAPI.Status>? result = null;
208                 string max_id = lst.get(lst.size - 1).id;
209                 
210                 try {
211                         result = api.get_timeline(_items_count, null, "", max_id);
212                 } catch(RestError e) {
213                         more.set_enabled(true);
214                         updating_error(e.message);
215                         return;
216                 } catch(ParseError e) {
217                         updating_error(e.message);
218                         return;
219                 }
220                 
221                 if(result.size < 2) {
222                         more.set_enabled(true);
223                         return;
224                 }
225                 
226                 lst.add_all(result.slice(1, result.size -1));
227                 
228                 finish_update(); //send signal
229                 
230                 more.set_enabled(true);
231                 
232                 refresh(true);
233         }
234 }