From 80a3f1908d8a6885aa6fb9c25f733164c7d7accb Mon Sep 17 00:00:00 2001 From: Jesper Thomschutz Date: Mon, 2 Apr 2007 20:14:57 +0200 Subject: [PATCH] Actually connects, and can now post messages --- twittersocket.cpp | 39 +++++++++++++++++++-------------------- twittersocket.h | 8 +++++--- w_connect.cpp | 30 ++++++++++++++++++++++++------ w_connect.h | 6 +++--- w_connect.ui | 18 +++++++++--------- w_timeline.cpp | 33 +++++++++++++++++++++++++++++++-- w_timeline.h | 17 ++++++++++++----- w_timeline.ui | 2 +- 8 files changed, 104 insertions(+), 49 deletions(-) diff --git a/twittersocket.cpp b/twittersocket.cpp index 9c607da..84e7dff 100644 --- a/twittersocket.cpp +++ b/twittersocket.cpp @@ -2,20 +2,32 @@ #include "twittersocket.h" -TwitterSocket::TwitterSocket(QObject *parent) : QHttp(parent) +TwitterSocket::TwitterSocket(QString user, QString pass, QObject *parent) : QHttp(parent) { qDebug("Constructed socket."); setHost("twitter.com"); + setUser(user, pass); + QObject::connect( this, SIGNAL( done(bool) ), this, SLOT(done( bool) ) ); } -bool TwitterSocket::accountValid(QString user, QString pass) +void TwitterSocket::accountValid() { - QObject::connect( this, SIGNAL( done( bool) ), this, SLOT( done( bool) ) ); - qDebug() << "Trying to connect with: " << user << " and " << pass; + get(QLatin1String("/statuses/friends_timeline.xml")); +} - setUser(user, pass); - get("/statuses/friends.xml"); - return true; +void TwitterSocket::update(QString message) +{ + QHttpRequestHeader header("POST", "/statuses/update.xml"); + header.setValue("Host", "twitter.com"); + // Twitterrific manages to get its name on the page using the source + // tag, but it doesn't seem to work for us :/ + const QString msg("source=Squawker&status="+message); + request(header, msg.toAscii()); +} + +void TwitterSocket::timeline() +{ + qDebug() << "You called timeline, it does nothing atm."; } void TwitterSocket::done( bool error) @@ -27,16 +39,3 @@ void TwitterSocket::done( bool error) else emit result(error, errorString()); } - - -/* - QHttpRequestHeader header("POST", "/statuses/update.xml"); - header.setValue("Host", "twitter.com"); - header.setValue("User-Agent", "Squawker"); - - QHttp *http = new QHttp(); - http->setHost("twitter.com"); - http->setUser("squawker","test123"); - http->request(header, "source=Squawker&status=Walking da turkey, again..."); -*/ - diff --git a/twittersocket.h b/twittersocket.h index 9614c3b..e3af803 100644 --- a/twittersocket.h +++ b/twittersocket.h @@ -8,9 +8,11 @@ class TwitterSocket : public QHttp Q_OBJECT public: - TwitterSocket(QObject *parent=0); + TwitterSocket(QString user, QString pass, QObject *parent=0); - bool accountValid(QString user, QString pass); + void timeline(); + void accountValid(); + void update(QString message); signals: void result( bool error, QString errorString); @@ -19,7 +21,7 @@ private: QHttp *http; private slots: - void done( bool error); + void done(bool error); }; diff --git a/w_connect.cpp b/w_connect.cpp index 7895060..d1274b2 100644 --- a/w_connect.cpp +++ b/w_connect.cpp @@ -9,21 +9,39 @@ w_Connect::w_Connect(QWidget *parent) le_user->setFocus(); - QObject::connect( pb_connect, SIGNAL( clicked() ), this, SLOT( connect() ) ); + QObject::connect(pb_connect, SIGNAL(clicked()), this, SLOT(connect())); } void w_Connect::connect() { pb_connect->setEnabled(false); - TwitterSocket *twittersocket = new TwitterSocket(); + pb_connect->setText(QLatin1String("Connecting")); + twittersocket = new TwitterSocket(le_user->text(), le_password->text()); QObject::connect( twittersocket, SIGNAL( result( bool, QString) ), this, SLOT( status( bool, QString) ) ); - twittersocket->accountValid( le_user->text(), le_password->text()); - - pb_connect->setEnabled(true); + twittersocket->accountValid(); } void w_Connect::status( bool error, QString errorString ) { - qDebug() << "error:" << error << " and " << errorString; + if (error == false) + { + w_timeline = new w_Timeline(twittersocket, this->parentWidget()); + w_timeline->show(); + this->destroy(); + delete this; + } + else + { + QMessageBox::critical(this, QLatin1String("Uh-oh."), + "An error occured:
" + errorString, + QMessageBox::Ok); + + pb_connect->setText(QLatin1String("Connect")); + pb_connect->setEnabled(true); + + le_password->clear(); + + qDebug() << "error:" << error << " and " << errorString; + } } diff --git a/w_connect.h b/w_connect.h index 96dd7c6..2a11fab 100644 --- a/w_connect.h +++ b/w_connect.h @@ -2,6 +2,7 @@ #define W_CONNECT_H #include "ui_w_connect.h" +#include "w_timeline.h" #include "twittersocket.h" class w_Connect : public QDialog, private Ui::w_connect @@ -11,15 +12,14 @@ class w_Connect : public QDialog, private Ui::w_connect public: w_Connect(QWidget *parent = 0); - public slots: void connect(); private slots: void status( bool error, QString errorString); private: - TwitterSocket twittersocket(); + TwitterSocket *twittersocket; + w_Timeline *w_timeline; }; - #endif diff --git a/w_connect.ui b/w_connect.ui index f2db805..a794ff6 100644 --- a/w_connect.ui +++ b/w_connect.ui @@ -5,21 +5,21 @@ 0 0 - 266 - 149 + 230 + 109 Connect - + 9 6 - + 0 @@ -27,24 +27,24 @@ 6 - + QLineEdit::Password - + - + Password - + User @@ -53,7 +53,7 @@ - + Connect diff --git a/w_timeline.cpp b/w_timeline.cpp index 6d9d3c3..d268379 100644 --- a/w_timeline.cpp +++ b/w_timeline.cpp @@ -2,9 +2,38 @@ #include "w_timeline.h" -w_Timeline::w_Timeline(QWidget *parent) +w_Timeline::w_Timeline(TwitterSocket *socket, QWidget *parent) { setupUi(this); - //Set focus on the message line edit le_message->setFocus(); + + twitterSocket = socket; + + QObject::connect(pb_update, SIGNAL(clicked()), this, SLOT(sendUpdate())); + + //Refresh the timeline ever so often... + timer = new QTimer(this); + QObject::connect(timer, SIGNAL(timeout()), this, SLOT(refresh())); + timer->start(9000); +} + +void w_Timeline::sendUpdate() +{ + QObject::connect( twitterSocket, SIGNAL( result(bool,QString) ), this, SLOT( updateResult(bool, QString) ) ); + twitterSocket->update(le_message->text()); +} + +void w_Timeline::updateResult(bool error, QString errorstring) +{ + qDebug() << "Update done:" << error; +} + +void w_Timeline::refresh() +{ + qDebug() << "Refreshing..."; +} + +void w_Timeline::refreshResult(bool error, QString errorstring) +{ + qDebug() << "Refresh result:" << error; } diff --git a/w_timeline.h b/w_timeline.h index 567199d..6d96297 100644 --- a/w_timeline.h +++ b/w_timeline.h @@ -2,17 +2,24 @@ #define W_TIMELINE_H #include "ui_w_timeline.h" - +#include "twittersocket.h" class w_Timeline : public QWidget, private Ui::w_timeline { Q_OBJECT public: - w_Timeline(QWidget *parent = 0); - - -public slots: + w_Timeline(TwitterSocket *socket, QWidget *parent = 0); + +private slots: + void sendUpdate(); + void refresh(); + void refreshResult(bool error, QString errorstring); + void updateResult(bool error, QString errorstring); + +private: + TwitterSocket *twitterSocket; + QTimer *timer; }; diff --git a/w_timeline.ui b/w_timeline.ui index 8e65f63..d4ce544 100644 --- a/w_timeline.ui +++ b/w_timeline.ui @@ -34,7 +34,7 @@ - + Squawk! -- 2.11.4.GIT