/** * Copyright Mikael H?gdahl - triyana@users.sourceforge.net * * This source is distributed under the terms of the Q Public License version 1.0, * created by Trolltech (www.trolltech.com). */ #include "db/DBFactory.h" #include "db/Import.h" #include "db/Security.h" #include "db/Set.h" #include "db/SetItem.h" #include "dlg/DownloadDlg.h" #include "dlg/InfoDlg.h" #include "dlg/ProgressDlg.h" #include "download/ClosedDay.h" #include "download/YahooDownload.h" #include "download/DownloadThread.h" #include "gui/XTrader.h" #include "help/Download.h" #include "Enums.h" #include "Progress.h" #include "Resource.h" #include #include #include #include #include /** * Create export dialog. */ DownloadDlg::DownloadDlg () : Fl_Dialog (515, 570, "DOWNLOAD DATA") { Fl_Window* o = this; o->box(FL_UP_BOX); { Fl_Input* o = aFile = new Fl_Input(10, 25, 245, 25, "DATA FILE"); o->labelfont(1); o->align(FL_ALIGN_TOP_LEFT); } { Fl_Box* o = aImport = new Fl_Box(10, 75, 245, 55, "AUTOIMPORT"); o->box(FL_ENGRAVED_FRAME); o->labelfont(1); o->align(FL_ALIGN_TOP_LEFT); } { Fl_Round_Button* o = aYes = new Fl_Round_Button(15, 75, 230, 30, "YES IMPORT FILE"); o->type(102); o->down_box(FL_ROUND_DOWN_BOX); } { Fl_Round_Button* o = aNo = new Fl_Round_Button(15, 100, 225, 30, "NO DONT IMPORT FILE"); o->type(102); o->down_box(FL_ROUND_DOWN_BOX); } { Fl_Input* o = aStartDate = new Fl_Input(10, 155, 245, 25, "STARTDATE"); o->labelfont(1); o->align(FL_ALIGN_TOP_LEFT); } { Fl_Input* o = aStopDate = new Fl_Input(10, 205, 245, 25, "STOPDATE"); o->labelfont(1); o->align(FL_ALIGN_TOP_LEFT); } { Fl_Choice* o = aType = new Fl_Choice(10, 255, 245, 25, "DOWNLOAD FROM"); o->down_box(FL_BORDER_BOX); o->labelfont(1); o->align(FL_ALIGN_TOP_LEFT); } { Fl_Check_Browser* o = aSet = new Fl_Check_Browser(10, 305, 245, 255, "SET LIST"); o->labelfont(1); o->align(FL_ALIGN_TOP_LEFT); } { Fl_Check_Browser* o = aSecurity = new Fl_Check_Browser(265, 305, 245, 255, "SECURITY_LIST"); o->labelfont(1); o->align(FL_ALIGN_TOP_LEFT); } aLatest = new Fl_Button(410, 25, 100, 25, "&LATEST"); aHistory = new Fl_Button(410, 60, 100, 25, "&History"); aBrowse = new Fl_Button(410, 95, 100, 25, "&BROWSE..."); aHelp = new Fl_Button(410, 130, 100, 25, "&HELP"); aClose = new Fl_Button(410, 165, 100, 25, "&CLOSE"); o->end(); aLatest->callback (Fl_Dialog::cb1_, this); aHistory->callback (Fl_Dialog::cb2_, this); aBrowse->callback (Fl_Dialog::cb3_, this); aHelp->callback (Fl_Dialog::cb5_, this); aClose->callback (Fl_Dialog::cb4_, this); label (STRING(DLG_DOWNLOAD)); aFile->label (STRING(RESULT_FILE)); aImport->label (STRING(IMPORT)); aYes->label (STRING(IMPORT_YES)); aNo->label (STRING(IMPORT_NO)); aStartDate->label (STRING(START_DATE)); aStopDate->label (STRING(STOP_DATE)); aType->label (STRING(DOWNLOAD_FROM)); aSet->label (STRING(SELECT_SET)); aSecurity->label (STRING(SELECT_SECURITY)); aLatest->label (STRING(LATEST)); aHistory->label (STRING(HISTORY)); aBrowse->label (STRING(BROWSE)); aHelp->label (STRING(HELP)); aClose->label (STRING(CLOSE_)); aFile->labelsize (XTrader::FontSize()); aImport->labelsize (XTrader::FontSize()); aYes->labelsize (XTrader::FontSize()); aNo->labelsize (XTrader::FontSize()); aStartDate->labelsize (XTrader::FontSize()); aStopDate->labelsize (XTrader::FontSize()); aType->labelsize (XTrader::FontSize()); aSet->labelsize (XTrader::FontSize()); aSecurity->labelsize (XTrader::FontSize()); XTrader::FixButton (aLatest); XTrader::FixButton (aHistory); XTrader::FixButton (aClose); XTrader::FixButton (aHelp); XTrader::FixButton (aBrowse); MHVector* ve = DBFactory::LoadSet (); ve->Sort(); for (int f = 0; f < ve->Size(); f++) { Set* set = (Set*) (*ve)[f]; aSet->add (set->GetSet()->Get()); } ve = DBFactory::LoadSecurity (); ve->Sort(Security::SORT_NAME); for (int f = 0; f < ve->Size(); f++) { Security* security = (Security*) (*ve)[f]; aSecurity->add (security->GetName()->Get()); } aFile->value ("download.xt"); aYes->value (1); aType->add (YahooDownload::Name()); aType->add (ClosedDay::Name()); aType->value (0); } /** * Download latest price data */ void DownloadDlg::cb1 () { download (XTRADER_DOWNLOAD_LATEST, 0, 0, aYes->value() == 1 ? XTRADER_DOWNLOAD_UPDATE : 0); } /** * Download history price data */ void DownloadDlg::cb2 () { if (MHDate::ValidDate (aStartDate->value()) == false || MHDate::ValidDate (aStopDate->value()) == false) fl_alert (STRING(ERROR_DATE)); else download (XTRADER_DOWNLOAD_HISTORY, aStartDate->value(), aStopDate->value(), aYes->value() == 1 ? XTRADER_DOWNLOAD_UPDATE : 0); } /** * Callback for file chooser */ void dlg_fc_callback(Fl_File_Chooser *fc, void *data) { DownloadDlg *d = static_cast(data); const char *file = fc->value(); if (file && *file) d->aFile->value(file); } /** * Browse for result file name */ void DownloadDlg::cb3 () { Fl_File_Chooser *fc = new Fl_File_Chooser (STRING(DLG_SELECT_WRITE_FILE), "*.*", Fl_File_Chooser::SINGLE, ""); fc->callback(dlg_fc_callback, this); delete fc; } /** * Quit */ void DownloadDlg::cb4 () { aRetVal = 0; } /** * Show some basic help */ void DownloadDlg::cb5 () { InfoDlg dlg (STRING(HELP_DOWNLOAD), aaDownload); dlg.show_modal (this); } /** * */ void DownloadDlg::download (int type, const char* start, const char* stop, int action) { BaseDownload* load = 0; Import* import = 0; DownloadThread* thread = 0; Security* security; SetItem* setItem; MHVector securityList; char buffer[100]; if (aFile->value () == 0 || *aFile->value () == '\0') fl_alert (STRING(ERROR_MISSING_FILE)); else { try { if (aType->value() == 0) load = new YahooDownload (); else if (aType->value() == 1) load = new ClosedDay (); else throw "VERY MUCH ERROR THANK YOU IN DownloadDlg::download"; // Get user selection of securities MHVector* ve = DBFactory::LoadSet (); MHVector setItems; // Check selected sets ve->Sort(); for (int f = 1; f <= aSet->nitems(); f++) { if (aSet->checked(f)) { Set* set = (Set*) (*ve)[f - 1]; if (set) { SetItem::Load (set, &setItems); SetItem::LoadSecurities (&setItems); for (int e = 0; e < setItems.Size(); e++) { setItem = (SetItem*) setItems[e]; if (setItem) { securityList.Push (new Security(*(setItem->GetSecurity()))); } } setItems.Erase(); } } } // Check selected securities ve = DBFactory::LoadSecurity (); ve->Sort(Security::SORT_NAME); for (int f = 1; f <= aSecurity->nitems(); f++) { if (aSecurity->checked(f)) { security = (Security*) (*ve)[f - 1]; if (security) { securityList.Push (new Security(*security)); } } } XTrader::StartWork (STRING(INFO_DOWNLOAD)); thread = new DownloadThread (); thread->aSecs = &securityList; thread->aFileName = aFile->value(); thread->aDownloadType = type; thread->aLoad = load; thread->aStart = start; thread->aStop = stop; thread->start (); while (thread->aStatus == XTRADER_WORKING) { Fl::check (); if (xtrader_abort() == true) { thread->aStatus = XTRADER_CANCEL; } else MHUtil::Sleep (0.01); } while (thread->get_finished() == false) { Fl::check (); MHUtil::Sleep (0.01); } thread->waitfor(); XTrader::StopWork (""); if (thread->aStatus == XTRADER_ERROR_NETWORK) { DBFactory::WriteLog (STRING(ERROR_NETWORK)); throw STRING(ERROR_NETWORK); } else if (thread->aStatus == XTRADER_ERROR_OPEN_FILE) { DBFactory::WriteLog ("%s (%s)", STRING(ERROR_OPEN_FILE), aFile->value()); throw STRING(ERROR_OPEN_FILE); } else if (thread->aStatus == XTRADER_CANCEL) { DBFactory::WriteLog (STRING(ERROR_CANCEL_DOWNLOAD)); throw STRING(ERROR_CANCEL_DOWNLOAD); } else if (thread->aStatus == XTRADER_DONE && action == XTRADER_DOWNLOAD_UPDATE) { // Import file into database if (MHFile::Size (aFile->value()) > 0) { MHDate date; double start = MHUtil::Time(); XTrader::StartWork (STRING(INFO_IMPORTING)); import = new Import (aFile->value()); import->Read (); XTrader::StopWork (""); snprintf (buffer, 99, "%s (%d %s / %.1fs)", STRING(INFO_IMPORT_FINISHED), import->Rows(), STRING(ROWS), MHUtil::Time() - start); XTrader::SetMessage (buffer); XTrader::SendMessage (0, "Import", "changed"); } else { XTrader::SetMessage (STRING(ERROR_NO_DATA_DOWNLOAD)); } } else if (thread->aStatus == XTRADER_DONE) { XTrader::SetMessage (STRING(DOWNLOAD_DONE)); } } catch (const char* e) { XTrader::SetError (e); } } delete load; delete import; delete thread; securityList.Erase (); Fl::redraw (); }