|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the examples of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "bencodeparser.h" |
|
43 #include "metainfo.h" |
|
44 |
|
45 #include <QDateTime> |
|
46 #include <QMetaType> |
|
47 #include <QString> |
|
48 |
|
49 MetaInfo::MetaInfo() |
|
50 { |
|
51 clear(); |
|
52 } |
|
53 |
|
54 void MetaInfo::clear() |
|
55 { |
|
56 errString = "Unknown error"; |
|
57 content.clear(); |
|
58 infoData.clear(); |
|
59 metaInfoMultiFiles.clear(); |
|
60 metaInfoAnnounce.clear(); |
|
61 metaInfoAnnounceList.clear(); |
|
62 metaInfoCreationDate = QDateTime(); |
|
63 metaInfoComment.clear(); |
|
64 metaInfoCreatedBy.clear(); |
|
65 metaInfoName.clear(); |
|
66 metaInfoPieceLength = 0; |
|
67 metaInfoSha1Sums.clear(); |
|
68 } |
|
69 |
|
70 bool MetaInfo::parse(const QByteArray &data) |
|
71 { |
|
72 clear(); |
|
73 content = data; |
|
74 |
|
75 BencodeParser parser; |
|
76 if (!parser.parse(content)) { |
|
77 errString = parser.errorString(); |
|
78 return false; |
|
79 } |
|
80 |
|
81 infoData = parser.infoSection(); |
|
82 |
|
83 QMap<QByteArray, QVariant> dict = parser.dictionary(); |
|
84 if (!dict.contains("info")) |
|
85 return false; |
|
86 |
|
87 QMap<QByteArray, QVariant> info = qVariantValue<Dictionary>(dict.value("info")); |
|
88 |
|
89 if (info.contains("files")) { |
|
90 metaInfoFileForm = MultiFileForm; |
|
91 |
|
92 QList<QVariant> files = info.value("files").toList(); |
|
93 |
|
94 for (int i = 0; i < files.size(); ++i) { |
|
95 QMap<QByteArray, QVariant> file = qVariantValue<Dictionary>(files.at(i)); |
|
96 QList<QVariant> pathElements = file.value("path").toList(); |
|
97 QByteArray path; |
|
98 foreach (QVariant p, pathElements) { |
|
99 if (!path.isEmpty()) |
|
100 path += "/"; |
|
101 path += p.toByteArray(); |
|
102 } |
|
103 |
|
104 MetaInfoMultiFile multiFile; |
|
105 multiFile.length = file.value("length").toLongLong(); |
|
106 multiFile.path = QString::fromUtf8(path); |
|
107 multiFile.md5sum = file.value("md5sum").toByteArray(); |
|
108 metaInfoMultiFiles << multiFile; |
|
109 } |
|
110 |
|
111 metaInfoName = QString::fromUtf8(info.value("name").toByteArray()); |
|
112 metaInfoPieceLength = info.value("piece length").toInt(); |
|
113 QByteArray pieces = info.value("pieces").toByteArray(); |
|
114 for (int i = 0; i < pieces.size(); i += 20) |
|
115 metaInfoSha1Sums << pieces.mid(i, 20); |
|
116 } else if (info.contains("length")) { |
|
117 metaInfoFileForm = SingleFileForm; |
|
118 metaInfoSingleFile.length = info.value("length").toLongLong(); |
|
119 metaInfoSingleFile.md5sum = info.value("md5sum").toByteArray(); |
|
120 metaInfoSingleFile.name = QString::fromUtf8(info.value("name").toByteArray()); |
|
121 metaInfoSingleFile.pieceLength = info.value("piece length").toInt(); |
|
122 |
|
123 QByteArray pieces = info.value("pieces").toByteArray(); |
|
124 for (int i = 0; i < pieces.size(); i += 20) |
|
125 metaInfoSingleFile.sha1Sums << pieces.mid(i, 20); |
|
126 } |
|
127 |
|
128 metaInfoAnnounce = QString::fromUtf8(dict.value("announce").toByteArray()); |
|
129 |
|
130 if (dict.contains("announce-list")) { |
|
131 // ### unimplemented |
|
132 } |
|
133 |
|
134 if (dict.contains("creation date")) |
|
135 metaInfoCreationDate.setTime_t(dict.value("creation date").toInt()); |
|
136 if (dict.contains("comment")) |
|
137 metaInfoComment = QString::fromUtf8(dict.value("comment").toByteArray()); |
|
138 if (dict.contains("created by")) |
|
139 metaInfoCreatedBy = QString::fromUtf8(dict.value("created by").toByteArray()); |
|
140 |
|
141 return true; |
|
142 } |
|
143 |
|
144 QByteArray MetaInfo::infoValue() const |
|
145 { |
|
146 return infoData; |
|
147 } |
|
148 |
|
149 QString MetaInfo::errorString() const |
|
150 { |
|
151 return errString; |
|
152 } |
|
153 |
|
154 MetaInfo::FileForm MetaInfo::fileForm() const |
|
155 { |
|
156 return metaInfoFileForm; |
|
157 } |
|
158 |
|
159 QString MetaInfo::announceUrl() const |
|
160 { |
|
161 return metaInfoAnnounce; |
|
162 } |
|
163 |
|
164 QStringList MetaInfo::announceList() const |
|
165 { |
|
166 return metaInfoAnnounceList; |
|
167 } |
|
168 |
|
169 QDateTime MetaInfo::creationDate() const |
|
170 { |
|
171 return metaInfoCreationDate; |
|
172 } |
|
173 |
|
174 QString MetaInfo::comment() const |
|
175 { |
|
176 return metaInfoComment; |
|
177 } |
|
178 |
|
179 QString MetaInfo::createdBy() const |
|
180 { |
|
181 return metaInfoCreatedBy; |
|
182 } |
|
183 |
|
184 MetaInfoSingleFile MetaInfo::singleFile() const |
|
185 { |
|
186 return metaInfoSingleFile; |
|
187 } |
|
188 |
|
189 QList<MetaInfoMultiFile> MetaInfo::multiFiles() const |
|
190 { |
|
191 return metaInfoMultiFiles; |
|
192 } |
|
193 |
|
194 QString MetaInfo::name() const |
|
195 { |
|
196 return metaInfoName; |
|
197 } |
|
198 |
|
199 int MetaInfo::pieceLength() const |
|
200 { |
|
201 return metaInfoPieceLength; |
|
202 } |
|
203 |
|
204 QList<QByteArray> MetaInfo::sha1Sums() const |
|
205 { |
|
206 return metaInfoSha1Sums; |
|
207 } |
|
208 |
|
209 qint64 MetaInfo::totalSize() const |
|
210 { |
|
211 if (fileForm() == SingleFileForm) |
|
212 return singleFile().length; |
|
213 |
|
214 qint64 size = 0; |
|
215 foreach (MetaInfoMultiFile file, multiFiles()) |
|
216 size += file.length; |
|
217 return size; |
|
218 } |