File indexing completed on 2024-07-21 04:35:15

0001 /*
0002     SPDX-FileCopyrightText: 2004 Ace Jones <acejones@users.sourceforge.net>
0003     SPDX-FileCopyrightText: 2004 Thomas Baumgart <thb@net-bembel.de>
0004 
0005     This file is part of libalkimia.
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #include "alkfinancequoteprocess.h"
0011 
0012 #include <QString>
0013 #include <QMap>
0014 #include <QtDebug>
0015 
0016 class AlkFinanceQuoteProcess::Private
0017 {
0018 public:
0019     typedef QMap<QString, QString> fqNameMap;
0020     bool m_isDone;
0021     QString m_string;
0022     fqNameMap m_fqNames;
0023 
0024     Private() : m_isDone(false)
0025     {
0026     }
0027 };
0028 
0029 AlkFinanceQuoteProcess::AlkFinanceQuoteProcess()
0030   : d(new Private)
0031 {
0032     d->m_fqNames["aex"] = "AEX";
0033     d->m_fqNames["aex_futures"] = "AEX Futures";
0034     d->m_fqNames["aex_options"] = "AEX Options";
0035     d->m_fqNames["amfiindia"] = "AMFI India";
0036     d->m_fqNames["asegr"] = "ASE";
0037     d->m_fqNames["asia"] = "Asia (Yahoo, ...)";
0038     d->m_fqNames["asx"] = "ASX";
0039     d->m_fqNames["australia"] = "Australia (ASX, Yahoo, ...)";
0040     d->m_fqNames["bmonesbittburns"] = "BMO NesbittBurns";
0041     d->m_fqNames["brasil"] = "Brasil (Yahoo, ...)";
0042     d->m_fqNames["canada"] = "Canada (Yahoo, ...)";
0043     d->m_fqNames["canadamutual"] = "Canada Mutual (Fund Library, ...)";
0044     d->m_fqNames["deka"] = "Deka Investments";
0045     d->m_fqNames["dutch"] = "Dutch (AEX, ...)";
0046     d->m_fqNames["dwsfunds"] = "DWS";
0047     d->m_fqNames["europe"] = "Europe (Yahoo, ...)";
0048     d->m_fqNames["fidelity"] = "Fidelity (Fidelity, ...)";
0049     d->m_fqNames["fidelity_direct"] = "Fidelity Direct";
0050     d->m_fqNames["financecanada"] = "Finance Canada";
0051     d->m_fqNames["ftportfolios"] = "First Trust (First Trust, ...)";
0052     d->m_fqNames["ftportfolios_direct"] = "First Trust Portfolios";
0053     d->m_fqNames["fundlibrary"] = "Fund Library";
0054     d->m_fqNames["greece"] = "Greece (ASE, ...)";
0055     d->m_fqNames["indiamutual"] = "India Mutual (AMFI, ...)";
0056     d->m_fqNames["maninv"] = "Man Investments";
0057     d->m_fqNames["fool"] = "Motley Fool";
0058     d->m_fqNames["nasdaq"] = "Nasdaq (Yahoo, ...)";
0059     d->m_fqNames["nz"] = "New Zealand (Yahoo, ...)";
0060     d->m_fqNames["nyse"] = "NYSE (Yahoo, ...)";
0061     d->m_fqNames["nzx"] = "NZX";
0062     d->m_fqNames["platinum"] = "Platinum Asset Management";
0063     d->m_fqNames["seb_funds"] = "SEB";
0064     d->m_fqNames["sharenet"] = "Sharenet";
0065     d->m_fqNames["za"] = "South Africa (Sharenet, ...)";
0066     d->m_fqNames["troweprice_direct"] = "T. Rowe Price";
0067     d->m_fqNames["troweprice"] = "T. Rowe Price";
0068     d->m_fqNames["tdefunds"] = "TD Efunds";
0069     d->m_fqNames["tdwaterhouse"] = "TD Waterhouse Canada";
0070     d->m_fqNames["tiaacref"] = "TIAA-CREF";
0071     d->m_fqNames["trustnet"] = "Trustnet";
0072     d->m_fqNames["uk_unit_trusts"] = "U.K. Unit Trusts";
0073     d->m_fqNames["unionfunds"] = "Union Investments";
0074     d->m_fqNames["tsp"] = "US Govt. Thrift Savings Plan";
0075     d->m_fqNames["usfedbonds"] = "US Treasury Bonds";
0076     d->m_fqNames["usa"] = "USA (Yahoo, Fool ...)";
0077     d->m_fqNames["vanguard"] = "Vanguard";
0078     d->m_fqNames["vwd"] = "VWD";
0079     d->m_fqNames["yahoo"] = "Yahoo";
0080     d->m_fqNames["yahoo_asia"] = "Yahoo Asia";
0081     d->m_fqNames["yahoo_australia"] = "Yahoo Australia";
0082     d->m_fqNames["yahoo_brasil"] = "Yahoo Brasil";
0083     d->m_fqNames["yahoo_europe"] = "Yahoo Europe";
0084     d->m_fqNames["yahoo_nz"] = "Yahoo New Zealand";
0085     d->m_fqNames["zifunds"] = "Zuerich Investments";
0086     connect(this, SIGNAL(readyReadStandardOutput()), this, SLOT(slotReceivedDataFromFilter()));
0087     connect(this, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(slotProcessExited()));
0088     connect(this, SIGNAL(error(QProcess::ProcessError)), this, SLOT(slotProcessExited()));
0089 }
0090 
0091 void AlkFinanceQuoteProcess::slotReceivedDataFromFilter()
0092 {
0093     QByteArray data(readAllStandardOutput());
0094 
0095 //   kDebug(2) << "WebPriceQuoteProcess::slotReceivedDataFromFilter(): " << QString(data);
0096     d->m_string += QString(data);
0097 }
0098 
0099 void AlkFinanceQuoteProcess::slotProcessExited()
0100 {
0101 //   kDebug(2) << "WebPriceQuoteProcess::slotProcessExited()";
0102     d->m_isDone = true;
0103 }
0104 
0105 void AlkFinanceQuoteProcess::testLaunch(const QString& scriptPath)
0106 {
0107     clearProgram();
0108 
0109     qDebug() << "running perl" << scriptPath << "-t";
0110 
0111     *this << "perl" << scriptPath << "-t";
0112     setOutputChannelMode(KProcess::OnlyStdoutChannel);
0113     start();
0114     if (!waitForStarted()) {
0115         qWarning("Unable to start FQ script");
0116     }
0117 }
0118 
0119 void AlkFinanceQuoteProcess::launch(const QString &scriptPath)
0120 {
0121     clearProgram();
0122 
0123     qDebug() << "running" << "perl" << scriptPath << "-l";
0124 
0125     *this << "perl" << scriptPath << "-l";
0126     setOutputChannelMode(KProcess::OnlyStdoutChannel);
0127     start();
0128     if (!waitForStarted()) {
0129         qWarning("Unable to start FQ script");
0130     }
0131 }
0132 
0133 bool AlkFinanceQuoteProcess::isFinished() const
0134 {
0135     return d->m_isDone;
0136 }
0137 
0138 const QStringList AlkFinanceQuoteProcess::getSourceList() const
0139 {
0140     #if QT_VERSION < QT_VERSION_CHECK(5,14,0)
0141     QStringList raw = d->m_string.split(0x0A, QString::SkipEmptyParts);
0142     #else
0143     QStringList raw = d->m_string.split(0x0A, Qt::SkipEmptyParts);
0144     #endif
0145     QStringList sources;
0146     QStringList::iterator it;
0147     for (it = raw.begin(); it != raw.end(); ++it) {
0148         if (d->m_fqNames[*it].isEmpty()) {
0149             sources.append(*it);
0150         } else {
0151             sources.append(d->m_fqNames[*it]);
0152         }
0153     }
0154     sources.sort();
0155     return sources;
0156 }
0157 
0158 const QString AlkFinanceQuoteProcess::crypticName(const QString &niceName) const
0159 {
0160     QString ret(niceName);
0161     Private::fqNameMap::const_iterator it;
0162     for (it = d->m_fqNames.begin(); it != d->m_fqNames.end(); ++it) {
0163         if (niceName == it.value()) {
0164             ret = it.key();
0165             break;
0166         }
0167     }
0168     return ret;
0169 }
0170 
0171 const QString AlkFinanceQuoteProcess::niceName(const QString &crypticName) const
0172 {
0173     QString ret(d->m_fqNames[crypticName]);
0174     if (ret.isEmpty()) {
0175         ret = crypticName;
0176     }
0177     return ret;
0178 }