1 /**
2 *  Copyright Mikael H?gdahl - triyana@users.sourceforge.net
3 *
4 *  This source is distributed under the terms of the Q Public License version 1.0,
5 *  created by Trolltech (www.trolltech.com).
6 */
7 
8 #include "db/DBFactory.h"
9 #include "db/Import.h"
10 #include "db/Security.h"
11 #include "db/Set.h"
12 #include "db/SetItem.h"
13 #include "dlg/DownloadDlg.h"
14 #include "dlg/InfoDlg.h"
15 #include "dlg/ProgressDlg.h"
16 #include "download/ClosedDay.h"
17 #include "download/YahooDownload.h"
18 #include "download/DownloadThread.h"
19 #include "gui/XTrader.h"
20 #include "help/Download.h"
21 #include "Enums.h"
22 #include "Progress.h"
23 #include "Resource.h"
24 #include <FL/fl_ask.H>
25 #include <FL/Fl_File_Chooser.H>
26 #include <MHDate.h>
27 #include <MHDebug.h>
28 #include <MHFile.h>
29 
30 
31 
32 /**
33 *  Create export dialog.
34 */
DownloadDlg()35 DownloadDlg::DownloadDlg () :
36 Fl_Dialog (515, 570, "DOWNLOAD DATA") {
37     Fl_Window* o = this;
38 
39     o->box(FL_UP_BOX);
40     { Fl_Input* o = aFile = new Fl_Input(10, 25, 245, 25, "DATA FILE");
41       o->labelfont(1);
42       o->align(FL_ALIGN_TOP_LEFT);
43     }
44     { Fl_Box* o = aImport = new Fl_Box(10, 75, 245, 55, "AUTOIMPORT");
45       o->box(FL_ENGRAVED_FRAME);
46       o->labelfont(1);
47       o->align(FL_ALIGN_TOP_LEFT);
48     }
49     { Fl_Round_Button* o = aYes = new Fl_Round_Button(15, 75, 230, 30, "YES IMPORT FILE");
50       o->type(102);
51       o->down_box(FL_ROUND_DOWN_BOX);
52     }
53     { Fl_Round_Button* o = aNo = new Fl_Round_Button(15, 100, 225, 30, "NO DONT IMPORT FILE");
54       o->type(102);
55       o->down_box(FL_ROUND_DOWN_BOX);
56     }
57     { Fl_Input* o = aStartDate = new Fl_Input(10, 155, 245, 25, "STARTDATE");
58       o->labelfont(1);
59       o->align(FL_ALIGN_TOP_LEFT);
60     }
61     { Fl_Input* o = aStopDate = new Fl_Input(10, 205, 245, 25, "STOPDATE");
62       o->labelfont(1);
63       o->align(FL_ALIGN_TOP_LEFT);
64     }
65     { Fl_Choice* o = aType = new Fl_Choice(10, 255, 245, 25, "DOWNLOAD FROM");
66       o->down_box(FL_BORDER_BOX);
67       o->labelfont(1);
68       o->align(FL_ALIGN_TOP_LEFT);
69     }
70     { Fl_Check_Browser* o = aSet = new Fl_Check_Browser(10, 305, 245, 255, "SET LIST");
71       o->labelfont(1);
72       o->align(FL_ALIGN_TOP_LEFT);
73     }
74     { Fl_Check_Browser* o = aSecurity = new Fl_Check_Browser(265, 305, 245, 255, "SECURITY_LIST");
75       o->labelfont(1);
76       o->align(FL_ALIGN_TOP_LEFT);
77     }
78     aLatest = new Fl_Button(410, 25, 100, 25, "&LATEST");
79     aHistory = new Fl_Button(410, 60, 100, 25, "&History");
80     aBrowse = new Fl_Button(410, 95, 100, 25, "&BROWSE...");
81     aHelp = new Fl_Button(410, 130, 100, 25, "&HELP");
82     aClose = new Fl_Button(410, 165, 100, 25, "&CLOSE");
83     o->end();
84 
85     aLatest->callback (Fl_Dialog::cb1_, this);
86     aHistory->callback (Fl_Dialog::cb2_, this);
87     aBrowse->callback (Fl_Dialog::cb3_, this);
88     aHelp->callback (Fl_Dialog::cb5_, this);
89     aClose->callback (Fl_Dialog::cb4_, this);
90 
91     label (STRING(DLG_DOWNLOAD));
92     aFile->label (STRING(RESULT_FILE));
93     aImport->label (STRING(IMPORT));
94     aYes->label (STRING(IMPORT_YES));
95     aNo->label (STRING(IMPORT_NO));
96     aStartDate->label (STRING(START_DATE));
97     aStopDate->label (STRING(STOP_DATE));
98     aType->label (STRING(DOWNLOAD_FROM));
99     aSet->label (STRING(SELECT_SET));
100     aSecurity->label (STRING(SELECT_SECURITY));
101 
102     aLatest->label (STRING(LATEST));
103     aHistory->label (STRING(HISTORY));
104     aBrowse->label (STRING(BROWSE));
105     aHelp->label (STRING(HELP));
106     aClose->label (STRING(CLOSE_));
107 
108     aFile->labelsize (XTrader::FontSize());
109     aImport->labelsize (XTrader::FontSize());
110     aYes->labelsize (XTrader::FontSize());
111     aNo->labelsize (XTrader::FontSize());
112     aStartDate->labelsize (XTrader::FontSize());
113     aStopDate->labelsize (XTrader::FontSize());
114     aType->labelsize (XTrader::FontSize());
115     aSet->labelsize (XTrader::FontSize());
116     aSecurity->labelsize (XTrader::FontSize());
117 
118     XTrader::FixButton (aLatest);
119     XTrader::FixButton (aHistory);
120     XTrader::FixButton (aClose);
121     XTrader::FixButton (aHelp);
122     XTrader::FixButton (aBrowse);
123 
124     MHVector* ve = DBFactory::LoadSet ();
125     ve->Sort();
126     for (int f = 0; f < ve->Size(); f++) {
127         Set* set = (Set*) (*ve)[f];
128         aSet->add (set->GetSet()->Get());
129     }
130 
131     ve = DBFactory::LoadSecurity ();
132     ve->Sort(Security::SORT_NAME);
133     for (int f = 0; f < ve->Size(); f++) {
134         Security* security = (Security*) (*ve)[f];
135         aSecurity->add (security->GetName()->Get());
136     }
137 
138     aFile->value ("download.xt");
139     aYes->value (1);
140     aType->add (YahooDownload::Name());
141     aType->add (ClosedDay::Name());
142     aType->value (0);
143 }
144 
145 
146 
147 /**
148 * Download latest price data
149 */
cb1()150 void DownloadDlg::cb1 () {
151     download (XTRADER_DOWNLOAD_LATEST, 0, 0, aYes->value() == 1 ? XTRADER_DOWNLOAD_UPDATE : 0);
152 }
153 
154 
155 
156 /**
157 * Download history price data
158 */
cb2()159 void DownloadDlg::cb2 () {
160     if (MHDate::ValidDate (aStartDate->value()) == false || MHDate::ValidDate (aStopDate->value()) == false)
161         fl_alert (STRING(ERROR_DATE));
162     else
163         download (XTRADER_DOWNLOAD_HISTORY, aStartDate->value(), aStopDate->value(), aYes->value() == 1 ? XTRADER_DOWNLOAD_UPDATE : 0);
164 }
165 
166 
167 /**
168  * Callback for file chooser
169  */
dlg_fc_callback(Fl_File_Chooser * fc,void * data)170 void dlg_fc_callback(Fl_File_Chooser *fc, void *data)
171 {
172 	DownloadDlg *d = static_cast<DownloadDlg *>(data);
173 	const char *file = fc->value();
174 	if (file && *file) d->aFile->value(file);
175 
176 }
177 
178 /**
179 * Browse for result file name
180 */
cb3()181 void DownloadDlg::cb3 () {
182     Fl_File_Chooser *fc = new Fl_File_Chooser (STRING(DLG_SELECT_WRITE_FILE), "*.*", Fl_File_Chooser::SINGLE, "");
183     fc->callback(dlg_fc_callback, this);
184     delete fc;
185 }
186 
187 
188 
189 /**
190 * Quit
191 */
cb4()192 void DownloadDlg::cb4 () {
193     aRetVal = 0;
194 }
195 
196 
197 
198 /**
199 * Show some basic help
200 */
cb5()201 void DownloadDlg::cb5 () {
202     InfoDlg dlg (STRING(HELP_DOWNLOAD), aaDownload);
203     dlg.show_modal (this);
204 }
205 
206 
207 
208 /**
209 *
210 */
download(int type,const char * start,const char * stop,int action)211 void DownloadDlg::download (int type, const char* start, const char* stop, int action) {
212     BaseDownload*       load    = 0;
213     Import*             import  = 0;
214     DownloadThread*     thread  = 0;
215     Security*           security;
216     SetItem*            setItem;
217     MHVector            securityList;
218     char                buffer[100];
219 
220     if (aFile->value () == 0 || *aFile->value () == '\0')
221         fl_alert (STRING(ERROR_MISSING_FILE));
222     else {
223         try {
224             if (aType->value() == 0)
225                 load = new YahooDownload ();
226             else if (aType->value() == 1)
227                 load = new ClosedDay ();
228             else
229                 throw "VERY MUCH ERROR THANK YOU IN DownloadDlg::download";
230 
231             // Get user selection of securities
232             MHVector* ve = DBFactory::LoadSet ();
233             MHVector  setItems;
234 
235             // Check selected sets
236             ve->Sort();
237             for (int f = 1; f <= aSet->nitems(); f++) {
238                 if (aSet->checked(f)) {
239                     Set* set = (Set*) (*ve)[f - 1];
240 
241                     if (set) {
242                         SetItem::Load (set, &setItems);
243                         SetItem::LoadSecurities (&setItems);
244 
245                         for (int e = 0; e < setItems.Size(); e++) {
246                             setItem = (SetItem*) setItems[e];
247 
248                             if (setItem) {
249                                 securityList.Push (new Security(*(setItem->GetSecurity())));
250                             }
251                         }
252                         setItems.Erase();
253                     }
254                 }
255             }
256 
257             // Check selected securities
258             ve = DBFactory::LoadSecurity ();
259             ve->Sort(Security::SORT_NAME);
260             for (int f = 1; f <= aSecurity->nitems(); f++) {
261                 if (aSecurity->checked(f)) {
262                     security = (Security*) (*ve)[f - 1];
263 
264                     if (security) {
265                         securityList.Push (new Security(*security));
266                     }
267                 }
268             }
269 
270             XTrader::StartWork (STRING(INFO_DOWNLOAD));
271             thread = new DownloadThread ();
272             thread->aSecs          = &securityList;
273             thread->aFileName      = aFile->value();
274             thread->aDownloadType  = type;
275             thread->aLoad          = load;
276             thread->aStart         = start;
277             thread->aStop          = stop;
278             thread->start ();
279 
280             while (thread->aStatus == XTRADER_WORKING) {
281                 Fl::check ();
282 
283                 if (xtrader_abort() == true) {
284                     thread->aStatus = XTRADER_CANCEL;
285                 }
286                 else
287                     MHUtil::Sleep (0.01);
288             }
289             while (thread->get_finished() == false) {
290                 Fl::check ();
291                 MHUtil::Sleep (0.01);
292             }
293             thread->waitfor();
294             XTrader::StopWork ("");
295 
296             if (thread->aStatus == XTRADER_ERROR_NETWORK) {
297                 DBFactory::WriteLog (STRING(ERROR_NETWORK));
298                 throw STRING(ERROR_NETWORK);
299             }
300             else if (thread->aStatus == XTRADER_ERROR_OPEN_FILE) {
301                 DBFactory::WriteLog ("%s (%s)", STRING(ERROR_OPEN_FILE), aFile->value());
302                 throw STRING(ERROR_OPEN_FILE);
303             }
304             else if (thread->aStatus == XTRADER_CANCEL) {
305                 DBFactory::WriteLog (STRING(ERROR_CANCEL_DOWNLOAD));
306                 throw STRING(ERROR_CANCEL_DOWNLOAD);
307             }
308             else if (thread->aStatus == XTRADER_DONE && action == XTRADER_DOWNLOAD_UPDATE) {
309                 // Import file into database
310                 if (MHFile::Size (aFile->value()) > 0) {
311                     MHDate date;
312                     double start = MHUtil::Time();
313                     XTrader::StartWork (STRING(INFO_IMPORTING));
314                     import = new Import (aFile->value());
315                     import->Read ();
316                     XTrader::StopWork ("");
317 
318                     snprintf (buffer, 99, "%s (%d %s / %.1fs)", STRING(INFO_IMPORT_FINISHED), import->Rows(), STRING(ROWS), MHUtil::Time() - start);
319                     XTrader::SetMessage (buffer);
320                     XTrader::SendMessage (0, "Import", "changed");
321                 }
322                 else {
323                     XTrader::SetMessage (STRING(ERROR_NO_DATA_DOWNLOAD));
324                 }
325             }
326             else if (thread->aStatus == XTRADER_DONE) {
327                 XTrader::SetMessage (STRING(DOWNLOAD_DONE));
328             }
329         }
330         catch (const char* e) {
331             XTrader::SetError (e);
332         }
333     }
334     delete load;
335     delete import;
336     delete thread;
337     securityList.Erase ();
338     Fl::redraw ();
339 }
340