Implement showing diffs of commits.
[anjuta-git-plugin.git] / plugins / git / plugin.c
blob34af35d6f3ab2faf27142a894412c3d165645506
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 plugin.c
4 Copyright (C) James Liggett 2008
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301 USA
22 #include "plugin.h"
23 #include "git-diff-dialog.h"
24 #include "git-commit-dialog.h"
25 #include "git-add-dialog.h"
26 #include "git-remove-dialog.h"
27 #include "git-resolve-dialog.h"
28 #include "git-merge-dialog.h"
29 #include "git-switch-dialog.h"
30 #include "git-create-branch-dialog.h"
31 #include "git-delete-branch-dialog.h"
32 #include "git-unstage-dialog.h"
33 #include "git-checkout-files-dialog.h"
34 #include "git-log-dialog.h"
36 #define UI_FILE PACKAGE_DATA_DIR"/ui/anjuta-git.ui"
38 static gpointer parent_class;
40 static GtkActionEntry actions_git[] =
43 "ActionMenuGit", /* Action name */
44 NULL, /* Stock icon, if any */
45 N_("_Git"), /* Display label */
46 NULL, /* short-cut */
47 NULL, /* Tooltip */
48 NULL /* action callback */
51 "ActionGitCommit", /* Action name */
52 GTK_STOCK_YES, /* Stock icon, if any */
53 N_("_Commit..."), /* Display label */
54 NULL, /* short-cut */
55 NULL, /* Tooltip */
56 G_CALLBACK (on_menu_git_commit) /* action callback */
59 "ActionGitUnstageFiles", /* Action name */
60 GTK_STOCK_CANCEL, /* Stock icon, if any */
61 N_("_Unstage files..."), /* Display label */
62 NULL, /* short-cut */
63 NULL, /* Tooltip */
64 G_CALLBACK (on_menu_git_unstage) /* action callback */
67 "ActionGitCheckoutFiles", /* Action name */
68 GTK_STOCK_UNDO, /* Stock icon, if any */
69 N_("_Check out files..."), /* Display label */
70 NULL, /* short-cut */
71 NULL, /* Tooltip */
72 G_CALLBACK (on_menu_git_checkout_files) /* action callback */
75 "ActionGitResolve", /* Action name */
76 GTK_STOCK_PREFERENCES, /* Stock icon, if any */
77 N_("_Resolve conflicts..."), /* Display label */
78 NULL, /* short-cut */
79 NULL, /* Tooltip */
80 G_CALLBACK (on_menu_git_resolve) /* action callback */
83 "ActionGitLog", /* Action name */
84 GTK_STOCK_ZOOM_100, /* Stock icon, if any */
85 N_("_View log..."), /* Display label */
86 NULL, /* short-cut */
87 NULL, /* Tooltip */
88 G_CALLBACK (on_menu_git_log) /* action callback */
91 "ActionGitAdd", /* Action name */
92 GTK_STOCK_ADD, /* Stock icon, if any */
93 N_("_Add..."), /* Display label */
94 NULL, /* short-cut */
95 NULL, /* Tooltip */
96 G_CALLBACK (on_menu_git_add) /* action callback */
99 "ActionGitRemove", /* Action name */
100 GTK_STOCK_REMOVE, /* Stock icon, if any */
101 N_("_Remove..."), /* Display label */
102 NULL, /* short-cut */
103 NULL, /* Tooltip */
104 G_CALLBACK (on_menu_git_remove) /* action callback */
107 "ActionGitCreateBranch", /* Action name */
108 GTK_STOCK_NEW, /* Stock icon, if any */
109 N_("_Create branch..."), /* Display label */
110 NULL, /* short-cut */
111 NULL, /* Tooltip */
112 G_CALLBACK (on_menu_git_create_branch) /* action callback */
115 "ActionGitDeleteBranch", /* Action name */
116 GTK_STOCK_DELETE, /* Stock icon, if any */
117 N_("_Delete branch..."), /* Display label */
118 NULL, /* short-cut */
119 NULL, /* Tooltip */
120 G_CALLBACK (on_menu_git_delete_branch) /* action callback */
123 "ActionGitSwitch", /* Action name */
124 GTK_STOCK_JUMP_TO, /* Stock icon, if any */
125 N_("_Switch to another branch..."), /* Display label */
126 NULL, /* short-cut */
127 NULL, /* Tooltip */
128 G_CALLBACK (on_menu_git_switch) /* action callback */
131 "ActionGitMerge", /* Action name */
132 GTK_STOCK_CONVERT, /* Stock icon, if any */
133 N_("_Merge..."), /* Display label */
134 NULL, /* short-cut */
135 NULL, /* Tooltip */
136 G_CALLBACK (on_menu_git_merge) /* action callback */
139 "ActionGitDiffUncommitted", /* Action name */
140 GTK_STOCK_ZOOM_100, /* Stock icon, if any */
141 N_("_Diff uncommitted changes"), /* Display label */
142 NULL, /* short-cut */
143 NULL, /* Tooltip */
144 G_CALLBACK (on_menu_git_diff) /* action callback */
148 static GtkActionEntry actions_log[] =
151 "ActionGitLogCommitDiff", /* Action name */
152 GTK_STOCK_ZOOM_100, /* Stock icon, if any */
153 N_("_Show commit diff"), /* Display label */
154 NULL, /* short-cut */
155 NULL, /* Tooltip */
156 G_CALLBACK (on_log_menu_git_commit_diff) /* action callback */
160 static void
161 on_project_root_added (AnjutaPlugin *plugin, const gchar *name,
162 const GValue *value, gpointer user_data)
164 Git *git_plugin;
165 gchar *project_root_uri;
166 GtkAction *git_menu_action;
168 git_plugin = ANJUTA_PLUGIN_GIT (plugin);
170 g_free (git_plugin->project_root_directory);
171 project_root_uri = g_value_dup_string (value);
172 git_plugin->project_root_directory = gnome_vfs_get_local_path_from_uri (project_root_uri);
174 git_menu_action = anjuta_ui_get_action (anjuta_shell_get_ui (plugin->shell,
175 NULL),
176 "ActionGroupGit",
177 "ActionMenuGit");
179 gtk_action_set_sensitive (git_menu_action, TRUE);
180 gtk_widget_set_sensitive (git_plugin->log_viewer, TRUE);
181 git_log_set_whole_project_sensitive (git_plugin, TRUE);
183 g_free (project_root_uri);
186 static void
187 on_project_root_removed (AnjutaPlugin *plugin, const gchar *name,
188 gpointer user_data)
190 GtkAction *git_menu_action;
191 Git *git_plugin;
193 git_plugin = ANJUTA_PLUGIN_GIT (plugin);
195 g_free (git_plugin->project_root_directory);
196 git_plugin->project_root_directory = NULL;
198 git_menu_action = anjuta_ui_get_action (anjuta_shell_get_ui (plugin->shell,
199 NULL),
200 "ActionGroupGit",
201 "ActionMenuGit");
203 gtk_action_set_sensitive (git_menu_action, FALSE);
204 gtk_widget_set_sensitive (git_plugin->log_viewer, FALSE);
205 git_log_window_clear (git_plugin);
208 static void
209 on_editor_added (AnjutaPlugin *plugin, const gchar *name, const GValue *value,
210 gpointer user_data)
212 Git *git_plugin;
213 gchar *current_editor_uri;
214 IAnjutaEditor *editor;
216 git_plugin = ANJUTA_PLUGIN_GIT (plugin);
217 editor = g_value_get_object (value);
219 if (IANJUTA_IS_EDITOR (editor))
221 g_free (git_plugin->current_editor_filename);
222 current_editor_uri = ianjuta_file_get_uri (IANJUTA_FILE (editor), NULL);
223 git_plugin->current_editor_filename = gnome_vfs_get_local_path_from_uri (current_editor_uri);
227 static void
228 on_editor_removed (AnjutaPlugin *plugin, const gchar *name, gpointer user_data)
230 Git *git_plugin;
232 git_plugin = ANJUTA_PLUGIN_GIT (plugin);
234 g_free (git_plugin->current_editor_filename);
235 git_plugin->current_editor_filename = NULL;
238 static gboolean
239 git_activate_plugin (AnjutaPlugin *plugin)
241 AnjutaUI *ui;
242 Git *git_plugin;
243 GtkAction *git_menu_action;
245 DEBUG_PRINT ("Git: Activating Git plugin ...");
247 git_plugin = ANJUTA_PLUGIN_GIT (plugin);
248 ui = anjuta_shell_get_ui (plugin->shell, NULL);
250 /* Add all our actions */
251 anjuta_ui_add_action_group_entries (ui, "ActionGroupGit",
252 _("Git operations"),
253 actions_git,
254 G_N_ELEMENTS (actions_git),
255 GETTEXT_PACKAGE, TRUE, plugin);
256 anjuta_ui_add_action_group_entries (ui, "ActionGroupGitLog",
257 _("Git log operations"),
258 actions_log,
259 G_N_ELEMENTS (actions_log),
260 GETTEXT_PACKAGE, TRUE, plugin);
262 git_plugin->uiid = anjuta_ui_merge (ui, UI_FILE);
264 git_plugin->log_popup_menu = gtk_ui_manager_get_widget (GTK_UI_MANAGER (ui),
265 "/PopupLog");
267 /* Add watches */
268 git_plugin->project_root_watch_id = anjuta_plugin_add_watch (plugin,
269 "project_root_uri",
270 on_project_root_added,
271 on_project_root_removed,
272 NULL);
274 git_plugin->editor_watch_id = anjuta_plugin_add_watch (plugin,
275 "document_manager_current_editor",
276 on_editor_added,
277 on_editor_removed,
278 NULL);
280 /* Log viewer */
281 git_plugin->log_viewer = git_log_window_create (git_plugin);
282 anjuta_shell_add_widget (plugin->shell,
283 git_plugin->log_viewer,
284 "GitLogViewer",
285 _("Git Log"),
286 GTK_STOCK_ZOOM_100,
287 ANJUTA_SHELL_PLACEMENT_CENTER,
288 NULL);
289 g_object_unref (git_plugin->log_viewer);
293 /* Git needs a working directory to work with; it can't take full paths,
294 * so make sure that Git can't be used if there's no project opened. */
295 git_menu_action = anjuta_ui_get_action (anjuta_shell_get_ui (plugin->shell,
296 NULL),
297 "ActionGroupGit",
298 "ActionMenuGit");
300 if (!git_plugin->project_root_directory)
302 gtk_action_set_sensitive (git_menu_action, FALSE);
303 gtk_widget_set_sensitive (git_plugin->log_viewer, FALSE);
306 return TRUE;
309 static gboolean
310 git_deactivate_plugin (AnjutaPlugin *plugin)
312 AnjutaUI *ui = anjuta_shell_get_ui (plugin->shell, NULL);
313 Git *git_plugin;
315 git_plugin = ANJUTA_PLUGIN_GIT (plugin);
317 DEBUG_PRINT ("Git: Dectivating Git plugin ...");
318 anjuta_ui_unmerge (ui, git_plugin->uiid);
319 anjuta_plugin_remove_watch (plugin, git_plugin->project_root_watch_id,
320 TRUE);
321 anjuta_plugin_remove_watch (plugin, git_plugin->editor_watch_id,
322 TRUE);
324 anjuta_shell_remove_widget (plugin->shell, git_plugin->log_viewer, NULL);
325 gtk_widget_destroy (git_plugin->log_popup_menu);
327 return TRUE;
330 static void
331 git_finalize (GObject *obj)
333 /* Finalization codes here */
334 G_OBJECT_CLASS (parent_class)->finalize (obj);
337 static void
338 git_dispose (GObject *obj)
340 /* Disposition codes */
341 G_OBJECT_CLASS (parent_class)->dispose (obj);
344 static void
345 git_instance_init (GObject *obj)
347 Git *plugin = ANJUTA_PLUGIN_GIT (obj);
348 plugin->uiid = 0;
351 static void
352 git_class_init (GObjectClass *klass)
354 AnjutaPluginClass *plugin_class = ANJUTA_PLUGIN_CLASS (klass);
356 parent_class = g_type_class_peek_parent (klass);
358 plugin_class->activate = git_activate_plugin;
359 plugin_class->deactivate = git_deactivate_plugin;
360 klass->finalize = git_finalize;
361 klass->dispose = git_dispose;
364 ANJUTA_PLUGIN_BOILERPLATE (Git, git);
365 ANJUTA_SIMPLE_PLUGIN (Git, git);