/* Copyright (C) 2008 by John Hobbs john@velvetcache.org This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifndef KICKTWEET_H #define KICKTWEET_H #define _KT_DEBUG #define KICKTWEET_VERSION "0.4.0.3" #define KICKTWEET_POPUP_TIMEOUT 5000 #ifdef _KT_DEBUG #include #endif #include #include #include #include #include #include #include #include #include #include #include "libtweet.h" enum TweetWorkerAction { SHUTDOWN, LOGIN, LOGOUT, SEND_TWEET, TIMELINES }; /*! This is the worker thread for our twittering! It is pretty stateless although it wraps a libTweet object which is not. */ class TweetWorker : public QObject, public QThread { Q_OBJECT public: TweetWorker () { httpHunter.setPattern("(https?://\\S*)"); tweet.setSource("kicktweet"); QThread::start(); } /*! Attempts to authenticate the provided credentials against the Twitter servers. \param _username The username to use. \param _password The password to use. */ void login (QString _username, QString _password) { #ifdef _KT_DEBUG std::cout << __PRETTY_FUNCTION__ << std::endl; #endif username = _username; password = _password; action = LOGIN; wait.wakeOne(); } /*! Attempts to send an update to Twitter. \param _update The Twitter status update. */ void sendTweet (QString _update) { #ifdef _KT_DEBUG std::cout << __PRETTY_FUNCTION__ << std::endl; #endif update = _update; action = SEND_TWEET; wait.wakeOne(); } /*! Used to shutdown this thread before closing the application. */ void shutdown () { #ifdef _KT_DEBUG std::cout << __PRETTY_FUNCTION__ << std::endl; #endif action = SHUTDOWN; wait.wakeOne(); } void logout () { #ifdef _KT_DEBUG std::cout << __PRETTY_FUNCTION__ << std::endl; #endif action = LOGOUT; wait.wakeOne(); } void getTimelines () { #ifdef _KT_DEBUG std::cout << __PRETTY_FUNCTION__ << std::endl; #endif action = TIMELINES; wait.wakeOne(); } QString getUsername () { return username; } signals: void loginFinished (vc::TweetResponse); void logoutFinished (vc::TweetResponse); void sendTweetFinished (vc::TweetResponse); void shutdownFinished (); void timelinesFinished (std::list); private: QWaitCondition wait; vc::Tweet tweet; TweetWorkerAction action; QRegExp httpHunter; QString username, password, update; QString convertUrlsToTinyUrls (QString input) { #ifdef _KT_DEBUG std::cout << __PRETTY_FUNCTION__ << std::endl; #endif int pos = 0; while (-1 != (pos = httpHunter.search(input,pos))) { std::string temp (httpHunter.cap(1).ascii()); if(tweet.tinyMyUrl(temp)) { if((int)temp.size() >= httpHunter.matchedLength()) { pos += httpHunter.matchedLength(); } else { input.replace(pos,httpHunter.matchedLength(),temp); pos += temp.size(); } } else { pos += httpHunter.matchedLength(); } } return input; } protected: void run () { #ifdef _KT_DEBUG std::cout << __PRETTY_FUNCTION__ << std::endl; #endif bool runLoop(true); while(runLoop) { wait.wait(); #ifdef _KT_DEBUG std::cout << __PRETTY_FUNCTION__ << " ACT!" << std::endl; #endif switch (action) { default: break; case SHUTDOWN: runLoop = false; break; case LOGIN: emit loginFinished(tweet.login(std::string(username.ascii()), std::string(password.ascii()))); break; case SEND_TWEET: emit sendTweetFinished(tweet.update(std::string(convertUrlsToTinyUrls(update).ascii()))); break; case LOGOUT: emit logoutFinished(tweet.logout()); break; case TIMELINES: emit timelinesFinished(tweet.getTimeline()); break; } } emit shutdownFinished (); } }; class KickTweet : public KPanelApplet { Q_OBJECT public: KickTweet(const QString& configFile, Type t = Normal, int actions = 0, QWidget *parent = 0, const char *name = 0); ~KickTweet(); virtual int widthForHeight(int height) const; virtual int heightForWidth(int width) const; virtual void about(); public slots: void tweetButtonEvent(); void doAbout (); void logout(); void login(); void sendTweet(); void showTimelines(); void cbLogin (vc::TweetResponse); void cbLogout (vc::TweetResponse); void cbSendTweet (vc::TweetResponse); void cbShutdownFinished (); void cbShowTimelines (std::list); protected: void resizeEvent(QResizeEvent *); private: TweetWorker worker; bool busy; KPopupMenu menu; bool loggedIn; QString username, password; void notify(QString); QPixmap px; KConfig * ksConfig; QWidget * mainView; QPushButton * tweetButton; QHBox * mainBox; QLineEdit * tweetText; QString tweetTemp; }; #endif