Add progress handling to GitCommand
[anjuta-git-plugin.git] / plugins / git / git-ui-utils.c
blobe9951bd87ef4305a57d19b08f29aac552c7f1f77
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 * anjuta
4 * Copyright (C) James Liggett 2007 <jrliggett@cox.net>
5 *
6 * anjuta is free software.
7 *
8 * You may redistribute it and/or modify it under the terms of the
9 * GNU General Public License, as published by the Free Software
10 * Foundation; either version 2 of the License, or (at your option)
11 * any later version.
13 * anjuta is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 * See the GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with anjuta. If not, write to:
20 * The Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02110-1301, USA.
25 #include "git-ui-utils.h"
27 /* Private structure for pulse progress */
28 typedef struct
30 AnjutaStatus *status;
31 gchar *text;
32 } PulseProgressData;
34 GitUIData*
35 git_ui_data_new (Git* plugin, GladeXML* gxml)
37 GitUIData* data = g_new0 (GitUIData, 1);
38 data->plugin = plugin;
39 data->gxml = gxml;
41 return data;
44 void
45 git_ui_data_free (GitUIData* data)
47 g_object_unref (data->gxml);
48 g_free (data);
51 GitProgressData *
52 git_progress_data_new (Git *plugin, const gchar *text)
54 GitProgressData *data;
55 AnjutaStatus *status;
57 data = g_new0 (GitProgressData, 1);
58 data->plugin = plugin;
59 data->text = g_strdup (text);
61 status = anjuta_shell_get_status (ANJUTA_PLUGIN (plugin)->shell, NULL);
62 anjuta_status_progress_add_ticks (status, 100);
64 return data;
67 void
68 git_progress_data_free (GitProgressData *data)
70 AnjutaStatus *status;
72 status = anjuta_shell_get_status (ANJUTA_PLUGIN (data->plugin)->shell,
73 NULL);
75 g_free (data->text);
76 g_free (data);
79 static void
80 on_message_view_destroy (Git* plugin, gpointer destroyed_view)
82 plugin->message_view = NULL;
85 void
86 create_message_view (Git* plugin)
88 IAnjutaMessageManager* message_manager;
91 message_manager = anjuta_shell_get_interface (ANJUTA_PLUGIN (plugin)->shell,
92 IAnjutaMessageManager, NULL);
93 plugin->message_view = ianjuta_message_manager_get_view_by_name (message_manager,
94 _("Git"),
95 NULL);
96 if (!plugin->message_view)
98 plugin->message_view = ianjuta_message_manager_add_view (message_manager,
99 _("Git"),
100 ICON_FILE,
101 NULL);
102 g_object_weak_ref (G_OBJECT (plugin->message_view),
103 (GWeakNotify) on_message_view_destroy, plugin);
106 ianjuta_message_view_clear(plugin->message_view, NULL);
107 ianjuta_message_manager_set_current_view (message_manager, plugin->message_view,
108 NULL);
111 gboolean
112 check_input (GtkWidget *parent, GtkWidget *widget, const gchar *input,
113 const gchar *error_message)
115 gboolean ret;
116 GtkWidget *dialog;
118 ret = FALSE;
120 if (input)
122 if (strlen (input) > 0)
123 ret = TRUE;
126 if (!ret)
128 dialog = gtk_message_dialog_new (GTK_WINDOW (parent),
129 GTK_DIALOG_DESTROY_WITH_PARENT,
130 GTK_MESSAGE_WARNING,
131 GTK_BUTTONS_OK,
132 error_message);
134 gtk_dialog_run (GTK_DIALOG (dialog));
135 gtk_widget_destroy (dialog);
137 gtk_window_set_focus (GTK_WINDOW (parent), widget);
141 return ret;
144 gchar *
145 get_log_from_textview (GtkWidget* textview)
147 gchar* log;
148 GtkTextBuffer* textbuf;
149 GtkTextIter iterbegin, iterend;
151 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview));
152 gtk_text_buffer_get_start_iter(textbuf, &iterbegin);
153 gtk_text_buffer_get_end_iter(textbuf, &iterend) ;
154 log = gtk_text_buffer_get_text(textbuf, &iterbegin, &iterend, FALSE);
155 return log;
158 static gboolean
159 status_pulse_timer (PulseProgressData *data)
161 anjuta_status_progress_pulse (data->status, data->text);
162 return TRUE;
165 static gboolean
166 pulse_timer (GtkProgressBar *progress_bar)
168 gtk_progress_bar_pulse (progress_bar);
169 return TRUE;
172 static void
173 on_pulse_timer_destroyed (PulseProgressData *data)
175 anjuta_status_progress_reset (data->status);
177 g_free (data->text);
178 g_free (data);
181 guint
182 status_bar_progress_pulse (Git *plugin, gchar *text)
184 PulseProgressData *data;
186 data = g_new0 (PulseProgressData, 1);
187 data->status = anjuta_shell_get_status (ANJUTA_PLUGIN (plugin)->shell,
188 NULL);
189 data->text = g_strdup (text);
191 return g_timeout_add_full (G_PRIORITY_DEFAULT, 100,
192 (GSourceFunc) status_pulse_timer, data,
193 (GDestroyNotify) on_pulse_timer_destroyed);
196 void
197 clear_status_bar_progress_pulse (guint timer_id)
199 g_source_remove (timer_id);
202 void
203 report_errors (AnjutaCommand *command, guint return_code)
205 gchar *message;
207 /* In some cases, git might report errors yet still indicate success.
208 * When this happens, use a warning dialog instead of an error, so the user
209 * knows that something actually happened. */
210 message = anjuta_command_get_error_message (command);
212 if (message)
214 if (return_code != 0)
215 anjuta_util_dialog_error (NULL, message);
216 else
217 anjuta_util_dialog_warning (NULL, message);
219 g_free (message);
223 static void
224 stop_pulse_timer (gpointer timer_id, GtkProgressBar *progress_bar)
226 g_source_remove (GPOINTER_TO_UINT (timer_id));
229 void
230 pulse_progress_bar (GtkProgressBar *progress_bar)
232 guint timer_id;
234 timer_id = g_timeout_add (100, (GSourceFunc) pulse_timer,
235 progress_bar);
236 g_object_set_data (G_OBJECT (progress_bar), "pulse-timer-id",
237 GUINT_TO_POINTER (timer_id));
239 g_object_weak_ref (G_OBJECT (progress_bar),
240 (GWeakNotify) stop_pulse_timer,
241 GUINT_TO_POINTER (timer_id));
244 gchar *
245 get_filename_from_full_path (gchar *path)
247 gchar *last_slash;
249 last_slash = strrchr (path, '/');
251 /* There might be a trailing slash in the string */
252 if ((last_slash - path) < strlen (path))
253 return g_strdup (last_slash + 1);
254 else
255 return g_strdup ("");
258 const gchar *
259 get_relative_path (const gchar *path, const gchar *working_directory)
261 /* Path could already be a relative path */
262 if (strstr (path, working_directory))
263 return path + strlen (working_directory) + 1;
264 else
265 return path;
268 void
269 on_command_finished (AnjutaCommand *command, guint return_code,
270 gpointer user_data)
272 report_errors (command, return_code);
274 g_object_unref (command);
277 void
278 on_status_command_data_arrived (AnjutaCommand *command,
279 AnjutaVcsStatusTreeView *tree_view)
281 GQueue *status_queue;
282 GitStatus *status;
283 gchar *path;
285 status_queue = git_status_command_get_status_queue (GIT_STATUS_COMMAND (command));
287 while (g_queue_peek_head (status_queue))
289 status = g_queue_pop_head (status_queue);
290 path = git_status_get_path (status);
292 anjuta_vcs_status_tree_view_add (tree_view, path,
293 git_status_get_vcs_status (status),
294 FALSE);
296 g_object_unref (status);
297 g_free (path);
301 void
302 on_command_info_arrived (AnjutaCommand *command, Git *plugin)
304 GQueue *info;
305 gchar *message;
307 info = git_command_get_info_queue (GIT_COMMAND (command));
309 while (g_queue_peek_head (info))
311 message = g_queue_pop_head (info);
312 ianjuta_message_view_append (plugin->message_view,
313 IANJUTA_MESSAGE_VIEW_TYPE_INFO,
314 message, "", NULL);
315 g_free (message);
319 void
320 on_command_progress (AnjutaCommand *command, gfloat progress,
321 GitProgressData *data)
323 AnjutaStatus *status;
324 gint ticks;
326 status = anjuta_shell_get_status (ANJUTA_PLUGIN (data->plugin)->shell,
327 NULL);
329 /* There are cases where there are multiple stages to a task, and each
330 * has their own progress indicator. If one stage has completed and another
331 * is beginning, add another 100 ticks to reflect the progress of this new
332 * stage. */
333 if (data->last_progress == 100)
335 anjuta_status_progress_add_ticks (status, 100);
336 data->last_progress = 0;
339 ticks = progress * 100;
340 anjuta_status_progress_increment_ticks (status,
341 (ticks - data->last_progress),
342 data->text);
343 data->last_progress = ticks;
346 void
347 on_list_branch_command_data_arrived (AnjutaCommand *command,
348 GitBranchComboData *data)
350 GQueue *output_queue;
351 GitBranch *branch;
353 output_queue = git_branch_list_command_get_output (GIT_BRANCH_LIST_COMMAND (command));
355 while (g_queue_peek_head (output_queue))
357 branch = g_queue_pop_head (output_queue);
358 git_branch_combo_model_append (data->model, branch);
359 g_object_unref (branch);
363 void
364 on_list_branch_command_finished (AnjutaCommand *command, guint return_code,
365 GitBranchComboData *data)
367 gtk_combo_box_set_active (data->combo_box, 0);
369 report_errors (command, return_code);
370 g_object_unref (command);
373 void
374 select_all_status_items (GtkButton *select_all_button,
375 AnjutaVcsStatusTreeView *tree_view)
377 anjuta_vcs_status_tree_view_select_all (tree_view);
380 void
381 clear_all_status_selections (GtkButton *clear_button,
382 AnjutaVcsStatusTreeView *tree_view)
384 anjuta_vcs_status_tree_view_unselect_all (tree_view);
387 void
388 init_whole_project (Git *plugin, GtkWidget* project, gboolean active)
390 gboolean project_loaded;
392 project_loaded = (plugin->project_root_directory != NULL);
394 gtk_widget_set_sensitive(project, project_loaded);
396 if (project_loaded)
397 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (project), active);
400 void
401 on_whole_project_toggled (GtkToggleButton* project, Git *plugin)
403 GtkWidget *path_entry;
405 path_entry = g_object_get_data (G_OBJECT (project), "file-entry");
407 gtk_widget_set_sensitive (path_entry,
408 !gtk_toggle_button_get_active (project));
411 void
412 send_raw_command_output_to_editor (AnjutaCommand *command,
413 IAnjutaEditor *editor)
415 GQueue *output;
416 gchar *line;
418 output = git_raw_output_command_get_output (GIT_RAW_OUTPUT_COMMAND (command));
420 while (g_queue_peek_head (output))
422 line = g_queue_pop_head (output);
423 ianjuta_editor_append (editor, line, strlen (line), NULL);
424 g_free (line);
428 void
429 on_diff_command_finished (AnjutaCommand *command, guint return_code,
430 Git *plugin)
432 AnjutaStatus *status;
434 status = anjuta_shell_get_status (ANJUTA_PLUGIN (plugin)->shell,
435 NULL);
437 anjuta_status (status, _("Git: Diff complete."), 5);
439 report_errors (command, return_code);
441 g_object_unref (command);
444 void
445 stop_status_bar_progress_pulse (AnjutaCommand *command, guint return_code,
446 gpointer timer_id)
448 clear_status_bar_progress_pulse (GPOINTER_TO_UINT (timer_id));
451 void
452 hide_pulse_progress_bar (AnjutaCommand *command, guint return_code,
453 GtkProgressBar *progress_bar)
455 guint timer_id;
457 /* If the progress bar has already been destroyed, the timer should be
458 * stopped by stop_pulse_timer */
459 if (GTK_IS_PROGRESS_BAR (progress_bar))
461 timer_id = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (progress_bar),
462 "pulse-timer-id"));
464 g_source_remove (GPOINTER_TO_UINT (timer_id));
465 gtk_widget_hide (GTK_WIDGET (progress_bar));
469 /* This function is normally intended to disconnect stock data-arrived signal
470 * handlers in this file. It is assumed that object is the user data for the
471 * callback. If you use any of the stock callbacks defined here, make sure
472 * to weak ref its target with this callback. Make sure to cancel this ref
473 * by connecting cancel_data_arrived_signal_disconnect to the command-finished
474 * signal so we don't try to disconnect signals on a destroyed command. */
475 void
476 disconnect_data_arrived_signals (AnjutaCommand *command, GObject *object)
478 guint data_arrived_signal;
480 if (ANJUTA_IS_COMMAND (command))
482 data_arrived_signal = g_signal_lookup ("data-arrived",
483 ANJUTA_TYPE_COMMAND);
485 g_signal_handlers_disconnect_matched (command,
486 G_SIGNAL_MATCH_DATA,
487 data_arrived_signal,
489 NULL,
490 NULL,
491 object);
496 void
497 cancel_data_arrived_signal_disconnect (AnjutaCommand *command,
498 guint return_code,
499 GObject *signal_target)
501 g_object_weak_unref (signal_target,
502 (GWeakNotify) disconnect_data_arrived_signals,
503 command);