Add description to the previously committed patch
[debian/pino.git] / src / user_info_list.vala
1 /* user_info_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 Gee;
24 using Auth;
25 using RestAPI;
26
27 public class UserInfoList : TimelineListAbstract {
28         
29         private Userpic userpic;
30         private Label username;
31         private CheckButton follow;
32         
33         private Label followers;
34         private Label friends;
35         private Label statuses;
36         
37         private TextView desc;
38         private HBox db;
39         private HBox uh;
40         private Label homepage;
41         
42         private HSeparator first_sep;
43         private HSeparator desc_sep;
44         
45         FullStatus? full_status = null;
46         
47         private string username_text = "<b><big>(%s) @%s</big></b>";
48         private string followers_text = _("Followers:") + "  <b>%s</b>";
49         private string friends_text = _("Friends:") + "  <b>%s</b>";
50         private string statuses_text = _("Statuses:") + "  <b>%s</b>";
51         private string homepage_text;
52         
53         public signal void start_fetch();
54         public signal void end_fetch();
55         public signal void follow_event(string msg);
56         
57         public UserInfoList(Window _parent, Accounts _accounts, Template _template,
58                 int __items_count, Icon? _icon, bool native_link_color, string fname = "", string icon_name = "",
59                 string icon_desc = "") {
60                 
61                 base(_parent, _accounts, TimelineType.USER, _template, __items_count, 
62                         _icon, fname, icon_name, icon_desc);
63                 
64                 need_more_button = false;
65                 
66                 var acc = accounts.get_current_account();
67                 api = new RestAPIUserInfo(acc);
68                 
69                 string link_color = template.gtk_style.sl_color;
70                 if(native_link_color)
71                         link_color = template.gtk_style.lk_color;
72                 
73                 homepage_text = _("Web:") + "  <a href='%s'><span foreground='" + link_color + "'>%s</span></a>";
74                 
75                 accounts.active_changed.connect(() => {
76                         set_empty();
77                 });
78                 
79                 gui_setup();
80                 
81                 set_empty();
82         }
83         
84         private void gui_setup() {
85                 var pb = new VBox(false, 0);
86                 userpic = new Userpic(template.cache);
87                 userpic.set_size_request(48, 48);
88                 pb.pack_start(userpic, false, false, 0);
89                 
90                 var up = new VBox(false, 0);
91                 username = new Label("");
92                 
93                 var fb = new HBox(false, 0);
94                 follow = new CheckButton.with_label(_("follow"));
95                 follow.toggled.connect(follow_toggled);
96                 fb.pack_end(follow, false, false, 0);
97                 
98                 up.pack_start(username, false, false, 2);
99                 up.pack_start(fb, false, false, 0);
100                 
101                 VBox sb = new VBox(false, 0);
102                 
103                 var fo = new HBox(false, 0);
104                 followers = new Label("");
105                 fo.pack_start(followers, false, false, 8);
106                 var fr = new HBox(false, 0);
107                 friends = new Label("");
108                 fr.pack_start(friends, false, false, 8);
109                 var st = new HBox(false, 0);
110                 statuses = new Label("");
111                 st.pack_start(statuses, false, false, 8);
112                 
113                 var ub = new HBox(false, 0);
114                 homepage = new Label("");
115                 ub.pack_start(homepage, false, false, 8);
116                 
117                 sb.pack_start(fo, false, false, 2);
118                 sb.pack_start(fr, false, false, 0);
119                 sb.pack_start(st, false, false, 0);
120                 sb.pack_start(ub, false, false, 0);
121                 
122                 uh = new HBox(false, 0);
123                 uh.pack_end(pb, false, false, 8);
124                 uh.pack_end(up, false, false, 0);
125                 uh.pack_start(sb, false, false, 0);
126                 
127                 vbox.pack_start(uh, false, false, 8);
128                 first_sep = new HSeparator();
129                 vbox.pack_start(first_sep, false, false, 0);
130                 
131                 db = new HBox(false, 0);
132                 desc = new TextView();
133                 desc.set_wrap_mode(Gtk.WrapMode.WORD);
134                 desc.set_sensitive(false);
135                 db.pack_start(desc, true, true, 8);
136                 
137                 vbox.pack_start(db, false, false, 8);
138                 
139                 desc_sep = new HSeparator();
140                 
141                 vbox.pack_start(desc_sep, false, false, 0);
142         }
143         
144         /* get older statuses */
145         protected override void get_older() {
146                 //TODO
147         }
148         
149         /* get data about following */
150         private void update_following() {
151                 bool follow_him = false;
152                 
153                 try {
154                         follow_him = api.check_friendship(full_status.user_screen_name, true);
155                 } catch(RestError e) {
156                         updating_error(e.message);
157                         return;
158                 } catch(ParseError e) {
159                         updating_error(e.message);
160                         return;
161                 }
162                 
163                 full_status.following = follow_him;
164                 follow.active = follow_him;
165                 follow.set_sensitive(true);
166         }
167         
168         /* get user full info and his timeline */
169         public void show_user(string screen_name) {
170                 if(accounts.get_current_account().login == screen_name)
171                         return;
172                 
173                 act.activate();
174                 
175                 if(full_status != null && full_status.user_screen_name == screen_name)
176                         return;
177                 
178                 start_fetch(); //signal
179                 
180                 set_empty();
181                 start_screen();
182                 
183                 full_status = new FullStatus();
184                 full_status.user_screen_name = screen_name;
185                 
186                 try {
187                         lst = api.get_timeline(_items_count, full_status, "");
188                 } catch(RestError.CODE_404 e) {
189                         set_empty();
190                         updating_error(_("This user does not exist")); //signal
191                         
192                         return;
193                 } catch(RestError e) {
194                         set_empty();
195                         updating_error(e.message); //signal
196                         
197                         return;
198                 } catch(ParseError e) {
199                         set_empty();
200                         updating_error(e.message);
201
202                         return;
203                 }
204                 
205                 refresh_info();
206                 
207                 if(lst.size > 0) {
208                         refresh();
209                         update_following();
210                 }
211                 else
212                         set_empty(false);
213                 
214                 end_fetch(); //signal
215                 
216                 more.set_sensitive(true);
217         }
218         
219         /* set info about user */
220         private void refresh_info() {
221                 //follow.set_sensitive(true);
222                 followers.set_markup(followers_text.printf(full_status.followers));
223                 friends.set_markup(friends_text.printf(full_status.friends));
224                 statuses.set_markup(statuses_text.printf(full_status.statuses));
225                 
226                 if(full_status.url != "") {
227                         homepage.set_markup(homepage_text.printf(full_status.url, full_status.url));
228                         homepage.show();
229                 } else {
230                         homepage.hide();
231                 }
232                 
233                 username.set_markup(username_text.printf(full_status.user_name,
234                         full_status.user_screen_name));
235                 
236                 userpic.set_pic(full_status.user_avatar);
237                 
238                 uh.show();
239                 first_sep.show();
240                 
241                 if(full_status.desc != "") {
242                         desc.buffer.text = full_status.desc;
243                         db.show();
244                         desc_sep.show();
245                 } else {
246                         db.hide();
247                         desc_sep.hide();
248                 }
249                         
250         }
251         
252         /* clear all data */
253         public override void set_empty(bool full = true) {
254                 if(full) {
255                         userpic.set_default();
256                         full_status = null;
257                         follow.set_sensitive(false);
258                         followers.set_markup(followers_text.printf("-"));
259                         friends.set_markup(friends_text.printf("-"));
260                         statuses.set_markup(statuses_text.printf("-"));
261                         homepage.hide();
262                 
263                         username.set_markup(username_text.printf("-", "-"));
264                         
265                         db.hide();
266                         uh.hide();
267                         first_sep.hide();
268                         desc_sep.hide();
269                         desc.buffer.text = "";
270                 }
271                 
272                 more.set_sensitive(false);
273                 
274                 lst.clear();
275                 last_focused = 0;
276                 //update_content(template.generate_message(_("Empty")));
277                 update_content(template.generate_user_show_form());
278         }
279         
280         /* follow/unfollow some user */
281         private void follow_toggled() {
282                 //skip if this is initialization
283                 if(full_status != null && follow.active == full_status.following)
284                         return;
285                 
286                 follow.set_sensitive(false);
287                 
288                 if(follow.active) {
289                         try {
290                                 api.follow_create(full_status.user_screen_name);
291                         } catch(RestError e) {
292                                 updating_error(e.message);
293                                 follow.active = full_status.following;
294                                 follow.set_sensitive(true);
295                                 return;
296                         }
297                 } else {
298                         try {
299                                 api.follow_destroy(full_status.user_screen_name);
300                         } catch(RestError e) {
301                                 updating_error(e.message);
302                                 follow.active = full_status.following;
303                                 follow.set_sensitive(true);
304                                 return;
305                         }
306                 }
307                 
308                 full_status.following = follow.active;
309                 follow.set_sensitive(true);
310                 
311                 if(follow.active)
312                         follow_event(_("Now you follow this user")); //signal
313                 else
314                         follow_event(_("User was unfollowed")); //signal
315         }
316 }