gnome-control-center/panels/system/cc-system-panel.c
Felipe Borges bb242fc58c system: Handle "gnome-control-center system subpage" cmdline arguments
This allows us to open subpages of the "System" panel. The arguments/
parameters are expected to match the panel's subpage tags (as in
AdwNavigationPage.tag).

With this, a future change could reintroduce desktop files for the
subpages, and just change the Exec line to accomodate the new format.
2024-02-22 09:57:01 +00:00

212 lines
6 KiB
C

/*
* cc-system-panel.c
*
* Copyright 2023 Gotam Gorabh <gautamy672@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include <config.h>
#include <glib/gi18n-lib.h>
#include "cc-list-row.h"
#include "cc-system-panel.h"
#include "cc-system-resources.h"
#include "about/cc-about-page.h"
#include "datetime/cc-datetime-page.h"
#include "region/cc-region-page.h"
#include "remote-desktop/cc-remote-desktop-page.h"
#include "remote-login/cc-remote-login-page.h"
#include "users/cc-users-page.h"
struct _CcSystemPanel
{
CcPanel parent_instance;
AdwNavigationView *navigation;
GtkWidget *remote_login_dialog;
AdwNavigationPage *software_updates_group;
};
enum
{
PROP_0,
PROP_PARAMETERS
};
CC_PANEL_REGISTER (CcSystemPanel, cc_system_panel)
static gboolean
gnome_software_allows_updates (void)
{
const gchar *schema_id = "org.gnome.software";
GSettingsSchemaSource *source;
g_autoptr(GSettingsSchema) schema = NULL;
g_autoptr(GSettings) settings = NULL;
source = g_settings_schema_source_get_default ();
if (source == NULL)
return FALSE;
schema = g_settings_schema_source_lookup (source, schema_id, FALSE);
if (schema == NULL)
return FALSE;
settings = g_settings_new (schema_id);
return g_settings_get_boolean (settings, "allow-updates");
}
static gboolean
gnome_software_exists (void)
{
g_autofree gchar *path = g_find_program_in_path ("gnome-software");
return path != NULL;
}
static gboolean
gpk_update_viewer_exists (void)
{
g_autofree gchar *path = g_find_program_in_path ("gpk-update-viewer");
return path != NULL;
}
static gboolean
show_software_updates_group (CcSystemPanel *self)
{
return (gnome_software_exists () && gnome_software_allows_updates ()) ||
gpk_update_viewer_exists ();
}
static void
cc_system_page_open_software_update (CcSystemPanel *self)
{
g_autoptr(GError) error = NULL;
gboolean ret;
char *argv[3];
if (gnome_software_exists ())
{
argv[0] = "gnome-software";
argv[1] = "--mode=updates";
argv[2] = NULL;
}
else
{
argv[0] = "gpk-update-viewer";
argv[1] = NULL;
}
ret = g_spawn_async (NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, &error);
if (!ret)
g_warning ("Failed to spawn %s: %s", argv[0], error->message);
}
static void
cc_system_panel_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
CcSystemPanel *self = CC_SYSTEM_PANEL (object);
switch (property_id)
{
case PROP_PARAMETERS:
{
GVariant *parameters = g_value_get_variant (value);
g_autoptr (GVariant) v = NULL;
const gchar *parameter = NULL;
AdwNavigationPage *subpage = NULL;
if (parameters == NULL || g_variant_n_children (parameters) <= 0)
return;
g_variant_get_child (parameters, 0, "v", &v);
if (!g_variant_is_of_type (v, G_VARIANT_TYPE_STRING))
{
g_warning ("Wrong type for the second argument GVariant, expected 's' but got '%s'",
(gchar *)g_variant_get_type (v));
return;
}
parameter = g_variant_get_string (v, NULL);
subpage = adw_navigation_view_find_page (self->navigation, parameter);
if (subpage)
adw_navigation_view_push (self->navigation, subpage);
else
g_warning ("No subpage named '%s' found", parameter);
return;
}
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
static void
on_secure_shell_row_clicked (CcSystemPanel *self)
{
if (self->remote_login_dialog == NULL) {
GtkWidget *parent = cc_shell_get_toplevel (cc_panel_get_shell (CC_PANEL (self)));
self->remote_login_dialog = g_object_new (CC_TYPE_REMOTE_LOGIN_PAGE, NULL);
gtk_window_set_transient_for (GTK_WINDOW (self->remote_login_dialog),
GTK_WINDOW (parent));
}
gtk_window_present (GTK_WINDOW (self->remote_login_dialog));
}
static void
cc_system_panel_class_init (CcSystemPanelClass *klass)
{
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->set_property = cc_system_panel_set_property;
g_object_class_override_property (object_class, PROP_PARAMETERS, "parameters");
gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/control-center/system/cc-system-panel.ui");
gtk_widget_class_bind_template_child (widget_class, CcSystemPanel, navigation);
gtk_widget_class_bind_template_child (widget_class, CcSystemPanel, software_updates_group);
gtk_widget_class_bind_template_callback (widget_class, cc_system_page_open_software_update);
gtk_widget_class_bind_template_callback (widget_class, on_secure_shell_row_clicked);
g_type_ensure (CC_TYPE_ABOUT_PAGE);
g_type_ensure (CC_TYPE_DATE_TIME_PAGE);
g_type_ensure (CC_TYPE_REGION_PAGE);
g_type_ensure (CC_TYPE_REMOTE_DESKTOP_PAGE);
g_type_ensure (CC_TYPE_REMOTE_LOGIN_PAGE);
g_type_ensure (CC_TYPE_USERS_PAGE);
}
static void
cc_system_panel_init (CcSystemPanel *self)
{
g_resources_register (cc_system_get_resource ());
gtk_widget_init_template (GTK_WIDGET (self));
gtk_widget_set_visible (GTK_WIDGET (self->software_updates_group), show_software_updates_group (self));
}