Add progress handling to GitCommand
[anjuta-git-plugin.git] / plugins / file-loader / plugin.c
blob58cc41e10e12ce38a05b8d88fb17cbada0d62669
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 plugin.c
4 Copyright (C) 2004 Naba Kumar
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 <config.h>
22 #include <libgnomevfs/gnome-vfs-mime-utils.h>
23 #include <libgnomevfs/gnome-vfs-mime.h>
24 #include <libgnomevfs/gnome-vfs-mime-handlers.h>
25 #include <libgnomevfs/gnome-vfs-ops.h>
27 #include <glib/gi18n.h>
29 #include <libanjuta/anjuta-shell.h>
30 #include <libanjuta/anjuta-status.h>
31 #include <libanjuta/anjuta-debug.h>
33 #include <libanjuta/interfaces/ianjuta-file.h>
34 #include <libanjuta/interfaces/ianjuta-file-loader.h>
35 #include <libanjuta/interfaces/ianjuta-document-manager.h>
36 #include <libanjuta/interfaces/ianjuta-wizard.h>
38 #include "plugin.h"
39 #include "dnd.h"
40 #include "anjuta-recent-chooser-menu.h"
42 #define UI_FILE PACKAGE_DATA_DIR"/ui/anjuta-loader-plugin.ui"
44 static gpointer parent_class;
46 static int
47 sort_wizards(gconstpointer wizard1, gconstpointer wizard2)
49 gchar* name1, *name2;
50 AnjutaPluginDescription* desc1 = (AnjutaPluginDescription*) wizard1;
51 AnjutaPluginDescription* desc2 = (AnjutaPluginDescription*) wizard2;
53 if ((anjuta_plugin_description_get_locale_string (desc1, "Wizard",
54 "Title", &name1) ||
55 anjuta_plugin_description_get_locale_string (desc1, "Anjuta Plugin",
56 "Name", &name1)) &&
57 (anjuta_plugin_description_get_locale_string (desc2, "Wizard",
58 "Title", &name2) ||
59 anjuta_plugin_description_get_locale_string (desc2, "Anjuta Plugin",
60 "Name", &name2)))
62 return strcmp(name1, name2);
64 else
65 return 0;
68 static void
69 set_recent_file (AnjutaFileLoaderPlugin *plugin, const gchar *uri,
70 const gchar *mime)
72 GtkRecentData *recent_data;
73 gchar *name;
75 DEBUG_PRINT ("Adding recent item of mimi-type: %s", mime);
77 name = g_path_get_basename (uri);
79 recent_data = g_slice_new (GtkRecentData);
81 recent_data->display_name = name;
82 recent_data->description = NULL;
83 recent_data->mime_type = (gchar *)mime;
84 recent_data->app_name = (gchar *) g_get_application_name ();
85 recent_data->app_exec = g_strjoin (" ", g_get_prgname (), "%u", NULL);
86 recent_data->groups = NULL;
87 recent_data->is_private = FALSE;
89 if (!gtk_recent_manager_add_full (plugin->recent_manager, uri, recent_data))
91 g_warning ("Unable to add '%s' to the list of recently used documents", uri);
94 g_free (name);
95 g_free (recent_data->app_exec);
96 g_slice_free (GtkRecentData, recent_data);
99 static void
100 launch_application_failure (AnjutaFileLoaderPlugin *plugin,
101 const gchar *uri,
102 GnomeVFSResult result)
104 const gchar *errmsg;
105 GtkWidget *parent;
107 errmsg = gnome_vfs_result_to_string (result);
108 parent =
109 gtk_widget_get_toplevel (GTK_WIDGET(ANJUTA_PLUGIN (plugin)->shell));
110 anjuta_util_dialog_error (GTK_WINDOW (parent),
111 "Can not open \"%s\".\n\n%s",
112 g_basename (uri), errmsg);
115 static GList *
116 get_available_plugins_for_mime (AnjutaFileLoaderPlugin* plugin,
117 const gchar *mime_type)
119 AnjutaPluginManager *plugin_manager;
120 GList *plugin_descs = NULL;
122 g_return_val_if_fail (mime_type != NULL, NULL);
124 plugin_manager = anjuta_shell_get_plugin_manager (ANJUTA_PLUGIN(plugin)->shell,
125 NULL);
126 plugin_descs = anjuta_plugin_manager_query (plugin_manager,
127 "Anjuta Plugin",
128 "Interfaces", "IAnjutaFile",
129 "File Loader",
130 "SupportedMimeTypes",
131 mime_type,
132 NULL);
133 if (!plugin_descs);
135 gchar* supertype = gnome_vfs_get_supertype_from_mime_type (mime_type);
136 plugin_descs = anjuta_plugin_manager_query (plugin_manager,
137 "Anjuta Plugin",
138 "Interfaces", "IAnjutaFile",
139 "File Loader",
140 "SupportedMimeTypes",
141 supertype,
142 NULL);
143 g_free (supertype);
146 return plugin_descs;
149 static void
150 open_with_dialog (AnjutaFileLoaderPlugin *plugin, const gchar *uri,
151 const gchar *mime_type)
153 GList *plugin_descs, *snode;
154 GList *mime_apps, *node;
155 GtkWidget *menu, *menuitem;
156 GnomeVFSMimeApplication *mime_app;
158 GtkWidget *dialog, *parent, *hbox, *label;
159 GtkWidget *options;
160 gchar *message;
161 AnjutaPluginManager *plugin_manager;
163 plugin_manager = anjuta_shell_get_plugin_manager (ANJUTA_PLUGIN (plugin)->shell,
164 NULL);
165 message = g_strdup_printf (_("<b>Cannot open \"%s\"</b>.\n\n"
166 "There is no plugin, default action, or application "
167 "configured to handle this file type.\n"
168 "\n"
169 "Mime type: %s\n"
170 "\n"
171 "You may choose to try opening it with the following "
172 "plugins or applications."),
173 g_basename(uri), mime_type);
174 parent =
175 gtk_widget_get_toplevel (GTK_WIDGET(ANJUTA_PLUGIN (plugin)->shell));
176 dialog = gtk_message_dialog_new_with_markup (GTK_WINDOW (parent),
177 GTK_DIALOG_DESTROY_WITH_PARENT,
178 GTK_MESSAGE_INFO,
179 GTK_BUTTONS_OK_CANCEL, message);
180 g_free (message);
182 hbox = gtk_hbox_new (FALSE, 5);
183 gtk_box_pack_start (GTK_BOX (GTK_DIALOG(dialog)->vbox), hbox,
184 FALSE, FALSE, 5);
185 label = gtk_label_new (_("Open with:"));
186 options = gtk_option_menu_new ();
187 gtk_box_pack_end (GTK_BOX(hbox), options, FALSE, FALSE, 10);
188 gtk_box_pack_end (GTK_BOX(hbox), label, FALSE, FALSE, 10);
190 menu = gtk_menu_new ();
192 /* Document manager plugin */
193 menuitem = gtk_menu_item_new_with_label (_("Document Manager"));
194 gtk_menu_append (menu, menuitem);
196 /* Open with plugins menu items */
197 plugin_descs = get_available_plugins_for_mime (plugin, mime_type);
198 snode = plugin_descs;
199 while (snode)
201 gchar *name;
202 AnjutaPluginDescription *desc;
204 desc = (AnjutaPluginDescription *)(snode->data);
206 name = NULL;
208 anjuta_plugin_description_get_locale_string (desc, "File Loader",
209 "Title", &name);
211 if (!name)
213 anjuta_plugin_description_get_locale_string (desc, "Anjuta Plugin",
214 "Name", &name);
216 if (!name)
218 anjuta_plugin_description_get_string (desc, "Anjuta Plugin",
219 "Location", &name);
221 menuitem = gtk_menu_item_new_with_label (name);
222 gtk_menu_append (menu, menuitem);
223 g_free (name);
224 snode = g_list_next (snode);
227 /* Open with application menu items */
228 mime_apps = gnome_vfs_mime_get_all_applications (mime_type);
229 if (mime_apps)
231 /* Separator */
232 menuitem = gtk_menu_item_new ();
233 gtk_menu_append (menu, menuitem);
235 node = mime_apps;
236 while (node)
238 mime_app = (GnomeVFSMimeApplication *)(node->data);
239 menuitem = gtk_menu_item_new_with_label (mime_app->name);
240 gtk_menu_append (menu, menuitem);
241 node = g_list_next (node);
244 gtk_option_menu_set_menu (GTK_OPTION_MENU (options), menu);
245 gtk_widget_show_all (hbox);
247 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_OK)
249 gint option;
251 option = gtk_option_menu_get_history(GTK_OPTION_MENU (options));
252 if (option == 0)
254 IAnjutaDocumentManager *docman;
255 docman = anjuta_shell_get_interface (ANJUTA_PLUGIN (plugin)->shell,
256 IAnjutaDocumentManager,
257 NULL);
258 if (docman)
260 ianjuta_file_open (IANJUTA_FILE (docman), uri, NULL);
262 else
264 g_warning ("No document manager plugin!!");
267 else if (option < (g_list_length (plugin_descs) + 1))
269 AnjutaPluginDescription *desc;
270 gchar *location = NULL;
272 option--;
273 desc = g_list_nth_data (plugin_descs, option);
274 anjuta_plugin_description_get_string (desc, "Anjuta Plugin",
275 "Location", &location);
276 g_assert (location != NULL);
277 if (location != NULL)
279 GObject *loaded_plugin;
281 loaded_plugin =
282 anjuta_plugin_manager_get_plugin_by_id (plugin_manager,
283 location);
284 if (loaded_plugin)
286 ianjuta_file_open (IANJUTA_FILE (loaded_plugin), uri, NULL);
287 set_recent_file (plugin, uri, mime_type);
289 else
291 anjuta_util_dialog_error (GTK_WINDOW (ANJUTA_PLUGIN(plugin)->shell),
292 "Failed to activate plugin: %s",
293 location);
295 g_free (location);
298 else
300 GList *uris = NULL;
301 GnomeVFSResult res;
303 option -= (g_list_length (plugin_descs) + 2);
304 mime_app = g_list_nth_data (mime_apps, option);
305 uris = g_list_prepend (uris, (gpointer)uri);
306 res = gnome_vfs_mime_application_launch (mime_app, uris);
307 if (res != GNOME_VFS_OK)
308 launch_application_failure (plugin, uri, res);
309 else
310 set_recent_file (plugin, uri, mime_type);
311 g_list_free (uris);
314 gnome_vfs_mime_application_list_free (mime_apps);
315 if (plugin_descs)
316 g_list_free (plugin_descs);
317 gtk_widget_destroy (dialog);
320 static gboolean
321 launch_in_default_application (AnjutaFileLoaderPlugin *plugin,
322 const gchar *mime_type, const gchar *uri)
324 GnomeVFSMimeAction *action;
325 GList *uris = NULL;
326 gboolean ret;
328 uris = g_list_prepend (uris, (gpointer)uri);
330 ret = TRUE;
331 action = gnome_vfs_mime_get_default_action (mime_type);
332 if (!action || gnome_vfs_mime_action_launch (action, uris) != GNOME_VFS_OK)
334 GnomeVFSMimeApplication *app;
335 GnomeVFSResult res;
336 app = gnome_vfs_mime_get_default_application (mime_type);
337 if (!app ||
338 (res =
339 gnome_vfs_mime_application_launch (app, uris)) != GNOME_VFS_OK)
341 open_with_dialog (plugin, uri, mime_type);
343 if (app)
344 gnome_vfs_mime_application_free (app);
346 if (action)
347 gnome_vfs_mime_action_free (action);
348 g_list_free (uris);
349 return ret;
352 static void
353 open_file (AnjutaFileLoaderPlugin *plugin, const gchar *uri)
355 GnomeVFSURI *vfs_uri;
356 gchar *dirname;
358 vfs_uri = gnome_vfs_uri_new (uri);
359 dirname = gnome_vfs_uri_extract_dirname (vfs_uri);
360 gnome_vfs_uri_unref (vfs_uri);
361 chdir (dirname);
362 g_free (dirname);
364 /* FIXME: We have to manage the error to know if we have to remove the recent file
366 ianjuta_file_loader_load (IANJUTA_FILE_LOADER (plugin),
367 uri, FALSE, NULL);
370 typedef struct
372 AnjutaFileLoaderPlugin *plugin;
373 const gchar *uri;
374 } RecentIdelOpenData;
376 static gboolean
377 on_open_recent_file_idle (gpointer data)
379 RecentIdelOpenData *rdata;
381 rdata = (RecentIdelOpenData*)data;
382 open_file (rdata->plugin, rdata->uri);
383 g_free (rdata);
384 return FALSE;
387 static gboolean
388 on_open_recent_file (GtkRecentChooser *chooser, AnjutaFileLoaderPlugin *plugin)
390 const gchar *uri;
391 GnomeVFSURI *vfs_uri;
392 gboolean ret = TRUE;
393 RecentIdelOpenData *rdata;
395 uri = gtk_recent_chooser_get_current_uri (chooser);
396 vfs_uri = gnome_vfs_uri_new (uri);
397 rdata = g_new0 (RecentIdelOpenData, 1);
398 rdata->plugin = plugin;
399 rdata->uri = uri;
400 g_idle_add (on_open_recent_file_idle, rdata);
401 gnome_vfs_uri_unref (vfs_uri);
403 return ret;
406 static void
407 on_open_response_ok (GtkDialog* dialog, gint id,
408 AnjutaFileLoaderPlugin *plugin)
410 GSList *list, *node;
412 if (id != GTK_RESPONSE_ACCEPT)
414 gtk_widget_destroy (GTK_WIDGET (dialog));
415 return;
418 list = gtk_file_chooser_get_uris (GTK_FILE_CHOOSER (dialog));
419 node = list;
420 while (node)
422 if (node->data)
423 open_file (plugin, (gchar *)node->data);
424 g_free (node->data);
425 node = g_slist_next (node);
427 g_slist_free (list);
430 static void
431 setup_file_filters (GtkFileChooser *fc)
433 GtkFileFilter *filter;
435 filter = gtk_file_filter_new ();
436 gtk_file_filter_set_name (filter, _("All files"));
437 gtk_file_filter_add_pattern (filter, "*");
438 gtk_file_chooser_add_filter (fc, filter);
440 filter = gtk_file_filter_new ();
441 gtk_file_filter_set_name (filter, _("Anjuta Projects"));
442 gtk_file_filter_add_pattern (filter, "*.anjuta");
443 gtk_file_filter_add_pattern (filter, "*.prj");
444 gtk_file_chooser_add_filter (fc, filter);
446 filter = gtk_file_filter_new ();
447 gtk_file_filter_set_name (filter, _("C/C++ source files"));
448 gtk_file_filter_add_pattern (filter, "*.c");
449 gtk_file_filter_add_pattern (filter, "*.cc");
450 gtk_file_filter_add_pattern (filter, "*.cpp");
451 gtk_file_filter_add_pattern (filter, "*.cxx");
452 gtk_file_filter_add_pattern (filter, "*.c++");
453 gtk_file_filter_add_pattern (filter, "*.h");
454 gtk_file_filter_add_pattern (filter, "*.hh");
455 gtk_file_filter_add_pattern (filter, "*.hpp");
456 gtk_file_chooser_add_filter (fc, filter);
458 filter = gtk_file_filter_new ();
459 gtk_file_filter_set_name (filter, _("C# source files"));
460 gtk_file_filter_add_pattern (filter, "*.cs");
461 gtk_file_filter_add_pattern (filter, "*.h");
462 gtk_file_chooser_add_filter (fc, filter);
464 filter = gtk_file_filter_new ();
465 gtk_file_filter_set_name (filter, _("Java source files"));
466 gtk_file_filter_add_pattern (filter, "*.java");
467 gtk_file_filter_add_pattern (filter, "*.js");
468 gtk_file_chooser_add_filter (fc, filter);
470 filter = gtk_file_filter_new ();
471 gtk_file_filter_set_name (filter, _("Pascal source files"));
472 gtk_file_filter_add_pattern (filter, "*.pas");
473 gtk_file_chooser_add_filter (fc, filter);
475 filter = gtk_file_filter_new ();
476 gtk_file_filter_set_name (filter, _("PHP source files"));
477 gtk_file_filter_add_pattern (filter, "*.php");
478 gtk_file_filter_add_pattern (filter, "*.php?");
479 gtk_file_filter_add_pattern (filter, "*.phtml");
480 gtk_file_chooser_add_filter (fc, filter);
482 filter = gtk_file_filter_new ();
483 gtk_file_filter_set_name (filter, _("Perl source files"));
484 gtk_file_filter_add_pattern (filter, "*.pl");
485 gtk_file_filter_add_pattern (filter, "*.pm");
486 gtk_file_chooser_add_filter (fc, filter);
488 filter = gtk_file_filter_new ();
489 gtk_file_filter_set_name (filter, _("Python source files"));
490 gtk_file_filter_add_pattern (filter, "*.py");
491 gtk_file_chooser_add_filter (fc, filter);
493 filter = gtk_file_filter_new ();
494 gtk_file_filter_set_name (filter, _("Hyper text markup files"));
495 gtk_file_filter_add_pattern (filter, "*.htm");
496 gtk_file_filter_add_pattern (filter, "*.html");
497 gtk_file_filter_add_pattern (filter, "*.xhtml");
498 gtk_file_filter_add_pattern (filter, "*.dhtml");
499 gtk_file_filter_add_pattern (filter, "*.cs");
500 gtk_file_chooser_add_filter (fc, filter);
502 filter = gtk_file_filter_new ();
503 gtk_file_filter_set_name (filter, _("Shell scripts files"));
504 gtk_file_filter_add_pattern (filter, "*.sh");
505 gtk_file_chooser_add_filter (fc, filter);
507 filter = gtk_file_filter_new ();
508 gtk_file_filter_set_name (filter, _("Makefiles"));
509 gtk_file_filter_add_pattern (filter, "Makefile*");
510 gtk_file_filter_add_pattern (filter, "makefile*");
511 gtk_file_chooser_add_filter (fc, filter);
513 filter = gtk_file_filter_new ();
514 gtk_file_filter_set_name (filter, _("Lua files"));
515 gtk_file_filter_add_pattern (filter, "*.lua");
516 gtk_file_chooser_add_filter (fc, filter);
518 filter = gtk_file_filter_new ();
519 gtk_file_filter_set_name (filter, _("Diff files"));
520 gtk_file_filter_add_pattern (filter, "*.diff");
521 gtk_file_filter_add_pattern (filter, "*.patch");
522 gtk_file_filter_add_pattern (filter, "*.cvsdiff");
523 gtk_file_chooser_add_filter (fc, filter);
526 static GtkWidget*
527 create_file_open_dialog_gui(GtkWindow* parent, AnjutaFileLoaderPlugin* plugin)
529 GtkWidget* dialog =
530 gtk_file_chooser_dialog_new (_("Open file"),
531 parent,
532 GTK_FILE_CHOOSER_ACTION_OPEN,
533 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
534 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
535 NULL);
536 gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER(dialog), TRUE);
537 gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (dialog), FALSE);
538 setup_file_filters (GTK_FILE_CHOOSER (dialog));
540 g_signal_connect(G_OBJECT(dialog), "response",
541 G_CALLBACK(on_open_response_ok), plugin);
542 g_signal_connect_swapped (GTK_OBJECT (dialog),
543 "response",
544 G_CALLBACK (gtk_widget_destroy),
545 GTK_OBJECT (dialog));
546 return dialog;
549 static void
550 on_new_activate (GtkAction *action, AnjutaFileLoaderPlugin *plugin)
552 AnjutaShell* shell = ANJUTA_PLUGIN (plugin)->shell;
553 IAnjutaDocumentManager *docman = anjuta_shell_get_interface (shell,
554 IAnjutaDocumentManager,
555 NULL);
556 if (docman)
557 ianjuta_document_manager_add_buffer (docman, NULL, NULL, NULL);
560 static void
561 on_open_activate (GtkAction *action, AnjutaFileLoaderPlugin *plugin)
563 GtkWidget *dlg;
565 dlg =
566 create_file_open_dialog_gui (GTK_WINDOW (ANJUTA_PLUGIN(plugin)->shell),
567 plugin);
568 gtk_widget_show (dlg);
571 static void
572 on_activate_wizard (GtkMenuItem *menuitem,
573 AnjutaFileLoaderPlugin *loader)
575 AnjutaPluginManager *plugin_manager;
576 AnjutaPluginDescription *desc;
578 desc = g_object_get_data (G_OBJECT (menuitem), "__plugin_desc");
579 plugin_manager = anjuta_shell_get_plugin_manager (ANJUTA_PLUGIN (loader)->shell,
580 NULL);
581 if (desc)
583 gchar *id;
584 GObject *plugin;
586 if (anjuta_plugin_description_get_string (desc, "Anjuta Plugin",
587 "Location", &id))
589 plugin =
590 anjuta_plugin_manager_get_plugin_by_id (plugin_manager, id);
591 ianjuta_wizard_activate (IANJUTA_WIZARD (plugin), NULL);
596 static GtkWidget*
597 on_create_submenu (gpointer user_data)
599 AnjutaPluginManager *plugin_manager;
600 AnjutaFileLoaderPlugin *loader;
601 GList *node;
602 gint count;
603 GtkWidget *submenu = NULL;
604 GList *plugin_descs = NULL;
606 loader = ANJUTA_PLUGIN_FILE_LOADER (user_data);
607 plugin_manager = anjuta_shell_get_plugin_manager (ANJUTA_PLUGIN (loader)->shell,
608 NULL);
609 submenu = gtk_menu_new ();
610 gtk_widget_show (submenu);
612 plugin_descs = anjuta_plugin_manager_query (plugin_manager,
613 "Anjuta Plugin",
614 "Interfaces", "IAnjutaWizard",
615 NULL);
616 plugin_descs = g_list_sort(plugin_descs, sort_wizards);
617 node = plugin_descs;
618 count = 0;
619 while (node)
621 AnjutaPluginDescription *desc;
622 GtkWidget *menuitem;
623 GtkWidget *icon;
624 gchar *str, *icon_path, *name;
626 desc = node->data;
628 icon = NULL;
629 name = NULL;
630 if (anjuta_plugin_description_get_locale_string (desc, "Wizard",
631 "Title", &str) ||
632 anjuta_plugin_description_get_locale_string (desc, "Anjuta Plugin",
633 "Name", &str))
635 count++;
636 if (count < 10)
637 name = g_strdup_printf ("_%d. %s", count, N_(str));
638 else
639 name = g_strdup_printf ("%d. %s", count, N_(str));
640 g_free (str);
642 if (anjuta_plugin_description_get_string (desc, "Anjuta Plugin",
643 "Icon", &str))
645 GdkPixbuf *pixbuf, *scaled_pixbuf;
646 gint height, width;
648 gtk_icon_size_lookup_for_settings (gtk_widget_get_settings (submenu),
649 GTK_ICON_SIZE_MENU,
650 &width, &height);
651 icon_path = g_build_filename (PACKAGE_PIXMAPS_DIR, str, NULL);
652 pixbuf = gdk_pixbuf_new_from_file (icon_path, NULL);
653 if (pixbuf)
655 scaled_pixbuf = gdk_pixbuf_scale_simple (pixbuf, width, height,
656 GDK_INTERP_BILINEAR);
657 icon = gtk_image_new_from_pixbuf (scaled_pixbuf);
658 g_object_unref (pixbuf);
659 g_object_unref (scaled_pixbuf);
661 else
662 icon = gtk_image_new ();
664 gtk_widget_show (icon);
665 g_free (icon_path);
666 g_free (str);
668 if (name)
670 menuitem = gtk_image_menu_item_new_with_mnemonic (name);
671 g_free(name);
672 gtk_widget_show (menuitem);
673 g_object_set_data (G_OBJECT (menuitem), "__plugin_desc", desc);
674 g_signal_connect (G_OBJECT (menuitem), "activate",
675 G_CALLBACK (on_activate_wizard),
676 loader);
677 if (icon)
678 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem),
679 icon);
680 gtk_menu_shell_append (GTK_MENU_SHELL (submenu), menuitem);
682 node = g_list_next (node);
684 g_list_free (plugin_descs);
685 return submenu;
688 static void
689 open_file_with (AnjutaFileLoaderPlugin *plugin, GtkMenuItem *menuitem,
690 const gchar *uri)
692 GList *mime_apps;
693 GnomeVFSMimeApplication *mime_app;
694 gchar *mime_type;
695 gint idx;
696 AnjutaPluginDescription *desc;
697 AnjutaPluginManager *plugin_manager;
699 idx = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (menuitem), "index"));
700 desc = (AnjutaPluginDescription*) g_object_get_data (G_OBJECT (menuitem),
701 "desc");
702 plugin_manager = anjuta_shell_get_plugin_manager (ANJUTA_PLUGIN (plugin)->shell,
703 NULL);
704 mime_type = anjuta_util_get_uri_mime_type (uri);
705 mime_apps = gnome_vfs_mime_get_all_applications (mime_type);
707 /* Open with plugin */
708 if (desc)
710 gchar *location = NULL;
712 anjuta_plugin_description_get_string (desc, "Anjuta Plugin",
713 "Location", &location);
714 g_assert (location != NULL);
715 if (location != NULL)
717 GObject *loaded_plugin;
719 loaded_plugin =
720 anjuta_plugin_manager_get_plugin_by_id (plugin_manager,
721 location);
722 if (loaded_plugin)
724 ianjuta_file_open (IANJUTA_FILE (loaded_plugin), uri, NULL);
725 set_recent_file (plugin, uri, mime_type);
727 else
729 anjuta_util_dialog_error (GTK_WINDOW (ANJUTA_PLUGIN(plugin)->shell),
730 _("Failed to activate plugin: %s"),
731 location);
733 g_free (location);
736 else
738 GList *uris = NULL;
739 GnomeVFSResult res;
741 mime_app = g_list_nth_data (mime_apps, idx);
742 uris = g_list_prepend (uris, (gpointer)uri);
743 res = gnome_vfs_mime_application_launch (mime_app, uris);
744 if (res != GNOME_VFS_OK)
745 launch_application_failure (plugin, uri, res);
746 else
747 set_recent_file (plugin, uri, mime_type);
748 g_list_free (uris);
750 gnome_vfs_mime_application_list_free (mime_apps);
751 g_free (mime_type);
754 static void
755 fm_open (GtkAction *action, AnjutaFileLoaderPlugin *plugin)
757 if (plugin->fm_current_uri)
758 open_file (plugin, plugin->fm_current_uri);
761 static void
762 fm_open_with (GtkMenuItem *menuitem, AnjutaFileLoaderPlugin *plugin)
764 if (plugin->fm_current_uri)
765 open_file_with (plugin, menuitem, plugin->fm_current_uri);
768 static void
769 pm_open (GtkAction *action, AnjutaFileLoaderPlugin *plugin)
771 if (plugin->pm_current_uri)
772 open_file (plugin, plugin->pm_current_uri);
775 static void
776 pm_open_with (GtkMenuItem *menuitem, AnjutaFileLoaderPlugin *plugin)
778 if (plugin->pm_current_uri)
779 open_file_with (plugin, menuitem, plugin->pm_current_uri);
782 static GtkActionEntry actions_file[] = {
784 "ActionFileNew",
785 GTK_STOCK_NEW,
786 N_("_New"),
787 "<control>n",
788 N_("New empty file"),
789 G_CALLBACK (on_new_activate)
792 "ActionFileOpen",
793 GTK_STOCK_OPEN,
794 N_("_Open..."),
795 "<control>o",
796 N_("Open file"),
797 G_CALLBACK (on_open_activate)
801 static GtkActionEntry popup_actions_file[] = {
803 "ActionPopupOpen",
804 GTK_STOCK_OPEN,
805 N_("_Open"), NULL,
806 N_("Open file"),
807 G_CALLBACK (fm_open)
810 "ActionPopupOpenWith",
811 NULL,
812 N_("Open _With"), NULL,
813 N_("Open with"), NULL
816 "ActionPopupPMOpen",
817 GTK_STOCK_OPEN,
818 N_("_Open"), NULL,
819 N_("Open file"),
820 G_CALLBACK (pm_open)
823 "ActionPopupPMOpenWith",
824 NULL,
825 N_("Open _With"), NULL,
826 N_("Open with"), NULL
830 static gboolean
831 create_open_with_submenu (AnjutaFileLoaderPlugin *plugin, GtkWidget *parentmenu,
832 const gchar *uri, GCallback callback,
833 gpointer callback_data)
835 GList *mime_apps, *node;
836 GnomeVFSMimeApplication *mime_app;
837 GList *plugin_descs, *snode;
838 GtkWidget *menu, *menuitem;
839 gchar *mime_type;
840 gint idx;
841 gboolean ret;
843 g_return_val_if_fail (GTK_IS_MENU_ITEM (parentmenu), FALSE);
845 menu = gtk_menu_new ();
846 gtk_widget_show (menu);
847 gtk_menu_item_set_submenu (GTK_MENU_ITEM (parentmenu), menu);
849 mime_type = anjuta_util_get_uri_mime_type (uri);
850 if (mime_type == NULL)
851 return FALSE;
853 idx = 0;
855 /* Open with plugins menu items */
856 plugin_descs = get_available_plugins_for_mime (plugin, mime_type);
857 snode = plugin_descs;
858 while (snode)
860 gchar *name;
861 AnjutaPluginDescription *desc;
863 desc = (AnjutaPluginDescription *)(snode->data);
864 name = NULL;
865 anjuta_plugin_description_get_locale_string (desc, "File Loader",
866 "Title", &name);
867 if (!name)
869 anjuta_plugin_description_get_locale_string (desc, "Anjuta Plugin",
870 "Name", &name);
872 if (!name)
874 anjuta_plugin_description_get_string (desc, "Anjuta Plugin",
875 "Location", &name);
877 menuitem = gtk_menu_item_new_with_label (name);
878 g_object_set_data (G_OBJECT (menuitem), "index", GINT_TO_POINTER (idx));
879 g_object_set_data (G_OBJECT (menuitem), "desc", (gpointer)(desc));
880 g_signal_connect (G_OBJECT (menuitem), "activate",
881 G_CALLBACK (callback), callback_data);
882 gtk_menu_append (menu, menuitem);
883 g_free (name);
884 snode = g_list_next (snode);
885 idx++;
888 /* Open with applications */
889 mime_apps = gnome_vfs_mime_get_all_applications (mime_type);
890 if (idx > 0 && mime_apps)
892 menuitem = gtk_menu_item_new ();
893 gtk_menu_append (menu, menuitem);
894 idx++;
896 g_free (mime_type);
897 idx = 0;
898 node = mime_apps;
899 while (node)
901 mime_app = (GnomeVFSMimeApplication *)(node->data);
902 menuitem = gtk_menu_item_new_with_label (mime_app->name);
903 g_object_set_data (G_OBJECT (menuitem), "index", GINT_TO_POINTER (idx));
904 g_signal_connect (G_OBJECT (menuitem), "activate",
905 G_CALLBACK (callback), callback_data);
906 gtk_menu_append (menu, menuitem);
907 node = g_list_next (node);
908 idx++;
910 gtk_widget_show_all (menu);
912 if (mime_apps == NULL && plugin_descs == NULL)
913 ret = FALSE;
914 else
915 ret = TRUE;
917 gnome_vfs_mime_application_list_free (mime_apps);
918 if (plugin_descs)
919 g_list_free (plugin_descs);
921 return ret;
924 static void
925 value_added_fm_current_uri (AnjutaPlugin *plugin, const char *name,
926 const GValue *value, gpointer data)
928 AnjutaUI *ui;
929 const gchar *uri;
930 AnjutaFileLoaderPlugin *fl_plugin;
931 GtkAction *action;
932 GtkWidget *parentmenu;
934 uri = g_value_get_string (value);
935 g_return_if_fail (name != NULL);
937 fl_plugin = ANJUTA_PLUGIN_FILE_LOADER (plugin);
938 ui = anjuta_shell_get_ui (plugin->shell, NULL);
940 action = anjuta_ui_get_action (ui, "ActionGroupPopupLoader", "ActionPopupOpen");
941 g_object_set (G_OBJECT (action), "sensitive", TRUE, NULL);
943 action = anjuta_ui_get_action (ui, "ActionGroupPopupLoader",
944 "ActionPopupOpenWith");
945 g_object_set (G_OBJECT (action), "sensitive", TRUE, NULL);
947 if (fl_plugin->fm_current_uri)
948 g_free (fl_plugin->fm_current_uri);
949 fl_plugin->fm_current_uri = g_strdup (uri);
951 parentmenu =
952 gtk_ui_manager_get_widget (GTK_UI_MANAGER(ui),
953 "/PopupFileManager/PlaceholderPopupFileOpen/OpenWith");
954 if (!create_open_with_submenu (fl_plugin, parentmenu, uri,
955 G_CALLBACK (fm_open_with), plugin))
956 g_object_set (G_OBJECT (action), "sensitive", FALSE, NULL);
959 static void
960 value_removed_fm_current_uri (AnjutaPlugin *plugin,
961 const char *name, gpointer data)
963 AnjutaUI *ui;
964 GtkAction *action;
965 AnjutaFileLoaderPlugin *fl_plugin;
967 fl_plugin = ANJUTA_PLUGIN_FILE_LOADER (plugin);
969 if (fl_plugin->fm_current_uri)
970 g_free (fl_plugin->fm_current_uri);
971 fl_plugin->fm_current_uri = NULL;
973 ui = anjuta_shell_get_ui (plugin->shell, NULL);
974 action = anjuta_ui_get_action (ui, "ActionGroupPopupLoader", "ActionPopupOpen");
975 g_object_set (G_OBJECT (action), "sensitive", FALSE, NULL);
977 action = anjuta_ui_get_action (ui, "ActionGroupPopupLoader",
978 "ActionPopupOpenWith");
979 g_object_set (G_OBJECT (action), "sensitive", FALSE, NULL);
982 static void
983 value_added_pm_current_uri (AnjutaPlugin *plugin, const char *name,
984 const GValue *value, gpointer data)
986 AnjutaUI *ui;
987 const gchar *uri;
988 AnjutaFileLoaderPlugin *fl_plugin;
989 GtkAction *action;
990 GtkWidget *parentmenu;
992 uri = g_value_get_string (value);
993 g_return_if_fail (name != NULL);
995 fl_plugin = ANJUTA_PLUGIN_FILE_LOADER (plugin);
996 ui = anjuta_shell_get_ui (plugin->shell, NULL);
998 action = anjuta_ui_get_action (ui, "ActionGroupPopupLoader", "ActionPopupPMOpen");
999 g_object_set (G_OBJECT (action), "sensitive", TRUE, NULL);
1001 action = anjuta_ui_get_action (ui, "ActionGroupPopupLoader",
1002 "ActionPopupPMOpenWith");
1003 g_object_set (G_OBJECT (action), "sensitive", TRUE, NULL);
1005 if (fl_plugin->pm_current_uri)
1006 g_free (fl_plugin->pm_current_uri);
1007 fl_plugin->pm_current_uri = g_strdup (uri);
1009 parentmenu =
1010 gtk_ui_manager_get_widget (GTK_UI_MANAGER(ui),
1011 "/PopupProjectManager/PlaceholderPopupProjectOpen/OpenWith");
1012 if (!create_open_with_submenu (fl_plugin, parentmenu, uri,
1013 G_CALLBACK (pm_open_with), plugin))
1014 g_object_set (G_OBJECT (action), "sensitive", FALSE, NULL);
1017 static void
1018 value_removed_pm_current_uri (AnjutaPlugin *plugin,
1019 const char *name, gpointer data)
1021 AnjutaUI *ui;
1022 GtkAction *action;
1023 AnjutaFileLoaderPlugin *fl_plugin;
1025 fl_plugin = ANJUTA_PLUGIN_FILE_LOADER (plugin);
1027 if (fl_plugin->pm_current_uri)
1028 g_free (fl_plugin->pm_current_uri);
1029 fl_plugin->pm_current_uri = NULL;
1031 ui = anjuta_shell_get_ui (plugin->shell, NULL);
1032 action = anjuta_ui_get_action (ui, "ActionGroupPopupLoader", "ActionPopupPMOpen");
1033 g_object_set (G_OBJECT (action), "sensitive", FALSE, NULL);
1035 action = anjuta_ui_get_action (ui, "ActionGroupPopupLoader",
1036 "ActionPopupPMOpenWith");
1037 g_object_set (G_OBJECT (action), "sensitive", FALSE, NULL);
1040 static void
1041 dnd_dropped (const gchar *uri, gpointer plugin)
1043 ianjuta_file_loader_load (IANJUTA_FILE_LOADER (plugin), uri, FALSE, NULL);
1046 static void
1047 on_session_load (AnjutaShell *shell, AnjutaSessionPhase phase,
1048 AnjutaSession *session,
1049 AnjutaFileLoaderPlugin *plugin)
1051 AnjutaStatus *status;
1052 const gchar *uri;
1053 gchar *mime_type;
1054 GList *files, *node;
1055 gint i;
1057 /* We want to load the files first before other session loads */
1058 if (phase != ANJUTA_SESSION_PHASE_FIRST)
1059 return;
1061 files = anjuta_session_get_string_list (session, "File Loader", "Files");
1062 if (!files)
1063 return;
1065 status = anjuta_shell_get_status (shell, NULL);
1066 anjuta_status_progress_add_ticks (status, g_list_length(files));
1068 /* Open project files first and then regular files */
1069 for (i = 0; i < 2; i++)
1071 node = files;
1072 while (node)
1074 uri = node->data;
1075 if (uri)
1077 gchar *label, *filename;
1079 mime_type = anjuta_util_get_uri_mime_type (uri);
1081 filename = g_path_get_basename (uri);
1082 if (strchr (filename, '#'))
1083 *(strchr (filename, '#')) = '\0';
1085 label = g_strconcat ("Loaded: ", filename, NULL);
1087 if (i == 0 && mime_type &&
1088 strcmp (mime_type, "application/x-anjuta") == 0)
1090 /* Project files first */
1091 /* FIXME: Ignore project files for now */
1093 ianjuta_file_loader_load (IANJUTA_FILE_LOADER (plugin),
1094 uri, FALSE, NULL);
1096 anjuta_status_progress_tick (status, NULL, label);
1098 else if (i != 0 &&
1099 (!mime_type ||
1100 strcmp (mime_type, "application/x-anjuta") != 0))
1102 /* Then rest of the files */
1103 ianjuta_file_loader_load (IANJUTA_FILE_LOADER (plugin),
1104 uri, FALSE, NULL);
1105 anjuta_status_progress_tick (status, NULL, label);
1107 g_free (filename);
1108 g_free (label);
1109 g_free (mime_type);
1111 node = g_list_next (node);
1114 if (files)
1116 g_list_foreach (files, (GFunc)g_free, NULL);
1117 g_list_free (files);
1121 static void
1122 setup_recent_chooser_menu (GtkRecentChooser* recent_menu, AnjutaFileLoaderPlugin* plugin)
1124 GtkRecentFilter *filter;
1126 gtk_recent_chooser_set_local_only (GTK_RECENT_CHOOSER (recent_menu), TRUE);
1127 gtk_recent_chooser_set_show_icons (GTK_RECENT_CHOOSER (recent_menu), TRUE);
1128 gtk_recent_chooser_set_sort_type (GTK_RECENT_CHOOSER (recent_menu), GTK_RECENT_SORT_MRU);
1129 gtk_recent_chooser_set_limit (GTK_RECENT_CHOOSER (recent_menu), 20);
1131 filter = gtk_recent_filter_new ();
1132 gtk_recent_filter_add_application (filter, g_get_application_name ());
1133 gtk_recent_chooser_set_filter (GTK_RECENT_CHOOSER (recent_menu), filter);
1135 g_signal_connect (recent_menu, "item-activated",
1136 G_CALLBACK (on_open_recent_file), plugin);
1139 static gboolean
1140 activate_plugin (AnjutaPlugin *plugin)
1142 GtkAction *action, *saction;
1143 AnjutaUI *ui;
1144 AnjutaFileLoaderPlugin *loader_plugin;
1145 GtkActionGroup *group;
1146 GtkWidget *widget;
1147 GtkWidget* recent_menu;
1148 GtkWidget* toolbar_menu;
1150 loader_plugin = ANJUTA_PLUGIN_FILE_LOADER (plugin);
1152 DEBUG_PRINT ("AnjutaFileLoaderPlugin: Activating File Loader plugin...");
1154 ui = anjuta_shell_get_ui (plugin->shell, NULL);
1156 /* Recent manager */
1157 loader_plugin->recent_manager = gtk_recent_manager_get_default();
1159 /* Add action group */
1160 loader_plugin->action_group =
1161 anjuta_ui_add_action_group_entries (ui, "ActionGroupLoader",
1162 _("File Loader"),
1163 actions_file,
1164 G_N_ELEMENTS (actions_file),
1165 GETTEXT_PACKAGE, TRUE, plugin);
1166 loader_plugin->popup_action_group =
1167 anjuta_ui_add_action_group_entries (ui, "ActionGroupPopupLoader",
1168 _("File Loader"),
1169 popup_actions_file,
1170 G_N_ELEMENTS (popup_actions_file),
1171 GETTEXT_PACKAGE, FALSE, plugin);
1172 saction = gtk_recent_action_new ("ActionFileWizard", _("New"),
1173 _("New file, project and project components."), NULL);
1174 g_object_set (saction, "stock-id", GTK_STOCK_NEW, NULL);
1175 g_signal_connect (saction, "activate", G_CALLBACK (on_new_activate), loader_plugin);
1176 gtk_action_group_add_action (loader_plugin->action_group,
1177 GTK_ACTION (saction));
1179 /* Set short labels */
1180 action = anjuta_ui_get_action (ui, "ActionGroupLoader", "ActionFileOpen");
1181 g_object_set (G_OBJECT (action), "short-label", _("Open"),
1182 "is-important", TRUE, NULL);
1184 group = gtk_action_group_new ("ActionGroupLoaderRecent");
1185 action = gtk_recent_action_new ("ActionFileOpenRecent", _("Open _Recent"),
1186 _("Open recent file"), NULL);
1187 g_object_set (action, "stock-id", GTK_STOCK_OPEN, NULL);
1188 setup_recent_chooser_menu (GTK_RECENT_CHOOSER (action), loader_plugin);
1189 g_signal_connect (action, "activate", G_CALLBACK (on_open_activate), loader_plugin);
1191 gtk_action_group_add_action (group, action);
1192 anjuta_ui_add_action_group (ui, "ActionGroupLoaderRecent",
1193 N_("Open recent files"), group, FALSE);
1194 loader_plugin->recent_group = group;
1196 /* Add UI */
1197 loader_plugin->uiid = anjuta_ui_merge (ui, UI_FILE);
1199 /* Adding submenus */
1200 recent_menu = anjuta_recent_chooser_menu_new_for_manager (loader_plugin->recent_manager);
1201 setup_recent_chooser_menu (GTK_RECENT_CHOOSER (recent_menu), loader_plugin);
1202 widget = gtk_ui_manager_get_widget (GTK_UI_MANAGER(ui),
1203 "/MenuMain/MenuFile/PlaceholderFileMenus/OpenRecent");
1204 gtk_menu_item_set_submenu (GTK_MENU_ITEM (widget),
1205 recent_menu);
1207 widget = gtk_ui_manager_get_widget (GTK_UI_MANAGER(ui),
1208 "/MenuMain/MenuFile/PlaceholderFileMenus/Wizard");
1209 gtk_menu_item_set_submenu (GTK_MENU_ITEM (widget), on_create_submenu(loader_plugin));
1211 widget = gtk_ui_manager_get_widget (GTK_UI_MANAGER(ui),
1212 "/ToolbarMain/PlaceholderFileToolbar/New");
1213 gtk_menu_tool_button_set_menu (GTK_MENU_TOOL_BUTTON (widget), on_create_submenu(loader_plugin));
1215 widget = gtk_ui_manager_get_widget (GTK_UI_MANAGER(ui),
1216 "/ToolbarMain/PlaceholderFileToolbar/Open");
1217 toolbar_menu = anjuta_recent_chooser_menu_new_for_manager (loader_plugin->recent_manager);
1218 setup_recent_chooser_menu (GTK_RECENT_CHOOSER (toolbar_menu), loader_plugin);
1219 gtk_menu_tool_button_set_menu (GTK_MENU_TOOL_BUTTON (widget),
1220 toolbar_menu);
1222 /* Install drag n drop handler */
1223 dnd_drop_init (GTK_WIDGET (plugin->shell), dnd_dropped, plugin,
1224 "text/plain", "text/html", "text/source", "application-x/anjuta",
1225 NULL);
1227 /* Add watches */
1228 loader_plugin->fm_watch_id =
1229 anjuta_plugin_add_watch (plugin, "file_manager_current_uri",
1230 value_added_fm_current_uri,
1231 value_removed_fm_current_uri, NULL);
1232 loader_plugin->pm_watch_id =
1233 anjuta_plugin_add_watch (plugin, "project_manager_current_uri",
1234 value_added_pm_current_uri,
1235 value_removed_pm_current_uri, NULL);
1237 /* Connect to session */
1238 g_signal_connect (G_OBJECT (plugin->shell), "load_session",
1239 G_CALLBACK (on_session_load), plugin);
1240 return TRUE;
1243 static gboolean
1244 deactivate_plugin (AnjutaPlugin *plugin)
1246 AnjutaUI *ui;
1247 AnjutaFileLoaderPlugin *loader_plugin;
1249 loader_plugin = ANJUTA_PLUGIN_FILE_LOADER (plugin);
1251 DEBUG_PRINT ("AnjutaFileLoaderPlugin: Deactivating File Loader plugin...");
1253 /* Disconnect session */
1254 g_signal_handlers_disconnect_by_func (G_OBJECT (plugin->shell),
1255 G_CALLBACK (on_session_load), plugin);
1257 ui = anjuta_shell_get_ui (plugin->shell, NULL);
1258 /* Remove watches */
1259 anjuta_plugin_remove_watch (plugin, loader_plugin->fm_watch_id, TRUE);
1260 anjuta_plugin_remove_watch (plugin, loader_plugin->pm_watch_id, TRUE);
1261 /* Uninstall dnd */
1262 dnd_drop_finalize (GTK_WIDGET (plugin->shell), plugin);
1263 /* Remove UI */
1264 anjuta_ui_unmerge (ui, loader_plugin->uiid);
1265 /* Remove action group */
1266 anjuta_ui_remove_action_group (ui, loader_plugin->action_group);
1267 anjuta_ui_remove_action_group (ui, loader_plugin->popup_action_group);
1268 anjuta_ui_remove_action_group (ui, loader_plugin->recent_group);
1269 return TRUE;
1272 static void
1273 dispose (GObject *obj)
1275 G_OBJECT_CLASS (parent_class)->dispose (obj);
1278 static void
1279 finalize (GObject *obj)
1281 /* Finalization codes here */
1282 G_OBJECT_CLASS (parent_class)->finalize (obj);
1285 static void
1286 anjuta_file_loader_plugin_instance_init (GObject *obj)
1288 AnjutaFileLoaderPlugin *plugin = ANJUTA_PLUGIN_FILE_LOADER (obj);
1290 plugin->fm_current_uri = NULL;
1291 plugin->pm_current_uri = NULL;
1294 static void
1295 anjuta_file_loader_plugin_class_init (GObjectClass *klass)
1297 AnjutaPluginClass *plugin_class = ANJUTA_PLUGIN_CLASS (klass);
1299 parent_class = g_type_class_peek_parent (klass);
1301 plugin_class->activate = activate_plugin;
1302 plugin_class->deactivate = deactivate_plugin;
1303 klass->dispose = dispose;
1304 klass->finalize = finalize;
1307 static GObject*
1308 iloader_load (IAnjutaFileLoader *loader, const gchar *uri,
1309 gboolean read_only, GError **err)
1311 gchar *mime_type;
1312 gchar *new_uri;
1313 GnomeVFSURI* vfs_uri;
1314 AnjutaStatus *status;
1315 AnjutaPluginManager *plugin_manager;
1316 GList *plugin_descs = NULL;
1317 GObject *plugin = NULL;
1319 g_return_val_if_fail (uri != NULL, NULL);
1320 vfs_uri = gnome_vfs_uri_new (uri);
1322 if (!gnome_vfs_uri_exists (vfs_uri))
1324 launch_application_failure (ANJUTA_PLUGIN_FILE_LOADER (loader),
1325 uri, GNOME_VFS_ERROR_NOT_FOUND);
1326 gnome_vfs_uri_unref (vfs_uri);
1327 return NULL;
1330 new_uri = gnome_vfs_uri_to_string (vfs_uri,
1331 GNOME_VFS_URI_HIDE_FRAGMENT_IDENTIFIER);
1332 mime_type = anjuta_util_get_uri_mime_type (new_uri);
1333 gnome_vfs_uri_unref (vfs_uri);
1335 if (mime_type == NULL)
1337 launch_application_failure (ANJUTA_PLUGIN_FILE_LOADER (loader),
1338 new_uri, GNOME_VFS_ERROR_NOT_FOUND);
1339 g_free (new_uri);
1340 return NULL;
1343 plugin_manager = anjuta_shell_get_plugin_manager (ANJUTA_PLUGIN (loader)->shell,
1344 NULL);
1345 status = anjuta_shell_get_status (ANJUTA_PLUGIN (loader)->shell, NULL);
1346 anjuta_status_busy_push (status);
1348 DEBUG_PRINT ("Opening URI: %s", uri);
1350 plugin_descs = anjuta_plugin_manager_query (plugin_manager,
1351 "Anjuta Plugin",
1352 "Interfaces", "IAnjutaFile",
1353 "File Loader",
1354 "SupportedMimeTypes",
1355 mime_type,
1356 NULL);
1358 if (g_list_length (plugin_descs) > 1)
1360 gchar* basename = g_path_get_basename (uri);
1361 gchar* message = g_strdup_printf ("Please select a plugin to open <b>%s</b>.",
1362 basename);
1363 plugin =
1364 anjuta_plugin_manager_select_and_activate (plugin_manager,
1365 "Open With",
1366 message,
1367 plugin_descs);
1368 g_free (basename);
1369 g_free (message);
1371 else if (g_list_length (plugin_descs) == 1)
1373 gchar *location = NULL;
1375 AnjutaPluginDescription *desc = plugin_descs->data;
1376 anjuta_plugin_description_get_string (desc, "Anjuta Plugin",
1377 "Location", &location);
1378 g_return_val_if_fail (location != NULL, NULL);
1379 plugin =
1380 anjuta_plugin_manager_get_plugin_by_id (plugin_manager,
1381 location);
1382 g_free (location);
1384 else
1386 launch_in_default_application (ANJUTA_PLUGIN_FILE_LOADER (loader), mime_type, uri);
1388 if (plugin)
1389 ianjuta_file_open (IANJUTA_FILE(plugin), uri, NULL);
1391 set_recent_file (ANJUTA_PLUGIN_FILE_LOADER (loader), new_uri, mime_type);
1393 if (plugin_descs)
1394 g_list_free (plugin_descs);
1395 g_free (mime_type);
1396 g_free (new_uri);
1397 anjuta_status_busy_pop (status);
1399 return plugin;
1402 static void
1403 iloader_iface_init(IAnjutaLoaderIface *iface)
1405 iface->find_plugins = NULL;
1408 static void
1409 iloader_file_iface_init(IAnjutaFileLoaderIface *iface)
1411 iface->load = iloader_load;
1414 ANJUTA_PLUGIN_BEGIN (AnjutaFileLoaderPlugin, anjuta_file_loader_plugin);
1415 ANJUTA_PLUGIN_ADD_INTERFACE (iloader, IANJUTA_TYPE_LOADER);
1416 ANJUTA_PLUGIN_ADD_INTERFACE (iloader_file, IANJUTA_TYPE_FILE_LOADER);
1417 ANJUTA_PLUGIN_END;
1419 ANJUTA_SIMPLE_PLUGIN (AnjutaFileLoaderPlugin, anjuta_file_loader_plugin);