Implemented basic UI utility functions.
[anjuta-git-plugin.git] / plugins / git / git-ui-utils.c
blobfbbf20fea72ce682a8e79d4d5882ef594f11a831
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_free (data);
50 static void
51 on_message_view_destroy (Git* plugin, gpointer destroyed_view)
53 plugin->message_view = NULL;
56 void
57 create_message_view (Git* plugin)
59 IAnjutaMessageManager* message_manager;
62 message_manager = anjuta_shell_get_interface (ANJUTA_PLUGIN (plugin)->shell,
63 IAnjutaMessageManager, NULL);
64 plugin->message_view = ianjuta_message_manager_get_view_by_name (message_manager,
65 _("Git"),
66 NULL);
67 if (!plugin->message_view)
69 plugin->message_view = ianjuta_message_manager_add_view (message_manager,
70 _("Git"),
71 ICON_FILE,
72 NULL);
73 g_object_weak_ref (G_OBJECT (plugin->message_view),
74 (GWeakNotify) on_message_view_destroy, plugin);
77 ianjuta_message_view_clear(plugin->message_view, NULL);
78 ianjuta_message_manager_set_current_view (message_manager, plugin->message_view,
79 NULL);
82 gboolean
83 check_input (GtkWidget *parent, GtkWidget *entry, const gchar *error_message)
85 gchar *input;
86 gboolean ret;
87 GtkWidget *dialog;
89 input = gtk_editable_get_chars (GTK_EDITABLE (entry), 0, -1);
91 if (strlen (input) > 0)
92 ret = TRUE;
93 else
95 dialog = gtk_message_dialog_new (GTK_WINDOW (parent),
96 GTK_DIALOG_DESTROY_WITH_PARENT,
97 GTK_MESSAGE_WARNING,
98 GTK_BUTTONS_OK,
99 error_message);
101 gtk_dialog_run (GTK_DIALOG (dialog));
102 gtk_widget_destroy (dialog);
104 gtk_window_set_focus (GTK_WINDOW (parent), entry);
106 ret = FALSE;
109 g_free (input);
111 return ret;
114 gchar *
115 get_log_from_textview (GtkWidget* textview)
117 gchar* log;
118 GtkTextBuffer* textbuf;
119 GtkTextIter iterbegin, iterend;
120 gchar* escaped_log;
122 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview));
123 gtk_text_buffer_get_start_iter(textbuf, &iterbegin);
124 gtk_text_buffer_get_end_iter(textbuf, &iterend) ;
125 log = gtk_text_buffer_get_text(textbuf, &iterbegin, &iterend, FALSE);
126 escaped_log = anjuta_util_escape_quotes (log);
127 return escaped_log;
130 static gboolean
131 status_pulse_timer (PulseProgressData *data)
133 anjuta_status_progress_pulse (data->status, data->text);
134 return TRUE;
137 static gboolean
138 pulse_timer (GtkProgressBar *progress_bar)
140 gtk_progress_bar_pulse (progress_bar);
141 return TRUE;
144 static void
145 on_pulse_timer_destroyed (PulseProgressData *data)
147 anjuta_status_progress_reset (data->status);
149 g_free (data->text);
150 g_free (data);
153 guint
154 status_bar_progress_pulse (Git *plugin, gchar *text)
156 PulseProgressData *data;
158 data = g_new0 (PulseProgressData, 1);
159 data->status = anjuta_shell_get_status (ANJUTA_PLUGIN (plugin)->shell,
160 NULL);
161 data->text = g_strdup (text);
163 return g_timeout_add_full (G_PRIORITY_DEFAULT, 100,
164 (GSourceFunc) status_pulse_timer, data,
165 (GDestroyNotify) on_pulse_timer_destroyed);
168 void
169 clear_status_bar_progress_pulse (guint timer_id)
171 g_source_remove (timer_id);
174 void
175 report_errors (AnjutaCommand *command, guint return_code)
177 gchar *message;
179 if (return_code != 0)
181 message = anjuta_command_get_error_message (command);
183 anjuta_util_dialog_error (NULL, message);
184 g_free (message);
188 static void
189 stop_pulse_timer (gpointer timer_id, GtkProgressBar *progress_bar)
191 g_source_remove (GPOINTER_TO_UINT (timer_id));
194 void
195 pulse_progress_bar (GtkProgressBar *progress_bar)
197 guint timer_id;
199 timer_id = g_timeout_add (100, (GSourceFunc) pulse_timer,
200 progress_bar);
201 g_object_set_data (G_OBJECT (progress_bar), "pulse-timer-id",
202 GUINT_TO_POINTER (timer_id));
204 g_object_weak_ref (G_OBJECT (progress_bar),
205 (GWeakNotify) stop_pulse_timer,
206 GUINT_TO_POINTER (timer_id));
209 gchar *
210 get_filename_from_full_path (gchar *path)
212 gchar *last_slash;
214 last_slash = strrchr (path, '/');
216 /* There might be a trailing slash in the string */
217 if ((last_slash - path) < strlen (path))
218 return g_strdup (last_slash + 1);
219 else
220 return g_strdup ("");
223 #if 0
224 void
225 on_status_command_finished (AnjutaCommand *command, guint return_code,
226 gpointer user_data)
228 report_errors (command, return_code);
230 git_status_command_destroy (GIT_STATUS_COMMAND (command));
233 void
234 on_status_command_data_arrived (AnjutaCommand *command,
235 AnjutaVcsStatusTreeView *tree_view)
237 GQueue *status_queue;
238 GitStatusStatus *status;
239 gchar *path;
241 status_queue = git_status_command_get_status_queue (GIT_STATUS_COMMAND (command));
243 while (g_queue_peek_head (status_queue))
245 status = g_queue_pop_head (status_queue);
246 path = git_status_get_path (status);
248 anjuta_vcs_status_tree_view_add (tree_view, path,
249 git_status_get_vcs_status (status),
250 FALSE);
252 git_status_destroy (status);
253 g_free (path);
256 #endif
258 void
259 on_command_info_arrived (AnjutaCommand *command, Git *plugin)
261 GQueue *info;
262 gchar *message;
264 info = git_command_get_info_queue (GIT_COMMAND (command));
266 while (g_queue_peek_head (info))
268 message = g_queue_pop_head (info);
269 ianjuta_message_view_append (plugin->message_view,
270 IANJUTA_MESSAGE_VIEW_TYPE_INFO,
271 message, "", NULL);
272 g_free (message);
276 void
277 select_all_status_items (GtkButton *select_all_button,
278 AnjutaVcsStatusTreeView *tree_view)
280 anjuta_vcs_status_tree_view_select_all (tree_view);
283 void
284 clear_all_status_selections (GtkButton *clear_button,
285 AnjutaVcsStatusTreeView *tree_view)
287 anjuta_vcs_status_tree_view_unselect_all (tree_view);
290 void
291 init_whole_project (Git *plugin, GtkWidget* project, gboolean active)
293 gboolean project_loaded;
295 project_loaded = (plugin->project_root_directory != NULL);
297 gtk_widget_set_sensitive(project, project_loaded);
299 if (project_loaded)
300 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (project), active);
303 void
304 on_whole_project_toggled (GtkToggleButton* project, Git *plugin)
306 GtkEntry* file_entry;
308 file_entry = g_object_get_data (G_OBJECT (project), "file_entry");
310 if (gtk_toggle_button_get_active(project) && plugin->project_root_directory)
312 gtk_entry_set_text (file_entry, plugin->project_root_directory);
313 gtk_widget_set_sensitive(GTK_WIDGET(file_entry), FALSE);
315 else
316 gtk_widget_set_sensitive(GTK_WIDGET(file_entry), TRUE);
319 #if 0
320 void
321 send_diff_command_output_to_editor (AnjutaCommand *command,
322 IAnjutaEditor *editor)
324 GQueue *output;
325 gchar *line;
327 output = git_diff_command_get_output (GIT_DIFF_COMMAND (command));
329 while (g_queue_peek_head (output))
331 line = g_queue_pop_head (output);
332 ianjuta_editor_append (editor, line, strlen (line), NULL);
333 g_free (line);
337 void
338 on_diff_command_finished (AnjutaCommand *command, guint return_code,
339 Git *plugin)
341 AnjutaStatus *status;
343 status = anjuta_shell_get_status (ANJUTA_PLUGIN (plugin)->shell,
344 NULL);
346 anjuta_status (status, _("Git: Diff complete."), 5);
348 report_errors (command, return_code);
350 git_diff_command_destroy (GIT_DIFF_COMMAND (command));
352 #endif
354 void
355 stop_status_bar_progress_pulse (AnjutaCommand *command, guint return_code,
356 gpointer timer_id)
358 clear_status_bar_progress_pulse (GPOINTER_TO_UINT (timer_id));
361 void
362 hide_pulse_progress_bar (AnjutaCommand *command, guint return_code,
363 GtkProgressBar *progress_bar)
365 guint timer_id;
367 /* If the progress bar has already been destroyed, the timer should be
368 * stopped by stop_pulse_timer */
369 if (GTK_IS_PROGRESS_BAR (progress_bar))
371 timer_id = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (progress_bar),
372 "pulse-timer-id"));
374 g_source_remove (GPOINTER_TO_UINT (timer_id));
375 gtk_widget_hide (GTK_WIDGET (progress_bar));
379 /* This function is normally intended to disconnect stock data-arrived signal
380 * handlers in this file. It is assumed that object is the user data for the
381 * callback. If you use any of the stock callbacks defined here, make sure
382 * to weak ref its target with this callback. Make sure to cancel this ref
383 * by connecting cancel_data_arrived_signal_disconnect to the command-finished
384 * signal so we don't try to disconnect signals on a destroyed command. */
385 void
386 disconnect_data_arrived_signals (AnjutaCommand *command, GObject *object)
388 guint data_arrived_signal;
390 if (ANJUTA_IS_COMMAND (command))
392 data_arrived_signal = g_signal_lookup ("data-arrived",
393 ANJUTA_TYPE_COMMAND);
395 g_signal_handlers_disconnect_matched (command,
396 G_SIGNAL_MATCH_DATA,
397 data_arrived_signal,
399 NULL,
400 NULL,
401 object);
406 void
407 cancel_data_arrived_signal_disconnect (AnjutaCommand *command,
408 guint return_code,
409 GObject *signal_target)
411 g_object_weak_unref (signal_target,
412 (GWeakNotify) disconnect_data_arrived_signals,
413 command);