private bool BackupBeforeRestore()
        {
            string lErrorMessage = "";
            bool   lResult       = fBackupMaker.MakeBackup(fBackupTask, BackupType.btRestore, ref lErrorMessage);

            fBackupTask.SetLastBackupStatus(lResult, lErrorMessage);
            return(lResult);
        }
        private void buttonManualBackup_Click(object sender, RoutedEventArgs e)
        {
            BackupTask lBackupTask = fSettings.Settings.BackupTasks[fSelectedBackupTaskIndex];

            string lErrorMessage = "";

            lBackupTask.SetLastBackupStatus(fBackupMaker.MakeBackup(lBackupTask, BackupType.btManual, ref lErrorMessage), lErrorMessage);

            fBackupMaker.RefreshDestinationDirectorySize(lBackupTask);

            // we made backup of currently selected item, refresh GUI to refresh last backup time
            SelectTaskByIndex(-1, false); // reselect same item, refresh interface
        }
        private void buttonNamedBackup_Click(object sender, RoutedEventArgs e)
        {
            NamedBackup lNamedBackupWindow = new NamedBackup();

            lNamedBackupWindow.Owner = this;
            lNamedBackupWindow.WindowStartupLocation = WindowStartupLocation.CenterOwner;

            if (lNamedBackupWindow.ShowDialog() == true)
            {
                BackupTask lBackupTask = fSettings.Settings.BackupTasks[fSelectedBackupTaskIndex];

                string lErrorMessage = "";
                lBackupTask.SetLastBackupStatus(fBackupMaker.MakeBackup(lBackupTask, BackupType.btManualWithCustomPostfix, lNamedBackupWindow.GetBackupName(), ref lErrorMessage), lErrorMessage);

                fBackupMaker.RefreshDestinationDirectorySize(lBackupTask);

                // we made backup of currently selected item, refresh GUI to refresh last backup time
                SelectTaskByIndex(-1, false); // reselect same item, refresh interface
            }
        }
        private void CheckTasks()
        {
            bool  lRefreshComboBox       = false;
            Int64 lSettingsChangeCounter = 0;

            for (int lIndex = 0; lIndex < fSettings.Settings.BackupTasks.Count; ++lIndex)
            {
                BackupTask lBackupTask       = fSettings.Settings.BackupTasks[lIndex];
                bool       lItemUpdateNeeded = false;

                // calculate global ChangeCount of all task, it will be used to see if there was some change made recently
                // we calculate ChangeCount only for parameters that we want to check
                lSettingsChangeCounter += lBackupTask.ChangeCounter + lBackupTask.Settings.ChangeCounter;

                // if paths are wrong we can't do anything, go to next task
                if (lBackupTask.Settings.SourcePathHelper.IsEmpty || String.IsNullOrEmpty(lBackupTask.Settings.DestinationPath))
                {
                    continue;
                }

                // check flag from SourceFileWatcher
                if (lBackupTask.SourceFilesChanged)
                {
                    // if auto backup enabled and check frequency
                    if (lBackupTask.Settings.IsAutoBackupActive && fBackupMaker.CheckBackupFrequency(lBackupTask))
                    {
                        string lErrorMessage  = "";
                        bool   lErrorOccurred = false;

                        bool lFileWasModified = false;
                        bool lFileIsLocked    = false;

                        // we will do something, anything happens it GUI need to be updated
                        lItemUpdateNeeded = true;

                        // check for files modification, function result success/error
                        if (!fBackupMaker.CheckFilesToBackup(lBackupTask, ref lFileWasModified, ref lFileIsLocked, ref lErrorMessage))
                        {
                            // error occurred (or file is locked)
                            lRefreshComboBox = true;
                            lErrorOccurred   = true;
                            lBackupTask.SetLastBackupStatus(!lErrorOccurred, lErrorMessage);
                        }
                        else if (lFileWasModified)
                        {
                            // success and file was modified
                            lErrorOccurred = !fBackupMaker.MakeBackup(lBackupTask, BackupType.btNormal, ref lErrorMessage);
                            lBackupTask.SetLastBackupStatus(!lErrorOccurred, lErrorMessage);
                            lRefreshComboBox = true;
                        }
                        else
                        {
                            // no error (and files are not locked) but also nothing changed
                            if (!String.IsNullOrEmpty(lBackupTask.LastBackupErrorMessage))
                            {
                                lBackupTask.LastBackupErrorMessage = "";
                                lRefreshComboBox = true;
                            }
                        }

                        // backup was updated or there was no change that is important for us
                        // if error then try again latter (do not reset FilesChanged flag)
                        if (!lErrorOccurred)
                        {
                            lBackupTask.SourceFilesChanged = false;
                        }
                    }
                }

                // check flag from DestinationFileWatcher
                if (lBackupTask.DestinationDirectoryChanged)
                {
                    string lErrorMessage = "";
                    // to check if Size was changed
                    long lLastDestinationDirectorySize = lBackupTask.DestinationDirectorySize;

                    // backup was made so we need to update destination directory size once more
                    fBackupMaker.RefreshDestinationDirectorySize(lBackupTask);
                    // if destination directory size limit is set then remove older files
                    fBackupMaker.CleanUpOldBackups(lBackupTask, ref lErrorMessage);

                    lBackupTask.DestinationDirectoryChanged = false;

                    // if size is same as before skip updating
                    lItemUpdateNeeded = (lLastDestinationDirectorySize != lBackupTask.DestinationDirectorySize);
                }

                // if we made a backup of currently selected item we need to refresh data on GUI
                if (fSelectedBackupTaskIndex == lIndex)
                {
                    // check if something change or maybe we updated destination directory size
                    if (lItemUpdateNeeded)
                    {
                        SelectTaskByIndex(lIndex, false); // reselect same item, refresh interface
                    }
                }
            }

            fTasksCheckedAtLeastOnce = true;

            // Last Backup Time of some item changed, refresh list
            if (lRefreshComboBox)
            {
                FillComboBoxItems();
            }

            // save settings if something changed
            if (fSettingsChangeCounter != lSettingsChangeCounter)
            {
                if (fSettingsChangeCounter != -1)
                {
                    fSettings.SaveConfig();
                }
                fSettingsChangeCounter = lSettingsChangeCounter;
            }
        }