Convert QUrl with percent encoding into string - qt

I use a URL entered by the user as text to initialize a QUrl object. Later I want to convert the QUrl back into a string for displaying it and to check it using regular expression. This works fine as long as the user does not enter any percent encoded URLs.
Why doesn't the following example code work?
qDebug() << QUrl("http://test.com/query?q=%2B%2Be%3Axyz%2Fen").toDisplayString(QUrl::FullyDecoded);
It simply doesn't decode any of the percent-encoded characters. It should print "http://test.com/query?q=++e:xyz/en" but it actually prints "http://test.com/query?q=%2B%2Be%3Axyz%2Fen".
I also tried a lot of other methods like fromUserInput() but I could not make the code work correctly in Qt5.3.
Can someone explain me how to do this and why the above code doesn't work (i.e. showing the decoded URL) even when using QUrl::FullyDecoded?
UPDATE
After getting the fromPercentEncoding() hint, I tried the following code:
QUrl UrlFromUserInput(const QString& input)
{
QByteArray latin = input.toLatin1();
QByteArray utf8 = input.toUtf8();
if (latin != utf8)
{
// URL string containing unicode characters (no percent encoding expected)
return QUrl::fromUserInput(input);
}
else
{
// URL string containing ASCII characters only (assume possible %-encoding)
return QUrl::fromUserInput(QUrl::fromPercentEncoding(input.toLatin1()));
}
}
This allows the user to input unicode URLs and percent-encoded URLs and it is possible to decode both kinds of URLs for displaying/matching. However the percent-encoded URLs did not work in QWebView... the web-server responded differently (it returned a different page). So obviously QUrl::fromPercentEncoding() is not a clean solution since it effectively changes the URL. I could create two QUrl objects in the above function... one constructed directly, one constructed using fromPercentEncoding(), using the first for QWebView and the latter for displaying/matching only... but this seems absurd.

