Debian Codegraph @

src/Gui/DownloadManager.h

raw
1 /***************************************************************************
2 * Copyright (c) 2013 Werner Mayer <wmayer[at]users.sourceforge.net> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library 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 Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23
24 #ifndef GUI_DIALOG_DOWNLOADMANAGER_H
25 #define GUI_DIALOG_DOWNLOADMANAGER_H
26
27 #include <QAbstractListModel>
28 #include <QDialog>
29 #include <QNetworkAccessManager>
30 #include <QNetworkReply>
31 #include <QUrl>
32 #include <FCGlobal.h>
33
34
35 class AutoSaver;
36 class QFileIconProvider;
37
38 namespace Gui
39 {
40 namespace Dialog
41 {
42 class DownloadItem;
43 class DownloadModel;
44 class Ui_DownloadManager;
45
46 class GuiExport DownloadManager: public QDialog
47 {
48 Q_OBJECT
49
50 public:
51 enum RemovePolicy
52 {
53 Never,
54 Exit,
55 SuccessFullDownload
56 };
57
58 Q_PROPERTY(
59 RemovePolicy removePolicy READ removePolicy WRITE setRemovePolicy
60 ) // clazy:exclude=qproperty-without-notify
61 Q_ENUM(RemovePolicy)
62
63 public:
64 static DownloadManager* getInstance();
65
66 private:
67 explicit DownloadManager(QWidget* parent = nullptr);
68 ~DownloadManager() override;
69
70 public:
71 int activeDownloads() const;
72 QNetworkAccessManager* networkAccessManager()
73 {
74 return m_manager;
75 }
76
77 RemovePolicy removePolicy() const;
78 void setRemovePolicy(RemovePolicy policy);
79 void closeEvent(QCloseEvent* e) override;
80 QUrl redirectUrl(const QUrl&) const;
81
82 public Q_SLOTS:
83 void download(const QNetworkRequest& request, bool requestFileName = false);
84 inline void download(const QUrl& url, bool requestFileName = false)
85 {
86 download(QNetworkRequest(url), requestFileName);
87 }
88 void handleUnsupportedContent(QNetworkReply* reply, bool requestFileName = false);
89 void cleanup();
90
91 private Q_SLOTS:
92 void save() const;
93 void updateRow();
94
95 private:
96 void addItem(DownloadItem* item);
97 void updateItemCount();
98 void load();
99
100 AutoSaver* m_autoSaver;
101 DownloadModel* m_model;
102 QNetworkAccessManager* m_manager;
103 QFileIconProvider* m_iconProvider;
104 QList<DownloadItem*> m_downloads;
105 RemovePolicy m_removePolicy;
106 friend class DownloadModel;
107
108 private:
109 Ui_DownloadManager* ui;
110 static DownloadManager* self;
111 };
112
113 class DownloadModel: public QAbstractListModel
114 {
115 friend class DownloadManager;
116 Q_OBJECT
117
118 public:
119 explicit DownloadModel(DownloadManager* downloadManager, QObject* parent = nullptr);
120 QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
121 int rowCount(const QModelIndex& parent = QModelIndex()) const override;
122 bool removeRows(int row, int count, const QModelIndex& parent = QModelIndex()) override;
123
124 private:
125 DownloadManager* m_downloadManager;
126 };
127
128 } // namespace Dialog
129 } // namespace Gui
130
131 #endif // GUI_DIALOG_DOWNLOADMANAGER_H