Imported Upstream version 0.2.0
[debian/pino.git] / src / popups.vala
1 /* popup.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 Notify;
23 using Gee;
24 using Auth;
25 using RestAPI;
26
27 public class Popups : Object {
28         
29         private Prefs prefs;
30         private Accounts accounts;
31         private Cache cache;
32         private Gdk.Pixbuf logo;
33         
34         private string? login;
35         
36         public Popups(Prefs _prefs, Accounts _accounts, Cache _cache,
37                 Gdk.Pixbuf _logo) {
38                 //libnotify init
39                 Notify.init(Config.APPNAME);
40                 
41                 prefs = _prefs;
42                 accounts = _accounts;
43                 cache = _cache;
44                 logo = _logo;
45                 
46                 login_changed();
47                 accounts.changed.connect(login_changed);
48                 accounts.active_changed.connect(login_changed);
49         }
50         
51         private void login_changed() {
52                 var acc = accounts.get_current_account();
53                 
54                 if(acc != null);
55                         login = acc.login;
56         }
57         
58         public void start(ArrayList<Status>? home, ArrayList<Status>? mentions,
59                 ArrayList<Status>? direct) {
60                 if(home == null)
61                         return;
62                 
63                 if(home.size > 3 || mentions.size > 3 || direct.size > 3)
64                         low_notify(home, mentions, direct);
65                 else
66                         full_notify(home, mentions, direct);
67         }
68         
69         private void show_popup(Status status, bool direct = false) {
70                 string head = status.user_screen_name;
71                 if(prefs.fullNames)
72                         head = status.user_name;
73                 
74                 if(direct)
75                         head = "%s %s %s".printf(_("Direct message"), _("from"), head);
76                 
77                 Notification popup = new Notification(head, status.text, null, null);
78                 
79                 string av_path = cache.get_or_download(status.user_avatar,
80                         Cache.Method.ASYNC, false);
81                 if(av_path == status.user_avatar)
82                         popup.set_icon_from_pixbuf(logo);
83                 else {
84                         popup.set_icon_from_pixbuf(new Gdk.Pixbuf.from_file(av_path));
85                 }
86                 popup.set_timeout(5000); //doesn't working... hm
87                 popup.set_urgency(Notify.Urgency.NORMAL);
88                 popup.show();
89         }
90         
91         private void show_short_popup(string text) {
92                 Notification popup = new Notification(_("Updates"),
93                         text, null, null);
94                 popup.set_icon_from_pixbuf(logo);
95                 popup.set_timeout(5000);
96                 popup.set_urgency(Notify.Urgency.NORMAL);
97                 popup.show();
98         }
99         
100         /* one popup for each new status or DM */
101         private void full_notify(ArrayList<Status> home, ArrayList<Status> mentions,
102                 ArrayList<Status> direct) {
103                 
104                 ArrayList<string> ids = new ArrayList<string>();
105                 
106                 if(prefs.showTimelineNotify) {
107                         for(int i = home.size -1; i > -1; i--) {
108                                 var status = home.get(i);
109                                 if(status.user_screen_name != login) {
110                                         show_popup(status);
111                                         ids.add(status.id);
112                                 }
113                         }
114                 }
115                 
116                 if(prefs.showMentionsNotify) {
117                         for(int i = mentions.size -1; i > -1; i--) {
118                                 var status = mentions.get(i);
119                                 if(!(status.id in ids))
120                                         show_popup(status);
121                         }
122                 }
123                 
124                 if(prefs.showDirectNotify) {
125                         for(int i = direct.size -1; i > -1; i--) {
126                                 var status = direct.get(i);
127                                 show_popup(status, true);
128                         }
129                 }
130                 
131         }
132         
133         /* one popup on all updates */
134         private void low_notify(ArrayList<Status> home, ArrayList<Status> mentions,
135                 ArrayList<Status> direct) {
136                 
137                 string result = "";
138                 
139                 if(prefs.showTimelineNotify && home.size > 0)
140                         result += _("in the home timeline: %d\n").printf(home.size);
141                 
142                 if(prefs.showMentionsNotify && mentions.size > 0)
143                         result += _("in mentions: %d\n").printf(mentions.size);
144                 
145                 if(prefs.showDirectNotify && direct.size > 0)
146                         result += _("in direct messages: %d").printf(direct.size);
147                 
148                 if(result != "")
149                         show_short_popup(result);
150         }
151 }