Esempio n. 1
0
        /// <summary>
        /// Handles the TextChanged event of the FilenameTester control.
        /// Used to update the status and tooltip to show if the current path is included or excluded
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void FilenameTester_TextChanged(object sender, EventArgs e)
        {
            //Basic sanity check
            if (FilenameTester.Text.Trim().Length == 0 || m_basepath == null || m_basepath.Length == 0 || (m_basepath.Length == 1 && string.IsNullOrEmpty(m_basepath[0])))
            {
                SetTooltipMessage("");
                TestResults.Visible = false;
                return;
            }

            string filename = FilenameTester.Text;

            //Prevent exceptions based on invalid input
            if (filename.IndexOfAny(System.IO.Path.GetInvalidPathChars()) >= 0)
            {
                SetTooltipMessage(string.Format(Strings.FilterEditor.InvalidCharsInFilenameError, filename));
                TestResults.Visible = false;
                return;
            }

            try
            {
                //Certain paths cause exceptions anyway
                System.IO.Path.GetFullPath(filename);
            }
            catch
            {
                SetTooltipMessage(string.Format(Strings.FilterEditor.InvalidCharsInFilenameError, filename));
                TestResults.Visible = false;
                return;
            }

            //System.IO.Path.IsPathRooted returns true for filenames starting with a backslash on windows
            if (System.IO.Path.GetFullPath(filename) != filename)
            {
                SetTooltipMessage(string.Format(Strings.FilterEditor.FilepathIsNotAbsoluteError, filename));
                TestResults.Visible = false;
                return;
            }

            //Find the source folder that matches the path entered
            string basepath = null;
            foreach (string s in m_basepath)
                if (filename.StartsWith(Library.Utility.Utility.AppendDirSeparator(s), Library.Utility.Utility.ClientFilenameStringComparision))
                {
                    basepath = s;
                    break;
                }

            if (string.IsNullOrEmpty(basepath))
            {
                SetTooltipMessage(string.Format(Strings.FilterEditor.FilenameIsNotInAnyRootFolder, filename, m_basepath));
                TestResults.Visible = false;
                return;
            }

            if (basepath.Equals(filename, Library.Utility.Utility.ClientFilenameStringComparision))
            {
                SetTooltipMessage(string.Format(Strings.FilterEditor.FolderIsRootFolder, filename));
                TestResults.Image = imageList.Images[0];
                TestResults.Visible = true;
                return;
            }

            //Build a list of parent folders to check
            List<string> parentFolders = new List<string>();
            string folder = filename;
            while (folder.Length > basepath.Length)
            {
                folder = System.IO.Path.GetDirectoryName(folder);
                parentFolders.Add(XervBackup.Library.Utility.Utility.AppendDirSeparator(folder));
            }

            //Work from the source towards the path
            parentFolders.Reverse();

            //Test if any of the parent folders are excluded
            string compact = EncodeAsFilter(GetFilterList(true));
            Library.Utility.FilenameFilter fn = new XervBackup.Library.Utility.FilenameFilter(XervBackup.Library.Utility.FilenameFilter.DecodeFilter(compact));
            Library.Utility.IFilenameFilter match;
            foreach (string s in parentFolders)
            {
                //Skip rootpath
                if (basepath.Equals(s, Library.Utility.Utility.ClientFilenameStringComparision))
                    continue;

                if (!fn.ShouldInclude(basepath, s, out match))
                {
                    SetTooltipMessage(string.Format(Strings.FilterEditor.FolderIsExcluded, s, ((Library.Utility.RegularExpressionFilter)match).Expression.ToString()));
                    TestResults.Image = imageList.Images[1];
                    TestResults.Visible = true;
                    return;
                }
            }

            //Update image based on the inclusion status
            TestResults.Image = imageList.Images[fn.ShouldInclude(basepath, filename, out match) ? 0 : 1];

            //Set the tooltip to inform the user of the result
            if (match == null)
                SetTooltipMessage(string.Format(Strings.FilterEditor.FileIsIncludedDefault, filename));
            else if (match.Include)
                SetTooltipMessage(string.Format(Strings.FilterEditor.FileIsIncluded, filename, ((Library.Utility.RegularExpressionFilter)match).Expression.ToString()));
            else
                SetTooltipMessage(string.Format(Strings.FilterEditor.FileIsExcluded, filename, ((Library.Utility.RegularExpressionFilter)match).Expression.ToString()));

            TestResults.Visible = true;
        }
