Fix a Gtk warning when checking path input in the log viewer.
[anjuta-git-plugin.git] / plugins / git / git-bisect-dialog.c
blobeec9fee8111ec71f6c513fa74b609e48b7d397b8
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 * anjuta
4 * Copyright (C) James Liggett 2008 <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-bisect-dialog.h"
27 static void
28 on_bisect_start_dialog_response (GtkDialog *dialog, gint response_id,
29 GitUIData *data)
31 GtkWidget *bisect_start_revision_radio;
32 GtkWidget *bisect_start_bad_revision_entry;
33 GtkWidget *bisect_start_good_revision_entry;
34 const gchar *bad_revision;
35 const gchar *good_revision;
36 GitBisectStartCommand *bisect_command;
38 if (response_id == GTK_RESPONSE_OK)
40 bisect_start_revision_radio = glade_xml_get_widget (data->gxml,
41 "bisect_start_revision_radio");
42 bisect_start_bad_revision_entry = glade_xml_get_widget (data->gxml,
43 "bisect_start_bad_revision_entry");
44 bisect_start_good_revision_entry = glade_xml_get_widget (data->gxml,
45 "bisect_start_good_revision_entry");
46 bad_revision = "";
48 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (bisect_start_revision_radio)))
50 bad_revision = gtk_entry_get_text (GTK_ENTRY (bisect_start_bad_revision_entry));
52 if (!git_check_input (GTK_WIDGET (dialog),
53 bisect_start_bad_revision_entry,
54 bad_revision, _("Please enter a revision.")))
56 return;
60 good_revision = gtk_entry_get_text (GTK_ENTRY (bisect_start_good_revision_entry));
62 /* Empty revisions means don't give it to bisect. Use gtk_entry_get_text
63 * so we can re-assign the pointers to NULL without worrying about
64 * leaks. */
65 if (strlen (bad_revision) == 0)
66 bad_revision = NULL;
68 if (strlen (good_revision) == 0)
69 good_revision = NULL;
71 bisect_command = git_bisect_start_command_new (data->plugin->project_root_directory,
72 bad_revision,
73 good_revision);
75 git_create_message_view (data->plugin);
77 g_signal_connect (G_OBJECT (bisect_command), "command-finished",
78 G_CALLBACK (on_git_command_finished),
79 data->plugin);
81 g_signal_connect (G_OBJECT (bisect_command), "data-arrived",
82 G_CALLBACK (on_git_command_info_arrived),
83 data->plugin);
85 anjuta_command_start (ANJUTA_COMMAND (bisect_command));
88 gtk_widget_destroy (GTK_WIDGET (dialog));
89 git_ui_data_free (data);
92 static void
93 on_bisect_start_revision_radio_toggled (GtkToggleButton *toggle_button,
94 GitUIData *data)
96 GtkWidget *bisect_start_dialog;
97 GtkWidget *bisect_start_revision_entry;
98 gboolean active;
100 bisect_start_dialog = glade_xml_get_widget (data->gxml,
101 "bisect_start_dialog");
102 bisect_start_revision_entry = glade_xml_get_widget (data->gxml,
103 "bisect_start_bad_revision_entry");
105 active = gtk_toggle_button_get_active (toggle_button);
106 gtk_widget_set_sensitive (bisect_start_revision_entry, active);
108 if (active)
110 gtk_window_set_focus (GTK_WINDOW (bisect_start_dialog),
111 bisect_start_revision_entry);
116 static void
117 bisect_start_dialog (Git *plugin)
119 GladeXML *gxml;
120 GtkWidget *dialog;
121 GtkWidget *bisect_start_revision_radio;
122 GitUIData *data;
124 gxml = glade_xml_new (GLADE_FILE, "bisect_start_dialog", NULL);
125 dialog = glade_xml_get_widget (gxml, "bisect_start_dialog");
126 bisect_start_revision_radio = glade_xml_get_widget (gxml,
127 "bisect_start_revision_radio");
128 data = git_ui_data_new (plugin, gxml);
130 g_signal_connect (G_OBJECT (dialog), "response",
131 G_CALLBACK (on_bisect_start_dialog_response),
132 data);
134 g_signal_connect (G_OBJECT (bisect_start_revision_radio), "toggled",
135 G_CALLBACK (on_bisect_start_revision_radio_toggled),
136 data);
138 gtk_widget_show_all (dialog);
141 static void
142 bisect_reset (Git *plugin)
144 GitBisectResetCommand *bisect_command;
146 bisect_command = git_bisect_reset_command_new (plugin->project_root_directory);
148 git_create_message_view (plugin);
150 g_signal_connect (G_OBJECT (bisect_command), "command-finished",
151 G_CALLBACK (on_git_command_finished),
152 plugin);
154 g_signal_connect (G_OBJECT (bisect_command), "data-arrived",
155 G_CALLBACK (on_git_command_info_arrived),
156 plugin);
158 anjuta_command_start (ANJUTA_COMMAND (bisect_command));
161 static void
162 bisect_state (Git *plugin, GitBisectState state, const gchar *revision)
164 GitBisectStateCommand *bisect_command;
166 bisect_command = git_bisect_state_command_new (plugin->project_root_directory,
167 state, revision);
169 git_create_message_view (plugin);
171 g_signal_connect (G_OBJECT (bisect_command), "command-finished",
172 G_CALLBACK (on_git_command_finished),
173 plugin);
175 g_signal_connect (G_OBJECT (bisect_command), "data-arrived",
176 G_CALLBACK (on_git_command_info_arrived),
177 plugin);
179 anjuta_command_start (ANJUTA_COMMAND (bisect_command));
182 static void
183 update_bisect_menus (AnjutaUI *ui, gboolean in_bisect)
185 GtkAction *bisect_start_action;
186 GtkAction *bisect_reset_action;
187 GtkAction *bisect_good_action;
188 GtkAction *bisect_bad_action;
189 GtkAction *bisect_log_menu_action;
191 bisect_start_action = anjuta_ui_get_action (ui, "ActionGroupGit",
192 "ActionGitBisectStart");
193 bisect_reset_action = anjuta_ui_get_action (ui, "ActionGroupGit",
194 "ActionGitBisectReset");
195 bisect_good_action = anjuta_ui_get_action (ui, "ActionGroupGit",
196 "ActionGitBisectGood");
197 bisect_bad_action = anjuta_ui_get_action (ui, "ActionGroupGit",
198 "ActionGitBisectBad");
199 bisect_log_menu_action = anjuta_ui_get_action (ui, "ActionGroupGitLog",
200 "ActionMenuGitLogBisect");
202 if (in_bisect)
204 gtk_action_set_sensitive (bisect_start_action, FALSE);
205 gtk_action_set_sensitive (bisect_reset_action, TRUE);
206 gtk_action_set_sensitive (bisect_good_action, TRUE);
207 gtk_action_set_sensitive (bisect_bad_action, TRUE);
208 gtk_action_set_sensitive (bisect_log_menu_action, TRUE);
210 else
212 gtk_action_set_sensitive (bisect_start_action, TRUE);
213 gtk_action_set_sensitive (bisect_reset_action, FALSE);
214 gtk_action_set_sensitive (bisect_good_action, FALSE);
215 gtk_action_set_sensitive (bisect_bad_action, FALSE);
216 gtk_action_set_sensitive (bisect_log_menu_action, FALSE);
220 static void
221 on_bisect_file_monitor_changed (GFileMonitor *file_monitor, GFile *file,
222 GFile *other_file, GFileMonitorEvent event_type,
223 AnjutaUI *ui)
225 switch (event_type)
227 case G_FILE_MONITOR_EVENT_CREATED:
228 /* This indicates that a bisect has started */
229 update_bisect_menus (ui, TRUE);
230 break;
231 case G_FILE_MONITOR_EVENT_DELETED:
232 /* This indicates that a bisect has started */
233 update_bisect_menus (ui, FALSE);
234 break;
235 default:
236 break;
241 void
242 on_menu_git_bisect_start (GtkAction *action, Git *plugin)
244 bisect_start_dialog (plugin);
247 void
248 on_menu_git_bisect_reset (GtkAction *action, Git *plugin)
250 bisect_reset (plugin);
253 void
254 on_menu_git_bisect_good (GtkAction *action, Git *plugin)
256 bisect_state (plugin, GIT_BISECT_STATE_GOOD, NULL);
259 void
260 on_menu_git_bisect_bad (GtkAction *action, Git *plugin)
262 bisect_state (plugin, GIT_BISECT_STATE_BAD, NULL);
265 void
266 on_log_menu_git_bisect_good (GtkAction *action, Git *plugin)
268 GitRevision *revision;
269 gchar *sha;
271 revision = git_log_get_selected_revision (plugin);
273 if (revision)
275 sha = git_revision_get_sha (revision);
277 bisect_state (plugin, GIT_BISECT_STATE_GOOD, sha);
278 g_free (sha);
282 void
283 on_log_menu_git_bisect_bad (GtkAction *action, Git *plugin)
285 GitRevision *revision;
286 gchar *sha;
288 revision = git_log_get_selected_revision (plugin);
290 if (revision)
292 sha = git_revision_get_sha (revision);
294 bisect_state (plugin, GIT_BISECT_STATE_BAD, sha);
295 g_free (sha);
299 GFileMonitor *
300 bisect_menus_init (Git *plugin)
302 AnjutaUI *ui;
303 gchar *bisect_log_path;
304 GFile *bisect_log_file;
305 GFileMonitor *bisect_file_monitor;
307 ui = anjuta_shell_get_ui (ANJUTA_PLUGIN (plugin)->shell, NULL);
309 /* Git keeps a bisect log file in a project's top-level .git directory.
310 * The presence of this file indicates that a bisect is in progress.
311 * We'll monitor the creation/deletion of this file to detect the
312 * occurrence of new bisect operations and the conclusions of existing ones
313 * and update the UI accordingly. */
314 bisect_log_path = g_strjoin (G_DIR_SEPARATOR_S,
315 plugin->project_root_directory,
316 ".git",
317 "BISECT_LOG",
318 NULL);
320 update_bisect_menus (ui, g_file_test (bisect_log_path, G_FILE_TEST_EXISTS));
322 bisect_log_file = g_file_new_for_path (bisect_log_path);
323 bisect_file_monitor = g_file_monitor_file (bisect_log_file, 0, NULL, NULL);
325 g_signal_connect (G_OBJECT (bisect_file_monitor), "changed",
326 G_CALLBACK (on_bisect_file_monitor_changed),
327 ui);
329 g_object_unref (bisect_log_file);
330 g_free (bisect_log_path);
332 return bisect_file_monitor;