gnome-shell/src/shell-realm-item.c

343 lines
9.4 KiB
C

#include "shell-global.h"
#include "shell-realm-item.h"
#include "shell-realm-tracker.h"
#include <meta/meta-workspace-manager.h>
#include <meta/display.h>
struct _ShellRealmItem {
GObject parent;
char *realm_name;
char *description;
guint64 namespace;
MetaWorkspaceContext *context;
ShellRealmItemStatus status;
gboolean tagged;
};
G_DEFINE_TYPE (ShellRealmItem, shell_realm_item, G_TYPE_OBJECT);
enum {
PROP_0,
PROP_ITEM_REALM_NAME,
PROP_ITEM_DESCRIPTION,
PROP_ITEM_NAMESPACE
};
static void
shell_realm_item_init (ShellRealmItem *item)
{
}
ShellRealmItem *
shell_realm_item_new (const char *realm_name, const char *description, guint64 namespace, guint8 status)
{
ShellRealmItem *item = g_object_new (SHELL_TYPE_REALM_ITEM, NULL);
item->realm_name = g_strdup (realm_name);
item->description = g_strdup (description);
item->namespace = namespace;
item->status = status;
item->context = NULL;
item->tagged = FALSE;
return item;
}
/**
* shell_realm_item_get_context:
* @item: A #ShellRealmItem instance
*
* Returns the #MetaWorkspaceContext associated with this realm if any.
*
* Returns: (transfer none) (nullable): a #MetaWorkspaceContext instance or
* %NULL if no context is associated with this realm.
*/
MetaWorkspaceContext *
shell_realm_item_get_context (ShellRealmItem *item)
{
return item->context;
}
void
shell_realm_item_set_context (ShellRealmItem *item, MetaWorkspaceContext *context)
{
item->context = context;
}
/**
* shell_realm_item_get_realm_name:
* @item: A #ShellRealmItem instance
*
* Returns: The name of the realm for this #ShellRealmItem
*/
const char *
shell_realm_item_get_realm_name (ShellRealmItem *item)
{
return item->realm_name;
}
/**
* shell_realm_item_get_description:
* @item: A #ShellRealmItem instance
*
* Returns: The description field for this realm or an empty string if no description is set
*/
const char *
shell_realm_item_get_description (ShellRealmItem *item)
{
if (item->description)
return item->description;
else
return "";
}
/**
* shell_realm_item_get_namespace:
* @item: A #ShellRealmItem instance
*
* Returns: The namespace field for this realm or 0 if no namespace is set
*/
guint64
shell_realm_item_get_namespace (ShellRealmItem *item)
{
return item->namespace;
}
/**
* shell_realm_item_get_context_id:
* @item: A #ShellRealmItem instance
*
* Returns: The context id for the #MetaWorkspaceContext of this realm or 0 if
* no context exists.
*/
guint
shell_realm_item_get_context_id (ShellRealmItem *item)
{
if (item->context) {
return meta_workspace_context_id (item->context);
} else {
return 0;
}
}
/**
* shell_realm_item_get_active_workspace:
* @item: A #ShellRealmItem
*
* Returns: (transfer none): The current workspace for the context
* belonging to this item.
*/
MetaWorkspace *
shell_realm_item_get_active_workspace (ShellRealmItem *item)
{
if (item->context) {
return meta_workspace_context_get_active_workspace (item->context);
} else {
return NULL;
}
}
/**
* shell_realm_item_set_current:
* @item: A #ShellRealmItem instance for a running realm
*
* Sends a DBUS request to change the current realm to this realm. This does not immediately
* influence any local state in GNOME shell. Once the realms daemon has changed the current realm
* it will emit a signal and the processing of that signal will update the local state.
*/
void
shell_realm_item_set_current (ShellRealmItem *item) {
ShellRealmTracker *tracker = shell_realm_tracker_get_default ();
if (item && item->realm_name) {
shell_realm_tracker_call_set_current (tracker, item->realm_name);
}
}
/**
* shell_realm_item_move_window_to_context:
* @item: A #ShellRealmItem instance for a running realm
* @window: A #MetaWindow for some window
*
* Move window to the currently active workspace in the #MetaWorkspaceContext for
* this realm.
*/
void
shell_realm_item_move_window_to_context (ShellRealmItem *item, MetaWindow *window)
{
if (item->context) {
meta_workspace_context_move_window_to_context (item->context, window);
} else {
g_warning ("ShellRealmItem: Attempted to move window to realm '%s' which has no workspace context", item->realm_name);
}
}
gboolean
shell_realm_is_flag_set(guint8 status, ShellRealmItemStatus flag)
{
return ((status & flag) != 0) ? TRUE : FALSE;
}
static gboolean
has_status_flag (ShellRealmItem *item, ShellRealmItemStatus flag)
{
return shell_realm_is_flag_set (item->status, flag);
}
static void
set_status_flag (ShellRealmItem *item, ShellRealmItemStatus flag, gboolean value)
{
if (value) {
item->status |= flag;
} else {
item->status &= ~flag;
}
}
/**
* shell_realm_item_is_current:
* @item: A #ShellRealmItem instance
*
* Returns: %TRUE if this #ShellRealmItem is the current realm
*/
gboolean
shell_realm_item_is_current (ShellRealmItem *item)
{
return has_status_flag (item, SHELL_REALM_ITEM_STATUS_CURRENT);
}
/**
* shell_realm_item_is_running:
* @item: A #ShellRealmItem instance
*
* Returns: %TRUE if this #ShellRealmItem is running
*/
gboolean
shell_realm_item_is_running (ShellRealmItem *item)
{
return has_status_flag (item, SHELL_REALM_ITEM_STATUS_RUNNING);
}
/**
* shell_realm_item_is_system:
* @item: A #ShellRealmItem instance
*
* Returns: %TRUE if this #ShellRealmItem is a system realm
*/
gboolean
shell_realm_item_is_system (ShellRealmItem *item)
{
return has_status_flag (item, SHELL_REALM_ITEM_STATUS_SYSTEM);
}
void shell_realm_item_set_current_flag (ShellRealmItem *item, gboolean value)
{
set_status_flag (item, SHELL_REALM_ITEM_STATUS_CURRENT, value);
}
void shell_realm_item_set_running_flag (ShellRealmItem *item, gboolean value)
{
set_status_flag (item, SHELL_REALM_ITEM_STATUS_RUNNING, value);
}
void shell_realm_item_update (ShellRealmItem *item, const char *realm_name, guint64 namespace, guint8 status)
{
if (g_strcmp0 (item->realm_name, realm_name)) {
g_message ("ShellRealmItem: Realm name changed from %s to %s", item->realm_name, realm_name);
g_free (item->realm_name);
item->realm_name = g_strdup (realm_name);
}
item->namespace = namespace;
item->status = status;
}
void
shell_realm_item_set_tagged (ShellRealmItem *item, gboolean is_tagged)
{
item->tagged = is_tagged;
}
gboolean
shell_realm_item_is_tagged (ShellRealmItem *item)
{
return item->tagged;
}
static void shell_realm_item_get_property (GObject *gobject,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
ShellRealmItem *item = SHELL_REALM_ITEM (gobject);
switch (prop_id) {
case PROP_ITEM_REALM_NAME:
g_value_set_string (value, shell_realm_item_get_realm_name (item));
break;
case PROP_ITEM_DESCRIPTION:
g_value_set_string (value, shell_realm_item_get_description (item));
break;
case PROP_ITEM_NAMESPACE:
g_value_set_uint64 (value, shell_realm_item_get_namespace (item));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
break;
}
}
static void shell_realm_item_dispose (GObject *object)
{
ShellRealmItem *item = SHELL_REALM_ITEM (object);
if (item->context) {
meta_workspace_context_remove (item->context);
item->context = NULL;
}
G_OBJECT_CLASS(shell_realm_item_parent_class)->dispose (object);
}
static void
shell_realm_item_finalize (GObject *object)
{
ShellRealmItem *item = SHELL_REALM_ITEM (object);
g_free (item->realm_name);
g_free (item->description);
G_OBJECT_CLASS(shell_realm_item_parent_class)->finalize (object);
}
static void
shell_realm_item_class_init (ShellRealmItemClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->get_property = shell_realm_item_get_property;
gobject_class->dispose = shell_realm_item_dispose;
gobject_class->finalize = shell_realm_item_finalize;
g_object_class_install_property (gobject_class,
PROP_ITEM_NAMESPACE,
g_param_spec_uint64 ("namespace",
"Context Namespace",
"PID namespace of context",
0, UINT64_MAX, 0,
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class,
PROP_ITEM_REALM_NAME,
g_param_spec_string ("realm-name",
"Realm Name",
"Name of realm associated with this context",
NULL,
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class,
PROP_ITEM_DESCRIPTION,
g_param_spec_string ("description",
"Realm Description",
"Optional description of realm",
NULL,
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
}