1 /* 2 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies) 3 * 4 * This library is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Library General Public 6 * License as published by the Free Software Foundation; either 7 * version 2 of the License, or (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Library General Public License for more details. 13 * 14 * You should have received a copy of the GNU Library General Public License 15 * along with this library; see the file COPYING.LIB. If not, write to 16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 17 * Boston, MA 02110-1301, USA. 18 * 19 */ 20 #include "config.h" 21 #include "KURL.h" 22 #include "CString.h" 23 #include "TextEncoding.h" 24 25 #include "NotImplemented.h" 26 #include "qurl.h" 27 28 namespace WebCore { 29 30 #if QT_VERSION < 0x040500 31 static const char hexnumbers[] = "0123456789ABCDEF"; 32 static inline char toHex(char c) 33 { 34 return hexnumbers[c & 0xf]; 35 } 36 #endif 37 38 KURL::KURL(const QUrl& url) 39 { 40 *this = KURL(KURL(), url.toEncoded().constData(), UTF8Encoding()); 41 } 42 43 KURL::operator QUrl() const 44 { 45 #if QT_VERSION < 0x040500 46 unsigned length = m_string.length(); 47 48 QByteArray ba; 49 ba.reserve(length); 50 51 int path = -1; 52 int host = m_string.find("://"); 53 if (host != -1) { 54 host += 3; 55 56 path = m_string.find('/', host); 57 } 58 59 for (unsigned i = 0; i < length; ++i) { 60 const char chr = static_cast<char>(m_string[i]); 61 62 switch (chr) { 63 encode: 64 case '{': 65 case '}': 66 case '|': 67 case '\\': 68 case '^': 69 case '`': 70 ba.append('%'); 71 ba.append(toHex((chr & 0xf0) >> 4)); 72 ba.append(toHex(chr & 0xf)); 73 break; 74 case '[': 75 case ']': 76 // special case: if this is the host part, don't encode 77 // otherwise, encode 78 if (host == -1 || (path != -1 && i >= path)) 79 goto encode; 80 // fall through 81 default: 82 ba.append(chr); 83 break; 84 } 85 } 86 #else 87 // Qt 4.5 or later 88 // No need for special encoding 89 QString str = QString::fromRawData(reinterpret_cast<const QChar*>(m_string.characters()), m_string.length()); 90 QByteArray ba = str.toUtf8(); 91 #endif 92 93 QUrl url = QUrl::fromEncoded(ba); 94 return url; 95 } 96 97 String KURL::fileSystemPath() const 98 { 99 if (!isValid() || !protocolIs("file")) 100 return String(); 101 102 return String(path()); 103 } 104 105 } 106 107