File indexing completed on 2024-06-09 05:00:09

0001 #include "plasma-onlinequote.h"
0002 
0003 #include "alkonlinequote.h"
0004 #include "alkonlinequotesprofile.h"
0005 #include "alkonlinequotesprofilemanager.h"
0006 #include "ui_configwidget.h"
0007 
0008 #include <QComboBox>
0009 #include <QPainter>
0010 #include <QFontMetrics>
0011 #include <QSizeF>
0012 #include <QTimer>
0013 #include <QtDebug>
0014 
0015 #include <plasma/svg.h>
0016 #include <plasma/theme.h>
0017 
0018 #include <KConfigDialog>
0019 
0020 
0021 class MyWidget : public QWidget, public Ui::ConfigWidget
0022 {
0023 public:
0024     MyWidget()
0025     {
0026         setupUi((QWidget *)this);
0027     }
0028 };
0029 
0030 PlasmaOnlineQuote::PlasmaOnlineQuote(QObject *parent, const QVariantList &args)
0031     : Plasma::Applet(parent, args)
0032     , m_svg(this)
0033     , m_icon("preferences-system-network")
0034     , m_widget(0)
0035     , m_price(0)
0036     , m_profile(nullptr)
0037 {
0038     setHasConfigurationInterface(true);
0039     m_svg.setImagePath("widgets/background");
0040 #if QT_VERSION < QT_VERSION_CHECK(5,0,0)
0041     // this will get us the standard applet background, for free!
0042     setBackgroundHints(DefaultBackground);
0043     resize(200, 200);
0044 #endif
0045     AlkOnlineQuotesProfileManager &manager = AlkOnlineQuotesProfileManager::instance();
0046     // manager is shared between plasmoids
0047     if(AlkOnlineQuotesProfileManager::instance().profiles().size() == 0) {
0048         manager.addProfile(new AlkOnlineQuotesProfile("alkimia4", AlkOnlineQuotesProfile::Type::Alkimia4));
0049         manager.addProfile(new AlkOnlineQuotesProfile("alkimia5", AlkOnlineQuotesProfile::Type::Alkimia5));
0050         manager.addProfile(new AlkOnlineQuotesProfile("kmymoney4", AlkOnlineQuotesProfile::Type::KMyMoney4));
0051         manager.addProfile(new AlkOnlineQuotesProfile("kmymoney5", AlkOnlineQuotesProfile::Type::KMyMoney5));
0052     }
0053 }
0054 
0055 PlasmaOnlineQuote::~PlasmaOnlineQuote()
0056 {
0057 #if QT_VERSION < QT_VERSION_CHECK(5,0,0)
0058     if (hasFailedToLaunch()) {
0059         // Do some cleanup here
0060     } else
0061 #endif
0062     {
0063         // Save settings
0064         config().sync();
0065     }
0066 }
0067 
0068 void PlasmaOnlineQuote::init()
0069 {
0070 #if QT_VERSION < QT_VERSION_CHECK(5,0,0)
0071     // A small demonstration of the setFailedToLaunch function
0072     if (m_icon.isNull()) {
0073         setFailedToLaunch(true, "No world to say hello");
0074     }
0075 #endif
0076     QString currentProfile = config().readEntry("profile");
0077     qDebug() << "reading current profile" << currentProfile;
0078     if (currentProfile.isEmpty())
0079         currentProfile = AlkOnlineQuotesProfileManager::instance().profiles().first()->name();
0080     qDebug() << "setup current profile" << currentProfile;
0081     m_profile = AlkOnlineQuotesProfileManager::instance().profile(currentProfile);
0082     QTimer::singleShot(100, this, SLOT(slotFetchQuote()));
0083 }
0084 
0085 void PlasmaOnlineQuote::configChanged()
0086 {
0087     qDebug() << "configChanged()";
0088 }
0089 
0090 void PlasmaOnlineQuote::createConfigurationInterface(KConfigDialog *parent)
0091 {
0092     m_widget = new MyWidget;
0093     QStringList profiles = AlkOnlineQuotesProfileManager::instance().profileNames();
0094     m_widget->m_profile->addItems(profiles);
0095     QString currentProfile = m_profile->name();
0096     int index = profiles.indexOf(currentProfile);
0097     m_widget->m_profile->setCurrentIndex(index);
0098     connect(m_widget->m_profile, SIGNAL(currentIndexChanged(QString)), this, SLOT(slotProfileChanged(QString)));
0099 
0100     QStringList sources = m_profile->quoteSources();
0101     m_widget->m_onlineQuote->addItems(sources);
0102     index = sources.indexOf(config().readEntry("onlinequote"));
0103     m_widget->m_onlineQuote->setCurrentIndex(index);
0104 
0105     m_widget->m_symbol->setText(config().readEntry("symbol"));
0106     m_widget->m_interval->setValue(config().readEntry("interval", 60));
0107     parent->addPage(dynamic_cast<QWidget*>(m_widget), "Online Source");
0108     connect(parent, SIGNAL(applyClicked()), this, SLOT(slotConfigAccepted()));
0109     connect(parent, SIGNAL(okClicked()), this, SLOT(slotConfigAccepted()));
0110 }
0111 
0112 void PlasmaOnlineQuote::slotProfileChanged(const QString &name)
0113 {
0114     AlkOnlineQuotesProfile *profile = AlkOnlineQuotesProfileManager::instance().profile(name);
0115     if (!profile) {
0116         qWarning() << "profile" << name << "not present";
0117         return;
0118     }
0119     m_profile = profile;
0120     QStringList sources = m_profile->quoteSources();
0121     m_widget->m_onlineQuote->clear();
0122     m_widget->m_onlineQuote->addItems(sources);
0123     int index = sources.indexOf(config().readEntry("onlinequote"));
0124     m_widget->m_onlineQuote->setCurrentIndex(index);
0125 }
0126 
0127 void PlasmaOnlineQuote::slotConfigAccepted()
0128 {
0129     config().writeEntry("profile", m_widget->m_profile->currentText());
0130     config().writeEntry("onlinequote", m_widget->m_onlineQuote->currentText());
0131     config().writeEntry("symbol", m_widget->m_symbol->text());
0132     config().writeEntry("interval", m_widget->m_interval->value());
0133     config().sync();
0134     Q_EMIT configNeedsSaving();
0135     slotFetchQuote();
0136     qDebug() << "configAccepted()";
0137 }
0138 
0139 void PlasmaOnlineQuote::slotFetchQuote()
0140 {
0141     if (config().readEntry("symbol").isEmpty() || config().readEntry("interval").toInt() == 0) {
0142         qDebug() << __FUNCTION__ << "no configuration found";
0143         return;
0144     }
0145     AlkOnlineQuote quote(m_profile);
0146     connect(&quote, SIGNAL(status(QString)), this, SLOT(slotLogStatus(QString)));
0147     connect(&quote, SIGNAL(error(QString)), this, SLOT(slotLogError(QString)));
0148     connect(&quote, SIGNAL(failed(QString,QString)), this, SLOT(slotLogFailed(QString,QString)));
0149     connect(&quote, SIGNAL(quote(QString,QString,QDate,double)), this, SLOT(slotReceivedQuote(QString,QString,QDate,double)));
0150     quote.launch(config().readEntry("symbol"), "", config().readEntry("onlinequote"));
0151     int interval = config().readEntry("interval").toInt()*1000;
0152     qDebug() << "setting timer to " << interval << "ms";
0153     QTimer::singleShot(interval, this, SLOT(slotFetchQuote()));
0154 }
0155 
0156 void PlasmaOnlineQuote::slotLogStatus(const QString &s)
0157 {
0158     qDebug() << s;
0159 }
0160 
0161 void PlasmaOnlineQuote::slotLogError(const QString &s)
0162 {
0163     slotLogStatus("Error:" + s);
0164 }
0165 
0166 void PlasmaOnlineQuote::slotLogFailed(const QString &id, const QString &symbol)
0167 {
0168     slotLogStatus(QString("Failed: %1 %2").arg(id, symbol));
0169 }
0170 
0171 void PlasmaOnlineQuote::slotReceivedQuote(const QString &id, const QString &symbol, const QDate &date, const double &price)
0172 {
0173     Q_UNUSED(id)
0174     Q_UNUSED(symbol)
0175     qDebug() << "got quote" << date << price;
0176     m_date = date;
0177     m_price = price;
0178 #if QT_VERSION < QT_VERSION_CHECK(5,0,0)
0179     update();
0180 #else
0181 #warning how to update ui ?
0182 #endif
0183 }
0184 
0185 void PlasmaOnlineQuote::paintInterface(QPainter *p,
0186         const QStyleOptionGraphicsItem *option, const QRect &contentsRect)
0187 {
0188     Q_UNUSED(option)
0189     p->setRenderHint(QPainter::SmoothPixmapTransform);
0190     p->setRenderHint(QPainter::Antialiasing);
0191 
0192     // Now we draw the applet, starting with our svg
0193     m_svg.resize((int)contentsRect.width(), (int)contentsRect.height());
0194     m_svg.paint(p, (int)contentsRect.left(), (int)contentsRect.top());
0195 
0196     // We place the icon and text
0197     p->drawPixmap(7, 0, m_icon.pixmap((int)contentsRect.width(),(int)contentsRect.width()-14));
0198     p->save();
0199     p->setPen(Qt::white);
0200     if (true) {
0201         qDebug() << "drawing" << config().readEntry("onlinequote") << "price" << m_price;
0202         p->drawText(contentsRect,
0203                     Qt::AlignVCenter | Qt::AlignHCenter,
0204                     config().readEntry("onlinequote"));
0205         p->drawText(contentsRect,
0206                     Qt::AlignBottom | Qt::AlignHCenter,
0207                     config().readEntry("symbol") + " " + QString::number(m_price,8,4));
0208 
0209     } else {
0210         p->drawText(contentsRect,
0211                 Qt::AlignBottom | Qt::AlignHCenter,
0212                 "Hello Plasmoid!");
0213     }
0214     p->restore();
0215 }
0216 
0217 // This is the command that links your applet to the .desktop file
0218 K_EXPORT_PLASMA_APPLET(onlinequote, PlasmaOnlineQuote)