summaryrefslogtreecommitdiff
path: root/src/uilocker.h
blob: ec188f1ada89bad786e1e4c33b24e9407c493901 (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
#ifndef MODORGANIZER_UILOCKER_INCLUDED
#define MODORGANIZER_UILOCKER_INCLUDED

#include <QMainWindow>
#include <mutex>

class UILockerInterface;

class UILocker
{
  friend class UILockerInterface;

public:
  // reason to show the widget
  //
  enum Reasons
  {
    NoReason = 0,

    // lock the ui
    LockUI,

    // because the output is required
    OutputRequired,

    // to prevent exiting until all processes are completed
    PreventExit
  };

  // returned by result()
  //
  enum Results
  {
    NoResult = 0,

    // the widget is still up
    StillLocked,

    // force unlock was clicked
    ForceUnlocked,

    // cancel was clicked
    Cancelled
  };

  class Session
  {
  public:
    ~Session();

    void unlock();
    void setInfo(DWORD pid, const QString& name);
    Results result() const;

    DWORD pid() const;
    const QString& name() const;

  private:
    mutable std::mutex m_mutex;
    DWORD m_pid;
    QString m_name;
  };

  UILocker();
  ~UILocker();

  static UILocker& instance();

  void setUserInterface(QWidget* parent);

  std::shared_ptr<Session> lock(Reasons reason);
  bool locked() const;

  Results result() const;

private:
  QWidget* m_parent;
  std::unique_ptr<UILockerInterface> m_ui;
  std::vector<std::weak_ptr<Session>> m_sessions;
  std::atomic<Results> m_result;
  std::vector<QPointer<QWidget>> m_disabled;

  void createUi(Reasons reason);

  void unlockCurrent();
  void unlock(Session* s);
  void updateLabel();

  void onForceUnlock();
  void onCancel();

  void disableAll();
  void enableAll();
  void disable(QWidget* w);
};

#endif  // MODORGANIZER_UILOCKER_INCLUDED