1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2005-2010 Ralf Habacker. All rights reserved. |
4 | ** |
5 | ** This file is part of the KDE installer for windows |
6 | ** |
7 | ** This library is free software; you can redistribute it and/or |
8 | ** modify it under the terms of the GNU Library General Public |
9 | ** License version 2 as published by the Free Software Foundation. |
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 GNU |
14 | ** Library General Public License for more details. |
15 | ** |
16 | ** You should have received a copy of the GNU Library General Public License |
17 | ** along with this library; see the file COPYING.LIB. If not, write to |
18 | ** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
19 | ** Boston, MA 02110-1301, USA. |
20 | ** |
21 | ****************************************************************************/ |
22 | #ifndef PACKAGE_H |
23 | #define PACKAGE_H |
24 | |
25 | #include "hash.h" |
26 | #include "misc.h" |
27 | #include "settings.h" |
28 | #include "typehelper.h" |
29 | |
30 | #include <QString> |
31 | #include <QUrl> |
32 | #include <QStringList> |
33 | #include <QHash> |
34 | #include <QtDebug> |
35 | |
36 | class QTextStream; |
37 | class Downloader; |
38 | class Installer; |
39 | class QFile; |
40 | |
41 | typedef QHash<QString, QString> StringHash; |
42 | |
43 | /* This class holds a package with all it's single files |
44 | a2ps-4.13b-1-bin.zip |
45 | a2ps-4.13b-1-dep.zip |
46 | a2ps-4.13b-1-doc.zip |
47 | a2ps-4.13b-1-lib.zip |
48 | a2ps-4.13b-1-src.exe |
49 | a2ps-4.13b-1-src.zip |
50 | */ |
51 | class Package |
52 | { |
53 | public: |
54 | class PackageVersion { |
55 | public: |
56 | /// constructor |
57 | PackageVersion(const QString &version=QString()); |
58 | /// assignment operator |
59 | PackageVersion &operator=(const PackageVersion &other); |
60 | /// equal operator |
61 | bool operator==(const PackageVersion &other) const; |
62 | /// not equal operator |
63 | bool operator!=(const PackageVersion &other) const; |
64 | /// lower than operator |
65 | bool operator<(const PackageVersion &other) const; |
66 | /// lower equal operator |
67 | bool operator<=(const PackageVersion &other) const; |
68 | /// greater than operator |
69 | bool operator>(const PackageVersion &other) const; |
70 | /// greater equal operator |
71 | bool operator>=(const PackageVersion &other) const; |
72 | /// equal string operator |
73 | bool operator==(const QString &other) const; |
74 | /// not equal string operator |
75 | bool operator!=(const QString &other) const; |
76 | /// check if version is empty |
77 | bool isEmpty() const; |
78 | /// return version in string representation |
79 | QString toString() const; |
80 | /// debug operator |
81 | friend QDebug &operator<<(QDebug &, const PackageVersion &); |
82 | protected: |
83 | /** internal compare function, used by operators |
84 | @param other Packageversion instance |
85 | @return -1 this < other, 1 this > other, 0 this = other |
86 | */ |
87 | int compare(const PackageVersion &other) const; |
88 | private: |
89 | QString m_version; |
90 | }; |
91 | |
92 | class PackageItem { |
93 | public: |
94 | PackageItem() { } |
95 | PackageItem(FileTypes::Type type) { m_contentType = type; } |
96 | PackageItem(const QString &type) { setContentType(type); } |
97 | |
98 | bool setContentType(const QString &type); |
99 | FileTypes::Type contentType() const { return m_contentType; } |
100 | |
101 | bool setFileName(const QString &_fileName) |
102 | { |
103 | m_fileName = _fileName; |
104 | return true; |
105 | } |
106 | const QString &fileName() const { return m_fileName; } |
107 | |
108 | bool setUrl(const QUrl &_url) |
109 | { |
110 | if (!_url.isValid()) |
111 | { |
112 | qCritical() << __FUNCTION__ << "invalid url" << _url.toString(); |
113 | return false; |
114 | } |
115 | m_url = _url; |
116 | return true; |
117 | } |
118 | const QUrl &url() const { return m_url; } |
119 | |
120 | bool setUrlAndFileName(const QUrl &url, const QString &fn); |
121 | |
122 | void setCheckSum(const QString &hash) { m_hash = hash; } |
123 | const QString &checkSum() const { return m_hash; } |
124 | |
125 | void setInstalled(bool state) { m_installed = state; } |
126 | bool installed() const { return m_installed; } |
127 | |
128 | PackageVersion version; // package item version |
129 | friend QDebug &operator<<(QDebug &, const Package::PackageItem &); |
130 | |
131 | protected: |
132 | QString m_fileName; // filename only |
133 | FileTypes::Type m_contentType; // BIN / LIB / DOC / SRC |
134 | bool m_installed; // true if already installed |
135 | QString m_hash; /// hash sum |
136 | QUrl m_url; // complete download url |
137 | QT_DEPRECATED bool set(const QUrl &url, const QString &fn, FileTypes::Type contentType=FileTypes::NONE, bool bInstalled = false); |
138 | QT_DEPRECATED bool set(const QUrl &url, const QString &fn, const QByteArray &contentType, bool bInstalled = false); |
139 | |
140 | }; |
141 | public: |
142 | typedef QHash<FileTypes::Type, PackageItem> PackageItemType; |
143 | |
144 | Package(); |
145 | Package(const Package &other); |
146 | Package(const QUrl &file); |
147 | |
148 | // base package related methods |
149 | |
150 | /// return name - the name contains the base name and the compiler |
151 | QString name() const { return m_name; } |
152 | /// set name |
153 | void setName(QString const &name) { m_name = name; } |
154 | |
155 | /// return version |
156 | const PackageVersion &version() const { return m_version.isEmpty() ? m_installedversion : m_version; } |
157 | /// set version |
158 | void setVersion(const QString &version) { m_version = PackageVersion(version); } |
159 | |
160 | /// return installed version |
161 | PackageVersion installedVersion() const { return m_installedversion; } |
162 | /// set version |
163 | void setInstalledVersion(const QString &version) { m_installedversion = PackageVersion(version); } |
164 | void setInstalledVersion(PackageVersion version) { m_installedversion = version; } |
165 | |
166 | /// return notes |
167 | QString notes() const { return m_notes; } |
168 | |
169 | /// return long notes |
170 | void setNotes(const QString ¬es) { m_notes = notes; } |
171 | |
172 | /// return long notes |
173 | QString longNotes() const { return m_longNotes; } |
174 | /// set long notes |
175 | void setLongNotes(const QString ¬es) { m_longNotes = notes; } |
176 | |
177 | /// return home url |
178 | QString homeURL() const { return m_homeUrl; } |
179 | /// return home url |
180 | void setHomeURL(const QString &url) { m_homeUrl = url; } |
181 | |
182 | /// return package categories |
183 | const QStringList &categories() const { return m_categories; } |
184 | /// set package categories |
185 | void addCategories(const QStringList &cat); |
186 | /// set package categories |
187 | void addCategories(const QString &cat); |
188 | |
189 | /// return package dependencies |
190 | const QStringList &deps() const { return m_deps; } |
191 | /// add package dependencies |
192 | void addDeps(const QStringList &addDeps); |
193 | |
194 | QString toString(bool installed=false, const QString &delim = "-" ) const; |
195 | QString getTypeAsString(bool requiredIsInstalled=false, const QString &delim = " " ) const; |
196 | |
197 | /** |
198 | return path relocations - path relocations are used to install files from an archive into |
199 | a different location |
200 | */ |
201 | const StringHash &pathRelocations() const { return m_pathRelocs; } |
202 | /// add path relocations |
203 | void addPathRelocation(const QString &key, const QString &value) { m_pathRelocs[key] = value; } |
204 | |
205 | /// package item related |
206 | /// -> probably better return pointer to packageitem and access its methods |
207 | |
208 | /// return state of a specific item type is available |
209 | bool hasType(FileTypes::Type contentType) const; |
210 | |
211 | /// return modifiable package item |
212 | Package::PackageItem &item(FileTypes::Type contentType); |
213 | |
214 | /// add a package item to this package |
215 | bool add(const PackageItem &item); |
216 | |
217 | /// check if specific content is already installed |
218 | QT_DEPRECATED bool isInstalled(FileTypes::Type type) const; |
219 | /// set that a specific content is already installed |
220 | QT_DEPRECATED void setInstalled(FileTypes::Type type); |
221 | |
222 | /// returns local fileName of package item e.g. xyz-1.2.3-bin.zip |
223 | QString localFileName(FileTypes::Type type) const; |
224 | |
225 | /// return full local path and filename for a package type in the download directory. If bCreateDir is true creates path |
226 | QString localFilePath(FileTypes::Type type, bool bCreateDir = false); |
227 | |
228 | /// returns complete url of package item e.g. http://host/path.../fileName |
229 | QUrl getUrl(FileTypes::Type type) const; |
230 | /// set url of package item e.g. http://host/path.../fileName |
231 | bool setUrl(FileTypes::Type type, const QUrl &url); |
232 | |
233 | /// download a package item |
234 | bool downloadItem(FileTypes::Type type); |
235 | /// install a package item |
236 | bool installItem(Installer *installer, FileTypes::Type type); |
237 | /// uninstall a package item |
238 | bool removeItem(Installer *installer, FileTypes::Type type); |
239 | |
240 | /// set Install state of a package type (e.g. from gnuwin32 manifests) |
241 | QT_DEPRECATED void setInstalled(const Package &other); |
242 | |
243 | Hash &hashType() { return m_hashType; } |
244 | |
245 | /// save package to stream |
246 | bool write(QTextStream &out) const; |
247 | /// load package from stream |
248 | bool read(QTextStream &in); |
249 | |
250 | /// dump package content |
251 | QT_DEPRECATED void dump(const QString &title=QString()) const; |
252 | |
253 | bool handled() const { return m_handled; } |
254 | void setHandled(bool state) { m_handled = state; } |
255 | |
256 | /// return last error in cleartext form |
257 | const QString &error() const { return m_lastError; } |
258 | |
259 | /// generate manifest file name |
260 | QString manifestFileName(const FileTypes::Type type) const; |
261 | |
262 | /// generate version file name |
263 | QString versionFileName(const FileTypes::Type type) const; |
264 | |
265 | /// user specific data pointer (currently used for ExternalInstallerControl) |
266 | bool setUserData(int index,void *data) |
267 | { |
268 | if (index >= 0 && index <= 1) |
269 | { |
270 | m_userData[index] = data; |
271 | return true; |
272 | } |
273 | return false; |
274 | } |
275 | |
276 | /// return user specific data pointer (currently used for ExternalInstallerControl) |
277 | void *userData(int index) |
278 | { |
279 | if (index >= 0 && index <= 1) |
280 | return m_userData[index]; |
281 | return 0; |
282 | } |
283 | |
284 | protected: |
285 | bool setError(const QString &text) { m_lastError = text; qCritical() << text; return false; } |
286 | |
287 | PackageItemType m_packages; |
288 | QString m_name; // base name (a2ps) |
289 | PackageVersion m_version; // base version (4.13b-1) |
290 | PackageVersion m_installedversion; |
291 | QString m_notes; // notes from package.notes |
292 | QString m_homeUrl; // home url of package |
293 | QString m_longNotes;// notes from package.notes |
294 | QStringList m_categories; |
295 | QStringList m_deps; |
296 | StringHash m_pathRelocs; |
297 | bool m_handled; // marker for several operations |
298 | Hash m_hashType; // contains the hash type for this package |
299 | QString m_lastError; |
300 | void *m_userData[2]; |
301 | static QString m_packageRoot; |
302 | friend QDebug &operator<<(QDebug &, const Package &); |
303 | }; |
304 | QDebug &operator<<(QDebug &, const FileTypes::Type); |
305 | QDebug &operator<<(QDebug &, const Package::PackageItem &); |
306 | QDebug &operator<<(QDebug &, const Package &); |
307 | QDebug &operator<<(QDebug &, const QList<Package*> &); |
308 | |
309 | |
310 | #endif |
311 | |