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

« back to all changes in this revision

Viewing changes to monitor/monitor.vala

  • Committer: Bazaar Package Importer
  • Author(s): Michael Terry
  • Date: 2010-01-10 10:03:06 UTC
  • mto: This revision was merged to the branch mainline in revision 8.
  • Revision ID: james.westby@ubuntu.com-20100110100306-awjywmnuln6yusb9
Tags: upstream-13.5
Import upstream version 13.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 
23
23
class Monitor : Object {
24
24
 
25
 
static const string GCONF_DIR = "/apps/deja-dup";
26
 
static const string LAST_RUN_KEY = "/apps/deja-dup/last-run";
27
 
static const string PERIODIC_KEY = "/apps/deja-dup/periodic";
28
 
static const string PERIODIC_PERIOD_KEY = "/apps/deja-dup/periodic-period";
29
 
static const string BACKEND_KEY = "/apps/deja-dup/backend";
30
 
static const string FILE_PATH_KEY = "/apps/deja-dup/file/path";
31
 
static const uint32 NM_STATE_CONNECTED = 3;
32
 
 
33
25
static MainLoop loop;
34
26
static uint timeout_id;
35
27
static Pid pid;
36
 
static bool connected = true;
 
28
static bool reactive_check;
37
29
static bool testing;
38
30
 
39
31
static bool show_version = false;
43
35
  {null}
44
36
};
45
37
 
46
 
static dynamic DBus.Object network_manager;
47
 
 
48
 
static void init_dbus_to_network_manager() throws DBus.Error, GLib.Error
49
 
50
 
  //Set up the DBus connection to network manager
51
 
  DBus.Connection conn = DBus.Bus.get(DBus.BusType.SYSTEM);
52
 
  network_manager = conn.get_object(
53
 
    "org.freedesktop.NetworkManager",
54
 
    "/org/freedesktop/NetworkManager",
55
 
    "org.freedesktop.NetworkManager");
56
 
 
57
 
  //Retrieve the network manager connection state.
58
 
  uint32 network_manager_state = network_manager.State;
59
 
  connected = network_manager_state == NM_STATE_CONNECTED;
60
 
 
61
 
  //Dbus signal when the state of the connection is changed.
62
 
  network_manager.StateChanged += network_manager_state_changed;
63
 
}
64
 
 
65
 
static void network_manager_state_changed(DBus.Object obj, uint32 new_state)
 
38
static Notify.Notification note;
 
39
 
 
40
static void network_changed(DejaDup.NetworkManager nm, bool connected)
66
41
{
67
 
  connected = new_state == NM_STATE_CONNECTED;
68
 
 
 
42
  reactive_check = true;
69
43
  if (connected)
70
 
      prepare_next_run();
71
 
}
72
 
 
73
 
static bool is_native()
74
 
{
75
 
  //Detect a remote backend such as Amazon S3 or SSH.
76
 
  bool native_path = true;
 
44
    prepare_next_run(); // in case network manager was blocking us
 
45
  reactive_check = false;
 
46
}
 
47
 
 
48
static void volume_added(VolumeMonitor vm, Volume vol)
 
49
{
 
50
  reactive_check = true;
 
51
  prepare_next_run(); // in case missing volume was blocking us
 
52
  reactive_check = false;
 
53
}
 
54
 
 
55
static bool is_ready(out string when)
 
56
{
77
57
  try {
78
 
    var client = GConf.Client.get_default();
79
 
    string backend_name = client.get_string(BACKEND_KEY);
80
 
 
81
 
    if (backend_name == "s3")
82
 
      native_path = false;
83
 
    else if (backend_name == "file")
84
 
    {
85
 
      string path = client.get_string(FILE_PATH_KEY);
86
 
      var backend_file = File.parse_name(path);
87
 
      native_path = backend_file.is_native();
88
 
    }
89
 
  }
90
 
  catch (GLib.Error e) {
91
 
    warning("%s", e.message);
92
 
  }
93
 
 
94
 
  return native_path;
 
58
    return DejaDup.Backend.get_default().is_ready(out when);
 
59
  }
 
60
  catch (Error e) {
 
61
    return true;
 
62
  }
95
63
}
96
64
 
97
65
static bool handle_options(out int status)
99
67
  status = 0;
100
68
  
101
69
  if (show_version) {
102
 
    print("%s %s\n", _("Déjà Dup Monitor"), Config.VERSION);
 
70
    print("%s %s\n", Environment.get_application_name(), Config.VERSION);
103
71
    return false;
104
72
  }
105
73
  
141
109
  int period_days;
142
110
  
143
111
  try {
144
 
    periodic = client.get_bool(PERIODIC_KEY);
145
 
    last_run_string = client.get_string(LAST_RUN_KEY);
146
 
    period_days = client.get_int(PERIODIC_PERIOD_KEY);
 
112
    periodic = client.get_bool(DejaDup.PERIODIC_KEY);
 
113
    last_run_string = client.get_string(DejaDup.LAST_RUN_KEY);
 
114
    period_days = client.get_int(DejaDup.PERIODIC_PERIOD_KEY);
147
115
  }
