summaryrefslogtreecommitdiff
path: root/src/multiprocess.h
blob: 810e63d23e832104de01378af7d96c6769e12b5d (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
#ifndef MODORGANIZER_MOMULTIPROCESS_INCLUDED
#define MODORGANIZER_MOMULTIPROCESS_INCLUDED

#include <QObject>
#include <QSharedMemory>
#include <QLocalServer>


/**
 * used to ensure only a single process of Mod Organizer is started and to
 * allow ephemeral processes to send messages to the primary (visible) one.
 * This way, other processes can start a download in the primary one
 **/
class MOMultiProcess : public QObject
{
  Q_OBJECT

public:
  // `allowMultiple`: if another process is running, run this one
  // disconnected from the shared memory
  explicit MOMultiProcess(bool allowMultiple, QObject *parent = 0);

  /**
   * @return true if this process's job is to forward data to the primary
   *              process through shared memory
   **/
  bool ephemeral() const
  {
    return m_Ephemeral;
  }

  // returns true if this is not the primary process, but was allowed because
  // of the AllowMultiple flag
  //
  bool secondary() const
  {
    return !m_Ephemeral && !m_OwnsSM;
  }

  /**
   * send a message to the primary process. This can be used to transmit download urls
   *
   * @param message message to send
   **/
  void sendMessage(const QString &message);

signals:

  /**
   * @brief emitted when an ephemeral process has sent a message (to us)
   *
   * @param message the message we received
   **/
  void messageSent(const QString &message);

public slots:

private slots:

  void receiveMessage();

private:
  bool m_Ephemeral;
  bool m_OwnsSM;
  QSharedMemory m_SharedMem;
  QLocalServer m_Server;

};

#endif // MODORGANIZER_MOMULTIPROCESS_INCLUDED