コード例 #1
0
ファイル: ConverterViewModel.cs プロジェクト: yvdjee/PSEGet
        // process message from DownloadAndConvertViewModel
        private void OnReceiveConvertParamMessage(MessageBase message)
        {
            if (message is DownloadAndConvertParamMessage)
            {
                IsBusy = true;

                var    msg      = message as DownloadAndConvertParamMessage;
                string savePath = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "\\Reports";

                var t = new Thread(DownloadAndConvertHelper.DownloadAndConvert);
                t.IsBackground = true;
                var param = new DownloadAndConvertParams();

                param.threadObject   = t;
                param.DownloadUri    = msg.DownloadURI;
                param.FromDate       = msg.FromDate;
                param.ToDate         = msg.ToDate;
                param.SavePath       = savePath;
                param.OutputSettings = OutputSettings;

                param.ReportDownloadedHandler =
                    (s, e) =>
                {
                    Action appMessage;
                    string fileName = Path.GetFileName(((ReportDownloader)s).CurrentDownloadFile);
                    if (e.Error != null)
                    {
                        if (e.Error is WebException)
                        {
                            appMessage = () =>
                            {
                                string msgText = "Failed to download " + fileName;
                                Messenger.Default.Send <ShowAppMessage, ProgressDialogViewModel>(
                                    new ShowAppMessage
                                {
                                    MessageTitle = msgText,
                                    AppMessage   = msgText + "\n" + e.Error.Message
                                });
                            };
                        }
                        else
                        {
                            appMessage = () =>
                            {
                                Messenger.Default.Send(new ShowProgressDialogMessage());
                                Messenger.Default.Send <ShowAppMessage, ProgressDialogViewModel>(
                                    new ShowAppMessage
                                {
                                    MessageTitle = "Error",
                                    AppMessage   = e.Error.Message
                                });
                            };
                        }
                    }
                    else
                    {
                        appMessage = null;
                        // download success
                        //appMessage = () =>
                        //{
                        //    Messenger.Default.Send<ShowAppMessage, ProgressDialogViewModel>(
                        //        new ShowAppMessage() { MessageTitle = "Done.", AppMessage = "Downloaded " + fileName }
                        //        );
                        //};
                    }
                    if (appMessage != null)
                    {
                        Application.Current.Dispatcher.Invoke(appMessage, null);
                    }
                };

                param.DownloadAllCompletedHandler =
                    (s, e) =>
                {
                    SendAppMessageDelegate appMessage = () =>
                    {
                        IsBusy = false;

                        var reportDownloader = (ReportDownloader)s;
                        int successCount     = (from f in reportDownloader.DownloadedFiles
                                                where (f.Success && f.Converted)
                                                select f).Count();
                        int failedCount = (from f in reportDownloader.DownloadedFiles
                                           where f.Success == false
                                           select f).Count();

                        string msgText =
                            String.Format("Successfully downloaded and converted: {0}.\nFailed count: {1}.",
                                          successCount, failedCount);
                        Messenger.Default.Send <ShowAppMessage, ProgressDialogViewModel>(
                            new ShowAppMessage {
                            AppMessage = msgText, MessageTitle = "Done."
                        });

                        Messenger.Default.Send <ShowAppMessage, ProgressDialogViewModel>(
                            new ShowAppMessage {
                            AppMessage = "Done!", MessageTitle = "Done!"
                        });
                    };

                    Application.Current.Dispatcher.Invoke(appMessage, null);
                };

                param.ConvertCompleteCallback =
                    (s, pseDocument) =>
                {
                    var dataService = ServiceLocator.Current.GetInstance <IPSEGetDataService>();    //new PSEGetDataService();
                    dataService.SaveTradeData(pseDocument);

                    Action appMessageAction = () =>
                    {
                        string fileName = Path.GetFileName(((ReportDownloader)s).CurrentDownloadFile);
                        Messenger.Default.Send <ShowAppMessage, ProgressDialogViewModel>(
                            new ShowAppMessage {
                            MessageTitle = "Done.", AppMessage = "Converted " + fileName
                        }
                            );
                    };
                    Application.Current.Dispatcher.Invoke(appMessageAction);
                };

                param.OnStartDownloadProcess =
                    (s, e) =>
                {
                    SendAppMessageDelegate appMessage = () =>
                    {
                        Messenger.Default.Send(
                            new ShowProgressDialogMessage());
                        Messenger.Default.Send <ShowAppMessage, ProgressDialogViewModel>(
                            new ShowAppMessage {
                            MessageTitle = "Trying..."
                        });
                    };
                    Application.Current.Dispatcher.Invoke(appMessage, null);
                };

                param.DownloadProgressHandler =
                    (s, e) =>
                {
                    SendAppMessageDelegate sendMsg = () =>
                    {
                        Messenger.Default.Send <ShowAppMessage, ProgressDialogViewModel>(
                            new ShowAppMessage
                        {
                            MessageTitle = "Downloading " + Path.GetFileName((string)s) + "..."
                        }
                            );
                    };
                    Application.Current.Dispatcher.Invoke(sendMsg, null);
                };

                param.BeforeConvertCallback =
                    s =>
                {
                    SendAppMessageDelegate sendMsg = () =>
                    {
                        Messenger.Default.Send <ShowAppMessage, ProgressDialogViewModel>(
                            new ShowAppMessage {
                            MessageTitle = "Converting " + Path.GetFileName(s) + "..."
                        }
                            );
                    };
                    Application.Current.Dispatcher.Invoke(sendMsg, null);
                };

                param.ConvertErrorHandler =
                    (downloader, e) =>
                {
                    Action appMessageAction = () =>
                    {
                        string msgText = "Failed to convert " +
                                         Path.GetFileName(((ReportDownloader)downloader).CurrentDownloadFile);
                        Messenger.Default.Send <ShowAppMessage, ProgressDialogViewModel>(
                            new ShowAppMessage
                        {
                            AppMessage   = msgText + "\n" + e.Message,
                            MessageTitle = msgText
                        });
                    };
                    Application.Current.Dispatcher.Invoke(appMessageAction);
                };

                param.ProcessCompletedCallback =
                    pseDocument =>
                {
                    if (pseDocument != null)
                    {
                        ShowExchangeNotice(pseDocument);
                    }
                };

                Messenger.Default.Send(new CloseWindowMessage());

                t.Start(param);

                //this.IsBusy = false;
            }
        }
