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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 <libanjuta/anjuta-shell.h>
27 #include <libanjuta/anjuta-status.h>
28 #include <libanjuta/anjuta-debug.h>
30 #include <libanjuta/interfaces/ianjuta-file.h>
31 #include <libanjuta/interfaces/ianjuta-file-loader.h>
32 #include <libanjuta/interfaces/ianjuta-document-manager.h>
33 #include <libanjuta/interfaces/ianjuta-wizard.h>
34 #include <libegg/menu/egg-submenu-action.h>
35 #include <libegg/menu/egg-recent-action.h>
40 #define UI_FILE PACKAGE_DATA_DIR"/ui/anjuta-loader-plugin.ui"
42 static gpointer parent_class
;
45 sort_wizards(gconstpointer wizard1
, gconstpointer wizard2
)
48 AnjutaPluginDescription
* desc1
= (AnjutaPluginDescription
*) wizard1
;
49 AnjutaPluginDescription
* desc2
= (AnjutaPluginDescription
*) wizard2
;
51 if ((anjuta_plugin_description_get_string (desc1
, "Wizard",
53 anjuta_plugin_description_get_string (desc1
, "Anjuta Plugin",
55 (anjuta_plugin_description_get_string (desc2
, "Wizard",
57 anjuta_plugin_description_get_string (desc2
, "Anjuta Plugin",
60 return strcmp(name1
, name2
);
67 set_recent_file (AnjutaFileLoaderPlugin
*plugin
, const gchar
*uri
,
70 EggRecentItem
*recent_item
;
71 DEBUG_PRINT ("Adding recent item of mimi-type: %s", mime
);
72 recent_item
= egg_recent_item_new ();
73 egg_recent_item_set_uri (recent_item
, uri
);
74 egg_recent_item_set_mime_type (recent_item
, mime
);
75 if (strcmp (mime
, "application/x-anjuta") == 0)
77 egg_recent_item_add_group (recent_item
, "anjuta-projects");
78 egg_recent_model_add_full (plugin
->recent_files_model_top
,
83 egg_recent_item_add_group (recent_item
, "anjuta-files");
84 egg_recent_model_add_full (plugin
->recent_files_model_bottom
,
90 launch_application_failure (AnjutaFileLoaderPlugin
*plugin
,
92 GnomeVFSResult result
)
97 errmsg
= gnome_vfs_result_to_string (result
);
99 gtk_widget_get_toplevel (GTK_WIDGET(ANJUTA_PLUGIN (plugin
)->shell
));
100 anjuta_util_dialog_error (GTK_WINDOW (parent
),
101 "Can not open \"%s\".\n\n%s",
102 g_basename (uri
), errmsg
);
106 get_available_plugins_for_mime (AnjutaFileLoaderPlugin
* plugin
,
107 const gchar
*mime_type
)
109 AnjutaPluginManager
*plugin_manager
;
110 GList
*plugin_descs
= NULL
;
112 g_return_val_if_fail (mime_type
!= NULL
, NULL
);
114 plugin_manager
= anjuta_shell_get_plugin_manager (ANJUTA_PLUGIN(plugin
)->shell
,
116 plugin_descs
= anjuta_plugin_manager_query (plugin_manager
,
118 "Interfaces", "IAnjutaFile",
120 "SupportedMimeTypes",
127 open_with_dialog (AnjutaFileLoaderPlugin
*plugin
, const gchar
*uri
,
128 const gchar
*mime_type
)
130 GList
*plugin_descs
, *snode
;
131 GList
*mime_apps
, *node
;
132 GtkWidget
*menu
, *menuitem
;
133 GnomeVFSMimeApplication
*mime_app
;
135 GtkWidget
*dialog
, *parent
, *hbox
, *label
;
138 AnjutaPluginManager
*plugin_manager
;
140 plugin_manager
= anjuta_shell_get_plugin_manager (ANJUTA_PLUGIN (plugin
)->shell
,
142 message
= g_strdup_printf (_("<b>Cannot open \"%s\"</b>.\n\n"
143 "There is no plugin, default action, or application "
144 "configured to handle this file type.\n"
148 "You may choose to try opening it with the following "
149 "plugins or applications."),
150 g_basename(uri
), mime_type
);
152 gtk_widget_get_toplevel (GTK_WIDGET(ANJUTA_PLUGIN (plugin
)->shell
));
153 dialog
= gtk_message_dialog_new_with_markup (GTK_WINDOW (parent
),
154 GTK_DIALOG_DESTROY_WITH_PARENT
,
156 GTK_BUTTONS_OK_CANCEL
, message
);
159 hbox
= gtk_hbox_new (FALSE
, 5);
160 gtk_box_pack_start (GTK_BOX (GTK_DIALOG(dialog
)->vbox
), hbox
,
162 label
= gtk_label_new (_("Open with:"));
163 options
= gtk_option_menu_new ();
164 gtk_box_pack_end (GTK_BOX(hbox
), options
, FALSE
, FALSE
, 10);
165 gtk_box_pack_end (GTK_BOX(hbox
), label
, FALSE
, FALSE
, 10);
167 menu
= gtk_menu_new ();
169 /* Document manager plugin */
170 menuitem
= gtk_menu_item_new_with_label (_("Document Manager"));
171 gtk_menu_append (menu
, menuitem
);
173 /* Open with plugins menu items */
174 plugin_descs
= get_available_plugins_for_mime (plugin
, mime_type
);
175 snode
= plugin_descs
;
179 AnjutaPluginDescription
*desc
;
181 desc
= (AnjutaPluginDescription
*)(snode
->data
);
185 anjuta_plugin_description_get_string (desc
, "File Loader",
190 anjuta_plugin_description_get_string (desc
, "Anjuta Plugin",
195 anjuta_plugin_description_get_string (desc
, "Anjuta Plugin",
198 menuitem
= gtk_menu_item_new_with_label (name
);
199 gtk_menu_append (menu
, menuitem
);
201 snode
= g_list_next (snode
);
204 /* Open with application menu items */
205 mime_apps
= gnome_vfs_mime_get_all_applications (mime_type
);
209 menuitem
= gtk_menu_item_new ();
210 gtk_menu_append (menu
, menuitem
);
215 mime_app
= (GnomeVFSMimeApplication
*)(node
->data
);
216 menuitem
= gtk_menu_item_new_with_label (mime_app
->name
);
217 gtk_menu_append (menu
, menuitem
);
218 node
= g_list_next (node
);
221 gtk_option_menu_set_menu (GTK_OPTION_MENU (options
), menu
);
222 gtk_widget_show_all (hbox
);
224 if (gtk_dialog_run (GTK_DIALOG (dialog
)) == GTK_RESPONSE_OK
)
228 option
= gtk_option_menu_get_history(GTK_OPTION_MENU (options
));
231 IAnjutaDocumentManager
*docman
;
232 docman
= anjuta_shell_get_interface (ANJUTA_PLUGIN (plugin
)->shell
,
233 IAnjutaDocumentManager
,
237 ianjuta_file_open (IANJUTA_FILE (docman
), uri
, NULL
);
241 g_warning ("No document manager plugin!!");
244 else if (option
< (g_list_length (plugin_descs
) + 1))
246 AnjutaPluginDescription
*desc
;
247 gchar
*location
= NULL
;
250 desc
= g_list_nth_data (plugin_descs
, option
);
251 anjuta_plugin_description_get_string (desc
, "Anjuta Plugin",
252 "Location", &location
);
253 g_assert (location
!= NULL
);
254 if (location
!= NULL
)
256 GObject
*loaded_plugin
;
259 anjuta_plugin_manager_get_plugin_by_id (plugin_manager
,
263 ianjuta_file_open (IANJUTA_FILE (loaded_plugin
), uri
, NULL
);
264 set_recent_file (plugin
, uri
, mime_type
);
268 anjuta_util_dialog_error (GTK_WINDOW (ANJUTA_PLUGIN(plugin
)->shell
),
269 "Failed to activate plugin: %s",
280 option
-= (g_list_length (plugin_descs
) + 2);
281 mime_app
= g_list_nth_data (mime_apps
, option
);
282 uris
= g_list_prepend (uris
, (gpointer
)uri
);
283 res
= gnome_vfs_mime_application_launch (mime_app
, uris
);
284 if (res
!= GNOME_VFS_OK
)
285 launch_application_failure (plugin
, uri
, res
);
287 set_recent_file (plugin
, uri
, mime_type
);
291 gnome_vfs_mime_application_list_free (mime_apps
);
293 g_list_free (plugin_descs
);
294 gtk_widget_destroy (dialog
);
298 launch_in_default_application (AnjutaFileLoaderPlugin
*plugin
,
299 const gchar
*mime_type
, const gchar
*uri
)
301 GnomeVFSMimeAction
*action
;
305 uris
= g_list_prepend (uris
, (gpointer
)uri
);
308 action
= gnome_vfs_mime_get_default_action (mime_type
);
309 if (!action
|| gnome_vfs_mime_action_launch (action
, uris
) != GNOME_VFS_OK
)
311 GnomeVFSMimeApplication
*app
;
313 app
= gnome_vfs_mime_get_default_application (mime_type
);
316 gnome_vfs_mime_application_launch (app
, uris
)) != GNOME_VFS_OK
)
318 open_with_dialog (plugin
, uri
, mime_type
);
321 gnome_vfs_mime_application_free (app
);
324 gnome_vfs_mime_action_free (action
);
330 open_file (AnjutaFileLoaderPlugin
*plugin
, const gchar
*uri
)
332 GnomeVFSURI
*vfs_uri
;
335 vfs_uri
= gnome_vfs_uri_new (uri
);
336 dirname
= gnome_vfs_uri_extract_dirname (vfs_uri
);
337 gnome_vfs_uri_unref (vfs_uri
);
341 ianjuta_file_loader_load (IANJUTA_FILE_LOADER (plugin
),
347 AnjutaFileLoaderPlugin
*plugin
;
349 } RecentIdelOpenData
;
352 on_open_recent_file_idle (gpointer data
)
354 RecentIdelOpenData
*rdata
;
356 rdata
= (RecentIdelOpenData
*)data
;
357 open_file (rdata
->plugin
, rdata
->uri
);
363 on_open_recent_file (EggRecentAction
*action
, AnjutaFileLoaderPlugin
*plugin
)
366 GnomeVFSURI
*vfs_uri
;
368 RecentIdelOpenData
*rdata
;
370 uri
= egg_recent_action_get_selected_uri (action
);
371 vfs_uri
= gnome_vfs_uri_new (uri
);
372 rdata
= g_new0 (RecentIdelOpenData
, 1);
373 rdata
->plugin
= plugin
;
375 g_idle_add (on_open_recent_file_idle
, rdata
);
376 gnome_vfs_uri_unref (vfs_uri
);
382 on_open_response_ok (GtkDialog
* dialog
, gint id
,
383 AnjutaFileLoaderPlugin
*plugin
)
387 if (id
!= GTK_RESPONSE_ACCEPT
)
389 gtk_widget_destroy (GTK_WIDGET (dialog
));
393 list
= gtk_file_chooser_get_uris (GTK_FILE_CHOOSER (dialog
));
398 open_file (plugin
, (gchar
*)node
->data
);
400 node
= g_slist_next (node
);
406 setup_file_filters (GtkFileChooser
*fc
)
408 GtkFileFilter
*filter
;
410 filter
= gtk_file_filter_new ();
411 gtk_file_filter_set_name (filter
, _("All files"));
412 gtk_file_filter_add_pattern (filter
, "*");
413 gtk_file_chooser_add_filter (fc
, filter
);
415 filter
= gtk_file_filter_new ();
416 gtk_file_filter_set_name (filter
, _("Anjuta Projects"));
417 gtk_file_filter_add_pattern (filter
, "*.anjuta");
418 gtk_file_filter_add_pattern (filter
, "*.prj");
419 gtk_file_chooser_add_filter (fc
, filter
);
421 filter
= gtk_file_filter_new ();
422 gtk_file_filter_set_name (filter
, _("C/C++ source files"));
423 gtk_file_filter_add_pattern (filter
, "*.c");
424 gtk_file_filter_add_pattern (filter
, "*.cc");
425 gtk_file_filter_add_pattern (filter
, "*.cpp");
426 gtk_file_filter_add_pattern (filter
, "*.cxx");
427 gtk_file_filter_add_pattern (filter
, "*.c++");
428 gtk_file_filter_add_pattern (filter
, "*.h");
429 gtk_file_filter_add_pattern (filter
, "*.hh");
430 gtk_file_filter_add_pattern (filter
, "*.hpp");
431 gtk_file_chooser_add_filter (fc
, filter
);
433 filter
= gtk_file_filter_new ();
434 gtk_file_filter_set_name (filter
, _("C# source files"));
435 gtk_file_filter_add_pattern (filter
, "*.cs");
436 gtk_file_filter_add_pattern (filter
, "*.h");
437 gtk_file_chooser_add_filter (fc
, filter
);
439 filter
= gtk_file_filter_new ();
440 gtk_file_filter_set_name (filter
, _("Java source files"));
441 gtk_file_filter_add_pattern (filter
, "*.java");
442 gtk_file_filter_add_pattern (filter
, "*.js");
443 gtk_file_chooser_add_filter (fc
, filter
);
445 filter
= gtk_file_filter_new ();
446 gtk_file_filter_set_name (filter
, _("Pascal source files"));
447 gtk_file_filter_add_pattern (filter
, "*.pas");
448 gtk_file_chooser_add_filter (fc
, filter
);
450 filter
= gtk_file_filter_new ();
451 gtk_file_filter_set_name (filter
, _("PHP source files"));
452 gtk_file_filter_add_pattern (filter
, "*.php");
453 gtk_file_filter_add_pattern (filter
, "*.php?");
454 gtk_file_filter_add_pattern (filter
, "*.phtml");
455 gtk_file_chooser_add_filter (fc
, filter
);
457 filter
= gtk_file_filter_new ();
458 gtk_file_filter_set_name (filter
, _("Perl source files"));
459 gtk_file_filter_add_pattern (filter
, "*.pl");
460 gtk_file_filter_add_pattern (filter
, "*.pm");
461 gtk_file_chooser_add_filter (fc
, filter
);
463 filter
= gtk_file_filter_new ();
464 gtk_file_filter_set_name (filter
, _("Python source files"));
465 gtk_file_filter_add_pattern (filter
, "*.py");
466 gtk_file_chooser_add_filter (fc
, filter
);
468 filter
= gtk_file_filter_new ();
469 gtk_file_filter_set_name (filter
, _("Hyper text markup files"));
470 gtk_file_filter_add_pattern (filter
, "*.htm");
471 gtk_file_filter_add_pattern (filter
, "*.html");
472 gtk_file_filter_add_pattern (filter
, "*.xhtml");
473 gtk_file_filter_add_pattern (filter
, "*.dhtml");
474 gtk_file_filter_add_pattern (filter
, "*.cs");
475 gtk_file_chooser_add_filter (fc
, filter
);
477 filter
= gtk_file_filter_new ();
478 gtk_file_filter_set_name (filter
, _("Shell scripts files"));
479 gtk_file_filter_add_pattern (filter
, "*.sh");
480 gtk_file_chooser_add_filter (fc
, filter
);
482 filter
= gtk_file_filter_new ();
483 gtk_file_filter_set_name (filter
, _("Makefiles"));
484 gtk_file_filter_add_pattern (filter
, "Makefile*");
485 gtk_file_filter_add_pattern (filter
, "makefile*");
486 gtk_file_chooser_add_filter (fc
, filter
);
488 filter
= gtk_file_filter_new ();
489 gtk_file_filter_set_name (filter
, _("Lua files"));
490 gtk_file_filter_add_pattern (filter
, "*.lua");
491 gtk_file_chooser_add_filter (fc
, filter
);
493 filter
= gtk_file_filter_new ();
494 gtk_file_filter_set_name (filter
, _("Diff files"));
495 gtk_file_filter_add_pattern (filter
, "*.diff");
496 gtk_file_filter_add_pattern (filter
, "*.patch");
497 gtk_file_filter_add_pattern (filter
, "*.cvsdiff");
498 gtk_file_chooser_add_filter (fc
, filter
);
502 create_file_open_dialog_gui(GtkWindow
* parent
, AnjutaFileLoaderPlugin
* plugin
)
505 gtk_file_chooser_dialog_new (_("Open file"),
507 GTK_FILE_CHOOSER_ACTION_OPEN
,
508 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
509 GTK_STOCK_OPEN
, GTK_RESPONSE_ACCEPT
,
511 gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER(dialog
), TRUE
);
512 gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (dialog
), FALSE
);
513 setup_file_filters (GTK_FILE_CHOOSER (dialog
));
515 g_signal_connect(G_OBJECT(dialog
), "response",
516 G_CALLBACK(on_open_response_ok
), plugin
);
517 g_signal_connect_swapped (GTK_OBJECT (dialog
),
519 G_CALLBACK (gtk_widget_destroy
),
520 GTK_OBJECT (dialog
));
525 on_open_activate (GtkAction
*action
, AnjutaFileLoaderPlugin
*plugin
)
530 create_file_open_dialog_gui (GTK_WINDOW (ANJUTA_PLUGIN(plugin
)->shell
),
532 gtk_widget_show (dlg
);
536 on_new_activate (GtkAction
*action
, gpointer data
)
538 IAnjutaDocumentManager
*docman
;
539 docman
= anjuta_shell_get_interface (ANJUTA_PLUGIN (data
)->shell
,
540 IAnjutaDocumentManager
, NULL
);
543 ianjuta_document_manager_add_buffer (docman
, "", "", NULL
);
548 on_activate_wizard (GtkMenuItem
*menuitem
,
549 AnjutaFileLoaderPlugin
*loader
)
551 AnjutaPluginManager
*plugin_manager
;
552 AnjutaPluginDescription
*desc
;
554 desc
= g_object_get_data (G_OBJECT (menuitem
), "__plugin_desc");
555 plugin_manager
= anjuta_shell_get_plugin_manager (ANJUTA_PLUGIN (loader
)->shell
,
562 if (anjuta_plugin_description_get_string (desc
, "Anjuta Plugin",
566 anjuta_plugin_manager_get_plugin_by_id (plugin_manager
, id
);
567 ianjuta_wizard_activate (IANJUTA_WIZARD (plugin
), NULL
);
573 on_create_submenu (gpointer user_data
)
575 AnjutaPluginManager
*plugin_manager
;
576 AnjutaFileLoaderPlugin
*loader
;
579 GtkWidget
*submenu
= NULL
;
580 GList
*plugin_descs
= NULL
;
582 loader
= ANJUTA_PLUGIN_FILE_LOADER (user_data
);
583 plugin_manager
= anjuta_shell_get_plugin_manager (ANJUTA_PLUGIN (loader
)->shell
,
585 submenu
= gtk_menu_new ();
586 gtk_widget_show (submenu
);
588 plugin_descs
= anjuta_plugin_manager_query (plugin_manager
,
590 "Interfaces", "IAnjutaWizard",
592 plugin_descs
= g_list_sort(plugin_descs
, sort_wizards
);
597 AnjutaPluginDescription
*desc
;
600 gchar
*str
, *icon_path
, *name
;
606 if (anjuta_plugin_description_get_string (desc
, "Wizard",
608 anjuta_plugin_description_get_string (desc
, "Anjuta Plugin",
613 name
= g_strdup_printf ("_%d. %s", count
, str
);
615 name
= g_strdup_printf ("%d. %s", count
, str
);
618 if (anjuta_plugin_description_get_string (desc
, "Anjuta Plugin",
621 GdkPixbuf
*pixbuf
, *scaled_pixbuf
;
624 gtk_icon_size_lookup_for_settings (gtk_widget_get_settings (submenu
),
627 icon_path
= g_build_filename (PACKAGE_PIXMAPS_DIR
, str
, NULL
);
628 pixbuf
= gdk_pixbuf_new_from_file (icon_path
, NULL
);
631 scaled_pixbuf
= gdk_pixbuf_scale_simple (pixbuf
, width
, height
,
632 GDK_INTERP_BILINEAR
);
633 icon
= gtk_image_new_from_pixbuf (scaled_pixbuf
);
634 g_object_unref (pixbuf
);
635 g_object_unref (scaled_pixbuf
);
638 icon
= gtk_image_new ();
640 gtk_widget_show (icon
);
646 menuitem
= gtk_image_menu_item_new_with_mnemonic (name
);
647 gtk_widget_show (menuitem
);
648 g_object_set_data (G_OBJECT (menuitem
), "__plugin_desc", desc
);
649 g_signal_connect (G_OBJECT (menuitem
), "activate",
650 G_CALLBACK (on_activate_wizard
),
653 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem
),
655 gtk_menu_shell_append (GTK_MENU_SHELL (submenu
), menuitem
);
657 node
= g_list_next (node
);
659 g_list_free (plugin_descs
);
665 on_recent_files_tooltip (GtkTooltips *tooltips, GtkWidget *menu_item,
666 EggRecentItem *item, gpointer user_data)
670 uri = egg_recent_item_get_uri_for_display (item);
671 tip = g_strdup_printf ("Open '%s'", uri);
673 gtk_tooltips_set_tip (tooltips, menu_item, tip, NULL);
680 open_file_with (AnjutaFileLoaderPlugin
*plugin
, GtkMenuItem
*menuitem
,
684 GnomeVFSMimeApplication
*mime_app
;
687 AnjutaPluginDescription
*desc
;
688 AnjutaPluginManager
*plugin_manager
;
690 idx
= GPOINTER_TO_INT (g_object_get_data (G_OBJECT (menuitem
), "index"));
691 desc
= (AnjutaPluginDescription
*) g_object_get_data (G_OBJECT (menuitem
),
693 plugin_manager
= anjuta_shell_get_plugin_manager (ANJUTA_PLUGIN (plugin
)->shell
,
695 mime_type
= anjuta_util_get_uri_mime_type (uri
);
696 mime_apps
= gnome_vfs_mime_get_all_applications (mime_type
);
698 /* Open with plugin */
701 gchar
*location
= NULL
;
703 anjuta_plugin_description_get_string (desc
, "Anjuta Plugin",
704 "Location", &location
);
705 g_assert (location
!= NULL
);
706 if (location
!= NULL
)
708 GObject
*loaded_plugin
;
711 anjuta_plugin_manager_get_plugin_by_id (plugin_manager
,
715 ianjuta_file_open (IANJUTA_FILE (loaded_plugin
), uri
, NULL
);
716 set_recent_file (plugin
, uri
, mime_type
);
720 anjuta_util_dialog_error (GTK_WINDOW (ANJUTA_PLUGIN(plugin
)->shell
),
721 _("Failed to activate plugin: %s"),
732 mime_app
= g_list_nth_data (mime_apps
, idx
);
733 uris
= g_list_prepend (uris
, (gpointer
)uri
);
734 res
= gnome_vfs_mime_application_launch (mime_app
, uris
);
735 if (res
!= GNOME_VFS_OK
)
736 launch_application_failure (plugin
, uri
, res
);
738 set_recent_file (plugin
, uri
, mime_type
);
741 gnome_vfs_mime_application_list_free (mime_apps
);
746 fm_open (GtkAction
*action
, AnjutaFileLoaderPlugin
*plugin
)
748 if (plugin
->fm_current_uri
)
749 open_file (plugin
, plugin
->fm_current_uri
);
753 fm_open_with (GtkMenuItem
*menuitem
, AnjutaFileLoaderPlugin
*plugin
)
755 if (plugin
->fm_current_uri
)
756 open_file_with (plugin
, menuitem
, plugin
->fm_current_uri
);
760 pm_open (GtkAction
*action
, AnjutaFileLoaderPlugin
*plugin
)
762 if (plugin
->pm_current_uri
)
763 open_file (plugin
, plugin
->pm_current_uri
);
767 pm_open_with (GtkMenuItem
*menuitem
, AnjutaFileLoaderPlugin
*plugin
)
769 if (plugin
->pm_current_uri
)
770 open_file_with (plugin
, menuitem
, plugin
->pm_current_uri
);
773 static GtkActionEntry actions_file
[] = {
779 N_("New empty editor."),
780 G_CALLBACK (on_new_activate
)
788 G_CALLBACK (on_open_activate
)
792 static GtkActionEntry popup_actions_file
[] = {
801 "ActionPopupOpenWith",
803 N_("Open _With"), NULL
,
804 N_("Open with"), NULL
814 "ActionPopupPMOpenWith",
816 N_("Open _With"), NULL
,
817 N_("Open with"), NULL
822 create_open_with_submenu (AnjutaFileLoaderPlugin
*plugin
, GtkWidget
*parentmenu
,
823 const gchar
*uri
, GCallback callback
,
824 gpointer callback_data
)
826 GList
*mime_apps
, *node
;
827 GnomeVFSMimeApplication
*mime_app
;
828 GList
*plugin_descs
, *snode
;
829 GtkWidget
*menu
, *menuitem
;
834 g_return_val_if_fail (GTK_IS_MENU_ITEM (parentmenu
), FALSE
);
836 menu
= gtk_menu_new ();
837 gtk_widget_show (menu
);
838 gtk_menu_item_set_submenu (GTK_MENU_ITEM (parentmenu
), menu
);
840 mime_type
= anjuta_util_get_uri_mime_type (uri
);
841 if (mime_type
== NULL
)
846 /* Open with plugins menu items */
847 plugin_descs
= get_available_plugins_for_mime (plugin
, mime_type
);
848 snode
= plugin_descs
;
852 AnjutaPluginDescription
*desc
;
854 desc
= (AnjutaPluginDescription
*)(snode
->data
);
856 anjuta_plugin_description_get_string (desc
, "File Loader",
860 anjuta_plugin_description_get_string (desc
, "Anjuta Plugin",
865 anjuta_plugin_description_get_string (desc
, "Anjuta Plugin",
868 menuitem
= gtk_menu_item_new_with_label (name
);
869 g_object_set_data (G_OBJECT (menuitem
), "index", GINT_TO_POINTER (idx
));
870 g_object_set_data (G_OBJECT (menuitem
), "desc", (gpointer
)(desc
));
871 g_signal_connect (G_OBJECT (menuitem
), "activate",
872 G_CALLBACK (callback
), callback_data
);
873 gtk_menu_append (menu
, menuitem
);
875 snode
= g_list_next (snode
);
879 /* Open with applications */
880 mime_apps
= gnome_vfs_mime_get_all_applications (mime_type
);
881 if (idx
> 0 && mime_apps
)
883 menuitem
= gtk_menu_item_new ();
884 gtk_menu_append (menu
, menuitem
);
892 mime_app
= (GnomeVFSMimeApplication
*)(node
->data
);
893 menuitem
= gtk_menu_item_new_with_label (mime_app
->name
);
894 g_object_set_data (G_OBJECT (menuitem
), "index", GINT_TO_POINTER (idx
));
895 g_signal_connect (G_OBJECT (menuitem
), "activate",
896 G_CALLBACK (callback
), callback_data
);
897 gtk_menu_append (menu
, menuitem
);
898 node
= g_list_next (node
);
901 gtk_widget_show_all (menu
);
903 if (mime_apps
== NULL
&& plugin_descs
== NULL
)
908 gnome_vfs_mime_application_list_free (mime_apps
);
910 g_list_free (plugin_descs
);
916 value_added_fm_current_uri (AnjutaPlugin
*plugin
, const char *name
,
917 const GValue
*value
, gpointer data
)
921 AnjutaFileLoaderPlugin
*fl_plugin
;
923 GtkWidget
*parentmenu
;
925 uri
= g_value_get_string (value
);
926 g_return_if_fail (name
!= NULL
);
928 fl_plugin
= ANJUTA_PLUGIN_FILE_LOADER (plugin
);
929 ui
= anjuta_shell_get_ui (plugin
->shell
, NULL
);
931 action
= anjuta_ui_get_action (ui
, "ActionGroupPopupLoader", "ActionPopupOpen");
932 g_object_set (G_OBJECT (action
), "sensitive", TRUE
, NULL
);
934 action
= anjuta_ui_get_action (ui
, "ActionGroupPopupLoader",
935 "ActionPopupOpenWith");
936 g_object_set (G_OBJECT (action
), "sensitive", TRUE
, NULL
);
938 if (fl_plugin
->fm_current_uri
)
939 g_free (fl_plugin
->fm_current_uri
);
940 fl_plugin
->fm_current_uri
= g_strdup (uri
);
943 gtk_ui_manager_get_widget (GTK_UI_MANAGER(ui
),
944 "/PopupFileManager/PlaceholderPopupFileOpen/OpenWith");
945 if (!create_open_with_submenu (fl_plugin
, parentmenu
, uri
,
946 G_CALLBACK (fm_open_with
), plugin
))
947 g_object_set (G_OBJECT (action
), "sensitive", FALSE
, NULL
);
951 value_removed_fm_current_uri (AnjutaPlugin
*plugin
,
952 const char *name
, gpointer data
)
956 AnjutaFileLoaderPlugin
*fl_plugin
;
958 fl_plugin
= ANJUTA_PLUGIN_FILE_LOADER (plugin
);
960 if (fl_plugin
->fm_current_uri
)
961 g_free (fl_plugin
->fm_current_uri
);
962 fl_plugin
->fm_current_uri
= NULL
;
964 ui
= anjuta_shell_get_ui (plugin
->shell
, NULL
);
965 action
= anjuta_ui_get_action (ui
, "ActionGroupPopupLoader", "ActionPopupOpen");
966 g_object_set (G_OBJECT (action
), "sensitive", FALSE
, NULL
);
968 action
= anjuta_ui_get_action (ui
, "ActionGroupPopupLoader",
969 "ActionPopupOpenWith");
970 g_object_set (G_OBJECT (action
), "sensitive", FALSE
, NULL
);
974 value_added_pm_current_uri (AnjutaPlugin
*plugin
, const char *name
,
975 const GValue
*value
, gpointer data
)
979 AnjutaFileLoaderPlugin
*fl_plugin
;
981 GtkWidget
*parentmenu
;
983 uri
= g_value_get_string (value
);
984 g_return_if_fail (name
!= NULL
);
986 fl_plugin
= ANJUTA_PLUGIN_FILE_LOADER (plugin
);
987 ui
= anjuta_shell_get_ui (plugin
->shell
, NULL
);
989 action
= anjuta_ui_get_action (ui
, "ActionGroupPopupLoader", "ActionPopupPMOpen");
990 g_object_set (G_OBJECT (action
), "sensitive", TRUE
, NULL
);
992 action
= anjuta_ui_get_action (ui
, "ActionGroupPopupLoader",
993 "ActionPopupPMOpenWith");
994 g_object_set (G_OBJECT (action
), "sensitive", TRUE
, NULL
);
996 if (fl_plugin
->pm_current_uri
)
997 g_free (fl_plugin
->pm_current_uri
);
998 fl_plugin
->pm_current_uri
= g_strdup (uri
);
1001 gtk_ui_manager_get_widget (GTK_UI_MANAGER(ui
),
1002 "/PopupProjectManager/PlaceholderPopupProjectOpen/OpenWith");
1003 if (!create_open_with_submenu (fl_plugin
, parentmenu
, uri
,
1004 G_CALLBACK (pm_open_with
), plugin
))
1005 g_object_set (G_OBJECT (action
), "sensitive", FALSE
, NULL
);
1009 value_removed_pm_current_uri (AnjutaPlugin
*plugin
,
1010 const char *name
, gpointer data
)
1014 AnjutaFileLoaderPlugin
*fl_plugin
;
1016 fl_plugin
= ANJUTA_PLUGIN_FILE_LOADER (plugin
);
1018 if (fl_plugin
->pm_current_uri
)
1019 g_free (fl_plugin
->pm_current_uri
);
1020 fl_plugin
->pm_current_uri
= NULL
;
1022 ui
= anjuta_shell_get_ui (plugin
->shell
, NULL
);
1023 action
= anjuta_ui_get_action (ui
, "ActionGroupPopupLoader", "ActionPopupPMOpen");
1024 g_object_set (G_OBJECT (action
), "sensitive", FALSE
, NULL
);
1026 action
= anjuta_ui_get_action (ui
, "ActionGroupPopupLoader",
1027 "ActionPopupPMOpenWith");
1028 g_object_set (G_OBJECT (action
), "sensitive", FALSE
, NULL
);
1032 dnd_dropped (const gchar
*uri
, gpointer plugin
)
1034 ianjuta_file_loader_load (IANJUTA_FILE_LOADER (plugin
), uri
, FALSE
, NULL
);
1038 on_session_load (AnjutaShell
*shell
, AnjutaSessionPhase phase
,
1039 AnjutaSession
*session
,
1040 AnjutaFileLoaderPlugin
*plugin
)
1042 AnjutaStatus
*status
;
1045 GList
*files
, *node
;
1048 /* We want to load the files first before other session loads */
1049 if (phase
!= ANJUTA_SESSION_PHASE_FIRST
)
1052 files
= anjuta_session_get_string_list (session
, "File Loader", "Files");
1056 status
= anjuta_shell_get_status (shell
, NULL
);
1057 anjuta_status_progress_add_ticks (status
, g_list_length(files
));
1059 /* Open project files first and then regular files */
1060 for (i
= 0; i
< 2; i
++)
1068 gchar
*label
, *filename
;
1070 mime_type
= anjuta_util_get_uri_mime_type (uri
);
1072 filename
= g_path_get_basename (uri
);
1073 if (strchr (filename
, '#'))
1074 *(strchr (filename
, '#')) = '\0';
1076 label
= g_strconcat ("Loaded: ", filename
, NULL
);
1078 if (i
== 0 && mime_type
&&
1079 strcmp (mime_type
, "application/x-anjuta") == 0)
1081 /* Project files first */
1082 /* FIXME: Ignore project files for now */
1084 ianjuta_file_loader_load (IANJUTA_FILE_LOADER (plugin),
1087 anjuta_status_progress_tick (status
, NULL
, label
);
1091 strcmp (mime_type
, "application/x-anjuta") != 0))
1093 /* Then rest of the files */
1094 ianjuta_file_loader_load (IANJUTA_FILE_LOADER (plugin
),
1096 anjuta_status_progress_tick (status
, NULL
, label
);
1102 node
= g_list_next (node
);
1107 g_list_foreach (files
, (GFunc
)g_free
, NULL
);
1108 g_list_free (files
);
1113 activate_plugin (AnjutaPlugin
*plugin
)
1115 EggSubmenuAction
*saction
;
1118 AnjutaFileLoaderPlugin
*loader_plugin
;
1119 // GtkWidget *recent_menu;
1120 // EggRecentViewGtk *recent_view;
1121 GtkActionGroup
*group
;
1123 loader_plugin
= ANJUTA_PLUGIN_FILE_LOADER (plugin
);
1125 DEBUG_PRINT ("AnjutaFileLoaderPlugin: Activating File Loader plugin...");
1127 ui
= anjuta_shell_get_ui (plugin
->shell
, NULL
);
1129 /* Add action group */
1130 loader_plugin
->action_group
=
1131 anjuta_ui_add_action_group_entries (ui
, "ActionGroupLoader",
1134 G_N_ELEMENTS (actions_file
),
1135 GETTEXT_PACKAGE
, TRUE
, plugin
);
1136 loader_plugin
->popup_action_group
=
1137 anjuta_ui_add_action_group_entries (ui
, "ActionGroupPopupLoader",
1140 G_N_ELEMENTS (popup_actions_file
),
1141 GETTEXT_PACKAGE
, FALSE
, plugin
);
1142 saction
= g_object_new (EGG_TYPE_SUBMENU_ACTION
,
1143 "name", "ActionFileWizard",
1145 "tooltip", _("New file, project and project components."),
1147 egg_submenu_action_set_menu_factory (saction
,
1148 on_create_submenu
, plugin
);
1149 gtk_action_group_add_action (loader_plugin
->action_group
,
1150 GTK_ACTION (saction
));
1152 /* Set short labels */
1153 action
= anjuta_ui_get_action (ui
, "ActionGroupLoader", "ActionFileNew");
1154 g_object_set (G_OBJECT (action
), "short-label", _("New"),
1155 "is-important", TRUE
, NULL
);
1156 action
= anjuta_ui_get_action (ui
, "ActionGroupLoader", "ActionFileOpen");
1157 g_object_set (G_OBJECT (action
), "short-label", _("Open"),
1158 "is-important", TRUE
, NULL
);
1160 group
= gtk_action_group_new ("ActionGroupLoaderRecent");
1161 action
= g_object_new (EGG_TYPE_RECENT_ACTION
,
1162 "name", "ActionFileOpenRecent",
1163 "label", _("Open _Recent"),
1164 "tooltip", _("Open recent file"),
1166 egg_recent_action_add_model (EGG_RECENT_ACTION (action
),
1167 loader_plugin
->recent_files_model_top
);
1168 egg_recent_action_add_model (EGG_RECENT_ACTION (action
),
1169 loader_plugin
->recent_files_model_bottom
);
1170 g_signal_connect (action
, "activate",
1171 G_CALLBACK (on_open_recent_file
), plugin
);
1172 gtk_action_group_add_action (group
, action
);
1173 anjuta_ui_add_action_group (ui
, "ActionGroupLoaderRecent",
1174 N_("Open recent files"), group
, FALSE
);
1175 loader_plugin
->recent_group
= group
;
1178 loader_plugin
->uiid
= anjuta_ui_merge (ui
, UI_FILE
);
1180 /* Install drag n drop handler */
1181 dnd_drop_init (GTK_WIDGET (plugin
->shell
), dnd_dropped
, plugin
,
1182 "text/plain", "text/html", "text/source", "application-x/anjuta",
1186 loader_plugin
->fm_watch_id
=
1187 anjuta_plugin_add_watch (plugin
, "file_manager_current_uri",
1188 value_added_fm_current_uri
,
1189 value_removed_fm_current_uri
, NULL
);
1190 loader_plugin
->pm_watch_id
=
1191 anjuta_plugin_add_watch (plugin
, "project_manager_current_uri",
1192 value_added_pm_current_uri
,
1193 value_removed_pm_current_uri
, NULL
);
1195 /* Connect to session */
1196 g_signal_connect (G_OBJECT (plugin
->shell
), "load_session",
1197 G_CALLBACK (on_session_load
), plugin
);
1202 deactivate_plugin (AnjutaPlugin
*plugin
)
1205 AnjutaFileLoaderPlugin
*loader_plugin
;
1207 loader_plugin
= ANJUTA_PLUGIN_FILE_LOADER (plugin
);
1209 DEBUG_PRINT ("AnjutaFileLoaderPlugin: Deactivating File Loader plugin...");
1211 /* Disconnect session */
1212 g_signal_handlers_disconnect_by_func (G_OBJECT (plugin
->shell
),
1213 G_CALLBACK (on_session_load
), plugin
);
1215 ui
= anjuta_shell_get_ui (plugin
->shell
, NULL
);
1216 /* Remove watches */
1217 anjuta_plugin_remove_watch (plugin
, loader_plugin
->fm_watch_id
, TRUE
);
1218 anjuta_plugin_remove_watch (plugin
, loader_plugin
->pm_watch_id
, TRUE
);
1220 dnd_drop_finalize (GTK_WIDGET (plugin
->shell
), plugin
);
1222 anjuta_ui_unmerge (ui
, loader_plugin
->uiid
);
1223 /* Remove action group */
1224 anjuta_ui_remove_action_group (ui
, loader_plugin
->action_group
);
1225 anjuta_ui_remove_action_group (ui
, loader_plugin
->popup_action_group
);
1226 anjuta_ui_remove_action_group (ui
, loader_plugin
->recent_group
);
1231 dispose (GObject
*obj
)
1233 AnjutaFileLoaderPlugin
*plugin
= ANJUTA_PLUGIN_FILE_LOADER (obj
);
1234 if (plugin
->recent_files_model_top
)
1236 g_object_unref (plugin
->recent_files_model_top
);
1237 plugin
->recent_files_model_top
= NULL
;
1239 if (plugin
->recent_files_model_bottom
)
1241 g_object_unref (plugin
->recent_files_model_bottom
);
1242 plugin
->recent_files_model_bottom
= NULL
;
1244 GNOME_CALL_PARENT (G_OBJECT_CLASS
, dispose
, (obj
));
1248 anjuta_file_loader_plugin_instance_init (GObject
*obj
)
1250 AnjutaFileLoaderPlugin
*plugin
= ANJUTA_PLUGIN_FILE_LOADER (obj
);
1252 plugin
->fm_current_uri
= NULL
;
1253 plugin
->pm_current_uri
= NULL
;
1255 plugin
->recent_files_model_top
=
1256 egg_recent_model_new (EGG_RECENT_MODEL_SORT_MRU
);
1257 egg_recent_model_set_limit (plugin
->recent_files_model_top
, 5);
1258 egg_recent_model_set_filter_groups (plugin
->recent_files_model_top
,
1259 "anjuta-projects", NULL
);
1260 plugin
->recent_files_model_bottom
=
1261 egg_recent_model_new (EGG_RECENT_MODEL_SORT_MRU
);
1262 egg_recent_model_set_limit (plugin
->recent_files_model_bottom
, 15);
1263 egg_recent_model_set_filter_groups (plugin
->recent_files_model_bottom
,
1264 "anjuta-files", NULL
);
1268 anjuta_file_loader_plugin_class_init (GObjectClass
*klass
)
1270 AnjutaPluginClass
*plugin_class
= ANJUTA_PLUGIN_CLASS (klass
);
1272 parent_class
= g_type_class_peek_parent (klass
);
1274 plugin_class
->activate
= activate_plugin
;
1275 plugin_class
->deactivate
= deactivate_plugin
;
1276 klass
->dispose
= dispose
;
1280 iloader_load (IAnjutaFileLoader
*loader
, const gchar
*uri
,
1281 gboolean read_only
, GError
**err
)
1285 GnomeVFSURI
* vfs_uri
;
1286 AnjutaStatus
*status
;
1287 AnjutaPluginManager
*plugin_manager
;
1288 GList
*plugin_descs
= NULL
;
1289 GObject
*plugin
= NULL
;
1291 g_return_val_if_fail (uri
!= NULL
, NULL
);
1292 vfs_uri
= gnome_vfs_uri_new (uri
);
1294 if (!gnome_vfs_uri_exists (vfs_uri
))
1296 launch_application_failure (ANJUTA_PLUGIN_FILE_LOADER (loader
),
1297 uri
, GNOME_VFS_ERROR_NOT_FOUND
);
1298 gnome_vfs_uri_unref (vfs_uri
);
1302 new_uri
= gnome_vfs_uri_to_string (vfs_uri
,
1303 GNOME_VFS_URI_HIDE_FRAGMENT_IDENTIFIER
);
1304 mime_type
= anjuta_util_get_uri_mime_type (new_uri
);
1305 gnome_vfs_uri_unref (vfs_uri
);
1307 if (mime_type
== NULL
)
1309 launch_application_failure (ANJUTA_PLUGIN_FILE_LOADER (loader
),
1310 new_uri
, GNOME_VFS_ERROR_NOT_FOUND
);
1315 plugin_manager
= anjuta_shell_get_plugin_manager (ANJUTA_PLUGIN (loader
)->shell
,
1317 status
= anjuta_shell_get_status (ANJUTA_PLUGIN (loader
)->shell
, NULL
);
1318 anjuta_status_busy_push (status
);
1320 DEBUG_PRINT ("Opening URI: %s", uri
);
1322 plugin_descs
= anjuta_plugin_manager_query (plugin_manager
,
1324 "Interfaces", "IAnjutaFile",
1326 "SupportedMimeTypes",
1330 if (g_list_length (plugin_descs
) > 1)
1333 anjuta_plugin_manager_select_and_activate (plugin_manager
,
1335 "Please select a plugin to open this file.",
1338 else if (g_list_length (plugin_descs
) == 1)
1340 gchar
*location
= NULL
;
1342 AnjutaPluginDescription
*desc
= plugin_descs
->data
;
1343 anjuta_plugin_description_get_string (desc
, "Anjuta Plugin",
1344 "Location", &location
);
1345 g_return_val_if_fail (location
!= NULL
, NULL
);
1347 anjuta_plugin_manager_get_plugin_by_id (plugin_manager
,
1353 launch_in_default_application (ANJUTA_PLUGIN_FILE_LOADER (loader
), mime_type
, uri
);
1356 ianjuta_file_open (IANJUTA_FILE(plugin
), uri
, NULL
);
1358 set_recent_file (ANJUTA_PLUGIN_FILE_LOADER (loader
), new_uri
, mime_type
);
1361 g_list_free (plugin_descs
);
1364 anjuta_status_busy_pop (status
);
1370 iloader_iface_init(IAnjutaLoaderIface
*iface
)
1372 iface
->find_plugins
= NULL
;
1376 iloader_file_iface_init(IAnjutaFileLoaderIface
*iface
)
1378 iface
->load
= iloader_load
;
1381 ANJUTA_PLUGIN_BEGIN (AnjutaFileLoaderPlugin
, anjuta_file_loader_plugin
);
1382 ANJUTA_PLUGIN_ADD_INTERFACE (iloader
, IANJUTA_TYPE_LOADER
);
1383 ANJUTA_PLUGIN_ADD_INTERFACE (iloader_file
, IANJUTA_TYPE_FILE_LOADER
);
1386 ANJUTA_SIMPLE_PLUGIN (AnjutaFileLoaderPlugin
, anjuta_file_loader_plugin
);