author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 06 Jul 2010 15:10:48 +0300 | |
changeset 30 | 5dc02b23752f |
parent 18 | 2f34d5167611 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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 |
** |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
9 |
** $QT_BEGIN_LICENSE:BSD$ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
10 |
** You may use this file under the terms of the BSD license as follows: |
0 | 11 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
12 |
** "Redistribution and use in source and binary forms, with or without |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
13 |
** modification, are permitted provided that the following conditions are |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
14 |
** met: |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
15 |
** * Redistributions of source code must retain the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
16 |
** notice, this list of conditions and the following disclaimer. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
17 |
** * Redistributions in binary form must reproduce the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
18 |
** notice, this list of conditions and the following disclaimer in |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
19 |
** the documentation and/or other materials provided with the |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
20 |
** distribution. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
21 |
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
22 |
** the names of its contributors may be used to endorse or promote |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
23 |
** products derived from this software without specific prior written |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
24 |
** permission. |
0 | 25 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
26 |
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
27 |
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
28 |
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
29 |
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
30 |
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
31 |
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
32 |
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
33 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
34 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
35 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
36 |
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
0 | 37 |
** $QT_END_LICENSE$ |
38 |
** |
|
39 |
****************************************************************************/ |
|
40 |
||
41 |
#include "bencodeparser.h" |
|
42 |
#include "metainfo.h" |
|
43 |
||
44 |
#include <QDateTime> |
|
45 |
#include <QMetaType> |
|
46 |
#include <QString> |
|
47 |
||
48 |
MetaInfo::MetaInfo() |
|
49 |
{ |
|
50 |
clear(); |
|
51 |
} |
|
52 |
||
53 |
void MetaInfo::clear() |
|
54 |
{ |
|
55 |
errString = "Unknown error"; |
|
56 |
content.clear(); |
|
57 |
infoData.clear(); |
|
58 |
metaInfoMultiFiles.clear(); |
|
59 |
metaInfoAnnounce.clear(); |
|
60 |
metaInfoAnnounceList.clear(); |
|
61 |
metaInfoCreationDate = QDateTime(); |
|
62 |
metaInfoComment.clear(); |
|
63 |
metaInfoCreatedBy.clear(); |
|
64 |
metaInfoName.clear(); |
|
65 |
metaInfoPieceLength = 0; |
|
66 |
metaInfoSha1Sums.clear(); |
|
67 |
} |
|
68 |
||
69 |
bool MetaInfo::parse(const QByteArray &data) |
|
70 |
{ |
|
71 |
clear(); |
|
72 |
content = data; |
|
73 |
||
74 |
BencodeParser parser; |
|
75 |
if (!parser.parse(content)) { |
|
76 |
errString = parser.errorString(); |
|
77 |
return false; |
|
78 |
} |
|
79 |
||
80 |
infoData = parser.infoSection(); |
|
81 |
||
82 |
QMap<QByteArray, QVariant> dict = parser.dictionary(); |
|
83 |
if (!dict.contains("info")) |
|
84 |
return false; |
|
85 |
||
86 |
QMap<QByteArray, QVariant> info = qVariantValue<Dictionary>(dict.value("info")); |
|
87 |
||
88 |
if (info.contains("files")) { |
|
89 |
metaInfoFileForm = MultiFileForm; |
|
90 |
||
91 |
QList<QVariant> files = info.value("files").toList(); |
|
92 |
||
93 |
for (int i = 0; i < files.size(); ++i) { |
|
94 |
QMap<QByteArray, QVariant> file = qVariantValue<Dictionary>(files.at(i)); |
|
95 |
QList<QVariant> pathElements = file.value("path").toList(); |
|
96 |
QByteArray path; |
|
97 |
foreach (QVariant p, pathElements) { |
|
98 |
if (!path.isEmpty()) |
|
99 |
path += "/"; |
|
100 |
path += p.toByteArray(); |
|
101 |
} |
|
102 |
||
103 |
MetaInfoMultiFile multiFile; |
|
104 |
multiFile.length = file.value("length").toLongLong(); |
|
105 |
multiFile.path = QString::fromUtf8(path); |
|
106 |
multiFile.md5sum = file.value("md5sum").toByteArray(); |
|
107 |
metaInfoMultiFiles << multiFile; |
|
108 |
} |
|
109 |
||
110 |
metaInfoName = QString::fromUtf8(info.value("name").toByteArray()); |
|
111 |
metaInfoPieceLength = info.value("piece length").toInt(); |
|
112 |
QByteArray pieces = info.value("pieces").toByteArray(); |
|
113 |
for (int i = 0; i < pieces.size(); i += 20) |
|
114 |
metaInfoSha1Sums << pieces.mid(i, 20); |
|
115 |
} else if (info.contains("length")) { |
|
116 |
metaInfoFileForm = SingleFileForm; |
|
117 |
metaInfoSingleFile.length = info.value("length").toLongLong(); |
|
118 |
metaInfoSingleFile.md5sum = info.value("md5sum").toByteArray(); |
|
119 |
metaInfoSingleFile.name = QString::fromUtf8(info.value("name").toByteArray()); |
|
120 |
metaInfoSingleFile.pieceLength = info.value("piece length").toInt(); |
|
121 |
||
122 |
QByteArray pieces = info.value("pieces").toByteArray(); |
|
123 |
for (int i = 0; i < pieces.size(); i += 20) |
|
124 |
metaInfoSingleFile.sha1Sums << pieces.mid(i, 20); |
|
125 |
} |
|
126 |
||
127 |
metaInfoAnnounce = QString::fromUtf8(dict.value("announce").toByteArray()); |
|
128 |
||
129 |
if (dict.contains("announce-list")) { |
|
130 |
// ### unimplemented |
|
131 |
} |
|
132 |
||
133 |
if (dict.contains("creation date")) |
|
134 |
metaInfoCreationDate.setTime_t(dict.value("creation date").toInt()); |
|
135 |
if (dict.contains("comment")) |
|
136 |
metaInfoComment = QString::fromUtf8(dict.value("comment").toByteArray()); |
|
137 |
if (dict.contains("created by")) |
|
138 |
metaInfoCreatedBy = QString::fromUtf8(dict.value("created by").toByteArray()); |
|
139 |
||
140 |
return true; |
|
141 |
} |
|
142 |
||
143 |
QByteArray MetaInfo::infoValue() const |
|
144 |
{ |
|
145 |
return infoData; |
|
146 |
} |
|
147 |
||
148 |
QString MetaInfo::errorString() const |
|
149 |
{ |
|
150 |
return errString; |
|
151 |
} |
|
152 |
||
153 |
MetaInfo::FileForm MetaInfo::fileForm() const |
|
154 |
{ |
|
155 |
return metaInfoFileForm; |
|
156 |
} |
|
157 |
||
158 |
QString MetaInfo::announceUrl() const |
|
159 |
{ |
|
160 |
return metaInfoAnnounce; |
|
161 |
} |
|
162 |
||
163 |
QStringList MetaInfo::announceList() const |
|
164 |
{ |
|
165 |
return metaInfoAnnounceList; |
|
166 |
} |
|
167 |
||
168 |
QDateTime MetaInfo::creationDate() const |
|
169 |
{ |
|
170 |
return metaInfoCreationDate; |
|
171 |
} |
|
172 |
||
173 |
QString MetaInfo::comment() const |
|
174 |
{ |
|
175 |
return metaInfoComment; |
|
176 |
} |
|
177 |
||
178 |
QString MetaInfo::createdBy() const |
|
179 |
{ |
|
180 |
return metaInfoCreatedBy; |
|
181 |
} |
|
182 |
||
183 |
MetaInfoSingleFile MetaInfo::singleFile() const |
|
184 |
{ |
|
185 |
return metaInfoSingleFile; |
|
186 |
} |
|
187 |
||
188 |
QList<MetaInfoMultiFile> MetaInfo::multiFiles() const |
|
189 |
{ |
|
190 |
return metaInfoMultiFiles; |
|
191 |
} |
|
192 |
||
193 |
QString MetaInfo::name() const |
|
194 |
{ |
|
195 |
return metaInfoName; |
|
196 |
} |
|
197 |
||
198 |
int MetaInfo::pieceLength() const |
|
199 |
{ |
|
200 |
return metaInfoPieceLength; |
|
201 |
} |
|
202 |
||
203 |
QList<QByteArray> MetaInfo::sha1Sums() const |
|
204 |
{ |
|
205 |
return metaInfoSha1Sums; |
|
206 |
} |
|
207 |
||
208 |
qint64 MetaInfo::totalSize() const |
|
209 |
{ |
|
210 |
if (fileForm() == SingleFileForm) |
|
211 |
return singleFile().length; |
|
212 |
||
213 |
qint64 size = 0; |
|
214 |
foreach (MetaInfoMultiFile file, multiFiles()) |
|
215 |
size += file.length; |
|
216 |
return size; |
|
217 |
} |