Implement support for unstaging files from commits.
[anjuta-git-plugin.git] / plugins / git / plugin.c
blob370bc3522424f2a986d990ac5bcabf5e6ee37d53
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"
34 #define UI_FILE PACKAGE_DATA_DIR"/ui/anjuta-git.ui"
36 static gpointer parent_class;
38 static GtkActionEntry actions_git[] = {
40 "ActionMenuGit", /* Action name */
41 NULL, /* Stock icon, if any */
42 N_("_Git"), /* Display label */
43 NULL, /* short-cut */
44 NULL, /* Tooltip */
45 NULL /* action callback */
48 "ActionGitCommit", /* Action name */
49 GTK_STOCK_YES, /* Stock icon, if any */
50 N_("_Commit..."), /* Display label */
51 NULL, /* short-cut */
52 NULL, /* Tooltip */
53 G_CALLBACK (on_menu_git_commit) /* action callback */
56 "ActionGitUnstageFiles", /* Action name */
57 GTK_STOCK_CANCEL, /* Stock icon, if any */
58 N_("_Unstage files..."), /* Display label */
59 NULL, /* short-cut */
60 NULL, /* Tooltip */
61 G_CALLBACK (on_menu_git_unstage) /* action callback */
65 "ActionGitResolve", /* Action name */
66 GTK_STOCK_PREFERENCES, /* Stock icon, if any */
67 N_("_Resolve conflicts..."), /* Display label */
68 NULL, /* short-cut */
69 NULL, /* Tooltip */
70 G_CALLBACK (on_menu_git_resolve) /* action callback */
73 "ActionGitAdd", /* Action name */
74 GTK_STOCK_ADD, /* Stock icon, if any */
75 N_("_Add..."), /* Display label */
76 NULL, /* short-cut */
77 NULL, /* Tooltip */
78 G_CALLBACK (on_menu_git_add) /* action callback */
81 "ActionGitRemove", /* Action name */
82 GTK_STOCK_REMOVE, /* Stock icon, if any */
83 N_("_Remove..."), /* Display label */
84 NULL, /* short-cut */
85 NULL, /* Tooltip */
86 G_CALLBACK (on_menu_git_remove) /* action callback */
89 "ActionGitCreateBranch", /* Action name */
90 GTK_STOCK_NEW, /* Stock icon, if any */
91 N_("_Create branch..."), /* Display label */
92 NULL, /* short-cut */
93 NULL, /* Tooltip */
94 G_CALLBACK (on_menu_git_create_branch) /* action callback */
97 "ActionGitDeleteBranch", /* Action name */
98 GTK_STOCK_DELETE, /* Stock icon, if any */
99 N_("_Delete branch..."), /* Display label */
100 NULL, /* short-cut */
101 NULL, /* Tooltip */
102 G_CALLBACK (on_menu_git_delete_branch) /* action callback */
105 "ActionGitSwitch", /* Action name */
106 GTK_STOCK_JUMP_TO, /* Stock icon, if any */
107 N_("_Switch to another branch..."), /* Display label */
108 NULL, /* short-cut */
109 NULL, /* Tooltip */
110 G_CALLBACK (on_menu_git_switch) /* action callback */
113 "ActionGitMerge", /* Action name */
114 GTK_STOCK_CONVERT, /* Stock icon, if any */
115 N_("_Merge..."), /* Display label */
116 NULL, /* short-cut */
117 NULL, /* Tooltip */
118 G_CALLBACK (on_menu_git_merge) /* action callback */
121 "ActionGitDiffUncommitted", /* Action name */
122 GTK_STOCK_ZOOM_100, /* Stock icon, if any */
123 N_("_Diff uncommitted changes"), /* Display label */
124 NULL, /* short-cut */
125 NULL, /* Tooltip */
126 G_CALLBACK (on_menu_git_diff) /* action callback */
130 static void
131 on_project_root_added (AnjutaPlugin *plugin, const gchar *name,
132 const GValue *value, gpointer user_data)
134 Git *git_plugin;
135 gchar *project_root_uri;
136 GtkAction *git_menu_action;
138 git_plugin = ANJUTA_PLUGIN_GIT (plugin);
140 g_free (git_plugin->project_root_directory);
141 project_root_uri = g_value_dup_string (value);
142 git_plugin->project_root_directory = gnome_vfs_get_local_path_from_uri (project_root_uri);
144 git_menu_action = anjuta_ui_get_action (anjuta_shell_get_ui (plugin->shell,
145 NULL),
146 "ActionGroupGit",
147 "ActionMenuGit");
149 gtk_action_set_sensitive (git_menu_action, TRUE);
151 g_free (project_root_uri);
154 static void
155 on_project_root_removed (AnjutaPlugin *plugin, const gchar *name,
156 gpointer user_data)
158 GtkAction *git_menu_action;
159 Git *git_plugin;
161 git_plugin = ANJUTA_PLUGIN_GIT (plugin);
163 g_free (git_plugin->project_root_directory);
164 git_plugin->project_root_directory = NULL;
166 git_menu_action = anjuta_ui_get_action (anjuta_shell_get_ui (plugin->shell,
167 NULL),
168 "ActionGroupGit",
169 "ActionMenuGit");
171 gtk_action_set_sensitive (git_menu_action, FALSE);
174 static void
175 on_editor_added (AnjutaPlugin *plugin, const gchar *name, const GValue *value,
176 gpointer user_data)
178 Git *git_plugin;
179 gchar *current_editor_uri;
180 IAnjutaEditor *editor;
182 git_plugin = ANJUTA_PLUGIN_GIT (plugin);
183 editor = g_value_get_object (value);
185 if (IANJUTA_IS_EDITOR (editor))
187 g_free (git_plugin->current_editor_filename);
188 current_editor_uri = ianjuta_file_get_uri (IANJUTA_FILE (editor), NULL);
189 git_plugin->current_editor_filename = gnome_vfs_get_local_path_from_uri (current_editor_uri);
193 static void
194 on_editor_removed (AnjutaPlugin *plugin, const gchar *name, gpointer user_data)
196 Git *git_plugin;
198 git_plugin = ANJUTA_PLUGIN_GIT (plugin);
200 g_free (git_plugin->current_editor_filename);
201 git_plugin->current_editor_filename = NULL;
204 static gboolean
205 git_activate_plugin (AnjutaPlugin *plugin)
207 AnjutaUI *ui;
208 Git *git_plugin;
209 GtkAction *git_menu_action
211 DEBUG_PRINT ("Git: Activating Git plugin ...");
213 git_plugin = ANJUTA_PLUGIN_GIT (plugin);
214 ui = anjuta_shell_get_ui (plugin->shell, NULL);
216 /* Add all our actions */
217 anjuta_ui_add_action_group_entries (ui, "ActionGroupGit",
218 _("Git operations"),
219 actions_git,
220 G_N_ELEMENTS (actions_git),
221 GETTEXT_PACKAGE, TRUE, plugin);
222 git_plugin->uiid = anjuta_ui_merge (ui, UI_FILE);
224 /* Add watches */
225 git_plugin->project_root_watch_id = anjuta_plugin_add_watch (plugin,
226 "project_root_uri",
227 on_project_root_added,
228 on_project_root_removed,
229 NULL);
231 git_plugin->editor_watch_id = anjuta_plugin_add_watch (plugin,
232 "document_manager_current_editor",
233 on_editor_added,
234 on_editor_removed,
235 NULL);
237 /* Git needs a working directory to work with; it can't take full paths,
238 * so make sure that Git can't be used if there's no project opened. */
239 git_menu_action = anjuta_ui_get_action (anjuta_shell_get_ui (plugin->shell,
240 NULL),
241 "ActionGroupGit",
242 "ActionMenuGit");
244 if (!git_plugin->project_root_directory)
245 gtk_action_set_sensitive (git_menu_action, FALSE);
247 return TRUE;
250 static gboolean
251 git_deactivate_plugin (AnjutaPlugin *plugin)
253 AnjutaUI *ui = anjuta_shell_get_ui (plugin->shell, NULL);
254 Git *git_plugin;
256 git_plugin = ANJUTA_PLUGIN_GIT (plugin);
258 DEBUG_PRINT ("Git: Dectivating Git plugin ...");
259 anjuta_ui_unmerge (ui, git_plugin->uiid);
260 anjuta_plugin_remove_watch (plugin, git_plugin->project_root_watch_id,
261 TRUE);
262 anjuta_plugin_remove_watch (plugin, git_plugin->editor_watch_id,
263 TRUE);
265 return TRUE;
268 static void
269 git_finalize (GObject *obj)
271 /* Finalization codes here */
272 G_OBJECT_CLASS (parent_class)->finalize (obj);
275 static void
276 git_dispose (GObject *obj)
278 /* Disposition codes */
279 G_OBJECT_CLASS (parent_class)->dispose (obj);
282 static void
283 git_instance_init (GObject *obj)
285 Git *plugin = ANJUTA_PLUGIN_GIT (obj);
286 plugin->uiid = 0;
289 static void
290 git_class_init (GObjectClass *klass)
292 AnjutaPluginClass *plugin_class = ANJUTA_PLUGIN_CLASS (klass);
294 parent_class = g_type_class_peek_parent (klass);
296 plugin_class->activate = git_activate_plugin;
297 plugin_class->deactivate = git_deactivate_plugin;
298 klass->finalize = git_finalize;
299 klass->dispose = git_dispose;
302 ANJUTA_PLUGIN_BOILERPLATE (Git, git);
303 ANJUTA_SIMPLE_PLUGIN (Git, git);