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-handlers.h>
24 #include <libgnomevfs/gnome-vfs-ops.h>
26 #include <glib/gi18n.h>
28 #include <libanjuta/anjuta-shell.h>
29 #include <libanjuta/anjuta-status.h>
30 #include <libanjuta/anjuta-debug.h>
32 #include <libanjuta/interfaces/ianjuta-file.h>
33 #include <libanjuta/interfaces/ianjuta-file-loader.h>
34 #include <libanjuta/interfaces/ianjuta-document-manager.h>
35 #include <libanjuta/interfaces/ianjuta-wizard.h>
36 #include <libegg/menu/egg-submenu-action.h>
37 #include <libegg/menu/egg-recent-action.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 EggRecentItem
*recent_item
;
73 DEBUG_PRINT ("Adding recent item of mimi-type: %s", mime
);
74 recent_item
= egg_recent_item_new ();
75 egg_recent_item_set_uri (recent_item
, uri
);
76 egg_recent_item_set_mime_type (recent_item
, mime
);
77 if (strcmp (mime
, "application/x-anjuta") == 0)
79 egg_recent_item_add_group (recent_item
, "anjuta-projects");
80 egg_recent_model_add_full (plugin
->recent_files_model_top
,
85 egg_recent_item_add_group (recent_item
, "anjuta-files");
86 egg_recent_model_add_full (plugin
->recent_files_model_bottom
,
92 launch_application_failure (AnjutaFileLoaderPlugin
*plugin
,
94 GnomeVFSResult result
)
99 errmsg
= gnome_vfs_result_to_string (result
);
101 gtk_widget_get_toplevel (GTK_WIDGET(ANJUTA_PLUGIN (plugin
)->shell
));
102 anjuta_util_dialog_error (GTK_WINDOW (parent
),
103 "Can not open \"%s\".\n\n%s",
104 g_basename (uri
), errmsg
);
108 get_available_plugins_for_mime (AnjutaFileLoaderPlugin
* plugin
,
109 const gchar
*mime_type
)
111 AnjutaPluginManager
*plugin_manager
;
112 GList
*plugin_descs
= NULL
;
114 g_return_val_if_fail (mime_type
!= NULL
, NULL
);
116 plugin_manager
= anjuta_shell_get_plugin_manager (ANJUTA_PLUGIN(plugin
)->shell
,
118 plugin_descs
= anjuta_plugin_manager_query (plugin_manager
,
120 "Interfaces", "IAnjutaFile",
122 "SupportedMimeTypes",
129 open_with_dialog (AnjutaFileLoaderPlugin
*plugin
, const gchar
*uri
,
130 const gchar
*mime_type
)
132 GList
*plugin_descs
, *snode
;
133 GList
*mime_apps
, *node
;
134 GtkWidget
*menu
, *menuitem
;
135 GnomeVFSMimeApplication
*mime_app
;
137 GtkWidget
*dialog
, *parent
, *hbox
, *label
;
140 AnjutaPluginManager
*plugin_manager
;
142 plugin_manager
= anjuta_shell_get_plugin_manager (ANJUTA_PLUGIN (plugin
)->shell
,
144 message
= g_strdup_printf (_("<b>Cannot open \"%s\"</b>.\n\n"
145 "There is no plugin, default action, or application "
146 "configured to handle this file type.\n"
150 "You may choose to try opening it with the following "
151 "plugins or applications."),
152 g_basename(uri
), mime_type
);
154 gtk_widget_get_toplevel (GTK_WIDGET(ANJUTA_PLUGIN (plugin
)->shell
));
155 dialog
= gtk_message_dialog_new_with_markup (GTK_WINDOW (parent
),
156 GTK_DIALOG_DESTROY_WITH_PARENT
,
158 GTK_BUTTONS_OK_CANCEL
, message
);
161 hbox
= gtk_hbox_new (FALSE
, 5);
162 gtk_box_pack_start (GTK_BOX (GTK_DIALOG(dialog
)->vbox
), hbox
,
164 label
= gtk_label_new (_("Open with:"));
165 options
= gtk_option_menu_new ();
166 gtk_box_pack_end (GTK_BOX(hbox
), options
, FALSE
, FALSE
, 10);
167 gtk_box_pack_end (GTK_BOX(hbox
), label
, FALSE
, FALSE
, 10);
169 menu
= gtk_menu_new ();
171 /* Document manager plugin */
172 menuitem
= gtk_menu_item_new_with_label (_("Document Manager"));
173 gtk_menu_append (menu
, menuitem
);
175 /* Open with plugins menu items */
176 plugin_descs
= get_available_plugins_for_mime (plugin
, mime_type
);
177 snode
= plugin_descs
;
181 AnjutaPluginDescription
*desc
;
183 desc
= (AnjutaPluginDescription
*)(snode
->data
);
187 anjuta_plugin_description_get_locale_string (desc
, "File Loader",
192 anjuta_plugin_description_get_locale_string (desc
, "Anjuta Plugin",
197 anjuta_plugin_description_get_string (desc
, "Anjuta Plugin",
200 menuitem
= gtk_menu_item_new_with_label (name
);
201 gtk_menu_append (menu
, menuitem
);
203 snode
= g_list_next (snode
);
206 /* Open with application menu items */
207 mime_apps
= gnome_vfs_mime_get_all_applications (mime_type
);
211 menuitem
= gtk_menu_item_new ();
212 gtk_menu_append (menu
, menuitem
);
217 mime_app
= (GnomeVFSMimeApplication
*)(node
->data
);
218 menuitem
= gtk_menu_item_new_with_label (mime_app
->name
);
219 gtk_menu_append (menu
, menuitem
);
220 node
= g_list_next (node
);
223 gtk_option_menu_set_menu (GTK_OPTION_MENU (options
), menu
);
224 gtk_widget_show_all (hbox
);
226 if (gtk_dialog_run (GTK_DIALOG (dialog
)) == GTK_RESPONSE_OK
)
230 option
= gtk_option_menu_get_history(GTK_OPTION_MENU (options
));
233 IAnjutaDocumentManager
*docman
;
234 docman
= anjuta_shell_get_interface (ANJUTA_PLUGIN (plugin
)->shell
,
235 IAnjutaDocumentManager
,
239 ianjuta_file_open (IANJUTA_FILE (docman
), uri
, NULL
);
243 g_warning ("No document manager plugin!!");
246 else if (option
< (g_list_length (plugin_descs
) + 1))
248 AnjutaPluginDescription
*desc
;
249 gchar
*location
= NULL
;
252 desc
= g_list_nth_data (plugin_descs
, option
);
253 anjuta_plugin_description_get_string (desc
, "Anjuta Plugin",
254 "Location", &location
);
255 g_assert (location
!= NULL
);
256 if (location
!= NULL
)
258 GObject
*loaded_plugin
;
261 anjuta_plugin_manager_get_plugin_by_id (plugin_manager
,
265 ianjuta_file_open (IANJUTA_FILE (loaded_plugin
), uri
, NULL
);
266 set_recent_file (plugin
, uri
, mime_type
);
270 anjuta_util_dialog_error (GTK_WINDOW (ANJUTA_PLUGIN(plugin
)->shell
),
271 "Failed to activate plugin: %s",
282 option
-= (g_list_length (plugin_descs
) + 2);
283 mime_app
= g_list_nth_data (mime_apps
, option
);
284 uris
= g_list_prepend (uris
, (gpointer
)uri
);
285 res
= gnome_vfs_mime_application_launch (mime_app
, uris
);
286 if (res
!= GNOME_VFS_OK
)
287 launch_application_failure (plugin
, uri
, res
);
289 set_recent_file (plugin
, uri
, mime_type
);
293 gnome_vfs_mime_application_list_free (mime_apps
);
295 g_list_free (plugin_descs
);
296 gtk_widget_destroy (dialog
);
300 launch_in_default_application (AnjutaFileLoaderPlugin
*plugin
,
301 const gchar
*mime_type
, const gchar
*uri
)
303 GnomeVFSMimeAction
*action
;
307 uris
= g_list_prepend (uris
, (gpointer
)uri
);
310 action
= gnome_vfs_mime_get_default_action (mime_type
);
311 if (!action
|| gnome_vfs_mime_action_launch (action
, uris
) != GNOME_VFS_OK
)
313 GnomeVFSMimeApplication
*app
;
315 app
= gnome_vfs_mime_get_default_application (mime_type
);
318 gnome_vfs_mime_application_launch (app
, uris
)) != GNOME_VFS_OK
)
320 open_with_dialog (plugin
, uri
, mime_type
);
323 gnome_vfs_mime_application_free (app
);
326 gnome_vfs_mime_action_free (action
);
332 open_file (AnjutaFileLoaderPlugin
*plugin
, const gchar
*uri
)
334 GnomeVFSURI
*vfs_uri
;
337 vfs_uri
= gnome_vfs_uri_new (uri
);
338 dirname
= gnome_vfs_uri_extract_dirname (vfs_uri
);
339 gnome_vfs_uri_unref (vfs_uri
);
343 ianjuta_file_loader_load (IANJUTA_FILE_LOADER (plugin
),
349 AnjutaFileLoaderPlugin
*plugin
;
351 } RecentIdelOpenData
;
354 on_open_recent_file_idle (gpointer data
)
356 RecentIdelOpenData
*rdata
;
358 rdata
= (RecentIdelOpenData
*)data
;
359 open_file (rdata
->plugin
, rdata
->uri
);
365 on_open_recent_file (EggRecentAction
*action
, AnjutaFileLoaderPlugin
*plugin
)
368 GnomeVFSURI
*vfs_uri
;
370 RecentIdelOpenData
*rdata
;
372 uri
= egg_recent_action_get_selected_uri (action
);
373 vfs_uri
= gnome_vfs_uri_new (uri
);
374 rdata
= g_new0 (RecentIdelOpenData
, 1);
375 rdata
->plugin
= plugin
;
377 g_idle_add (on_open_recent_file_idle
, rdata
);
378 gnome_vfs_uri_unref (vfs_uri
);
384 on_open_response_ok (GtkDialog
* dialog
, gint id
,
385 AnjutaFileLoaderPlugin
*plugin
)
389 if (id
!= GTK_RESPONSE_ACCEPT
)
391 gtk_widget_destroy (GTK_WIDGET (dialog
));
395 list
= gtk_file_chooser_get_uris (GTK_FILE_CHOOSER (dialog
));
400 open_file (plugin
, (gchar
*)node
->data
);
402 node
= g_slist_next (node
);
408 setup_file_filters (GtkFileChooser
*fc
)
410 GtkFileFilter
*filter
;
412 filter
= gtk_file_filter_new ();
413 gtk_file_filter_set_name (filter
, _("All files"));
414 gtk_file_filter_add_pattern (filter
, "*");
415 gtk_file_chooser_add_filter (fc
, filter
);
417 filter
= gtk_file_filter_new ();
418 gtk_file_filter_set_name (filter
, _("Anjuta Projects"));
419 gtk_file_filter_add_pattern (filter
, "*.anjuta");
420 gtk_file_filter_add_pattern (filter
, "*.prj");
421 gtk_file_chooser_add_filter (fc
, filter
);
423 filter
= gtk_file_filter_new ();
424 gtk_file_filter_set_name (filter
, _("C/C++ source files"));
425 gtk_file_filter_add_pattern (filter
, "*.c");
426 gtk_file_filter_add_pattern (filter
, "*.cc");
427 gtk_file_filter_add_pattern (filter
, "*.cpp");
428 gtk_file_filter_add_pattern (filter
, "*.cxx");
429 gtk_file_filter_add_pattern (filter
, "*.c++");
430 gtk_file_filter_add_pattern (filter
, "*.h");
431 gtk_file_filter_add_pattern (filter
, "*.hh");
432 gtk_file_filter_add_pattern (filter
, "*.hpp");
433 gtk_file_chooser_add_filter (fc
, filter
);
435 filter
= gtk_file_filter_new ();
436 gtk_file_filter_set_name (filter
, _("C# source files"));
437 gtk_file_filter_add_pattern (filter
, "*.cs");
438 gtk_file_filter_add_pattern (filter
, "*.h");
439 gtk_file_chooser_add_filter (fc
, filter
);
441 filter
= gtk_file_filter_new ();
442 gtk_file_filter_set_name (filter
, _("Java source files"));
443 gtk_file_filter_add_pattern (filter
, "*.java");
444 gtk_file_filter_add_pattern (filter
, "*.js");
445 gtk_file_chooser_add_filter (fc
, filter
);
447 filter
= gtk_file_filter_new ();
448 gtk_file_filter_set_name (filter
, _("Pascal source files"));
449 gtk_file_filter_add_pattern (filter
, "*.pas");
450 gtk_file_chooser_add_filter (fc
, filter
);
452 filter
= gtk_file_filter_new ();
453 gtk_file_filter_set_name (filter
, _("PHP source files"));
454 gtk_file_filter_add_pattern (filter
, "*.php");
455 gtk_file_filter_add_pattern (filter
, "*.php?");
456 gtk_file_filter_add_pattern (filter
, "*.phtml");
457 gtk_file_chooser_add_filter (fc
, filter
);
459 filter
= gtk_file_filter_new ();
460 gtk_file_filter_set_name (filter
, _("Perl source files"));
461 gtk_file_filter_add_pattern (filter
, "*.pl");
462 gtk_file_filter_add_pattern (filter
, "*.pm");
463 gtk_file_chooser_add_filter (fc
, filter
);
465 filter
= gtk_file_filter_new ();
466 gtk_file_filter_set_name (filter
, _("Python source files"));
467 gtk_file_filter_add_pattern (filter
, "*.py");
468 gtk_file_chooser_add_filter (fc
, filter
);
470 filter
= gtk_file_filter_new ();
471 gtk_file_filter_set_name (filter
, _("Hyper text markup files"));
472 gtk_file_filter_add_pattern (filter
, "*.htm");
473 gtk_file_filter_add_pattern (filter
, "*.html");
474 gtk_file_filter_add_pattern (filter
, "*.xhtml");
475 gtk_file_filter_add_pattern (filter
, "*.dhtml");
476 gtk_file_filter_add_pattern (filter
, "*.cs");
477 gtk_file_chooser_add_filter (fc
, filter
);
479 filter
= gtk_file_filter_new ();
480 gtk_file_filter_set_name (filter
, _("Shell scripts files"));
481 gtk_file_filter_add_pattern (filter
, "*.sh");
482 gtk_file_chooser_add_filter (fc
, filter
);
484 filter
= gtk_file_filter_new ();
485 gtk_file_filter_set_name (filter
, _("Makefiles"));
486 gtk_file_filter_add_pattern (filter
, "Makefile*");
487 gtk_file_filter_add_pattern (filter
, "makefile*");
488 gtk_file_chooser_add_filter (fc
, filter
);
490 filter
= gtk_file_filter_new ();
491 gtk_file_filter_set_name (filter
, _("Lua files"));
492 gtk_file_filter_add_pattern (filter
, "*.lua");
493 gtk_file_chooser_add_filter (fc
, filter
);
495 filter
= gtk_file_filter_new ();
496 gtk_file_filter_set_name (filter
, _("Diff files"));
497 gtk_file_filter_add_pattern (filter
, "*.diff");
498 gtk_file_filter_add_pattern (filter
, "*.patch");
499 gtk_file_filter_add_pattern (filter
, "*.cvsdiff");
500 gtk_file_chooser_add_filter (fc
, filter
);
504 create_file_open_dialog_gui(GtkWindow
* parent
, AnjutaFileLoaderPlugin
* plugin
)
507 gtk_file_chooser_dialog_new (_("Open file"),
509 GTK_FILE_CHOOSER_ACTION_OPEN
,
510 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
511 GTK_STOCK_OPEN
, GTK_RESPONSE_ACCEPT
,
513 gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER(dialog
), TRUE
);
514 gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (dialog
), FALSE
);
515 setup_file_filters (GTK_FILE_CHOOSER (dialog
));
517 g_signal_connect(G_OBJECT(dialog
), "response",
518 G_CALLBACK(on_open_response_ok
), plugin
);
519 g_signal_connect_swapped (GTK_OBJECT (dialog
),
521 G_CALLBACK (gtk_widget_destroy
),
522 GTK_OBJECT (dialog
));
527 on_open_activate (GtkAction
*action
, AnjutaFileLoaderPlugin
*plugin
)
532 create_file_open_dialog_gui (GTK_WINDOW (ANJUTA_PLUGIN(plugin
)->shell
),
534 gtk_widget_show (dlg
);
538 on_activate_wizard (GtkMenuItem
*menuitem
,
539 AnjutaFileLoaderPlugin
*loader
)
541 AnjutaPluginManager
*plugin_manager
;
542 AnjutaPluginDescription
*desc
;
544 desc
= g_object_get_data (G_OBJECT (menuitem
), "__plugin_desc");
545 plugin_manager
= anjuta_shell_get_plugin_manager (ANJUTA_PLUGIN (loader
)->shell
,
552 if (anjuta_plugin_description_get_string (desc
, "Anjuta Plugin",
556 anjuta_plugin_manager_get_plugin_by_id (plugin_manager
, id
);
557 ianjuta_wizard_activate (IANJUTA_WIZARD (plugin
), NULL
);
563 on_create_submenu (gpointer user_data
)
565 AnjutaPluginManager
*plugin_manager
;
566 AnjutaFileLoaderPlugin
*loader
;
569 GtkWidget
*submenu
= NULL
;
570 GList
*plugin_descs
= NULL
;
572 loader
= ANJUTA_PLUGIN_FILE_LOADER (user_data
);
573 plugin_manager
= anjuta_shell_get_plugin_manager (ANJUTA_PLUGIN (loader
)->shell
,
575 submenu
= gtk_menu_new ();
576 gtk_widget_show (submenu
);
578 plugin_descs
= anjuta_plugin_manager_query (plugin_manager
,
580 "Interfaces", "IAnjutaWizard",
582 plugin_descs
= g_list_sort(plugin_descs
, sort_wizards
);
587 AnjutaPluginDescription
*desc
;
590 gchar
*str
, *icon_path
, *name
;
596 if (anjuta_plugin_description_get_locale_string (desc
, "Wizard",
598 anjuta_plugin_description_get_locale_string (desc
, "Anjuta Plugin",
603 name
= g_strdup_printf ("_%d. %s", count
, N_(str
));
605 name
= g_strdup_printf ("%d. %s", count
, N_(str
));
608 if (anjuta_plugin_description_get_string (desc
, "Anjuta Plugin",
611 GdkPixbuf
*pixbuf
, *scaled_pixbuf
;
614 gtk_icon_size_lookup_for_settings (gtk_widget_get_settings (submenu
),
617 icon_path
= g_build_filename (PACKAGE_PIXMAPS_DIR
, str
, NULL
);
618 pixbuf
= gdk_pixbuf_new_from_file (icon_path
, NULL
);
621 scaled_pixbuf
= gdk_pixbuf_scale_simple (pixbuf
, width
, height
,
622 GDK_INTERP_BILINEAR
);
623 icon
= gtk_image_new_from_pixbuf (scaled_pixbuf
);
624 g_object_unref (pixbuf
);
625 g_object_unref (scaled_pixbuf
);
628 icon
= gtk_image_new ();
630 gtk_widget_show (icon
);
636 menuitem
= gtk_image_menu_item_new_with_mnemonic (name
);
638 gtk_widget_show (menuitem
);
639 g_object_set_data (G_OBJECT (menuitem
), "__plugin_desc", desc
);
640 g_signal_connect (G_OBJECT (menuitem
), "activate",
641 G_CALLBACK (on_activate_wizard
),
644 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem
),
646 gtk_menu_shell_append (GTK_MENU_SHELL (submenu
), menuitem
);
648 node
= g_list_next (node
);
650 g_list_free (plugin_descs
);
656 on_recent_files_tooltip (GtkTooltips *tooltips, GtkWidget *menu_item,
657 EggRecentItem *item, gpointer user_data)
661 uri = egg_recent_item_get_uri_for_display (item);
662 tip = g_strdup_printf ("Open '%s'", uri);
664 gtk_tooltips_set_tip (tooltips, menu_item, tip, NULL);
671 open_file_with (AnjutaFileLoaderPlugin
*plugin
, GtkMenuItem
*menuitem
,
675 GnomeVFSMimeApplication
*mime_app
;
678 AnjutaPluginDescription
*desc
;
679 AnjutaPluginManager
*plugin_manager
;
681 idx
= GPOINTER_TO_INT (g_object_get_data (G_OBJECT (menuitem
), "index"));
682 desc
= (AnjutaPluginDescription
*) g_object_get_data (G_OBJECT (menuitem
),
684 plugin_manager
= anjuta_shell_get_plugin_manager (ANJUTA_PLUGIN (plugin
)->shell
,
686 mime_type
= anjuta_util_get_uri_mime_type (uri
);
687 mime_apps
= gnome_vfs_mime_get_all_applications (mime_type
);
689 /* Open with plugin */
692 gchar
*location
= NULL
;
694 anjuta_plugin_description_get_string (desc
, "Anjuta Plugin",
695 "Location", &location
);
696 g_assert (location
!= NULL
);
697 if (location
!= NULL
)
699 GObject
*loaded_plugin
;
702 anjuta_plugin_manager_get_plugin_by_id (plugin_manager
,
706 ianjuta_file_open (IANJUTA_FILE (loaded_plugin
), uri
, NULL
);
707 set_recent_file (plugin
, uri
, mime_type
);
711 anjuta_util_dialog_error (GTK_WINDOW (ANJUTA_PLUGIN(plugin
)->shell
),
712 _("Failed to activate plugin: %s"),
723 mime_app
= g_list_nth_data (mime_apps
, idx
);
724 uris
= g_list_prepend (uris
, (gpointer
)uri
);
725 res
= gnome_vfs_mime_application_launch (mime_app
, uris
);
726 if (res
!= GNOME_VFS_OK
)
727 launch_application_failure (plugin
, uri
, res
);
729 set_recent_file (plugin
, uri
, mime_type
);
732 gnome_vfs_mime_application_list_free (mime_apps
);
737 fm_open (GtkAction
*action
, AnjutaFileLoaderPlugin
*plugin
)
739 if (plugin
->fm_current_uri
)
740 open_file (plugin
, plugin
->fm_current_uri
);
744 fm_open_with (GtkMenuItem
*menuitem
, AnjutaFileLoaderPlugin
*plugin
)
746 if (plugin
->fm_current_uri
)
747 open_file_with (plugin
, menuitem
, plugin
->fm_current_uri
);
751 pm_open (GtkAction
*action
, AnjutaFileLoaderPlugin
*plugin
)
753 if (plugin
->pm_current_uri
)
754 open_file (plugin
, plugin
->pm_current_uri
);
758 pm_open_with (GtkMenuItem
*menuitem
, AnjutaFileLoaderPlugin
*plugin
)
760 if (plugin
->pm_current_uri
)
761 open_file_with (plugin
, menuitem
, plugin
->pm_current_uri
);
764 static GtkActionEntry actions_file
[] = {
771 G_CALLBACK (on_open_activate
)
775 static GtkActionEntry popup_actions_file
[] = {
784 "ActionPopupOpenWith",
786 N_("Open _With"), NULL
,
787 N_("Open with"), NULL
797 "ActionPopupPMOpenWith",
799 N_("Open _With"), NULL
,
800 N_("Open with"), NULL
805 create_open_with_submenu (AnjutaFileLoaderPlugin
*plugin
, GtkWidget
*parentmenu
,
806 const gchar
*uri
, GCallback callback
,
807 gpointer callback_data
)
809 GList
*mime_apps
, *node
;
810 GnomeVFSMimeApplication
*mime_app
;
811 GList
*plugin_descs
, *snode
;
812 GtkWidget
*menu
, *menuitem
;
817 g_return_val_if_fail (GTK_IS_MENU_ITEM (parentmenu
), FALSE
);
819 menu
= gtk_menu_new ();
820 gtk_widget_show (menu
);
821 gtk_menu_item_set_submenu (GTK_MENU_ITEM (parentmenu
), menu
);
823 mime_type
= anjuta_util_get_uri_mime_type (uri
);
824 if (mime_type
== NULL
)
829 /* Open with plugins menu items */
830 plugin_descs
= get_available_plugins_for_mime (plugin
, mime_type
);
831 snode
= plugin_descs
;
835 AnjutaPluginDescription
*desc
;
837 desc
= (AnjutaPluginDescription
*)(snode
->data
);
839 anjuta_plugin_description_get_locale_string (desc
, "File Loader",
843 anjuta_plugin_description_get_locale_string (desc
, "Anjuta Plugin",
848 anjuta_plugin_description_get_string (desc
, "Anjuta Plugin",
851 menuitem
= gtk_menu_item_new_with_label (name
);
852 g_object_set_data (G_OBJECT (menuitem
), "index", GINT_TO_POINTER (idx
));
853 g_object_set_data (G_OBJECT (menuitem
), "desc", (gpointer
)(desc
));
854 g_signal_connect (G_OBJECT (menuitem
), "activate",
855 G_CALLBACK (callback
), callback_data
);
856 gtk_menu_append (menu
, menuitem
);
858 snode
= g_list_next (snode
);
862 /* Open with applications */
863 mime_apps
= gnome_vfs_mime_get_all_applications (mime_type
);
864 if (idx
> 0 && mime_apps
)
866 menuitem
= gtk_menu_item_new ();
867 gtk_menu_append (menu
, menuitem
);
875 mime_app
= (GnomeVFSMimeApplication
*)(node
->data
);
876 menuitem
= gtk_menu_item_new_with_label (mime_app
->name
);
877 g_object_set_data (G_OBJECT (menuitem
), "index", GINT_TO_POINTER (idx
));
878 g_signal_connect (G_OBJECT (menuitem
), "activate",
879 G_CALLBACK (callback
), callback_data
);
880 gtk_menu_append (menu
, menuitem
);
881 node
= g_list_next (node
);
884 gtk_widget_show_all (menu
);
886 if (mime_apps
== NULL
&& plugin_descs
== NULL
)
891 gnome_vfs_mime_application_list_free (mime_apps
);
893 g_list_free (plugin_descs
);
899 value_added_fm_current_uri (AnjutaPlugin
*plugin
, const char *name
,
900 const GValue
*value
, gpointer data
)
904 AnjutaFileLoaderPlugin
*fl_plugin
;
906 GtkWidget
*parentmenu
;
908 uri
= g_value_get_string (value
);
909 g_return_if_fail (name
!= NULL
);
911 fl_plugin
= ANJUTA_PLUGIN_FILE_LOADER (plugin
);
912 ui
= anjuta_shell_get_ui (plugin
->shell
, NULL
);
914 action
= anjuta_ui_get_action (ui
, "ActionGroupPopupLoader", "ActionPopupOpen");
915 g_object_set (G_OBJECT (action
), "sensitive", TRUE
, NULL
);
917 action
= anjuta_ui_get_action (ui
, "ActionGroupPopupLoader",
918 "ActionPopupOpenWith");
919 g_object_set (G_OBJECT (action
), "sensitive", TRUE
, NULL
);
921 if (fl_plugin
->fm_current_uri
)
922 g_free (fl_plugin
->fm_current_uri
);
923 fl_plugin
->fm_current_uri
= g_strdup (uri
);
926 gtk_ui_manager_get_widget (GTK_UI_MANAGER(ui
),
927 "/PopupFileManager/PlaceholderPopupFileOpen/OpenWith");
928 if (!create_open_with_submenu (fl_plugin
, parentmenu
, uri
,
929 G_CALLBACK (fm_open_with
), plugin
))
930 g_object_set (G_OBJECT (action
), "sensitive", FALSE
, NULL
);
934 value_removed_fm_current_uri (AnjutaPlugin
*plugin
,
935 const char *name
, gpointer data
)
939 AnjutaFileLoaderPlugin
*fl_plugin
;
941 fl_plugin
= ANJUTA_PLUGIN_FILE_LOADER (plugin
);
943 if (fl_plugin
->fm_current_uri
)
944 g_free (fl_plugin
->fm_current_uri
);
945 fl_plugin
->fm_current_uri
= NULL
;
947 ui
= anjuta_shell_get_ui (plugin
->shell
, NULL
);
948 action
= anjuta_ui_get_action (ui
, "ActionGroupPopupLoader", "ActionPopupOpen");
949 g_object_set (G_OBJECT (action
), "sensitive", FALSE
, NULL
);
951 action
= anjuta_ui_get_action (ui
, "ActionGroupPopupLoader",
952 "ActionPopupOpenWith");
953 g_object_set (G_OBJECT (action
), "sensitive", FALSE
, NULL
);
957 value_added_pm_current_uri (AnjutaPlugin
*plugin
, const char *name
,
958 const GValue
*value
, gpointer data
)
962 AnjutaFileLoaderPlugin
*fl_plugin
;
964 GtkWidget
*parentmenu
;
966 uri
= g_value_get_string (value
);
967 g_return_if_fail (name
!= NULL
);
969 fl_plugin
= ANJUTA_PLUGIN_FILE_LOADER (plugin
);
970 ui
= anjuta_shell_get_ui (plugin
->shell
, NULL
);
972 action
= anjuta_ui_get_action (ui
, "ActionGroupPopupLoader", "ActionPopupPMOpen");
973 g_object_set (G_OBJECT (action
), "sensitive", TRUE
, NULL
);
975 action
= anjuta_ui_get_action (ui
, "ActionGroupPopupLoader",
976 "ActionPopupPMOpenWith");
977 g_object_set (G_OBJECT (action
), "sensitive", TRUE
, NULL
);
979 if (fl_plugin
->pm_current_uri
)
980 g_free (fl_plugin
->pm_current_uri
);
981 fl_plugin
->pm_current_uri
= g_strdup (uri
);
984 gtk_ui_manager_get_widget (GTK_UI_MANAGER(ui
),
985 "/PopupProjectManager/PlaceholderPopupProjectOpen/OpenWith");
986 if (!create_open_with_submenu (fl_plugin
, parentmenu
, uri
,
987 G_CALLBACK (pm_open_with
), plugin
))
988 g_object_set (G_OBJECT (action
), "sensitive", FALSE
, NULL
);
992 value_removed_pm_current_uri (AnjutaPlugin
*plugin
,
993 const char *name
, gpointer data
)
997 AnjutaFileLoaderPlugin
*fl_plugin
;
999 fl_plugin
= ANJUTA_PLUGIN_FILE_LOADER (plugin
);
1001 if (fl_plugin
->pm_current_uri
)
1002 g_free (fl_plugin
->pm_current_uri
);
1003 fl_plugin
->pm_current_uri
= NULL
;
1005 ui
= anjuta_shell_get_ui (plugin
->shell
, NULL
);
1006 action
= anjuta_ui_get_action (ui
, "ActionGroupPopupLoader", "ActionPopupPMOpen");
1007 g_object_set (G_OBJECT (action
), "sensitive", FALSE
, NULL
);
1009 action
= anjuta_ui_get_action (ui
, "ActionGroupPopupLoader",
1010 "ActionPopupPMOpenWith");
1011 g_object_set (G_OBJECT (action
), "sensitive", FALSE
, NULL
);
1015 dnd_dropped (const gchar
*uri
, gpointer plugin
)
1017 ianjuta_file_loader_load (IANJUTA_FILE_LOADER (plugin
), uri
, FALSE
, NULL
);
1021 on_session_load (AnjutaShell
*shell
, AnjutaSessionPhase phase
,
1022 AnjutaSession
*session
,
1023 AnjutaFileLoaderPlugin
*plugin
)
1025 AnjutaStatus
*status
;
1028 GList
*files
, *node
;
1031 /* We want to load the files first before other session loads */
1032 if (phase
!= ANJUTA_SESSION_PHASE_FIRST
)
1035 files
= anjuta_session_get_string_list (session
, "File Loader", "Files");
1039 status
= anjuta_shell_get_status (shell
, NULL
);
1040 anjuta_status_progress_add_ticks (status
, g_list_length(files
));
1042 /* Open project files first and then regular files */
1043 for (i
= 0; i
< 2; i
++)
1051 gchar
*label
, *filename
;
1053 mime_type
= anjuta_util_get_uri_mime_type (uri
);
1055 filename
= g_path_get_basename (uri
);
1056 if (strchr (filename
, '#'))
1057 *(strchr (filename
, '#')) = '\0';
1059 label
= g_strconcat ("Loaded: ", filename
, NULL
);
1061 if (i
== 0 && mime_type
&&
1062 strcmp (mime_type
, "application/x-anjuta") == 0)
1064 /* Project files first */
1065 /* FIXME: Ignore project files for now */
1067 ianjuta_file_loader_load (IANJUTA_FILE_LOADER (plugin),
1070 anjuta_status_progress_tick (status
, NULL
, label
);
1074 strcmp (mime_type
, "application/x-anjuta") != 0))
1076 /* Then rest of the files */
1077 ianjuta_file_loader_load (IANJUTA_FILE_LOADER (plugin
),
1079 anjuta_status_progress_tick (status
, NULL
, label
);
1085 node
= g_list_next (node
);
1090 g_list_foreach (files
, (GFunc
)g_free
, NULL
);
1091 g_list_free (files
);
1096 activate_plugin (AnjutaPlugin
*plugin
)
1098 EggSubmenuAction
*saction
;
1101 AnjutaFileLoaderPlugin
*loader_plugin
;
1102 // GtkWidget *recent_menu;
1103 // EggRecentViewGtk *recent_view;
1104 GtkActionGroup
*group
;
1106 loader_plugin
= ANJUTA_PLUGIN_FILE_LOADER (plugin
);
1108 DEBUG_PRINT ("AnjutaFileLoaderPlugin: Activating File Loader plugin...");
1110 ui
= anjuta_shell_get_ui (plugin
->shell
, NULL
);
1112 /* Add action group */
1113 loader_plugin
->action_group
=
1114 anjuta_ui_add_action_group_entries (ui
, "ActionGroupLoader",
1117 G_N_ELEMENTS (actions_file
),
1118 GETTEXT_PACKAGE
, TRUE
, plugin
);
1119 loader_plugin
->popup_action_group
=
1120 anjuta_ui_add_action_group_entries (ui
, "ActionGroupPopupLoader",
1123 G_N_ELEMENTS (popup_actions_file
),
1124 GETTEXT_PACKAGE
, FALSE
, plugin
);
1125 saction
= g_object_new (EGG_TYPE_SUBMENU_ACTION
,
1126 "name", "ActionFileWizard",
1128 "tooltip", _("New file, project and project components."),
1130 egg_submenu_action_set_menu_factory (saction
,
1131 on_create_submenu
, plugin
);
1132 gtk_action_group_add_action (loader_plugin
->action_group
,
1133 GTK_ACTION (saction
));
1135 /* Set short labels */
1136 action
= anjuta_ui_get_action (ui
, "ActionGroupLoader", "ActionFileOpen");
1137 g_object_set (G_OBJECT (action
), "short-label", _("Open"),
1138 "is-important", TRUE
, NULL
);
1140 group
= gtk_action_group_new ("ActionGroupLoaderRecent");
1141 action
= g_object_new (EGG_TYPE_RECENT_ACTION
,
1142 "name", "ActionFileOpenRecent",
1143 "label", _("Open _Recent"),
1144 "tooltip", _("Open recent file"),
1146 egg_recent_action_add_model (EGG_RECENT_ACTION (action
),
1147 loader_plugin
->recent_files_model_top
);
1148 egg_recent_action_add_model (EGG_RECENT_ACTION (action
),
1149 loader_plugin
->recent_files_model_bottom
);
1150 g_signal_connect (action
, "activate",
1151 G_CALLBACK (on_open_recent_file
), plugin
);
1152 gtk_action_group_add_action (group
, action
);
1153 anjuta_ui_add_action_group (ui
, "ActionGroupLoaderRecent",
1154 N_("Open recent files"), group
, FALSE
);
1155 loader_plugin
->recent_group
= group
;
1158 loader_plugin
->uiid
= anjuta_ui_merge (ui
, UI_FILE
);
1160 /* Install drag n drop handler */
1161 dnd_drop_init (GTK_WIDGET (plugin
->shell
), dnd_dropped
, plugin
,
1162 "text/plain", "text/html", "text/source", "application-x/anjuta",
1166 loader_plugin
->fm_watch_id
=
1167 anjuta_plugin_add_watch (plugin
, "file_manager_current_uri",
1168 value_added_fm_current_uri
,
1169 value_removed_fm_current_uri
, NULL
);
1170 loader_plugin
->pm_watch_id
=
1171 anjuta_plugin_add_watch (plugin
, "project_manager_current_uri",
1172 value_added_pm_current_uri
,
1173 value_removed_pm_current_uri
, NULL
);
1175 /* Connect to session */
1176 g_signal_connect (G_OBJECT (plugin
->shell
), "load_session",
1177 G_CALLBACK (on_session_load
), plugin
);
1182 deactivate_plugin (AnjutaPlugin
*plugin
)
1185 AnjutaFileLoaderPlugin
*loader_plugin
;
1187 loader_plugin
= ANJUTA_PLUGIN_FILE_LOADER (plugin
);
1189 DEBUG_PRINT ("AnjutaFileLoaderPlugin: Deactivating File Loader plugin...");
1191 /* Disconnect session */
1192 g_signal_handlers_disconnect_by_func (G_OBJECT (plugin
->shell
),
1193 G_CALLBACK (on_session_load
), plugin
);
1195 ui
= anjuta_shell_get_ui (plugin
->shell
, NULL
);
1196 /* Remove watches */
1197 anjuta_plugin_remove_watch (plugin
, loader_plugin
->fm_watch_id
, TRUE
);
1198 anjuta_plugin_remove_watch (plugin
, loader_plugin
->pm_watch_id
, TRUE
);
1200 dnd_drop_finalize (GTK_WIDGET (plugin
->shell
), plugin
);
1202 anjuta_ui_unmerge (ui
, loader_plugin
->uiid
);
1203 /* Remove action group */
1204 anjuta_ui_remove_action_group (ui
, loader_plugin
->action_group
);
1205 anjuta_ui_remove_action_group (ui
, loader_plugin
->popup_action_group
);
1206 anjuta_ui_remove_action_group (ui
, loader_plugin
->recent_group
);
1211 dispose (GObject
*obj
)
1213 AnjutaFileLoaderPlugin
*plugin
= ANJUTA_PLUGIN_FILE_LOADER (obj
);
1214 if (plugin
->recent_files_model_top
)
1216 g_object_unref (plugin
->recent_files_model_top
);
1217 plugin
->recent_files_model_top
= NULL
;
1219 if (plugin
->recent_files_model_bottom
)
1221 g_object_unref (plugin
->recent_files_model_bottom
);
1222 plugin
->recent_files_model_bottom
= NULL
;
1224 GNOME_CALL_PARENT (G_OBJECT_CLASS
, dispose
, (obj
));
1228 anjuta_file_loader_plugin_instance_init (GObject
*obj
)
1230 AnjutaFileLoaderPlugin
*plugin
= ANJUTA_PLUGIN_FILE_LOADER (obj
);
1232 plugin
->fm_current_uri
= NULL
;
1233 plugin
->pm_current_uri
= NULL
;
1235 plugin
->recent_files_model_top
=
1236 egg_recent_model_new (EGG_RECENT_MODEL_SORT_MRU
);
1237 egg_recent_model_set_limit (plugin
->recent_files_model_top
, 5);
1238 egg_recent_model_set_filter_groups (plugin
->recent_files_model_top
,
1239 "anjuta-projects", NULL
);
1240 plugin
->recent_files_model_bottom
=
1241 egg_recent_model_new (EGG_RECENT_MODEL_SORT_MRU
);
1242 egg_recent_model_set_limit (plugin
->recent_files_model_bottom
, 15);
1243 egg_recent_model_set_filter_groups (plugin
->recent_files_model_bottom
,
1244 "anjuta-files", NULL
);
1248 anjuta_file_loader_plugin_class_init (GObjectClass
*klass
)
1250 AnjutaPluginClass
*plugin_class
= ANJUTA_PLUGIN_CLASS (klass
);
1252 parent_class
= g_type_class_peek_parent (klass
);
1254 plugin_class
->activate
= activate_plugin
;
1255 plugin_class
->deactivate
= deactivate_plugin
;
1256 klass
->dispose
= dispose
;
1260 iloader_load (IAnjutaFileLoader
*loader
, const gchar
*uri
,
1261 gboolean read_only
, GError
**err
)
1265 GnomeVFSURI
* vfs_uri
;
1266 AnjutaStatus
*status
;
1267 AnjutaPluginManager
*plugin_manager
;
1268 GList
*plugin_descs
= NULL
;
1269 GObject
*plugin
= NULL
;
1271 g_return_val_if_fail (uri
!= NULL
, NULL
);
1272 vfs_uri
= gnome_vfs_uri_new (uri
);
1274 if (!gnome_vfs_uri_exists (vfs_uri
))
1276 launch_application_failure (ANJUTA_PLUGIN_FILE_LOADER (loader
),
1277 uri
, GNOME_VFS_ERROR_NOT_FOUND
);
1278 gnome_vfs_uri_unref (vfs_uri
);
1282 new_uri
= gnome_vfs_uri_to_string (vfs_uri
,
1283 GNOME_VFS_URI_HIDE_FRAGMENT_IDENTIFIER
);
1284 mime_type
= anjuta_util_get_uri_mime_type (new_uri
);
1285 gnome_vfs_uri_unref (vfs_uri
);
1287 if (mime_type
== NULL
)
1289 launch_application_failure (ANJUTA_PLUGIN_FILE_LOADER (loader
),
1290 new_uri
, GNOME_VFS_ERROR_NOT_FOUND
);
1295 plugin_manager
= anjuta_shell_get_plugin_manager (ANJUTA_PLUGIN (loader
)->shell
,
1297 status
= anjuta_shell_get_status (ANJUTA_PLUGIN (loader
)->shell
, NULL
);
1298 anjuta_status_busy_push (status
);
1300 DEBUG_PRINT ("Opening URI: %s", uri
);
1302 plugin_descs
= anjuta_plugin_manager_query (plugin_manager
,
1304 "Interfaces", "IAnjutaFile",
1306 "SupportedMimeTypes",
1310 if (g_list_length (plugin_descs
) > 1)
1313 anjuta_plugin_manager_select_and_activate (plugin_manager
,
1315 "Please select a plugin to open this file.",
1318 else if (g_list_length (plugin_descs
) == 1)
1320 gchar
*location
= NULL
;
1322 AnjutaPluginDescription
*desc
= plugin_descs
->data
;
1323 anjuta_plugin_description_get_string (desc
, "Anjuta Plugin",
1324 "Location", &location
);
1325 g_return_val_if_fail (location
!= NULL
, NULL
);
1327 anjuta_plugin_manager_get_plugin_by_id (plugin_manager
,
1333 launch_in_default_application (ANJUTA_PLUGIN_FILE_LOADER (loader
), mime_type
, uri
);
1336 ianjuta_file_open (IANJUTA_FILE(plugin
), uri
, NULL
);
1338 set_recent_file (ANJUTA_PLUGIN_FILE_LOADER (loader
), new_uri
, mime_type
);
1341 g_list_free (plugin_descs
);
1344 anjuta_status_busy_pop (status
);
1350 iloader_iface_init(IAnjutaLoaderIface
*iface
)
1352 iface
->find_plugins
= NULL
;
1356 iloader_file_iface_init(IAnjutaFileLoaderIface
*iface
)
1358 iface
->load
= iloader_load
;
1361 ANJUTA_PLUGIN_BEGIN (AnjutaFileLoaderPlugin
, anjuta_file_loader_plugin
);
1362 ANJUTA_PLUGIN_ADD_INTERFACE (iloader
, IANJUTA_TYPE_LOADER
);
1363 ANJUTA_PLUGIN_ADD_INTERFACE (iloader_file
, IANJUTA_TYPE_FILE_LOADER
);
1366 ANJUTA_SIMPLE_PLUGIN (AnjutaFileLoaderPlugin
, anjuta_file_loader_plugin
);