src/download.h

changeset 667
31540c1f22ea
parent 606
3dd6f343ec06
equal deleted inserted replaced
666:c595cfb4791c 667:31540c1f22ea
1 /* 1 /*
2 * LDForge: LDraw parts authoring CAD 2 * LDForge: LDraw parts authoring CAD
3 * Copyright (C) 2013 Santeri Piippo 3 * Copyright (C) 2013, 2014 Santeri Piippo
4 * 4 *
5 * This program is free software: you can redistribute it and/or modify 5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by 6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or 7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version. 8 * (at your option) any later version.
18 18
19 #ifndef LDFORGE_DOWNLOAD_H 19 #ifndef LDFORGE_DOWNLOAD_H
20 #define LDFORGE_DOWNLOAD_H 20 #define LDFORGE_DOWNLOAD_H
21 21
22 #include <QDialog> 22 #include <QDialog>
23 #include "common.h" 23 #include "main.h"
24 #include "types.h" 24 #include "types.h"
25 25
26 class LDFile; 26 class LDDocument;
27 class QFile; 27 class QFile;
28 class PartDownloadRequest; 28 class PartDownloadRequest;
29 class Ui_DownloadFrom; 29 class Ui_DownloadFrom;
30 class QNetworkAccessManager; 30 class QNetworkAccessManager;
31 class QNetworkRequest; 31 class QNetworkRequest;
33 class QAbstractButton; 33 class QAbstractButton;
34 34
35 // ============================================================================= 35 // =============================================================================
36 // ----------------------------------------------------------------------------- 36 // -----------------------------------------------------------------------------
37 class PartDownloader : public QDialog 37 class PartDownloader : public QDialog
38 { Q_OBJECT 38 {
39 PROPERTY (LDFile*, primaryFile, setPrimaryFile) 39 typedefs:
40 PROPERTY (bool, aborted, setAborted)
41
42 public:
43 constexpr static const char* k_UnofficialURL = "http://ldraw.org/library/unofficial/";
44
45 enum Source 40 enum Source
46 { PartsTracker, 41 {
42 PartsTracker,
47 CustomURL, 43 CustomURL,
48 }; 44 };
49 45
50 enum Button 46 enum Button
51 { Download, 47 {
48 Download,
52 Abort, 49 Abort,
53 Close 50 Close
54 }; 51 };
55 52
56 enum TableColumn 53 enum TableColumn
57 { PartLabelColumn, 54 {
55 PartLabelColumn,
58 ProgressColumn, 56 ProgressColumn,
59 }; 57 };
60 58
61 explicit PartDownloader (QWidget* parent = null); 59 using RequestList = QList<PartDownloadRequest*>;
62 virtual ~PartDownloader(); 60
63 str getURL() const; 61 properties:
64 static str getDownloadPath(); 62 Q_OBJECT
65 Source getSource() const; 63 PROPERTY (public, LDDocument*, PrimaryFile, NO_OPS, STOCK_WRITE)
66 void downloadFile (str dest, str url, bool primary); 64 PROPERTY (public, bool, Aborted, BOOL_OPS, STOCK_WRITE)
67 void modifyDest (str& dest) const; 65 PROPERTY (private, Ui_DownloadFrom*, Interface, NO_OPS, STOCK_WRITE)
68 QPushButton* getButton (Button i); 66 PROPERTY (private, QStringList, FilesToDownload, LIST_OPS, STOCK_WRITE)
69 static void k_download(); 67 PROPERTY (private, RequestList, Requests, LIST_OPS, STOCK_WRITE)
68 PROPERTY (private, QPushButton*, DownloadButton, NO_OPS, STOCK_WRITE)
69
70 public:
71 explicit PartDownloader (QWidget* parent = null);
72 virtual ~PartDownloader();
73
74 void downloadFile (QString dest, QString url, bool primary);
75 QPushButton* getButton (Button i);
76 QString getURL() const;
77 Source getSource() const;
78 void modifyDestination (QString& dest) const;
79
80 static QString getDownloadPath();
81 static void staticBegin();
70 82
71 public slots: 83 public slots:
72 void sourceChanged (int i); 84 void buttonClicked (QAbstractButton* btn);
73 void checkIfFinished(); 85 void checkIfFinished();
74 void buttonClicked (QAbstractButton* btn); 86 void sourceChanged (int i);
75
76 protected:
77 Ui_DownloadFrom* ui;
78 friend class PartDownloadRequest;
79
80 private:
81 QList<str> m_filesToDownload;
82 QList<PartDownloadRequest*> m_requests;
83 QPushButton* m_downloadButton;
84 }; 87 };
85 88
86 // ============================================================================= 89 // =============================================================================
87 // ----------------------------------------------------------------------------- 90 // -----------------------------------------------------------------------------
88 class PartDownloadRequest : public QObject 91 class PartDownloadRequest : public QObject
89 { Q_OBJECT 92 {
90 PROPERTY (int, tableRow, setTableRow) 93 typedefs:
94 enum EState
95 {
96 ERequesting,
97 EDownloading,
98 EFinished,
99 EFailed,
100 };
101
102 properties:
103 Q_OBJECT
104 PROPERTY (public, int, TableRow, NUM_OPS, STOCK_WRITE)
105 PROPERTY (private, EState, State, NO_OPS, STOCK_WRITE)
106 PROPERTY (private, PartDownloader*, Prompt, NO_OPS, STOCK_WRITE)
107 PROPERTY (private, QString, URL, STR_OPS, STOCK_WRITE)
108 PROPERTY (private, QString, Destinaton, STR_OPS, STOCK_WRITE)
109 PROPERTY (private, QString, FilePath, STR_OPS, STOCK_WRITE)
110 PROPERTY (private, QNetworkAccessManager*, NAM, NO_OPS, STOCK_WRITE)
111 PROPERTY (private, QNetworkReply*, Reply, NO_OPS, STOCK_WRITE)
112 PROPERTY (private, bool, FirstUpdate, BOOL_OPS, STOCK_WRITE)
113 PROPERTY (private, int64, BytesRead, NUM_OPS, STOCK_WRITE)
114 PROPERTY (private, int64, BytesTotal, NUM_OPS, STOCK_WRITE)
115 PROPERTY (private, bool, Primary, BOOL_OPS, STOCK_WRITE)
116 PROPERTY (private, QFile*, FilePointer, NO_OPS, STOCK_WRITE)
91 117
92 public: 118 public:
93 enum State 119 explicit PartDownloadRequest (QString url, QString dest, bool primary, PartDownloader* parent);
94 { Requesting,
95 Downloading,
96 Finished,
97 Failed,
98 };
99
100 explicit PartDownloadRequest (str url, str dest, bool primary, PartDownloader* parent);
101 PartDownloadRequest (const PartDownloadRequest&) = delete; 120 PartDownloadRequest (const PartDownloadRequest&) = delete;
102 virtual ~PartDownloadRequest(); 121 virtual ~PartDownloadRequest();
103 void updateToTable(); 122 void updateToTable();
104 bool isFinished() const; 123 bool isFinished() const;
105 const State& state() const;
106 124
107 void operator= (const PartDownloadRequest&) = delete; 125 void operator= (const PartDownloadRequest&) = delete;
108 126
109 public slots: 127 public slots:
110 void downloadFinished(); 128 void downloadFinished();
111 void readyRead(); 129 void readyRead();
112 void downloadProgress (qint64 recv, qint64 total); 130 void downloadProgress (qint64 recv, qint64 total);
113 void abort(); 131 void abort();
114
115 private:
116 PartDownloader* m_prompt;
117 str m_url, m_dest, m_fpath;
118 QNetworkAccessManager* m_nam;
119 QNetworkReply* m_reply;
120 bool m_firstUpdate;
121 State m_state;
122 int64 m_bytesRead, m_bytesTotal;
123 bool m_primary;
124 QFile* m_fp;
125 }; 132 };
126 133
127 #endif // LDFORGE_DOWNLOAD_H 134 #endif // LDFORGE_DOWNLOAD_H

mercurial