Implement simple branch creation.
[anjuta-git-plugin.git] / plugins / git / plugin.c
bloba0aa7827e11a50953b8ab4832e3d1820bff9ed12
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"
32 #define UI_FILE PACKAGE_DATA_DIR"/ui/anjuta-git.ui"
34 static gpointer parent_class;
36 static GtkActionEntry actions_git[] = {
38 "ActionMenuGit", /* Action name */
39 NULL, /* Stock icon, if any */
40 N_("_Git"), /* Display label */
41 NULL, /* short-cut */
42 NULL, /* Tooltip */
43 NULL /* action callback */
46 "ActionGitCommit", /* Action name */
47 GTK_STOCK_YES, /* Stock icon, if any */
48 N_("_Commit..."), /* Display label */
49 NULL, /* short-cut */
50 NULL, /* Tooltip */
51 G_CALLBACK (on_menu_git_commit) /* action callback */
54 "ActionGitResolve", /* Action name */
55 GTK_STOCK_PREFERENCES, /* Stock icon, if any */
56 N_("_Resolve conflicts..."), /* Display label */
57 NULL, /* short-cut */
58 NULL, /* Tooltip */
59 G_CALLBACK (on_menu_git_resolve) /* action callback */
62 "ActionGitAdd", /* Action name */
63 GTK_STOCK_ADD, /* Stock icon, if any */
64 N_("_Add..."), /* Display label */
65 NULL, /* short-cut */
66 NULL, /* Tooltip */
67 G_CALLBACK (on_menu_git_add) /* action callback */
70 "ActionGitRemove", /* Action name */
71 GTK_STOCK_REMOVE, /* Stock icon, if any */
72 N_("_Remove..."), /* Display label */
73 NULL, /* short-cut */
74 NULL, /* Tooltip */
75 G_CALLBACK (on_menu_git_remove) /* action callback */
78 "ActionGitCreateBranch", /* Action name */
79 GTK_STOCK_NEW, /* Stock icon, if any */
80 N_("_Create branch..."), /* Display label */
81 NULL, /* short-cut */
82 NULL, /* Tooltip */
83 G_CALLBACK (on_menu_git_create_branch) /* action callback */
86 "ActionGitSwitch", /* Action name */
87 GTK_STOCK_JUMP_TO, /* Stock icon, if any */
88 N_("_Switch to another branch..."), /* Display label */
89 NULL, /* short-cut */
90 NULL, /* Tooltip */
91 G_CALLBACK (on_menu_git_switch) /* action callback */
94 "ActionGitMerge", /* Action name */
95 GTK_STOCK_CONVERT, /* Stock icon, if any */
96 N_("_Merge..."), /* Display label */
97 NULL, /* short-cut */
98 NULL, /* Tooltip */
99 G_CALLBACK (on_menu_git_merge) /* action callback */
102 "ActionGitDiffUncommitted", /* Action name */
103 GTK_STOCK_ZOOM_100, /* Stock icon, if any */
104 N_("_Diff uncommitted changes"), /* Display label */
105 NULL, /* short-cut */
106 NULL, /* Tooltip */
107 G_CALLBACK (on_menu_git_diff) /* action callback */
111 static void
112 on_project_root_added (AnjutaPlugin *plugin, const gchar *name,
113 const GValue *value, gpointer user_data)
115 Git *git_plugin;
116 gchar *project_root_uri;
117 GtkAction *git_menu_action;
119 git_plugin = ANJUTA_PLUGIN_GIT (plugin);
121 g_free (git_plugin->project_root_directory);
122 project_root_uri = g_value_dup_string (value);
123 git_plugin->project_root_directory = gnome_vfs_get_local_path_from_uri (project_root_uri);
125 git_menu_action = anjuta_ui_get_action (anjuta_shell_get_ui (plugin->shell,
126 NULL),
127 "ActionGroupGit",
128 "ActionMenuGit");
130 gtk_action_set_sensitive (git_menu_action, TRUE);
132 g_free (project_root_uri);
135 static void
136 on_project_root_removed (AnjutaPlugin *plugin, const gchar *name,
137 gpointer user_data)
139 GtkAction *git_menu_action;
140 Git *git_plugin;
142 git_plugin = ANJUTA_PLUGIN_GIT (plugin);
144 g_free (git_plugin->project_root_directory);
145 git_plugin->project_root_directory = NULL;
147 git_menu_action = anjuta_ui_get_action (anjuta_shell_get_ui (plugin->shell,
148 NULL),
149 "ActionGroupGit",
150 "ActionMenuGit");
152 gtk_action_set_sensitive (git_menu_action, FALSE);
155 static void
156 on_editor_added (AnjutaPlugin *plugin, const gchar *name, const GValue *value,
157 gpointer user_data)
159 Git *git_plugin;
160 gchar *current_editor_uri;
161 IAnjutaEditor *editor;
163 git_plugin = ANJUTA_PLUGIN_GIT (plugin);
164 editor = g_value_get_object (value);
166 if (IANJUTA_IS_EDITOR (editor))
168 g_free (git_plugin->current_editor_filename);
169 current_editor_uri = ianjuta_file_get_uri (IANJUTA_FILE (editor), NULL);
170 git_plugin->current_editor_filename = gnome_vfs_get_local_path_from_uri (current_editor_uri);
174 static void
175 on_editor_removed (AnjutaPlugin *plugin, const gchar *name, gpointer user_data)
177 Git *git_plugin;
179 git_plugin = ANJUTA_PLUGIN_GIT (plugin);
181 g_free (git_plugin->current_editor_filename);
182 git_plugin->current_editor_filename = NULL;
185 static gboolean
186 git_activate_plugin (AnjutaPlugin *plugin)
188 AnjutaUI *ui;
189 Git *git_plugin;
190 GtkAction *git_menu_action
192 DEBUG_PRINT ("Git: Activating Git plugin ...");
194 git_plugin = ANJUTA_PLUGIN_GIT (plugin);
195 ui = anjuta_shell_get_ui (plugin->shell, NULL);
197 /* Add all our actions */
198 anjuta_ui_add_action_group_entries (ui, "ActionGroupGit",
199 _("Git operations"),
200 actions_git,
201 G_N_ELEMENTS (actions_git),
202 GETTEXT_PACKAGE, TRUE, plugin);
203 git_plugin->uiid = anjuta_ui_merge (ui, UI_FILE);
205 /* Add watches */
206 git_plugin->project_root_watch_id = anjuta_plugin_add_watch (plugin,
207 "project_root_uri",
208 on_project_root_added,
209 on_project_root_removed,
210 NULL);
212 git_plugin->editor_watch_id = anjuta_plugin_add_watch (plugin,
213 "document_manager_current_editor",
214 on_editor_added,
215 on_editor_removed,
216 NULL);
218 /* Git needs a working directory to work with; it can't take full paths,
219 * so make sure that Git can't be used if there's no project opened. */
220 git_menu_action = anjuta_ui_get_action (anjuta_shell_get_ui (plugin->shell,
221 NULL),
222 "ActionGroupGit",
223 "ActionMenuGit");
225 if (!git_plugin->project_root_directory)
226 gtk_action_set_sensitive (git_menu_action, FALSE);
228 return TRUE;
231 static gboolean
232 git_deactivate_plugin (AnjutaPlugin *plugin)
234 AnjutaUI *ui = anjuta_shell_get_ui (plugin->shell, NULL);
235 Git *git_plugin;
237 git_plugin = ANJUTA_PLUGIN_GIT (plugin);
239 DEBUG_PRINT ("Git: Dectivating Git plugin ...");
240 anjuta_ui_unmerge (ui, git_plugin->uiid);
241 anjuta_plugin_remove_watch (plugin, git_plugin->project_root_watch_id,
242 TRUE);
243 anjuta_plugin_remove_watch (plugin, git_plugin->editor_watch_id,
244 TRUE);
246 return TRUE;
249 static void
250 git_finalize (GObject *obj)
252 /* Finalization codes here */
253 G_OBJECT_CLASS (parent_class)->finalize (obj);
256 static void
257 git_dispose (GObject *obj)
259 /* Disposition codes */
260 G_OBJECT_CLASS (parent_class)->dispose (obj);
263 static void
264 git_instance_init (GObject *obj)
266 Git *plugin = ANJUTA_PLUGIN_GIT (obj);
267 plugin->uiid = 0;
270 static void
271 git_class_init (GObjectClass *klass)
273 AnjutaPluginClass *plugin_class = ANJUTA_PLUGIN_CLASS (klass);
275 parent_class = g_type_class_peek_parent (klass);
277 plugin_class->activate = git_activate_plugin;
278 plugin_class->deactivate = git_deactivate_plugin;
279 klass->finalize = git_finalize;
280 klass->dispose = git_dispose;
283 ANJUTA_PLUGIN_BOILERPLATE (Git, git);
284 ANJUTA_SIMPLE_PLUGIN (Git, git);