public bool SaveCanvasAs()
        {
            if (this.dashboardHelper == null)
            {
                Epi.WPF.Dashboard.Dialogs.MsgBox.ShowInformation(DashboardSharedStrings.CANNOT_SAVE_CANVAS_NO_DATA_SOURCE);
                return false;
            }

            System.Windows.Forms.SaveFileDialog dlg = new System.Windows.Forms.SaveFileDialog();
            //Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
            dlg.DefaultExt = ".cvs7";
            dlg.Filter = "Epi Info 7 Dashboard Canvas File|*.cvs7";
            if (!string.IsNullOrEmpty(lastSavedCanvasFileName))
            {
                dlg.FileName = lastSavedCanvasFileName;
            }
            else if (dashboardHelper.IsUsingEpiProject && File.Exists(dashboardHelper.View.Project.FilePath))
            {
                FileInfo fileInfo = new FileInfo(dashboardHelper.View.Project.FilePath);
                dlg.InitialDirectory = fileInfo.DirectoryName;
            }

            System.Windows.Forms.DialogResult result = dlg.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.OK)
            //if (dlg.ShowDialog().Value)
            {
                System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
                System.Xml.XmlElement root = doc.CreateElement("DashboardCanvas");

                root.AppendChild(dashboardHelper.Serialize(doc));
                root.AppendChild(this.SerializeGadgets(doc));
                root.AppendChild(this.SerializeOutputSettings(doc));

                try
                {
                    System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(dlg.FileName, Encoding.UTF8);
                    writer.Formatting = Formatting.Indented;

                    root.WriteTo(writer);
                    writer.Close();

                    string message = string.Format(DashboardSharedStrings.NOTIFICATION_CANVAS_SAVE, dlg.FileName);
                    NotificationMessage savedMessage = new NotificationMessage(message, false, NotificationButtonType.None, false, 7);
                    panelNotification.AddMessage(savedMessage);

                    lastSavedCanvasFileName = dlg.FileName;
                    this.currentCanvas = dlg.FileName;
                    IsCanvasDirty = false;

                    if (CanvasChanged != null)
                    {
                        CanvasChanged(lastSavedCanvasFileName);
                    }

                    return true;
                }
                catch (IOException)
                {
                    Epi.WPF.Dashboard.Dialogs.MsgBox.ShowInformation(DashboardSharedStrings.ERROR_CANVAS_SAVE_IO_EXCEPTION);
                }
            }

            return false;
        }
        public bool SaveCanvas()
        {
            if (this.dashboardHelper == null)
            {
                Epi.WPF.Dashboard.Dialogs.MsgBox.ShowInformation(DashboardSharedStrings.CANNOT_SAVE_CANVAS_NO_DATA_SOURCE);
                return false;
            }

            string fileToSave = string.Empty;

            if (!string.IsNullOrEmpty(this.lastSavedCanvasFileName))
            {
                fileToSave = lastSavedCanvasFileName;
            }
            else if (!string.IsNullOrEmpty(this.CurrentCanvas))
            {
                fileToSave = this.CurrentCanvas;
            }
            else
            {
                return this.SaveCanvasAs();
            }

            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            System.Xml.XmlElement root = doc.CreateElement("DashboardCanvas");

            root.AppendChild(dashboardHelper.Serialize(doc));
            root.AppendChild(this.SerializeGadgets(doc));
            root.AppendChild(this.SerializeOutputSettings(doc));

            try
            {
                System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(fileToSave, Encoding.UTF8);
                writer.Formatting = Formatting.Indented;

                root.WriteTo(writer);
                writer.Close();

                string message = string.Format(DashboardSharedStrings.NOTIFICATION_CANVAS_SAVE, fileToSave);
                NotificationMessage savedMessage = new NotificationMessage(message, false, NotificationButtonType.None, false, 7);
                panelNotification.AddMessage(savedMessage);

                lastSavedCanvasFileName = fileToSave;
                this.currentCanvas = fileToSave;
                IsCanvasDirty = false;

                if (CanvasChanged != null)
                {
                    CanvasChanged(lastSavedCanvasFileName);
                }

                return true;
            }
            catch (IOException)
            {
                Epi.WPF.Dashboard.Dialogs.MsgBox.ShowInformation(DashboardSharedStrings.ERROR_CANVAS_SAVE_IO_EXCEPTION);
            }

            return false;
        }
        private void CreateFromXml(string fileName)
        {
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.Load(fileName);

            bool isLegacyCanvas = false;
            if (doc.InnerXml.Contains("EpiDashboard."))
            {
                isLegacyCanvas = true;
                doc.InnerXml = doc.InnerXml.Replace("EpiDashboard.", "Epi.WPF.Dashboard.");
            }

            foreach (System.Xml.XmlElement element in doc.DocumentElement.ChildNodes)
            {
                if (element.Name.ToLower().Equals("dashboardhelper"))
                {
                    DashboardHelper helper = new DashboardHelper();
                    helper.NotificationEvent += new NotificationEventHandler(dashboardHelper_NotificationEvent);

                    foreach (System.Xml.XmlElement child in element.ChildNodes)
                    {
                        // Check to see if PRJ file exists and if not, prompt the user that this is the case; then offer to select a new PRJ file.
                        if (child.Name.Equals("projectPath") && !string.IsNullOrEmpty(child.InnerText))
                        {
                            FileInfo fiProject = new FileInfo(child.InnerText);
                            FileInfo fiCanvas = new FileInfo(fileName);
                            string projectPath = child.InnerText;

                            if (File.Exists(fiCanvas.Directory + "\\" + fiProject.Name))
                            {
                                projectPath = fiCanvas.Directory + "\\" + fiProject.Name;
                                child.InnerText = projectPath;
                            }
                            else if (!System.IO.File.Exists(child.InnerText))
                            {
                                string message = string.Format(DashboardSharedStrings.ERROR_PROJECT_NOT_FOUND_FOR_CANVAS, child.InnerText);
                                Epi.WPF.Dashboard.Dialogs.MsgBox.ShowError(message);

                                System.Windows.Forms.OpenFileDialog projectDialog = new System.Windows.Forms.OpenFileDialog();
                                projectDialog.Filter = "Epi Info 7 Project File|*.prj";

                                System.Windows.Forms.DialogResult projectDialogResult = projectDialog.ShowDialog();

                                if (projectDialogResult == System.Windows.Forms.DialogResult.OK)
                                {
                                    child.InnerText = projectDialog.FileName;
                                }
                                else
                                {
                                    return;
                                }
                            }
                        }
                        else if (child.Name.Equals("connectionString") && !string.IsNullOrEmpty(child.InnerText))
                        {
                            string connStr;

                            try
                            {
                                connStr = Configuration.Decrypt(child.InnerText);
                            }
                            catch (System.Security.Cryptography.CryptographicException)
                            {
                                Epi.WPF.Dashboard.Dialogs.MsgBox.ShowError(DashboardSharedStrings.ERROR_CANNOT_DECRYPT);
                                return;
                            }

                            if (connStr.ToLower().StartsWith("provider=microsoft.ace") || connStr.ToLower().StartsWith("provider=microsoft.jet.oledb"))
                            {
                                string filePath = string.Empty;

                                int indexOf = connStr.IndexOf("Data Source=");

                                if (indexOf >= 0)
                                {
                                    indexOf += 12;
                                    filePath = connStr.Substring(indexOf, connStr.Length - indexOf);

                                    if (filePath.ToLower().Contains("extended properties"))
                                    {
                                        indexOf = filePath.IndexOf(';');
                                        if (indexOf >= 0)
                                        {
                                            filePath = filePath.Substring(0, indexOf);
                                        }
                                    }
                                    filePath = filePath.TrimStart('\"');
                                    filePath = filePath.TrimEnd('\"');

                                    FileInfo fiDataSource = new FileInfo(filePath);
                                    FileInfo fiCanvas = new FileInfo(fileName);
                                    string dsPath = filePath;

                                    if (File.Exists(fiCanvas.Directory + "\\" + fiDataSource.Name))
                                    {
                                        dsPath = fiCanvas.Directory + "\\" + fiDataSource.Name;
                                        child.InnerText = Configuration.Encrypt(dsPath);
                                    }
                                    else if (!System.IO.File.Exists(dsPath))
                                    {
                                        string message = string.Format(DashboardSharedStrings.ERROR_CANVAS_DATA_SOURCE_NOT_FOUND, dsPath);
                                        Epi.WPF.Dashboard.Dialogs.MsgBox.ShowError(message);
                                        return;
                                    }
                                }
                            }
                        }
                    }

                    try
                    {
                        helper.CreateFromXml(element);
                    }
                    catch (ViewNotFoundException ex)
                    {
                        Epi.WPF.Dashboard.Dialogs.MsgBox.ShowError(ex.Message);
                        return;
                    }
                    catch (System.Data.SqlClient.SqlException ex)
                    {
                        Epi.WPF.Dashboard.Dialogs.MsgBox.ShowError(ex.Message);
                        return;
                    }
                    catch (ApplicationException ex)
                    {
                        string message = ex.Message;
                        if (message.ToLower().Equals("error executing select query against the database."))
                        {
                            message = DashboardSharedStrings.ERROR_DATA_SOURCE_PERMISSIONS;
                        }
                        Epi.WPF.Dashboard.Dialogs.MsgBox.ShowError(message);
                        return;
                    }
                    catch (System.Security.Cryptography.CryptographicException ex)
                    {
                        Epi.WPF.Dashboard.Dialogs.MsgBox.ShowError(string.Format(SharedStrings.ERROR_CRYPTO_KEYS, ex.Message));
                        return;
                    }

                    // If its a non-Epi 7 data source, make sure the database exists
                    if (helper.Database != null)
                    {
                        try
                        {
                            bool success = helper.Database.TestConnection();
                        }
                        catch (Exception ex)
                        {
                            Epi.WPF.Dashboard.Dialogs.MsgBox.ShowError(ex.Message);
                            return;
                        }
                    }

                    this.dashboardHelper = helper;
                    this.dashboardHelper.SetDashboardControl(this);

                    canvasMain.Height = DEFAULT_CANVAS_HEIGHT;

                    RemoveAllGadgets();
                    EnableDisableMenus(true);
                    AddFilterGadget();
                    AddDefinedVariablesGadget();
                    ResetCanvasProperties();

                    this.currentCanvas = fileName;

                    //dashboardHelper.GenerateRecordCount(true);
                    UpdateRecordCount();
                    ReCacheDataSource();
                }
                if (element.Name.ToLower().Equals("gadgets"))
                {
                    foreach (XmlElement child in element.ChildNodes)
                    {
                        try
                        {
                            string value = child.Attributes["gadgetType"].Value;

                            if (isLegacyCanvas)
                            {
                                if (value.Contains("Standard"))
                                {
                                    value = value.Replace("Epi.WPF.Dashboard.", "Epi.WPF.Dashboard.Gadgets.Reporting.");
                                }
                                else
                                {
                                    value = value.Replace("Epi.WPF.Dashboard.", "Epi.WPF.Dashboard.Gadgets.Analysis.");
                                }
                            }

                            Type gadgetType = Type.GetType(value);

                            // The 2x2 gadget was removed and its functionality absorbed into the MxN (crosstab) gadget.
                            // This code, along with updates to the CreateFromXml routine in the Crosstab gadget, ensures
                            // that 2x2 gadgets created in canvas files from versions 7.0.9.61 and prior will still be
                            // loaded without issue. They will now be loaded as Crosstab gadgets instead.
                            if (child.Attributes["gadgetType"].Value.Equals("Epi.WPF.Dashboard.Gadgets.Analysis.TwoByTwoTableControl"))
                            {
                                gadgetType = Type.GetType("Epi.WPF.Dashboard.Gadgets.Analysis.CrosstabControl");
                            }

                            IGadget gadget = (IGadget)Activator.CreateInstance(gadgetType, new object[] { dashboardHelper });
                            gadget.CreateFromXml(child);
                            AddGadgetToCanvasFromFile(gadget);
                        }
                        catch (NullReferenceException)
                        {
                            Epi.WPF.Dashboard.Dialogs.MsgBox.ShowError(DashboardSharedStrings.GADGET_LOAD_ERROR);
                            return;
                        }
                    }
                }
                if (element.Name.ToLower().Equals("outputsettings"))
                {
                    try
                    {
                        foreach (XmlElement child in element.ChildNodes)
                        {
                            switch (child.Name.ToLower())
                            {
                                case "showcanvassummaryinfo":
                                    ShowCanvasSummaryInfoInOutput = bool.Parse(child.InnerText);
                                    break;
                                case "showgadgetheadings":
                                    ShowGadgetHeadingsInOutput = bool.Parse(child.InnerText);
                                    break;
                                case "showgadgetsettings":
                                    ShowGadgetSettingsInOutput = bool.Parse(child.InnerText);
                                    break;
                                case "usealternatingcolors":
                                    UseAlternatingColorsInOutput = bool.Parse(child.InnerText);
                                    break;
                                case "sortgadgets":
                                    SortGadgetsTopToBottom = bool.Parse(child.InnerText);
                                    break;
                                case "canvasheight":
                                    int height;
                                    bool success = false;
                                    success = int.TryParse(child.InnerText, out height);
                                    if (success && height > 8000)
                                    {
                                        canvasMain.Height = height;
                                    }
                                    else
                                    {
                                        canvasMain.Height = DEFAULT_CANVAS_HEIGHT;
                                    }
                                    break;
                                case "customheading":
                                    CustomOutputHeading = child.InnerText;
                                    break;
                                case "customsummary":
                                    CustomOutputSummaryText = child.InnerText;
                                    break;
                                case "customconclusion":
                                    CustomOutputConclusionText = child.InnerText;
                                    break;
                                case "customtablefontfamily":
                                    CustomOutputTableFontFamily = child.InnerText;
                                    break;
                                case "tablefontsize":
                                    TableFontSize = int.Parse(child.InnerText);
                                    break;
                            }
                        }

                        //mainTabControl.Visibility = Visibility.Visible;
                        //mainTabRow.Height = new GridLength(27);

                        dataDictionary.SetDataView(this.dashboardHelper.GenerateDataDictionaryTable().DefaultView);
                        dataDictionary.Refresh();

                        dataDisplay.SetDataView(this.dashboardHelper.GenerateView());
                        dataDisplay.Refresh();
                    }
                    catch (Exception ex)
                    {
                        Epi.WPF.Dashboard.Dialogs.MsgBox.ShowException(ex);
                    }
                }
            }

            string ldmTxt = string.Format(DashboardSharedStrings.CANVAS_LOADED, fileName);
            NotificationMessage loadedMessage = new NotificationMessage(ldmTxt, false, NotificationButtonType.None, false, 7);
            panelNotification.AddMessage(loadedMessage);

            lastSavedCanvasFileName = string.Empty;

            AddResizeAdorners();

            this.IsCanvasDirty = false;

            if (CanvasChanged != null)
            {
                CanvasChanged(currentCanvas);
            }

            this.scrollViewerAnalysis.ScrollToTop();
        }
 private void SendNotificationMessage(string message, bool showButtons, NotificationButtonType buttonType, bool requiresInteraction, int duration)
 {
     NotificationMessage msg = new NotificationMessage(message, showButtons, buttonType, requiresInteraction, duration);
     panelNotification.AddMessage(msg);
 }
        public void AddMessage(NotificationMessage message)
        {
            messageQueue.Enqueue(message);

            //if (!message.RequiresInteraction)
            //{
                btnAction.Visibility = System.Windows.Visibility.Collapsed;
                worker = new System.ComponentModel.BackgroundWorker();
                worker.DoWork += new System.ComponentModel.DoWorkEventHandler(worker_DoWork);
                worker.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(worker_WorkerCompleted);
                worker.RunWorkerAsync(message);
            //}
        }