lib/QuotePlugin.cpp

Go to the documentation of this file.
00001 /*
00002  *  Qtstalker stock charter
00003  *
00004  *  Copyright (C) 2001-2007 Stefan S. Stratigakos
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
00019  *  USA.
00020  */
00021 
00022 #include "QuotePlugin.h"
00023 #include <qdir.h>
00024 #include "HelpWindow.h"
00025 #include "Config.h"
00026 #include "../pics/download.xpm"
00027 #include "../pics/canceldownload.xpm"
00028 #include <qstringlist.h>
00029 #include <qmessagebox.h>
00030 #include <qapplication.h>
00031 #include <qlabel.h>
00032 #include <qpixmap.h>
00033 #include <qtooltip.h>
00034 #include <qtabwidget.h>
00035 
00036 QuotePlugin::QuotePlugin () : QTabDialog (0, "QuoteDialog", FALSE, 0)
00037 {
00038   saveFlag = FALSE;
00039   op = 0;
00040   chartIndex = 0;
00041   errorLoop = 0;
00042   stringDone = tr("Done");
00043   stringCanceled = tr("Canceled");
00044   
00045   timer = new QTimer(this);
00046   connect(timer, SIGNAL(timeout()), this, SLOT(slotTimeout()));
00047 
00048   buildGui();
00049 }
00050 
00051 QuotePlugin::~QuotePlugin ()
00052 {
00053   if (op)
00054   {
00055     op->stop();
00056     delete op;
00057   }
00058 
00059   delete timer;
00060 }
00061 
00062 void QuotePlugin::setChartIndex (DBIndex *d)
00063 {
00064   chartIndex = d;
00065 }
00066 
00067 void QuotePlugin::buildGui ()
00068 {
00069   baseWidget = new QWidget(this);
00070   
00071   vbox = new QVBoxLayout(baseWidget);
00072   vbox->setSpacing(2);
00073   vbox->setMargin(5);
00074   
00075   toolbar = new Toolbar(baseWidget, Toolbar::Horizontal);
00076   vbox->addWidget(toolbar);
00077 
00078   QString s("update");
00079   QString s2(tr("Update"));
00080   toolbar->addButton(s, download, s2);
00081   QObject::connect(toolbar->getButton(s), SIGNAL(clicked()), this, SLOT(getQuotes()));
00082   toolbar->getButton(s)->setAccel(CTRL+Key_U);
00083   
00084   s = "cancelDownload";
00085   s2 = tr("Cancel Update");
00086   toolbar->addButton(s, canceldownload, s2);
00087   QObject::connect(toolbar->getButton(s), SIGNAL(clicked()), this, SLOT(cancelDownload()));
00088   toolbar->setButtonStatus(s, FALSE);
00089   toolbar->getButton(s)->setAccel(CTRL+Key_C);
00090   
00091   vbox->addSpacing(5);
00092 
00093   // quote plugins insert their gui widget here
00094 
00095   grid = new QGridLayout(vbox, 1, 2);
00096   grid->setSpacing(5);
00097   grid->setColStretch(1, 1);
00098 
00099   vbox->addSpacing(5);
00100 
00101   QLabel *label = new QLabel(tr("Progress:"), baseWidget);
00102   vbox->addWidget(label);
00103   progressBar = new QProgressBar(baseWidget);
00104   vbox->addWidget(progressBar);
00105 
00106   vbox->addSpacing(10);
00107 
00108   QTabWidget *tabs = new QTabWidget(baseWidget);
00109   vbox->addWidget(tabs);
00110 
00111   addTab(baseWidget, tr("Quotes"));
00112 
00113   setOkButton(tr("&Done"));
00114   QObject::connect(this, SIGNAL(applyButtonPressed()), this, SLOT(accept()));
00115   
00116   setHelpButton();
00117   QObject::connect(this, SIGNAL(helpButtonPressed()), this, SLOT(help()));
00118 
00119   // construct status page
00120 
00121   QWidget *w = new QWidget(baseWidget);
00122   
00123   QVBoxLayout *tvbox = new QVBoxLayout(w);
00124   tvbox->setSpacing(2);
00125   tvbox->setMargin(5);
00126 
00127   label = new QLabel(tr("Download Status:"), w);
00128   tvbox->addWidget(label);
00129   
00130   statusLog = new QTextEdit(w);
00131   statusLog->setTextFormat(Qt::LogText);
00132   statusLog->setReadOnly(TRUE);
00133   tvbox->addWidget(statusLog);
00134   
00135   tabs->addTab(w, tr("Status"));
00136 
00137   // construct download parms page
00138 
00139   w = new QWidget(baseWidget);
00140   
00141   QGridLayout *tgrid = new QGridLayout(w, 3, 2);
00142   tgrid->setSpacing(2);
00143   tgrid->setMargin(5);
00144   tgrid->setColStretch(1, 1);
00145 
00146   label = new QLabel(tr("Retry"), w);
00147   tgrid->addWidget(label, 0, 0);
00148   
00149   retrySpin = new QSpinBox(0, 99, 1, w);
00150   retrySpin->setValue(3);
00151   tgrid->addWidget(retrySpin, 0, 1);
00152 
00153   label = new QLabel(tr("Timeout"), w);
00154   tgrid->addWidget(label, 1, 0);
00155   
00156   timeoutSpin = new QSpinBox(0, 99, 1, w);
00157   timeoutSpin->setValue(15);
00158   tgrid->addWidget(timeoutSpin, 1, 1);
00159   
00160   tabs->addTab(w, tr("Timeout"));
00161 }
00162 
00163 void QuotePlugin::stripJunk (QString &d, QString &s)
00164 {
00165   s = d.stripWhiteSpace();
00166 
00167   while (1)
00168   {
00169     int p = s.find('"', 0, TRUE);
00170     if (p == -1)
00171       break;
00172     else
00173       s.remove(p, 1);
00174   }
00175 }
00176 
00177 bool QuotePlugin::setTFloat (QString &d, bool flag)
00178 {
00179   QString s = d;
00180   
00181   while (s.contains("A"))
00182     s = s.remove(s.find("A", 0, TRUE), 1);
00183   
00184   while (s.contains("B"))
00185     s = s.remove(s.find("B", 0, TRUE), 1);
00186 
00187   while (s.contains("K"))
00188   {
00189     s = s.remove(s.find("K", 0, TRUE), 1);
00190     s.append("000");
00191   }
00192 
00193   if (flag)
00194   {
00195     while (s.contains(","))
00196       s = s.replace(s.find(",", 0, TRUE), 1, ".");
00197   }
00198   else
00199   {
00200     while (s.contains(","))
00201       s = s.remove(s.find(",", 0, TRUE), 1);
00202   }
00203 
00204   bool ok;
00205   tfloat = s.toFloat(&ok);
00206   if (! ok)
00207     return TRUE;
00208   else
00209     return FALSE;
00210 }
00211 
00212 void QuotePlugin::createDirectory (QString &d, QString &path)
00213 {
00214   Config config;
00215   config.getData(Config::DataPath, path);
00216   
00217   QStringList l = QStringList::split("/", d, FALSE);
00218   int loop;
00219   for (loop = 0; loop < (int) l.count(); loop++)
00220   {
00221     path.append("/");
00222     path.append(l[loop]);
00223     QDir dir(path);
00224     if (! dir.exists(path, TRUE))
00225     {
00226       if (! dir.mkdir(path, TRUE))
00227       {
00228         path.truncate(0);
00229         return;
00230       }
00231     }
00232   }
00233 }
00234 
00235 void QuotePlugin::getPluginName (QString &d)
00236 {
00237   d = pluginName;
00238 }
00239 
00240 void QuotePlugin::getHelpFile (QString &d)
00241 {
00242   d = helpFile;
00243 }
00244 
00245 void QuotePlugin::getFile (QString &url)
00246 {
00247   if (op)
00248   {
00249     op->stop();
00250     delete op;
00251   }
00252 
00253   data.truncate(0);
00254   
00255   timer->start(timeoutSpin->value() * 1000, TRUE);
00256   
00257   op = new QUrlOperator(url);
00258   connect(op, SIGNAL(finished(QNetworkOperation *)), this, SLOT(getFileDone(QNetworkOperation *)));
00259   connect(op, SIGNAL(data(const QByteArray &, QNetworkOperation *)), this, SLOT(dataReady(const QByteArray &, QNetworkOperation *)));
00260 // qDebug("url=%s", url.latin1());
00261   op->get();
00262 }
00263 
00264 void QuotePlugin::copyFile (QString &url, QString &file)
00265 {
00266   if (op)
00267   {
00268     op->stop();
00269     delete op;
00270   }
00271     
00272   timer->start(timeoutSpin->value() * 1000, TRUE);
00273   
00274   QDir dir(file);
00275   dir.remove(file);
00276 
00277   op = new QUrlOperator();
00278   connect(op, SIGNAL(finished(QNetworkOperation *)), this, SLOT(copyFileDone(QNetworkOperation *)));
00279   op->copy(url, file, FALSE, FALSE);
00280 }
00281 
00282 void QuotePlugin::getFileDone (QNetworkOperation *o)
00283 {
00284   if (! o)
00285     return;
00286 
00287   if (o->state() == QNetworkProtocol::StDone && o->operation() == QNetworkProtocol::OpGet)
00288   {
00289     timer->stop();
00290     emit signalGetFileDone(FALSE);
00291     return;
00292   }
00293 
00294   if (o->state() == QNetworkProtocol::StFailed)
00295   {
00296     timer->stop();
00297     emit signalGetFileDone(TRUE);
00298   }
00299 }
00300 
00301 void QuotePlugin::copyFileDone (QNetworkOperation *o)
00302 {
00303   if (! o)
00304     return;
00305 
00306   if (o->state() != QNetworkProtocol::StDone)
00307     return;
00308 
00309   if (o->errorCode() != QNetworkProtocol::NoError)
00310   {
00311     timer->stop();
00312     QString s = QObject::tr("Download error: ") + o->protocolDetail();
00313     emit signalCopyFileDone(s);
00314     return;
00315   }
00316   
00317   QDir dir(file);
00318   if (! dir.exists(file, TRUE))
00319     return;
00320   
00321   timer->stop();
00322 
00323   emit signalCopyFileDone(QString());
00324 }
00325 
00326 void QuotePlugin::dataReady (const QByteArray &d, QNetworkOperation *)
00327 {
00328   int loop;
00329   for (loop = 0; loop < (int) d.size(); loop++)
00330     data.append(d[loop]);
00331 }
00332 
00333 void QuotePlugin::slotTimeout ()
00334 {
00335   timer->stop();
00336   if (op)
00337     op->stop();
00338   emit signalTimeout();
00339 }
00340 
00341 void QuotePlugin::getQuotes ()
00342 {
00343   statusLog->clear();
00344   QString s(tr("Any errors will be listed, otherwise silent.\nUpdating ..."));
00345   printStatusLogMessage(s);
00346   disableGUI();
00347   update();
00348 }
00349 
00350 void QuotePlugin::downloadComplete ()
00351 {
00352   enableGUI();
00353   emit chartUpdated();
00354 }
00355 
00356 void QuotePlugin::cancelDownload ()
00357 {
00358   QString s(tr("Update cancelled."));
00359   printStatusLogMessage(s);
00360   enableGUI();
00361 }
00362 
00363 void QuotePlugin::enableGUI ()
00364 {
00365   QString s("update"); 
00366   toolbar->setButtonStatus(s, TRUE);
00367   s = "cancelDownload";
00368   toolbar->setButtonStatus(s, FALSE);
00369 }
00370 
00371 void QuotePlugin::disableGUI ()
00372 {
00373   QString s("update"); 
00374   toolbar->setButtonStatus(s, FALSE);
00375   s = "cancelDownload";
00376   toolbar->setButtonStatus(s, TRUE);
00377 }
00378 
00379 void QuotePlugin::printStatusLogMessage (QString &d)
00380 {
00381   statusLog->append(d);
00382 }
00383 
00384 void QuotePlugin::help ()
00385 {
00386   HelpWindow *hw = new HelpWindow(this, helpFile);
00387   hw->show();
00388 }
00389 
00390 // virtual function
00391 void QuotePlugin::update ()
00392 {
00393 }
00394 
00395 void QuotePlugin::slotWakeup ()
00396 {
00397   emit signalWakeup();
00398 }
00399