2006-11-14 Günther Brammer <GBrammer@gmx.de>
[anjuta-git-plugin.git] / plugins / cvs-plugin / plugin.c
blob83b2418d722dfbda2b6eaef1e86946b2479a86ab
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 plugin.c
4 Copyright (C) 2000 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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-preferences.h>
30 #include <libanjuta/interfaces/ianjuta-vcs.h>
32 #include "plugin.h"
33 #include "cvs-actions.h"
34 #include "cvs-interface.h"
36 #define UI_FILE PACKAGE_DATA_DIR"/ui/anjuta-cvs.ui"
37 #define ICON_FILE "anjuta-cvs-plugin.png"
39 static gpointer parent_class;
41 static GtkActionEntry actions_cvs[] = {
43 "ActionMenuCVS", /* Action name */
44 NULL, /* Stock icon, if any */
45 N_("_CVS"), /* Display label */
46 NULL, /* short-cut */
47 NULL, /* Tooltip */
48 NULL
51 "ActionCVSAdd", /* Action name */
52 GTK_STOCK_ADD, /* Stock icon, if any */
53 N_("_Add"), /* Display label */
54 NULL, /* short-cut */
55 N_("Add a new file/directory to the CVS tree"), /* Tooltip */
56 G_CALLBACK (on_menu_cvs_add) /* action callback */
59 "ActionCVSRemove", /* Action name */
60 GTK_STOCK_REMOVE, /* Stock icon, if any */
61 N_("_Remove"), /* Display label */
62 NULL, /* short-cut */
63 N_("Remove a file/directory from CVS tree"), /* Tooltip */
64 G_CALLBACK (on_menu_cvs_remove) /* action callback */
67 "ActionCVSCommit", /* Action name */
68 GTK_STOCK_YES, /* Stock icon, if any */
69 N_("_Commit"), /* Display label */
70 NULL, /* short-cut */
71 N_("Commit your changes to the CVS tree"), /* Tooltip */
72 G_CALLBACK (on_menu_cvs_commit) /* action callback */
75 "ActionCVSUpdate", /* Action name */
76 GTK_STOCK_REFRESH, /* Stock icon, if any */
77 N_("_Update"), /* Display label */
78 "<control>u", /* short-cut */
79 N_("Sync your local copy with the CVS tree"), /* Tooltip */
80 G_CALLBACK (on_menu_cvs_update) /* action callback */
83 "ActionCVSDiff", /* Action name */
84 GTK_STOCK_ZOOM_100, /* Stock icon, if any */
85 N_("_Diff"), /* Display label */
86 NULL, /* short-cut */
87 N_("Show differences between your local copy and the tree"),/*Tooltip*/
88 G_CALLBACK (on_menu_cvs_diff) /* action callback */
91 "ActionCVSStatus", /* Action name */
92 GTK_STOCK_ZOOM_100, /* Stock icon, if any */
93 N_("_Show Status"), /* Display label */
94 NULL, /* short-cut */
95 N_("Show the status of a file/directory"), /* Tooltip */
96 G_CALLBACK (on_menu_cvs_status) /* action callback */
99 "ActionCVSLog", /* Action name */
100 GTK_STOCK_ZOOM_100, /* Stock icon, if any */
101 N_("_Show Log"), /* Display label */
102 NULL, /* short-cut */
103 N_("Show the log of a file/directory"), /* Tooltip */
104 G_CALLBACK (on_menu_cvs_log) /* action callback */
107 "ActionCVSImport", /* Action name */
108 GTK_STOCK_ADD, /* Stock icon, if any */
109 N_("_Import Tree"), /* Display label */
110 NULL, /* short-cut */
111 N_("Import a new source tree to CVS"), /* Tooltip */
112 G_CALLBACK (on_menu_cvs_import) /* action callback */
116 static GtkActionEntry popup_actions_cvs[] = {
118 "ActionPopupCVS", /* Action name */
119 NULL, /* Stock icon, if any */
120 N_("_CVS"), /* Display label */
121 NULL, /* short-cut */
122 NULL, /* Tooltip */
123 NULL
126 "ActionPopupCVSCommit", /* Action name */
127 GTK_STOCK_YES, /* Stock icon, if any */
128 N_("_Commit"), /* Display label */
129 NULL, /* short-cut */
130 N_("Commit your changes to the CVS tree"), /* Tooltip */
131 G_CALLBACK (on_fm_cvs_commit) /* action callback */
134 "ActionPopupCVSUpdate", /* Action name */
135 GTK_STOCK_REFRESH, /* Stock icon, if any */
136 N_("_Update"), /* Display label */
137 NULL, /* short-cut */
138 N_("Sync your local copy with the CVS tree"), /* Tooltip */
139 G_CALLBACK (on_fm_cvs_update) /* action callback */
142 "ActionPopupCVSDiff", /* Action name */
143 GTK_STOCK_ZOOM_100, /* Stock icon, if any */
144 N_("_Diff"), /* Display label */
145 NULL, /* short-cut */
146 N_("Show differences between your local copy and the tree"),/*Tooltip*/
147 G_CALLBACK (on_fm_cvs_diff) /* action callback */
150 "ActionPopupCVSStatus", /* Action name */
151 GTK_STOCK_ZOOM_100, /* Stock icon, if any */
152 N_("_Show Status"), /* Display label */
153 NULL, /* short-cut */
154 N_("Show the status of a file/directory"), /* Tooltip */
155 G_CALLBACK (on_fm_cvs_status) /* action callback */
158 "ActionPopupCVSLog", /* Action name */
159 GTK_STOCK_ZOOM_100, /* Stock icon, if any */
160 N_("_Show Log"), /* Display label */
161 NULL, /* short-cut */
162 N_("Show the log of a file/directory"), /* Tooltip */
163 G_CALLBACK (on_fm_cvs_log) /* action callback */
167 static void
168 value_added_fm_current_uri (AnjutaPlugin *plugin, const char *name,
169 const GValue *value, gpointer data)
171 AnjutaUI *ui;
172 GtkAction *cvs_menu_action;
173 const gchar *uri;
174 GnomeVFSURI *cvs_uri = NULL;
175 gchar *cvs_text_uri = NULL;
176 gchar *cvs_dir;
177 gchar *filename;
178 GnomeVFSDirectoryHandle* handle;
179 GnomeVFSFileInfo info;
180 GnomeVFSResult result;
182 uri = g_value_get_string (value);
183 filename = gnome_vfs_get_local_path_from_uri (uri);
184 g_return_if_fail (filename != NULL);
186 CVSPlugin *cvs_plugin = CVS_PLUGIN (plugin);
187 ui = anjuta_shell_get_ui (plugin->shell, NULL);
189 if (cvs_plugin->fm_current_filename)
190 g_free (cvs_plugin->fm_current_filename);
191 cvs_plugin->fm_current_filename = filename;
193 /* Show popup menu if CVS directory exists */
194 cvs_menu_action = anjuta_ui_get_action (ui, "ActionGroupPopupCVS", "ActionPopupCVS");
196 /* If a directory is selected we check if it contains a "CVS" directory,
197 if it is a file we check if it's directory contains a "CVS" directory */
198 result = gnome_vfs_get_file_info(uri, &info,
199 GNOME_VFS_FILE_INFO_DEFAULT);
200 if (result == GNOME_VFS_OK)
202 if (info.type == GNOME_VFS_FILE_TYPE_DIRECTORY)
204 cvs_dir = g_strconcat(uri, "/CVS", NULL);
207 else
209 cvs_uri = gnome_vfs_uri_new (uri);
210 cvs_text_uri = gnome_vfs_uri_extract_dirname(cvs_uri);
211 cvs_dir = g_strconcat(cvs_text_uri, "/CVS", NULL);
212 g_free(cvs_text_uri);
213 gnome_vfs_uri_unref(cvs_uri);
216 else
217 return; /* Strange... */
218 if (gnome_vfs_directory_open(&handle, cvs_dir,
219 GNOME_VFS_FILE_INFO_DEFAULT) == GNOME_VFS_OK)
222 g_object_set (G_OBJECT (cvs_menu_action), "sensitive", TRUE, NULL);
224 else
226 g_object_set (G_OBJECT (cvs_menu_action), "sensitive", FALSE, NULL);
228 g_free (cvs_dir);
231 static void
232 value_removed_fm_current_uri (AnjutaPlugin *plugin,
233 const char *name, gpointer data)
235 AnjutaUI *ui;
236 GtkAction *action;
238 CVSPlugin *cvs_plugin = CVS_PLUGIN (plugin);
240 if (cvs_plugin->fm_current_filename)
241 g_free (cvs_plugin->fm_current_filename);
242 cvs_plugin->fm_current_filename = NULL;
244 ui = anjuta_shell_get_ui (plugin->shell, NULL);
245 action = anjuta_ui_get_action (ui, "ActionGroupPopupCVS", "ActionPopupCVS");
246 g_object_set (G_OBJECT (action), "sensitive", FALSE, NULL);
250 static void
251 value_added_project_root_uri (AnjutaPlugin *plugin, const gchar *name,
252 const GValue *value, gpointer user_data)
254 CVSPlugin *bb_plugin;
255 const gchar *root_uri;
257 bb_plugin = CVS_PLUGIN (plugin);
259 DEBUG_PRINT ("Project added");
261 if (bb_plugin->project_root_dir)
262 g_free (bb_plugin->project_root_dir);
263 bb_plugin->project_root_dir = NULL;
265 root_uri = g_value_get_string (value);
266 if (root_uri)
268 bb_plugin->project_root_dir =
269 gnome_vfs_get_local_path_from_uri (root_uri);
270 if (bb_plugin->project_root_dir)
272 // update_project_ui (bb_plugin);
277 static void
278 value_removed_project_root_uri (AnjutaPlugin *plugin, const gchar *name,
279 gpointer user_data)
281 CVSPlugin *bb_plugin;
283 bb_plugin = CVS_PLUGIN (plugin);
284 if (bb_plugin->project_root_dir)
285 g_free (bb_plugin->project_root_dir);
286 bb_plugin->project_root_dir = NULL;
287 // update_project_ui (bb_plugin);
290 static void
291 value_added_current_editor (AnjutaPlugin *plugin, const char *name,
292 const GValue *value, gpointer data)
294 AnjutaUI *ui;
295 gchar *uri;
296 GObject *editor;
298 editor = g_value_get_object (value);
300 CVSPlugin *cvs_plugin = CVS_PLUGIN (plugin);
301 ui = anjuta_shell_get_ui (plugin->shell, NULL);
303 if (cvs_plugin->current_editor_filename)
304 g_free (cvs_plugin->current_editor_filename);
305 cvs_plugin->current_editor_filename = NULL;
307 uri = ianjuta_file_get_uri (IANJUTA_FILE (editor), NULL);
308 if (uri)
310 gchar *filename;
312 filename = gnome_vfs_get_local_path_from_uri (uri);
313 g_return_if_fail (filename != NULL);
314 cvs_plugin->current_editor_filename = filename;
315 g_free (uri);
316 // update_module_ui (cvs_plugin);
320 static void
321 value_removed_current_editor (AnjutaPlugin *plugin,
322 const char *name, gpointer data)
324 CVSPlugin *cvs_plugin = CVS_PLUGIN (plugin);
326 if (cvs_plugin->current_editor_filename)
327 g_free (cvs_plugin->current_editor_filename);
328 cvs_plugin->current_editor_filename = NULL;
330 // update_module_ui (cvs_plugin);
333 static gboolean
334 activate_plugin (AnjutaPlugin *plugin)
336 AnjutaUI *ui;
337 CVSPlugin *cvs_plugin;
339 DEBUG_PRINT ("CVSPlugin: Activating CVS plugin ...");
340 cvs_plugin = CVS_PLUGIN (plugin);
342 ui = anjuta_shell_get_ui (plugin->shell, NULL);
344 /* Add all our actions */
345 cvs_plugin->cvs_action_group =
346 anjuta_ui_add_action_group_entries (ui, "ActionGroupCVS",
347 _("CVS operations"),
348 actions_cvs,
349 G_N_ELEMENTS (actions_cvs),
350 GETTEXT_PACKAGE, TRUE, plugin);
351 cvs_plugin->cvs_popup_action_group =
352 anjuta_ui_add_action_group_entries (ui, "ActionGroupPopupCVS",
353 _("CVS popup operations"),
354 popup_actions_cvs,
355 G_N_ELEMENTS (popup_actions_cvs),
356 GETTEXT_PACKAGE, FALSE, plugin);
357 /* UI merge */
358 cvs_plugin->uiid = anjuta_ui_merge (ui, UI_FILE);
360 /* Add watches */
361 cvs_plugin->fm_watch_id =
362 anjuta_plugin_add_watch (plugin, "file_manager_current_uri",
363 value_added_fm_current_uri,
364 value_removed_fm_current_uri, NULL);
365 cvs_plugin->project_watch_id =
366 anjuta_plugin_add_watch (plugin, "project_root_uri",
367 value_added_project_root_uri,
368 value_removed_project_root_uri, NULL);
369 cvs_plugin->editor_watch_id =
370 anjuta_plugin_add_watch (plugin, "document_manager_current_editor",
371 value_added_current_editor,
372 value_removed_current_editor, NULL);
373 return TRUE;
376 static gboolean
377 deactivate_plugin (AnjutaPlugin *plugin)
379 AnjutaUI *ui;
380 CVSPlugin *cvs_plugin;
382 DEBUG_PRINT ("CVSPlugin: Dectivating CVS plugin ...");
384 cvs_plugin = CVS_PLUGIN (plugin);
385 ui = anjuta_shell_get_ui (plugin->shell, NULL);
387 /* Remove watches */
388 anjuta_plugin_remove_watch (plugin, cvs_plugin->fm_watch_id, TRUE);
389 anjuta_plugin_remove_watch (plugin, cvs_plugin->project_watch_id, TRUE);
390 anjuta_plugin_remove_watch (plugin, cvs_plugin->editor_watch_id, TRUE);
392 /* Unmerge UI */
393 anjuta_ui_unmerge (ui, cvs_plugin->uiid);
395 /* Remove action groups */
396 anjuta_ui_remove_action_group (ui, cvs_plugin->cvs_action_group);
397 anjuta_ui_remove_action_group (ui, cvs_plugin->cvs_popup_action_group);
399 return TRUE;
402 static void
403 finalize (GObject *obj)
405 // CVSPlugin *plugin = CVS_PLUGIN (obj);
406 GNOME_CALL_PARENT (G_OBJECT_CLASS, finalize, (G_OBJECT(obj)));
409 static void
410 dispose (GObject *obj)
412 // CVSPlugin *plugin = CVS_PLUGIN (obj);
413 GNOME_CALL_PARENT (G_OBJECT_CLASS, dispose, (G_OBJECT(obj)));
416 static void
417 cvs_plugin_instance_init (GObject *obj)
419 CVSPlugin *plugin = CVS_PLUGIN (obj);
420 plugin->uiid = 0;
421 plugin->executing_command = FALSE;
422 plugin->mesg_view = NULL;
423 plugin->launcher = NULL;
424 plugin->diff_editor = NULL;
425 plugin->fm_current_filename = NULL;
426 plugin->project_root_dir = NULL;
427 plugin->current_editor_filename = NULL;
430 static void
431 cvs_plugin_class_init (GObjectClass *klass)
433 AnjutaPluginClass *plugin_class = ANJUTA_PLUGIN_CLASS (klass);
435 parent_class = g_type_class_peek_parent (klass);
437 plugin_class->activate = activate_plugin;
438 plugin_class->deactivate = deactivate_plugin;
439 klass->dispose = dispose;
440 klass->finalize = finalize;
443 /* Interface */
445 static void
446 ianjuta_cvs_add (IAnjutaVcs *obj, const gchar* filename,
447 GError **err)
449 anjuta_cvs_add(ANJUTA_PLUGIN(obj), filename, FALSE, err);
452 static void
453 ianjuta_cvs_commit (IAnjutaVcs *obj, const gchar* filename, const gchar* log,
454 gboolean recurse, GError **err)
456 anjuta_cvs_commit (ANJUTA_PLUGIN(obj), filename, log, "", recurse, err);
459 static void
460 ianjuta_cvs_remove (IAnjutaVcs *obj, const gchar* filename, GError **err)
462 anjuta_cvs_remove (ANJUTA_PLUGIN(obj), filename, err);
466 static void
467 ianjuta_cvs_update (IAnjutaVcs *obj, const gchar* filename, gboolean recurse, GError **err)
469 anjuta_cvs_update(ANJUTA_PLUGIN(obj), filename, recurse, FALSE, TRUE, FALSE,"", err);}
471 static void
472 ianjuta_vcs_iface_init (IAnjutaVcsIface *iface)
474 iface->add = ianjuta_cvs_add;
475 iface->remove = ianjuta_cvs_remove;
476 iface->update = ianjuta_cvs_update;
477 iface->commit = ianjuta_cvs_commit;
480 static void
481 ipreferences_merge(IAnjutaPreferences* ipref, AnjutaPreferences* prefs, GError** e)
483 /* Create the messages preferences page */
484 GladeXML* gxml;
485 gxml = glade_xml_new (GLADE_FILE, "cvs", NULL);
486 anjuta_preferences_add_page (prefs, gxml, "cvs", _("CVS"), ICON_FILE);
487 g_object_unref (gxml);
490 static void
491 ipreferences_unmerge(IAnjutaPreferences* ipref, AnjutaPreferences* prefs, GError** e)
493 anjuta_preferences_dialog_remove_page(ANJUTA_PREFERENCES_DIALOG(prefs),
494 _("CVS"));
497 static void
498 ipreferences_iface_init(IAnjutaPreferencesIface* iface)
500 iface->merge = ipreferences_merge;
501 iface->unmerge = ipreferences_unmerge;
504 ANJUTA_PLUGIN_BEGIN (CVSPlugin, cvs_plugin);
505 ANJUTA_PLUGIN_ADD_INTERFACE(ianjuta_vcs, IANJUTA_TYPE_VCS);
506 ANJUTA_PLUGIN_ADD_INTERFACE(ipreferences, IANJUTA_TYPE_PREFERENCES);
507 ANJUTA_PLUGIN_END;
509 ANJUTA_SIMPLE_PLUGIN (CVSPlugin, cvs_plugin);