Diffstat (limited to 'noncore/todayplugins/stockticker/stocktickerlib/stocktickerpluginwidget.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | noncore/todayplugins/stockticker/stocktickerlib/stocktickerpluginwidget.cpp | 183 |
1 files changed, 129 insertions, 54 deletions
diff --git a/noncore/todayplugins/stockticker/stocktickerlib/stocktickerpluginwidget.cpp b/noncore/todayplugins/stockticker/stocktickerlib/stocktickerpluginwidget.cpp index 8830472..572a720 100644 --- a/noncore/todayplugins/stockticker/stocktickerlib/stocktickerpluginwidget.cpp +++ b/noncore/todayplugins/stockticker/stocktickerlib/stocktickerpluginwidget.cpp | |||
@@ -13,8 +13,9 @@ | |||
13 | * (at your option) any later version. * | 13 | * (at your option) any later version. * |
14 | * * | 14 | * * |
15 | ***************************************************************************/ | 15 | ***************************************************************************/ |
16 | 16 | ||
17 | #include <qsocket.h> | ||
17 | #include <qvaluelist.h> | 18 | #include <qvaluelist.h> |
18 | #include <qtl.h> | 19 | #include <qtl.h> |
19 | #include <qstring.h> | 20 | #include <qstring.h> |
20 | #include <qstringlist.h> | 21 | #include <qstringlist.h> |
@@ -22,61 +23,34 @@ | |||
22 | #include <qlayout.h> | 23 | #include <qlayout.h> |
23 | #include <qlineedit.h> | 24 | #include <qlineedit.h> |
24 | #include <qregexp.h> | 25 | #include <qregexp.h> |
25 | #include <qtimer.h> | 26 | #include <qtimer.h> |
27 | #include <qmessagebox.h> | ||
26 | 28 | ||
27 | #include <qpe/config.h> | 29 | #include <qpe/config.h> |
28 | #include <qpe/timestring.h> | 30 | #include <qpe/timestring.h> |
29 | #include <qpe/qcopenvelope_qws.h> | 31 | #include <qpe/qcopenvelope_qws.h> |
32 | #include <qpe/network.h> | ||
30 | 33 | ||
31 | #include <opie/oticker.h> | 34 | #include <opie/oticker.h> |
32 | //#include "ticker.h" | ||
33 | 35 | ||
34 | extern "C" { | 36 | extern "C" { |
35 | #include "libstocks/stocks.h" | 37 | #include "libstocks/stocks.h" |
36 | } | 38 | } |
37 | 39 | ||
38 | #include "stocktickerpluginwidget.h" | 40 | #include <pthread.h> |
39 | |||
40 | StockTickerPluginWidget::StockTickerPluginWidget( QWidget *parent, const char* name) | ||
41 | : QWidget(parent, name ) { | ||
42 | init(); | ||
43 | startTimer(1000); | ||
44 | } | ||
45 | |||
46 | StockTickerPluginWidget::~StockTickerPluginWidget() { | ||
47 | } | ||
48 | |||
49 | void StockTickerPluginWidget::init() { | ||
50 | |||
51 | QHBoxLayout* layout = new QHBoxLayout( this ); | ||
52 | |||
53 | stocktickerTicker = new OTicker(this); | ||
54 | stocktickerTicker->setMinimumHeight(15); | ||
55 | connect( stocktickerTicker, SIGNAL( mousePressed()), this, SLOT( doStocks() )); | ||
56 | 41 | ||
57 | layout->addWidget( stocktickerTicker); | 42 | #include "stocktickerpluginwidget.h" |
58 | |||
59 | } | ||
60 | |||
61 | void StockTickerPluginWidget::doStocks() { | ||
62 | Config cfg( "stockticker"); | ||
63 | cfg.setGroup( "Symbols" ); | ||
64 | QString symbollist; | ||
65 | symbollist = cfg.readEntry("Symbols", ""); | ||
66 | symbollist.replace(QRegExp(" "),"+");//seperated by + | ||
67 | // qDebug(symbollist); | ||
68 | 43 | ||
69 | getStocks( symbollist.latin1()); | 44 | QString output; |
45 | OTicker *stocktickerTicker; | ||
70 | 46 | ||
71 | stocktickerTicker->setText( output ); | 47 | void getStocks( const QString *blah) { |
72 | } | ||
73 | |||
74 | void StockTickerPluginWidget::getStocks( const char *blah) { | ||
75 | 48 | ||
49 | // stocktickerTicker->setText( "Downloading stock data."); | ||
76 | stock *stocks_quotes=NULL; | 50 | stock *stocks_quotes=NULL; |
77 | stock *stocks_tmp; | 51 | stock *stocks_tmp; |
78 | 52 | qDebug("%s", blah->latin1()); | |
79 | QString tempString; | 53 | QString tempString; |
80 | output = ""; | 54 | output = ""; |
81 | 55 | ||
82 | libstocks_return_code error; | 56 | libstocks_return_code error; |
@@ -100,10 +74,31 @@ void StockTickerPluginWidget::getStocks( const char *blah) { | |||
100 | domaxPriceCheck=cfg.readBoolEntry("maxPriceCheck",1); | 74 | domaxPriceCheck=cfg.readBoolEntry("maxPriceCheck",1); |
101 | dovariationCheck=cfg.readBoolEntry("variationCheck",1); | 75 | dovariationCheck=cfg.readBoolEntry("variationCheck",1); |
102 | dovolumeCheck=cfg.readBoolEntry("volumeCheck",1); | 76 | dovolumeCheck=cfg.readBoolEntry("volumeCheck",1); |
103 | 77 | ||
104 | DefProxy(); | 78 | // DefProxy(); |
105 | char *stock_liste = (char *)blah; | 79 | { |
80 | char *proxy; | ||
81 | libstocks_return_code error; | ||
82 | |||
83 | /* Proxy support */ | ||
84 | /* Checks for "http_proxy" environment variable */ | ||
85 | proxy = getenv("http_proxy"); | ||
86 | if(proxy) { | ||
87 | /* printf("proxy set\n"); */ | ||
88 | error = set_proxy(proxy); | ||
89 | if (error) { | ||
90 | printf("Proxy error (%d)\n", error); | ||
91 | QString tempString; | ||
92 | tempString.sprintf("Proxy error (%d)\n", error); | ||
93 | output = tempString; | ||
94 | return; | ||
95 | // exit(1); | ||
96 | } | ||
97 | } | ||
98 | } | ||
99 | char *stock_liste = (char *)blah->latin1(); | ||
100 | // char *stock_liste = (char *)blah; | ||
106 | /* Get the stocks and process errors */ | 101 | /* Get the stocks and process errors */ |
107 | error = get_stocks( stock_liste, &stocks_quotes); | 102 | error = get_stocks( stock_liste, &stocks_quotes); |
108 | 103 | ||
109 | if (error) { | 104 | if (error) { |
@@ -216,39 +211,119 @@ void StockTickerPluginWidget::getStocks( const char *blah) { | |||
216 | } | 211 | } |
217 | 212 | ||
218 | /* frees stocks */ | 213 | /* frees stocks */ |
219 | free_stocks(stocks_quotes); | 214 | free_stocks(stocks_quotes); |
215 | stocktickerTicker->setText( output ); | ||
220 | 216 | ||
221 | } | 217 | } |
222 | 218 | ||
223 | void StockTickerPluginWidget::DefProxy(void) { | 219 | StockTickerPluginWidget::StockTickerPluginWidget( QWidget *parent, const char* name) |
224 | char *proxy; | 220 | : QWidget(parent, name ) { |
225 | libstocks_return_code error; | 221 | init(); |
222 | startTimer(1000); | ||
223 | // checkConnection(); | ||
224 | } | ||
226 | 225 | ||
227 | /* Proxy support */ | 226 | StockTickerPluginWidget::~StockTickerPluginWidget() { |
228 | /* Checks for "http_proxy" environment variable */ | ||
229 | proxy = getenv("http_proxy"); | ||
230 | if(proxy) { | ||
231 | /* printf("proxy set\n"); */ | ||
232 | error = set_proxy(proxy); | ||
233 | if (error) { | ||
234 | // printf("Proxy error (%d)\n", error); | ||
235 | QString tempString; | ||
236 | tempString.sprintf("Proxy error (%d)\n", error); | ||
237 | output = tempString; | ||
238 | return; | ||
239 | // exit(1); | ||
240 | } | 227 | } |
228 | |||
229 | void StockTickerPluginWidget::init() { | ||
230 | |||
231 | QHBoxLayout* layout = new QHBoxLayout( this ); | ||
232 | stocktickerTicker = new OTicker(this); | ||
233 | stocktickerTicker->setMinimumHeight(15); | ||
234 | connect( stocktickerTicker, SIGNAL( mousePressed()), this, SLOT( checkConnection() )); | ||
235 | layout->addWidget( stocktickerTicker); | ||
236 | wasError = true; | ||
237 | } | ||
238 | |||
239 | void StockTickerPluginWidget::doStocks() { | ||
240 | Config cfg( "stockticker"); | ||
241 | cfg.setGroup( "Symbols" ); | ||
242 | QString symbollist; | ||
243 | symbollist = cfg.readEntry("Symbols", ""); | ||
244 | symbollist.replace(QRegExp(" "),"+");//seperated by + | ||
245 | |||
246 | // qDebug(symbollist); | ||
247 | if (!symbollist.isEmpty()) { | ||
248 | pthread_t thread1; | ||
249 | pthread_create(&thread1,NULL, (void * (*)(void *))getStocks, &symbollist); | ||
241 | } | 250 | } |
251 | // pthread_join(thread1,NULL); | ||
252 | // getStocks( symbollist.latin1() ); | ||
253 | } | ||
254 | |||
255 | void StockTickerPluginWidget::DefProxy(void) { | ||
256 | // char *proxy; | ||
257 | // libstocks_return_code error; | ||
258 | |||
259 | // /* Proxy support */ | ||
260 | // /* Checks for "http_proxy" environment variable */ | ||
261 | // proxy = getenv("http_proxy"); | ||
262 | // if(proxy) { | ||
263 | // /* printf("proxy set\n"); */ | ||
264 | // error = set_proxy(proxy); | ||
265 | // if (error) { | ||
266 | // // printf("Proxy error (%d)\n", error); | ||
267 | // QString tempString; | ||
268 | // tempString.sprintf("Proxy error (%d)\n", error); | ||
269 | // output = tempString; | ||
270 | // return; | ||
271 | // // exit(1); | ||
272 | // } | ||
273 | // } | ||
242 | } | 274 | } |
243 | 275 | ||
244 | void StockTickerPluginWidget::timerEvent( QTimerEvent *e ) { | 276 | void StockTickerPluginWidget::timerEvent( QTimerEvent *e ) { |
245 | killTimer(e->timerId()); | 277 | killTimer(e->timerId()); |
278 | checkConnection(); | ||
279 | } | ||
280 | |||
281 | void StockTickerPluginWidget::checkConnection() { | ||
282 | // qDebug("checking connection"); | ||
283 | // Sock = new QSocket( this ); | ||
284 | |||
285 | // if( wasError) | ||
286 | // stocktickerTicker->setText("Checking connection"); | ||
287 | |||
288 | // if(Sock->state() == QSocket::Idle) { | ||
289 | // Sock->connectToHost("finance.yahoo.com", 80); | ||
290 | // connect( Sock, SIGNAL( error(int) ), SLOT(socketError(int)) ); | ||
291 | // connect( Sock, SIGNAL( hostFound() ), SLOT(isConnected()) ); | ||
292 | // } else { | ||
293 | // qDebug("State is not Idle"); | ||
294 | isConnected(); | ||
295 | // } | ||
296 | } | ||
297 | |||
298 | void StockTickerPluginWidget::isConnected() { | ||
299 | // qDebug("We connect, so ok to grab stocks"); | ||
246 | doStocks(); | 300 | doStocks(); |
301 | |||
247 | Config cfg( "stockticker"); | 302 | Config cfg( "stockticker"); |
248 | cfg.setGroup("Timer"); | 303 | cfg.setGroup("Timer"); |
249 | timerDelay= cfg.readNumEntry("Delay",0); | 304 | timerDelay= cfg.readNumEntry("Delay",0); |
250 | if(timerDelay > 0) | 305 | if(timerDelay > 0) |
251 | startTimer(timerDelay*60000); | 306 | startTimer(timerDelay*60000); |
252 | qDebug("timer set for %d",(timerDelay*60000)/60000); | 307 | qDebug("timer set for %d",(timerDelay*60000)/60000); |
308 | wasError = false; | ||
309 | |||
310 | // Sock->close(); | ||
311 | } | ||
312 | |||
313 | void StockTickerPluginWidget::socketError(int errcode) { | ||
314 | switch(errcode) { | ||
315 | case QSocket::ErrConnectionRefused: | ||
316 | output = tr("Connection refused."); | ||
317 | break; | ||
318 | case QSocket::ErrHostNotFound: | ||
319 | output = tr("Could not find server."); | ||
320 | break; | ||
321 | case QSocket::ErrSocketRead : | ||
322 | output = tr("Socket read error."); | ||
323 | break; | ||
324 | }; | ||
325 | stocktickerTicker->setText( output ); | ||
326 | wasError = true; | ||
327 | // Sock->close(); | ||
253 | 328 | ||
254 | } | 329 | } |