Integrate removing files with the file manager
[anjuta-git-plugin.git] / plugins / subversion / plugin.c
blob4c2143404b295e6f8d0d6458d9f1f4d9a01f1c1f
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 plugin.c
4 Copyright (C) 2004 Naba Kumar, Johannes Schmid
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, Boston, MA 02110-1301 USA
21 #include <libgnomevfs/gnome-vfs-utils.h>
22 #include <libgnomevfs/gnome-vfs.h>
24 #include <libanjuta/anjuta-shell.h>
25 #include <libanjuta/anjuta-preferences.h>
26 #include <libanjuta/anjuta-debug.h>
27 #include <libanjuta/interfaces/ianjuta-file.h>
28 #include <libanjuta/interfaces/ianjuta-document-manager.h>
29 #include <libanjuta/interfaces/ianjuta-file-manager.h>
30 #include <libanjuta/interfaces/ianjuta-project-manager.h>
31 #include <libanjuta/interfaces/ianjuta-vcs.h>
33 #include "plugin.h"
34 #include "subversion-add-dialog.h"
35 #include "subversion-remove-dialog.h"
36 #include "subversion-commit-dialog.h"
37 #include "subversion-update-dialog.h"
38 #include "subversion-revert-dialog.h"
39 #include "subversion-log-dialog.h"
40 #include "subversion-diff-dialog.h"
41 #include "subversion-copy-dialog.h"
42 #include "subversion-switch-dialog.h"
43 #include "subversion-merge-dialog.h"
44 #include "subversion-resolve-dialog.h"
46 #define UI_FILE PACKAGE_DATA_DIR"/ui/anjuta-subversion.ui"
48 static gpointer parent_class;
50 static GtkActionEntry actions_subversion[] = {
52 "ActionMenuSubversion", /* Action name */
53 NULL, /* Stock icon, if any */
54 N_("_Subversion"), /* Display label */
55 NULL, /* short-cut */
56 NULL, /* Tooltip */
57 NULL
60 "ActionSubversionAdd", /* Action name */
61 GTK_STOCK_ADD, /* Stock icon, if any */
62 N_("_Add..."), /* Display label */
63 NULL, /* short-cut */
64 N_("Add a new file/directory to the Subversion tree"), /* Tooltip */
65 G_CALLBACK (on_menu_subversion_add) /* action callback */
68 "ActionSubversionRemove", /* Action name */
69 GTK_STOCK_REMOVE, /* Stock icon, if any */
70 N_("_Remove..."), /* Display label */
71 NULL, /* short-cut */
72 N_("Remove a file/directory from Subversion tree"), /* Tooltip */
73 G_CALLBACK (on_menu_subversion_remove) /* action callback */
76 "ActionSubversionCommit", /* Action name */
77 GTK_STOCK_YES, /* Stock icon, if any */
78 N_("_Commit..."), /* Display label */
79 NULL, /* short-cut */
80 N_("Commit your changes to the Subversion tree"), /* Tooltip */
81 G_CALLBACK (on_menu_subversion_commit) /* action callback */
84 "ActionSubversionRevert", /* Action name */
85 GTK_STOCK_UNDO, /* Stock icon, if any */
86 N_("_Revert..."), /* Display label */
87 NULL, /* short-cut */
88 N_("Revert changes to your working copy."), /* Tooltip */
89 G_CALLBACK (on_menu_subversion_revert) /* action callback */
92 "ActionSubversionResolve", /* Action name */
93 GTK_STOCK_PREFERENCES, /* Stock icon, if any */
94 N_("_Resolve Conflicts..."), /* Display label */
95 NULL, /* short-cut */
96 N_("Resolve conflicts in your working copy."), /* Tooltip */
97 G_CALLBACK (on_menu_subversion_resolve) /* action callback */
100 "ActionSubversionUpdate", /* Action name */
101 GTK_STOCK_REFRESH, /* Stock icon, if any */
102 N_("_Update..."), /* Display label */
103 NULL, /* short-cut */
104 N_("Sync your local copy with the Subversion tree"), /* Tooltip */
105 G_CALLBACK (on_menu_subversion_update) /* action callback */
108 "ActionSubversionCopy", /* Action name */
109 GTK_STOCK_COPY, /* Stock icon, if any */
110 N_("Copy Files/Folders..."), /* Display label */
111 NULL, /* short-cut */
112 N_("Copy files/folders in the repository"), /* Tooltip */
113 G_CALLBACK (on_menu_subversion_copy) /* action callback */
116 "ActionSubversionSwitch", /* Action name */
117 GTK_STOCK_JUMP_TO, /* Stock icon, if any */
118 N_("Switch to a Branch/Tag..."), /* Display label */
119 NULL, /* short-cut */
120 N_("Switch your local copy to a branch or tag in the repository"), /* Tooltip */
121 G_CALLBACK (on_menu_subversion_switch) /* action callback */
124 "ActionSubversionMerge", /* Action name */
125 GTK_STOCK_CONVERT, /* Stock icon, if any */
126 N_("Merge..."), /* Display label */
127 NULL, /* short-cut */
128 N_("Merge changes into your working copy"), /* Tooltip */
129 G_CALLBACK (on_menu_subversion_merge) /* action callback */
132 "ActionSubversionLog", /* Action name */
133 GTK_STOCK_ZOOM_100, /* Stock icon, if any */
134 N_("_View Log..."), /* Display label */
135 NULL, /* short-cut */
136 N_("View file history"), /* Tooltip */
137 G_CALLBACK (on_menu_subversion_log) /* action callback */
140 "ActionSubversionDiff", /* Action name */
141 GTK_STOCK_ZOOM_100, /* Stock icon, if any */
142 N_("_Diff..."), /* Display label */
143 NULL, /* short-cut */
144 N_("Diff local tree with repository"), /* Tooltip */
145 G_CALLBACK (on_menu_subversion_diff) /* action callback */
149 static GtkActionEntry popup_actions_subversion[] = {
151 "ActionPopupSubversion", /* Action name */
152 NULL, /* Stock icon, if any */
153 N_("_Subversion"), /* Display label */
154 NULL, /* short-cut */
155 NULL, /* Tooltip */
156 NULL
159 "ActionPopupSubversionUpdate", /* Action name */
160 GTK_STOCK_REFRESH, /* Stock icon, if any */
161 N_("_Update..."), /* Display label */
162 NULL, /* short-cut */
163 N_("Sync your local copy with the Subversion tree"), /* Tooltip */
164 G_CALLBACK (on_fm_subversion_update) /* action callback */
167 "ActionPopupSubversionAdd", /* Action name */
168 GTK_STOCK_ADD, /* Stock icon, if any */
169 N_("_Add..."), /* Display label */
170 NULL, /* short-cut */
171 N_("Add a new file/directory to the Subversion tree"), /* Tooltip */
172 G_CALLBACK (on_fm_subversion_add) /* action callback */
175 "ActionPopupSubversionRemove", /* Action name */
176 GTK_STOCK_REMOVE, /* Stock icon, if any */
177 N_("_Remove..."), /* Display label */
178 NULL, /* short-cut */
179 N_("Remove a file/directory from Subversion tree"), /* Tooltip */
180 G_CALLBACK (on_fm_subversion_remove) /* action callback */
183 "ActionPopupSubversionLog", /* Action name */
184 GTK_STOCK_ZOOM_100, /* Stock icon, if any */
185 N_("_View Log..."), /* Display label */
186 NULL, /* short-cut */
187 N_("View file history"), /* Tooltip */
188 G_CALLBACK (on_fm_subversion_log) /* action callback */
191 "ActionPopupSubversionCopy", /* Action name */
192 GTK_STOCK_COPY, /* Stock icon, if any */
193 N_("Copy..."), /* Display label */
194 NULL, /* short-cut */
195 N_("Copy files/folders in the repository"), /* Tooltip */
196 G_CALLBACK (on_fm_subversion_copy) /* action callback */
199 "ActionPopupSubversionDiff", /* Action name */
200 GTK_STOCK_ZOOM_100, /* Stock icon, if any */
201 N_("Diff..."), /* Display label */
202 NULL, /* short-cut */
203 N_("Diff local tree with repository"), /* Tooltip */
204 G_CALLBACK (on_fm_subversion_diff) /* action callback */
208 static void
209 value_added_fm_current_file (AnjutaPlugin *plugin, const char *name,
210 const GValue *value, gpointer data)
212 AnjutaUI *ui;
213 GtkAction *subversion_menu_action;
214 gchar *uri;
215 GnomeVFSURI *subversion_uri = NULL;
216 gchar *subversion_text_uri = NULL;
217 gchar *subversion_dir;
218 gchar *filename;
219 GnomeVFSDirectoryHandle* handle;
220 GnomeVFSFileInfo info;
221 GnomeVFSResult result;
222 GFile* file;
224 file = G_FILE(g_value_get_object (value));
225 uri = g_file_get_uri (file);
226 filename = g_file_get_path (file);
227 g_return_if_fail (filename != NULL);
229 Subversion *subversion = ANJUTA_PLUGIN_SUBVERSION (plugin);
230 ui = anjuta_shell_get_ui (plugin->shell, NULL);
232 if (subversion->fm_current_filename)
233 g_free (subversion->fm_current_filename);
234 subversion->fm_current_filename = filename;
236 /* Show popup menu if Subversion directory exists */
237 subversion_menu_action = anjuta_ui_get_action (ui, "ActionGroupPopupSubversion", "ActionPopupSubversion");
239 /* If a directory is selected we check if it contains a "Subversion" directory,
240 if it is a file we check if it's directory contains a "Subversion" directory */
241 result = gnome_vfs_get_file_info(uri, &info,
242 GNOME_VFS_FILE_INFO_DEFAULT);
243 if (result == GNOME_VFS_OK)
245 if (info.type == GNOME_VFS_FILE_TYPE_DIRECTORY)
247 /* Is "Subversion" a valid svn directory in a local checkout,
248 * If yes, we should check for both .svn and Subversion.
250 /* subversion_dir = g_build_filename (uri, "Subversion", NULL); */
251 subversion_dir = g_build_filename (uri, ".svn", NULL);
254 else
256 subversion_uri = gnome_vfs_uri_new (uri);
257 subversion_text_uri = gnome_vfs_uri_extract_dirname(subversion_uri);
258 /* subversion_dir = g_strconcat(subversion_text_uri, "/Subversion", NULL); */
259 subversion_dir = g_build_filename(subversion_text_uri, ".svn", NULL);
260 g_free(subversion_text_uri);
261 gnome_vfs_uri_unref(subversion_uri);
264 else
265 return; /* Strange... */
266 if (gnome_vfs_directory_open(&handle, subversion_dir,
267 GNOME_VFS_FILE_INFO_DEFAULT) == GNOME_VFS_OK)
270 g_object_set (G_OBJECT (subversion_menu_action), "sensitive", TRUE, NULL);
272 else
274 g_object_set (G_OBJECT (subversion_menu_action), "sensitive", FALSE, NULL);
276 g_free (subversion_dir);
277 g_free (uri);
280 static void
281 value_removed_fm_current_file (AnjutaPlugin *plugin,
282 const char *name, gpointer data)
284 AnjutaUI *ui;
285 GtkAction *action;
287 Subversion *subversion = ANJUTA_PLUGIN_SUBVERSION (plugin);
289 if (subversion->fm_current_filename)
290 g_free (subversion->fm_current_filename);
291 subversion->fm_current_filename = NULL;
293 ui = anjuta_shell_get_ui (plugin->shell, NULL);
294 action = anjuta_ui_get_action (ui, "ActionGroupPopupSubversion", "ActionPopupSubversion");
295 g_object_set (G_OBJECT (action), "sensitive", FALSE, NULL);
298 static void
299 value_added_project_root_uri (AnjutaPlugin *plugin, const gchar *name,
300 const GValue *value, gpointer user_data)
302 Subversion *bb_plugin;
303 const gchar *root_uri;
304 GtkAction *commit_action;
305 GtkAction *revert_action;
306 GtkAction *resolve_action;
308 bb_plugin = ANJUTA_PLUGIN_SUBVERSION (plugin);
309 commit_action = anjuta_ui_get_action (anjuta_shell_get_ui (plugin->shell,
310 NULL),
311 "ActionGroupSubversion",
312 "ActionSubversionCommit");
313 revert_action = anjuta_ui_get_action (anjuta_shell_get_ui (plugin->shell,
314 NULL),
315 "ActionGroupSubversion",
316 "ActionSubversionRevert");
317 resolve_action = anjuta_ui_get_action (anjuta_shell_get_ui (plugin->shell,
318 NULL),
319 "ActionGroupSubversion",
320 "ActionSubversionResolve");
322 DEBUG_PRINT ("Project added");
324 if (bb_plugin->project_root_dir)
325 g_free (bb_plugin->project_root_dir);
326 bb_plugin->project_root_dir = NULL;
328 root_uri = g_value_get_string (value);
329 if (root_uri)
331 bb_plugin->project_root_dir =
332 gnome_vfs_get_local_path_from_uri (root_uri);
333 if (bb_plugin->project_root_dir)
335 // update_project_ui (bb_plugin);
336 subversion_log_set_whole_project_sensitive (bb_plugin->log_gxml,
337 TRUE);
338 gtk_action_set_sensitive (commit_action, TRUE);
339 gtk_action_set_sensitive (revert_action, TRUE);
340 gtk_action_set_sensitive (resolve_action, TRUE);
347 static void
348 value_removed_project_root_uri (AnjutaPlugin *plugin, const gchar *name,
349 gpointer user_data)
351 Subversion *bb_plugin;
352 GtkAction *commit_action;
353 GtkAction *revert_action;
354 GtkAction *resolve_action;
356 bb_plugin = ANJUTA_PLUGIN_SUBVERSION (plugin);
357 commit_action = anjuta_ui_get_action (anjuta_shell_get_ui (plugin->shell,
358 NULL),
359 "ActionGroupSubversion",
360 "ActionSubversionCommit");
361 revert_action = anjuta_ui_get_action (anjuta_shell_get_ui (plugin->shell,
362 NULL),
363 "ActionGroupSubversion",
364 "ActionSubversionRevert");
365 resolve_action = anjuta_ui_get_action (anjuta_shell_get_ui (plugin->shell,
366 NULL),
367 "ActionGroupSubversion",
368 "ActionSubversionResolve");
370 if (bb_plugin->project_root_dir)
371 g_free (bb_plugin->project_root_dir);
372 bb_plugin->project_root_dir = NULL;
373 // update_project_ui (bb_plugin);
375 subversion_log_set_whole_project_sensitive (bb_plugin->log_gxml,
376 FALSE);
377 gtk_action_set_sensitive (commit_action, FALSE);
378 gtk_action_set_sensitive (revert_action, FALSE);
379 gtk_action_set_sensitive (resolve_action, FALSE);
382 static void
383 value_added_current_editor (AnjutaPlugin *plugin, const char *name,
384 const GValue *value, gpointer data)
386 AnjutaUI *ui;
387 GFile* file;
388 GObject *editor;
390 editor = g_value_get_object (value);
392 if (!IANJUTA_IS_EDITOR(editor))
393 return;
395 Subversion *subversion = ANJUTA_PLUGIN_SUBVERSION (plugin);
396 ui = anjuta_shell_get_ui (plugin->shell, NULL);
398 if (subversion->current_editor_filename)
399 g_free (subversion->current_editor_filename);
400 subversion->current_editor_filename = NULL;
402 file = ianjuta_file_get_file (IANJUTA_FILE (editor), NULL);
403 if (file)
405 gchar *filename;
407 filename = g_file_get_path (file);
408 if (!filename)
409 return;
410 subversion->current_editor_filename = filename;
414 static void
415 value_removed_current_editor (AnjutaPlugin *plugin,
416 const char *name, gpointer data)
418 Subversion *subversion = ANJUTA_PLUGIN_SUBVERSION (plugin);
420 if (subversion->current_editor_filename)
421 g_free (subversion->current_editor_filename);
422 subversion->current_editor_filename = NULL;
424 // update_module_ui (subversion);
427 static gboolean
428 activate_plugin (AnjutaPlugin *plugin)
430 AnjutaUI *ui;
431 Subversion *subversion;
432 GtkAction *commit_action;
433 GtkAction *revert_action;
434 GtkAction *resolve_action;
436 DEBUG_PRINT ("Subversion: Activating Subversion plugin ...");
437 subversion = ANJUTA_PLUGIN_SUBVERSION (plugin);
439 ui = anjuta_shell_get_ui (plugin->shell, NULL);
441 /* Add all our actions */
442 subversion->action_group =
443 anjuta_ui_add_action_group_entries (ui, "ActionGroupSubversion",
444 _("Subversion operations"),
445 actions_subversion,
446 G_N_ELEMENTS (actions_subversion),
447 GETTEXT_PACKAGE, TRUE, plugin);
448 subversion->popup_action_group =
449 anjuta_ui_add_action_group_entries (ui, "ActionGroupPopupSubversion",
450 _("Subversion popup operations"),
451 popup_actions_subversion,
452 G_N_ELEMENTS (popup_actions_subversion),
453 GETTEXT_PACKAGE, FALSE, plugin);
455 /* Merge UI */
456 subversion->uiid = anjuta_ui_merge (ui, UI_FILE);
458 subversion->log_gxml = glade_xml_new (GLADE_FILE, "subversion_log",
459 NULL);
461 /* Add watches */
462 subversion->fm_watch_id =
463 anjuta_plugin_add_watch (plugin, IANJUTA_FILE_MANAGER_SELECTED_FILE,
464 value_added_fm_current_file,
465 value_removed_fm_current_file, NULL);
466 subversion->project_watch_id =
467 anjuta_plugin_add_watch (plugin, IANJUTA_PROJECT_MANAGER_PROJECT_ROOT_URI,
468 value_added_project_root_uri,
469 value_removed_project_root_uri, NULL);
470 subversion->editor_watch_id =
471 anjuta_plugin_add_watch (plugin, IANJUTA_DOCUMENT_MANAGER_CURRENT_DOCUMENT,
472 value_added_current_editor,
473 value_removed_current_editor, NULL);
475 subversion->log_viewer = subversion_log_window_create (subversion);
476 anjuta_shell_add_widget (plugin->shell, subversion->log_viewer,
477 "AnjutaSubversionLogViewer",
478 _("Subversion Log"),
479 GTK_STOCK_ZOOM_100,
480 ANJUTA_SHELL_PLACEMENT_CENTER,
481 NULL);
483 g_object_unref (subversion->log_viewer);
485 commit_action = anjuta_ui_get_action (anjuta_shell_get_ui (plugin->shell,
486 NULL),
487 "ActionGroupSubversion",
488 "ActionSubversionCommit");
489 revert_action = anjuta_ui_get_action (anjuta_shell_get_ui (plugin->shell,
490 NULL),
491 "ActionGroupSubversion",
492 "ActionSubversionRevert");
493 resolve_action = anjuta_ui_get_action (anjuta_shell_get_ui (plugin->shell,
494 NULL),
495 "ActionGroupSubversion",
496 "ActionSubversionResolve");
498 if (!subversion->project_root_dir)
500 gtk_action_set_sensitive (commit_action, FALSE);
501 gtk_action_set_sensitive (revert_action, FALSE);
502 gtk_action_set_sensitive (resolve_action, FALSE);
505 return TRUE;
508 static gboolean
509 deactivate_plugin (AnjutaPlugin *plugin)
511 Subversion *subversion = ANJUTA_PLUGIN_SUBVERSION (plugin);
512 AnjutaUI *ui = anjuta_shell_get_ui (plugin->shell, NULL);
513 DEBUG_PRINT ("Subversion: Dectivating Subversion plugin ...");
514 anjuta_plugin_remove_watch (plugin, subversion->fm_watch_id, TRUE);
515 anjuta_plugin_remove_watch (plugin, subversion->project_watch_id, TRUE);
516 anjuta_plugin_remove_watch (plugin, subversion->editor_watch_id, TRUE);
517 anjuta_ui_unmerge (ui, ANJUTA_PLUGIN_SUBVERSION (plugin)->uiid);
518 anjuta_ui_remove_action_group (ui, ANJUTA_PLUGIN_SUBVERSION (plugin)->action_group);
519 anjuta_ui_remove_action_group (ui, ANJUTA_PLUGIN_SUBVERSION (plugin)->popup_action_group);
521 anjuta_shell_remove_widget (plugin->shell,
522 ANJUTA_PLUGIN_SUBVERSION (plugin)->log_viewer,
523 NULL);
525 g_object_unref (ANJUTA_PLUGIN_SUBVERSION (plugin)->log_gxml);
527 return TRUE;
530 static void
531 finalize (GObject *obj)
533 apr_terminate ();
534 G_OBJECT_CLASS (parent_class)->finalize (obj);
537 static void
538 dispose (GObject *obj)
540 // Subversion *plugin = ANJUTA_PLUGIN_SUBVERSION (obj);
541 G_OBJECT_CLASS (parent_class)->dispose (obj);
544 static void
545 subversion_instance_init (GObject *obj)
547 Subversion *plugin = ANJUTA_PLUGIN_SUBVERSION (obj);
548 plugin->uiid = 0;
549 plugin->mesg_view = NULL;
550 plugin->launcher = NULL;
551 plugin->fm_current_filename = NULL;
552 plugin->project_root_dir = NULL;
553 plugin->current_editor_filename = NULL;
554 plugin->log_gxml = NULL;
555 plugin->log_viewer = NULL;
557 apr_initialize ();
560 static void
561 subversion_class_init (GObjectClass *klass)
563 AnjutaPluginClass *plugin_class = ANJUTA_PLUGIN_CLASS (klass);
565 parent_class = g_type_class_peek_parent (klass);
567 plugin_class->activate = activate_plugin;
568 plugin_class->deactivate = deactivate_plugin;
569 klass->dispose = dispose;
570 klass->finalize = finalize;
573 ANJUTA_PLUGIN_BEGIN (Subversion, subversion);
574 ANJUTA_PLUGIN_END;
576 ANJUTA_SIMPLE_PLUGIN (Subversion, subversion);