Imported Upstream version 0.2.1
[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                 if(lst.size > 0) {
99                         since_id = lst.get(0).id;
100                         first_time = false;
101                 }
102                 
103                 try {
104                         result = api.get_timeline(_items_count, null, since_id);
105                 } catch(RestError e) {
106                         updating_error(e.message);
107                         return result;
108                 }
109                 
110                 if(result.size > 0) { //if we got some statuses
111                         if((!_parent_focus || !act.active) && lst.size > 0) { //if this list is not visible and we have updates
112                                 
113                                 have_fresh = true;
114                         }
115                         
116                         if(lst.size == 0) {
117                                 lst.add_all(result);
118                         } else {
119                                 int i = 0;
120                                 foreach(Status status in result) { //insert statuses at the start of the list
121                                         lst.insert(i, status);
122                                         i++;
123                                 }
124                         }
125                 }
126                 
127                 if(!parent_focus) //delete only if main window out of focus
128                         delete_extra();
129                 
130                 warning("SIZE: %d", lst.size);
131                 
132                 refresh();
133                 
134                 if((act.active && _parent_focus) || last_focused == 0) {
135                         if(lst.size > 0)
136                                 last_focused = (int)lst.get(0).created_at.mktime();
137                 }
138                 
139                 finish_update(); //send signal
140                 
141                 if(first_time)
142                         result.clear();
143                 
144                 return result;
145         }
146         
147         /* insert new status */
148         public void insert_status(Status status) {
149                 lst.insert(0, status);
150                 delete_extra();
151                 
152                 last_focused = (int)lst.get(0).created_at.mktime();
153                 
154                 refresh();
155         }
156         
157         /* delete status with some id */
158         protected override void destroy_status(string id) {
159                 try {
160                         api.destroy_status(id);
161                 } catch(RestError e) {
162                         warning(e.message);
163                         updating_error(e.message);
164                         return;
165                 }
166                 
167                 //delete status from the list
168                 foreach(Status status in lst) {
169                         if(status.id == id) {
170                                 lst.remove(status);
171                                 break;
172                         }
173                 }
174                 
175                 last_focused = (int)lst.get(0).created_at.mktime();
176                 
177                 refresh();
178                 
179                 deleted(_("Your status has been deleted successfully")); //signal
180         }
181         
182         /* get older statuses */
183         protected override void get_older() {
184                 if(lst.size < 1)
185                         return;
186                 
187                 more.set_enabled(false);
188                 
189                 ArrayList<RestAPI.Status> result;
190                 string max_id = lst.get(lst.size - 1).id;
191                 
192                 try {
193                         result = api.get_timeline(_items_count, null, "", max_id);
194                 } catch(RestError e) {
195                         more.set_enabled(true);
196                         updating_error(e.message);
197                         return;
198                 }
199                 
200                 if(result.size < 2) {
201                         more.set_enabled(true);
202                         return;
203                 }
204                 
205                 lst.add_all(result.slice(1, result.size -1));
206                 
207                 finish_update(); //send signal
208                 
209                 more.set_enabled(true);
210                 
211                 refresh();
212         }
213 }