void ChatDialog::appendChatMessage(const QString& nick, const QString& text, time_t timestamp) { QTextCharFormat nickFormat; nickFormat.setForeground(Qt::darkGreen); nickFormat.setFontWeight(QFont::Bold); nickFormat.setFontUnderline(true); nickFormat.setUnderlineColor(Qt::gray); // Print who & when QTextCursor cursor(ui->textEdit->textCursor()); cursor.movePosition(QTextCursor::End); QTextTableFormat tableFormat; tableFormat.setBorder(0); QTextTable *table = cursor.insertTable(1, 2, tableFormat); QString from = QString("%1 ").arg(nick); QTextTableCell fromCell = table->cellAt(0, 0); fromCell.setFormat(nickFormat); fromCell.firstCursorPosition().insertText(from); printTimeInCell(table, timestamp); // Print what QTextCursor nextCursor(ui->textEdit->textCursor()); nextCursor.movePosition(QTextCursor::End); table = nextCursor.insertTable(1, 1, tableFormat); table->cellAt(0, 0).firstCursorPosition().insertText(text); // Popup notification showMessage(from, text); QScrollBar *bar = ui->textEdit->verticalScrollBar(); bar->setValue(bar->maximum()); }
Buffer::CursorId Buffer::createCursor() { CursorId res = nextCursor(); cursors[res].bucket = startBucket; cursors[res].bucketIdx = 0; cursors[res].bucketOffs = 0; return res; }
void SocketMultiplexer::serviceThread(void*) { std::vector<IArchNetwork::PollEntry> pfds; IArchNetwork::PollEntry pfd; // service the connections for (;;) { Thread::testCancel(); // wait until there are jobs to handle { Lock lock(m_mutex); while (!(bool)*m_jobsReady) { m_jobsReady->wait(); } } // lock the job list lockJobListLock(); lockJobList(); // collect poll entries if (m_update) { m_update = false; pfds.clear(); pfds.reserve(m_socketJobMap.size()); JobCursor cursor = newCursor(); JobCursor jobCursor = nextCursor(cursor); while (jobCursor != m_socketJobs.end()) { ISocketMultiplexerJob* job = *jobCursor; if (job != NULL) { pfd.m_socket = job->getSocket(); pfd.m_events = 0; if (job->isReadable()) { pfd.m_events |= IArchNetwork::kPOLLIN; } if (job->isWritable()) { pfd.m_events |= IArchNetwork::kPOLLOUT; } pfds.push_back(pfd); } jobCursor = nextCursor(cursor); } deleteCursor(cursor); } int status; try { // check for status if (!pfds.empty()) { status = ARCH->pollSocket(&pfds[0], (int)pfds.size(), -1); } else { status = 0; } } catch (XArchNetwork& e) { LOG((CLOG_WARN "error in socket multiplexer: %s", e.what())); status = 0; } if (status != 0) { // iterate over socket jobs, invoking each and saving the // new job. UInt32 i = 0; JobCursor cursor = newCursor(); JobCursor jobCursor = nextCursor(cursor); while (i < pfds.size() && jobCursor != m_socketJobs.end()) { if (*jobCursor != NULL) { // get poll state unsigned short revents = pfds[i].m_revents; bool read = ((revents & IArchNetwork::kPOLLIN) != 0); bool write = ((revents & IArchNetwork::kPOLLOUT) != 0); bool error = ((revents & (IArchNetwork::kPOLLERR | IArchNetwork::kPOLLNVAL)) != 0); // run job ISocketMultiplexerJob* job = *jobCursor; ISocketMultiplexerJob* newJob = job->run(read, write, error); // save job, if different if (newJob != job) { Lock lock(m_mutex); delete job; *jobCursor = newJob; m_update = true; } ++i; } // next job jobCursor = nextCursor(cursor); } deleteCursor(cursor); } // delete any removed socket jobs for (SocketJobMap::iterator i = m_socketJobMap.begin(); i != m_socketJobMap.end();) { if (*(i->second) == NULL) { m_socketJobMap.erase(i++); m_update = true; } else { ++i; } } // unlock the job list unlockJobList(); } }
Buffer::CursorId Buffer::createCursor(Buffer::CursorId ref) { CursorId res = nextCursor(); cursors[res] = cursors[ref]; return res; }
int main( int argc, char* argv[] ) { /* Get username and password from command line args */ std::string userName( "" ); std::string passWord( "" ); /*if( argc > 4 ) { for( int i = 1; i < argc; i += 2 ) { if( 0 == strncmp( argv[i], "-u", strlen("-u") ) ) { userName = argv[i+1]; } else if( 0 == strncmp( argv[i], "-p", strlen("-p") ) ) { passWord = argv[i+1]; } } if( ( 0 == userName.length() ) || ( 0 == passWord.length() ) ) { printUsage(); return 0; } } else { printUsage(); return 0; }*/ twitCurl twitterObj; std::string tmpStr, tmpStr2; std::string replyMsg; char tmpBuf[1024]; /* Set twitter username and password */ twitterObj.setTwitterUsername( userName ); twitterObj.setTwitterPassword( passWord ); /* Set proxy server usename, password, IP and port (if present) */ memset( tmpBuf, 0, 1024 ); printf( "\nDo you have a proxy server configured (0 for no; 1 for yes): " ); gets( tmpBuf ); tmpStr = tmpBuf; if( std::string::npos != tmpStr.find( "1" ) ) { memset( tmpBuf, 0, 1024 ); printf( "\nEnter proxy server IP: " ); gets( tmpBuf ); tmpStr = tmpBuf; twitterObj.setProxyServerIp( tmpStr ); memset( tmpBuf, 0, 1024 ); printf( "\nEnter proxy server port: " ); gets( tmpBuf ); tmpStr = tmpBuf; twitterObj.setProxyServerPort( tmpStr ); memset( tmpBuf, 0, 1024 ); printf( "\nEnter proxy server username: "******"\nEnter proxy server password: "******"vlC5S1NCMHHg8mD1ghPRkA" ) ); twitterObj.getOAuth().setConsumerSecret( std::string( "3w4cIrHyI3IYUZW5O2ppcFXmsACDaENzFdLIKmEU84" ) ); /* Step 1: Check if we alredy have OAuth access token from a previous run */ std::string myOAuthAccessTokenKey(""); std::string myOAuthAccessTokenSecret(""); std::ifstream oAuthTokenKeyIn; std::ifstream oAuthTokenSecretIn; oAuthTokenKeyIn.open( "twitterClient_token_key.txt" ); oAuthTokenSecretIn.open( "twitterClient_token_secret.txt" ); memset( tmpBuf, 0, 1024 ); oAuthTokenKeyIn >> tmpBuf; myOAuthAccessTokenKey = tmpBuf; memset( tmpBuf, 0, 1024 ); oAuthTokenSecretIn >> tmpBuf; myOAuthAccessTokenSecret = tmpBuf; oAuthTokenKeyIn.close(); oAuthTokenSecretIn.close(); if( myOAuthAccessTokenKey.size() && myOAuthAccessTokenSecret.size() ) { /* If we already have these keys, then no need to go through auth again */ printf( "\nUsing:\nKey: %s\nSecret: %s\n\n", myOAuthAccessTokenKey.c_str(), myOAuthAccessTokenSecret.c_str() ); twitterObj.getOAuth().setOAuthTokenKey( myOAuthAccessTokenKey ); twitterObj.getOAuth().setOAuthTokenSecret( myOAuthAccessTokenSecret ); } else { /* Step 2: Get request token key and secret */ std::string authUrl; twitterObj.oAuthRequestToken( authUrl ); /* Step 3: Get PIN */ memset( tmpBuf, 0, 1024 ); printf( "\nDo you want to visit twitter.com for PIN (0 for no; 1 for yes): " ); gets( tmpBuf ); tmpStr = tmpBuf; if( std::string::npos != tmpStr.find( "1" ) ) { /* Ask user to visit twitter.com auth page and get PIN */ memset( tmpBuf, 0, 1024 ); printf( "\nPlease visit this link in web browser and authorize this application:\n%s", authUrl.c_str() ); printf( "\nEnter the PIN provided by twitter: " ); gets( tmpBuf ); tmpStr = tmpBuf; twitterObj.getOAuth().setOAuthPin( tmpStr ); } else { /* Else, pass auth url to twitCurl and get it via twitCurl PIN handling */ twitterObj.oAuthHandlePIN( authUrl ); } /* Step 4: Exchange request token with access token */ twitterObj.oAuthAccessToken(); /* Step 5: Now, save this access token key and secret for future use without PIN */ twitterObj.getOAuth().getOAuthTokenKey( myOAuthAccessTokenKey ); twitterObj.getOAuth().getOAuthTokenSecret( myOAuthAccessTokenSecret ); /* Step 6: Save these keys in a file or wherever */ std::ofstream oAuthTokenKeyOut; std::ofstream oAuthTokenSecretOut; oAuthTokenKeyOut.open( "twitterClient_token_key.txt" ); oAuthTokenSecretOut.open( "twitterClient_token_secret.txt" ); oAuthTokenKeyOut.clear(); oAuthTokenSecretOut.clear(); oAuthTokenKeyOut << myOAuthAccessTokenKey.c_str(); oAuthTokenSecretOut << myOAuthAccessTokenSecret.c_str(); oAuthTokenKeyOut.close(); oAuthTokenSecretOut.close(); } /* OAuth flow ends */ /* Account credentials verification */ if( twitterObj.accountVerifyCredGet() ) { twitterObj.getLastWebResponse( replyMsg ); printf( "\ntwitterClient:: twitCurl::accountVerifyCredGet web response:\n%s\n", replyMsg.c_str() ); } else { twitterObj.getLastCurlError( replyMsg ); printf( "\ntwitterClient:: twitCurl::accountVerifyCredGet error:\n%s\n", replyMsg.c_str() ); } /* Get followers' ids */ std::string nextCursor(""); std::string searchUser("nextbigwhat"); do { if( twitterObj.followersIdsGet( nextCursor, searchUser ) ) { twitterObj.getLastWebResponse( replyMsg ); printf( "\ntwitterClient:: twitCurl::followersIdsGet for user [%s] web response:\n%s\n", searchUser.c_str(), replyMsg.c_str() ); // JSON: "next_cursor":1422208797779779359, nextCursor = ""; size_t nNextCursorStart = replyMsg.find("next_cursor"); if( std::string::npos == nNextCursorStart ) { nNextCursorStart += strlen("next_cursor:\""); size_t nNextCursorEnd = replyMsg.substr(nNextCursorStart).find(","); if( std::string::npos != nNextCursorEnd ) { nextCursor = replyMsg.substr(nNextCursorStart, (nNextCursorEnd - nNextCursorStart)); printf("\nNEXT CURSOR: %s\n\n\n\n\n", nextCursor.c_str()); } } } else { twitterObj.getLastCurlError( replyMsg ); printf( "\ntwitterClient:: twitCurl::followersIdsGet error:\n%s\n", replyMsg.c_str() ); break; } } while( !nextCursor.empty() && nextCursor.compare("0") ); /* Get block list */ nextCursor = ""; if( twitterObj.blockListGet( nextCursor, false, false ) ) { twitterObj.getLastWebResponse( replyMsg ); printf( "\ntwitterClient:: twitCurl::blockListGet web response:\n%s\n", replyMsg.c_str() ); } else { twitterObj.getLastCurlError( replyMsg ); printf( "\ntwitterClient:: twitCurl::blockListGet error:\n%s\n", replyMsg.c_str() ); } /* Get blocked ids */ nextCursor = ""; if( twitterObj.blockIdsGet( nextCursor, true ) ) { twitterObj.getLastWebResponse( replyMsg ); printf( "\ntwitterClient:: twitCurl::blockIdsGet web response:\n%s\n", replyMsg.c_str() ); } else { twitterObj.getLastCurlError( replyMsg ); printf( "\ntwitterClient:: twitCurl::blockIdsGet error:\n%s\n", replyMsg.c_str() ); } /* Post a new status message */ memset( tmpBuf, 0, 1024 ); printf( "\nEnter a new status message: " ); gets( tmpBuf ); tmpStr = tmpBuf; replyMsg = ""; if( twitterObj.statusUpdate( tmpStr ) ) { twitterObj.getLastWebResponse( replyMsg ); printf( "\ntwitterClient:: twitCurl::statusUpdate web response:\n%s\n", replyMsg.c_str() ); } else { twitterObj.getLastCurlError( replyMsg ); printf( "\ntwitterClient:: twitCurl::statusUpdate error:\n%s\n", replyMsg.c_str() ); } /* Post a new reply */ memset( tmpBuf, 0, 1024 ); printf( "\nEnter message id to reply to : " ); gets( tmpBuf ); tmpStr2 = tmpBuf; memset( tmpBuf, 0, 1024 ); printf( "\nEnter a reply message: " ); gets( tmpBuf ); tmpStr = tmpBuf; replyMsg = ""; if( twitterObj.statusUpdate( tmpStr, tmpStr2 ) ) { twitterObj.getLastWebResponse( replyMsg ); printf( "\ntwitterClient:: twitCurl::statusUpdate web response:\n%s\n", replyMsg.c_str() ); } else { twitterObj.getLastCurlError( replyMsg ); printf( "\ntwitterClient:: twitCurl::statusUpdate error:\n%s\n", replyMsg.c_str() ); } /* Search a string */ printf( "\nEnter string to search: " ); memset( tmpBuf, 0, 1024 ); gets( tmpBuf ); tmpStr = tmpBuf; printf( "\nLimit search results to: " ); memset( tmpBuf, 0, 1024 ); gets( tmpBuf ); tmpStr2 = tmpBuf; replyMsg = ""; if( twitterObj.search( tmpStr, tmpStr2 ) ) { twitterObj.getLastWebResponse( replyMsg ); printf( "\ntwitterClient:: twitCurl::search web response:\n%s\n", replyMsg.c_str() ); } else { twitterObj.getLastCurlError( replyMsg ); printf( "\ntwitterClient:: twitCurl::search error:\n%s\n", replyMsg.c_str() ); } #ifdef _TWITCURL_TEST_ /* Get user timeline */ replyMsg = ""; printf( "\nGetting user timeline\n" ); if( twitterObj.timelineUserGet( true, true, 0 ) ) { twitterObj.getLastWebResponse( replyMsg ); printf( "\ntwitterClient:: twitCurl::timelineUserGet web response:\n%s\n", replyMsg.c_str() ); } else { twitterObj.getLastCurlError( replyMsg ); printf( "\ntwitterClient:: twitCurl::timelineUserGet error:\n%s\n", replyMsg.c_str() ); } /* Destroy a status message */ memset( tmpBuf, 0, 1024 ); printf( "\nEnter status message id to delete: " ); gets( tmpBuf ); tmpStr = tmpBuf; replyMsg = ""; if( twitterObj.statusDestroyById( tmpStr ) ) { twitterObj.getLastWebResponse( replyMsg ); printf( "\ntwitterClient:: twitCurl::statusDestroyById web response:\n%s\n", replyMsg.c_str() ); } else { twitterObj.getLastCurlError( replyMsg ); printf( "\ntwitterClient:: twitCurl::statusDestroyById error:\n%s\n", replyMsg.c_str() ); } /* Get public timeline */ replyMsg = ""; printf( "\nGetting public timeline\n" ); if( twitterObj.timelinePublicGet() ) { twitterObj.getLastWebResponse( replyMsg ); printf( "\ntwitterClient:: twitCurl::timelinePublicGet web response:\n%s\n", replyMsg.c_str() ); } else { twitterObj.getLastCurlError( replyMsg ); printf( "\ntwitterClient:: twitCurl::timelinePublicGet error:\n%s\n", replyMsg.c_str() ); } /* Get friend ids */ replyMsg = ""; printf( "\nGetting friend ids\n" ); tmpStr = "techcrunch"; if( twitterObj.friendsIdsGet( tmpStr, false ) ) { twitterObj.getLastWebResponse( replyMsg ); printf( "\ntwitterClient:: twitCurl::friendsIdsGet web response:\n%s\n", replyMsg.c_str() ); } else { twitterObj.getLastCurlError( replyMsg ); printf( "\ntwitterClient:: twitCurl::friendsIdsGet error:\n%s\n", replyMsg.c_str() ); } /* Get trends */ if( twitterObj.trendsDailyGet() ) { twitterObj.getLastWebResponse( replyMsg ); printf( "\ntwitterClient:: twitCurl::trendsDailyGet web response:\n%s\n", replyMsg.c_str() ); } else { twitterObj.getLastCurlError( replyMsg ); printf( "\ntwitterClient:: twitCurl::trendsDailyGet error:\n%s\n", replyMsg.c_str() ); } #endif // _TWITCURL_TEST_ return 0; }