#Conclusion
I've done some research, the conclusion so far is: absurd.
QUrl::fromPercentEncoding() is the way to go and what OP has done in the UPDATE section should've been the accepted answer to the question in title.
I think Qt's document of QUrl::toDisplayString is a little bit misleading :
"Returns a human-displayable string representation of the URL. The output can be customized by passing flags with options. The option
RemovePassword is always enabled, since passwords should never be
shown back to users."
Actually it doesn't claim any decoding ability, the document here is unclear about it's behavior. But at least the password part is true. I've found some clues on Gitorious:
"Add QUrl::toDisplayString(), which is toString() without password. And fix documentation of toString() which said this was the method to
use for displaying to humans, while this has never been true."
#Test Code
In order to discern the decoding ability of different functions. The following code has been tested on Qt 5.2.1 (not tested on Qt 5.3 yet!)
QString target(/*path*/);
QUrl url_path(target);
qDebug() << "[Original String]:" << target;
qDebug() << "--------------------------------------------------------------------";
qDebug() << "(QUrl::toEncoded) :" << url_path.toEncoded(QUrl::FullyEncoded);
qDebug() << "(QUrl::url) :" << url_path.url();
qDebug() << "(QUrl::toString) :" << url_path.toString();
qDebug() << "(QUrl::toDisplayString) :" << url_path.toDisplayString(QUrl::FullyDecoded);
qDebug() << "(QUrl::fromPercentEncoding):" << url_path.fromPercentEncoding(target.toUtf8());
Return QByteArray: QUrl::toEncoded
Return QString: QUrl::url, QUrl::toString, QUrl::toDisplayString, QUrl::fromPercentEncoding
P.S. QUrl::url is just synonym for QUrl::toString.
#Output
[Case 1]: When target path = "%_%" (test the functionality of encoding):
[Original String]: "%_%"
--------------------------------------------------------------------
(QUrl::toEncoded) : "%25_%25"
(QUrl::url) : "%25_%25"
(QUrl::toString) : "%25_%25"
(QUrl::toDisplayString) : "%25_%25"
(QUrl::fromPercentEncoding): "%_%"
[Case 2]: When target path = "Meow !" (test the functionality of encoding):
[Original String]: "Meow !"
--------------------------------------------------------------------
(QUrl::toEncoded) : "Meow%20!"
(QUrl::url) : "Meow !"
(QUrl::toString) : "Meow !"
(QUrl::toDisplayString) : "Meow%20!" // "Meow !" when using QUrl::PrettyDecoded mode
(QUrl::fromPercentEncoding): "Meow !"
[Case 3]: When target path = "Meow|!" (test the functionality of encoding):
[Original String]: "Meow|!"
--------------------------------------------------------------------
(QUrl::toEncoded) : "Meow%7C!"
(QUrl::url) : "Meow%7C!"
(QUrl::toString) : "Meow%7C!"
(QUrl::toDisplayString) : "Meow|!" // "Meow%7C!" when using QUrl::PrettyDecoded mode
(QUrl::fromPercentEncoding): "Meow|!"
[Case 4]: When target path = "http://test.com/query?q=++e:xyz/en" (none % encoded):
[Original String]: "http://test.com/query?q=++e:xyz/en"
--------------------------------------------------------------------
(QUrl::toEncoded) : "http://test.com/query?q=++e:xyz/en"
(QUrl::url) : "http://test.com/query?q=++e:xyz/en"
(QUrl::toString) : "http://test.com/query?q=++e:xyz/en"
(QUrl::toDisplayString) : "http://test.com/query?q=++e:xyz/en"
(QUrl::fromPercentEncoding): "http://test.com/query?q=++e:xyz/en"
[Case 5]: When target path = "http://test.com/query?q=%2B%2Be%3Axyz%2Fen" (% encoded):
[Original String]: "http://test.com/query?q=%2B%2Be%3Axyz%2Fen"
--------------------------------------------------------------------
(QUrl::toEncoded) : "http://test.com/query?q=%2B%2Be%3Axyz%2Fen"
(QUrl::url) : "http://test.com/query?q=%2B%2Be%3Axyz%2Fen"
(QUrl::toString) : "http://test.com/query?q=%2B%2Be%3Axyz%2Fen"
(QUrl::toDisplayString) : "http://test.com/query?q=%2B%2Be%3Axyz%2Fen"
(QUrl::fromPercentEncoding): "http://test.com/query?q=++e:xyz/en"
P.S. I also encounter the bug that Ilya mentioned in comments: Percent Encoding doesn't seem to be working for '+' in QUrl
#Summary
The result of QUrl::toDisplayString is ambiguous. As the document says, the QUrl::FullyDecoded mode must be used with care. No matter what type of URL you got, encode them by QUrl::toEncode and display them with QUrl::fromPercentEncoding when necessary.
As for the malfunction of percent-encoded URLs in QWebView mentioned in OP, more details are needed to debug it. Different function and different mode used could be the reason.
#Helpful Resources
RFC 3986 (which QUrl conforms)
Encode table
Source of qurl.cpp on Gitorious

I am not sure why toDisplayString(QUrl::FullyDecoded) does not work.
After trying several versions I have found that copy.query(QUrl::FullyDecoded) does decode the query part. The Documentation has an example with the the following code does return the decoded URL:
QUrl url("http://test.com/query?q=%2B%2Be%3Axyz%2Fen");
url.setQuery(url.query(QUrl::FullyDecoded), QUrl::DecodedMode);
qDebug() << url.toString();
To solve the problem this way is not optimal because the query part is copied without need.

You can use QUrlQuery::toString(QUrl::FullyEncoded) or QUrl::fromPercentEncoding() for this converting.

Related

Qt ActiveX and OneNote (desktop 2016 32bit)

