summaryrefslogtreecommitdiff
path: root/src/appfinder-gdbus.c
blob: 4ab342dfc6c1e0bbdaadad83cc205b037e6db4ac (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
 * Copyright (C) 2013 Nick Schermer <nick@xfce.org>
 *
 * 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.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <libxfce4ui/libxfce4ui.h>

#include <src/appfinder-gdbus.h>
#include <src/appfinder-private.h>



#define APPFINDER_DBUS_SERVICE     "org.xfce.Appfinder"
#define APPFINDER_DBUS_INTERFACE   APPFINDER_DBUS_SERVICE
#define APPFINDER_DBUS_PATH        "/org/xfce/Appfinder"
#define APPFINDER_DBUS_METHOD_OPEN "OpenWindow"
#define APPFINDER_DBUS_METHOD_QUIT "Quit"



static const gchar appfinder_gdbus_introspection_xml[] =
  "<node>"
    "<interface name='" APPFINDER_DBUS_INTERFACE "'>"
      "<method name='" APPFINDER_DBUS_METHOD_OPEN "'>"
        "<arg type='b' name='expanded' direction='in'/>"
        "<arg type='s' name='startup-id' direction='in'/>"
      "</method>"
      "<method name='" APPFINDER_DBUS_METHOD_QUIT "'/>"
    "</interface>"
  "</node>";



static void
appfinder_gdbus_method_call (GDBusConnection       *connection,
                             const gchar           *sender,
                             const gchar           *object_path,
                             const gchar           *interface_name,
                             const gchar           *method_name,
                             GVariant              *parameters,
                             GDBusMethodInvocation *invocation,
                             gpointer               user_data)
{
  gboolean  expanded;
  gchar    *startup_id = NULL;

  g_return_if_fail (!g_strcmp0 (object_path, APPFINDER_DBUS_PATH));
  g_return_if_fail (!g_strcmp0 (interface_name, APPFINDER_DBUS_INTERFACE));

  APPFINDER_DEBUG ("received dbus method %s", method_name);

  if (g_strcmp0 (method_name, APPFINDER_DBUS_METHOD_OPEN) == 0)
    {
      /* get paramenters */
      g_variant_get (parameters, "(bs)", &expanded, &startup_id);

      appfinder_window_new (startup_id, expanded);

      /* everything went fine */
      g_dbus_method_invocation_return_value (invocation, NULL);

      g_free (startup_id);
    }
  else if (g_strcmp0 (method_name, APPFINDER_DBUS_METHOD_QUIT) == 0)
    {
      /* close all windows and quit */
      g_printerr ("%s: %s.\n", PACKAGE_NAME, _("Forced to quit"));

      gtk_main_quit ();
    }
  else
    {
      g_dbus_method_invocation_return_error (invocation,
          G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD,
          "Unknown method for DBus service " APPFINDER_DBUS_SERVICE);
    }
}



static const GDBusInterfaceVTable appfinder_gdbus_vtable =
{
  appfinder_gdbus_method_call,
  NULL, /* get property */
  NULL  /* set property */
};



static void
appfinder_gdbus_bus_acquired (GDBusConnection *connection,
                              const gchar     *name,
                              gpointer         user_data)
{
  guint          register_id;
  GDBusNodeInfo *info;
  GError        *error = NULL;

  info = g_dbus_node_info_new_for_xml (appfinder_gdbus_introspection_xml, NULL);
  g_assert (info != NULL);
  g_assert (*info->interfaces != NULL);

  register_id = g_dbus_connection_register_object (connection,
                                                   APPFINDER_DBUS_PATH,
                                                   *info->interfaces, /* first iface */
                                                   &appfinder_gdbus_vtable,
                                                   user_data,
                                                   NULL,
                                                   &error);

  APPFINDER_DEBUG ("registered interface with id %d", register_id);

  if (register_id == 0)
    {
      g_message ("Failed to register object: %s", error->message);
      g_error_free (error);
    }

  g_dbus_node_info_unref (info);
}



gboolean
appfinder_gdbus_service (GError **error)
{
  guint owner_id;

  owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
                             APPFINDER_DBUS_SERVICE,
                             G_BUS_NAME_OWNER_FLAGS_NONE,
                             appfinder_gdbus_bus_acquired,
                             NULL,
                             NULL,
                             NULL,
                             NULL);

  return (owner_id != 0);
}



gboolean
appfinder_gdbus_quit (GError **error)
{
  GVariant        *reply;
  GDBusConnection *connection;
  GError          *err = NULL;
  gboolean         result;

  connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, error);
  if (G_UNLIKELY (connection == NULL))
    return FALSE;

  reply = g_dbus_connection_call_sync (connection,
                                       APPFINDER_DBUS_SERVICE,
                                       APPFINDER_DBUS_PATH,
                                       APPFINDER_DBUS_INTERFACE,
                                       APPFINDER_DBUS_METHOD_QUIT,
                                       NULL,
                                       NULL,
                                       G_DBUS_CALL_FLAGS_NO_AUTO_START,
                                       2000,
                                       NULL,
                                       &err);

  g_object_unref (connection);

  result = (reply != NULL);
  if (G_LIKELY (result))
    g_variant_unref (reply);
  else
    g_propagate_error (error, err);

  return result;
}



gboolean
appfinder_gdbus_open_window (gboolean      expanded,
                             const gchar  *startup_id,
                             GError      **error)
{
  GVariant        *reply;
  GDBusConnection *connection;
  GError          *err = NULL;
  gboolean         result;

  connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, error);
  if (G_UNLIKELY (connection == NULL))
    return FALSE;

  if (startup_id == NULL)
    startup_id = "";

  reply = g_dbus_connection_call_sync (connection,
                                       APPFINDER_DBUS_SERVICE,
                                       APPFINDER_DBUS_PATH,
                                       APPFINDER_DBUS_INTERFACE,
                                       APPFINDER_DBUS_METHOD_OPEN,
                                       g_variant_new ("(bs)",
                                                      expanded,
                                                      startup_id),
                                       NULL,
                                       G_DBUS_CALL_FLAGS_NO_AUTO_START,
                                       2000,
                                       NULL,
                                       &err);

  g_object_unref (connection);

  result = (reply != NULL);
  if (G_LIKELY (result))
    g_variant_unref (reply);
  else
    g_propagate_error (error, err);

  return result;
}