summaryrefslogtreecommitdiff
path: root/src/contacts-operation.vala
blob: 97a430c6020028b6e2fe5545345eeb66630fd76c (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
/*
 * Copyright (C) 2021 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/>.
 */

/**
 * Contacts.Operation is a simple interface to describe actions that can be
 * executed and possibly undone later on (for example, using a button on an
 * in-app notification).
 *
 * Since some operations might not be able undoable later onwards, there is a
 * property `reversable` that you should check first before calling undo().
 *
 * Note that you probably shouldn't be calling the execute() method directly,
 * but use the API provided by {@link OperationQueue} instead.
 */
public abstract class Contacts.Operation : Object {

  /** The UUID of the operation */
  public string uuid { get; private set; default = Uuid.string_random (); }

  /**
   * Whether undo() can be called on this object
   */
  public abstract bool reversable { get; }

  /**
   * A user-facing string that tells us what the operation does
   */
  public abstract string description { owned get; }

  /**
   * This the actual implementation of the operation that a subclass needs to
   * implement.
   */
  public abstract async void execute () throws GLib.Error;

  /**
   * The is the public API undo. If you want, you can override it still, e.g.
   * to provide better warnings.
   */
  public virtual async void undo () throws GLib.Error {
    // FIXME: should throw an error instead so we can show something to the user
    if (!this.reversable) {
      warning ("Can't undo '%s'", this.description);
      return;
    }

    yield this._undo ();
  }

  /**
   * This the actual implementation of the undo that a subclass needs to
   * implement.
   */
  protected abstract async void _undo () throws GLib.Error;
}