author | llornkcor <llornkcor> | 2002-11-05 01:10:38 (UTC) |
---|---|---|
committer | llornkcor <llornkcor> | 2002-11-05 01:10:38 (UTC) |
commit | 9ac6d0a443c417158e3f2acf2de0629c51b6511c (patch) (unidiff) | |
tree | e9791e964d950a49c43b05459e0710b5aa8d5dae /noncore/todayplugins/stockticker/stocktickerlib/stocktickerpluginwidget.cpp | |
parent | 4efdcbfa9ae55ba42b10b5003c9e524b2e1f0eef (diff) | |
download | opie-9ac6d0a443c417158e3f2acf2de0629c51b6511c.zip opie-9ac6d0a443c417158e3f2acf2de0629c51b6511c.tar.gz opie-9ac6d0a443c417158e3f2acf2de0629c51b6511c.tar.bz2 |
pthread stuff.
Diffstat (limited to 'noncore/todayplugins/stockticker/stocktickerlib/stocktickerpluginwidget.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/todayplugins/stockticker/stocktickerlib/stocktickerpluginwidget.cpp | 195 |
1 files changed, 135 insertions, 60 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 | |||
@@ -11,74 +11,48 @@ | |||
11 | * it under the terms of the GNU General Public License as published by * | 11 | * it under the terms of the GNU General Public License as published by * |
12 | * the Free Software Foundation; either version 2 of the License, or * | 12 | * the Free Software Foundation; either version 2 of the License, or * |
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> |
21 | #include <qobject.h> | 22 | #include <qobject.h> |
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 | 41 | ||
49 | void StockTickerPluginWidget::init() { | 42 | #include "stocktickerpluginwidget.h" |
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 | |||
57 | layout->addWidget( stocktickerTicker); | ||
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; |
70 | 45 | OTicker *stocktickerTicker; | |
71 | stocktickerTicker->setText( output ); | ||
72 | } | ||
73 | 46 | ||
74 | void StockTickerPluginWidget::getStocks( const char *blah) { | 47 | void getStocks( const QString *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; |
83 | 57 | ||
84 | Config cfg( "stockticker"); | 58 | Config cfg( "stockticker"); |
@@ -98,14 +72,35 @@ void StockTickerPluginWidget::getStocks( const char *blah) { | |||
98 | doopenPriceCheck=cfg.readBoolEntry("openPriceCheck",1); | 72 | doopenPriceCheck=cfg.readBoolEntry("openPriceCheck",1); |
99 | dominPriceCheck=cfg.readBoolEntry("minPriceCheck",1); | 73 | dominPriceCheck=cfg.readBoolEntry("minPriceCheck",1); |
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) { |
110 | printf("Error in getting stocks (%d)\n", error); | 105 | printf("Error in getting stocks (%d)\n", error); |
111 | tempString.sprintf("Error in getting stocks (%d)\n", error); | 106 | tempString.sprintf("Error in getting stocks (%d)\n", error); |
@@ -214,41 +209,121 @@ void StockTickerPluginWidget::getStocks( const char *blah) { | |||
214 | /* Simple function which help to browse in the stocks list */ | 209 | /* Simple function which help to browse in the stocks list */ |
215 | stocks_tmp = next_stock(stocks_tmp); | 210 | stocks_tmp = next_stock(stocks_tmp); |
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 */ | 227 | } |
229 | proxy = getenv("http_proxy"); | 228 | |
230 | if(proxy) { | 229 | void StockTickerPluginWidget::init() { |
231 | /* printf("proxy set\n"); */ | 230 | |
232 | error = set_proxy(proxy); | 231 | QHBoxLayout* layout = new QHBoxLayout( this ); |
233 | if (error) { | 232 | stocktickerTicker = new OTicker(this); |
234 | // printf("Proxy error (%d)\n", error); | 233 | stocktickerTicker->setMinimumHeight(15); |
235 | QString tempString; | 234 | connect( stocktickerTicker, SIGNAL( mousePressed()), this, SLOT( checkConnection() )); |
236 | tempString.sprintf("Proxy error (%d)\n", error); | 235 | layout->addWidget( stocktickerTicker); |
237 | output = tempString; | 236 | wasError = true; |
238 | return; | 237 | } |
239 | // exit(1); | 238 | |
240 | } | 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 | // } | ||
274 | } | ||
275 | |||
276 | void StockTickerPluginWidget::timerEvent( QTimerEvent *e ) { | ||
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 | // } | ||
242 | } | 296 | } |
243 | 297 | ||
244 | void StockTickerPluginWidget::timerEvent( QTimerEvent *e ) { | 298 | void StockTickerPluginWidget::isConnected() { |
245 | killTimer(e->timerId()); | 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); |
253 | 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(); | ||
328 | |||
254 | } | 329 | } |