summaryrefslogtreecommitdiff
path: root/panels/printers/pp-printer.c
blob: 220c93291af48bb0c6d2959006cd8a5d280a1d49 (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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
 * Copyright (C) 2016 Red Hat, Inc
 *
 * 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, see <http://www.gnu.org/licenses/>.
 *
 * Authors: Martin Hatina <mhatina@redhat.com>
 *          Marek Kasik <mkasik@redhat.com>
 */

#include "pp-printer.h"

#include "pp-utils.h"

typedef struct _PpPrinter        PpPrinter;
typedef struct _PpPrinterPrivate PpPrinterPrivate;

struct _PpPrinter
{
  GObject           parent_instance;
  PpPrinterPrivate *priv;
};

struct _PpPrinterPrivate
{
  gchar *printer_name;
};

G_DEFINE_TYPE_WITH_PRIVATE (PpPrinter, pp_printer, G_TYPE_OBJECT)

enum
{
  PROP_0 = 0,
  PROP_NAME
};

static void
pp_printer_dispose (GObject *object)
{
  PpPrinterPrivate *priv = PP_PRINTER (object)->priv;

  g_free (priv->printer_name);

  G_OBJECT_CLASS (pp_printer_parent_class)->dispose (object);
}

static void
pp_printer_finalize (GObject *object)
{
  G_OBJECT_CLASS (pp_printer_parent_class)->finalize (object);
}

static void
pp_printer_get_property (GObject    *object,
                         guint       property_id,
                         GValue     *value,
                         GParamSpec *pspec)
{
  PpPrinter *self = PP_PRINTER (object);

  switch (property_id)
    {
      case PROP_NAME:
        g_value_set_string (value, self->priv->printer_name);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
        break;
    }
}

static void
pp_printer_set_property (GObject      *object,
                         guint         property_id,
                         const GValue *value,
                         GParamSpec   *pspec)
{
  PpPrinter *self = PP_PRINTER (object);

  switch (property_id)
    {
      case PROP_NAME:
        g_free (self->priv->printer_name);
        self->priv->printer_name = g_value_dup_string (value);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
        break;
    }

}

static void
pp_printer_class_init (PpPrinterClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->set_property = pp_printer_set_property;
  gobject_class->get_property = pp_printer_get_property;
  gobject_class->dispose = pp_printer_dispose;
  gobject_class->finalize = pp_printer_finalize;

  g_object_class_install_property (gobject_class, PROP_NAME,
    g_param_spec_string ("printer-name",
                         "Printer name",
                         "Name of this printer",
                         NULL,
                         G_PARAM_READWRITE));
}

static void
pp_printer_init (PpPrinter *printer)
{
  printer->priv = G_TYPE_INSTANCE_GET_PRIVATE (printer,
                                               PP_TYPE_PRINTER,
                                               PpPrinterPrivate);
}

PpPrinter *
pp_printer_new (const gchar *name)
{
  PpPrinter *self = g_object_new (PP_TYPE_PRINTER, "printer-name", name, NULL);

  return self;
}

static void
printer_rename_thread (GTask        *task,
                       gpointer      source_object,
                       gpointer      task_data,
                       GCancellable *cancellable)
{
  PpPrinter *printer = PP_PRINTER (source_object);
  gboolean   result;
  gchar     *new_printer_name = task_data;
  gchar     *old_printer_name;

  g_object_get (printer, "printer-name", &old_printer_name, NULL);

  result = printer_rename (old_printer_name, new_printer_name);

  if (result)
    {
      g_object_set (printer, "printer-name", new_printer_name, NULL);
    }

  g_free (old_printer_name);

  g_task_return_boolean (task, result);
}

static void
printer_rename_dbus_cb (GObject      *source_object,
                        GAsyncResult *res,
                        gpointer      user_data)
{
  PpPrinter *printer;
  GVariant  *output;
  gboolean   result = FALSE;
  GError    *error = NULL;
  GTask     *task = user_data;
  gchar     *old_printer_name;

  output = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object),
                                          res,
                                          &error);
  g_object_unref (source_object);

  if (output != NULL)
    {
      const gchar *ret_error;

      printer = g_task_get_source_object (task);
      g_object_get (printer, "printer-name", &old_printer_name, NULL);

      g_variant_get (output, "(&s)", &ret_error);
      if (ret_error[0] != '\0')
        {
          g_warning ("cups-pk-helper: renaming of printer %s failed: %s", old_printer_name, ret_error);
        }
      else
        {
          result = TRUE;
          g_object_set (printer, "printer-name", g_task_get_task_data (task), NULL);
        }

      g_task_return_boolean (task, result);

      g_free (old_printer_name);
      g_variant_unref (output);
    }
  else
    {
      if (error->domain == G_DBUS_ERROR &&
          (error->code == G_DBUS_ERROR_SERVICE_UNKNOWN ||
           error->code == G_DBUS_ERROR_UNKNOWN_METHOD))
        {
          g_warning ("Update cups-pk-helper to at least 0.2.6 please to be able to use PrinterRename method.");
          g_error_free (error);

          g_task_run_in_thread (task, printer_rename_thread);
        }
      else
        {
          g_task_return_boolean (task, FALSE);
        }
    }
}

static void
get_bus_cb (GObject      *source_object,
            GAsyncResult *res,
            gpointer      user_data)
{
  GDBusConnection *bus;
  GError          *error = NULL;
  GTask           *task = user_data;
  gchar           *printer_name;

  bus = g_bus_get_finish (res, &error);
  if (bus != NULL)
    {
      g_object_get (g_task_get_source_object (task),
                    "printer-name", &printer_name,
                    NULL);
      g_dbus_connection_call (bus,
                              MECHANISM_BUS,
                              "/",
                              MECHANISM_BUS,
                              "PrinterRename",
                              g_variant_new ("(ss)",
                                             printer_name,
                                             g_task_get_task_data (task)),
                              G_VARIANT_TYPE ("(s)"),
                              G_DBUS_CALL_FLAGS_NONE,
                              -1,
                              g_task_get_cancellable (task),
                              printer_rename_dbus_cb,
                              task);

      g_free (printer_name);
    }
  else
    {
      g_warning ("Failed to get system bus: %s", error->message);
      g_error_free (error);
      g_task_return_boolean (task, FALSE);
    }
}

void
pp_printer_rename_async (PpPrinter           *printer,
                         const gchar         *new_printer_name,
                         GCancellable        *cancellable,
                         GAsyncReadyCallback  callback,
                         gpointer             user_data)
{
  GTask *task;

  task = g_task_new (G_OBJECT (printer), cancellable, callback, user_data);
  g_task_set_task_data (task, g_strdup (new_printer_name), g_free);

  g_bus_get (G_BUS_TYPE_SYSTEM,
             cancellable,
             get_bus_cb,
             task);
}

gboolean
pp_printer_rename_finish (PpPrinter     *printer,
                          GAsyncResult  *res,
                          GError       **error)
{
  g_return_val_if_fail (g_task_is_valid (res, printer), FALSE);
  g_object_unref (res);

  return g_task_propagate_boolean (G_TASK (res), error);
}