public CanvasImageDownloadViewModel(List<ServiceSheetViewModel> serviceVMs, CanvasUserViewModel canvasUserEntered)
 {
     AllServices = serviceVMs;
     CanvasUser = canvasUserEntered;
     setMaxMinProgressBar();
     BackgroundWorker worker = new BackgroundWorker();
     worker.WorkerReportsProgress = true;
     worker.DoWork += worker_DoWork;
     worker.ProgressChanged += worker_ProgressChanged;
     worker.RunWorkerCompleted += worker_RunWorkerCompleted;
     worker.RunWorkerAsync();
 }
        public static ImageSource downloadImage(string downloadUrl, CanvasUserViewModel currentUser)
        {
            string canvasUrl = "https://www.gocanvas.com/apiv2/images.xml?image_id=" + downloadUrl + "&username="******"&password="******""))
                {
                    return null;
                }
                
                MemoryStream ms = new MemoryStream(downloadedData);

                BitmapImage bmImage = new BitmapImage();
                bmImage.BeginInit();
                bmImage.StreamSource = ms;
                bmImage.EndInit();

                returnedImage = bmImage;
            }
            catch
            {
                MessageBox.Show("Unable to download data.");
                return null;
            }

            //Need to check for errors
            //string errorCode = validateCanvasXml(rootElement);

            //if (!errorCode.Equals(""))
            //{
            //    return null;
            //}
            return returnedImage;
        }
        //RT 12/10/16 - Rewriting to use new classes of servicesheet, serviceDay
        //public static List<oldServiceSubmissionModel> downloadXml(string canvasUsername, string canvasPassword, string beginDate, string endDate)
        //RT 26/11/16 - Changing to use canvasUser
        //public static ObservableCollection<ServiceSheetViewModel> downloadXml(string canvasUsername, string canvasPassword, DateTime beginDate, DateTime endDate)
        public static ObservableCollection<ServiceSheetViewModel> downloadXml(CanvasUserViewModel canvasUserVM, DateTime beginDate, DateTime endDate)
        {
            //RT 13/10/16 - Date must be in the USA format
            string startDateStr = beginDate.ToString("MM/dd/yyyy", null);
            string endDateStr = endDate.ToString("MM/dd/yyyy", null);
            string canvasUrl = "https://www.gocanvas.com/apiv2/submissions.xml?username="******"&password="******"&form_id=1285373&begin_date=" + startDateStr + "&end_date=" + endDateStr;

            XElement rootElement;

            rootElement = loadElements(canvasUrl);
            if (rootElement == null)
            {
                //Failed validation or download failed.  Error will have been shown
                return null;
            }

            XElement totalPagesNode = rootElement.Element("TotalPages");
            int noOfPages = Convert.ToInt32(totalPagesNode.Value);

            //RT 13/10/16 - Adding support for multiple pages from Canvas
            int pageCounter = 1;

            List<ServiceSheetViewModel> allSubmissions = new List<ServiceSheetViewModel>();

            while (pageCounter <= noOfPages)
            {
                if (pageCounter == 1)
                {
                    XElement submissions = rootElement.Element("Submissions");

                    //ServiceSubmissionModel[] allSubmissions;
                    allSubmissions.AddRange(parseSubmissions(submissions));
                }
                else
                {
                    canvasUrl = "https://www.gocanvas.com/apiv2/submissions.xml?username="******"&password="******"&form_id=1285373&begin_date=" + startDateStr + "&end_date=" + endDateStr + "&page=" +pageCounter;

                    rootElement = loadElements(canvasUrl);
                    if (rootElement == null)
                    {
                        //Failed validation or download failed.  Error will have been shown
                        return null;
                    }

                    XElement submissions = rootElement.Element("Submissions");

                    //ServiceSubmissionModel[] allSubmissions;
                    allSubmissions.AddRange(parseSubmissions(submissions));
                }
                pageCounter++;
            }

            ObservableCollection<ServiceSheetViewModel> retval = new ObservableCollection<ServiceSheetViewModel>(allSubmissions);
            return retval;
        }
        private void downloadCanvasData(object canvasPasswordBox)
        {
            CanvasUserVM = new CanvasUserViewModel();
            CanvasUserView userView = new CanvasUserView();
            userView.DataContext = CanvasUserVM;
            bool? userResult = userView.ShowDialog();

            //RT 3/12/16 - The box may have been cancelled
            if (userResult != true)
            {
                return;
            }
            
            //CanvasUserVM.CanvasPasswordBox = (PasswordBox)canvasPasswordBox;
            //RT 26/11/16 - Changing the password to use a PasswordBox for security
            //AllServiceSheets = CanvasDataReader.downloadXml(CanvasUser.Username, CanvasUser.Password, DtStartSubmissionsDownload, DtEndSubmissionsDownload);
            AllServiceSheets = CanvasDataReader.downloadXml(CanvasUserVM, DtStartSubmissionsDownload, DtEndSubmissionsDownload);

            //If no submissions have been returned, then exit.  None available, or error has occured.  Error will have been shown already
            if (AllServiceSheets == null)
            {
                return;
            }

            //Now we need to download the images from Canvas, using a progress bar
            CanvasImageDownloadView imageDownloadView = new CanvasImageDownloadView();
            List<ServiceSheetViewModel> serviceSheetList = new List<ServiceSheetViewModel>(AllServiceSheets);
            CanvasImageDownloadViewModel imageVM = new CanvasImageDownloadViewModel(serviceSheetList, CanvasUserVM);
            imageDownloadView.DataContext = imageVM;
            bool? result = imageDownloadView.ShowDialog();
            //Set the servicesheets back to the result from the dialog

            if (result == true)
            {
                AllServiceSheets = new ObservableCollection<ServiceSheetViewModel>(imageVM.AllServices);
            }
            else
            {
                AllServiceSheets = new ObservableCollection<ServiceSheetViewModel>();
            }
            
        }