2 // This file is part of the aMule Project.
4 // Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2007-2008 Dévai Tamás ( gonosztopi@amule.org )
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "MagnetURI.h"
39 // wx/chartype.h defines _T
40 # define _C(ch) wxChar(ch)
44 CMagnetURI::CMagnetURI(const STRING
& uri
)
46 if (uri
.compare(0, 7, _T("magnet:")) == 0) {
47 size_t start
= uri
.find(_C('?'));
48 if (start
== STRING::npos
) start
= uri
.length();
49 while (start
< uri
.length() - 1) {
50 size_t end
= uri
.find(_C('&'), start
+ 1);
51 if (end
== STRING::npos
) end
= uri
.length();
52 size_t pos
= uri
.find(_C('='), start
+ 1);
53 if (pos
== STRING::npos
) pos
= uri
.length();
55 m_fields
.push_back(Field_Type(uri
.substr(start
+ 1, pos
- start
- 1), uri
.substr(pos
+ 1, end
- pos
- 1)));
62 STRING
CMagnetURI::GetLink() const
64 STRING
retval(_T("magnet:"));
66 for (List_Type::const_iterator it
= m_fields
.begin(); it
!= m_fields
.end(); ++it
) {
67 if (it
== m_fields
.begin()) {
68 retval
.append(1, _C('?'));
70 retval
.append(1, _C('&'));
72 retval
.append(it
->first
);
73 retval
.append(1, _C('='));
74 retval
.append(it
->second
);
79 CMagnetURI::Value_List
CMagnetURI::GetField(const STRING
& name
) const
83 for (List_Type::const_iterator it
= m_fields
.begin(); it
!= m_fields
.end(); ++it
) {
84 if (it
->first
.compare(name
) == 0) {
85 retval
.push_back(it
->second
);
92 bool CMagnetED2KConverter::CanConvertToED2K() const
97 for (List_Type::const_iterator it
= m_fields
.begin(); it
!= m_fields
.end(); ++it
) {
98 if (it
->first
.compare(_T("xl")) == 0) {
102 if (it
->first
.compare(_T("xt")) == 0) {
103 if ((it
->second
.compare(0, 9, _T("urn:ed2k:")) == 0) ||
104 (it
->second
.compare(0, 13, _T("urn:ed2khash:")) == 0))
110 if (has_urn
&& has_xl
) break;
112 return has_urn
&& has_xl
;
115 STRING
CMagnetED2KConverter::GetED2KLink() const
117 if (CanConvertToED2K()) {
120 STRING
len(GetField(_T("xl")).front());
122 Value_List dn_val
= GetField(_T("dn"));
123 if (!dn_val
.empty()) {
126 // This should never happen. Has anyone seen a link without a file name?
127 // Just in case, assign a reasonable(?) name to that unnamed file.
128 dn
= _T("FileName.ext");
130 Value_List urn_list
= GetField(_T("xt"));
131 // Use the first ed2k-hash found.
132 for (Value_List::iterator it
= urn_list
.begin(); it
!= urn_list
.end(); ++it
) {
133 if (it
->compare(0, 9, _T("urn:ed2k:")) == 0) {
134 hash
= it
->substr(9);
136 } else if (it
->compare(0, 13, _T("urn:ed2khash:")) == 0) {
137 hash
= it
->substr(13);
141 return STRING(_T("ed2k://|file|")).append(dn
).append(1, _C('|')).append(len
).append(1, _C('|')).append(hash
).append(_T("|/"));