summaryrefslogtreecommitdiff
path: root/src/contacts-operation-list.vala
blob: c895bf1e0ae3cb9309649aca501850e9cce61432 (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
/*
 * Copyright (C) 2022 Niels De Graef <nielsdegraef@gmail.com>
 *
 * 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/>.
 */

/**
 * A list of {@link Contacts.Operation}s.
 */
public class Contacts.OperationList : Object {

  // A helper class to add extra (private) API to operations
  private class OpEntry {
    public Operation operation;
    public Cancellable? cancellable;
    public uint timeout_src;
    public bool finished;

    public OpEntry (Operation operation, Cancellable? cancellable = null) {
      this.operation = operation;
      this.cancellable = cancellable;
      this.timeout_src = 0;
      this.finished = false;
    }

    public bool is_cancelled () {
      return (this.cancellable != null) && this.cancellable.is_cancelled ();
    }
  }

  private GenericArray<OpEntry?> operations = new GenericArray<OpEntry?> ();

  public OperationList () {
  }

  /** Asynchronously executes the given operation */
  public async void execute (Operation operation,
                             Cancellable? cancellable) throws GLib.Error {
    yield execute_with_timeout (operation, 0, cancellable);
  }

  /** Asynchronously executes the given operation after a timeout */
  public async void execute_with_timeout (Operation operation,
                                          uint timeout,
                                          Cancellable? cancellable) throws GLib.Error {
    // Create a new OpEntry to keep track and add it
    var entry = new OpEntry (operation, cancellable);
    this.operations.add (entry);

    // Schedule the callback
    SourceFunc callback = execute_with_timeout.callback;
    if (timeout > 0) {
      entry.timeout_src = Timeout.add_seconds (timeout, (owned) callback);
    } else {
      entry.timeout_src = Idle.add ((owned) callback);
    }

    // Let the main loop take control again, our callback should be scheduled
    // at this point.
    yield;

    yield execute_operation_now (entry);
  }

  /** Cancel the operation with the given UUID */
  public async void cancel_operation (string uuid) throws GLib.Error {
    debug ("Cancelling operation '%s'", uuid);

    unowned var entry = find_by_uuid (uuid);
    if (entry == null || entry.finished) { // FIXME: throw some error
      warning ("Can't cancel operation with uuid '%s': not found", uuid);
      return;
    }

    if (entry.finished) { // FIXME: throw some error
      warning ("Can't cancel operation '%s': already finished",
               entry.operation.description);
      return;
    }

    if (entry.is_cancelled ())
      return; // no-op

    entry.cancellable.cancel ();
  }

  /**
   * Undo the operation with the given UUID
   */
  public async void undo_operation (string uuid) throws GLib.Error {
    debug ("Undoing operation '%s'", uuid);

    unowned var entry = find_by_uuid (uuid);
    if (entry == null) { // FIXME: throw some error
      warning ("Can't undo operation with uuid '%s': not found", uuid);
      return;
    }

    if (!entry.operation.reversable || !entry.finished || entry.is_cancelled ()) {
      warning ("Can't undo operation with uuid '%s'", uuid);
      return;
    }

    yield entry.operation.undo ();
  }

  /**
   * Returns whether there are operations that are still unfinished
   */
  public bool has_pending_operations () {
    return (find_next_todo () != null);
  }

  /**
   * Flushes the current list of operaions. This will execute any operation
   * that was still scheduled for execution.
   */
  public async void flush () throws GLib.Error {
    if (!has_pending_operations ())
      return;

    debug ("Flushing %u operations", this.operations.length);

    unowned var entry = find_next_todo ();
    while (entry != null) {
      debug ("Flushing operation '%s'", entry.operation.description);
      yield execute_operation_now (entry);
      entry = find_next_todo ();
    }

    this.operations.remove_range (0, this.operations.length);
  }

  private unowned OpEntry? find_next_todo () {
    for (uint i = 0; i < operations.length; i++) {
      unowned var entry = operations[i];
      if (!entry.finished && !entry.is_cancelled ())
        return entry;
    }

    return null;
  }

  private unowned OpEntry? find_by_uuid (string uuid) {
    for (uint i = 0; i < operations.length; i++) {
      if (operations[i].operation.uuid == uuid)
        return operations[i];
    }

    return null;
  }

  private async void execute_operation_now (OpEntry? entry) throws GLib.Error {
    // Clear any scheduled callbacks
    entry.timeout_src = 0;

    // Check if it might've been scheduled in the meantime
    if (entry.is_cancelled ()) {
      throw new IOError.CANCELLED ("Operation '%s' was cancelled",
                                   entry.operation.description);
    }

    debug ("Starting execution of operation '%s' (%s)",
           entry.operation.description, entry.operation.uuid);
    yield entry.operation.execute ();
    entry.finished = true;
    debug ("Finished operation '%s' (%s)",
           entry.operation.description, entry.operation.uuid);
  }
}