Encoding recognition upgraded. Added even more network error texts.

This commit is contained in:
Martin Rotter 2014-02-23 10:33:10 +01:00
parent 04f109654a
commit 4fd368c23a
4 changed files with 29 additions and 16 deletions

View File

@ -70,27 +70,21 @@ QPair<FeedsModelStandardFeed*, QNetworkReply::NetworkError> FeedsModelStandardFe
if ((result.second = NetworkFactory::downloadFeedFile(url,
Settings::instance()->value(APP_CFG_FEEDS, "feed_update_timeout", DOWNLOAD_TIMEOUT).toInt(),
feed_contents,
true,
!username.isEmpty(),
username,
password)) == QNetworkReply::NoError) {
// Feed XML was obtained, now we need to try to guess
// its encoding before we can read further data.
QXmlStreamReader xml_stream_reader(feed_contents);
QString xml_schema_encoding;
QString xml_contents_encoded;
QRegExp encoding_rexp("encoding=\"[^\"]\\S+\"");
// TODO: Use QRegExp and capture encoding attribute with it
// instead of heavy QXmlStreamReader.
// We have several chances to read the XML version directly
// from XML declaration.
for (int i = 0; i < 2 && !xml_stream_reader.atEnd(); i++) {
if ((xml_schema_encoding = xml_stream_reader.documentEncoding().toString()).isEmpty()) {
xml_stream_reader.readNext();
}
else {
break;
}
if (encoding_rexp.indexIn(feed_contents) != -1 &&
!(xml_schema_encoding = encoding_rexp.cap(0)).isEmpty()) {
// Some "encoding" attribute was found.
encoding_rexp.setPattern("[^\"]\\S+[^\"]");
encoding_rexp.indexIn(xml_schema_encoding, 9);
xml_schema_encoding = encoding_rexp.cap(0);
}
if (result.first == NULL) {
@ -175,8 +169,8 @@ QVariant FeedsModelStandardFeed::data(int column, int role) const {
}
else if (column == FDS_MODEL_COUNTS_INDEX) {
// TODO: Changeable text.
return QString("%1").arg(QString::number(countOfUnreadMessages()),
QString::number(countOfAllMessages()));
return QString("%1").arg(QString::number(countOfUnreadMessages()));
//QString::number(countOfAllMessages()));
}
else {
return QVariant();

View File

@ -13,6 +13,7 @@ NetworkFactory::NetworkFactory() {
QString NetworkFactory::networkErrorText(QNetworkReply::NetworkError error_code) {
switch (error_code) {
case QNetworkReply::ProtocolUnknownError:
case QNetworkReply::ProtocolFailure:
return QObject::tr("protocol error");
@ -37,6 +38,9 @@ QString NetworkFactory::networkErrorText(QNetworkReply::NetworkError error_code)
case QNetworkReply::TemporaryNetworkFailureError:
return QObject::tr("temporary failure");
case QNetworkReply::AuthenticationRequiredError:
return QObject::tr("authentication failed");
case QNetworkReply::ProxyAuthenticationRequiredError:
return QObject::tr("proxy authentication required");
@ -49,6 +53,9 @@ QString NetworkFactory::networkErrorText(QNetworkReply::NetworkError error_code)
case QNetworkReply::UnknownContentError:
return QObject::tr("uknown content");
case QNetworkReply::ContentNotFoundError:
return QObject::tr("content not found");
default:
return QObject::tr("unknown error");
}

View File

@ -7,6 +7,8 @@ WebBrowserNetworkAccessManager::WebBrowserNetworkAccessManager(QObject *parent)
: BaseNetworkAccessManager(parent) {
connect(this, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)),
this, SLOT(onSslErrors(QNetworkReply*,QList<QSslError>)));
connect(this, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
this, SLOT(onAuthenticationRequired(QNetworkReply*,QAuthenticator*)));
}
WebBrowserNetworkAccessManager::~WebBrowserNetworkAccessManager() {
@ -22,3 +24,12 @@ void WebBrowserNetworkAccessManager::onSslErrors(QNetworkReply *reply,
reply->ignoreSslErrors(error);
}
void WebBrowserNetworkAccessManager::onAuthenticationRequired(QNetworkReply *reply,
QAuthenticator *authenticator) {
Q_UNUSED(authenticator);
// Authentication is required but this feed does not contain it.
qDebug("URL '%s' requested authentication but username/password is not available.",
qPrintable(reply->url().toString()));
}

View File

@ -15,6 +15,7 @@ class WebBrowserNetworkAccessManager : public BaseNetworkAccessManager {
protected slots:
void onSslErrors(QNetworkReply *reply, const QList<QSslError> &error);
void onAuthenticationRequired(QNetworkReply * reply, QAuthenticator *authenticator);
};
#endif // WEBBROWSERNETWORKACCESSMANAGER_H