git-delete-branches-pane.c 8.8 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 25 26 27 28 29
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * git-shell-test
 * Copyright (C) James Liggett 2010 <jrliggett@cox.net>
 * 
 * git-shell-test 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 3 of the License, or
 * (at your option) any later version.
 * 
 * git-shell-test 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "git-delete-branches-pane.h"

struct _GitDeleteBranchesPanePriv
{
	GtkBuilder *builder;
};

G_DEFINE_TYPE (GitDeleteBranchesPane, git_delete_branches_pane, GIT_TYPE_PANE);

static void
30
on_ok_action_activated (GtkAction *action, GitDeleteBranchesPane *self)
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
{
	Git *plugin;
	GtkToggleButton *require_merged_check;
	GList *selected_local_branches;
	GList *selected_remote_branches;
	GitBranchDeleteCommand *local_delete_command;
	GitBranchDeleteCommand *remote_delete_command;
	AnjutaCommandQueue *queue;

	plugin = ANJUTA_PLUGIN_GIT (anjuta_dock_pane_get_plugin (ANJUTA_DOCK_PANE (self)));
	require_merged_check = GTK_TOGGLE_BUTTON (gtk_builder_get_object (self->priv->builder,
	                                                                  "require_merged_check"));
	selected_local_branches = git_branches_pane_get_selected_local_branches (GIT_BRANCHES_PANE (plugin->branches_pane));
	selected_remote_branches = git_branches_pane_get_selected_remote_branches (GIT_BRANCHES_PANE (plugin->branches_pane));

	/* The user might not have selected anything */
	if (git_branches_pane_count_selected_items (GIT_BRANCHES_PANE (plugin->branches_pane)) > 0)
	{
		queue = anjuta_command_queue_new (ANJUTA_COMMAND_QUEUE_EXECUTE_MANUAL);
		
		if (selected_local_branches)
		{
			local_delete_command = git_branch_delete_command_new (plugin->project_root_directory,
			                                                      selected_local_branches,
			                                                      FALSE,
			                                                      gtk_toggle_button_get_active (require_merged_check));

58
			anjuta_util_glist_strings_free (selected_local_branches);
59

60 61 62 63 64 65
			g_signal_connect (G_OBJECT (local_delete_command), 
			                  "command-finished",
			                  G_CALLBACK (git_pane_report_errors),
			                  plugin);
			

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
			g_signal_connect (G_OBJECT (local_delete_command), 
			                  "command-finished",
			                  G_CALLBACK (g_object_unref),
			                  NULL);

			anjuta_command_queue_push (queue, 
			                           ANJUTA_COMMAND (local_delete_command));
		}

		if (selected_remote_branches)
		{
			remote_delete_command = git_branch_delete_command_new (plugin->project_root_directory,
			                                                       selected_remote_branches,
			                                                       TRUE,
			                                                       gtk_toggle_button_get_active (require_merged_check));

82
			anjuta_util_glist_strings_free (selected_remote_branches);
83

84 85 86 87 88 89
			g_signal_connect (G_OBJECT (remote_delete_command), 
			                  "command-finished",
			                  G_CALLBACK (git_pane_report_errors),
			                  plugin);


90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
			g_signal_connect (G_OBJECT (remote_delete_command), "command-finished",
			                  G_CALLBACK (g_object_unref),
			                  NULL);
			
			anjuta_command_queue_push (queue, 
			                           ANJUTA_COMMAND (remote_delete_command));
		}

		/* Run the commands */
		g_signal_connect (G_OBJECT (queue), "finished",
		                  G_CALLBACK (g_object_unref),
		                  NULL);

		anjuta_command_queue_start (queue);
	}


107
	git_pane_remove_from_dock (GIT_PANE (self));
108 109 110
	                            
}

111 112 113 114 115 116 117 118 119 120 121 122
static void
on_delete_branches_pane_map (GtkWidget *widget, GitBranchesPane *branches_pane)
{
	git_branches_pane_set_select_column_visible (branches_pane, TRUE);
}

static void
on_delete_branches_pane_unmap (GtkWidget *widget, GitBranchesPane *branches_pane)
{
	git_branches_pane_set_select_column_visible (branches_pane, FALSE);
}

