コード例 #1
0
        public static BackupTask GetDefaultBackupTask(string name)
        {
            BackupTask task = new BackupTask();

            task.Name = name;

            Collection <CompressionItem> items = task.FilesFoldersList;
            CompressionItem item = new CompressionItem();

            item.CompressionDegree = CompressionDegree.Normal;
            item.IsFolder          = true;
            item.Target            = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
            items.Add(item);

            item = new CompressionItem();
            item.CompressionDegree = CompressionDegree.Normal;
            item.IsFolder          = true;
            item.Target            = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop);
            items.Add(item);

            string firefoxSettings = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"Mozilla\Firefox\Profiles");

            if (Directory.Exists(firefoxSettings))
            {
                item = new CompressionItem();
                item.CompressionDegree = CompressionDegree.Normal;
                item.IsFolder          = true;
                item.Target            = firefoxSettings;
                items.Add(item);
            }

            string thunderBirdSettings = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"Thunderbird\Profiles");

            if (Directory.Exists(thunderBirdSettings))
            {
                item = new CompressionItem();
                item.CompressionDegree = CompressionDegree.Normal;
                item.IsFolder          = true;
                item.Target            = thunderBirdSettings;
                items.Add(item);
            }

            task.UnscheduleAllDays();
            task.Hours   = Constants.DefaultHours;
            task.Minutes = Constants.DefaultMinutes;

            return(task);
        }
