twitter.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include "twitter.h"
  2. #include "ui_twitter.h"
  3. Twitter::Twitter(QWidget *parent) :
  4. QMainWindow(parent),
  5. ui(new Ui::Twitter)
  6. {
  7. ui->setupUi(this);
  8. }
  9. Twitter::~Twitter()
  10. {
  11. delete ui;
  12. }
  13. void Twitter::changeEvent(QEvent *e)
  14. {
  15. QMainWindow::changeEvent(e);
  16. switch (e->type()) {
  17. case QEvent::LanguageChange:
  18. ui->retranslateUi(this);
  19. break;
  20. default:
  21. break;
  22. }
  23. }
  24. void Twitter::on_pushButton_released()
  25. { qDebug("Button pusehd");
  26. QNetworkAccessManager *manager = new QNetworkAccessManager(this);
  27. QString name = ui->lineEdit->text();
  28. qDebug()<<name;
  29. QString URL = "http://twitter.com/statuses/user_timeline/";
  30. URL.append(name);
  31. URL.append(".rss?count=5");
  32. //QUrl url("http://twitter.com/statuses/user_timeline/manikantan_k.rss?count=5");
  33. qDebug()<<URL;
  34. QUrl url(URL);
  35. qDebug()<<url.toString();
  36. QNetworkReply* reply = manager->get(QNetworkRequest(url));
  37. QObject::connect(manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(replyFinished(QNetworkReply*)));
  38. }
  39. void Twitter::replyFinished(QNetworkReply * a ) {
  40. qDebug("replied");
  41. QString str (a->readAll());
  42. if(a->error() == QNetworkReply::NoError) {
  43. qDebug("No errors");
  44. xml.addData(str);
  45. parseXml();
  46. }
  47. else {
  48. qDebug("errors in reply");
  49. }
  50. }
  51. void Twitter::parseXml()
  52. { qDebug("Parsing Twitter Feed");
  53. QString currentTag;
  54. QStringList feedcontent;
  55. while (!xml.atEnd()) {
  56. xml.readNext();
  57. if (xml.isStartElement()) {
  58. if (xml.name() == "item"){
  59. }
  60. currentTag = xml.name().toString();
  61. } else if (xml.isEndElement()) {
  62. if (xml.name() == "item") {
  63. }
  64. } else if (xml.isCharacters() && !xml.isWhitespace()) {
  65. if (currentTag == "description")
  66. //qDebug("sd %s", xml.text().toString());
  67. { qDebug()<<"Got"<< xml.text().toString();
  68. feedcontent.append(xml.text().toString());
  69. }
  70. /* else if (currentTag == "link")
  71. linkString += xml.text().toString();
  72. else if (currentTag == "pubDate")
  73. dateString += xml.text().toString(); */
  74. }
  75. }
  76. if (xml.error() && xml.error() != QXmlStreamReader::PrematureEndOfDocumentError) {
  77. //qWarning() << "XML ERROR:" << xml.lineNumber() << ": " << xml.errorString();
  78. //http.abort();
  79. }
  80. ui->listView->setModel(new QStringListModel(feedcontent));
  81. }