| 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 |
#ifndef GUI_DIALOG_DOWNLOADITEM_H |
| 24 |
#define GUI_DIALOG_DOWNLOADITEM_H |
| 25 |
|
| 26 |
#include <QBasicTimer> |
| 27 |
#include <QElapsedTimer> |
| 28 |
#include <QFile> |
| 29 |
#include <QLabel> |
| 30 |
#include <QNetworkAccessManager> |
| 31 |
#include <QNetworkReply> |
| 32 |
#include <QTableView> |
| 33 |
#include <QTime> |
| 34 |
#include <QUrl> |
| 35 |
|
| 36 |
|
| 37 |
class AutoSaver; |
| 38 |
class QFileIconProvider; |
| 39 |
|
| 40 |
class EditTableView: public QTableView |
| 41 |
{ |
| 42 |
Q_OBJECT |
| 43 |
|
| 44 |
public: |
| 45 |
explicit EditTableView(QWidget* parent = nullptr); |
| 46 |
void keyPressEvent(QKeyEvent* event) override; |
| 47 |
|
| 48 |
public Q_SLOTS: |
| 49 |
void removeOne(); |
| 50 |
void removeAll(); |
| 51 |
}; |
| 52 |
|
| 53 |
class SqueezeLabel: public QLabel |
| 54 |
{ |
| 55 |
Q_OBJECT |
| 56 |
|
| 57 |
public: |
| 58 |
explicit SqueezeLabel(QWidget* parent = nullptr); |
| 59 |
|
| 60 |
protected: |
| 61 |
void paintEvent(QPaintEvent* event) override; |
| 62 |
}; |
| 63 |
|
| 64 |
/* |
| 65 |
This class will call the save() slot on the parent object when the parent changes. |
| 66 |
It will wait several seconds after changed() to combining multiple changes and |
| 67 |
prevent continuous writing to disk. |
| 68 |
*/ |
| 69 |
class AutoSaver: public QObject |
| 70 |
{ |
| 71 |
|
| 72 |
Q_OBJECT |
| 73 |
|
| 74 |
public: |
| 75 |
explicit AutoSaver(QObject* parent); |
| 76 |
~AutoSaver() override; |
| 77 |
void saveIfNecessary(); |
| 78 |
|
| 79 |
public Q_SLOTS: |
| 80 |
void changeOccurred(); |
| 81 |
|
| 82 |
protected: |
| 83 |
void timerEvent(QTimerEvent* event) override; |
| 84 |
|
| 85 |
private: |
| 86 |
QBasicTimer m_timer; |
| 87 |
QElapsedTimer m_firstChange; |
| 88 |
}; |
| 89 |
|
| 90 |
class NetworkAccessManager: public QNetworkAccessManager |
| 91 |
{ |
| 92 |
Q_OBJECT |
| 93 |
|
| 94 |
public: |
| 95 |
explicit NetworkAccessManager(QObject* parent = nullptr); |
| 96 |
|
| 97 |
private Q_SLOTS: |
| 98 |
void authenticationRequired(QNetworkReply* reply, QAuthenticator* auth); // clazy:exclude=overridden-signal |
| 99 |
void proxyAuthenticationRequired( |
| 100 |
const QNetworkProxy& proxy, |
| 101 |
QAuthenticator* auth |
| 102 |
); // clazy:exclude=overridden-signal |
| 103 |
}; |
| 104 |
|
| 105 |
#include "ui_DownloadItem.h" |
| 106 |
|
| 107 |
namespace Gui |
| 108 |
{ |
| 109 |
namespace Dialog |
| 110 |
{ |
| 111 |
class DownloadModel; |
| 112 |
|
| 113 |
class DownloadItem: public QWidget, public Ui_DownloadItem |
| 114 |
{ |
| 115 |
Q_OBJECT |
| 116 |
|
| 117 |
Q_SIGNALS: |
| 118 |
void statusChanged(); |
| 119 |
|
| 120 |
public: |
| 121 |
explicit DownloadItem( |
| 122 |
QNetworkReply* reply = nullptr, |
| 123 |
bool requestFileName = false, |
| 124 |
QWidget* parent = nullptr |
| 125 |
); |
| 126 |
bool downloading() const; |
| 127 |
bool downloadedSuccessfully() const; |
| 128 |
|
| 129 |
QUrl m_url; |
| 130 |
QString m_fileName; |
| 131 |
|
| 132 |
QFile m_output; |
| 133 |
QNetworkReply* m_reply; |
| 134 |
|
| 135 |
private Q_SLOTS: |
| 136 |
void stop(); |
| 137 |
void tryAgain(); |
| 138 |
void open(); |
| 139 |
void openFolder(); |
| 140 |
|
| 141 |
void downloadReadyRead(); |
| 142 |
void error(QNetworkReply::NetworkError code); |
| 143 |
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal); |
| 144 |
void metaDataChanged(); |
| 145 |
void finished(); |
| 146 |
|
| 147 |
private: |
| 148 |
void contextMenuEvent(QContextMenuEvent*) override; |
| 149 |
void getFileName(); |
| 150 |
void init(); |
| 151 |
void updateInfoLabel(); |
| 152 |
QString dataString(int size) const; |
| 153 |
QString getDownloadDirectory() const; |
| 154 |
QString saveFileName(const QString& directory) const; |
| 155 |
|
| 156 |
bool m_requestFileName; |
| 157 |
qint64 m_bytesReceived; |
| 158 |
QElapsedTimer m_downloadTime; |
| 159 |
}; |
| 160 |
|
| 161 |
} // namespace Dialog |
| 162 |
} // namespace Gui |
| 163 |
|
| 164 |
#endif // GUI_DIALOG_DOWNLOADITEM_H |