Imported Upstream version 0.2.4
[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         public signal void deleted(string message);
69         
70         public TimelineList(Window _parent, Accounts _accounts, TimelineType timeline_type,
71                 Template _template, int __items_count = 20, Icon? _icon = null,
72                 Icon? _icon_fresh = null, string fname = "", string icon_name = "",
73                 string icon_desc = "", bool _active = false) {
74                 
75                 base(_parent, _accounts, timeline_type, _template, __items_count, 
76                         _icon, fname, icon_name, icon_desc, _active);
77                 
78                 icon_fresh = _icon_fresh;
79         }
80         
81         public override void show_smart() {
82                 show();
83                 
84                 if(lst.size > 0) {
85                         last_focused = (int)lst.get(0).created_at.mktime();
86                         have_fresh = false;
87                 }
88                 
89                 act.set_gicon(icon);
90         }
91         
92         /* get new statuses and update the list */
93         public override ArrayList<Status>? update() {
94                 ArrayList<Status> result = null;
95                 string since_id = "";
96                 bool first_time = true;
97                 
98                 //clearing temporary statuses
99                 for(int i = 0; i < lst.size; i++) {
100                         if(lst.get(i).tmp) {
101                                 lst.remove_at(i);
102                                 i--;
103                         }
104                 }
105                 
106                 if(lst.size > 0) {
107                         since_id = lst.get(0).id;
108                         first_time = false;
109                 }
110                 
111                 try {
112                         result = api.get_timeline(_items_count, null, since_id);
113                 } catch(RestError e) {
114                         updating_error(e.message);
115                         return result;
116                 }
117                 
118                 if(result.size > 0) { //if we got some statuses
119                         if((!_parent_focus || !act.active) && lst.size > 0) { //if this list is not visible and we have updates
120                                 
121                                 have_fresh = true;
122                         }
123                         
124                         if(lst.size == 0) {
125                                 lst.add_all(result);
126                         } else {
127                                 int i = 0;
128                                 foreach(Status status in result) { //insert statuses at the start of the list
129                                         lst.insert(i, status);
130                                         i++;
131                                 }
132                         }
133                 }
134                 
135                 if(!parent_focus) //delete only if main window out of focus
136                         delete_extra();
137                 
138                 warning("SIZE: %d", lst.size);
139                 
140                 refresh();
141                 
142                 if((act.active && _parent_focus) || last_focused == 0) {
143                         if(lst.size > 0)
144                                 last_focused = (int)lst.get(0).created_at.mktime();
145                 }
146                 
147                 finish_update(); //send signal
148                 
149                 if(first_time)
150                         result.clear();
151                 
152                 return result;
153         }
154         
155         /* insert new status */
156         public void insert_status(Status status) {
157                 lst.insert(0, status);
158                 //tmp_lst.add(status);
159                 //delete_extra();
160                 
161                 //last_focused = (int)lst.get(0).created_at.mktime();
162                 
163                 refresh();
164         }
165         
166         /* delete status with some id */
167         protected override void destroy_status(string id) {
168                 try {
169                         api.destroy_status(id);
170                 } catch(RestError e) {
171                         warning(e.message);
172                         updating_error(e.message);
173                         return;
174                 }
175                 
176                 //delete status from the list
177                 foreach(Status status in lst) {
178                         if(status.id == id) {
179                                 lst.remove(status);
180                                 break;
181                         }
182                 }
183                 
184                 last_focused = (int)lst.get(0).created_at.mktime();
185                 
186                 refresh();
187                 
188                 deleted(_("Your status has been deleted successfully")); //signal
189         }
190         
191         /* get older statuses */
192         protected override void get_older() {
193                 if(lst.size < 1)
194                         return;
195                 
196                 more.set_enabled(false);
197                 
198                 ArrayList<RestAPI.Status> result;
199                 string max_id = lst.get(lst.size - 1).id;
200                 
201                 try {
202                         result = api.get_timeline(_items_count, null, "", max_id);
203                 } catch(RestError e) {
204                         more.set_enabled(true);
205                         updating_error(e.message);
206                         return;
207                 }
208                 
209                 if(result.size < 2) {
210                         more.set_enabled(true);
211                         return;
212                 }
213                 
214                 lst.add_all(result.slice(1, result.size -1));
215                 
216                 finish_update(); //send signal
217                 
218                 more.set_enabled(true);
219                 
220                 refresh();
221         }
222 }