1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
|
/***************************************************************************
kequitypriceupdatedlg.cpp - description
-------------------
begin : Mon Sep 1 2003
copyright : (C) 2000-2003 by Michael Edwardes
email : [email protected]
Javier Campos Morales <[email protected]>
Felix Rodriguez <[email protected]>
John C <[email protected]>
Thomas Baumgart <[email protected]>
Kevin Tambascio <[email protected]>
Ace Jones <[email protected]>
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
// ----------------------------------------------------------------------------
// QT Includes
#include <tqlabel.h>
#include <tqpushbutton.h>
#include <tqradiobutton.h>
#include <tqfile.h>
#include <tqtextstream.h>
#include <tqtimer.h>
#include <tqlayout.h>
// ----------------------------------------------------------------------------
// KDE Includes
#include <klocale.h>
#include <kmessagebox.h>
#include <kpushbutton.h>
#include <ktextedit.h>
#include <klistview.h>
#include <kdebug.h>
#include <kprogress.h>
#include <kglobal.h>
#include <kconfig.h>
// ----------------------------------------------------------------------------
// Project Includes
#include "kequitypriceupdatedlg.h"
#include "../mymoney/mymoneyfile.h"
#include "../mymoney/mymoneysecurity.h"
#include "../mymoney/mymoneyprice.h"
#include "../kmymoneyglobalsettings.h"
#define SYMBOL_COL 0
#define NAME_COL 1
#define PRICE_COL 2
#define DATE_COL 3
#define ID_COL 4
#define SOURCE_COL 5
KEquityPriceUpdateDlg::KEquityPriceUpdateDlg(TQWidget *parent, const TQString& securityId) :
KEquityPriceUpdateDlgDecl(parent),
m_fUpdateAll(false)
{
lvEquityList->setRootIsDecorated(false);
lvEquityList->setColumnText(0, i18n("Symbol"));
lvEquityList->addColumn(i18n("Symbol"));
lvEquityList->addColumn(i18n("Name"),125);
lvEquityList->addColumn(i18n("Price"));
lvEquityList->addColumn(i18n("Date"));
// This is a "get it up and running" hack. Will replace this in the future.
lvEquityList->addColumn("ID");
lvEquityList->addColumn("Source");
lvEquityList->setColumnWidth(ID_COL, 0);
lvEquityList->setMultiSelection(true);
lvEquityList->setColumnWidthMode(SYMBOL_COL, TQListView::Maximum);
lvEquityList->setColumnWidthMode(ID_COL, TQListView::Manual);
lvEquityList->setAllColumnsShowFocus(true);
btnUpdateAll->setEnabled(false);
MyMoneyFile* file = MyMoneyFile::instance();
//
// Add each price pair that we know about
//
// send in securityId == "XXX YYY" to get a single-shot update for XXX to YYY.
// for consistency reasons, this accepts the same delimiters as WebPriceQuote::launch()
TQRegExp splitrx("([0-9a-z\\.]+)[^a-z0-9]+([0-9a-z\\.]+)",false /*case sensitive*/);
MyMoneySecurityPair currencyIds;
if ( splitrx.search(securityId) != -1 )
currencyIds = MyMoneySecurityPair(splitrx.cap(1).utf8(),splitrx.cap(2).utf8());
MyMoneyPriceList prices = file->priceList();
for(MyMoneyPriceList::ConstIterator it_price = prices.begin(); it_price != prices.end(); ++it_price)
{
const MyMoneySecurityPair& pair = it_price.key();
if ( file->security( pair.first ).isCurrency() && ( securityId.isEmpty() || ( pair == currencyIds ) ) )
{
const MyMoneyPriceEntries& entries = (*it_price);
if(entries.count() > 0 && entries.begin().key() <= TQDate::currentDate()) {
addPricePair(pair);
btnUpdateAll->setEnabled(true);
}
}
}
//
// Add each investment
//
TQValueList<MyMoneySecurity> securities = file->securityList();
for(TQValueList<MyMoneySecurity>::ConstIterator it = securities.begin(); it != securities.end(); ++it)
{
if ( !(*it).isCurrency()
&& ( securityId.isEmpty() || ( (*it).id() == securityId ) )
&& !(*it).value("kmm-online-source").isEmpty()
)
{
addInvestment(*it);
btnUpdateAll->setEnabled(true);
}
}
// if list is empty, add the request price pair
if(lvEquityList->firstChild() == 0) {
addPricePair(currencyIds, true);
}
connect(btnOK, TQT_SIGNAL(clicked()), this, TQT_SLOT(accept()));
connect(btnCancel, TQT_SIGNAL(clicked()), this, TQT_SLOT(reject()));
connect(btnUpdateSelected, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotUpdateSelectedClicked()));
connect(btnUpdateAll, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotUpdateAllClicked()));
connect(&m_webQuote,TQT_SIGNAL(quote(const TQString&, const TQString&,const TQDate&, const double&)),
this,TQT_SLOT(slotReceivedQuote(const TQString&, const TQString&,const TQDate&, const double&)));
connect(&m_webQuote,TQT_SIGNAL(failed(const TQString&, const TQString&)),
this,TQT_SLOT(slotQuoteFailed(const TQString&, const TQString&)));
connect(&m_webQuote,TQT_SIGNAL(status(const TQString&)),
this,TQT_SLOT(logStatusMessage(const TQString&)));
connect(&m_webQuote,TQT_SIGNAL(error(const TQString&)),
this,TQT_SLOT(logErrorMessage(const TQString&)));
connect(lvEquityList, TQT_SIGNAL(selectionChanged()), this, TQT_SLOT(slotUpdateSelection()));
// Not implemented yet.
btnConfigure->hide();
//connect(btnConfigure, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotConfigureClicked()));
if ( !securityId.isEmpty() )
{
btnUpdateSelected->hide();
btnUpdateAll->hide();
// delete layout1;
TQTimer::singleShot(100,this,TQT_SLOT(slotUpdateAllClicked()));
}
// Hide OK button until we have received the first update
btnOK->setEnabled(false);
slotUpdateSelection();
// previous versions of this dialog allowed to store a "Don't ask again" switch.
// Since we don't support it anymore, we just get rid of it
TDEConfig* config = TDEGlobal::config();
config->setGroup("Notification Messages");
config->deleteEntry("KEquityPriceUpdateDlg::slotQuoteFailed::Price Update Failed");
}
KEquityPriceUpdateDlg::~KEquityPriceUpdateDlg()
{
}
void KEquityPriceUpdateDlg::addPricePair(const MyMoneySecurityPair& pair, bool dontCheckExistance)
{
MyMoneyFile* file = MyMoneyFile::instance();
TQString symbol = TQString("%1 > %2").arg(pair.first,pair.second);
TQString id = TQString("%1 %2").arg(pair.first,pair.second);
if ( ! lvEquityList->findItem(id,ID_COL,TQt::ExactMatch) )
{
MyMoneyPrice pr = file->price(pair.first,pair.second);
if(pr.source() != "KMyMoney") {
bool keep = true;
if((pair.first == file->baseCurrency().id())
|| (pair.second == file->baseCurrency().id())) {
const TQString& foreignCurrency = file->foreignCurrency(pair.first, pair.second);
// check that the foreign currency is still in use
TQValueList<MyMoneyAccount>::const_iterator it_a;
TQValueList<MyMoneyAccount> list;
file->accountList(list);
for(it_a = list.begin(); !dontCheckExistance && it_a != list.end(); ++it_a) {
// if it's an account denominated in the foreign currency
// keep it
if(((*it_a).currencyId() == foreignCurrency)
&& !(*it_a).isClosed())
break;
// if it's an investment traded in the foreign currency
// keep it
if((*it_a).isInvest() && !(*it_a).isClosed()) {
MyMoneySecurity sec = file->security((*it_a).currencyId());
if(sec.tradingCurrency() == foreignCurrency)
break;
}
}
// if it is in use, it_a is not equal to list.end()
if(it_a == list.end() && !dontCheckExistance)
keep = false;
}
if(keep) {
KListViewItem* item = new KListViewItem(lvEquityList,
symbol,
i18n("%1 units in %2").arg(pair.first,pair.second));
if(pr.isValid()) {
item->setText(PRICE_COL, pr.rate(pair.second).formatMoney(file->currency(pair.second).tradingSymbol(), KMyMoneyGlobalSettings::pricePrecision()));
item->setText(DATE_COL, pr.date().toString(Qt::ISODate));
}
item->setText(ID_COL,id);
item->setText(SOURCE_COL, "Yahoo Currency"); // This string value should not be localized
}
}
}
}
void KEquityPriceUpdateDlg::addInvestment(const MyMoneySecurity& inv)
{
MyMoneyFile* file = MyMoneyFile::instance();
TQString symbol = inv.tradingSymbol();
TQString id = inv.id();
if ( ! lvEquityList->findItem(id, ID_COL, TQt::ExactMatch) )
{
// check that the security is still in use
TQValueList<MyMoneyAccount>::const_iterator it_a;
TQValueList<MyMoneyAccount> list;
file->accountList(list);
for(it_a = list.begin(); it_a != list.end(); ++it_a) {
if((*it_a).isInvest()
&& ((*it_a).currencyId() == inv.id())
&& !(*it_a).isClosed())
break;
}
// if it is in use, it_a is not equal to list.end()
if(it_a != list.end()) {
KListViewItem* item = new KListViewItem(lvEquityList, symbol, inv.name());
MyMoneySecurity currency = file->currency(inv.tradingCurrency());
MyMoneyPrice pr = file->price(id.utf8(), inv.tradingCurrency());
if(pr.isValid()) {
item->setText(PRICE_COL, pr.rate(currency.id()).formatMoney(currency.tradingSymbol(), KMyMoneyGlobalSettings::pricePrecision()));
item->setText(DATE_COL, pr.date().toString(Qt::ISODate));
}
item->setText(ID_COL,id);
if (inv.value("kmm-online-quote-system") == "Finance::Quote")
item->setText(SOURCE_COL, TQString("Finance::Quote %1").arg( inv.value("kmm-online-source")));
else
item->setText(SOURCE_COL, inv.value("kmm-online-source"));
// If this investment is denominated in a foreign currency, ensure that
// the appropriate price pair is also on the list
if ( currency.id() != file->baseCurrency().id() )
{
addPricePair(MyMoneySecurityPair(currency.id(),file->baseCurrency().id()));
}
}
}
}
void KEquityPriceUpdateDlg::logErrorMessage(const TQString& message)
{
logStatusMessage(TQString("<font color=\"red\"><b>") + message + TQString("</b></font>"));
}
void KEquityPriceUpdateDlg::logStatusMessage(const TQString& message)
{
lbStatus->append(message);
}
MyMoneyPrice KEquityPriceUpdateDlg::price(const TQString& id) const
{
MyMoneyPrice price;
TQListViewItem* item;
if((item = lvEquityList->findItem(id, ID_COL, TQt::ExactMatch)) != 0) {
MyMoneyMoney rate(item->text(PRICE_COL));
if ( !rate.isZero() )
{
TQString id = item->text(ID_COL).utf8();
// if the ID has a space, then this is TWO ID's, so it's a currency quote
if ( TQString(id).contains(" ") )
{
TQStringList ids = TQStringList::split(" ",TQString(id));
TQString fromid = ids[0].utf8();
TQString toid = ids[1].utf8();
price = MyMoneyPrice(fromid,toid,TQDate().fromString(item->text(DATE_COL), Qt::ISODate),rate,item->text(SOURCE_COL));
}
else
// otherwise, it's a security quote
{
MyMoneySecurity security = MyMoneyFile::instance()->security(id);
price = MyMoneyPrice(id, security.tradingCurrency(), TQDate().fromString(item->text(DATE_COL), Qt::ISODate), rate, item->text(SOURCE_COL));
}
}
}
return price;
}
void KEquityPriceUpdateDlg::storePrices(void)
{
// update the new prices into the equities
MyMoneyFile* file = MyMoneyFile::instance();
TQValueList<MyMoneySecurity> equities = file->securityList();
TQListViewItem* item = lvEquityList->firstChild();
MyMoneyFileTransaction ft;
TQString name;
try {
while ( item )
{
// turn on signals before we modify the last entry in the list
MyMoneyFile::instance()->blockSignals(item->nextSibling() != 0);
MyMoneyMoney rate(item->text(PRICE_COL));
if ( !rate.isZero() )
{
TQString id = item->text(ID_COL).utf8();
// if the ID has a space, then this is TWO ID's, so it's a currency quote
if ( TQString(id).contains(" ") )
{
TQStringList ids = TQStringList::split(" ",TQString(id));
TQString fromid = ids[0].utf8();
TQString toid = ids[1].utf8();
name = TQString("%1 --> %2").arg(fromid).arg(toid);
MyMoneyPrice price(fromid,toid,TQDate().fromString(item->text(DATE_COL), Qt::ISODate),rate,item->text(SOURCE_COL));
file->addPrice(price);
}
else
// otherwise, it's a security quote
{
MyMoneySecurity security = MyMoneyFile::instance()->security(id);
name = security.name();
MyMoneyPrice price(id, security.tradingCurrency(), TQDate().fromString(item->text(DATE_COL), Qt::ISODate), rate, item->text(SOURCE_COL));
// TODO (Ace) Better handling of the case where there is already a price
// for this date. Currently, it just overrides the old value. Really it
// should check to see if the price is the same and prompt the user.
MyMoneyFile::instance()->addPrice(price);
}
}
item = item->nextSibling();
}
ft.commit();
} catch(MyMoneyException *e) {
tqDebug("Unable to add price information for %s", name.data());
delete e;
}
}
void KEquityPriceUpdateDlg::slotUpdateSelection(void)
{
btnUpdateSelected->setEnabled(false);
TQListViewItem* item = lvEquityList->firstChild();
while ( item && !item->isSelected())
item = item->nextSibling();
if(item)
btnUpdateSelected->setEnabled(true);
}
void KEquityPriceUpdateDlg::slotUpdateSelectedClicked(void)
{
TQListViewItem* item = lvEquityList->firstChild();
int skipCnt = 1;
while ( item && !item->isSelected())
{
skipCnt++;
item = item->nextSibling();
}
if(item) {
prgOnlineProgress->setTotalSteps(1+lvEquityList->childCount());
prgOnlineProgress->setProgress(skipCnt);
m_webQuote.launch(item->text(SYMBOL_COL),item->text(ID_COL),item->text(SOURCE_COL));
}
else
logErrorMessage("No security selected.");
}
void KEquityPriceUpdateDlg::slotUpdateAllClicked(void)
{
TQListViewItem* item = lvEquityList->firstChild();
if ( item )
{
prgOnlineProgress->setTotalSteps(1+lvEquityList->childCount());
prgOnlineProgress->setProgress(1);
m_fUpdateAll = true;
m_webQuote.launch(item->text(SYMBOL_COL),item->text(ID_COL),item->text(SOURCE_COL));
}
else
logErrorMessage("Security list is empty.");
}
void KEquityPriceUpdateDlg::slotQuoteFailed(const TQString& _id, const TQString& _symbol)
{
TQListViewItem* item = lvEquityList->findItem(_id,ID_COL,TQt::ExactMatch);
// Give the user some options
int result;
if(_id.contains(" ")) {
result = KMessageBox::warningContinueCancel(this, i18n("Failed to retrieve an exchange rate for %1 from %2. It will be skipped this time.").arg(_symbol, item->text(SOURCE_COL)), i18n("Price Update Failed"));
} else {
result = KMessageBox::questionYesNoCancel(this, TQString("<qt>%1</qt>").arg(i18n("Failed to retrieve a quote for %1 from %2. Press <b>No</b> to remove the online price source from this security permanently, <b>Yes</b> to continue updating this security during future price updates or <b>Cancel</b> to stop the current update operation.").arg(_symbol, item->text(SOURCE_COL))), i18n("Price Update Failed"), KStdGuiItem::yes(), KStdGuiItem::no());
}
if ( result == KMessageBox::No )
{
// Disable price updates for this security
MyMoneyFileTransaction ft;
try {
// Get this security (by ID)
MyMoneySecurity security = MyMoneyFile::instance()->security(_id.utf8());
// Set the quote source to blank
security.setValue("kmm-online-source",TQString());
security.setValue("kmm-online-quote-system",TQString());
// Re-commit the security
MyMoneyFile::instance()->modifySecurity(security);
ft.commit();
} catch(MyMoneyException* e) {
KMessageBox::error(this, TQString("<qt>")+i18n("Cannot update security <b>%1</b>: %2").arg(_symbol, e->what())+TQString("</qt>"), i18n("Price Update Failed"));
delete e;
}
}
// As long as the user doesn't want to cancel, move on!
if ( result != KMessageBox::Cancel )
{
TQListViewItem* next = NULL;
prgOnlineProgress->advance(1);
item->listView()->setSelected(item, false);
// launch the NEXT one ... in case of m_fUpdateAll == false, we
// need to parse the list to find the next selected one
next = item->nextSibling();
if ( !m_fUpdateAll )
{
while(next && !next->isSelected())
{
prgOnlineProgress->advance(1);
next = next->nextSibling();
}
}
if (next)
{
m_webQuote.launch(next->text(SYMBOL_COL),next->text(ID_COL),next->text(SOURCE_COL));
}
else
{
finishUpdate();
}
}
else
{
finishUpdate();
}
}
void KEquityPriceUpdateDlg::slotReceivedQuote(const TQString& _id, const TQString& _symbol,const TQDate& _date, const double& _price)
{
TQListViewItem* item = lvEquityList->findItem(_id,ID_COL,TQt::ExactMatch);
TQListViewItem* next = NULL;
if ( item )
{
if ( _price > 0.0f && _date.isValid() )
{
TQDate date = _date;
if ( date > TQDate::currentDate() )
date = TQDate::currentDate();
double price = _price;
TQString id = _id.utf8();
MyMoneySecurity sec;
if ( _id.contains(" ") == 0) {
MyMoneySecurity security = MyMoneyFile::instance()->security(id);
TQString factor = security.value("kmm-online-factor");
if(!factor.isEmpty()) {
price *= MyMoneyMoney(factor).toDouble();
}
try {
sec = MyMoneyFile::instance()->security(id);
sec = MyMoneyFile::instance()->security(sec.tradingCurrency());
} catch(MyMoneyException *e) {
sec = MyMoneySecurity();
delete e;
}
} else {
TQRegExp splitrx("([0-9a-z\\.]+)[^a-z0-9]+([0-9a-z\\.]+)",false /*case sensitive*/);
if ( splitrx.search(_id) != -1 ) {
try {
sec = MyMoneyFile::instance()->security(splitrx.cap(2).utf8());
} catch(MyMoneyException *e) {
sec = MyMoneySecurity();
delete e;
}
}
}
item->setText(PRICE_COL, TDEGlobal::locale()->formatMoney(price, sec.tradingSymbol(), KMyMoneyGlobalSettings::pricePrecision()));
item->setText(DATE_COL, date.toString(Qt::ISODate));
logStatusMessage(i18n("Price for %1 updated (id %2)").arg(_symbol,_id));
// make sure to make OK button available
btnOK->setEnabled(true);
}
else
{
logErrorMessage(i18n("Received an invalid price for %1, unable to update.").arg(_symbol));
}
prgOnlineProgress->advance(1);
item->listView()->setSelected(item, false);
// launch the NEXT one ... in case of m_fUpdateAll == false, we
// need to parse the list to find the next selected one
next = item->nextSibling();
if ( !m_fUpdateAll )
{
while(next && !next->isSelected())
{
prgOnlineProgress->advance(1);
next = next->nextSibling();
}
}
}
else
{
logErrorMessage(i18n("Received a price for %1 (id %2), but this symbol is not on the list! Aborting entire update.").arg(_symbol,_id));
}
if (next)
{
m_webQuote.launch(next->text(SYMBOL_COL),next->text(ID_COL),next->text(SOURCE_COL));
}
else
{
finishUpdate();
}
}
void KEquityPriceUpdateDlg::finishUpdate(void)
{
// we've run past the end, reset to the default value.
m_fUpdateAll = false;
// force progress bar to show 100%
prgOnlineProgress->setProgress(prgOnlineProgress->totalSteps());
}
// Make sure, that these definitions are only used within this file
// this does not seem to be necessary, but when building RPMs the
// build option 'final' is used and all CPP files are concatenated.
// So it could well be, that in another CPP file these definitions
// are also used.
#undef SYMBOL_COL
#undef NAME_COL
#undef PRICE_COL
#undef DATE_COL
#undef ID_COL
#undef SOURCE_COL
#include "kequitypriceupdatedlg.moc"
// vim:cin:si:ai:et:ts=2:sw=2:
|