コード例 #2
0
        /// <summary>
        /// Loads settings from an xml document
        /// </summary>
        /// <returns>The options</returns>
        /// <exception cref="OptionsException">Any problems during loading</exception>
        public static ProgramOptions LoadSettings()
        {
            ProgramOptions options = new ProgramOptions();

            try
            {
                if (File.Exists(Files.ProfileFile))
                {
                    XmlDocument document = new XmlDocument();
                    document.Load(Files.ProfileFile);

                    options.AmountOf7ZipProcessesToProcessSynchronously = readNode(document, "/Settings/Core/Performance/AmountOf7ZipProcessesToProcessSynchronously", Constants.AmountOf7ZipProcessesToProcessSynchronouslyMinimum, Constants.AmountOf7ZipProcessesToProcessSynchronouslyMaximum, Constants.AmountOf7ZipProcessesToProcessSynchronouslyDefault);
                    options.AmountOfStoragesToProcessSynchronously      = readNode(document, "/Settings/Core/Performance/AmountOfStoragesToProcessSynchronously", Constants.AmountOfStoragesToProcessSynchronouslyMinimum, Constants.AmountOfStoragesToProcessSynchronouslyMaximum, Constants.AmountOfStoragesToProcessSynchronouslyDefault);
                    string priority = readNode(document, "/Settings/Core/Performance/ProcessingPriority", ThreadPriority.BelowNormal.ToString());
                    options.Priority = (ThreadPriority)ThreadPriorityLevel.Parse(typeof(ThreadPriority), priority);

                    options.DontCareAboutPasswordLength = readNode(document, "/Settings/Core/Security/DontCareAboutPasswordLength", false);

                    string logLevel = readNode(document, "/Settings/Core/Logging/Level", LogLevel.Normal.ToString());
                    options.LoggingLevel = (LogLevel)ThreadPriorityLevel.Parse(typeof(LogLevel), logLevel);

                    options.LogsFolder = readNode(document, "/Settings/Core/Logging/Location", Directories.LogsFolder);

                    options.ShowSchedulerInTray        = readNode(document, "/Settings/Core/ScheduleApplication/ShowInTray", true);
                    options.PuttingOffBackupCpuLoading = (byte)readNode(document, "/Settings/Core/ScheduleApplication/PuttingOffBackupCpuLoading", Constants.MinimumCpuLoading, Constants.MaximumCpuLoading, Constants.DefaultCpuLoading);
                    options.DontNeedScheduler          = readNode(document, "/Settings/Core/ScheduleApplication/" + _DONT_NEED_SCHEDULER_TAG, false);

                    options.HaveNoNetworkAndInternet      = readNode(document, "/Settings/Core/" + _CONFIGURATOR_TAG + "/" + _HAVE_NO_INTERNET_AND_NETWORK_TAG, false);
                    options.DontCareAboutSchedulerStartup = readNode(document, "/Settings/Core/" + _CONFIGURATOR_TAG + "/" + _DONT_CARE_ABOUT_SCHEDULER_STARTUP_TAG, false);
                    options.HideAboutTab = readNode(document, "/Settings/Core/" + _CONFIGURATOR_TAG + "/" + _HIDE_ABOUT_TAB_TAG, false);

                    XmlNodeList taskNodes = document.SelectNodes("/Settings/BackupTasks/Task");

                    foreach (XmlNode taskNode in taskNodes)
                    {
                        BackupTask task = new BackupTask();

                        task.Name           = taskNode.Attributes[_NAME].Value;
                        task.SecretPassword = taskNode[_PASSWORD].InnerText;

                        XmlNodeList compressionItemsNodes = taskNode[_WHAT_TAG].ChildNodes;
                        XmlNodeList storagesNodes         = taskNode[_WHERE_TAG].ChildNodes;
                        XmlNodeList beforeNodes           = taskNode[_CHAIN_OF_PROGRAMS_TO_RUN][_BEFORE_BACKUP].ChildNodes;
                        XmlNodeList afterNodes            = taskNode[_CHAIN_OF_PROGRAMS_TO_RUN][_AFTER_BACKUP].ChildNodes;

                        foreach (XmlNode nodeItem in beforeNodes)
                        {
                            BackupEventTaskInfo info = new BackupEventTaskInfo(
                                nodeItem.Attributes[_NAME].Value,
                                nodeItem.Attributes[_ARGUMENTS].Value);
                            task.BeforeBackupTasksChain.Add(info);
                        }

                        foreach (XmlNode nodeItem in afterNodes)
                        {
                            BackupEventTaskInfo info = new BackupEventTaskInfo(
                                nodeItem.Attributes[_NAME].Value,
                                nodeItem.Attributes[_ARGUMENTS].Value);
                            task.AfterBackupTasksChain.Add(info);
                        }

                        foreach (XmlNode compressionItemNode in compressionItemsNodes)
                        {
                            CompressionItem item = new CompressionItem(
                                compressionItemNode.Attributes[_TARGET_TAG].Value,
                                bool.Parse(compressionItemNode.Attributes[_IS_FOLDER].Value),
                                (CompressionDegree)CompressionDegree.Parse(typeof(CompressionDegree), compressionItemNode.Attributes[_COMPRESSION_DEGREE_TAG].Value));

                            task.FilesFoldersList.Add(item);
                        }

                        XmlNode schedule = taskNode[_SCHEDULE_TAG];
                        XmlNode zeroHour = schedule[_TIME_TAG];
                        XmlNode days     = schedule[_DAYS_TAG];

                        task.Hours   = byte.Parse(zeroHour.Attributes[_HOUR_TAG].Value);
                        task.Minutes = byte.Parse(zeroHour.Attributes[_MINUTE_TAG].Value);

                        foreach (DayOfWeek enumItem in DayOfWeek.GetValues(typeof(DayOfWeek)))
                        {
                            task.SetSchedulingStateOfDay(enumItem, bool.Parse(days.Attributes[enumItem.ToString()].Value));
                        }

                        foreach (XmlNode storageNode in storagesNodes)
                        {
                            Dictionary <string, string> settings = new Dictionary <string, string>();
                            foreach (XmlNode node in storageNode.ChildNodes)
                            {
                                settings.Add(node.Name, node.InnerText);
                            }

                            XmlAttribute assemblyAttribute = storageNode.Attributes[_ASSEMBLY_TAG];

                            // this is done to prevent using different assemblies of a different copies of a program
                            Assembly assembly = (assemblyAttribute != null) ?
                                                Assembly.LoadFrom(assemblyAttribute.Value) :
                                                Assembly.GetExecutingAssembly();
                            string      type    = storageNode.Attributes[_TYPE_TAG].Value;
                            StorageBase storage = (StorageBase)Activator.CreateInstance(assembly.GetType(type), settings);

                            task.Storages.Add(storage);
                        }

                        options.BackupTasks.Add(task.Name, task);
                    }
                }
            }
            catch (Exception exc)
            {
                Debug.WriteLine(exc.ToString());
                throw new OptionsException(
                          string.Format(CultureInfo.CurrentCulture, _fileInaccessibleOrCorrupted, Files.ProfileFile, exc.Message));
            }

            return(options);
        }