1 /*
2  * LibrePCB - Professional EDA for everyone!
3  * Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
4  * https://librepcb.org/
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef LIBREPCB_NETWORKACCESSMANAGER_H
21 #define LIBREPCB_NETWORKACCESSMANAGER_H
22 
23 /*******************************************************************************
24  *  Includes
25  ******************************************************************************/
26 #include "../fileio/filepath.h"
27 
28 #include <QtCore>
29 #include <QtNetwork>
30 
31 /*******************************************************************************
32  *  Namespace / Forward Declarations
33  ******************************************************************************/
34 namespace librepcb {
35 
36 /*******************************************************************************
37  *  Class NetworkAccessManager
38  ******************************************************************************/
39 
40 /**
41  * @brief A network access manager which processes network requests in a
42  separate thread
43  *
44  * @note    One instance of this class must be created in the main application
45  thread, and
46  *          must be deleted before stopping the main application thread. It's
47  not allowed
48  *          to create a librepcb::NetworkAccessManager object in other threads,
49  or to
50  *          create multiple instances at the same time.
51  *
52  * After the singleton was created, you can get it with the static method
53  #instance().
54  * But for executing network requests, you don't need to access this object
55  directly.
56  * You only need the classes librepcb::NetworkRequest and librepcb::FileDownload
57  instead.
58 
59  * @see librepcb::NetworkRequestBase, librepcb::NetworkRequest,
60  librepcb::FileDownload
61  */
62 class NetworkAccessManager final : public QThread {
63   Q_OBJECT
64 
65 public:
66   // Constructors / Destructor
67   NetworkAccessManager() noexcept;
68   NetworkAccessManager(const NetworkAccessManager& other) = delete;
69   ~NetworkAccessManager() noexcept;
70 
71   // General Methods
72   QNetworkReply* get(const QNetworkRequest& request) noexcept;
73   QNetworkReply* post(const QNetworkRequest& request,
74                       const QByteArray& data) noexcept;
75 
76   // Operator Overloadings
77   NetworkAccessManager& operator=(const NetworkAccessManager& rhs) = delete;
78 
79   // Static Methods
80   static NetworkAccessManager* instance() noexcept;
81 
82 private:  // Methods
83   void run() noexcept override;
84   void stop() noexcept;
85 
86 private:  // Data
87   QSemaphore mThreadStartSemaphore;
88   QNetworkAccessManager* mManager;
89   static NetworkAccessManager* sInstance;
90 };
91 
92 }  // namespace librepcb
93 
94 #endif  // LIBREPCB_NETWORKACCESSMANAGER_H
95