2012-07-09 20:48:27 +00:00
|
|
|
/*
|
|
|
|
* This file is part of gitg
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 - Jesse van den Kieboom
|
|
|
|
*
|
|
|
|
* gitg is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* gitg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with gitg. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace GitgGtk
|
|
|
|
{
|
|
|
|
public class DiffView : WebKit.WebView
|
|
|
|
{
|
|
|
|
private Ggit.Diff? d_diff;
|
2013-02-21 16:41:25 +00:00
|
|
|
private Gitg.Commit? d_commit;
|
2012-07-17 09:46:17 +00:00
|
|
|
private Settings d_fontsettings;
|
2012-07-09 20:48:27 +00:00
|
|
|
|
|
|
|
private static Gee.HashMap<string, GitgGtk.DiffView> s_diffmap;
|
|
|
|
private static uint64 s_diff_id;
|
|
|
|
|
|
|
|
public File? custom_css { get; construct; }
|
|
|
|
public File? custom_js { get; construct; }
|
2012-07-11 06:44:23 +00:00
|
|
|
public Ggit.DiffOptions? options { get; construct set; }
|
2012-07-09 20:48:27 +00:00
|
|
|
|
2012-08-17 09:19:35 +00:00
|
|
|
private Cancellable d_cancellable;
|
2012-07-09 20:48:27 +00:00
|
|
|
private bool d_loaded;
|
2013-03-01 23:33:29 +00:00
|
|
|
private ulong d_diffid;
|
2012-07-09 20:48:27 +00:00
|
|
|
|
|
|
|
public Ggit.Diff? diff
|
|
|
|
{
|
|
|
|
get { return d_diff; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
d_diff = value;
|
2012-07-11 06:44:23 +00:00
|
|
|
d_commit = null;
|
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-21 16:41:25 +00:00
|
|
|
public Gitg.Commit? commit
|
2012-07-11 06:44:23 +00:00
|
|
|
{
|
|
|
|
get { return d_commit; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
d_commit = value;
|
|
|
|
d_diff = null;
|
|
|
|
|
2012-07-09 20:48:27 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static construct
|
|
|
|
{
|
2012-08-17 09:19:35 +00:00
|
|
|
s_diffmap = new Gee.HashMap<string, GitgGtk.DiffView>();
|
2012-07-09 20:48:27 +00:00
|
|
|
|
2012-08-17 09:19:35 +00:00
|
|
|
var context = WebKit.WebContext.get_default();
|
|
|
|
context.register_uri_scheme("gitg-diff", gitg_diff_request);
|
|
|
|
}
|
2012-07-09 20:48:27 +00:00
|
|
|
|
2012-08-17 09:19:35 +00:00
|
|
|
private static DiffViewRequest? parse_request(WebKit.URISchemeRequest request)
|
|
|
|
{
|
|
|
|
var uri = new Soup.URI(request.get_uri());
|
|
|
|
var path = uri.get_path();
|
|
|
|
var parts = path.split("/", 3);
|
2012-07-09 20:48:27 +00:00
|
|
|
|
2012-08-17 09:19:35 +00:00
|
|
|
if (parts.length != 3)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2012-07-09 20:48:27 +00:00
|
|
|
|
2012-08-17 09:19:35 +00:00
|
|
|
uri.set_scheme(parts[1]);
|
|
|
|
uri.set_path("/" + parts[2]);
|
|
|
|
|
|
|
|
DiffView? view = null;
|
|
|
|
|
|
|
|
var q = uri.get_query();
|
|
|
|
|
|
|
|
if (q != null)
|
|
|
|
{
|
|
|
|
var f = Soup.Form.decode(q);
|
|
|
|
var vid = f.lookup("viewid");
|
|
|
|
|
|
|
|
if (vid != null && s_diffmap.has_key(vid))
|
|
|
|
{
|
|
|
|
view = s_diffmap[vid];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (parts[1])
|
|
|
|
{
|
|
|
|
case "resource":
|
|
|
|
return new DiffViewRequestResource(view, request, uri);
|
|
|
|
case "diff":
|
|
|
|
return new DiffViewRequestDiff(view, request, uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void gitg_diff_request(WebKit.URISchemeRequest request)
|
|
|
|
{
|
|
|
|
var req = parse_request(request);
|
|
|
|
|
|
|
|
if (req.view != null)
|
|
|
|
{
|
|
|
|
req.view.request(req);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
req.run(null);
|
|
|
|
}
|
2012-07-09 20:48:27 +00:00
|
|
|
}
|
|
|
|
|
2012-08-17 09:19:35 +00:00
|
|
|
private void parse_font(string val, ref string family, ref uint size)
|
2012-07-17 09:46:17 +00:00
|
|
|
{
|
|
|
|
var fdesc = Pango.FontDescription.from_string(val);
|
|
|
|
|
|
|
|
var f = fdesc.get_family();
|
|
|
|
var s = fdesc.get_size();
|
|
|
|
|
|
|
|
if (f != null && f != "")
|
|
|
|
{
|
|
|
|
family = f;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s != 0)
|
|
|
|
{
|
|
|
|
if (fdesc.get_size_is_absolute())
|
|
|
|
{
|
|
|
|
size = s;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size = s / Pango.SCALE;
|
|
|
|
}
|
2012-10-30 21:21:10 +00:00
|
|
|
|
|
|
|
size = (uint)(size * get_screen().get_resolution() / 72.0);
|
2012-07-17 09:46:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-17 09:19:35 +00:00
|
|
|
public void request(DiffViewRequest request)
|
|
|
|
{
|
2013-03-01 23:33:29 +00:00
|
|
|
var did = request.parameter("diffid");
|
|
|
|
|
|
|
|
if (did != null)
|
|
|
|
{
|
|
|
|
uint64 i = uint64.parse(did);
|
|
|
|
|
|
|
|
if (i == d_diffid)
|
|
|
|
{
|
|
|
|
request.run(d_cancellable);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Still finish request, but with something bogus
|
|
|
|
request.finish_empty();
|
2012-08-17 09:19:35 +00:00
|
|
|
}
|
|
|
|
|
2012-07-17 09:46:17 +00:00
|
|
|
private void update_font_settings()
|
|
|
|
{
|
|
|
|
var settings = get_settings();
|
|
|
|
|
|
|
|
var fname = settings.default_font_family;
|
|
|
|
var fsize = settings.default_font_size;
|
|
|
|
|
|
|
|
parse_font(d_fontsettings.get_string("font-name"), ref fname, ref fsize);
|
|
|
|
|
|
|
|
settings.default_font_family = fname;
|
|
|
|
settings.default_font_size = fsize;
|
|
|
|
|
|
|
|
fname = settings.monospace_font_family;
|
|
|
|
fsize = settings.default_monospace_font_size;
|
|
|
|
|
|
|
|
parse_font(d_fontsettings.get_string("monospace-font-name"), ref fname, ref fsize);
|
|
|
|
|
|
|
|
settings.monospace_font_family = fname;
|
|
|
|
settings.default_monospace_font_size = fsize;
|
|
|
|
}
|
|
|
|
|
2012-08-17 09:19:35 +00:00
|
|
|
protected override void constructed()
|
2012-07-09 20:48:27 +00:00
|
|
|
{
|
2012-08-17 09:19:35 +00:00
|
|
|
base.constructed();
|
2012-07-09 20:48:27 +00:00
|
|
|
|
2012-08-17 09:19:35 +00:00
|
|
|
var settings = new WebKit.Settings();
|
2012-07-09 20:48:27 +00:00
|
|
|
|
2012-10-29 16:16:30 +00:00
|
|
|
var dbg = Environment.get_variable("GITG_GTK_DIFF_VIEW_DEBUG") != null;
|
2012-07-17 07:52:05 +00:00
|
|
|
|
|
|
|
if (dbg)
|
|
|
|
{
|
|
|
|
settings.enable_developer_extras = true;
|
2012-10-29 15:57:21 +00:00
|
|
|
|
|
|
|
Timeout.add(500, () => {
|
|
|
|
get_inspector().show();
|
|
|
|
return false;
|
|
|
|
});
|
2012-07-17 07:52:05 +00:00
|
|
|
}
|
|
|
|
|
2012-07-09 20:48:27 +00:00
|
|
|
settings.javascript_can_access_clipboard = true;
|
2012-07-17 09:46:17 +00:00
|
|
|
|
|
|
|
d_fontsettings = new Settings("org.gnome.desktop.interface");
|
2012-07-09 20:48:27 +00:00
|
|
|
set_settings(settings);
|
|
|
|
|
2012-07-17 09:46:17 +00:00
|
|
|
update_font_settings();
|
|
|
|
|
|
|
|
d_fontsettings.changed["monospace-font-name"].connect((s, k) => {
|
|
|
|
update_font_settings();
|
|
|
|
});
|
|
|
|
|
|
|
|
d_fontsettings.changed["font-name"].connect((s, k) => {
|
|
|
|
update_font_settings();
|
|
|
|
});
|
|
|
|
|
2012-07-09 20:48:27 +00:00
|
|
|
++s_diff_id;
|
|
|
|
s_diffmap[s_diff_id.to_string()] = this;
|
|
|
|
|
2012-08-17 09:19:35 +00:00
|
|
|
d_cancellable = new Cancellable();
|
|
|
|
|
|
|
|
load_changed.connect((v, ev) => {
|
|
|
|
if (ev == WebKit.LoadEvent.FINISHED)
|
|
|
|
{
|
|
|
|
d_loaded = true;
|
|
|
|
update();
|
|
|
|
}
|
2012-07-10 15:06:18 +00:00
|
|
|
});
|
|
|
|
|
2012-07-09 20:48:27 +00:00
|
|
|
// Load the diff base html
|
2012-10-30 21:21:10 +00:00
|
|
|
var uri = "gitg-diff:///resource/org/gnome/gitg/gtk/diff-view/diff-view.html?viewid=" + s_diff_id.to_string();
|
2012-07-09 20:48:27 +00:00
|
|
|
|
|
|
|
// Add custom js as a query parameter
|
|
|
|
if (custom_js != null)
|
|
|
|
{
|
|
|
|
uri += "&js=" + Soup.URI.encode(custom_js.get_uri(), null);
|
|
|
|
}
|
|
|
|
|
2012-08-17 09:19:35 +00:00
|
|
|
// Add custom css as a query parameter
|
|
|
|
if (custom_css != null)
|
|
|
|
{
|
|
|
|
uri += "&css=" + Soup.URI.encode(custom_css.get_uri(), null);
|
|
|
|
}
|
|
|
|
|
2013-03-02 13:33:20 +00:00
|
|
|
if (dbg)
|
|
|
|
{
|
|
|
|
uri += "&debug=true";
|
|
|
|
}
|
|
|
|
|
2012-07-10 15:06:18 +00:00
|
|
|
d_loaded = false;
|
2012-07-09 20:48:27 +00:00
|
|
|
|
|
|
|
load_uri(uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
public DiffView(File? custom_js)
|
|
|
|
{
|
|
|
|
Object(custom_js: custom_js);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void update()
|
|
|
|
{
|
2012-07-11 06:44:23 +00:00
|
|
|
if (!d_loaded || (d_diff == null && d_commit == null))
|
2012-07-09 20:48:27 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-17 09:19:35 +00:00
|
|
|
// Cancel running operations
|
|
|
|
d_cancellable.cancel();
|
|
|
|
d_cancellable = new Cancellable();
|
|
|
|
|
2013-03-01 23:33:29 +00:00
|
|
|
++d_diffid;
|
|
|
|
|
2012-07-11 06:44:23 +00:00
|
|
|
if (d_commit != null)
|
|
|
|
{
|
2013-02-21 16:41:25 +00:00
|
|
|
d_diff = d_commit.get_diff(options);
|
2012-07-11 06:44:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (d_diff != null)
|
|
|
|
{
|
2013-03-01 23:33:29 +00:00
|
|
|
run_javascript.begin("update_diff(%lu);".printf(d_diffid), d_cancellable, (obj, res) => {
|
2012-08-17 09:19:35 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
run_javascript.end(res);
|
|
|
|
} catch {}
|
|
|
|
});
|
2012-07-11 06:44:23 +00:00
|
|
|
}
|
2012-07-09 20:48:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// vi:ts=4
|