aboutsummaryrefslogtreecommitdiff
path: root/src/service.vala
blob: e651811beeee6bfb4539c9caf4daa525f70e873b (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
/*
 * Copyright 2013 Canonical Ltd.
 * Copyright 2025 Robert Tari
 *
 * 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; version 3.
 *
 * 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:
 *   Charles Kerr <charles.kerr@canonical.com>
 *   Robert Ancell <robert.ancell@canonical.com>
 *   Robert Tari <robert@tari.in>
 */

/**
 * Boilerplate class to own the name on the bus,
 * to create the profiles, and to export them on the bus.
 */
public class Service: Object
{
  private MainLoop loop;
  private SimpleActionGroup actions;
  private HashTable<string,Profile> profiles;
  private Bluetooth bluetooth;
  private DBusConnection connection;
  private uint exported_action_id;
  private const string OBJECT_PATH = "/org/ayatana/indicator/bluetooth";

  private void unexport ()
  {
    if (connection != null)
      {
        profiles.for_each ((name, profile)
          => profile.unexport_menu (connection));

        if (exported_action_id != 0)
          {
            debug (@"unexporting action group '$(OBJECT_PATH)'");
            connection.unexport_action_group (exported_action_id);
            exported_action_id = 0;
          }
      }
  }

  public Service (Bluetooth bluetooth_service)
  {
    actions = new SimpleActionGroup ();
    bluetooth = bluetooth_service;

    profiles = new HashTable<string,Profile> (str_hash, str_equal);
    profiles.insert ("phone", new Phone (bluetooth, actions));
    profiles.insert ("desktop", new Desktop (bluetooth, actions));
    profiles.insert ("greeter", new Greeter (bluetooth, actions));
  }

  public int run ()
  {
    if (loop != null)
      {
        warning ("service is already running");
        return Posix.EXIT_FAILURE;
      }

    var own_name_id = Bus.own_name (BusType.SESSION,
                                    "org.ayatana.indicator.bluetooth",
                                    BusNameOwnerFlags.NONE,
                                    on_bus_acquired,
                                    null,
                                    on_name_lost);

    var system_name_id = Bus.own_name (BusType.SYSTEM,
                                       "org.ayatana.indicator.bluetooth",
                                       BusNameOwnerFlags.NONE,
                                       on_system_bus_acquired,
                                       null,
                                       null);

    bluetooth.agent_manager_ready.connect (() => {
        bluetooth.add_agent ("/agent");
    });

    loop = new MainLoop (null, false);
    loop.run ();

    // cleanup
    unexport ();
    Bus.unown_name (own_name_id);
    Bus.unown_name (system_name_id);
    return Posix.EXIT_SUCCESS;
  }

  void on_system_bus_acquired (DBusConnection connection, string name)
  {
    try
    {
        connection.register_object ("/agent", new Agent (bluetooth));
    }
    catch (GLib.IOError pError)
    {
        warning ("Panic: Failed registering pairing agent: %s", pError.message);
    }
  }

  void on_bus_acquired (DBusConnection connection, string name)
  {
    debug (@"bus acquired: $name");
    this.connection = connection;

    try
      {
        debug (@"exporting action group '$(OBJECT_PATH)'");
        exported_action_id = connection.export_action_group (OBJECT_PATH,
                                                             actions);
      }
    catch (Error e)
      {
        critical (@"Unable to export actions on $OBJECT_PATH: $(e.message)");
      }

    profiles.for_each ((name, profile)
        => profile.export_menu (connection, @"$OBJECT_PATH/$name"));
  }

  void on_name_lost (DBusConnection connection, string name)
  {
    debug (@"name lost: $name");
    loop.quit ();
  }
}