Updated Spanish translation
[anjuta-git-plugin.git] / plugins / file-manager / plugin.c
blobe3997b9856796265abcf2b60e24999f54fa7d392
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 * plugin.c
4 * Copyright (C) Johannes Schmid 2007 <jhs@gnome.org>
5 *
6 * plugin.c is free software.
7 *
8 * You may redistribute it and/or modify it under the terms of the
9 * GNU General Public License, as published by the Free Software
10 * Foundation; either version 2 of the License, or (at your option)
11 * any later version.
13 * plugin.c is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 * See the GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with plugin.c. If not, write to:
20 * The Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02110-1301, USA.
25 #include <config.h>
26 #include <glade/glade-xml.h>
27 #include <libgnomevfs/gnome-vfs-utils.h>
28 #include <libanjuta/anjuta-shell.h>
29 #include <libanjuta/anjuta-debug.h>
30 #include <libanjuta/anjuta-preferences.h>
31 #include <libanjuta/interfaces/ianjuta-file-loader.h>
32 #include <libanjuta/interfaces/ianjuta-file-manager.h>
33 #include <libanjuta/interfaces/ianjuta-preferences.h>
34 #include "plugin.h"
36 #define UI_FILE PACKAGE_DATA_DIR"/ui/file-manager.ui"
37 #define ICON_FILE "anjuta-file-manager-plugin-48.png"
38 #define FILE_MANAGER_GLADE PACKAGE_DATA_DIR"/glade/file-manager.glade"
39 #define FILE_MANAGER_GLADE_ROOT "filemanager_prefs"
41 #define PREF_ROOT "filemanager.root"
42 #define PREF_FILTER_BINARY "filemanager.filter.binary"
43 #define PREF_FILTER_HIDDEN "filemanager.filter.hidden"
44 #define PREF_FILTER_BACKUP "filemanager.filter.backup"
46 #define REGISTER_NOTIFY(key, func) \
47 notify_id = anjuta_preferences_notify_add (file_manager->prefs, \
48 key, func, file_manager, NULL); \
49 file_manager->gconf_notify_ids = g_list_prepend (file_manager->gconf_notify_ids, \
50 GUINT_TO_POINTER(notify_id));
52 static gpointer parent_class;
54 typedef struct _ScrollPosition ScrollPosition;
55 struct _ScrollPosition
57 GtkWidget* window;
58 gdouble hvalue;
59 gdouble vvalue;
62 static void
63 on_file_manager_refresh (GtkAction* action, AnjutaFileManager* file_manager)
65 file_view_refresh (file_manager->fv, TRUE);
68 static GtkActionEntry popup_actions[] =
71 "ActionPopupFileManagerRefresh", GTK_STOCK_REFRESH,
72 N_("_Refresh"), NULL, N_("Refresh file manager tree"),
73 G_CALLBACK (on_file_manager_refresh)
77 static void
78 file_manager_set_default_uri (AnjutaFileManager* file_manager)
80 gchar* uri =
81 uri = gnome_vfs_get_uri_from_local_path(anjuta_preferences_get (file_manager->prefs, PREF_ROOT));
82 g_object_set (G_OBJECT (file_manager->fv), "base_uri", uri, NULL);
83 file_manager->have_project = FALSE;
84 g_free (uri);
87 static void
88 project_root_added (AnjutaPlugin *plugin, const gchar *name,
89 const GValue *value, gpointer user_data)
91 AnjutaFileManager* file_manager;
92 const gchar *root_uri;
94 file_manager = (AnjutaFileManager*) plugin;
95 root_uri = g_value_get_string (value);
96 if (root_uri)
98 g_object_set (G_OBJECT(file_manager->fv), "base_uri", root_uri, NULL);
99 file_view_refresh (file_manager->fv, FALSE);
100 file_manager->have_project = TRUE;
102 else
104 file_manager_set_default_uri (file_manager);
105 file_view_refresh (file_manager->fv, FALSE);
109 static void
110 project_root_removed (AnjutaPlugin *plugin, const gchar *name,
111 gpointer user_data)
113 AnjutaFileManager* file_manager = (AnjutaFileManager*) plugin;
114 file_manager_set_default_uri (file_manager);
115 file_view_refresh (file_manager->fv, FALSE);
118 static void
119 on_file_view_current_uri_changed (AnjutaFileView* view, gchar* uri,
120 AnjutaFileManager* file_manager)
122 if (uri)
124 GValue* value;
125 value = g_new0 (GValue, 1);
126 g_value_init (value, G_TYPE_STRING);
127 g_value_take_string (value, uri);
128 anjuta_shell_add_value (ANJUTA_PLUGIN (file_manager)->shell,
129 "file_manager_current_uri", value, NULL);
131 else
133 anjuta_shell_remove_value (ANJUTA_PLUGIN(file_manager)->shell,
134 "file_manager_current_uri", NULL);
138 static void
139 on_file_view_open_file (AnjutaFileView* view, const char *uri,
140 AnjutaFileManager* file_manager)
142 IAnjutaFileLoader *loader;
143 g_return_if_fail (uri != NULL);
144 loader = anjuta_shell_get_interface (ANJUTA_PLUGIN (file_manager)->shell,
145 IAnjutaFileLoader, NULL);
146 g_return_if_fail (loader != NULL);
148 ianjuta_file_loader_load (loader, uri, FALSE, NULL);
151 static void
152 on_file_view_show_popup_menu (AnjutaFileView* view, const gchar* uri,
153 gboolean is_dir,guint button,
154 guint32 time, AnjutaFileManager* file_manager)
156 GtkWidget *popup;
157 AnjutaUI* ui = anjuta_shell_get_ui (ANJUTA_PLUGIN(file_manager)->shell,
158 NULL);
159 popup = gtk_ui_manager_get_widget (GTK_UI_MANAGER (ui),
160 "/PopupFileManager");
161 g_return_if_fail (GTK_IS_WIDGET (popup));
162 gtk_menu_popup (GTK_MENU (popup), NULL, NULL, NULL, NULL, button, time);
165 static void
166 on_gconf_notify(GConfClient *gclient, guint cnxn_id,
167 GConfEntry *entry, gpointer user_data)
169 AnjutaFileManager* file_manager = (AnjutaFileManager*) user_data;
170 GtkTreeModel* model = gtk_tree_view_get_model (GTK_TREE_VIEW (file_manager->fv));
172 g_object_set (G_OBJECT (model),
173 "filter_binary", anjuta_preferences_get_int (file_manager->prefs, PREF_FILTER_BINARY),
174 "filter_hidden", anjuta_preferences_get_int (file_manager->prefs, PREF_FILTER_HIDDEN),
175 "filter_backup", anjuta_preferences_get_int (file_manager->prefs, PREF_FILTER_BACKUP), NULL);
177 if (!file_manager->have_project)
179 file_manager_set_default_uri (file_manager);
180 file_view_refresh (file_manager->fv, FALSE);
182 else
184 file_view_refresh (file_manager->fv, TRUE);
189 static gboolean
190 file_manager_activate (AnjutaPlugin *plugin)
192 AnjutaUI *ui;
193 AnjutaFileManager *file_manager;
194 gint notify_id;
196 DEBUG_PRINT ("AnjutaFileManager: Activating AnjutaFileManager plugin ...");
197 file_manager = (AnjutaFileManager*) plugin;
199 file_manager->prefs = anjuta_shell_get_preferences (plugin->shell, NULL);
201 /* Add all UI actions and merge UI */
202 ui = anjuta_shell_get_ui (plugin->shell, NULL);
204 /* Add action group */
205 file_manager->action_group =
206 anjuta_ui_add_action_group_entries (ui, "ActionGroupFileManager",
207 _("File manager popup actions"),
208 popup_actions, 1,
209 GETTEXT_PACKAGE, FALSE,
210 plugin);
212 file_manager->uiid = anjuta_ui_merge (ui, UI_FILE);
214 file_manager->sw = gtk_scrolled_window_new (NULL, NULL);
215 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (file_manager->sw),
216 GTK_POLICY_AUTOMATIC,
217 GTK_POLICY_AUTOMATIC);
218 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (file_manager->sw),
219 GTK_SHADOW_IN);
221 file_manager->fv = ANJUTA_FILE_VIEW (file_view_new ());
223 g_signal_connect (G_OBJECT (file_manager->fv), "file-open",
224 G_CALLBACK (on_file_view_open_file), file_manager);
225 g_signal_connect (G_OBJECT(file_manager->fv), "show-popup-menu",
226 G_CALLBACK (on_file_view_show_popup_menu), file_manager);
227 g_signal_connect (G_OBJECT(file_manager->fv), "current-uri-changed",
228 G_CALLBACK (on_file_view_current_uri_changed),
229 file_manager);
230 file_manager_set_default_uri (file_manager);
231 file_view_refresh (file_manager->fv, FALSE);
233 gtk_container_add (GTK_CONTAINER (file_manager->sw),
234 GTK_WIDGET (file_manager->fv));
236 gtk_widget_show_all (file_manager->sw);
238 anjuta_shell_add_widget (plugin->shell, file_manager->sw,
239 "AnjutaFileManager",
240 _("Files"), GTK_STOCK_OPEN,
241 ANJUTA_SHELL_PLACEMENT_LEFT, NULL);
243 file_manager->root_watch_id =
244 anjuta_plugin_add_watch (plugin, "project_root_uri",
245 project_root_added,
246 project_root_removed, NULL);
249 REGISTER_NOTIFY (PREF_ROOT, on_gconf_notify);
250 REGISTER_NOTIFY (PREF_FILTER_BINARY, on_gconf_notify);
251 REGISTER_NOTIFY (PREF_FILTER_BACKUP, on_gconf_notify);
252 REGISTER_NOTIFY (PREF_FILTER_HIDDEN, on_gconf_notify);
253 on_gconf_notify (NULL, 0, NULL, file_manager);
255 return TRUE;
258 static gboolean
259 file_manager_deactivate (AnjutaPlugin *plugin)
261 AnjutaUI *ui;
262 AnjutaFileManager *file_manager;
264 DEBUG_PRINT ("AnjutaFileManager: Dectivating AnjutaFileManager plugin ...");
266 file_manager = (AnjutaFileManager*) plugin;
267 ui = anjuta_shell_get_ui (plugin->shell, NULL);
269 anjuta_plugin_remove_watch (plugin, file_manager->root_watch_id, TRUE);
270 anjuta_ui_remove_action_group (ui, ((AnjutaFileManager*)plugin)->action_group);
271 anjuta_ui_unmerge (ui, ((AnjutaFileManager*)plugin)->uiid);
273 anjuta_shell_remove_widget (plugin->shell, file_manager->sw, NULL);
274 return TRUE;
277 static void
278 file_manager_finalize (GObject *obj)
280 AnjutaFileManager *plugin = (AnjutaFileManager*)obj;
281 GList* id;
282 for (id = plugin->gconf_notify_ids; id != NULL; id = id->next)
284 anjuta_preferences_notify_remove(plugin->prefs,GPOINTER_TO_UINT(id->data));
286 g_list_free(plugin->gconf_notify_ids);
288 /* Finalization codes here */
289 G_OBJECT_CLASS (parent_class)->finalize (obj);
292 static void
293 file_manager_dispose (GObject *obj)
295 /* Disposition codes */
296 G_OBJECT_CLASS (parent_class)->dispose (obj);
299 static void
300 file_manager_instance_init (GObject *obj)
302 AnjutaFileManager *plugin = (AnjutaFileManager*)obj;
304 plugin->gconf_notify_ids = NULL;
305 plugin->have_project = FALSE;
307 plugin->uiid = 0;
310 static void
311 file_manager_class_init (GObjectClass *klass)
313 AnjutaPluginClass *plugin_class = ANJUTA_PLUGIN_CLASS (klass);
315 parent_class = g_type_class_peek_parent (klass);
317 plugin_class->activate = file_manager_activate;
318 plugin_class->deactivate = file_manager_deactivate;
320 klass->finalize = file_manager_finalize;
321 klass->dispose = file_manager_dispose;
324 static void
325 ifile_manager_set_root (IAnjutaFileManager *ifile_manager, const gchar *root,
326 GError **err)
328 AnjutaFileManager* file_manager = (AnjutaFileManager*) ifile_manager;
329 g_object_set (G_OBJECT(file_manager->fv), "base_uri", root, NULL);
332 static void
333 ifile_manager_set_selected (IAnjutaFileManager *file_manager,
334 const gchar *root, GError **err)
336 /* TODO */
339 static gchar*
340 ifile_manager_get_selected (IAnjutaFileManager *ifile_manager, GError **err)
342 AnjutaFileManager* file_manager = (AnjutaFileManager*) ifile_manager;
343 return file_view_get_selected (file_manager->fv);
346 static void
347 ifile_manager_iface_init (IAnjutaFileManagerIface *iface)
349 iface->set_root = ifile_manager_set_root;
350 iface->get_selected = ifile_manager_get_selected;
351 iface->set_selected = ifile_manager_set_selected;
354 static void
355 ipreferences_merge (IAnjutaPreferences* ipref,
356 AnjutaPreferences* prefs,
357 GError** e)
359 GladeXML* gxml;
361 gxml = glade_xml_new (FILE_MANAGER_GLADE, FILE_MANAGER_GLADE_ROOT, NULL);
363 anjuta_preferences_add_page (prefs, gxml, FILE_MANAGER_GLADE_ROOT, _("File Manager"),
364 ICON_FILE);
367 static void
368 ipreferences_unmerge (IAnjutaPreferences* ipref,
369 AnjutaPreferences* prefs,
370 GError** e)
372 anjuta_preferences_remove_page (prefs, _("File Manager"));
375 static void
376 ipreferences_iface_init (IAnjutaPreferencesIface* iface)
378 iface->merge = ipreferences_merge;
379 iface->unmerge = ipreferences_unmerge;
382 ANJUTA_PLUGIN_BEGIN (AnjutaFileManager, file_manager);
383 ANJUTA_PLUGIN_ADD_INTERFACE (ifile_manager, IANJUTA_TYPE_FILE_MANAGER);
384 ANJUTA_PLUGIN_ADD_INTERFACE (ipreferences, IANJUTA_TYPE_PREFERENCES);
385 ANJUTA_PLUGIN_END
387 ANJUTA_SIMPLE_PLUGIN (AnjutaFileManager, file_manager);