File indexing completed on 2025-03-09 03:54:51
0001 #include <QApplication> 0002 #include <QStringList> 0003 #include <QTimer> 0004 #include <QDebug> 0005 0006 #include "tweeter.h" 0007 0008 const char OPT_OAUTH[] = "-o"; 0009 const char OPT_XAUTH[] = "-x"; 0010 const char OPT_USERNAME[] = "-u"; 0011 const char OPT_PASSWORD[] = "-p"; 0012 const char OPT_STATUS[] = "-m"; 0013 0014 const char USAGE[] = "\n" 0015 "Usage: tweetdemo [OPTION]...\n" 0016 "Get OAuth access tokens from Twitter's OAuth service and " 0017 "(optionally) post a status update on a user's timeline\n" 0018 "\nOptions:\n" 0019 " %1\t\tLink with Twitter OAuth service, i.e get access tokens\n" 0020 " %2\t\tLink with Twitter XAuth service, i.e get access tokens using the XAuth protocol\n" 0021 " %3 <username>\tTwitter username to be used while using XAuth (-x option)\n" 0022 " %4 <password>\tTwitter password to be used while using XAuth (-x option)\n" 0023 " %5\t\tStatus update message, enclosed in double quotes\n"; 0024 0025 0026 class Helper : public QObject { 0027 Q_OBJECT 0028 0029 public: 0030 Helper() : QObject(), tweeter_(this), waitForMsg_(false), msg_(QString()) {} 0031 0032 public slots: 0033 void processArgs() { 0034 QStringList argList = qApp->arguments(); 0035 QByteArray help = QString(USAGE).arg(OPT_OAUTH, 0036 OPT_XAUTH, 0037 OPT_USERNAME, 0038 OPT_PASSWORD, 0039 OPT_STATUS).toLatin1(); 0040 const char *helpText = help.constData(); 0041 connect(&tweeter_, SIGNAL(linkingFailed()), this, SLOT(onLinkingFailed())); 0042 connect(&tweeter_, SIGNAL(linkingSucceeded()), this, SLOT(onLinkingSucceeded())); 0043 0044 if (argList.contains(OPT_OAUTH)) { 0045 if (argList.contains(OPT_STATUS)) { 0046 waitForMsg_ = true; 0047 msg_ = argList.at(argList.indexOf(OPT_STATUS) + 1); 0048 } 0049 // Start OAuth 0050 tweeter_.doOAuth(); 0051 } else if (argList.contains(OPT_XAUTH)) { 0052 if (!(argList.contains(OPT_USERNAME) && argList.contains(OPT_PASSWORD))) { 0053 qDebug() << "\nError: Username or Password missing!"; 0054 qDebug() << helpText; 0055 qApp->exit(1); 0056 } 0057 0058 QString username = argList.at(argList.indexOf(OPT_USERNAME) + 1); 0059 QString password = argList.at(argList.indexOf(OPT_PASSWORD) + 1); 0060 0061 if (argList.contains(OPT_STATUS)) { 0062 waitForMsg_ = true; 0063 msg_ = argList.at(argList.indexOf(OPT_STATUS) + 1); 0064 } 0065 // Start XAuth 0066 tweeter_.doXAuth(username, password); 0067 } else if (argList.contains(OPT_STATUS)) { 0068 QString statusMessage = argList.at(argList.indexOf(OPT_STATUS) + 1); 0069 postStatusUpdate(statusMessage); 0070 } else { 0071 qDebug() << helpText; 0072 qApp->exit(1); 0073 } 0074 } 0075 0076 void onLinkingFailed() { 0077 qDebug() << "Linking failed!"; 0078 qApp->exit(1); 0079 } 0080 0081 void onLinkingSucceeded() { 0082 qDebug() << "Linking succeeded!"; 0083 if (waitForMsg_) { 0084 postStatusUpdate(msg_); 0085 } else { 0086 qApp->quit(); 0087 } 0088 } 0089 0090 private slots: 0091 void postStatusUpdate(const QString& msg) { 0092 connect(&tweeter_, SIGNAL(statusPosted()), qApp, SLOT(quit())); 0093 tweeter_.postStatusUpdate(msg); 0094 } 0095 0096 private: 0097 Tweeter tweeter_; 0098 bool waitForMsg_; 0099 QString msg_; 0100 }; 0101 0102 int main(int argc, char *argv[]) { 0103 QApplication a(argc, argv); 0104 QCoreApplication::setOrganizationName("MySoft"); 0105 QCoreApplication::setOrganizationDomain("mysoft.com"); 0106 QCoreApplication::setApplicationName("tweeter"); 0107 Helper helper; 0108 QTimer::singleShot(0, &helper, SLOT(processArgs())); 0109 0110 return a.exec(); 0111 } 0112 0113 #include "main.moc"