Integrate adding files with the file manager
[anjuta-git-plugin.git] / plugins / profiler / plugin.c
blob923f98729a1affa8741a34989c7ae8cd6572e14f
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 * plugin.c
4 * Copyright (C) James Liggett 2006 <jrliggett@cox.net>
5 *
6 * plugin.c is free software.
7 *
8 * You may redistribute it and/or modify it under the terms of the
9 * GNU General Public License, as published by the Free Software
10 * Foundation; either version 2, or (at your option) any later version.
12 * plugin.c is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 * See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with plugin.c. See the file "COPYING". If not,
19 * write to: The Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
25 #include "plugin.h"
28 #define UI_FILE PACKAGE_DATA_DIR"/ui/profiler.ui"
29 #define GLADE_FILE PACKAGE_DATA_DIR"/glade/profiler.glade"
30 #define ICON_FILE PACKAGE_PIXMAPS_DIR"/anjuta-profiler-plugin-48.png"
32 static gpointer parent_class;
34 static void
35 add_options_strings (GPtrArray *options, const gchar *prefix, const gchar *args)
37 gchar **split_args; /* List of arguements split by lines */
38 gchar **current_string;
39 gchar *full_arg; /* Argument with prefix */
41 if (strlen (args) > 0)
43 split_args = g_strsplit (args, "\n", -1);
45 for (current_string = split_args;
46 *current_string;
47 current_string++)
50 if (strlen (*current_string) > 0)
52 full_arg = g_strconcat (prefix, *current_string, NULL);
53 g_ptr_array_add (options, full_arg);
57 g_strfreev (split_args);
61 static GPtrArray *
62 setup_options (Profiler *profiler)
64 GPtrArray *options;
65 gchar *symbols;
67 options = g_ptr_array_new ();
69 /* First handle the easy ones: -a, -c, and -z */
70 if (gprof_options_get_int (profiler->options, "no_show_static"))
72 g_ptr_array_add (options, g_strdup ("-a"));
75 if (gprof_options_get_int (profiler->options, "show_possible_called"))
77 g_ptr_array_add (options, g_strdup ("-c"));
80 if (gprof_options_get_int (profiler->options, "show_uncalled"))
82 g_ptr_array_add (options, g_strdup ("-z"));
85 /* If the user wants to modify the call graph, put in -p so we have a flat
86 * profile. */
87 if (!gprof_options_get_int (profiler->options, "show_all_symbols"))
89 g_ptr_array_add (options, g_strdup ("-p"));
91 symbols = gprof_options_get_string (profiler->options, "symbols");
93 if (gprof_options_get_int (profiler->options, "include_symbols"))
95 add_options_strings (options, "-q", symbols);
98 if (gprof_options_get_int (profiler->options, "exclude_symbols"))
100 add_options_strings (options, "-Q", symbols);
103 g_free (symbols);
106 /* Time propagation options */
107 if (!gprof_options_get_int (profiler->options, "propagate_all_symbols"))
110 symbols = gprof_options_get_string (profiler->options,
111 "propagation_symbols");
113 if (gprof_options_get_int (profiler->options,
114 "propagate_include_symbols"))
116 add_options_strings (options, "-n", symbols);
119 if (gprof_options_get_int (profiler->options,
120 "propagate_exclude_symbols"))
122 add_options_strings (options, "-N", symbols);
125 g_free (symbols);
128 /* NULL terminate the array for compatibility with g_strfreev */
130 g_ptr_array_add (options, NULL);
132 /* Other options not directly passed to gprof */
134 /* If there is an existing profile data monitor and automatic refresh
135 * is disabled, cancel the monitor */
137 if (profiler->profile_data_monitor)
139 if (!gprof_options_get_int (profiler->options, "automatic_refresh"))
141 gnome_vfs_monitor_cancel (profiler->profile_data_monitor);
142 profiler->profile_data_monitor = NULL;
146 return options;
149 static gboolean
150 profiler_get_data (Profiler *profiler)
152 GPtrArray *options;
153 gchar **option_strings;
154 gchar *profiling_data_path;
155 gchar *profiling_data_path_from_options;
156 gboolean ret = FALSE;
158 if (profiler->profile_target_path)
161 options = setup_options (profiler);
163 profiling_data_path_from_options = gprof_options_get_string (profiler->options,
164 "profile_data_file");
166 if (strlen (profiling_data_path_from_options) > 0)
167 profiling_data_path = profiling_data_path_from_options;
168 else
169 profiling_data_path = NULL;
171 if (!gprof_profile_data_init_profile (profiler->profile_data,
172 profiler->profile_target_path,
173 profiling_data_path,
174 options))
176 anjuta_util_dialog_error (GTK_WINDOW (ANJUTA_PLUGIN (profiler)->shell),
177 _("Could not get profiling data."
178 "\n\n"
179 "Please check the path to "
180 "this target's profiling data file."));
183 option_strings = (gchar **) g_ptr_array_free (options, FALSE);
184 g_free (profiling_data_path_from_options);
185 g_strfreev (option_strings);
187 ret = TRUE;
190 return ret;
193 static void
194 on_profile_data_changed (GnomeVFSMonitorHandle *handle,
195 const gchar *monitor_uri, const gchar *info_uri,
196 GnomeVFSMonitorEventType event,
197 gpointer user_data)
199 Profiler *profiler;
201 profiler = PROFILER (user_data);
203 switch (event)
205 case GNOME_VFS_MONITOR_EVENT_CHANGED:
206 if (profiler_get_data (profiler))
207 gprof_view_manager_refresh_views (profiler->view_manager);
208 break;
209 case GNOME_VFS_MONITOR_EVENT_DELETED:
210 gnome_vfs_monitor_cancel (handle);
211 profiler->profile_data_monitor = NULL;
212 break;
213 default:
214 break;
218 static void
219 profiler_set_target (Profiler *profiler, const gchar *profile_target_uri)
221 gchar *profile_target_path;
222 gchar *profile_target_dir;
223 gchar *profile_data_path;
224 gchar *profile_data_path_from_options;
225 gchar *profile_data_uri;
227 if (profiler->profile_target_path)
229 g_free (profiler->profile_target_path);
230 profiler->profile_target_path = NULL;
233 if (profile_target_uri)
235 profile_target_path = gnome_vfs_get_local_path_from_uri (profile_target_uri);
237 profile_data_path_from_options = gprof_options_get_string (profiler->options,
238 "profile_data_file");
240 if (strlen (profile_data_path_from_options) > 0)
242 profile_data_path = g_strdup (profile_data_path_from_options);
243 profile_target_dir = NULL;
245 else
247 profile_target_dir = g_path_get_dirname (profile_target_path);
248 profile_data_path = g_build_filename (profile_target_dir, "gmon.out",
249 NULL);
252 g_free (profile_data_path_from_options);
254 profile_data_uri = gnome_vfs_get_uri_from_local_path (profile_data_path);
256 if (g_file_test (profile_data_path, G_FILE_TEST_EXISTS))
258 profiler->profile_target_path = profile_target_path;
261 /* Set up a file change monitor for automatic refresh if enabled */
262 if (gprof_options_get_int (profiler->options,
263 "automatic_refresh"))
265 /* Cancel any existing monitor */
266 if (profiler->profile_data_monitor)
267 gnome_vfs_monitor_cancel (profiler->profile_data_monitor);
269 gnome_vfs_monitor_add (&profiler->profile_data_monitor,
270 profile_data_uri, GNOME_VFS_MONITOR_FILE,
271 on_profile_data_changed,
272 (gpointer) profiler);
275 /* Show user the profiler views if they aren't visible so they
276 * know what happened */
277 anjuta_shell_present_widget (ANJUTA_PLUGIN (profiler)->shell,
278 gprof_view_manager_get_notebook (profiler->view_manager),
279 NULL);
281 else
283 anjuta_util_dialog_error (GTK_WINDOW (ANJUTA_PLUGIN (profiler)->shell),
284 _("This target does not have any "
285 "profiling data.\n\n"
286 "Please ensure that the target is "
287 "complied with profiling support "
288 "and that it is run at least "
289 "once."));
292 g_free (profile_target_dir);
293 g_free (profile_data_path);
294 g_free (profile_data_uri);
298 static GProfOptions *
299 register_options ()
301 GProfOptions *options;
303 options = gprof_options_new ();
305 gprof_options_register_key (options, "automatic_refresh", "0",
306 "automatic_refresh_check",
307 OPTION_TYPE_TOGGLE);
309 gprof_options_register_key (options, "no_show_static", "0",
310 "no_show_static_check",
311 OPTION_TYPE_TOGGLE);
313 gprof_options_register_key (options, "show_possible_called", "0",
314 "show_possible_called_check",
315 OPTION_TYPE_TOGGLE);
317 gprof_options_register_key (options, "show_uncalled", "0",
318 "show_uncalled_check",
319 OPTION_TYPE_TOGGLE);
321 gprof_options_register_key (options, "show_all_symbols", "1",
322 "show_all_symbols_radio",
323 OPTION_TYPE_TOGGLE);
325 gprof_options_register_key (options, "include_symbols", "0",
326 "include_symbols_radio",
327 OPTION_TYPE_TOGGLE);
329 gprof_options_register_key (options, "exclude_symbols", "0",
330 "exclude_symbols_radio",
331 OPTION_TYPE_TOGGLE);
333 gprof_options_register_key (options, "symbols", "", "symbols_text_view",
334 OPTION_TYPE_TEXT_ENTRY);
336 gprof_options_register_key (options, "propagate_all_symbols", "1",
337 "propagate_all_symbols_radio",
338 OPTION_TYPE_TOGGLE);
340 gprof_options_register_key (options, "propagate_include_symbols", "0",
341 "propagate_include_symbols_radio",
342 OPTION_TYPE_TOGGLE);
344 gprof_options_register_key (options, "propagate_exclude_symbols", "0",
345 "propagate_exclude_symbols_radio",
346 OPTION_TYPE_TOGGLE);
348 gprof_options_register_key (options, "propagation_symbols", "",
349 "propagation_text_view",
350 OPTION_TYPE_TEXT_ENTRY);
352 gprof_options_register_key (options, "profile_data_file", "",
353 "profile_data_file_entry",
354 OPTION_TYPE_ENTRY);
356 return options;
359 static void
360 on_profile_data_browse_button_clicked (GtkButton *button, GladeXML *gxml)
362 GtkWidget *select_file_dialog;
363 GtkWidget *profile_data_file_entry;
364 GtkWidget *profiling_options_dialog;
365 gchar *selected_file;
367 profile_data_file_entry = glade_xml_get_widget (gxml, "profile_data_file_entry");
368 profiling_options_dialog = glade_xml_get_widget (gxml,
369 "profiling_options_dialog");
370 select_file_dialog = gtk_file_chooser_dialog_new ("Select Data File",
371 GTK_WINDOW (profiling_options_dialog),
372 GTK_FILE_CHOOSER_ACTION_OPEN,
373 GTK_STOCK_CANCEL,
374 GTK_RESPONSE_CANCEL,
375 GTK_STOCK_OPEN,
376 GTK_RESPONSE_ACCEPT,
377 NULL);
379 if (gtk_dialog_run (GTK_DIALOG (select_file_dialog)) == GTK_RESPONSE_ACCEPT)
381 selected_file = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (select_file_dialog));
382 gtk_entry_set_text (GTK_ENTRY (profile_data_file_entry), selected_file);
383 g_free (selected_file);
386 gtk_widget_destroy (select_file_dialog);
389 static void
390 on_profiling_options_button_clicked (GtkButton *button, gpointer *user_data)
392 Profiler *profiler;
393 GladeXML *gxml;
394 GtkWidget *profiling_options_dialog;
395 GtkWidget *profile_data_browse_button;
397 profiler = PROFILER (user_data);
398 gxml = glade_xml_new (GLADE_FILE, "profiling_options_dialog",
399 NULL);
400 profiling_options_dialog = glade_xml_get_widget (gxml, "profiling_options_dialog");
401 profile_data_browse_button = glade_xml_get_widget (gxml,
402 "profile_data_browse_button");
404 g_signal_connect (profile_data_browse_button, "clicked",
405 G_CALLBACK (on_profile_data_browse_button_clicked),
406 gxml);
408 g_signal_connect (profiling_options_dialog, "response", G_CALLBACK (gtk_widget_hide),
409 profiling_options_dialog);
411 gprof_options_create_window (profiler->options, gxml);
413 gtk_window_set_transient_for (GTK_WINDOW (profiling_options_dialog),
414 GTK_WINDOW (ANJUTA_PLUGIN(profiler)->shell));
416 gtk_dialog_run (GTK_DIALOG (profiling_options_dialog));
418 g_object_unref (gxml);
421 static void
422 on_select_other_target_button_clicked (GtkButton *button,
423 GtkTreeView *targets_list_view)
425 GtkTreeModel *model;
426 GtkWidget *target_chooser_dialog;
427 GtkTreeIter iter;
428 gchar *selected_target_path;
429 gchar *selected_target_uri;
430 GtkTreeSelection *selection;
431 GtkTreePath *new_target_path;
433 model = gtk_tree_view_get_model (targets_list_view);
434 target_chooser_dialog = gtk_file_chooser_dialog_new ("Select Target",
435 NULL,
436 GTK_FILE_CHOOSER_ACTION_OPEN,
437 GTK_STOCK_CANCEL,
438 GTK_RESPONSE_CANCEL,
439 GTK_STOCK_OPEN,
440 GTK_RESPONSE_ACCEPT,
441 NULL);
443 if (gtk_dialog_run (GTK_DIALOG (target_chooser_dialog)) == GTK_RESPONSE_ACCEPT)
445 selected_target_path = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (target_chooser_dialog));
446 selected_target_uri = gnome_vfs_get_uri_from_local_path (selected_target_path);
447 selection = gtk_tree_view_get_selection (targets_list_view);
449 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
450 gtk_list_store_set (GTK_LIST_STORE (model), &iter, 0,
451 selected_target_path, 1,
452 selected_target_uri, -1);
454 gtk_tree_selection_select_iter (selection, &iter);
455 new_target_path = gtk_tree_model_get_path (model, &iter);
456 gtk_tree_view_scroll_to_cell (targets_list_view, new_target_path, NULL,
457 TRUE, 0.5, 0.0);
459 g_free (selected_target_path);
460 g_free (selected_target_uri);
461 gtk_tree_path_free (new_target_path);
464 gtk_widget_destroy (target_chooser_dialog);
467 static gboolean
468 on_target_selected (GtkTreeSelection *selection, GtkTreeModel *model,
469 GtkTreePath *path, gboolean path_currently_selected,
470 Profiler *profiler)
472 GtkTreeIter list_iter;
473 gchar *target_uri;
475 gtk_tree_model_get_iter (model, &list_iter, path);
476 gtk_tree_model_get (model, &list_iter, 1, &target_uri, -1);
478 if (target_uri)
480 gprof_options_set_target (profiler->options, target_uri);
481 g_free (target_uri);
484 return TRUE;
487 static void
488 on_profiler_select_target (GtkAction *action, Profiler *profiler)
490 GladeXML *gxml;
491 GtkWidget *select_target_dialog;
492 GtkWidget *profiling_options_button;
493 GtkWidget *select_other_target_button;
494 GtkWidget *targets_list_view;
495 GtkTreeViewColumn *column;
496 GtkCellRenderer *renderer;
497 GtkListStore *targets_list_store;
498 gint response;
499 GList *current_target;
500 GtkTreeIter iter;
501 GList *exec_targets;
502 IAnjutaProjectManager *project_manager;
503 GtkTreeSelection *selection;
504 GtkTreeModel *model;
505 gchar *target = NULL;
506 gchar *relative_path;
507 guint project_root_uri_length;
509 gxml = glade_xml_new (GLADE_FILE, "select_target_dialog", NULL);
510 select_target_dialog = glade_xml_get_widget (gxml,
511 "select_target_dialog");
512 targets_list_view = glade_xml_get_widget (gxml,
513 "targets_list_view");
514 profiling_options_button = glade_xml_get_widget (gxml,
515 "profiling_options_button");
516 select_other_target_button = glade_xml_get_widget (gxml,
517 "select_other_target_button");
519 g_signal_connect (profiling_options_button, "clicked",
520 G_CALLBACK (on_profiling_options_button_clicked),
521 profiler);
523 g_signal_connect (select_other_target_button, "clicked",
524 G_CALLBACK (on_select_other_target_button_clicked),
525 targets_list_view);
527 gtk_window_set_transient_for (GTK_WINDOW (select_target_dialog),
528 GTK_WINDOW (ANJUTA_PLUGIN(profiler)->shell));
530 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (targets_list_view));
531 gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
532 gtk_tree_selection_set_select_function (selection,
533 (GtkTreeSelectionFunc) on_target_selected,
534 profiler, NULL);
535 targets_list_store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING);
537 column = gtk_tree_view_column_new ();
538 gtk_tree_view_column_set_sizing (column,
539 GTK_TREE_VIEW_COLUMN_AUTOSIZE);
541 renderer = gtk_cell_renderer_text_new ();
542 gtk_tree_view_column_pack_start (column, renderer, FALSE);
543 gtk_tree_view_column_add_attribute (column, renderer, "text",
545 gtk_tree_view_append_column (GTK_TREE_VIEW (targets_list_view), column);
546 gtk_tree_view_set_expander_column (GTK_TREE_VIEW (targets_list_view), column);
548 if (profiler->project_root_uri)
550 project_manager = anjuta_shell_get_interface (ANJUTA_PLUGIN (profiler)->shell,
551 IAnjutaProjectManager, NULL);
553 exec_targets = ianjuta_project_manager_get_targets (project_manager,
554 IANJUTA_PROJECT_MANAGER_TARGET_EXECUTABLE,
555 NULL);
557 project_root_uri_length = strlen (profiler->project_root_uri) + 1;
559 if (exec_targets)
561 /* Populate listview */
562 current_target = exec_targets;
564 while (current_target)
566 relative_path = (gchar *) current_target->data + project_root_uri_length;
568 gtk_list_store_append (targets_list_store, &iter);
569 gtk_list_store_set (targets_list_store, &iter, 0, relative_path, 1,
570 current_target->data, -1);
572 g_free (current_target->data);
573 current_target = g_list_next (current_target);
575 g_list_free (exec_targets);
577 gtk_tree_view_set_model (GTK_TREE_VIEW (targets_list_view),
578 GTK_TREE_MODEL (targets_list_store));
579 g_object_unref (targets_list_store);
582 else
584 gtk_tree_view_set_model (GTK_TREE_VIEW (targets_list_view),
585 GTK_TREE_MODEL (targets_list_store));
586 g_object_unref (targets_list_store);
589 /* Run dialog */
590 response = gtk_dialog_run (GTK_DIALOG (select_target_dialog));
592 if (response == GTK_RESPONSE_OK)
594 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (targets_list_view));
595 if (gtk_tree_selection_get_selected (selection, &model, &iter))
597 gtk_tree_model_get (model, &iter, 1, &target, -1);
598 profiler_set_target (profiler, target);
600 if (profiler_get_data (profiler))
601 gprof_view_manager_refresh_views (profiler->view_manager);
603 else
604 profiler_set_target (profiler, NULL);
607 gtk_widget_hide (select_target_dialog);
608 g_object_unref (gxml);
611 static void
612 on_profiler_refresh (GtkAction *action, Profiler *profiler)
614 if (profiler_get_data (profiler))
615 gprof_view_manager_refresh_views (profiler->view_manager);
618 static void
619 on_profiler_delete_data (GtkAction *action, Profiler *profiler)
621 gchar *profile_target_dir;
622 gchar *profile_data_path_from_options;
623 gchar *profile_data_path;
625 if (profiler->profile_target_path)
627 profile_data_path_from_options = gprof_options_get_string (profiler->options,
628 "profile_data_file");
629 /* Delete given path if we have one, or just use the default */
630 if (strlen (profile_data_path_from_options) > 0)
631 g_unlink (profile_data_path_from_options);
632 else
635 profile_target_dir = g_path_get_dirname (profiler->profile_target_path);
636 profile_data_path = g_build_filename (profile_target_dir,
637 "gmon.out", NULL);
639 g_unlink (profile_data_path);
641 g_free (profile_target_dir);
642 g_free (profile_data_path);
645 g_free (profile_data_path_from_options);
649 static void
650 project_root_added (AnjutaPlugin *plugin, const gchar *name,
651 const GValue *value, gpointer user_data)
653 Profiler *profiler;
654 const gchar *root_uri;
656 profiler = PROFILER (plugin);
657 root_uri = g_value_get_string (value);
659 if (root_uri)
661 g_free (profiler->project_root_uri);
662 profiler->project_root_uri = g_strdup (root_uri);
668 static void
669 project_root_removed (AnjutaPlugin *plugin, const gchar *name,
670 gpointer user_data)
672 Profiler *profiler;
674 profiler = PROFILER (plugin);
676 g_free (profiler->project_root_uri);
677 profiler->project_root_uri = NULL;
680 static void
681 on_session_load (AnjutaShell *shell, AnjutaSessionPhase phase,
682 AnjutaSession *session,
683 Profiler *plugin)
685 const gchar *session_dir;
686 gchar *settings_file_path;
688 if (phase == ANJUTA_SESSION_PHASE_NORMAL)
690 session_dir = anjuta_session_get_session_directory (session);
691 settings_file_path = g_build_filename (session_dir,
692 "profiler-settings.xml",
693 NULL);
695 gprof_options_load (plugin->options, settings_file_path);
697 g_free (settings_file_path);
701 static void
702 on_session_save (AnjutaShell *shell, AnjutaSessionPhase phase,
703 AnjutaSession *session,
704 Profiler *plugin)
706 const gchar *session_dir;
707 gchar *settings_file_path;
709 if (phase == ANJUTA_SESSION_PHASE_NORMAL)
711 session_dir = anjuta_session_get_session_directory (session);
712 settings_file_path = g_build_filename (session_dir,
713 "profiler-settings.xml",
714 NULL);
716 gprof_options_save (plugin->options, settings_file_path);
718 g_free (settings_file_path);
723 static GtkActionEntry actions_file[] = {
725 "ActionMenuDebug", /* Action name */
726 NULL, /* Stock icon, if any */
727 N_("Debug"), /* Display label */
728 NULL, /* short-cut */
729 NULL, /* Tooltip */
730 NULL /* Action callback */
733 "ActionMenuProfiler", /* Action name */
734 "profiler-icon", /* Stock icon, if any */
735 N_("Profiler"), /* Display label */
736 NULL, /* short-cut */
737 NULL, /* Tooltip */
738 NULL /* Action callback */
741 "ActionProfilerSelectTarget", /* Action name */
742 GTK_STOCK_EXECUTE, /* Stock icon, if any */
743 N_("Select Target..."), /* Display label */
744 NULL, /* short-cut */
745 NULL, /* Tooltip */
746 G_CALLBACK (on_profiler_select_target) /* Action callback */
749 "ActionProfilerRefresh", /* Action name */
750 GTK_STOCK_REFRESH, /* Stock icon, if any */
751 N_("Refresh"), /* Display label */
752 NULL, /* short-cut */
753 NULL, /* Tooltip */
754 G_CALLBACK (on_profiler_refresh) /* Action callback */
757 "ActionProfilerDeleteData", /* Action name */
758 GTK_STOCK_DELETE, /* Stock icon, if any */
759 N_("Delete Data"), /* Display label */
760 NULL, /* short-cut */
761 NULL, /* Tooltip */
762 G_CALLBACK (on_profiler_delete_data) /* Action callback */
766 static void
767 register_stock_icons (AnjutaPlugin *plugin)
769 AnjutaUI *ui;
770 GtkIconFactory *icon_factory;
771 GtkIconSet *icon_set;
772 static gboolean registered = FALSE;
774 if (registered)
775 return;
776 registered = TRUE;
778 /* Register stock icons */
779 ui = anjuta_shell_get_ui (plugin->shell, NULL);
780 icon_factory = anjuta_ui_get_icon_factory (ui);
781 REGISTER_ICON ("anjuta-profiler-plugin-48.png", "profiler-icon");
784 static gboolean
785 profiler_activate (AnjutaPlugin *plugin)
788 AnjutaUI *ui;
789 Profiler *profiler;
790 IAnjutaSymbolManager *symbol_manager;
791 IAnjutaDocumentManager *document_manager;
793 DEBUG_PRINT ("Profiler: Activating Profiler plugin ...");
794 profiler = PROFILER (plugin);
796 /* Add all UI actions and merge UI */
797 ui = anjuta_shell_get_ui (plugin->shell, NULL);
798 register_stock_icons (plugin);
800 profiler->action_group =
801 anjuta_ui_add_action_group_entries (ui, "ActionGroupProfiler",
802 _("Application Performance Profiler"),
803 actions_file,
804 G_N_ELEMENTS (actions_file),
805 GETTEXT_PACKAGE, TRUE,
806 plugin);
807 profiler->uiid = anjuta_ui_merge (ui, UI_FILE);
809 profiler->view_manager = gprof_view_manager_new ();
810 profiler->profile_data = gprof_profile_data_new ();
812 symbol_manager = anjuta_shell_get_interface (plugin->shell,
813 IAnjutaSymbolManager,
814 NULL);
816 document_manager = anjuta_shell_get_interface (plugin->shell,
817 IAnjutaDocumentManager,
818 NULL);
820 gprof_view_manager_add_view (profiler->view_manager,
821 GPROF_VIEW (gprof_flat_profile_view_new (profiler->profile_data,
822 symbol_manager,
823 document_manager)),
824 _("Flat Profile"));
825 gprof_view_manager_add_view (profiler->view_manager,
826 GPROF_VIEW (gprof_call_graph_view_new (profiler->profile_data,
827 symbol_manager,
828 document_manager)),
829 _("Call Graph"));
830 gprof_view_manager_add_view (profiler->view_manager,
831 GPROF_VIEW (gprof_function_call_tree_view_new (profiler->profile_data,
832 symbol_manager,
833 document_manager)),
834 _("Function Call Tree"));
836 #ifdef HAVE_GRAPHVIZ
837 gprof_view_manager_add_view (profiler->view_manager,
838 GPROF_VIEW (gprof_function_call_chart_view_new (profiler->profile_data,
839 symbol_manager,
840 document_manager)),
841 _("Function Call Chart"));
842 #endif
844 anjuta_shell_add_widget (plugin->shell,
845 gprof_view_manager_get_notebook (profiler->view_manager),
846 "Profiler",
847 _("Profiler"),
848 "profiler-icon",
849 ANJUTA_SHELL_PLACEMENT_CENTER,
850 NULL);
852 profiler->project_watch_id = anjuta_plugin_add_watch (plugin, IANJUTA_PROJECT_MANAGER_PROJECT_ROOT_URI,
853 project_root_added,
854 project_root_removed, NULL);
856 profiler->options = register_options ();
858 /* Set up session save/load */
859 g_signal_connect (G_OBJECT (plugin->shell), "save_session",
860 G_CALLBACK (on_session_save), plugin);
862 g_signal_connect (G_OBJECT (plugin->shell), "load_session",
863 G_CALLBACK (on_session_load), plugin);
865 return TRUE;
868 static gboolean
869 profiler_deactivate (AnjutaPlugin *plugin)
872 AnjutaUI *ui;
873 Profiler *profiler;
875 DEBUG_PRINT ("Profiler: Dectivating Profiler plugin ...");
877 /* Disconnect session save/load */
878 g_signal_handlers_disconnect_by_func (G_OBJECT (plugin->shell),
879 G_CALLBACK (on_session_save), plugin);
881 g_signal_handlers_disconnect_by_func (G_OBJECT (plugin->shell),
882 G_CALLBACK (on_session_load), plugin);
884 ui = anjuta_shell_get_ui (plugin->shell, NULL);
885 profiler = PROFILER (plugin);
887 anjuta_plugin_remove_watch (plugin, profiler->project_watch_id, TRUE);
889 anjuta_ui_unmerge (ui, PROFILER (plugin)->uiid);
890 anjuta_ui_remove_action_group (ui, PROFILER (plugin)->action_group);
892 anjuta_shell_remove_widget (plugin->shell,
893 gprof_view_manager_get_notebook (profiler->view_manager),
894 NULL);
896 profiler_set_target (profiler, NULL);
897 gprof_view_manager_free (profiler->view_manager);
898 gprof_profile_data_free (profiler->profile_data);
900 gprof_options_destroy (profiler->options);
902 g_free (profiler->project_root_uri);
904 if (profiler->profile_data_monitor)
905 gnome_vfs_monitor_cancel (profiler->profile_data_monitor);
907 return TRUE;
910 static void
911 profiler_finalize (GObject *obj)
913 /* Finalization codes here */
914 G_OBJECT_CLASS (parent_class)->finalize (obj);
917 static void
918 profiler_dispose (GObject *obj)
920 /* Disposition codes */
921 G_OBJECT_CLASS (parent_class)->dispose (obj);
924 static void
925 profiler_instance_init (GObject *obj)
927 Profiler *profiler = PROFILER (obj);
929 profiler->uiid = 0;
930 profiler->project_root_uri = NULL;
931 profiler->profile_target_path = NULL;
936 static void
937 profiler_class_init (GObjectClass *klass)
939 AnjutaPluginClass *plugin_class = ANJUTA_PLUGIN_CLASS (klass);
941 parent_class = g_type_class_peek_parent (klass);
943 plugin_class->activate = profiler_activate;
944 plugin_class->deactivate = profiler_deactivate;
945 klass->finalize = profiler_finalize;
946 klass->dispose = profiler_dispose;
949 /* File open interface */
950 static void
951 ifile_open (IAnjutaFile *manager, GFile* file,
952 GError **err)
954 Profiler *profiler;
956 profiler = PROFILER (manager);
958 gchar* uri = g_file_get_uri (file);
960 profiler_set_target (profiler, uri);
962 /* Respect user settings for this target if they exist. Otherwise, don't
963 * create an entry for this target to avoid having the settings file
964 * balloon with the settings for a bunch of targets, espcially if this
965 * is a one-time operation. If previous settings don't exist, just use
966 * the defaults. */
967 if (gprof_options_has_target (profiler->options, uri))
968 gprof_options_set_target (profiler->options, uri);
969 else
970 gprof_options_set_target (profiler->options, NULL);
972 if (profiler_get_data (profiler))
973 gprof_view_manager_refresh_views (profiler->view_manager);
974 g_free (file);
977 static GFile*
978 ifile_get_file (IAnjutaFile *manager, GError **err)
980 DEBUG_PRINT ("Unsupported operation");
981 return NULL;
984 static void
985 ifile_iface_init (IAnjutaFileIface *iface)
987 iface->open = ifile_open;
988 iface->get_file = ifile_get_file;
991 ANJUTA_PLUGIN_BEGIN (Profiler, profiler);
992 ANJUTA_PLUGIN_ADD_INTERFACE (ifile, IANJUTA_TYPE_FILE);
993 ANJUTA_PLUGIN_END;
995 ANJUTA_SIMPLE_PLUGIN (Profiler, profiler);