gitg-ref-action-fetch.vala 3.84 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * This file is part of gitg
 *
 * Copyright (C) 2014 - 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 Gitg
{

class RefActionFetch : GitgExt.UIElement, GitgExt.Action, GitgExt.RefAction, Object
{
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
	// Do this to pull in config.h before glib.h (for gettext...)
	private const string version = Gitg.Config.VERSION;

	public GitgExt.Application? application { owned get; construct set; }
	public GitgExt.RefActionInterface action_interface { get; construct set; }
	public Gitg.Ref reference { get; construct set; }

	private Gitg.Ref? d_remote_ref;
	private Gitg.Remote? d_remote;

	public RefActionFetch(GitgExt.Application        application,
	                      GitgExt.RefActionInterface action_interface,
	                      Gitg.Ref                   reference)
	{
		Object(application:      application,
		       action_interface: action_interface,
		       reference:        reference);

		var branch = reference as Ggit.Branch;

		if (branch != null)
		{
			try
			{
				d_remote_ref = branch.get_upstream() as Gitg.Ref;
			} catch {}
		}
		else if (reference.parsed_name.remote_name != null)
		{
			d_remote_ref = reference;
		}

		if (d_remote_ref != null)
		{
			d_remote = application.remote_lookup.lookup(d_remote_ref.parsed_name.remote_name);
		}
	}

	public string id
	{
		owned get { return "/org/gnome/gitg/ref-actions/fetch"; }
	}

	public string display_name
	{
		owned get
		{
			if (d_remote != null)
			{
				return _("Fetch from %s").printf(d_remote_ref.parsed_name.remote_name);
			}
			else
			{
				return "";
			}
		}
	}

	public string description
	{
		owned get { return _("Fetch remote objects from %s").printf(d_remote_ref.parsed_name.remote_name); }
	}

	public bool available
	{
		get { return d_remote != null; }
	}

93
	public async bool fetch()
94
	{
95 96 97 98 99 100 101
		var notification = new RemoteNotification(d_remote);
		application.notifications.add(notification);

		notification.text = _("Fetching from %s").printf(d_remote.get_url());

		var updates = new Gee.ArrayList<string>();

102
		var tip_updated_id = d_remote.tip_updated.connect((d_remote, name, a, b) => {
103 104 105 106 107 108 109 110 111 112 113 114
			if (a.is_zero())
			{
				/* Translators: new refers to a new remote reference having been fetched, */
				updates.add(@"$name (%s)".printf(_("new")));
			}
			else
			{
				/* Translators: updated refers to a remote reference having been updated, */
				updates.add(@"$name (%s)".printf(_("updated")));
			}
		});

115 116 117 118 119 120 121 122
		try
		{
			yield d_remote.fetch(null, null);
		}
		catch (Error e)
		{
			notification.error(_("Failed to fetch from %s: %s").printf(d_remote.get_url(), e.message));
			stderr.printf("Failed to fetch: %s\n", e.message);
123

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
			return false;
		}
		finally
		{
			((Object)d_remote).disconnect(tip_updated_id);
		}

		if (updates.size == 0)
		{
			/* Translators: the %s will get replaced with the remote url, */
			notification.success(_("Fetched from %s: everything is up to date").printf(d_remote.get_url()));
		}
		else
		{
			/* Translators: the first %s is the remote url to fetch from,
			 * the second is a list of references that got updated. */
			notification.success(_("Fetched from %s: %s").printf(d_remote.get_url(), string.joinv(", ", updates.to_array())));
		}
142

143 144 145 146 147 148 149
		return true;
	}

	public void activate()
	{
		fetch.begin((obj, res) => {
			fetch.end(res);
150 151
		});
	}
152 153 154 155 156
}

}

// ex:set ts=4 noet