123 124 125 126
static void
git_delete_branches_pane_init (GitDeleteBranchesPane *self)
{
	gchar *objects[] = {"delete_branches_pane",
127 128
						"ok_action",
						"cancel_action",
129 130
						NULL};
	GError *error = NULL;
131 132
	GtkAction *ok_action;
	GtkAction *cancel_action;
133 134 135 136 137 138 139 140 141 142 143 144

	self->priv = g_new0 (GitDeleteBranchesPanePriv, 1);
	self->priv->builder = gtk_builder_new ();

	if (!gtk_builder_add_objects_from_file (self->priv->builder, BUILDER_FILE, 
	                                        objects, 
	                                        &error))
	{
		g_warning ("Couldn't load builder file: %s", error->message);
		g_error_free (error);
	}

145 146 147 148
	ok_action = GTK_ACTION (gtk_builder_get_object (self->priv->builder,
	                                                "ok_action"));
	cancel_action = GTK_ACTION (gtk_builder_get_object (self->priv->builder,
	                                                "cancel_action"));
149

150 151
	g_signal_connect (G_OBJECT (ok_action), "activate",
	                  G_CALLBACK (on_ok_action_activated),
152 153
	                  self);

154
	g_signal_connect_swapped (G_OBJECT (cancel_action), "activate",
155 156
	                          G_CALLBACK (git_pane_remove_from_dock),
	                          self);
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
}

static void
git_delete_branches_pane_finalize (GObject *object)
{
	GitDeleteBranchesPane *self;

	self = GIT_DELETE_BRANCHES_PANE (object);

	g_object_unref (self->priv->builder);
	g_free (self->priv);

	G_OBJECT_CLASS (git_delete_branches_pane_parent_class)->finalize (object);
}

static GtkWidget *
get_widget (AnjutaDockPane *pane)
{
	GitDeleteBranchesPane *self;

	self = GIT_DELETE_BRANCHES_PANE (pane);

	return GTK_WIDGET (gtk_builder_get_object (self->priv->builder,
	                                           "delete_branches_pane"));
}

static void
git_delete_branches_pane_class_init (GitDeleteBranchesPaneClass *klass)
{
	GObjectClass* object_class = G_OBJECT_CLASS (klass);
	AnjutaDockPaneClass* pane_class = ANJUTA_DOCK_PANE_CLASS (klass);

	object_class->finalize = git_delete_branches_pane_finalize;
	pane_class->get_widget = get_widget;
	pane_class->refresh = NULL;
}


AnjutaDockPane *
git_delete_branches_pane_new (Git *plugin)
{
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
	GitDeleteBranchesPane *self;
	GtkWidget *delete_branches_pane;
	
	self = g_object_new (GIT_TYPE_DELETE_BRANCHES_PANE, "plugin", plugin, NULL);
	delete_branches_pane = GTK_WIDGET (gtk_builder_get_object (self->priv->builder,
	                                                           "delete_branches_pane"));

	g_signal_connect (G_OBJECT (delete_branches_pane), "map",
	                  G_CALLBACK (on_delete_branches_pane_map),
	                  plugin->branches_pane);

	g_signal_connect (G_OBJECT (delete_branches_pane), "unmap",
	                  G_CALLBACK (on_delete_branches_pane_unmap),
	                  plugin->branches_pane);

	return ANJUTA_DOCK_PANE (self);
214 215 216 217 218 219 220 221 222
}

void
on_delete_branches_button_clicked (GtkAction *action, Git *plugin)
{
	AnjutaDockPane *delete_branches_pane;

	delete_branches_pane = git_delete_branches_pane_new (plugin);

223 224 225
	anjuta_dock_replace_command_pane (ANJUTA_DOCK (plugin->dock), "DeleteBranches",
	                                  "Delete Branches", NULL, delete_branches_pane,
	                                  GDL_DOCK_BOTTOM, NULL, 0, NULL);
226
}
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261

void
on_git_branch_delete_activated (GtkAction *action, Git *plugin)
{
	gchar *selected_branch;
	GList *branches;
	GitBranchDeleteCommand *delete_command;

	selected_branch = git_branches_pane_get_selected_branch (GIT_BRANCHES_PANE (plugin->branches_pane));

	if (anjuta_util_dialog_boolean_question (NULL, FALSE, 
	                                         _("Are you sure you want to delete branch %s?"),
	                                         selected_branch))
	{
		branches = g_list_append (NULL, selected_branch);
		delete_command = git_branch_delete_command_new (plugin->project_root_directory,
		                                                branches, 
		                                                git_branches_pane_is_selected_branch_remote (GIT_BRANCHES_PANE (plugin->branches_pane)),
		                                                FALSE);

		g_list_free (branches);

		g_signal_connect (G_OBJECT (delete_command), "command-finished",
		                  G_CALLBACK (git_pane_report_errors),
		                  plugin);

		g_signal_connect (G_OBJECT (delete_command), "command-finished",
		                  G_CALLBACK (g_object_unref),
		                  NULL);

		anjuta_command_start (ANJUTA_COMMAND (delete_command));
	}

	g_free (selected_branch);
}