1 /******************************************************************************
2   This source file is part of the Avogadro project.
3   This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5 
6 #ifndef AVOGADRO_MOLEQUEUE_MOLEQUEUEMANAGER_H
7 #define AVOGADRO_MOLEQUEUE_MOLEQUEUEMANAGER_H
8 
9 #include <QtCore/QObject>
10 
11 #include "molequeuequeuelistmodel.h"
12 
13 #include <avogadro/core/avogadrocore.h>
14 
15 #include "client/client.h"
16 
17 namespace Avogadro {
18 namespace MoleQueue {
19 
20 /**
21  * @class MoleQueueManager molequeuemanager.h
22  * <avogadro/molequeue/molequeuemanager.h>
23  * @brief The MoleQueueManager class provides access to a MoleQueue server.
24  *
25  * This singleton class provides access to a single MoleQueue::Client instance
26  * that can be used to communicate with the server. The available queues and
27  * programs are cached in a MoleQueueQueueListModel (queueListModel()). The
28  * connectIfNeeded convenience function can be used to ensure that the client
29  * is connected before use.
30  */
31 class AVOGADROMOLEQUEUE_EXPORT MoleQueueManager : public QObject
32 {
33   Q_OBJECT
34 public:
35   explicit MoleQueueManager(QObject* parent_ = 0);
36   ~MoleQueueManager() override;
37 
38   /**
39    * @return The singleton instance.
40    */
41   static MoleQueueManager& instance();
42 
43   /**
44    * Test if the client is connected, and if not, attempt a connection.
45    * @return True if the client is already connected or a new connection has
46    * been successfully created. False if the new connection failed.
47    */
48   bool connectIfNeeded();
49 
50   /**
51    * @return A reference to the managed Client instance.
52    * @{
53    */
54   Client& client();
55   const Client& client() const;
56   /** @} */
57 
58   /**
59    * @return A QAbstractItemModel subclass representing the queue/program tree.
60    */
61   MoleQueueQueueListModel& queueListModel();
62 
63 public slots:
64   /**
65    * Request that the cached queue list is updated.
66    * @return True if the request is send successfully.
67    */
68   bool requestQueueList();
69 
70 signals:
71   /**
72    * Emitted when the internal queue list is updated.
73    */
74   void queueListUpdated();
75 
76 private slots:
77   void updateQueueModel(const QJsonObject& queueList);
78 
79 private:
80   static MoleQueueManager* m_instance;
81   Client m_client;
82   MoleQueueQueueListModel m_queueModel;
83 };
84 
85 } // namespace MoleQueue
86 } // namespace Avogadro
87 
88 #endif // AVOGADRO_MOLEQUEUE_MOLEQUEUEMANAGER_H
89