Esempio n. 2
0
        private void UpgradeFromVersionOne()
        {
            //Upgrade from the previous design with one source folder and multiple filters
            MessageBox.Show(this, Strings.SelectFiles.UpgradeWarning, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);

            string p = Library.Utility.Utility.AppendDirSeparator(m_wrapper.SourcePath);
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.LoadXml(m_wrapper.EncodedFilterXml);

            List<KeyValuePair<bool, string>> filters = new List<KeyValuePair<bool,string>>();
            foreach (System.Xml.XmlNode n in doc.SelectNodes("root/filter"))
                filters.Add(new KeyValuePair<bool, string>(bool.Parse(n.Attributes["include"].Value), n.Attributes["filter"].Value));

            //See what folders are included with the current setup
            Library.Utility.FilenameFilter filter = new XervBackup.Library.Utility.FilenameFilter(filters);
            IncludeDocuments.Checked = filter.ShouldInclude(p, m_myDocuments);
            IncludeImages.Checked = filter.ShouldInclude(p, m_myPictures);
            IncludeMusic.Checked = filter.ShouldInclude(p, m_myMusic);
            IncludeDesktop.Checked = filter.ShouldInclude(p, m_desktop);
            IncludeSettings.Checked = filter.ShouldInclude(p, m_appData);

            //Remove any filters relating to the special folders
            for (int i = 0; i < filters.Count; i++)
                foreach (string s in m_specialFolders)
                    if (s.StartsWith(p))
                    {
                        if (filters[i].Value == Library.Utility.FilenameFilter.ConvertGlobbingToRegExp(s.Substring(p.Length - 1) + "*"))
                        {
                            filters.RemoveAt(i);
                            i--;
                            break;
                        }
                    }

            //Remove any "exclude all" filters
            for (int i = 0; i < filters.Count; i++)
                if (filters[i].Key == false && filters[i].Value == ".*")
                {
                    filters.RemoveAt(i);
                    i--;
                }

            //See if there are extra filters that are not supported
            bool unsupported = false;
            foreach (KeyValuePair<bool, string> f in filters)
                if (f.Value.StartsWith(Library.Utility.FilenameFilter.ConvertGlobbingToRegExp(System.IO.Path.DirectorySeparatorChar.ToString())))
                {
                    unsupported = true;
                    break;
                }

            //If none of the special folders are included, we don't support the setup with simple mode
            unsupported |= !(IncludeDocuments.Checked | IncludeImages.Checked | IncludeMusic.Checked | IncludeDesktop.Checked | IncludeSettings.Checked);

            InnerControlContainer.Controls.Clear();
            AddFolderControl().SelectedPath = m_wrapper.SourcePath;

            //Make sure the extra filters are not included
            if (!unsupported)
            {
                doc = new System.Xml.XmlDocument();
                System.Xml.XmlNode root = doc.AppendChild(doc.CreateElement("root"));
                foreach (KeyValuePair<bool, string> f in filters)
                {
                    System.Xml.XmlNode n = root.AppendChild(doc.CreateElement("filter"));
                    n.Attributes.Append(doc.CreateAttribute("include")).Value = f.Key.ToString();
                    n.Attributes.Append(doc.CreateAttribute("filter")).Value = f.Value;
                    n.Attributes.Append(doc.CreateAttribute("globbing")).Value = "";
                }

                m_wrapper.EncodedFilterXml = doc.OuterXml;
            }

            if (unsupported)
                FolderRadio.Checked = true;
            else
                DocumentsRadio.Checked = true;
        }