Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

stocktickerpluginwidget.cpp

Go to the documentation of this file.
00001   /*
00002    * stocktickerpluginwidget.cpp
00003    *
00004    * copyright   : (c) 2002 by L.J. Potter
00005    * email       : llornkcor@handhelds.org
00006    *
00007    */
00008   /***************************************************************************
00009    *                                                                         *
00010    *   This program is free software; you can redistribute it and/or modify  *
00011    *   it under the terms of the GNU General Public License as published by  *
00012    *   the Free Software Foundation; either version 2 of the License, or     *
00013    *   (at your option) any later version.                                   *
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   //    stocktickerTicker->setText( "Downloading stock data.");
00043       stock *stocks_quotes=NULL;
00044       stock *stocks_tmp;
00045       stock_liste = blah;
00046       ::free ( blah );
00047       //    char *stock_liste = (char *)blah->latin1();
00048       //      odebug << "" << stock_liste.data() << "" << oendl; 
00049       output = "";
00050   QString tempString;
00051       libstocks_return_code error;
00052 
00053 //        Config cfg( "stockticker");
00054 //        cfg.setGroup( "Fields" );
00055 //        bool dotimeCheck, dodateCheck, dosymbolCheck, donameCheck;
00056 //        bool docurrentPriceCheck, dolastPriceCheck, doopenPriceCheck;
00057 //        bool dominPriceCheck, domaxPriceCheck, dovariationCheck, dovolumeCheck;
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 //        dotimeCheck=cfg.readBoolEntry("timeCheck",1);
00074 //        dodateCheck=cfg.readBoolEntry("dateCheck",1);
00075 //        dosymbolCheck=cfg.readBoolEntry("symbolCheck",1);
00076 //        donameCheck=cfg.readBoolEntry("nameCheck",1);
00077 //        docurrentPriceCheck=cfg.readBoolEntry("currentPriceCheck",1);
00078 //        dolastPriceCheck=cfg.readBoolEntry("lastPriceCheck",1);
00079 //        doopenPriceCheck=cfg.readBoolEntry("openPriceCheck",1);
00080 //        dominPriceCheck=cfg.readBoolEntry("minPriceCheck",1);
00081 //        domaxPriceCheck=cfg.readBoolEntry("maxPriceCheck",1);
00082 //        dovariationCheck=cfg.readBoolEntry("variationCheck",1);
00083 //        dovolumeCheck=cfg.readBoolEntry("volumeCheck",1);
00084 
00085   //    DefProxy();
00086       // {
00087        char *proxy;
00088        //       libstocks_return_code error;
00089 
00090          /* Proxy support */
00091          /* Checks for "http_proxy" environment variable */
00092        proxy = getenv("http_proxy");
00093        if(proxy) {
00094              /* printf("proxy set\n"); */
00095            error = set_proxy(proxy);
00096            if (error) {
00097 //                 printf("Proxy error (%d)\n", error);
00098 //                 QString tempString2;
00099 //                 tempString2.sprintf("Proxy error (%d)\n", error);
00100 //                 output = tempString2;
00101                //               delete tempString2;
00102                return;
00103   //             exit(1);
00104            }
00105        }
00106        ::free(proxy);
00107        //  }
00108         /* Get the stocks and process errors */
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         /* Displays the stocks */
00121       while(stocks_tmp!=0){
00122 
00123           if (stocks_tmp->Time) {
00124   //            printf("%s ", stocks_tmp->Time);
00125               tempString.sprintf("|  %s  ", stocks_tmp->Time);
00126               tempString.replace(QRegExp("\""),"");
00127               if( dotimeCheck)
00128                   output +=tempString;
00129           }
00130           if (stocks_tmp->Date) {
00131   //             printf("%s ", stocks_tmp->Date);
00132               tempString.sprintf("|  %s  ", stocks_tmp->Date);
00133               tempString.replace(QRegExp("\""),"");
00134               if(dodateCheck)
00135                   output +=tempString;
00136           }
00137   //         printf("\n");
00138 
00139   //         printf("----------------------------------------\n");
00140 
00141           if ( strlen(stocks_tmp->Symbol) > 20 ) {
00142   //             printf("| Symbol    | %.20s     |\n",stocks_tmp->Symbol);
00143               tempString.sprintf("|  Symbol %s  ",stocks_tmp->Symbol);
00144               if(dosymbolCheck)
00145                   output +=tempString;
00146           }
00147           else {
00148   //             printf("| Symbol    | %-20s |\n",stocks_tmp->Symbol);
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   //                 printf("| Name %.20s  |\n",stocks_tmp->Name);
00157                   tempString.sprintf("|  Name %s  ",stocks_tmp->Name);
00158                   tempString.stripWhiteSpace();
00159                   if(donameCheck)
00160                       output +=tempString;
00161               } else {
00162   //                 printf("| Name      | %-20s     |\n",stocks_tmp->Name);
00163                   tempString.sprintf("|  Name %s  ",stocks_tmp->Name);
00164                   tempString.stripWhiteSpace();
00165                   if(donameCheck)
00166                       output +=tempString;
00167               }
00168           }
00169           else {
00170   //             printf("| Name      |                          |\n");
00171               tempString.sprintf("|  Name  | |");
00172               if(donameCheck)
00173                   output +=tempString;
00174           }
00175 
00176   //         printf("| Price     | %-7.2f                  |\n", stocks_tmp->CurrentPrice);
00177           tempString.sprintf("|  Price %-7.2f  ", stocks_tmp->CurrentPrice);
00178           if(docurrentPriceCheck)
00179               output +=tempString;
00180 
00181   //         printf("| Yesterday | %-7.2f                  |\n",stocks_tmp->LastPrice);
00182           tempString.sprintf("|  Yesterday  %-7.2f  ",stocks_tmp->LastPrice);
00183           if(dolastPriceCheck)
00184               output +=tempString;
00185 
00186   //         printf("| Open      | %-7.2f                  |\n",stocks_tmp->OpenPrice);
00187           tempString.sprintf("|  Open %-7.2f  ",stocks_tmp->OpenPrice);
00188           if(doopenPriceCheck)
00189               output +=tempString;
00190 
00191   //         printf("| Min       | %-7.2f                  |\n", stocks_tmp->MinPrice);
00192           tempString.sprintf("|  Min %-7.2f  ", stocks_tmp->MinPrice);
00193           if(dominPriceCheck)
00194               output +=tempString;
00195 
00196   //         printf("| Max       | %-7.2f                  |\n",stocks_tmp->MaxPrice);
00197           tempString.sprintf("|  Max %-7.2f  ",stocks_tmp->MaxPrice);
00198           if(domaxPriceCheck)
00199               output +=tempString;
00200 
00201   //         printf("| Var       | %-6.2f (%5.2f %%)         |\n", stocks_tmp->Variation, stocks_tmp->Pourcentage);
00202           tempString.sprintf("|  Var %-6.2f (%5.2f %%)  ", stocks_tmp->Variation, stocks_tmp->Pourcentage);
00203           if(dovariationCheck)
00204                   output +=tempString;
00205 
00206   //         printf("| Volume    | %-9d                |\n", stocks_tmp->Volume);
00207           tempString.sprintf("|  Volume %-9d  ", stocks_tmp->Volume);
00208           if(dovolumeCheck)
00209               output +=tempString;
00210 
00211   //         printf("----------------------------------------\n\n");
00212           tempString.sprintf("||==++==|");
00213           output +=tempString;
00214 
00215             /* Simple function which help to browse in the stocks list */
00216           stocks_tmp = next_stock(stocks_tmp);
00217       }
00218 
00219       stocktickerTicker->setText( output.latin1() );
00220 
00221         /* frees stocks */
00222       free_stocks(stocks_quotes);
00223       free_stocks(stocks_tmp);
00224 
00225       stock_liste="";
00226       //delete stock_liste;
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   //    checkConnection();
00238   }
00239 
00240   StockTickerPluginWidget::~StockTickerPluginWidget() {
00241   }
00242 
00243   void StockTickerPluginWidget::init() {
00244 
00245       QHBoxLayout* layout = new QHBoxLayout( this );
00246       stocktickerTicker = new OTicker(this);
00247   //    stocktickerTicker->setMinimumHeight(15);
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(" "),"+");//seperated by +
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             //::free((void*)thread1);
00271              //getStocks(blah);
00272       }
00273   }
00274 
00275   void StockTickerPluginWidget::timerEvent( QTimerEvent *e ) {
00276       killTimer(e->timerId());
00277       checkConnection();
00278   }
00279 
00280   void StockTickerPluginWidget::checkConnection() {
00281   //     odebug << "checking connection" << oendl; 
00282   //     Sock = new QSocket( this );
00283 
00284   //      if( wasError)
00285   //          stocktickerTicker->setText("Checking connection");
00286 
00287   //     if(Sock->state() == QSocket::Idle) {
00288   //         Sock->connectToHost("finance.yahoo.com", 80);
00289   //         connect( Sock, SIGNAL( error(int) ),  SLOT(socketError(int)) );
00290   //         connect( Sock, SIGNAL( hostFound() ), SLOT(isConnected()) );
00291   //     } else {
00292   //         odebug << "State is not Idle" << oendl; 
00293           isConnected();
00294   //    }
00295   }
00296 
00297   void StockTickerPluginWidget::isConnected() {
00298   //    odebug << "We connect, so ok to grab stocks" << oendl; 
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       //      odebug << "timer set for " << (timerDelay*60000)/60000 << "" << oendl; 
00308       wasError = false;
00309 
00310   //    Sock->close();
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   //    Sock->close();
00328 
00329   }

Generated on Sat Nov 5 16:18:01 2005 for OPIE by  doxygen 1.4.2