From 6f49ddb0e3f46411e74b62f157efab502886c471 Mon Sep 17 00:00:00 2001 From: Christian Hergert Date: Thu, 16 May 2019 00:54:06 -0700 Subject: [PATCH] libsysprof-ui: start on open support --- src/libsysprof-ui/sysprof-display.c | 132 +++++++++++++++++++++++- src/libsysprof-ui/sysprof-display.h | 10 +- src/libsysprof-ui/sysprof-notebook.c | 51 +++++++++ src/libsysprof-ui/sysprof-notebook.h | 3 + src/libsysprof-ui/ui/sysprof-display.ui | 11 ++ src/sysprof/sysprof-window.c | 40 ++++++- 6 files changed, 242 insertions(+), 5 deletions(-) diff --git a/src/libsysprof-ui/sysprof-display.c b/src/libsysprof-ui/sysprof-display.c index 1a90c861..ff263af3 100644 --- a/src/libsysprof-ui/sysprof-display.c +++ b/src/libsysprof-ui/sysprof-display.c @@ -35,9 +35,15 @@ typedef struct { + GFile *file; + SysprofProfiler *profiler; + + /* Template Objects */ SysprofCaptureView *capture_view; + SysprofEmptyStateView *failed_view; SysprofEmptyStateView *empty_view; SysprofRecordingStateView *recording_view; + GtkStack *stack; } SysprofDisplayPrivate; G_DEFINE_TYPE_WITH_PRIVATE (SysprofDisplay, sysprof_display, GTK_TYPE_BIN) @@ -73,6 +79,9 @@ sysprof_display_dup_title (SysprofDisplay *self) g_return_val_if_fail (SYSPROF_IS_DISPLAY (self), NULL); + if (priv->file != NULL) + return g_file_get_basename (priv->file); + if ((reader = sysprof_capture_view_get_reader (priv->capture_view))) { const gchar *filename; @@ -88,6 +97,11 @@ sysprof_display_dup_title (SysprofDisplay *self) static void sysprof_display_finalize (GObject *object) { + SysprofDisplay *self = (SysprofDisplay *)object; + SysprofDisplayPrivate *priv = sysprof_display_get_instance_private (self); + + g_clear_object (&priv->profiler); + G_OBJECT_CLASS (sysprof_display_parent_class)->finalize (object); } @@ -143,9 +157,11 @@ sysprof_display_class_init (SysprofDisplayClass *klass) g_object_class_install_properties (object_class, N_PROPS, properties); gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/sysprof/ui/sysprof-display.ui"); - gtk_widget_class_bind_template_child_private (widget_class, SysprofDisplay , empty_view); - gtk_widget_class_bind_template_child_private (widget_class, SysprofDisplay , recording_view); - gtk_widget_class_bind_template_child_private (widget_class, SysprofDisplay , capture_view); + gtk_widget_class_bind_template_child_private (widget_class, SysprofDisplay, capture_view); + gtk_widget_class_bind_template_child_private (widget_class, SysprofDisplay, empty_view); + gtk_widget_class_bind_template_child_private (widget_class, SysprofDisplay, failed_view); + gtk_widget_class_bind_template_child_private (widget_class, SysprofDisplay, recording_view); + gtk_widget_class_bind_template_child_private (widget_class, SysprofDisplay, stack); g_type_ensure (SYSPROF_TYPE_CAPTURE_VIEW); g_type_ensure (SYSPROF_TYPE_EMPTY_STATE_VIEW); @@ -157,3 +173,113 @@ sysprof_display_init (SysprofDisplay *self) { gtk_widget_init_template (GTK_WIDGET (self)); } + +/** + * sysprof_display_get_profiler: + * + * Gets the proflier for the display. + * + * Returns: (transfer none) (nullable): a #SysprofProfiler or %NULL + * + * Since: 3.34 + */ +SysprofProfiler * +sysprof_display_get_profiler (SysprofDisplay *self) +{ + SysprofDisplayPrivate *priv = sysprof_display_get_instance_private (self); + + g_return_val_if_fail (SYSPROF_IS_DISPLAY (self), NULL); + + return priv->profiler; +} + +/** + * sysprof_display_is_empty: + * + * Checks if any content is or will be loaded into @self. + * + * Returns: %TRUE if the tab is unperterbed. + * + * Since: 3.34 + */ +gboolean +sysprof_display_is_empty (SysprofDisplay *self) +{ + SysprofDisplayPrivate *priv = sysprof_display_get_instance_private (self); + + g_return_val_if_fail (SYSPROF_IS_DISPLAY (self), FALSE); + + return priv->file == NULL && + priv->profiler == NULL && + NULL == sysprof_capture_view_get_reader (priv->capture_view); +} + +static void +sysprof_display_open_cb (GObject *object, + GAsyncResult *result, + gpointer user_data) +{ + g_autoptr(SysprofDisplay) self = user_data; + SysprofDisplayPrivate *priv = sysprof_display_get_instance_private (self); + g_autoptr(SysprofCaptureReader) reader = NULL; + g_autoptr(GError) error = NULL; + + g_assert (SYSPROF_IS_DISPLAY (self)); + g_assert (G_IS_TASK (result)); + + if (!(reader = g_task_propagate_pointer (G_TASK (result), &error))) + { + gtk_stack_set_visible_child (priv->stack, GTK_WIDGET (priv->failed_view)); + return; + } + + sysprof_capture_view_load_async (priv->capture_view, reader, NULL, NULL, NULL); + gtk_stack_set_visible_child (priv->stack, GTK_WIDGET (priv->capture_view)); +} + +static void +sysprof_display_open_worker (GTask *task, + gpointer source_object, + gpointer task_data, + GCancellable *cancellable) +{ + g_autofree gchar *path = NULL; + g_autoptr(GError) error = NULL; + SysprofCaptureReader *reader; + GFile *file = task_data; + + g_assert (G_IS_TASK (task)); + g_assert (source_object == NULL); + g_assert (G_IS_FILE (file)); + g_assert (!cancellable || G_IS_CANCELLABLE (cancellable)); + + path = g_file_get_path (file); + + if (!(reader = sysprof_capture_reader_new (path, &error))) + g_task_return_error (task, g_steal_pointer ((&error))); + else + g_task_return_pointer (task, + g_steal_pointer (&reader), + (GDestroyNotify) sysprof_capture_reader_unref); +} + +void +sysprof_display_open (SysprofDisplay *self, + GFile *file) +{ + SysprofDisplayPrivate *priv = sysprof_display_get_instance_private (self); + g_autoptr(GTask) task = NULL; + + g_return_if_fail (SYSPROF_IS_DISPLAY (self)); + g_return_if_fail (G_IS_FILE (file)); + g_return_if_fail (g_file_is_native (file)); + g_return_if_fail (sysprof_display_is_empty (self)); + + g_set_object (&priv->file, file); + + task = g_task_new (NULL, NULL, sysprof_display_open_cb, g_object_ref (self)); + g_task_set_task_data (task, g_file_dup (file), g_object_unref); + g_task_run_in_thread (task, sysprof_display_open_worker); + + g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_TITLE]); +} diff --git a/src/libsysprof-ui/sysprof-display.h b/src/libsysprof-ui/sysprof-display.h index 0b6c735a..6d7f1941 100644 --- a/src/libsysprof-ui/sysprof-display.h +++ b/src/libsysprof-ui/sysprof-display.h @@ -21,6 +21,7 @@ #pragma once #include +#include #include "sysprof-version-macros.h" @@ -40,6 +41,13 @@ struct _SysprofDisplayClass } __attribute__((aligned(8))); SYSPROF_AVAILABLE_IN_ALL -GtkWidget *sysprof_display_new (void); +GtkWidget *sysprof_display_new (void); +SYSPROF_AVAILABLE_IN_ALL +SysprofProfiler *sysprof_display_get_profiler (SysprofDisplay *self); +SYSPROF_AVAILABLE_IN_ALL +gboolean sysprof_display_is_empty (SysprofDisplay *self); +SYSPROF_AVAILABLE_IN_ALL +void sysprof_display_open (SysprofDisplay *self, + GFile *file); G_END_DECLS diff --git a/src/libsysprof-ui/sysprof-notebook.c b/src/libsysprof-ui/sysprof-notebook.c index 0be49776..d0b02e6b 100644 --- a/src/libsysprof-ui/sysprof-notebook.c +++ b/src/libsysprof-ui/sysprof-notebook.c @@ -83,6 +83,9 @@ sysprof_notebook_page_removed (GtkNotebook *notebook, g_assert (SYSPROF_IS_NOTEBOOK (notebook)); g_assert (GTK_IS_WIDGET (child)); + if (gtk_widget_in_destruction (GTK_WIDGET (notebook))) + return; + if (gtk_notebook_get_n_pages (notebook) == 0) { child = sysprof_display_new (); @@ -165,3 +168,51 @@ sysprof_notebook_close_current (SysprofNotebook *self) if ((page = gtk_notebook_get_current_page (GTK_NOTEBOOK (self))) >= 0) gtk_widget_destroy (gtk_notebook_get_nth_page (GTK_NOTEBOOK (self), page)); } + +static void +find_empty_display_cb (GtkWidget *widget, + gpointer user_data) +{ + GtkWidget **display = user_data; + + g_assert (GTK_IS_WIDGET (widget)); + g_assert (display != NULL); + + if (*display != NULL) + return; + + if (SYSPROF_IS_DISPLAY (widget) && + sysprof_display_is_empty (SYSPROF_DISPLAY (widget))) + *display = widget; +} + +void +sysprof_notebook_open (SysprofNotebook *self, + GFile *file) +{ + GtkWidget *display = NULL; + gint page; + + g_return_if_fail (SYSPROF_IS_NOTEBOOK (self)); + g_return_if_fail (g_file_is_native (file)); + + gtk_container_foreach (GTK_CONTAINER (self), + find_empty_display_cb, + &display); + + if (display == NULL) + { + + display = sysprof_display_new (); + page = gtk_notebook_insert_page (GTK_NOTEBOOK (self), display, NULL, -1); + gtk_widget_show (display); + } + else + { + page = gtk_notebook_page_num (GTK_NOTEBOOK (self), display); + } + + gtk_notebook_set_current_page (GTK_NOTEBOOK (self), page); + + sysprof_display_open (SYSPROF_DISPLAY (display), file); +} diff --git a/src/libsysprof-ui/sysprof-notebook.h b/src/libsysprof-ui/sysprof-notebook.h index 7a064399..de3cfc18 100644 --- a/src/libsysprof-ui/sysprof-notebook.h +++ b/src/libsysprof-ui/sysprof-notebook.h @@ -43,5 +43,8 @@ SYSPROF_AVAILABLE_IN_ALL GtkWidget *sysprof_notebook_new (void); SYSPROF_AVAILABLE_IN_ALL void sysprof_notebook_close_current (SysprofNotebook *self); +SYSPROF_AVAILABLE_IN_ALL +void sysprof_notebook_open (SysprofNotebook *self, + GFile *file); G_END_DECLS diff --git a/src/libsysprof-ui/ui/sysprof-display.ui b/src/libsysprof-ui/ui/sysprof-display.ui index 1392b5ab..df0071ab 100644 --- a/src/libsysprof-ui/ui/sysprof-display.ui +++ b/src/libsysprof-ui/ui/sysprof-display.ui @@ -31,6 +31,17 @@ recording + + + computer-fail-symbolic + Something went wrong + Sysprof failed to access the requested performance data. + true + + + failed + + diff --git a/src/sysprof/sysprof-window.c b/src/sysprof/sysprof-window.c index 249637b7..2cd261d1 100644 --- a/src/sysprof/sysprof-window.c +++ b/src/sysprof/sysprof-window.c @@ -22,6 +22,7 @@ #include "config.h" +#include #include #include "sysprof-window.h" @@ -134,13 +135,50 @@ sysprof_window_open (SysprofWindow *self, g_return_if_fail (SYSPROF_IS_WINDOW (self)); g_return_if_fail (G_IS_FILE (file)); - + sysprof_notebook_open (self->notebook, file); } void sysprof_window_open_from_dialog (SysprofWindow *self) { + GtkFileChooserNative *dialog; + GtkFileFilter *filter; + gint response; + g_return_if_fail (SYSPROF_IS_WINDOW (self)); + + /* Translators: This is a window title. */ + dialog = gtk_file_chooser_native_new (_("Open Capture…"), + GTK_WINDOW (self), + GTK_FILE_CHOOSER_ACTION_OPEN, + /* Translators: This is a button. */ + _("Open"), + /* Translators: This is a button. */ + _("Cancel")); + + gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (dialog), TRUE); + + filter = gtk_file_filter_new (); + gtk_file_filter_set_name (filter, _("Sysprof Captures")); + gtk_file_filter_add_pattern (filter, "*.syscap"); + gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter); + + filter = gtk_file_filter_new (); + gtk_file_filter_set_name (filter, _("All Files")); + gtk_file_filter_add_pattern (filter, "*"); + gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter); + + response = gtk_native_dialog_run (GTK_NATIVE_DIALOG (dialog)); + + if (response == GTK_RESPONSE_ACCEPT) + { + g_autoptr(GFile) file = NULL; + + file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog)); + sysprof_window_open (self, file); + } + + gtk_native_dialog_destroy (GTK_NATIVE_DIALOG (dialog)); } void