1/*
2 * This file is part of gitg
3 *
4 * Copyright (C) 2014 - Jesse van den Kieboom
5 *
6 * gitg is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * gitg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with gitg. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20namespace Gitg
21{
22
23class RefActionRename : GitgExt.UIElement, GitgExt.Action, GitgExt.RefAction, Object
24{
25	// Do this to pull in config.h before glib.h (for gettext...)
26	private const string version = Gitg.Config.VERSION;
27
28	public GitgExt.Application? application { owned get; construct set; }
29	public GitgExt.RefActionInterface action_interface { get; construct set; }
30	public Gitg.Ref reference { get; construct set; }
31
32	public RefActionRename(GitgExt.Application        application,
33	                       GitgExt.RefActionInterface action_interface,
34	                       Gitg.Ref                   reference)
35	{
36		Object(application:      application,
37		       action_interface: action_interface,
38		       reference:        reference);
39	}
40
41	public string id
42	{
43		owned get { return "/org/gnome/gitg/ref-actions/rename"; }
44	}
45
46	public string display_name
47	{
48		owned get { return _("Rename"); }
49	}
50
51	public string description
52	{
53		owned get { return _("Rename the selected reference"); }
54	}
55
56	public bool enabled
57	{
58		get
59		{
60			return    reference.is_branch()
61			       || reference.is_tag();
62		}
63	}
64
65	public void activate()
66	{
67		action_interface.edit_ref_name(reference, on_ref_name_editing_done);
68	}
69
70	private void on_ref_name_editing_done(string new_text, bool cancelled)
71	{
72		if (cancelled)
73		{
74			return;
75		}
76
77		string orig;
78		string? prefix;
79
80		var pn = reference.parsed_name;
81
82		if (pn.rtype == Gitg.RefType.REMOTE)
83		{
84			orig = pn.remote_branch;
85			prefix = pn.prefix + "/" + pn.remote_name + "/";
86		}
87		else
88		{
89			orig = pn.shortname;
90			prefix = pn.prefix;
91		}
92
93		if (orig == new_text)
94		{
95			return;
96		}
97
98		if (!Ggit.Ref.is_valid_name(@"$prefix$new_text"))
99		{
100			var msg = _("The specified name “%s” contains invalid characters").printf(new_text);
101
102			action_interface.application.show_infobar(_("Invalid name"),
103			                                          msg,
104			                                          Gtk.MessageType.ERROR);
105
106			return;
107		}
108
109		var branch = reference as Ggit.Branch;
110		Gitg.Ref? new_ref = null;
111
112		try
113		{
114			if (branch != null)
115			{
116				new_ref = branch.move(new_text,
117				                      Ggit.CreateFlags.NONE) as Gitg.Ref;
118			}
119			else
120			{
121				var msg = "rename: ref %s to %s".printf(reference.get_name(),
122				                                        new_text);
123
124				new_ref = reference.rename(new_text,
125				                           false,
126				                           msg) as Gitg.Ref;
127			}
128		}
129		catch (Error e)
130		{
131			action_interface.application.show_infobar(_("Failed to rename"),
132			                                          e.message,
133			                                          Gtk.MessageType.ERROR);
134
135			return;
136		}
137
138		action_interface.replace_ref(reference, new_ref);
139	}
140}
141
142}
143
144// ex:set ts=4 noet
145