~ubuntu-branches/ubuntu/precise/deja-dup/precise-proposed

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
/* -*- Mode: Vala; indent-tabs-mode: nil; tab-width: 2 -*- */
/*
    This file is part of Déjà Dup.
    © 2009–2010 Michael Terry <mike@mterry.name>,
    © 2009 Andrew Fister <temposs@gmail.com>

    Déjà Dup 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 3 of the License, or
    (at your option) any later version.

    Déjà Dup 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 Déjà Dup.  If not, see <http://www.gnu.org/licenses/>.
*/

using GLib;

namespace DejaDup {

abstract class StatusProvider : Object
{
  public enum Status {ONLINE, OFFLINE, UNKNOWN}
  public Status status {get; protected set; default = Status.UNKNOWN;}

  protected DBusProxy proxy {get; private set;}

  construct {
    try {
      proxy = create_proxy();
      proxy.notify["g-name-owner"].connect(owner_changed);
      proxy.g_signal.connect(handle_signal);
      check_owner_status();
    }
    catch (Error e) {
      warning("%s\n", e.message);
    }
  }

  protected void update_status(Status new_status)
  {
    if (status != new_status)
      status = new_status;
  }

  private void owner_changed()
  {
    check_owner_status();
  }

  private async void check_owner_status()
  {
    if (proxy.g_name_owner == null)
      update_status(Status.UNKNOWN);
    else {
      Status status = Status.UNKNOWN;
      try {
        status = yield query_status();
      }
      catch (Error e) {
        warning("%s\n", e.message);
      }
      update_status(status);
    }
  }

  protected abstract DBusProxy create_proxy() throws Error;
  protected abstract async Status query_status() throws Error;
  protected abstract void handle_signal(string sender_name, string signal_name,
                                        GLib.Variant parameters);
}

class StatusNetworkManager : StatusProvider
{
  static const uint32 NM_OLD_STATE_CONNECTED = 3;
  static const uint32 NM_STATE_CONNECTED_LOCAL = 50;
  static const uint32 NM_STATE_CONNECTED_SITE = 60;
  static const uint32 NM_STATE_CONNECTED_GLOBAL = 70;

  protected override DBusProxy create_proxy() throws Error
  {
    // FIXME: use async version when I figure out the syntax
    return new DBusProxy.for_bus_sync(BusType.SYSTEM, DBusProxyFlags.NONE, null, 
                                      "org.freedesktop.NetworkManager",
                                      "/org/freedesktop/NetworkManager",
                                      "org.freedesktop.NetworkManager", null);
  }

  protected override async StatusProvider.Status query_status() throws Error
  {
    Variant state_val = proxy.get_cached_property("State");
    if (state_val == null || !state_val.is_of_type(VariantType.UINT32))
      return Status.UNKNOWN;

    uint32 state = state_val.get_uint32();
    if (state == NM_OLD_STATE_CONNECTED ||
        state == NM_STATE_CONNECTED_LOCAL ||
        state == NM_STATE_CONNECTED_SITE ||
        state == NM_STATE_CONNECTED_GLOBAL)
      return Status.ONLINE;
    else
      return Status.OFFLINE;
  }

  protected override void handle_signal(string sender_name, string signal_name,
                                        GLib.Variant parameters)
  {
    if (signal_name == "StateChanged") {
      uint32 state;
      parameters.get("(u)", out state);

      if (state == NM_OLD_STATE_CONNECTED ||
          state == NM_STATE_CONNECTED_LOCAL ||
          state == NM_STATE_CONNECTED_SITE ||
          state == NM_STATE_CONNECTED_GLOBAL)
        update_status(Status.ONLINE);
      else
        update_status(Status.OFFLINE);
    }
  }  
}

class StatusConnectionManager : StatusProvider
{
  static const string CM_STATE_CONNECTED = "online";

  protected override DBusProxy create_proxy() throws Error
  {
    // FIXME: use async version when I figure out the syntax
    return new DBusProxy.for_bus_sync(BusType.SYSTEM, DBusProxyFlags.NONE, null, 
                                      "org.moblin.connman", "/",
                                      "org.moblin.connman.Manager", null);
  }

  protected override async StatusProvider.Status query_status() throws Error
  {
    Variant state_val = yield proxy.call("GetState", null,
                                         DBusCallFlags.NONE, -1, null);
    if (state_val == null)
      return Status.UNKNOWN;

    string state;
    state_val.get("(s)", out state);
    if (state == CM_STATE_CONNECTED)
      return Status.ONLINE;
    else if (state != null)
      return Status.OFFLINE;
    else
      return Status.UNKNOWN;
  }

  protected override void handle_signal(string sender_name, string signal_name,
                                        GLib.Variant parameters)
  {
    if (signal_name == "StateChanged") {
      string state;
      parameters.get("(s)", out state);
      update_status(state == CM_STATE_CONNECTED ? Status.ONLINE : Status.OFFLINE);
    }
  }
}

public class Network : Object
{
  public bool connected {get; set; default = true;}

  public new static Network get() {
    if (singleton == null)
      singleton = new Network();
    return singleton;
  }

  static Network singleton;
  List<StatusProvider> providers;

  construct {
    providers = new List<StatusProvider>();
    add_provider(new StatusNetworkManager());
    add_provider(new StatusConnectionManager());
    update_status();
  }

  void add_provider(StatusProvider p) {
    providers.prepend(p);
    p.notify["status"].connect(update_status);
  }

  void update_status()
  {
    /* If any of our network status providers is active and running, use it */
    bool offline = false;
    bool online = false;
    foreach (StatusProvider p in providers) {
      if (p.status == StatusProvider.Status.OFFLINE)
        offline = true;
      else if (p.status == StatusProvider.Status.ONLINE)
        online = true;
    }

    bool merged_status;
    if (online)
      merged_status = true;
    else if (offline)
      merged_status = false;
    else
      merged_status = true; // no information, assume online

    if (merged_status != connected)
      connected = merged_status;
  }
}

} // end namespace