148
116
  catch (Error e) {
149
117
    error("%s", e.message);
205
173
  TimeVal next_time = date_to_timeval(date);
206
174
  
207
175
  if (testing)
208
 
    return 10;
 
176
    return 5;
209
177
  else
210
178
    return next_time.tv_sec - cur_time.tv_sec;
211
179
}
216
184
  pid = (Pid)0;
217
185
}
218
186
 
 
187
static void notify_delay(string header, string reason)
 
188
{
 
189
  if (note == null) {
 
190
    Notify.init(Environment.get_application_name());
 
191
    note = new Notify.Notification(header, reason,
 
192
                                   "deja-dup-backup", null);
 
193
    note.closed.connect((n) => {note = null;});
 
194
  }
 
195
  else
 
196
    note.update(header, reason, "deja-dup-backup");
 
197
 
 
198
  try {
 
199
    note.show();
 
200
  }
 
201
  catch (Error e) {
 
202
    warning("%s\n", e.message);
 
203
  }
 
204
}
 
205
 
219
206
static bool kickoff()
220
207
{
221
208
  long wait_time;
228
215
    return false;
229
216
  }
230
217
 
231
 
  // Now we secretly schedule another kickoff tomorrow, in case something
232
 
  // goes wrong with this run (or user chooses to ignore for now)
233
 
  // If this run is successful, it will change 'last-run' key and this will
234
 
  // get rescheduled anyway.
235
 
  prepare_tomorrow();
236
 
 
237
 
  if (!is_native() && !connected) {
238
 
      debug("No connection found. Postponing the backup.");
239
 
      return false;
240
 
  }
241
 
 
242
 
  // Don't run right now if an applet is already running
243
 
  if (pid == (Pid)0) {
 
218
  if (!reactive_check) {
 
219
    // Now we secretly schedule another kickoff tomorrow, in case something
 
220
    // goes wrong with this run (or user chooses to ignore for now)
 
221
    // If this run is successful, it will change 'last-run' key and this will
 
222
    // get rescheduled anyway.
 
223
    prepare_tomorrow();
 
224
  }
 
225
 
 
226
  string when;
 
227
  if (!is_ready(out when)) {
 
228
    debug("Postponing the backup.");
 
229
    if (!reactive_check)
 
230
      notify_delay(_("Scheduled backup delayed"), when);
 
231
    return false;
 
232
  }
 
233
 
 
234
  // Don't run right now if an instance is already running
 
235
  if (pid == (Pid)0 && !DejaDup.test_bus_claimed("operation")) {
244
236
    try {
245
237
      string[] argv = new string[3];
246
238
      argv[0] = "deja-dup";
314
306
  var client = GConf.Client.get_default();
315
307
  
316
308
  try {
317
 
    client.add_dir(GCONF_DIR, GConf.ClientPreloadType.NONE);
318
 
    client.notify_add(LAST_RUN_KEY, prepare_next_run);
319
 
    client.notify_add(PERIODIC_KEY, prepare_next_run);
320
 
    client.notify_add(PERIODIC_PERIOD_KEY, prepare_next_run);
 
309
    client.add_dir(DejaDup.GCONF_DIR, GConf.ClientPreloadType.NONE);
 
310
    client.notify_add(DejaDup.LAST_RUN_KEY, prepare_next_run);
 
311
    client.notify_add(DejaDup.PERIODIC_KEY, prepare_next_run);
 
312
    client.notify_add(DejaDup.PERIODIC_PERIOD_KEY, prepare_next_run);
321
313
  }
322
314
  catch (Error e) {
323
315
    warning("%s\n", e.message);
326
318
 
327
319
static int main(string[] args)
328
320
{
329
 
  try {
330
 
    init_dbus_to_network_manager();
331
 
  }
332
 
  catch (Error e) {
333
 
    warning("%s\n\n%s", e.message, "Failed to initialize DBus\n");
334
 
  }
335
 
 
336
321
  GLib.Intl.textdomain(Config.GETTEXT_PACKAGE);
337
322
  GLib.Intl.bindtextdomain(Config.GETTEXT_PACKAGE, Config.LOCALE_DIR);
338
323
  GLib.Intl.bind_textdomain_codeset(Config.GETTEXT_PACKAGE, "UTF-8");
339
324
  
340
325
  // Translators: Monitor in this sense means something akin to 'watcher', not
341
 
  // a computer monitor.  This program acts like a daemon that kicks off
 
326
  // a computer screen.  This program acts like a daemon that kicks off
342
327
  // backups at scheduled times.
343
328
  GLib.Environment.set_application_name(_("Déjà Dup Monitor"));
344
329
  
355
340
  if (!handle_options(out status))
356
341
    return status;
357
342
 
 
343
  DejaDup.initialize();
 
344
  DejaDup.NetworkManager.get().changed.connect(network_changed);
 
345
  VolumeMonitor.get().volume_added.connect(volume_added);
 
346
 
358
347
  loop = new MainLoop(null, false);
359
348
  
360
349
  prepare_next_run();