Trying to export student exam results from excel to Student OneNote pages.
importing the excel data using QAxObject dynamic calls works flawlessly however a similar setup with OneNote yeilds unkown
getHieracrchy Documentation below is the output of dumpdoc.exe :
void GetHierarchy (QString bstrStartNodeID, HierarchyScope hsScope, QString& pbstrHierarchyXmlOut, XMLSchema xsSchema)
QVariantList params = ...
onenote_object->dynamicCall("GetHierarchy(QString, HierarchyScope, QString&, XMLSchema)", params);
Implementation :
QAxObject* onenote = new QAxObject("OneNote.Application");
QString doc = onenote->generateDocumentation();
qDebug() << onenote->control(); // {dc67e480-c3cb-49f8-8232-60b0c2056c8e}
QVariantList params;
QString xml_out;
params << "" << 4 << xml_out << 0;
onenote->dynamicCall("GetHierarchy(QString, HierarchyScope, QString&, XMLSchema)", params);
Error
QAxBase: Error calling IDispatch member GetHierarchy: Unknown error
I have tried running dumpcpp against the CLSID of the OneNote.Application and the registry entry for the typelib thinking it might be a type error for a none builtin Qt->COM type, but no output is produced.
dumpcpp -nometaobject -getfile {0EA692EE-BB50-4E3C-AEF0-356D91732725} -o onenote
I can manipulate the pages via powershell and see the typelib reference in visual basic but want to access OneNote from C++.

SCP always returns the same error code

I have a problem copying files with scp. I use Qt and copy my files with scp using QProcess. And when something bad happens I always get exitCode=1. It always returns 1. I tried copying files with a terminal. The first time I got the error "Permission denied" and the exit code was 1. Then I unplugged my Ethernet cable and got the error "Network is unreachable". And the return code was still 1. It confuses me very much cause in my application I have to distinct these types of errors.
Any help is appreciated. Thank you so much!
See this code as a working example:
bool Utility::untarScript(QString filename, QString& statusMessages)
{
// Untar tar-bzip2 file, only extract script to temp-folder
QProcess tar;
QStringList arguments;
arguments << "-xvjf";
arguments << filename;
arguments << "-C";
arguments << QDir::tempPath();
arguments << "--strip-components=1";
arguments << "--wildcards";
arguments << "*/folder.*";
// tar -xjf $file -C $tmpDir --strip-components=1 --wildcards
tar.start("tar", arguments);
// Wait for tar to finish
if (tar.waitForFinished(10000) == true)
{
if (tar.exitCode() == 0)
{
statusMessages.append(tar.readAllStandardError());
return true;
}
}
statusMessages.append(tar.readAllStandardError());
statusMessages.append(tar.readAllStandardOutput());
statusMessages.append(QString("Exitcode = %1\n").arg(tar.exitCode()));
return false;
}
It gathers all available process output for you to analyse. Especially look at readAllStandardError().

Arduino with servo and RTC

