Stellarium 0.12.4
MultiLevelJsonBase.hpp
1 /*
2  * Copyright (C) 2008 Fabien Chereau
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
17  */
18 
19 #ifndef _MULTILEVELJSONBASE_HPP_
20 #define _MULTILEVELJSONBASE_HPP_
21 
22 #include <QList>
23 #include <QString>
24 #include <QVariantMap>
25 #include <QNetworkReply>
26 
27 #include "StelSkyLayer.hpp"
28 
29 class QIODevice;
30 class StelCore;
31 
35 {
36  Q_OBJECT
37 
38  friend class JsonLoadThread;
39 
40 public:
43 
46  void initFromUrl(const QString& url);
47 
50  void initFromQVariantMap(const QVariantMap& map);
51 
54 
56  QString getShortName() const {return shortName;}
57 
59  bool hasErrorOccured() const {return errorOccured;}
60 
62  int getLevel() const {return parent()==NULL ? 0 : (qobject_cast<MultiLevelJsonBase*>(parent()))->getLevel()+1;}
63 
66  QVariantMap toQVariantMap() const;
67 
71 
72 private slots:
74  void downloadFinished();
76  void jsonLoadFinished();
77 
78 protected:
81  virtual void loadFromQVariantMap(const QVariantMap& map)=0;
82 
84  bool isDeletionScheduled() const {return timeWhenDeletionScheduled>0.;}
85 
87  QString shortName;
88 
90  QString baseUrl;
91 
93  QString contructorUrl;
94 
96  QList<MultiLevelJsonBase*> subTiles;
97 
100 
101  void updatePercent(int tot, int numToBeLoaded);
102 
104  void deleteUnusedSubTiles();
105 
108 
110  void cancelDeletion();
111 
113  static QVariantMap loadFromJSON(QIODevice& input, bool qZcompressed=false, bool gzCompressed=false);
114 
115 private:
117  QString getBaseUrl() const {return baseUrl;}
118 
119  // Used to download remote JSON files if needed
120  class QNetworkReply* httpReply;
121 
122  // The delay after which a scheduled deletion will occur
123  float deletionDelay;
124 
125  class JsonLoadThread* loadThread;
126 
127  // Time at which deletion was first scheduled
128  double timeWhenDeletionScheduled;
129 
130  // The temporary map filled in a thread
131  QVariantMap temporaryResultMap;
132 
133  bool loadingState;
134  int lastPercent;
135 
137  static class QNetworkAccessManager* networkAccessManager;
138 
139  static QNetworkAccessManager& getNetworkAccessManager();
140 };
141 
142 #endif // _MULTILEVELJSONBASE_HPP_