Allow / in the regexes for search/groups
[debian/pino.git] / src / timeline_direct_list.vala
1 /* timeline_direct_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 Gee;
23 using Auth;
24 using RestAPI;
25 using Gtk;
26
27 public class TimelineDirectList : TimelineList {
28         
29         public TimelineDirectList(Window _parent, Accounts _accounts,
30                 Template _template, int __items_count, Icon? _icon, Icon? _icon_fresh,
31                 string fname, string icon_name, string icon_desc, bool _active = false) {
32                 
33                 base(_parent, _accounts, TimelineType.HOME, _template, __items_count,
34                         _icon, _icon_fresh, fname, icon_name, icon_desc, _active);
35                 
36                 var acc = accounts.get_current_account();
37                 api = new RestAPIDirect(acc);
38                 api.request.connect((req) => start_update(req));
39         }
40         
41         /* refresh timeline */
42         public override void refresh(bool with_favorites = false) {
43                 if(lst.size == 0)
44                         set_empty();
45                 else
46                         update_content(template.generate_direct(lst, last_focused));
47         }
48         
49         /* get new direct messages and update the list */
50         public override ArrayList<Status>? update() {
51                 ArrayList<RestAPI.Status> result = null;
52                 string since_id = "";
53                 bool first_time = true;
54                 
55                 if(lst.size > 0) {
56                         since_id = lst.get(0).id;
57                         first_time = false;
58                 }
59                 
60                 try {
61                         result = api.get_timeline(_items_count, null, since_id);
62                 } catch(RestError e) {
63                         updating_error(e.message);
64                         return result;
65                 } catch(ParseError e) {
66                         updating_error(e.message);
67                         return result;
68                 }
69                 
70                 if(result.size > 0) { //if we get some statuses
71                         if((!_parent_focus || !act.active) && lst.size > 0) { //if this list is not visible and we have updates
72                                 
73                                 have_fresh = true;
74                         }
75                         
76                         if(lst.size == 0) {
77                                 lst.add_all(result);
78                         } else {
79                                 int i = 0;
80                                 foreach(Status status in result) { //insert statuses at the start of the list
81                                         lst.insert(i, status);
82                                         i++;
83                                 }
84                         }
85                 }
86                 
87                 if(!parent_focus) //delete only if main window out of focus
88                         delete_extra();
89                 
90                 debug("SIZE: %d", lst.size);
91                 
92                 refresh();
93                 
94                 if((act.active && _parent_focus) || last_focused == 0) {
95                         if(lst.size > 0)
96                                 last_focused = (int)lst.get(0).created_at.mktime();
97                 }
98                 
99                 finish_update(); //send signal
100                 
101                 if(first_time)
102                         result.clear();
103                 
104                 return result;
105         }
106         
107         /* get older direct messages */
108         protected override void get_older() {
109                 if(lst.size < 1)
110                         return;
111                 
112                 more.set_enabled(false);
113                 
114                 ArrayList<RestAPI.Status> result;
115                 string max_id = lst.get(lst.size - 1).id;
116                 
117                 try {
118                         result = api.get_timeline(_items_count, null, "", max_id);
119                 } catch(RestError e) {
120                         more.set_enabled(true);
121                         updating_error(e.message);
122                         return;
123                 } catch(ParseError e) {
124                         more.set_enabled(true);
125                         updating_error(e.message);
126                         return;
127                 }
128                 
129                 if(result.size < 2) {
130                         more.set_enabled(true);
131                         return;
132                 }
133                 
134                 lst.add_all(result.slice(1, result.size -1));
135                 refresh();
136                 finish_update(); //send signal
137                 
138                 more.set_enabled(true);
139         }
140         
141         /* delete direct message with some id */
142         protected override void destroy_status(string id) {
143                 try {
144                         api.destroy_status(id);
145                 } catch(RestError e) {
146                         updating_error(e.message);
147                         return;
148                 } catch(ParseError e) {
149                         updating_error(e.message);
150                         return;
151                 }
152                 
153                 //delete status from the list
154                 foreach(Status status in lst) {
155                         if(status.id == id) {
156                                 lst.remove(status);
157                                 break;
158                         }
159                 }
160                 
161                 refresh();
162                 
163                 deleted(_("Your direct message has been deleted successfully")); //signal
164         }
165 }