コード例 #2
0
ファイル: ConverterViewModel.cs プロジェクト: yvdjee/PSEGet
        private void OnRcvConvertFromFileDataParamMessage(MessageBase msg)
        {
            if (msg is ConvertFromFileDataParamMessage)
            {
                var dataParam = msg as ConvertFromFileDataParamMessage;
                if (dataParam.FileList.Count() == 0)
                {
                    Messenger.Default.Send(new ShowAppMessage {
                        MessageTitle = "Information", AppMessage = "Nothing to convert."
                    });
                    return;
                }

                var t = new Thread(ConvertFromFileHelper.ConvertFromFiles);
                t.IsBackground = false;

                var param = new ConvertFromFilesParam();
                param.FileList       = dataParam.FileList;
                param.OutputSettings = OutputSettings;
                param.threadObject   = t;

                IsBusy = true;
                ShowCloseProgressDialogDelegate showProgressDelegate = () =>
                {
                    Messenger.Default.Send(new CloseWindowMessage());
                    Messenger.Default.Send(new ShowProgressDialogMessage());
                };

                ShowCloseProgressDialogDelegate closeProgressDelegate = () =>
                {
                    IsBusy = false;
                    Messenger.Default.Send <ShowAppMessage, ProgressDialogViewModel>(
                        new ShowAppMessage {
                        MessageTitle = "Done!", AppMessage = "Done."
                    });
                };

                param.OnStartProcess = () => { Application.Current.Dispatcher.Invoke(showProgressDelegate, null); };

                param.BeforeConvertCallback = reportFilename =>
                {
                    SendAppMessageDelegate appMessage = () =>
                    {
                        Messenger.Default.Send <ShowAppMessage, ProgressDialogViewModel>(
                            new ShowAppMessage {
                            MessageTitle = "Converting " + Path.GetFileName(reportFilename) + "..."
                        }
                            );
                    };
                    Application.Current.Dispatcher.Invoke(appMessage, null);
                };

                param.ProgressCallback = (reportFilename, pseDocument) =>
                {
                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        var dataService = ServiceLocator.Current.GetInstance <IPSEGetDataService>(); //new PSEGetDataService();
                        dataService.SaveTradeData(pseDocument);

                        Messenger.Default.Send <ShowAppMessage, ProgressDialogViewModel>(
                            new ShowAppMessage
                        {
                            MessageTitle = "Done.",
                            AppMessage   = "Converted " + Path.GetFileName(reportFilename)
                        }
                            );
                    });
                };

                param.ExceptionCallback = e =>
                {
                    //errorList.Add(e);
                    //System.Windows.Forms.MessageBox.Show(e.Message);
                    if (e is SQLiteException)
                    {
                        MessageBox.Show("The conversion was successful but PSEGet was unable to update the database.");
                    }
                    else
                    {
                        MessageBox.Show(e.Message);
                    }
                    IsBusy = false;
                    Application.Current.Dispatcher.Invoke(closeProgressDelegate, null);
                };
                param.CompletedCallback = pseDocument =>
                {
                    Application.Current.Dispatcher.Invoke(closeProgressDelegate, null);
                    ShowExchangeNotice(pseDocument);
                };


                t.Start(param);
            }
        }