Implement support for listing and merging branches.
[anjuta-git-plugin.git] / plugins / git / plugin.c
blobaeb3f1eaa9c27a5aaabd40db9f0fd5eb4fa9deb5
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"
30 #define UI_FILE PACKAGE_DATA_DIR"/ui/anjuta-git.ui"
32 static gpointer parent_class;
34 static GtkActionEntry actions_git[] = {
36 "ActionMenuGit", /* Action name */
37 NULL, /* Stock icon, if any */
38 N_("_Git"), /* Display label */
39 NULL, /* short-cut */
40 NULL, /* Tooltip */
41 NULL /* action callback */
44 "ActionGitCommit", /* Action name */
45 GTK_STOCK_YES, /* Stock icon, if any */
46 N_("_Commit..."), /* Display label */
47 NULL, /* short-cut */
48 NULL, /* Tooltip */
49 G_CALLBACK (on_menu_git_commit) /* action callback */
52 "ActionGitResolve", /* Action name */
53 GTK_STOCK_PREFERENCES, /* Stock icon, if any */
54 N_("_Resolve Conflicts..."), /* Display label */
55 NULL, /* short-cut */
56 NULL, /* Tooltip */
57 G_CALLBACK (on_menu_git_resolve) /* action callback */
60 "ActionGitAdd", /* Action name */
61 GTK_STOCK_ADD, /* Stock icon, if any */
62 N_("_Add..."), /* Display label */
63 NULL, /* short-cut */
64 NULL, /* Tooltip */
65 G_CALLBACK (on_menu_git_add) /* action callback */
68 "ActionGitRemove", /* Action name */
69 GTK_STOCK_REMOVE, /* Stock icon, if any */
70 N_("_Remove..."), /* Display label */
71 NULL, /* short-cut */
72 NULL, /* Tooltip */
73 G_CALLBACK (on_menu_git_remove) /* action callback */
76 "ActionGitMerge", /* Action name */
77 GTK_STOCK_CONVERT, /* Stock icon, if any */
78 N_("_Merge..."), /* Display label */
79 NULL, /* short-cut */
80 NULL, /* Tooltip */
81 G_CALLBACK (on_menu_git_merge) /* action callback */
84 "ActionGitDiffUncommitted", /* Action name */
85 GTK_STOCK_ZOOM_100, /* Stock icon, if any */
86 N_("_Diff uncommitted changes"), /* Display label */
87 NULL, /* short-cut */
88 NULL, /* Tooltip */
89 G_CALLBACK (on_menu_git_diff) /* action callback */
93 static void
94 on_project_root_added (AnjutaPlugin *plugin, const gchar *name,
95 const GValue *value, gpointer user_data)
97 Git *git_plugin;
98 gchar *project_root_uri;
99 GtkAction *git_menu_action;
101 git_plugin = ANJUTA_PLUGIN_GIT (plugin);
103 g_free (git_plugin->project_root_directory);
104 project_root_uri = g_value_dup_string (value);
105 git_plugin->project_root_directory = gnome_vfs_get_local_path_from_uri (project_root_uri);
107 git_menu_action = anjuta_ui_get_action (anjuta_shell_get_ui (plugin->shell,
108 NULL),
109 "ActionGroupGit",
110 "ActionMenuGit");
112 gtk_action_set_sensitive (git_menu_action, TRUE);
114 g_free (project_root_uri);
117 static void
118 on_project_root_removed (AnjutaPlugin *plugin, const gchar *name,
119 gpointer user_data)
121 GtkAction *git_menu_action;
122 Git *git_plugin;
124 git_plugin = ANJUTA_PLUGIN_GIT (plugin);
126 g_free (git_plugin->project_root_directory);
127 git_plugin->project_root_directory = NULL;
129 git_menu_action = anjuta_ui_get_action (anjuta_shell_get_ui (plugin->shell,
130 NULL),
131 "ActionGroupGit",
132 "ActionMenuGit");
134 gtk_action_set_sensitive (git_menu_action, FALSE);
137 static void
138 on_editor_added (AnjutaPlugin *plugin, const gchar *name, const GValue *value,
139 gpointer user_data)
141 Git *git_plugin;
142 gchar *current_editor_uri;
143 IAnjutaEditor *editor;
145 git_plugin = ANJUTA_PLUGIN_GIT (plugin);
146 editor = g_value_get_object (value);
148 if (IANJUTA_IS_EDITOR (editor))
150 g_free (git_plugin->current_editor_filename);
151 current_editor_uri = ianjuta_file_get_uri (IANJUTA_FILE (editor), NULL);
152 git_plugin->current_editor_filename = gnome_vfs_get_local_path_from_uri (current_editor_uri);
156 static void
157 on_editor_removed (AnjutaPlugin *plugin, const gchar *name, gpointer user_data)
159 Git *git_plugin;
161 git_plugin = ANJUTA_PLUGIN_GIT (plugin);
163 g_free (git_plugin->current_editor_filename);
164 git_plugin->current_editor_filename = NULL;
167 static gboolean
168 git_activate_plugin (AnjutaPlugin *plugin)
170 AnjutaUI *ui;
171 Git *git_plugin;
172 GtkAction *git_menu_action
174 DEBUG_PRINT ("Git: Activating Git plugin ...");
176 git_plugin = ANJUTA_PLUGIN_GIT (plugin);
177 ui = anjuta_shell_get_ui (plugin->shell, NULL);
179 /* Add all our actions */
180 anjuta_ui_add_action_group_entries (ui, "ActionGroupGit",
181 _("Git operations"),
182 actions_git,
183 G_N_ELEMENTS (actions_git),
184 GETTEXT_PACKAGE, TRUE, plugin);
185 git_plugin->uiid = anjuta_ui_merge (ui, UI_FILE);
187 /* Add watches */
188 git_plugin->project_root_watch_id = anjuta_plugin_add_watch (plugin,
189 "project_root_uri",
190 on_project_root_added,
191 on_project_root_removed,
192 NULL);
194 git_plugin->editor_watch_id = anjuta_plugin_add_watch (plugin,
195 "document_manager_current_editor",
196 on_editor_added,
197 on_editor_removed,
198 NULL);
200 /* Git needs a working directory to work with; it can't take full paths,
201 * so make sure that Git can't be used if there's no project opened. */
202 git_menu_action = anjuta_ui_get_action (anjuta_shell_get_ui (plugin->shell,
203 NULL),
204 "ActionGroupGit",
205 "ActionMenuGit");
207 gtk_action_set_sensitive (git_menu_action, FALSE);
209 return TRUE;
212 static gboolean
213 git_deactivate_plugin (AnjutaPlugin *plugin)
215 AnjutaUI *ui = anjuta_shell_get_ui (plugin->shell, NULL);
216 Git *git_plugin;
218 git_plugin = ANJUTA_PLUGIN_GIT (plugin);
220 DEBUG_PRINT ("Git: Dectivating Git plugin ...");
221 anjuta_ui_unmerge (ui, git_plugin->uiid);
222 anjuta_plugin_remove_watch (plugin, git_plugin->project_root_watch_id,
223 TRUE);
224 anjuta_plugin_remove_watch (plugin, git_plugin->editor_watch_id,
225 TRUE);
227 return TRUE;
230 static void
231 git_finalize (GObject *obj)
233 /* Finalization codes here */
234 G_OBJECT_CLASS (parent_class)->finalize (obj);
237 static void
238 git_dispose (GObject *obj)
240 /* Disposition codes */
241 G_OBJECT_CLASS (parent_class)->dispose (obj);
244 static void
245 git_instance_init (GObject *obj)
247 Git *plugin = ANJUTA_PLUGIN_GIT (obj);
248 plugin->uiid = 0;
251 static void
252 git_class_init (GObjectClass *klass)
254 AnjutaPluginClass *plugin_class = ANJUTA_PLUGIN_CLASS (klass);
256 parent_class = g_type_class_peek_parent (klass);
258 plugin_class->activate = git_activate_plugin;
259 plugin_class->deactivate = git_deactivate_plugin;
260 klass->finalize = git_finalize;
261 klass->dispose = git_dispose;
264 ANJUTA_PLUGIN_BOILERPLATE (Git, git);
265 ANJUTA_SIMPLE_PLUGIN (Git, git);