Imported Upstream version 0.2.10
[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                 
61                 if((home != null && home.size > 3) || (mentions != null && mentions.size > 3)
62                         || (direct != null && direct.size > 3)) {
63                         
64                         low_notify(home, mentions, direct);
65                 } else {
66                         full_notify(home, mentions, direct);
67                 }
68         }
69         
70         private void show_popup(Status status, bool direct = false) {
71                 string head = status.user_screen_name;
72                 if(prefs.fullNames)
73                         head = status.user_name;
74                 
75                 if(direct)
76                         head = "%s %s %s".printf(_("Direct message"), _("from"), head);
77                 
78                 Notification popup = new Notification(GLib.Markup.escape_text(head),
79                         GLib.Markup.escape_text(status.text), null, null);
80                 
81                 string av_path = cache.get_or_download(status.user_avatar,
82                         Cache.Method.ASYNC, false);
83                 if(av_path == status.user_avatar)
84                         popup.set_icon_from_pixbuf(logo);
85                 else {
86                         popup.set_icon_from_pixbuf(new Gdk.Pixbuf.from_file(av_path));
87                 }
88                 popup.set_timeout(5000); //doesn't working... hm
89                 popup.set_urgency(Notify.Urgency.NORMAL);
90                 popup.show();
91         }
92         
93         private void show_short_popup(string text) {
94                 Notification popup = new Notification(_("Updates"),
95                         text, null, null);
96                 popup.set_icon_from_pixbuf(logo);
97                 popup.set_timeout(5000);
98                 popup.set_urgency(Notify.Urgency.NORMAL);
99                 popup.show();
100         }
101         
102         /* one popup for each new status or DM */
103         private void full_notify(ArrayList<Status>? home, ArrayList<Status>? mentions,
104                 ArrayList<Status>? direct) {
105                 
106                 ArrayList<string> ids = new ArrayList<string>();
107                 
108                 if(prefs.showTimelineNotify && home != null) {
109                         for(int i = home.size -1; i > -1; i--) {
110                                 var status = home.get(i);
111                                 if(status.user_screen_name != login) {
112                                         show_popup(status);
113                                         ids.add(status.id);
114                                 }
115                         }
116                 }
117                 
118                 if(prefs.showMentionsNotify && mentions != null) {
119                         for(int i = mentions.size -1; i > -1; i--) {
120                                 var status = mentions.get(i);
121                                 if(!(status.id in ids))
122                                         show_popup(status);
123                         }
124                 }
125                 
126                 if(prefs.showDirectNotify && direct != null) {
127                         for(int i = direct.size -1; i > -1; i--) {
128                                 var status = direct.get(i);
129                                 show_popup(status, true);
130                         }
131                 }
132                 
133         }
134         
135         /* one popup on all updates */
136         private void low_notify(ArrayList<Status>? home, ArrayList<Status>? mentions,
137                 ArrayList<Status>? direct) {
138                 
139                 string result = "";
140                 
141                 if(home != null && prefs.showTimelineNotify && home.size > 0)
142                         result += _("in the home timeline: %d\n").printf(home.size);
143                 
144                 if(mentions != null && prefs.showMentionsNotify && mentions.size > 0)
145                         result += _("in mentions: %d\n").printf(mentions.size);
146                 
147                 if(direct != null && prefs.showDirectNotify && direct.size > 0)
148                         result += _("in direct messages: %d").printf(direct.size);
149                 
150                 if(result != "")
151                         show_short_popup(result);
152         }
153 }