1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
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
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>
40 #include "anjuta-recent-chooser-menu.h"
42 #define UI_FILE PACKAGE_DATA_DIR"/ui/anjuta-loader-plugin.ui"
44 static gpointer parent_class
;
47 sort_wizards(gconstpointer wizard1
, gconstpointer wizard2
)
50 AnjutaPluginDescription
* desc1
= (AnjutaPluginDescription
*) wizard1
;
51 AnjutaPluginDescription
* desc2
= (AnjutaPluginDescription
*) wizard2
;
53 if ((anjuta_plugin_description_get_locale_string (desc1
, "Wizard",
55 anjuta_plugin_description_get_locale_string (desc1
, "Anjuta Plugin",
57 (anjuta_plugin_description_get_locale_string (desc2
, "Wizard",
59 anjuta_plugin_description_get_locale_string (desc2
, "Anjuta Plugin",
62 return strcmp(name1
, name2
);
69 set_recent_file (AnjutaFileLoaderPlugin
*plugin
, const gchar
*uri
,
72 GtkRecentData
*recent_data
;
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
);
95 g_free (recent_data
->app_exec
);
96 g_slice_free (GtkRecentData
, recent_data
);
100 launch_application_failure (AnjutaFileLoaderPlugin
*plugin
,
102 GnomeVFSResult result
)
107 errmsg
= gnome_vfs_result_to_string (result
);
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
);
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
,
126 plugin_descs
= anjuta_plugin_manager_query (plugin_manager
,
128 "Interfaces", "IAnjutaFile",
130 "SupportedMimeTypes",
135 gchar
* supertype
= gnome_vfs_get_supertype_from_mime_type (mime_type
);
136 plugin_descs
= anjuta_plugin_manager_query (plugin_manager
,
138 "Interfaces", "IAnjutaFile",
140 "SupportedMimeTypes",
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
;
161 AnjutaPluginManager
*plugin_manager
;
163 plugin_manager
= anjuta_shell_get_plugin_manager (ANJUTA_PLUGIN (plugin
)->shell
,
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"
171 "You may choose to try opening it with the following "
172 "plugins or applications."),
173 g_basename(uri
), mime_type
);
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
,
179 GTK_BUTTONS_OK_CANCEL
, message
);
182 hbox
= gtk_hbox_new (FALSE
, 5);
183 gtk_box_pack_start (GTK_BOX (GTK_DIALOG(dialog
)->vbox
), hbox
,
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
;
202 AnjutaPluginDescription
*desc
;
204 desc
= (AnjutaPluginDescription
*)(snode
->data
);
208 anjuta_plugin_description_get_locale_string (desc
, "File Loader",
213 anjuta_plugin_description_get_locale_string (desc
, "Anjuta Plugin",
218 anjuta_plugin_description_get_string (desc
, "Anjuta Plugin",
221 menuitem
= gtk_menu_item_new_with_label (name
);
222 gtk_menu_append (menu
, menuitem
);
224 snode
= g_list_next (snode
);
227 /* Open with application menu items */
228 mime_apps
= gnome_vfs_mime_get_all_applications (mime_type
);
232 menuitem
= gtk_menu_item_new ();
233 gtk_menu_append (menu
, menuitem
);
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
)
251 option
= gtk_option_menu_get_history(GTK_OPTION_MENU (options
));
254 IAnjutaDocumentManager
*docman
;
255 docman
= anjuta_shell_get_interface (ANJUTA_PLUGIN (plugin
)->shell
,
256 IAnjutaDocumentManager
,
260 ianjuta_file_open (IANJUTA_FILE (docman
), uri
, NULL
);
264 g_warning ("No document manager plugin!!");
267 else if (option
< (g_list_length (plugin_descs
) + 1))
269 AnjutaPluginDescription
*desc
;
270 gchar
*location
= NULL
;
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
;
282 anjuta_plugin_manager_get_plugin_by_id (plugin_manager
,
286 ianjuta_file_open (IANJUTA_FILE (loaded_plugin
), uri
, NULL
);
287 set_recent_file (plugin
, uri
, mime_type
);
291 anjuta_util_dialog_error (GTK_WINDOW (ANJUTA_PLUGIN(plugin
)->shell
),
292 "Failed to activate plugin: %s",
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
);
310 set_recent_file (plugin
, uri
, mime_type
);
314 gnome_vfs_mime_application_list_free (mime_apps
);
316 g_list_free (plugin_descs
);
317 gtk_widget_destroy (dialog
);
321 launch_in_default_application (AnjutaFileLoaderPlugin
*plugin
,
322 const gchar
*mime_type
, const gchar
*uri
)
324 GnomeVFSMimeAction
*action
;
328 uris
= g_list_prepend (uris
, (gpointer
)uri
);
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
;
336 app
= gnome_vfs_mime_get_default_application (mime_type
);
339 gnome_vfs_mime_application_launch (app
, uris
)) != GNOME_VFS_OK
)
341 open_with_dialog (plugin
, uri
, mime_type
);
344 gnome_vfs_mime_application_free (app
);
347 gnome_vfs_mime_action_free (action
);
353 open_file (AnjutaFileLoaderPlugin
*plugin
, const gchar
*uri
)
355 GnomeVFSURI
*vfs_uri
;
358 vfs_uri
= gnome_vfs_uri_new (uri
);
359 dirname
= gnome_vfs_uri_extract_dirname (vfs_uri
);
360 gnome_vfs_uri_unref (vfs_uri
);
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
),
372 AnjutaFileLoaderPlugin
*plugin
;
374 } RecentIdelOpenData
;
377 on_open_recent_file_idle (gpointer data
)
379 RecentIdelOpenData
*rdata
;
381 rdata
= (RecentIdelOpenData
*)data
;
382 open_file (rdata
->plugin
, rdata
->uri
);
388 on_open_recent_file (GtkRecentChooser
*chooser
, AnjutaFileLoaderPlugin
*plugin
)
391 GnomeVFSURI
*vfs_uri
;
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
;
400 g_idle_add (on_open_recent_file_idle
, rdata
);
401 gnome_vfs_uri_unref (vfs_uri
);
407 on_open_response_ok (GtkDialog
* dialog
, gint id
,
408 AnjutaFileLoaderPlugin
*plugin
)
412 if (id
!= GTK_RESPONSE_ACCEPT
)
414 gtk_widget_destroy (GTK_WIDGET (dialog
));
418 list
= gtk_file_chooser_get_uris (GTK_FILE_CHOOSER (dialog
));
423 open_file (plugin
, (gchar
*)node
->data
);
425 node
= g_slist_next (node
);
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
);
527 create_file_open_dialog_gui(GtkWindow
* parent
, AnjutaFileLoaderPlugin
* plugin
)
530 gtk_file_chooser_dialog_new (_("Open file"),
532 GTK_FILE_CHOOSER_ACTION_OPEN
,
533 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
534 GTK_STOCK_OPEN
, GTK_RESPONSE_ACCEPT
,
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
),
544 G_CALLBACK (gtk_widget_destroy
),
545 GTK_OBJECT (dialog
));
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
,
557 ianjuta_document_manager_add_buffer (docman
, NULL
, NULL
, NULL
);
561 on_open_activate (GtkAction
*action
, AnjutaFileLoaderPlugin
*plugin
)
566 create_file_open_dialog_gui (GTK_WINDOW (ANJUTA_PLUGIN(plugin
)->shell
),
568 gtk_widget_show (dlg
);
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
,
586 if (anjuta_plugin_description_get_string (desc
, "Anjuta Plugin",
590 anjuta_plugin_manager_get_plugin_by_id (plugin_manager
, id
);
591 ianjuta_wizard_activate (IANJUTA_WIZARD (plugin
), NULL
);
597 on_create_submenu (gpointer user_data
)
599 AnjutaPluginManager
*plugin_manager
;
600 AnjutaFileLoaderPlugin
*loader
;
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
,
609 submenu
= gtk_menu_new ();
610 gtk_widget_show (submenu
);
612 plugin_descs
= anjuta_plugin_manager_query (plugin_manager
,
614 "Interfaces", "IAnjutaWizard",
616 plugin_descs
= g_list_sort(plugin_descs
, sort_wizards
);
621 AnjutaPluginDescription
*desc
;
624 gchar
*str
, *icon_path
, *name
;
630 if (anjuta_plugin_description_get_locale_string (desc
, "Wizard",
632 anjuta_plugin_description_get_locale_string (desc
, "Anjuta Plugin",
637 name
= g_strdup_printf ("_%d. %s", count
, N_(str
));
639 name
= g_strdup_printf ("%d. %s", count
, N_(str
));
642 if (anjuta_plugin_description_get_string (desc
, "Anjuta Plugin",
645 GdkPixbuf
*pixbuf
, *scaled_pixbuf
;
648 gtk_icon_size_lookup_for_settings (gtk_widget_get_settings (submenu
),
651 icon_path
= g_build_filename (PACKAGE_PIXMAPS_DIR
, str
, NULL
);
652 pixbuf
= gdk_pixbuf_new_from_file (icon_path
, NULL
);
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
);
662 icon
= gtk_image_new ();
664 gtk_widget_show (icon
);
670 menuitem
= gtk_image_menu_item_new_with_mnemonic (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
),
678 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem
),
680 gtk_menu_shell_append (GTK_MENU_SHELL (submenu
), menuitem
);
682 node
= g_list_next (node
);
684 g_list_free (plugin_descs
);
689 open_file_with (AnjutaFileLoaderPlugin
*plugin
, GtkMenuItem
*menuitem
,
693 GnomeVFSMimeApplication
*mime_app
;
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
),
702 plugin_manager
= anjuta_shell_get_plugin_manager (ANJUTA_PLUGIN (plugin
)->shell
,
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 */
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
;
720 anjuta_plugin_manager_get_plugin_by_id (plugin_manager
,
724 ianjuta_file_open (IANJUTA_FILE (loaded_plugin
), uri
, NULL
);
725 set_recent_file (plugin
, uri
, mime_type
);
729 anjuta_util_dialog_error (GTK_WINDOW (ANJUTA_PLUGIN(plugin
)->shell
),
730 _("Failed to activate plugin: %s"),
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
);
747 set_recent_file (plugin
, uri
, mime_type
);
750 gnome_vfs_mime_application_list_free (mime_apps
);
755 fm_open (GtkAction
*action
, AnjutaFileLoaderPlugin
*plugin
)
757 if (plugin
->fm_current_uri
)
758 open_file (plugin
, plugin
->fm_current_uri
);
762 fm_open_with (GtkMenuItem
*menuitem
, AnjutaFileLoaderPlugin
*plugin
)
764 if (plugin
->fm_current_uri
)
765 open_file_with (plugin
, menuitem
, plugin
->fm_current_uri
);
769 pm_open (GtkAction
*action
, AnjutaFileLoaderPlugin
*plugin
)
771 if (plugin
->pm_current_uri
)
772 open_file (plugin
, plugin
->pm_current_uri
);
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
[] = {
788 N_("New empty file"),
789 G_CALLBACK (on_new_activate
)
797 G_CALLBACK (on_open_activate
)
801 static GtkActionEntry popup_actions_file
[] = {
810 "ActionPopupOpenWith",
812 N_("Open _With"), NULL
,
813 N_("Open with"), NULL
823 "ActionPopupPMOpenWith",
825 N_("Open _With"), NULL
,
826 N_("Open with"), NULL
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
;
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
)
855 /* Open with plugins menu items */
856 plugin_descs
= get_available_plugins_for_mime (plugin
, mime_type
);
857 snode
= plugin_descs
;
861 AnjutaPluginDescription
*desc
;
863 desc
= (AnjutaPluginDescription
*)(snode
->data
);
865 anjuta_plugin_description_get_locale_string (desc
, "File Loader",
869 anjuta_plugin_description_get_locale_string (desc
, "Anjuta Plugin",
874 anjuta_plugin_description_get_string (desc
, "Anjuta Plugin",
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
);
884 snode
= g_list_next (snode
);
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
);
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
);
910 gtk_widget_show_all (menu
);
912 if (mime_apps
== NULL
&& plugin_descs
== NULL
)
917 gnome_vfs_mime_application_list_free (mime_apps
);
919 g_list_free (plugin_descs
);
925 value_added_fm_current_uri (AnjutaPlugin
*plugin
, const char *name
,
926 const GValue
*value
, gpointer data
)
930 AnjutaFileLoaderPlugin
*fl_plugin
;
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
);
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
);
960 value_removed_fm_current_uri (AnjutaPlugin
*plugin
,
961 const char *name
, gpointer data
)
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
);
983 value_added_pm_current_uri (AnjutaPlugin
*plugin
, const char *name
,
984 const GValue
*value
, gpointer data
)
988 AnjutaFileLoaderPlugin
*fl_plugin
;
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
);
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
);
1018 value_removed_pm_current_uri (AnjutaPlugin
*plugin
,
1019 const char *name
, gpointer data
)
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
);
1041 dnd_dropped (const gchar
*uri
, gpointer plugin
)
1043 ianjuta_file_loader_load (IANJUTA_FILE_LOADER (plugin
), uri
, FALSE
, NULL
);
1047 on_session_load (AnjutaShell
*shell
, AnjutaSessionPhase phase
,
1048 AnjutaSession
*session
,
1049 AnjutaFileLoaderPlugin
*plugin
)
1051 AnjutaStatus
*status
;
1054 GList
*files
, *node
;
1057 /* We want to load the files first before other session loads */
1058 if (phase
!= ANJUTA_SESSION_PHASE_FIRST
)
1061 files
= anjuta_session_get_string_list (session
, "File Loader", "Files");
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
++)
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),
1096 anjuta_status_progress_tick (status
, NULL
, label
);
1100 strcmp (mime_type
, "application/x-anjuta") != 0))
1102 /* Then rest of the files */
1103 ianjuta_file_loader_load (IANJUTA_FILE_LOADER (plugin
),
1105 anjuta_status_progress_tick (status
, NULL
, label
);
1111 node
= g_list_next (node
);
1116 g_list_foreach (files
, (GFunc
)g_free
, NULL
);
1117 g_list_free (files
);
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
);
1140 activate_plugin (AnjutaPlugin
*plugin
)
1142 GtkAction
*action
, *saction
;
1144 AnjutaFileLoaderPlugin
*loader_plugin
;
1145 GtkActionGroup
*group
;
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",
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",
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
;
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
),
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
),
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",
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
);
1244 deactivate_plugin (AnjutaPlugin
*plugin
)
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
);
1262 dnd_drop_finalize (GTK_WIDGET (plugin
->shell
), plugin
);
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
);
1273 dispose (GObject
*obj
)
1275 G_OBJECT_CLASS (parent_class
)->dispose (obj
);
1279 finalize (GObject
*obj
)
1281 /* Finalization codes here */
1282 G_OBJECT_CLASS (parent_class
)->finalize (obj
);
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
;
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
;
1308 iloader_load (IAnjutaFileLoader
*loader
, const gchar
*uri
,
1309 gboolean read_only
, GError
**err
)
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
);
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
);
1343 plugin_manager
= anjuta_shell_get_plugin_manager (ANJUTA_PLUGIN (loader
)->shell
,
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
,
1352 "Interfaces", "IAnjutaFile",
1354 "SupportedMimeTypes",
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>.",
1364 anjuta_plugin_manager_select_and_activate (plugin_manager
,
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
);
1380 anjuta_plugin_manager_get_plugin_by_id (plugin_manager
,
1386 launch_in_default_application (ANJUTA_PLUGIN_FILE_LOADER (loader
), mime_type
, uri
);
1389 ianjuta_file_open (IANJUTA_FILE(plugin
), uri
, NULL
);
1391 set_recent_file (ANJUTA_PLUGIN_FILE_LOADER (loader
), new_uri
, mime_type
);
1394 g_list_free (plugin_descs
);
1397 anjuta_status_busy_pop (status
);
1403 iloader_iface_init(IAnjutaLoaderIface
*iface
)
1405 iface
->find_plugins
= NULL
;
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
);
1419 ANJUTA_SIMPLE_PLUGIN (AnjutaFileLoaderPlugin
, anjuta_file_loader_plugin
);