1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4 * Copyright (C) James Liggett 2010 <jrliggett@cox.net>
6 * anjuta is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * anjuta is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "git-stash-pane.h"
30 struct _GitStashPanePriv
35 G_DEFINE_TYPE (GitStashPane
, git_stash_pane
, GIT_TYPE_PANE
);
38 on_stash_list_command_started (AnjutaCommand
*command
, GitStashPane
*self
)
40 GtkTreeView
*stash_view
;
41 GtkTreeStore
*stash_model
;
43 stash_view
= GTK_TREE_VIEW (gtk_builder_get_object (self
->priv
->builder
,
45 stash_model
= GTK_TREE_STORE (gtk_builder_get_object (self
->priv
->builder
,
48 gtk_tree_view_set_model (stash_view
, NULL
);
49 gtk_tree_store_clear (stash_model
);
53 on_stash_list_command_finished (AnjutaCommand
*command
, guint return_code
,
56 GtkTreeView
*stash_view
;
57 GtkTreeModel
*stash_model
;
59 stash_view
= GTK_TREE_VIEW (gtk_builder_get_object (self
->priv
->builder
,
61 stash_model
= GTK_TREE_MODEL (gtk_builder_get_object (self
->priv
->builder
,
64 gtk_tree_view_set_model (stash_view
, stash_model
);
68 on_stash_diff_command_finished (AnjutaCommand
*command
, guint return_code
,
69 GtkTreeStore
*stash_model
)
71 GtkTreePath
*parent_path
;
72 GtkTreeIter parent_iter
;
79 parent_path
= g_object_get_data (G_OBJECT (command
), "parent-path");
80 gtk_tree_model_get_iter (GTK_TREE_MODEL (stash_model
), &parent_iter
,
83 output
= git_raw_output_command_get_output (GIT_RAW_OUTPUT_COMMAND (command
));
85 while (g_queue_peek_head (output
))
87 output_line
= g_queue_pop_head (output
);
89 gtk_tree_store_append (stash_model
, &iter
, &parent_iter
);
90 gtk_tree_store_set (stash_model
, &iter
,
91 COL_DIFF
, output_line
,
100 on_stash_list_command_data_arrived (AnjutaCommand
*command
,
101 GtkTreeStore
*stash_model
)
109 gchar
*working_directory
;
110 GitStashShowCommand
*show_command
;
112 output
= git_stash_list_command_get_output (GIT_STASH_LIST_COMMAND (command
));
114 while (g_queue_peek_head (output
))
116 stash
= g_queue_pop_head (output
);
117 number
= git_stash_get_number (stash
);
118 message
= git_stash_get_message (stash
);
119 id
= git_stash_get_id (stash
);
122 gtk_tree_store_append (stash_model
, &iter
, NULL
);
123 gtk_tree_store_set (stash_model
, &iter
,
125 COL_MESSAGE
, message
,
129 g_object_get (G_OBJECT (command
), "working-directory",
130 &working_directory
, NULL
);
131 show_command
= git_stash_show_command_new (working_directory
, id
);
133 g_free (working_directory
);
135 g_object_set_data_full (G_OBJECT (show_command
), "parent-path",
136 gtk_tree_model_get_path (GTK_TREE_MODEL (stash_model
),
138 (GDestroyNotify
) gtk_tree_path_free
);
140 g_signal_connect (G_OBJECT (show_command
), "command-finished",
141 G_CALLBACK (on_stash_diff_command_finished
),
144 g_signal_connect (G_OBJECT (show_command
), "command-finished",
145 G_CALLBACK (g_object_unref
),
148 anjuta_command_start (ANJUTA_COMMAND (show_command
));
150 g_object_unref (stash
);
157 on_stash_view_button_press_event (GtkWidget
*stash_view
, GdkEventButton
*event
,
162 gboolean ret
= FALSE
;
164 path_valid
= gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (stash_view
),
165 event
->x
, event
->y
, &path
, NULL
,
168 if (event
->type
== GDK_BUTTON_PRESS
&& event
->button
== 3)
170 if (path_valid
&& gtk_tree_path_get_depth (path
) == 1)
172 git_pane_popup_menu (GIT_PANE (self
), "GitStashPopup", event
->button
,
177 /* Don't forward button presses to diff renderers */
180 ret
= gtk_tree_path_get_depth (path
) == 2;
181 gtk_tree_path_free (path
);
188 stash_message_renderer_data_func (GtkTreeViewColumn
*tree_column
,
189 GtkCellRenderer
*renderer
,
197 /* Don't show this column on diffs */
198 visible
= gtk_tree_store_iter_depth (GTK_TREE_STORE (model
), iter
) == 0;
199 gtk_cell_renderer_set_visible (renderer
, visible
);
203 gtk_tree_model_get (model
, iter
, COL_MESSAGE
, &message
, -1);
204 g_object_set (renderer
, "text", message
, NULL
);
209 g_object_set (renderer
, "text", "", NULL
);
213 stash_number_renderer_data_func (GtkTreeViewColumn
*tree_column
,
214 GtkCellRenderer
*renderer
,
221 gchar
*number_string
;
223 /* Don't show this column on diffs */
224 visible
= gtk_tree_store_iter_depth (GTK_TREE_STORE (model
), iter
) == 0;
225 gtk_cell_renderer_set_visible (renderer
, visible
);
229 gtk_tree_model_get (model
, iter
, COL_NUMBER
, &number
, -1);
230 number_string
= g_strdup_printf ("%i", number
);
232 g_object_set (renderer
, "text", number_string
, NULL
);
233 g_free (number_string
);
236 g_object_set (renderer
, "text", "", NULL
);
240 on_stash_view_row_selected (GtkTreeSelection
*selection
,
243 gboolean path_currently_selected
,
246 return gtk_tree_path_get_depth (path
) == 1;
250 git_stash_pane_init (GitStashPane
*self
)
252 gchar
*objects
[] = {"stash_pane",
255 GError
*error
= NULL
;
256 GtkTreeView
*stash_view
;
257 GtkTreeViewColumn
*stash_number_column
;
258 GtkCellRenderer
*stash_number_renderer
;
259 GtkTreeViewColumn
*stash_message_column
;
260 GtkCellRenderer
*stash_message_renderer
;
261 GtkCellRenderer
*diff_renderer
;
262 GtkTreeSelection
*selection
;
264 self
->priv
= g_new0 (GitStashPanePriv
, 1);
265 self
->priv
->builder
= gtk_builder_new ();
267 if (!gtk_builder_add_objects_from_file (self
->priv
->builder
, BUILDER_FILE
,
271 g_warning ("Couldn't load builder file: %s", error
->message
);
272 g_error_free (error
);
275 stash_view
= GTK_TREE_VIEW (gtk_builder_get_object (self
->priv
->builder
,
277 stash_number_column
= GTK_TREE_VIEW_COLUMN (gtk_builder_get_object (self
->priv
->builder
,
278 "stash_number_column"));
279 stash_number_renderer
= GTK_CELL_RENDERER (gtk_builder_get_object (self
->priv
->builder
,
280 "stash_number_renderer"));
281 stash_message_column
= GTK_TREE_VIEW_COLUMN (gtk_builder_get_object (self
->priv
->builder
,
282 "stash_message_column"));
283 stash_message_renderer
= GTK_CELL_RENDERER (gtk_builder_get_object (self
->priv
->builder
,
284 "stash_message_renderer"));
285 diff_renderer
= anjuta_cell_renderer_diff_new ();
286 selection
= gtk_tree_view_get_selection (stash_view
);
288 gtk_tree_view_column_set_cell_data_func (stash_number_column
, stash_number_renderer
,
289 stash_number_renderer_data_func
,
291 gtk_tree_view_column_set_cell_data_func (stash_message_column
, stash_message_renderer
,
292 stash_message_renderer_data_func
,
295 gtk_tree_view_column_pack_start (stash_message_column
, diff_renderer
, TRUE
);
296 gtk_tree_view_column_add_attribute (stash_message_column
, diff_renderer
,
299 /* Don't allow diffs to be selected */
300 gtk_tree_selection_set_select_function (selection
, on_stash_view_row_selected
,
303 g_signal_connect (G_OBJECT (stash_view
), "button-press-event",
304 G_CALLBACK (on_stash_view_button_press_event
),
309 git_stash_pane_finalize (GObject
*object
)
313 self
= GIT_STASH_PANE (object
);
315 g_object_unref (self
->priv
->builder
);
318 G_OBJECT_CLASS (git_stash_pane_parent_class
)->finalize (object
);
322 git_stash_pane_get_widget (AnjutaDockPane
*pane
)
326 self
= GIT_STASH_PANE (pane
);
328 return GTK_WIDGET (gtk_builder_get_object (self
->priv
->builder
,
333 git_stash_pane_class_init (GitStashPaneClass
*klass
)
335 GObjectClass
* object_class
= G_OBJECT_CLASS (klass
);
336 AnjutaDockPaneClass
*pane_class
= ANJUTA_DOCK_PANE_CLASS (klass
);
338 object_class
->finalize
= git_stash_pane_finalize
;
339 pane_class
->get_widget
= git_stash_pane_get_widget
;
340 pane_class
->refresh
= NULL
;
345 git_stash_pane_new (Git
*plugin
)
348 GtkTreeStore
*stash_model
;
350 self
= g_object_new (GIT_TYPE_STASH_PANE
, "plugin", plugin
, NULL
);
351 stash_model
= GTK_TREE_STORE (gtk_builder_get_object (self
->priv
->builder
,
354 g_signal_connect (G_OBJECT (plugin
->stash_list_command
), "command-started",
355 G_CALLBACK (on_stash_list_command_started
),
358 g_signal_connect (G_OBJECT (plugin
->stash_list_command
), "command-finished",
359 G_CALLBACK (on_stash_list_command_finished
),
362 g_signal_connect (G_OBJECT (plugin
->stash_list_command
), "data-arrived",
363 G_CALLBACK (on_stash_list_command_data_arrived
),
366 return ANJUTA_DOCK_PANE (self
);
370 git_stash_pane_get_selected_stash_id (GitStashPane
*self
)
372 GtkTreeView
*stash_view
;
373 GtkTreeSelection
*selection
;
375 GtkTreeModel
*stash_model
;
378 stash_view
= GTK_TREE_VIEW (gtk_builder_get_object (self
->priv
->builder
,
380 selection
= gtk_tree_view_get_selection (stash_view
);
383 if (gtk_tree_selection_get_selected (selection
, &stash_model
, &iter
))
384 gtk_tree_model_get (stash_model
, &iter
, COL_ID
, &id
, -1);
390 git_stash_pane_get_selected_stash_number (GitStashPane
*self
)
392 GtkTreeView
*stash_view
;
393 GtkTreeSelection
*selection
;
395 GtkTreeModel
*stash_model
;
398 stash_view
= GTK_TREE_VIEW (gtk_builder_get_object (self
->priv
->builder
,
400 selection
= gtk_tree_view_get_selection (stash_view
);
403 if (gtk_tree_selection_get_selected (selection
, &stash_model
, &iter
))
404 gtk_tree_model_get (stash_model
, &iter
, COL_NUMBER
, &number
, -1);