public bool SaveCanvasAs()
        {
            if (this.DashboardHelper == null)
            {
                Epi.Windows.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");

                AppendChildNodes(root, 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.Windows.MsgBox.ShowInformation(DashboardSharedStrings.ERROR_CANVAS_SAVE_IO_EXCEPTION);
                }
            }

            return false;
        }
        public bool SaveCanvas()
        {
            if (this.DashboardHelper == null)
            {
                Epi.Windows.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");

            AppendChildNodes(root, 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.Windows.MsgBox.ShowInformation(DashboardSharedStrings.ERROR_CANVAS_SAVE_IO_EXCEPTION);
            }

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

            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);
                    helper.IsAutoClosing = this.IsGeneratingHTMLFromCommandLine;

                    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.Windows.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.Windows.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);

                                    bool isTextFile = filePath.ToLower().Contains("text;");

                                    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) && !isTextFile)
                                    {
                                        string message = string.Format(DashboardSharedStrings.ERROR_CANVAS_DATA_SOURCE_NOT_FOUND, dsPath);
                                        Epi.Windows.MsgBox.ShowError(message);
                                        return;
                                    }
                                    // TODO: Add else if for isTextFile and path not found.
                                }
                            }
                        }
                    }

                    try
                    {
                        helper.CreateFromXml(element);
                    }
                    catch (ViewNotFoundException ex)
                    {
                        Epi.Windows.MsgBox.ShowError(ex.Message);
                        return;
                    }
                    catch (System.Data.SqlClient.SqlException ex)
                    {
                        Epi.Windows.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.Windows.MsgBox.ShowError(message);
                        return;
                    }
                    catch (System.Security.Cryptography.CryptographicException ex)
                    {
                        Epi.Windows.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.Windows.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"))
                {
                    gadgetXmlElement = element;
                }
                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;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Epi.Windows.MsgBox.ShowException(ex);
                    }
                }
                if (element.Name.ToLower().Equals("canvassettings"))
                {
                    try
                    {
                        foreach (XmlElement child in element.ChildNodes)
                        {
                            switch (child.Name.ToLower())
                            {
                                case "editmode":
                                    LoadInEditMode = bool.Parse(child.InnerText);
                                    break;
                                case "layoutmode":
                                    if (child.InnerText.ToLower().Equals("free"))
                                    {
                                        canvasMain.AllowDragging = true;
                                        statusStripButtonVerticalArrange.IsSelected = false;
                                    }
                                    else
                                    {
                                        canvasMain.AllowDragging = false;
                                        statusStripButtonVerticalArrange.IsSelected = true;
                                    }
                                    break;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Epi.Windows.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);
            }

            if (DashboardHelper.DataFilters.Count > 0)
            {
                infoPanel.Visibility = Visibility.Visible;
                infoPanel.Text = string.Format(DashboardSharedStrings.CANVAS_FILTERS_IN_EFFECT, DashboardHelper.DataFilters.GenerateReadableDataFilterString());
            }
            else
            {
                infoPanel.Visibility = Visibility.Collapsed;
                infoPanel.Text = string.Empty;
            }

            this.scrollViewer.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);
            //}
        }