sync
[gitlive] / Browser / View.js
1 Gtk = imports.gi.Gtk;
2 WebKit = imports.gi.WebKit;
3 Soup = imports.gi.Soup;
4
5 Browser = {
6 //    Tabbed : imports.Tabbed
7         Settings : imports.Settings
8 }
9
10         
11
12 Browser.View = new GType({
13     parent: WebKit.WebView.type,
14     name: "BrowserView",
15     init: function ()
16     {
17         // Private
18         var tab;
19         
20         var update_title = function (web_view, web_frame, title)
21         {
22             if(title.length > 25)
23                 title = title.slice(0,25) + "...";
24
25             tab.get_tab_label().label = title;
26         };
27
28         var update_url = function (web_view, web_frame)
29         {
30             //var toolbar = tab.get_toolbar();
31
32             //toolbar.set_url(web_frame.get_uri());
33             //toolbar.set_can_go_back(web_view.can_go_back());
34             //toolbar.set_can_go_forward(web_view.can_go_forward());
35         };
36
37         var update_progress = function (bar, progress)
38         {
39             //tab.get_toolbar().set_progress(progress / 100);
40         };
41
42         var create_new_tab = function (web_view, web_frame, new_web_view)
43         {
44             //new_web_view = new Browser.View();
45             //new_web_view.signal.web_view_ready.connect(show_new_tab);
46             //return new_web_view;
47         };
48         //var show_new_tab = function (new_web_view)
49         //{
50         //    Browser.Tabbed.browser.new_tab("", new_web_view);
51
52         //    return false;
53         //};
54
55         var hover_link = function (web_view, link, url)
56         {
57             //tab.get_statusbar().set_status(url);
58         };
59
60         var load_finished = function ()
61         {
62             //tab.get_toolbar().set_progress(0);
63         };
64
65         var load_committed = function (web_view, web_frame)
66         {
67             update_url(web_view, web_frame);
68         };
69
70         var clicked_link = function (web_view, web_frame, request,
71                                      action, decision, window)
72         {
73             if(action.get_reason() == WebKit.WebNavigationReason.LINK_CLICKED &&
74                action.get_button() == 2)
75             {
76               //  browser.new_tab(request.get_uri(), null);
77                 return true;
78             }
79
80             return false;
81         };
82
83         // Public
84         this.browse = function (url)
85         {
86             if(url.search("://") < 0)
87                 url = "http://" + url;
88
89             this.open(url);
90         };
91
92         this.set_tab = function (new_tab)
93         {
94             //tab = new_tab;
95         };
96
97         this.get_tab = function ()
98         {
99             //return tab;
100         };
101
102         // Implementation
103                 
104         //this.set_scroll_adjustments(null, null);
105
106         //this.signal.title_changed.connect(update_title);
107         //this.signal.load_committed.connect(load_committed);
108         //this.signal.load_finished.connect(load_finished);
109         //this.signal.load_progress_changed.connect(update_progress);
110
111         // For some reason, this segfaults seed in the instance init closure handler
112         // Once that's fixed, uncommenting the next line will give middle-click-open-in-new tab
113         //this.signal.navigation_policy_decision_requested.connect(clicked_link);
114
115         //this.signal.hovering_over_link.connect(hover_link);
116
117         //this.signal.create_web_view.connect(create_new_tab);
118                 var authmsg = false;
119                 
120                 this.signal.resource_request_starting.connect(function(
121                                                 web_view,
122                                                 web_frame,
123                                                  web_resource,
124                                                  request, // WebKitNetworkRequest
125                                                  response
126                                                                                                                            ) {
127                                 print("request starting")
128                                 print(request);
129                                 
130                                 if (!authmsg) { 
131                                         
132                                         var auth = new Soup.Auth.c_new(
133                                                 Soup.AuthBasic.type,
134                                                 request.message,
135                                                 "Basic realm=\"Test\"");
136          
137                                         
138                 
139                                         auth.authenticate(Browser.Settings.netrc.login,Browser.Settings.netrc.password);
140                                         var authmsg = auth.get_authorization(request.message);
141                                 }
142                                 //print(authmsg);
143                                 request.message.request_headers.append(
144                                         'Authorization', authmsg);
145                         
146                         
147                         
148                 });
149     }
150 });