summaryrefslogtreecommitdiff
path: root/src/egg-unique.c
blob: 41a518285bd906077562c041f6981a42a5ffee12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2008 Richard Hughes <richard@hughsie.com>
 *
 * Licensed under the GNU General Public License Version 2
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"

#include <string.h>
#include <glib.h>
#include <unique/unique.h>

#include "egg-unique.h"
#include "egg-debug.h"

static void     egg_unique_finalize   (GObject        *object);

#define EGG_UNIQUE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), EGG_UNIQUE_TYPE, EggUniquePrivate))

struct EggUniquePrivate
{
	UniqueApp		*uniqueapp;
};

enum {
	ACTIVATED,
	LAST_SIGNAL
};

static guint signals [LAST_SIGNAL] = { 0 };

G_DEFINE_TYPE (EggUnique, egg_unique, G_TYPE_OBJECT)

/**
 * egg_unique_message_cb:
 **/
static void
egg_unique_message_cb (UniqueApp *app, UniqueCommand command, UniqueMessageData *message_data, guint time_s, EggUnique *egg_unique)
{
	g_return_if_fail (EGG_IS_UNIQUE (egg_unique));
	if (command == UNIQUE_ACTIVATE)
		g_signal_emit (egg_unique, signals [ACTIVATED], 0);
}

/**
 * egg_unique_assign:
 * @egg_unique: This class instance
 * @service: The service name
 * Return value: %FALSE if we should exit as another instance is running
 **/
gboolean
egg_unique_assign (EggUnique *egg_unique, const gchar *service)
{
	g_return_val_if_fail (EGG_IS_UNIQUE (egg_unique), FALSE);
	g_return_val_if_fail (service != NULL, FALSE);

	if (egg_unique->priv->uniqueapp != NULL) {
		g_warning ("already assigned!");
		return FALSE;
	}

	/* check to see if the user has another instance open */
	egg_unique->priv->uniqueapp = unique_app_new (service, NULL);
	if (unique_app_is_running (egg_unique->priv->uniqueapp)) {
		egg_debug ("You have another instance running. This program will now close");
		unique_app_send_message (egg_unique->priv->uniqueapp, UNIQUE_ACTIVATE, NULL);
		return FALSE;
	}

	/* Listen for messages from another instances */
	g_signal_connect (G_OBJECT (egg_unique->priv->uniqueapp), "message-received",
			  G_CALLBACK (egg_unique_message_cb), egg_unique);
	return TRUE;
}

/**
 * egg_unique_class_init:
 * @egg_unique: This class instance
 **/
static void
egg_unique_class_init (EggUniqueClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	object_class->finalize = egg_unique_finalize;
	g_type_class_add_private (klass, sizeof (EggUniquePrivate));

	signals [ACTIVATED] =
		g_signal_new ("activated",
			      G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
			      G_STRUCT_OFFSET (EggUniqueClass, activated),
			      NULL, NULL, g_cclosure_marshal_VOID__VOID,
			      G_TYPE_NONE, 0);
}

/**
 * egg_unique_init:
 * @egg_unique: This class instance
 **/
static void
egg_unique_init (EggUnique *egg_unique)
{
	egg_unique->priv = EGG_UNIQUE_GET_PRIVATE (egg_unique);
	egg_unique->priv->uniqueapp = NULL;
}

/**
 * egg_unique_finalize:
 * @object: This class instance
 **/
static void
egg_unique_finalize (GObject *object)
{
	EggUnique *egg_unique;
	g_return_if_fail (object != NULL);
	g_return_if_fail (EGG_IS_UNIQUE (object));

	egg_unique = EGG_UNIQUE_OBJECT (object);
	egg_unique->priv = EGG_UNIQUE_GET_PRIVATE (egg_unique);

	if (egg_unique->priv->uniqueapp != NULL)
		g_object_unref (egg_unique->priv->uniqueapp);
	G_OBJECT_CLASS (egg_unique_parent_class)->finalize (object);
}

/**
 * egg_unique_new:
 * Return value: new class instance.
 **/
EggUnique *
egg_unique_new (void)
{
	EggUnique *egg_unique;
	egg_unique = g_object_new (EGG_UNIQUE_TYPE, NULL);
	return EGG_UNIQUE_OBJECT (egg_unique);
}