aboutsummaryrefslogtreecommitdiff
path: root/src/indicator-printers-service.c
blob: c350ee0462c300873d821a2cda991f68a9f5a3a3 (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
/*
 * Copyright 2012 Canonical Ltd.
 *
 * Authors: Lars Uebernickel <lars.uebernickel@canonical.com>
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3, as published
 * by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY, 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/>.
 */

#include <libindicator/indicator-service.h>
#include <libdbusmenu-glib/dbusmenu-glib.h>
#include <gtk/gtk.h>
#include "dbus-names.h"


static void
service_shutdown (IndicatorService *service, gpointer user_data)
{
    g_debug("Shutting down indicator-printers-service");
    gtk_main_quit ();
}


static void
show_system_settings (DbusmenuMenuitem *menuitem,
                      guint timestamp,
                      gpointer user_data)
{
    GAppInfo *appinfo;
    GError *err = NULL;

    appinfo = g_app_info_create_from_commandline ("gnome-control-center printing",
                                                  "gnome-control-center",
                                                  G_APP_INFO_CREATE_SUPPORTS_STARTUP_NOTIFICATION,
                                                  &err);
    if (err) {
        g_warning ("failed to create application info: %s", err->message);
        g_error_free (err);
        return;
    }

    g_app_info_launch (appinfo, NULL, NULL, &err);
    if (err) {
        g_warning ("failed to launch gnome-control-center: %s", err->message);
        g_error_free (err);
    }

    g_object_unref (appinfo);
}


static void
dbusmenu_menuitem_append_separator (DbusmenuMenuitem *item)
{
    DbusmenuMenuitem *separator;

    separator = dbusmenu_menuitem_new();
    dbusmenu_menuitem_property_set(separator,
                                   "type",
                                   DBUSMENU_CLIENT_TYPES_SEPARATOR);

    dbusmenu_menuitem_child_append(item, separator);
    g_object_unref (separator);
}


static void
dbusmenu_menuitem_append_label (DbusmenuMenuitem *item,
                                const gchar *text,
                                GCallback activated,
                                gpointer user_data)
{
    DbusmenuMenuitem *child;

    child = dbusmenu_menuitem_new ();
    dbusmenu_menuitem_property_set (child, "label", text);
    g_signal_connect (child, "item-activated", activated, user_data);

    dbusmenu_menuitem_child_append(item, child);
    g_object_unref (child);
}


static DbusmenuMenuitem *
create_menu ()
{
    DbusmenuMenuitem *root;

    root = dbusmenu_menuitem_new ();
    dbusmenu_menuitem_append_separator (root);
    dbusmenu_menuitem_append_label (root,
                                    "Printer Settings…",
                                    G_CALLBACK(show_system_settings),
                                    NULL);
    return root;
}


int main (int argc, char *argv[])
{
    IndicatorService *service;
    DbusmenuServer *menuserver;
    DbusmenuMenuitem *root;
    GError *err = NULL;

    gtk_init (&argc, &argv);

    service = indicator_service_new_version (INDICATOR_PRINTERS_DBUS_NAME,
                                             INDICATOR_PRINTERS_DBUS_VERSION);
    g_signal_connect (service,
                      "shutdown",
                      G_CALLBACK (service_shutdown),
                      NULL);

    menuserver = dbusmenu_server_new (INDICATOR_PRINTERS_DBUS_OBJECT_PATH);
    root = create_menu ();
    dbusmenu_server_set_root (menuserver, root);
    g_object_unref (root);

    gtk_main ();

    g_object_unref (menuserver);
    g_object_unref (service);
    return 0;
}