I am making a project that involves a RTC and a servo motor so that it only turns on at a certain time. A snippet from the loop is:
void loop() {
DateTime now = rtc.now();
if (DateTime == 19:10) {
//Some stuff
} else {
return();
}
}
and my error is:
Arduino: 1.6.8 (Windows 10), Board: "Arduino/Genuino Uno"
C:\Users\User\Documents\Arduino\Servo_motor\Servo_motor.ino: In function 'void loop()':
Servo_motor:36: error: expected primary-expression before '==' token
if (DateTime == 19:10) {
^
Servo_motor:36: error: expected ')' before ':' token
if (DateTime == 19:10) {
^
Servo_motor:45: error: expected primary-expression before '}' token
}
^
Servo_motor:45: error: return-statement with a value, in function returning 'void' [-fpermissive]
Servo_motor:45: error: expected ';' before '}' token
Multiple libraries were found for "RTClib.h"
Used: C:\Program Files (x86)\Arduino\libraries\RTClib
Not used: C:\Users\User\Documents\Arduino\libraries\arduino_786051
exit status 1
expected primary-expression before '==' token
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
I am really confused. Can someone please help?
I'm going to assume you're using the Adafruit RTClib located here, as this is likely the one accessible from the IDE, or that a tutorial will use. It's also a fork of the other available RTClib, so this answer is likely to pertain to both.
If you check RTClib.h, you will find the publicly available methods for DateTime and all the RTC classes. If you do so, you'll notice that there is no operator== method, and in general this means that you can't use that as a form of comparison.
In order to do what you want, you need to use DateTime.minute() and DateTime.hour() and compare them separately. In your loop block it would look as follows:
void loop() {
DateTime now = rtc.now();
if (now.hour() == 19 && now.minute() == 10) {
//Some stuff
} else {
return;
}
}
However, this has the possibility of running the code in question quite a few times, as this check will succeed every time the loop runs during the minute following the RTC ticking over to 19:10.
It seems you're comparing the the type with a constant (time without quotes).
Dont you mean something like this?
if (now == "19:10") {

QTextStream readAll() removes Newlines

I am using QFile and QTextStream to first read a file and then write the read contents unmodified back into the same file. Here is the code:
QFile inFile("file.txt");
if(!inFile.open(QFile::ReadOnly | QFile::Text))
{
qCritical() << "ERROR: Unable to open input file: " << "file.txt";
exit(1);
}
QTextStream inStream(&inFile);
QString fileContents = inStream.readAll();
inFile.close();
QFile outFile("file.txt");
if(!outFile.open(QFile::WriteOnly | QFile::Text))
{
qCritical() << "ERROR: Unable to open output file: " << "file.txt";
exit(1);
}
QTextStream outSream(&outFile);
outSream << fileContents;
outFile.close();
However, this transforms the file.txt given below:
1
2
3
4
5
into
12345
i.e. newlines are getting removed in this process. Why is this happening? How can I prevent it?
The QIODevice::Text flag passed to open() tells Qt to convert Windows-style line terminators (\r\n) into C++-style terminators (\n).
Are you operating in Windows? You should be able to see the \r\n in a binary editor on the input and the output file.
About QIODevice::Text in openMode the official document says.
When reading, the end-of-line terminators are translated to '\n'. When writing, the end-of-line terminators are translated to the local encoding, for example '\r\n' for Win32.
It says Win32,while working on Win64,Qt5.8 I found it works differently.
With QIODevice::Text in openMode, QIODevice::readAll() remove all '\r','\t'.
And talk about \n,they are replaced by \r whatever openMode is using.
May be removed if using QIODevice::Text mode.
The solution is to not use QFile::Text or QIODevice::Text, if you are reading, just use QIODevice::ReadOnly , in that way you can also figure out the exact line ending used and handle them appropriately. I had this problem with Mac(CR) or '\r' line ending while opening with QIODevice::Text.

QFile::copy says "can't open source file for input"?

I'm going to copy one file using QFile::copy function but this function always returns false and errorString says :
"Cannot open D:/tmp/buf34.txt for input"
I tried to run this program with administrator privilege but nothing changed. My code is really simple :
QString source = url.toLocalFile();
QString destination = _dir.absolutePath()
+ QString("/%1").arg(QFileInfo(source).fileName());
qDebug()<<"Cp from :" << source << " to : "<< destination;
QFile file(source);
qDebug()<<file.copy(destination);
qDebug()<<file.errorString();
Edit:
I have QListView occupied with a QFileSystemModel. I try to drag one file from this ListView to a QLabel. For the QLabel a destination path is set. In drop event I try to copy file.
QFile::copy uses QFile::open but overwrites the error message open would give by the unhelpful "Cannot open %1 for input" you got.
So, you should try opening the file yourself to get that original error message:
qDebug()<<file.open(QFile::ReadOnly);
qDebug()<<file.errorString();

Resources