Implement branch switching.
[anjuta-git-plugin.git] / plugins / git / plugin.c
blob77592dd6dad3284b667fed6a9301ca6fa4b31c1b
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"
31 #define UI_FILE PACKAGE_DATA_DIR"/ui/anjuta-git.ui"
33 static gpointer parent_class;
35 static GtkActionEntry actions_git[] = {
37 "ActionMenuGit", /* Action name */
38 NULL, /* Stock icon, if any */
39 N_("_Git"), /* Display label */
40 NULL, /* short-cut */
41 NULL, /* Tooltip */
42 NULL /* action callback */
45 "ActionGitCommit", /* Action name */
46 GTK_STOCK_YES, /* Stock icon, if any */
47 N_("_Commit..."), /* Display label */
48 NULL, /* short-cut */
49 NULL, /* Tooltip */
50 G_CALLBACK (on_menu_git_commit) /* action callback */
53 "ActionGitResolve", /* Action name */
54 GTK_STOCK_PREFERENCES, /* Stock icon, if any */
55 N_("_Resolve Conflicts..."), /* Display label */
56 NULL, /* short-cut */
57 NULL, /* Tooltip */
58 G_CALLBACK (on_menu_git_resolve) /* action callback */
61 "ActionGitAdd", /* Action name */
62 GTK_STOCK_ADD, /* Stock icon, if any */
63 N_("_Add..."), /* Display label */
64 NULL, /* short-cut */
65 NULL, /* Tooltip */
66 G_CALLBACK (on_menu_git_add) /* action callback */
69 "ActionGitRemove", /* Action name */
70 GTK_STOCK_REMOVE, /* Stock icon, if any */
71 N_("_Remove..."), /* Display label */
72 NULL, /* short-cut */
73 NULL, /* Tooltip */
74 G_CALLBACK (on_menu_git_remove) /* action callback */
77 "ActionGitSwitch", /* Action name */
78 GTK_STOCK_JUMP_TO, /* Stock icon, if any */
79 N_("_Switch to another branch..."), /* Display label */
80 NULL, /* short-cut */
81 NULL, /* Tooltip */
82 G_CALLBACK (on_menu_git_switch) /* action callback */
85 "ActionGitMerge", /* Action name */
86 GTK_STOCK_CONVERT, /* Stock icon, if any */
87 N_("_Merge..."), /* Display label */
88 NULL, /* short-cut */
89 NULL, /* Tooltip */
90 G_CALLBACK (on_menu_git_merge) /* action callback */
93 "ActionGitDiffUncommitted", /* Action name */
94 GTK_STOCK_ZOOM_100, /* Stock icon, if any */
95 N_("_Diff uncommitted changes"), /* Display label */
96 NULL, /* short-cut */
97 NULL, /* Tooltip */
98 G_CALLBACK (on_menu_git_diff) /* action callback */
102 static void
103 on_project_root_added (AnjutaPlugin *plugin, const gchar *name,
104 const GValue *value, gpointer user_data)
106 Git *git_plugin;
107 gchar *project_root_uri;
108 GtkAction *git_menu_action;
110 git_plugin = ANJUTA_PLUGIN_GIT (plugin);
112 g_free (git_plugin->project_root_directory);
113 project_root_uri = g_value_dup_string (value);
114 git_plugin->project_root_directory = gnome_vfs_get_local_path_from_uri (project_root_uri);
116 git_menu_action = anjuta_ui_get_action (anjuta_shell_get_ui (plugin->shell,
117 NULL),
118 "ActionGroupGit",
119 "ActionMenuGit");
121 gtk_action_set_sensitive (git_menu_action, TRUE);
123 g_free (project_root_uri);
126 static void
127 on_project_root_removed (AnjutaPlugin *plugin, const gchar *name,
128 gpointer user_data)
130 GtkAction *git_menu_action;
131 Git *git_plugin;
133 git_plugin = ANJUTA_PLUGIN_GIT (plugin);
135 g_free (git_plugin->project_root_directory);
136 git_plugin->project_root_directory = NULL;
138 git_menu_action = anjuta_ui_get_action (anjuta_shell_get_ui (plugin->shell,
139 NULL),
140 "ActionGroupGit",
141 "ActionMenuGit");
143 gtk_action_set_sensitive (git_menu_action, FALSE);
146 static void
147 on_editor_added (AnjutaPlugin *plugin, const gchar *name, const GValue *value,
148 gpointer user_data)
150 Git *git_plugin;
151 gchar *current_editor_uri;
152 IAnjutaEditor *editor;
154 git_plugin = ANJUTA_PLUGIN_GIT (plugin);
155 editor = g_value_get_object (value);
157 if (IANJUTA_IS_EDITOR (editor))
159 g_free (git_plugin->current_editor_filename);
160 current_editor_uri = ianjuta_file_get_uri (IANJUTA_FILE (editor), NULL);
161 git_plugin->current_editor_filename = gnome_vfs_get_local_path_from_uri (current_editor_uri);
165 static void
166 on_editor_removed (AnjutaPlugin *plugin, const gchar *name, gpointer user_data)
168 Git *git_plugin;
170 git_plugin = ANJUTA_PLUGIN_GIT (plugin);
172 g_free (git_plugin->current_editor_filename);
173 git_plugin->current_editor_filename = NULL;
176 static gboolean
177 git_activate_plugin (AnjutaPlugin *plugin)
179 AnjutaUI *ui;
180 Git *git_plugin;
181 GtkAction *git_menu_action
183 DEBUG_PRINT ("Git: Activating Git plugin ...");
185 git_plugin = ANJUTA_PLUGIN_GIT (plugin);
186 ui = anjuta_shell_get_ui (plugin->shell, NULL);
188 /* Add all our actions */
189 anjuta_ui_add_action_group_entries (ui, "ActionGroupGit",
190 _("Git operations"),
191 actions_git,
192 G_N_ELEMENTS (actions_git),
193 GETTEXT_PACKAGE, TRUE, plugin);
194 git_plugin->uiid = anjuta_ui_merge (ui, UI_FILE);
196 /* Add watches */
197 git_plugin->project_root_watch_id = anjuta_plugin_add_watch (plugin,
198 "project_root_uri",
199 on_project_root_added,
200 on_project_root_removed,
201 NULL);
203 git_plugin->editor_watch_id = anjuta_plugin_add_watch (plugin,
204 "document_manager_current_editor",
205 on_editor_added,
206 on_editor_removed,
207 NULL);
209 /* Git needs a working directory to work with; it can't take full paths,
210 * so make sure that Git can't be used if there's no project opened. */
211 git_menu_action = anjuta_ui_get_action (anjuta_shell_get_ui (plugin->shell,
212 NULL),
213 "ActionGroupGit",
214 "ActionMenuGit");
216 gtk_action_set_sensitive (git_menu_action, FALSE);
218 return TRUE;
221 static gboolean
222 git_deactivate_plugin (AnjutaPlugin *plugin)
224 AnjutaUI *ui = anjuta_shell_get_ui (plugin->shell, NULL);
225 Git *git_plugin;
227 git_plugin = ANJUTA_PLUGIN_GIT (plugin);
229 DEBUG_PRINT ("Git: Dectivating Git plugin ...");
230 anjuta_ui_unmerge (ui, git_plugin->uiid);
231 anjuta_plugin_remove_watch (plugin, git_plugin->project_root_watch_id,
232 TRUE);
233 anjuta_plugin_remove_watch (plugin, git_plugin->editor_watch_id,
234 TRUE);
236 return TRUE;
239 static void
240 git_finalize (GObject *obj)
242 /* Finalization codes here */
243 G_OBJECT_CLASS (parent_class)->finalize (obj);
246 static void
247 git_dispose (GObject *obj)
249 /* Disposition codes */
250 G_OBJECT_CLASS (parent_class)->dispose (obj);
253 static void
254 git_instance_init (GObject *obj)
256 Git *plugin = ANJUTA_PLUGIN_GIT (obj);
257 plugin->uiid = 0;
260 static void
261 git_class_init (GObjectClass *klass)
263 AnjutaPluginClass *plugin_class = ANJUTA_PLUGIN_CLASS (klass);
265 parent_class = g_type_class_peek_parent (klass);
267 plugin_class->activate = git_activate_plugin;
268 plugin_class->deactivate = git_deactivate_plugin;
269 klass->finalize = git_finalize;
270 klass->dispose = git_dispose;
273 ANJUTA_PLUGIN_BOILERPLATE (Git, git);
274 ANJUTA_SIMPLE_PLUGIN (Git, git);