7224263cb8d204890ac1c791ec5dbda8bdfbbc40
[debian/pino.git] / src / content_view.vala
1 using Gtk;
2 using WebKit;
3 using Gee;
4
5 public class ContentView : GLib.Object {
6         
7         private WebView? view;
8         private ScrolledWindow scroll;
9         protected VScrollbar slider;
10         public Frame frame;
11         private Accounts accounts;
12         private VisualStyle visual_style;
13         private Template tpl;
14         
15         private HashMap<string, string> content_map;
16         private HashMap<string, string> scroll_map;
17         
18         private string current_stream = "";
19         
20         public ContentView(Accounts accounts, VisualStyle visual_style) {
21                 view = new WebView();
22                 view.set_size_request(250, 350);
23                 view.navigation_policy_decision_requested.connect(event_route);
24                 view.settings.enable_default_context_menu = false;
25                 
26                 scroll = new ScrolledWindow(null, null);
27                 scroll.set_policy(PolicyType.AUTOMATIC, PolicyType.AUTOMATIC);
28                 scroll.add(view);
29                 
30                 slider = (VScrollbar)scroll.get_vscrollbar();
31                 
32                 frame = new Frame(null);
33                 frame.add(scroll);
34                 
35                 content_map = new HashMap<string, string>(str_hash, str_equal);
36                 scroll_map = new HashMap<string, string>(str_hash, str_equal);
37                 
38                 view.load_finished.connect((f) => {
39                         if(scroll_map.has_key(current_stream)) {
40                                 slider.set_value(scroll_map[current_stream].to_double());
41                         }
42                 });
43                 
44                 //when scroll to the bottom
45                 slider.value_changed.connect(slider_move);
46                 
47                 this.accounts = accounts;
48                 this.visual_style = visual_style;
49                 this.visual_style.changed.connect(update_style);
50                 
51                 tpl = new Template(this.visual_style);
52                 
53                 this.accounts.stream_was_updated.connect(generate_list);
54         }
55         
56         private void slider_move() {
57                 double max = slider.adjustment.upper;
58                 double current = slider.get_value();
59                 double scroll_size = slider.adjustment.page_size;
60                 
61                 if(current != 0 && current + scroll_size == max) {
62                         debug("need more");
63                         AStream? stream = accounts.stream_from_hash(current_stream);
64                         
65                         if(stream == null)
66                                 return;
67                         
68                         stream.menu_more();
69                 }
70         }
71         
72         private void generate_list(string hash, AStream stream) {
73                 debug(hash);
74                 
75                 if(stream.statuses.size == 0 && stream.statuses_fresh.size == 0)
76                         return;
77                 
78                 content_map.set(hash, tpl.stream_to_list(stream, hash));
79                 
80                 if(hash == current_stream)
81                         set_current_list(hash);
82         }
83         
84         public void set_current_list(string? hash) {
85                 if(hash == null)
86                         return;
87                 
88                 if(current_stream != "")
89                         scroll_map[current_stream] = slider.get_value().to_string();
90                 
91                 current_stream = hash;
92                 
93                 if(content_map.has_key(hash)) {
94                         view.load_string(content_map.get(hash), "text/html", "utf8", "file:///");
95                 } else {
96                         view.load_string("empty", "text/html", "utf8", "file:///");
97                 }
98                 
99                 debug(content_map.size.to_string());
100         }
101         
102         private void update_style() {
103                 view.settings.set_property("default-font-size", visual_style.font_size);
104                 view.settings.set_property("default-font-family", visual_style.font_family);
105                 
106                 tpl.render_header();
107                 
108                 accounts.update_all_streams();
109                 
110                 set_current_list(current_stream);
111                 
112                 debug("style changed");
113         }
114         
115         private bool event_route(WebFrame p0, NetworkRequest request,
116                 WebNavigationAction action, WebPolicyDecision decision) {
117                 if(request.uri == "")
118                         return false;
119                 
120                 string prot = request.uri.split("://")[0];
121                 string path = request.uri.split("://")[1];
122                 debug(prot);
123                 debug(path);
124                 
125                 if(prot == "file")
126                         return false;
127                 
128                 if(prot == "http" || prot == "https" || prot == "ftp") {
129                         GLib.Pid pid;
130                         GLib.Process.spawn_async(".", {"/usr/bin/xdg-open", request.uri}, null,
131                                 GLib.SpawnFlags.STDOUT_TO_DEV_NULL, null, out pid);
132                         return true;
133                 }
134                 
135                 accounts.new_content_action(prot, path);
136                 
137                 return true;
138         }
139 }