TrackerRequestUrlCreator.cpp

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 #include "Qt.h"
00024 #include "TrackerRequestUrlCreator.h"
00025 #include "Imports.cpp"
00026 
00028 
00053 Uri TrackerRequestUrlCreator::createRequestUrl (const TrackerRequest &trackerRequest)
00054 {
00055     Uri::QueryItemList queryItems;
00056 
00057     queryItems += Uri::QueryItem (ParameterInfoHashName,
00058         trackerRequest.infoHash().toQByteArray());
00059 
00060     queryItems += Uri::QueryItem (ParameterPeerIdName,
00061         trackerRequest.peerInfo().peerId().toQByteArray());
00062 
00063     queryItems += Uri::QueryItem (ParameterPortName,
00064         QByteArray::number (trackerRequest.peerInfo().port()));
00065 
00066     queryItems += Uri::QueryItem (ParameterUploadedName,
00067         QByteArray::number (trackerRequest.uploaded()));
00068 
00069     queryItems += Uri::QueryItem (ParameterDownloadedName,
00070         QByteArray::number (trackerRequest.downloaded()));
00071 
00072     queryItems += Uri::QueryItem (ParameterLeftName,
00073         QByteArray::number (trackerRequest.left()));
00074 
00075     queryItems += Uri::QueryItem (ParameterCompactName,
00076         trackerRequest.compact() ? CompactTrueValueString :
00077             CompactFalseValueString);
00078 
00079     queryItems += Uri::QueryItem (ParameterEventName,
00080         eventToString (trackerRequest.event()));
00081 
00082     const QByteArray host = trackerRequest.peerInfo().host();
00083     if (host != PeerInfo().host())
00084         queryItems += Uri::QueryItem (ParameterIpName, host);
00085 
00086     queryItems += Uri::QueryItem (ParameterNumWantName,
00087         QByteArray::number (trackerRequest.numWant()));
00088 
00089     const QByteArray key = trackerRequest.key();
00090     if (!key.isEmpty())
00091         queryItems += Uri::QueryItem (ParameterKeyName, key);
00092 
00093     const QByteArray trackerId = trackerRequest.trackerId();
00094     if (!trackerId.isEmpty())
00095         queryItems += Uri::QueryItem (ParameterTrackerIdName, trackerId);
00096 
00097     Uri requestUrl (trackerRequest.announceUrl());
00098     requestUrl.appendQueryItems (queryItems);
00099     return requestUrl;
00100 }
00101 
00103 
00108 QByteArray TrackerRequestUrlCreator::eventToString (TrackerRequest::Event event)
00109 {
00110     switch (event)
00111     {
00112     case TrackerRequest::Empty:
00113         return "empty";
00114         break;
00115     case TrackerRequest::Started:
00116         return "started";
00117         break;
00118     case TrackerRequest::Completed:
00119         return "completed";
00120         break;
00121     case TrackerRequest::Stopped:
00122         return "stopped";
00123         break;
00124     default:
00125         Q_ASSERT (false); // Unhandled Event value!
00126         break;
00127     }
00128 
00129     return "unknown";
00130 }
00131 
00132 // Static constants definitions
00133 const char *TrackerRequestUrlCreator::ParameterInfoHashName     = "info_hash";
00134 const char *TrackerRequestUrlCreator::ParameterPeerIdName       = "peer_id";
00135 const char *TrackerRequestUrlCreator::ParameterPortName         = "port";
00136 const char *TrackerRequestUrlCreator::ParameterUploadedName     = "uploaded";
00137 const char *TrackerRequestUrlCreator::ParameterDownloadedName   = "downloaded";
00138 const char *TrackerRequestUrlCreator::ParameterLeftName         = "left";
00139 const char *TrackerRequestUrlCreator::ParameterCompactName      = "compact";
00140 const char *TrackerRequestUrlCreator::ParameterEventName        = "event";
00141 const char *TrackerRequestUrlCreator::ParameterIpName           = "ip";
00142 const char *TrackerRequestUrlCreator::ParameterNumWantName      = "numwant";
00143 const char *TrackerRequestUrlCreator::ParameterKeyName          = "key";
00144 const char *TrackerRequestUrlCreator::ParameterTrackerIdName    = "trackerid";
00145 const char *TrackerRequestUrlCreator::CompactTrueValueString    = "1";
00146 const char *TrackerRequestUrlCreator::CompactFalseValueString   = "0";