00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <opie2/oticker.h>
00018
00019 #include <qpe/config.h>
00020
00021 #include <qlayout.h>
00022
00023 using namespace Opie::Ui;
00024 using namespace Opie::Ui;
00025 extern "C" {
00026 #include "libstocks/stocks.h"
00027 }
00028
00029 #include <pthread.h>
00030
00031 #include "stocktickerpluginwidget.h"
00032
00033 QString output;
00034 OTicker *stocktickerTicker;
00035 QCString stock_liste;
00036 bool dotimeCheck, dodateCheck, dosymbolCheck, donameCheck;
00037 bool docurrentPriceCheck, dolastPriceCheck, doopenPriceCheck;
00038 bool dominPriceCheck, domaxPriceCheck, dovariationCheck, dovolumeCheck;
00039
00040 void getStocks(char *blah) {
00041
00042
00043 stock *stocks_quotes=NULL;
00044 stock *stocks_tmp;
00045 stock_liste = blah;
00046 ::free ( blah );
00047
00048
00049 output = "";
00050 QString tempString;
00051 libstocks_return_code error;
00052
00053
00054
00055
00056
00057
00058
00059 dotimeCheck=dodateCheck=dosymbolCheck=donameCheck= docurrentPriceCheck=dolastPriceCheck=doopenPriceCheck=dominPriceCheck=domaxPriceCheck=dovariationCheck=dovolumeCheck=false;
00060
00061 dotimeCheck=1;
00062 dodateCheck=1;
00063 dosymbolCheck=1;
00064 donameCheck=1;
00065 docurrentPriceCheck=1;
00066 dolastPriceCheck=1;
00067 doopenPriceCheck=1;
00068 dominPriceCheck=1;
00069 domaxPriceCheck=1;
00070 dovariationCheck=1;
00071 dovolumeCheck=1;
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087 char *proxy;
00088
00089
00090
00091
00092 proxy = getenv("http_proxy");
00093 if(proxy) {
00094
00095 error = set_proxy(proxy);
00096 if (error) {
00097
00098
00099
00100
00101
00102 return;
00103
00104 }
00105 }
00106 ::free(proxy);
00107
00108
00109 error = get_stocks( stock_liste.data(), &stocks_quotes);
00110
00111 if (error) {
00112 printf("Error in getting stocks (%d)\n", error);
00113 tempString.sprintf("Error in getting stocks (%d)\n", error);
00114 output =tempString;
00115 return;
00116 }
00117
00118 stocks_tmp = stocks_quotes;
00119
00120
00121 while(stocks_tmp!=0){
00122
00123 if (stocks_tmp->Time) {
00124
00125 tempString.sprintf("| %s ", stocks_tmp->Time);
00126 tempString.replace(QRegExp("\""),"");
00127 if( dotimeCheck)
00128 output +=tempString;
00129 }
00130 if (stocks_tmp->Date) {
00131
00132 tempString.sprintf("| %s ", stocks_tmp->Date);
00133 tempString.replace(QRegExp("\""),"");
00134 if(dodateCheck)
00135 output +=tempString;
00136 }
00137
00138
00139
00140
00141 if ( strlen(stocks_tmp->Symbol) > 20 ) {
00142
00143 tempString.sprintf("| Symbol %s ",stocks_tmp->Symbol);
00144 if(dosymbolCheck)
00145 output +=tempString;
00146 }
00147 else {
00148
00149 tempString.sprintf("| Symbol %s ",stocks_tmp->Symbol);
00150 if(dosymbolCheck)
00151 output +=tempString;
00152 }
00153
00154 if (stocks_tmp->Name) {
00155 if ( strlen(stocks_tmp->Name) > 20 ) {
00156
00157 tempString.sprintf("| Name %s ",stocks_tmp->Name);
00158 tempString.stripWhiteSpace();
00159 if(donameCheck)
00160 output +=tempString;
00161 } else {
00162
00163 tempString.sprintf("| Name %s ",stocks_tmp->Name);
00164 tempString.stripWhiteSpace();
00165 if(donameCheck)
00166 output +=tempString;
00167 }
00168 }
00169 else {
00170
00171 tempString.sprintf("| Name | |");
00172 if(donameCheck)
00173 output +=tempString;
00174 }
00175
00176
00177 tempString.sprintf("| Price %-7.2f ", stocks_tmp->CurrentPrice);
00178 if(docurrentPriceCheck)
00179 output +=tempString;
00180
00181
00182 tempString.sprintf("| Yesterday %-7.2f ",stocks_tmp->LastPrice);
00183 if(dolastPriceCheck)
00184 output +=tempString;
00185
00186
00187 tempString.sprintf("| Open %-7.2f ",stocks_tmp->OpenPrice);
00188 if(doopenPriceCheck)
00189 output +=tempString;
00190
00191
00192 tempString.sprintf("| Min %-7.2f ", stocks_tmp->MinPrice);
00193 if(dominPriceCheck)
00194 output +=tempString;
00195
00196
00197 tempString.sprintf("| Max %-7.2f ",stocks_tmp->MaxPrice);
00198 if(domaxPriceCheck)
00199 output +=tempString;
00200
00201
00202 tempString.sprintf("| Var %-6.2f (%5.2f %%) ", stocks_tmp->Variation, stocks_tmp->Pourcentage);
00203 if(dovariationCheck)
00204 output +=tempString;
00205
00206
00207 tempString.sprintf("| Volume %-9d ", stocks_tmp->Volume);
00208 if(dovolumeCheck)
00209 output +=tempString;
00210
00211
00212 tempString.sprintf("||==++==|");
00213 output +=tempString;
00214
00215
00216 stocks_tmp = next_stock(stocks_tmp);
00217 }
00218
00219 stocktickerTicker->setText( output.latin1() );
00220
00221
00222 free_stocks(stocks_quotes);
00223 free_stocks(stocks_tmp);
00224
00225 stock_liste="";
00226
00227 tempString="";
00228 output="";
00229 }
00230
00231 StockTickerPluginWidget::StockTickerPluginWidget( QWidget *parent, const char* name)
00232 : QWidget(parent, name ) {
00233 init();
00234 startTimer(1000);
00235
00236 stocktickerTicker->setTextFormat(Qt::RichText);
00237
00238 }
00239
00240 StockTickerPluginWidget::~StockTickerPluginWidget() {
00241 }
00242
00243 void StockTickerPluginWidget::init() {
00244
00245 QHBoxLayout* layout = new QHBoxLayout( this );
00246 stocktickerTicker = new OTicker(this);
00247
00248 connect( stocktickerTicker, SIGNAL( mousePressed()), this, SLOT( checkConnection() ));
00249 layout->addWidget( stocktickerTicker);
00250 wasError = true;
00251 }
00252
00253 void StockTickerPluginWidget::doStocks() {
00254 Config cfg( "stockticker");
00255 cfg.setGroup( "Symbols" );
00256 symbollist="";
00257 symbollist = cfg.readEntry("Symbols", "");
00258 symbollist.replace(QRegExp(" "),"+");
00259
00260 cfg.setGroup("Timer");
00261 stocktickerTicker->setUpdateTime( cfg.readNumEntry("ScrollSpeed",50));
00262 stocktickerTicker->setScrollLength( cfg.readNumEntry("ScrollLength",10));
00263
00264 if (!symbollist.isEmpty()) {
00265 pthread_t thread1;
00266 char *blah = ::strdup(symbollist.latin1());
00267 pthread_create( &thread1, NULL, (void * (*)(void *))getStocks, (void *) blah);
00268 pthread_detach( thread1);
00269
00270
00271
00272 }
00273 }
00274
00275 void StockTickerPluginWidget::timerEvent( QTimerEvent *e ) {
00276 killTimer(e->timerId());
00277 checkConnection();
00278 }
00279
00280 void StockTickerPluginWidget::checkConnection() {
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293 isConnected();
00294
00295 }
00296
00297 void StockTickerPluginWidget::isConnected() {
00298
00299 if(this->isVisible())
00300 doStocks();
00301
00302 Config cfg( "stockticker");
00303 cfg.setGroup("Timer");
00304 timerDelay= cfg.readNumEntry("Delay",0);
00305 if(timerDelay > 0)
00306 startTimer(timerDelay*60000);
00307
00308 wasError = false;
00309
00310
00311 }
00312
00313 void StockTickerPluginWidget::socketError(int errcode) {
00314 switch(errcode) {
00315 case QSocket::ErrConnectionRefused:
00316 output = tr("Connection refused.");
00317 break;
00318 case QSocket::ErrHostNotFound:
00319 output = tr("Could not find server.");
00320 break;
00321 case QSocket::ErrSocketRead :
00322 output = tr("Socket read error.");
00323 break;
00324 };
00325 stocktickerTicker->setText( output );
00326 wasError = true;
00327
00328
00329 }