TorrentParser.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2006-2007 by Peter Dimov.
00004 
00005 This file is part of Calitko (http://www.calitko.org).
00006 
00007 Calitko is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2 of the License, or
00010 (at your option) any later version.
00011 
00012 Calitko is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Calitko; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021 */
00022 
00023 #ifndef PROTOCOLS__BIT_TORRENT__TORRENTS__TORRENT_PARSER_H
00024 #define PROTOCOLS__BIT_TORRENT__TORRENTS__TORRENT_PARSER_H
00025 
00026 #include "Torrent.h"
00027 #include "Imports.h"
00028 
00029 namespace Protocols {
00030 namespace BitTorrent {
00031 namespace Torrents {
00032 
00034 
00061 class TorrentParser
00062 {
00063     STATIC_HELPER (TorrentParser)
00064 
00065 public:
00066     static bool         parseAndLoadTorrent (const QByteArray &rawTorrentData,
00067                                              Torrent &torrent);
00068 
00069 private:
00070     static bool         loadAllTorrentData (const QByteArray &rawData,
00071                                             Torrent &torrent);
00072     static bool         loadTorrentBasicData (const BDictionary *bBasicData,
00073                                               Torrent &torrent);
00074     static bool         loadTorrentFilesInfo (const BDictionary *bInfo,
00075                                               Torrent &torrent);
00076     static bool         getAndLoadAnnounce (const BDictionary *bBasicData,
00077                                             Torrent &torrent);
00078     static bool         getAndLoadAnnounceList (const BDictionary *bBasicData,
00079                                                 Torrent &torrent);
00080     static bool         getAndLoadCreationDate (const BDictionary *bBasicData,
00081                                                 Torrent &torrent);
00082     static bool         getAndLoadComment (const BDictionary *bBasicData,
00083                                            Torrent &torrent);
00084     static bool         getAndLoadCreatedBy (const BDictionary *bBasicData,
00085                                              Torrent &torrent);
00086     static bool         getAndLoadPieceLength (const BDictionary *bInfo,
00087                                                Torrent &torrent);
00088     static bool         getAndLoadPieces (const BDictionary *bInfo,
00089                                           Torrent &torrent);
00090     static bool         getAndLoadPrivate (const BDictionary *bInfo,
00091                                            Torrent &torrent);
00092     static bool         getAndLoadSingleModeFileInfo (const BDictionary *bInfo,
00093                                                       Torrent &torrent);
00094     static bool         getAndLoadMultipleModeFileInfo (const BDictionary *bInfo,
00095                                                         Torrent &torrent);
00096     static bool         getAndLoadDirectoryName (const BDictionary *bFileInfo,
00097                                             QString &directoryName);
00098     static bool         getAndLoadFileName (const BDictionary *bFileInfo,
00099                                             QString &fileName);
00100     static bool         getAndLoadFilePath (const BDictionary *bFileInfo,
00101                                             QString &filePath);
00102     static bool         getAndLoadFileLength (const BDictionary *bFileInfo,
00103                                               Torrent::FileInfo &fileInfo);
00104     static bool         getAndLoadFileChecksum (const BDictionary *bFileInfo,
00105                                                 Torrent::FileInfo &fileInfo);
00106     static bool         getAndLoadFileInfoList (const BList *bFileInfoList,
00107                                             Torrent::FileInfoList &fileInfoList);
00108     static bool         getAndLoadFileInfo (const BDictionary *bFileInfo,
00109                                             Torrent::FileInfo &fileInfo);
00110 
00111     static bool         isValidAnnounceUrl (const Uri &announceUrl);
00112     static bool         isValidCreationDate (int creationDate);
00113     static bool         isValidPieceLength (int pieceLength);
00114     static bool         isValidPieces (const QByteArray &pieces);
00115     static bool         isValidPrivate (int privateFlag);
00116     static bool         isValidFileLength (qint64 fileLength);
00117     static bool         isValidFileChecksum (const QByteArray &fileChecksum);
00118 
00119     static bool         convertBTierToTier (const BList *bTier,
00120                                             QList <Uri> &tier);
00121     static bool         convertPiecesToPieceList (const QByteArray &pieces,
00122                                                 Torrent::PieceList &pieceList);
00123     static bool         convertBFilePathToFilePath (const BList *bFilePath,
00124                                                     const QString &delimiter,
00125                                                     QString &filePath);
00126     static bool         convertPrivateFlagToBool (int privateFlag);
00127 
00128     static void         appendDirectoryName (const QString &directoryName,
00129                                              const QString &delimiter,
00130                                              Torrent::FileInfoList &fileInfoList);
00131 
00132     // The content of a metainfo file keys
00133     static const char *InfoKeyName;
00134     static const char *AnnounceKeyName;
00135     static const char *AnnounceListKeyName;
00136     static const char *CreationDateKeyName;
00137     static const char *CommentKeyName;
00138     static const char *CreatedByKeyName;
00139     static const char *PieceLengthKeyName;
00140     static const char *PiecesKeyName;
00141     static const char *PrivateKeyName;
00142     static const char *FileNameKeyName;
00143     static const char *FileLengthKeyName;
00144     static const char *ChecksumKeyName;
00145     static const char *FilesKeyName;
00146     static const char *PathKeyName;
00147 
00148     static const char *DirectoryDelimiter;  
00149 };
00150 
00151 } // namespace Torrents
00152 } // namespace BitTorrent
00153 } // namespace Protocols
00154 
00155 #endif // PROTOCOLS__BIT_TORRENT__TORRENTS__TORRENT_PARSER_H