author | hgs |
Fri, 15 Oct 2010 17:30:59 -0400 | |
changeset 16 | 3c88a81ff781 |
parent 12 | afcd8e6d025b |
permissions | -rw-r--r-- |
0
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
1 |
/* |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
2 |
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
3 |
* All rights reserved. |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
4 |
* |
3 | 5 |
* This program is free software: you can redistribute it and/or modify |
6 |
* it under the terms of the GNU Lesser General Public License as published by |
|
7 |
* the Free Software Foundation, version 2.1 of the License. |
|
8 |
* |
|
9 |
* This program 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 |
|
12 |
* GNU Lesser General Public License for more details. |
|
0
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
13 |
* |
3 | 14 |
* You should have received a copy of the GNU Lesser General Public License |
15 |
* along with this program. If not, |
|
16 |
* see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/". |
|
0
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
17 |
* |
3 | 18 |
* Description: |
0
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
19 |
* |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
20 |
*/ |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
21 |
|
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
22 |
#include <QRegExp> |
16 | 23 |
#include <QDebug> |
24 |
#include <QStringList> |
|
0
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
25 |
#include "UiUtil.h" |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
26 |
|
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
27 |
#define GOOGLE_SEARCH_ENGINE "http://www.google.com/search?q=" |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
28 |
|
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
29 |
namespace WRT { |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
30 |
|
12 | 31 |
|
32 |
QString UiUtil::removeScheme(const QString & str ) |
|
33 |
{ |
|
34 |
||
35 |
QUrl url(str); |
|
36 |
QString scheme=url.scheme(); |
|
37 |
QString urlStr = str; |
|
38 |
||
39 |
if (scheme == "http" || scheme == "https") { |
|
40 |
urlStr.remove(0, scheme.length() + 3); //remove "scheme://" |
|
41 |
} |
|
42 |
return urlStr; |
|
43 |
||
44 |
} |
|
45 |
||
0
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
46 |
QString UiUtil::loadFiletoString(const QString &name) |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
47 |
{ |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
48 |
QFile file(name); |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
49 |
file.open(QFile::ReadOnly); |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
50 |
QString fileString = QLatin1String(file.readAll()); |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
51 |
file.close(); |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
52 |
return fileString; |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
53 |
} |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
54 |
|
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
55 |
QUrl UiUtil::searchUrl(const QString &string) |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
56 |
{ |
16 | 57 |
QUrl url = searchUrlWithoutCheck(string); |
0
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
58 |
if(url.isValid()) |
16 | 59 |
return url; |
9 | 60 |
else |
61 |
return QUrl(); |
|
0
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
62 |
} |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
63 |
|
16 | 64 |
QUrl UiUtil::searchUrlWithoutCheck(const QString &string) |
65 |
{ |
|
66 |
QString urlStr = string.trimmed(); |
|
67 |
QString urlStrEncoded = QUrl::toPercentEncoding(string); |
|
68 |
QUrl url(QLatin1String(GOOGLE_SEARCH_ENGINE) + urlStrEncoded, QUrl::TolerantMode); |
|
69 |
return url; |
|
70 |
} |
|
71 |
||
72 |
QUrl UiUtil::encodeQueryItems(QUrl& url, bool encodeNonAscii) |
|
73 |
{ |
|
74 |
QList< QPair<QString, QString> > queryItems = url.queryItems(); |
|
75 |
if (queryItems.size() != 0) { |
|
76 |
QString urlStr = url.toString(); |
|
77 |
int queryIndex = urlStr.indexOf("?"); |
|
78 |
int queryLength = urlStr.length() - queryIndex - 1; |
|
79 |
Q_ASSERT(queryIndex != -1 && queryLength >= 0); |
|
80 |
urlStr = urlStr.right(queryLength); |
|
81 |
||
82 |
QString queryStr; |
|
83 |
bool hasValidQuery = false; |
|
84 |
QStringList queryList = urlStr.split("&"); |
|
85 |
//qDebug() << "queryList!!!!!!!!!!!!!!!:" << queryList; |
|
86 |
for(int i = 0; i < queryList.size(); i++) { |
|
87 |
QString query = queryList.at(i); |
|
88 |
//qDebug() << "query!!!!!!!!!!!!:" << query; |
|
89 |
||
90 |
if (query.isEmpty()) { |
|
91 |
if ((i == queryList.size() - 1) && queryList.at(i - 1).isEmpty()) // "&&" ==> ["","",""] ==> "%25%25" |
|
92 |
continue; |
|
93 |
queryStr += QUrl::toPercentEncoding("&"); // encode '&' since it is a part of the query text |
|
94 |
} else { |
|
95 |
int delimiterIndex = query.indexOf("="); |
|
96 |
if (delimiterIndex == -1 || delimiterIndex == 0) { |
|
97 |
// it is not a valid query, instead, it is a part of query |
|
98 |
if (hasValidQuery) |
|
99 |
queryStr += QUrl::toPercentEncoding("&"); // encode '&' since it is a part of the query text |
|
100 |
queryStr += encodeNonAscii ? QUrl::toPercentEncoding(query, "%") : query; // "q=a&dc$da" ==> ["q=a","dc$da"] ==> for "dc$da", it is a part of the last query |
|
101 |
} |
|
102 |
else { |
|
103 |
// it is a valid query, close last query before add a new one |
|
104 |
// "q=a=c&z=dcda" ==> ["q=a=c","z=dcda"] ==> for "q=a=c", "q" is the key and "a=c" is the value |
|
105 |
if (hasValidQuery) |
|
106 |
queryStr += "&"; // don't encode this '&' since it is a part of the query delimiter |
|
107 |
QString key = encodeNonAscii ? QUrl::toPercentEncoding(query.left(delimiterIndex), "%") : query.left(delimiterIndex); |
|
108 |
QString value = encodeNonAscii ? QUrl::toPercentEncoding(query.right(query.length() - delimiterIndex - 1), "%") : query.right(query.length() - delimiterIndex - 1); |
|
109 |
QString newQuery = key + "=" + value; |
|
110 |
queryStr += newQuery; |
|
111 |
hasValidQuery = true; |
|
112 |
} |
|
113 |
} |
|
114 |
||
115 |
} |
|
116 |
QUrl encodedUrl = QUrl(url.toString().left(queryIndex + 1) + queryStr); |
|
117 |
return encodedUrl; |
|
118 |
//qDebug() << "new query url ---> " << url.toString(); |
|
119 |
} |
|
120 |
||
121 |
return url; |
|
122 |
} |
|
123 |
||
0
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
124 |
QUrl UiUtil::guessUrlFromString(const QString &string) |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
125 |
{ |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
126 |
QString urlStr = string.trimmed(); |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
127 |
QRegExp schemaTest(QLatin1String("^[a-zA-Z]+\\:.*")); |
16 | 128 |
QRegExp fileTest(QLatin1String("^[a-zA-Z]:/.*")); |
129 |
||
130 |
//remove new line character |
|
131 |
if (urlStr.contains("\n")) |
|
132 |
urlStr.replace(QLatin1String("\n"), QLatin1String("")); |
|
0
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
133 |
|
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
134 |
// Check if it looks like a qualified URL. Try parsing it and see. |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
135 |
bool hasSchema = schemaTest.exactMatch(urlStr); |
16 | 136 |
bool isFileUrl = fileTest.exactMatch(urlStr); |
0
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
137 |
bool hasSpaces = urlStr.contains(" "); |
16 | 138 |
|
139 |
// Is it a file path (ex. "c:/data/test.html")? |
|
140 |
if (isFileUrl) { |
|
141 |
if (QFile::exists(urlStr)) { |
|
142 |
return QUrl::fromLocalFile(urlStr); |
|
143 |
} |
|
144 |
} |
|
145 |
||
0
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
146 |
if (hasSchema) { |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
147 |
if(!hasSpaces){ |
16 | 148 |
QUrl url(urlStr, QUrl::TolerantMode); |
149 |
if (url.isValid()) |
|
150 |
return encodeQueryItems(url, false); |
|
151 |
} |
|
152 |
else{ |
|
153 |
if(urlStr.contains("https://")){ |
|
154 |
urlStr.remove("https://" ,Qt::CaseInsensitive); |
|
155 |
} |
|
156 |
else if(urlStr.contains("http://")){ |
|
157 |
urlStr.remove("http://" ,Qt::CaseInsensitive); |
|
158 |
} |
|
159 |
else if(urlStr.contains("rtsp://")){ |
|
160 |
urlStr.remove("rtsp://" ,Qt::CaseInsensitive); |
|
161 |
} |
|
162 |
QUrl url = searchUrlWithoutCheck(urlStr); |
|
163 |
if(url.isValid()) |
|
164 |
return url; |
|
165 |
} |
|
0
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
166 |
} |
16 | 167 |
|
0
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
168 |
// Might be a shorturl - try to detect the schema. |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
169 |
if (!hasSchema) { |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
170 |
int dotIndex = urlStr.indexOf(QLatin1Char('.')); |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
171 |
if (dotIndex != -1 && !hasSpaces) { |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
172 |
QUrl url; |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
173 |
urlStr.endsWith(".") ? ( url.setUrl(QLatin1String("http://") + urlStr + QLatin1String("com"), QUrl::TolerantMode) ) : ( url.setUrl(QLatin1String("http://") + urlStr, QUrl::TolerantMode) ); |
12 | 174 |
if (url.isValid()){ |
16 | 175 |
return encodeQueryItems(url, false); |
12 | 176 |
} |
16 | 177 |
//--Condition for Character DOT(.)-- |
12 | 178 |
else { |
16 | 179 |
QUrl url = searchUrlWithoutCheck(urlStr); |
12 | 180 |
if(url.isValid()) |
181 |
return url; |
|
182 |
} |
|
0
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
183 |
} |
16 | 184 |
//--The string parameter is simple text and a search should be performed. Like for Special Character :\ etc.-- |
185 |
else { |
|
186 |
QUrl url = searchUrlWithoutCheck(urlStr); |
|
0
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
187 |
if(url.isValid()) |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
188 |
return url; |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
189 |
} |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
190 |
} |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
191 |
|
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
192 |
// Fall back to QUrl's own tolerant parser. |
16 | 193 |
QUrl url = QUrl(string, QUrl::TolerantMode); |
194 |
return encodeQueryItems(url, false); |
|
0
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
195 |
} |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
196 |
|
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
197 |
} |