AmirH
19th May 2015, 19:53
Hi,
I'm developping an app on windows which tooltips French news by the system tray.
After succesfully developped the functions that read xml files and fill them, I wanna download the rss file of lemonde.fr (which u know is a xml file) to add the last news in the local xml file.
I tried to do all necessary work for downloading that file but I have a "Unknown error" with reply->errorString(); and then the program crashes.
Here is the principal code :
Window::Window()
{
AppVars::urlRSSlist << "http://rss.lemonde.fr/c/205/f/3050/index.rss";
AppVars::netManList<< new QNetworkAccessManager(this);
QObject::connect (AppVars::netManList[0], SIGNAL(finished(QNetworkReply*)), this, SLOT(downloadedRSSalaune(QNetworkReply*))) ;
for(int i=0;i<AppVars::urlRSSlist.size();i++){
qDebug() << "enter for loop for i = "+QString::number(i);
AppVars::netManList[i]->get(QNetworkRequest(QUrl(AppVars::urlRSSlist[i])));
}
}
void Window::downloadedRSSalaune(QNetworkReply* reply){
qDebug() << reply->errorString();
QByteArray data =QByteArray(reply->readAll());
QList<QString> titleList;
QList<QString> linkList;
QDomDocument *domfile = new QDomDocument("alaune_xml");
if(!(domfile->setContent(data))){
return;
}
QDomNode principalNode = domfile->documentElement();
QDomNode channelNode = principalNode.namedItem("rss").namedItem("channel");
QDomNodeList listItems = channelNode.childNodes();
int k = 0;
int i = 0;
while(k<10 && i<listItems.length()){
if(listItems.at(i).nodeName() == "item"){
qDebug() << listItems.at(i).namedItem("title").toElement().text();
titleList << listItems.at(i).namedItem("title").toElement().text();
linkList << listItems.at(i).namedItem("link").toElement().text();
++k;
}
++i;
}
firstInsert10ArticlesXML(titleList, linkList, "alaune");
tooltipInstantNews("DirectNews France","Les articles sont maintenant à jours.","");
}
void Window::firstInsert10ArticlesXML(QList<QString> titles, QList<QString> links, QString cat)
{
QString filepath = QCoreApplication::applicationDirPath();
filepath.append("\\history.xml");
AppFiles::historyXML->setFileName(filepath);
if(!(AppFiles::historyXML->open(QIODevice::ReadOnly))){
return;
}
// QDomDocument to parse xml file
QDomDocument *domfile = new QDomDocument("history_xml");
if(!(domfile->setContent(AppFiles::historyXML))){
return;
}
AppFiles::historyXML->close();
QList<QDomElement *> newItems;
for(int i = 0; i<10; i++){
newItems<<new QDomElement(domfile->createElement("item"));
QDomElement titleSubItem = domfile->createElement("title");
QDomText titleText = domfile->createTextNode(titles[i]);
QDomElement linkSubItem = domfile->createElement("link");
QDomText linkText = domfile->createTextNode(links[i]);
titleSubItem.appendChild(titleText);
linkSubItem.appendChild(linkText);
newItems[i]->appendChild(titleSubItem);
newItems[i]->appendChild(linkSubItem);
}
QDomNode principalNode = domfile->documentElement();
QDomNode catNode = principalNode.namedItem(cat);
if(catNode.isNull())
return;
for(int i = 0; i<10; i++){
catNode.insertBefore(*newItems[i],catNode.firstChild());
}
QString write_doc = domfile->toString();
if(!(AppFiles::historyXML->open(QIODevice::WriteOnly))){
return;
}
AppFiles::historyXML->resize(0);
QTextStream stream(AppFiles::historyXML);
stream << write_doc;
AppFiles::historyXML->close();
ReadXML();
}
void Window::ReadXML()
{
QString filepath = QCoreApplication::applicationDirPath();
filepath.append("\\history.xml");
AppFiles::historyXML->setFileName(filepath);
if(!(AppFiles::historyXML->open(QIODevice::ReadOnly))){
return;
}
// QDomDocument to parse xml file
QDomDocument *domfile = new QDomDocument("history_xml");
if(!(domfile->setContent(AppFiles::historyXML))){
return;
}
AppFiles::historyXML->close();
QDomNode principalNode = domfile->documentElement();
QList<QDomNode> listNodes;
listNodes<<principalNode.namedItem("alaune")<<principalNode.namedItem("monde")<<principalNode.namedItem("politique")<<principalNode.namedItem("societesante")
<<principalNode.namedItem("economie")<<principalNode.namedItem("science")<<principalNode.namedItem("art")<<principalNode.namedItem("sport")
<<principalNode.namedItem("people");
for(int k=0;k<9;k++){
QDomNode item = listNodes[k].firstChild();
int i = 0;
while(!listNodes[k].isNull() && !item.isNull() && i<10){
//for each article found, enable the QAction, and attribute text and link
listArticles[k][i]->setEnabled(true);
QString titleTrunc = item.namedItem("title").toElement().text();
if(titleTrunc.size() > 50)
titleTrunc = titleTrunc.left(50).append("...");
listArticles[k][i]->setText(titleTrunc);
AppVars::listShortcuts[k][i] = item.namedItem("link").toElement().text();
++i;
item = item.nextSibling();
}
}
}
void Window::tooltipInstantNews(QString title, QString categorie, QString url)
{
// recieved from getrssthread.cpp, show directly (no use of any xml file) the news and attribute system tray message link
sticon->setToolTip(categorie+" : \n"+title);
AppVars::shortcutMessage = url;
if(AppVars::NotifBool)
sticon->showMessage(categorie,title,QSystemTrayIcon::Infor mation);
}
I may have to put some if(!ERROR) but I don't know where and how.
Window class derivates from QWidget
AppVars::urlRSSlist is a QList<String> (global var)
AppVars::netManList is a QList<QNetworkAccessManager*> (global var)
if something not clear, please tell me.
Thank u in advance for your help.
I'm developping an app on windows which tooltips French news by the system tray.
After succesfully developped the functions that read xml files and fill them, I wanna download the rss file of lemonde.fr (which u know is a xml file) to add the last news in the local xml file.
I tried to do all necessary work for downloading that file but I have a "Unknown error" with reply->errorString(); and then the program crashes.
Here is the principal code :
Window::Window()
{
AppVars::urlRSSlist << "http://rss.lemonde.fr/c/205/f/3050/index.rss";
AppVars::netManList<< new QNetworkAccessManager(this);
QObject::connect (AppVars::netManList[0], SIGNAL(finished(QNetworkReply*)), this, SLOT(downloadedRSSalaune(QNetworkReply*))) ;
for(int i=0;i<AppVars::urlRSSlist.size();i++){
qDebug() << "enter for loop for i = "+QString::number(i);
AppVars::netManList[i]->get(QNetworkRequest(QUrl(AppVars::urlRSSlist[i])));
}
}
void Window::downloadedRSSalaune(QNetworkReply* reply){
qDebug() << reply->errorString();
QByteArray data =QByteArray(reply->readAll());
QList<QString> titleList;
QList<QString> linkList;
QDomDocument *domfile = new QDomDocument("alaune_xml");
if(!(domfile->setContent(data))){
return;
}
QDomNode principalNode = domfile->documentElement();
QDomNode channelNode = principalNode.namedItem("rss").namedItem("channel");
QDomNodeList listItems = channelNode.childNodes();
int k = 0;
int i = 0;
while(k<10 && i<listItems.length()){
if(listItems.at(i).nodeName() == "item"){
qDebug() << listItems.at(i).namedItem("title").toElement().text();
titleList << listItems.at(i).namedItem("title").toElement().text();
linkList << listItems.at(i).namedItem("link").toElement().text();
++k;
}
++i;
}
firstInsert10ArticlesXML(titleList, linkList, "alaune");
tooltipInstantNews("DirectNews France","Les articles sont maintenant à jours.","");
}
void Window::firstInsert10ArticlesXML(QList<QString> titles, QList<QString> links, QString cat)
{
QString filepath = QCoreApplication::applicationDirPath();
filepath.append("\\history.xml");
AppFiles::historyXML->setFileName(filepath);
if(!(AppFiles::historyXML->open(QIODevice::ReadOnly))){
return;
}
// QDomDocument to parse xml file
QDomDocument *domfile = new QDomDocument("history_xml");
if(!(domfile->setContent(AppFiles::historyXML))){
return;
}
AppFiles::historyXML->close();
QList<QDomElement *> newItems;
for(int i = 0; i<10; i++){
newItems<<new QDomElement(domfile->createElement("item"));
QDomElement titleSubItem = domfile->createElement("title");
QDomText titleText = domfile->createTextNode(titles[i]);
QDomElement linkSubItem = domfile->createElement("link");
QDomText linkText = domfile->createTextNode(links[i]);
titleSubItem.appendChild(titleText);
linkSubItem.appendChild(linkText);
newItems[i]->appendChild(titleSubItem);
newItems[i]->appendChild(linkSubItem);
}
QDomNode principalNode = domfile->documentElement();
QDomNode catNode = principalNode.namedItem(cat);
if(catNode.isNull())
return;
for(int i = 0; i<10; i++){
catNode.insertBefore(*newItems[i],catNode.firstChild());
}
QString write_doc = domfile->toString();
if(!(AppFiles::historyXML->open(QIODevice::WriteOnly))){
return;
}
AppFiles::historyXML->resize(0);
QTextStream stream(AppFiles::historyXML);
stream << write_doc;
AppFiles::historyXML->close();
ReadXML();
}
void Window::ReadXML()
{
QString filepath = QCoreApplication::applicationDirPath();
filepath.append("\\history.xml");
AppFiles::historyXML->setFileName(filepath);
if(!(AppFiles::historyXML->open(QIODevice::ReadOnly))){
return;
}
// QDomDocument to parse xml file
QDomDocument *domfile = new QDomDocument("history_xml");
if(!(domfile->setContent(AppFiles::historyXML))){
return;
}
AppFiles::historyXML->close();
QDomNode principalNode = domfile->documentElement();
QList<QDomNode> listNodes;
listNodes<<principalNode.namedItem("alaune")<<principalNode.namedItem("monde")<<principalNode.namedItem("politique")<<principalNode.namedItem("societesante")
<<principalNode.namedItem("economie")<<principalNode.namedItem("science")<<principalNode.namedItem("art")<<principalNode.namedItem("sport")
<<principalNode.namedItem("people");
for(int k=0;k<9;k++){
QDomNode item = listNodes[k].firstChild();
int i = 0;
while(!listNodes[k].isNull() && !item.isNull() && i<10){
//for each article found, enable the QAction, and attribute text and link
listArticles[k][i]->setEnabled(true);
QString titleTrunc = item.namedItem("title").toElement().text();
if(titleTrunc.size() > 50)
titleTrunc = titleTrunc.left(50).append("...");
listArticles[k][i]->setText(titleTrunc);
AppVars::listShortcuts[k][i] = item.namedItem("link").toElement().text();
++i;
item = item.nextSibling();
}
}
}
void Window::tooltipInstantNews(QString title, QString categorie, QString url)
{
// recieved from getrssthread.cpp, show directly (no use of any xml file) the news and attribute system tray message link
sticon->setToolTip(categorie+" : \n"+title);
AppVars::shortcutMessage = url;
if(AppVars::NotifBool)
sticon->showMessage(categorie,title,QSystemTrayIcon::Infor mation);
}
I may have to put some if(!ERROR) but I don't know where and how.
Window class derivates from QWidget
AppVars::urlRSSlist is a QList<String> (global var)
AppVars::netManList is a QList<QNetworkAccessManager*> (global var)
if something not clear, please tell me.
Thank u in advance for your help.