Exemple #1
0
        /// <summary>
        /// Function to Load exclusion from settings to Exclude Box
        /// </summary>
        /// <returns></returns>
        public void LoadExcludeData()
        {
            settingsManager = Settings.Instance;
            settingsManager.SetOwnerWindow(this);

            ExcludeData loadedExcludeData = settingsManager.LoadExcludeData(leftPath, rightPath);
            int         fileTypeListSize  = loadedExcludeData.ExcludeFileTypeList.Count;
            int         fileNameListSize  = loadedExcludeData.ExcludeFileNameList.Count;
            int         folderListSize    = loadedExcludeData.ExcludeFolderList.Count;

            if (fileTypeListSize != 0)
            {
                for (int i = 0; i < fileTypeListSize; i++)
                {
                    excludeFileTypes.Add(loadedExcludeData.ExcludeFileTypeList[i]);
                }
            }

            if (fileNameListSize != 0)
            {
                for (int i = 0; i < fileNameListSize; i++)
                {
                    excludeFileNames.Add(loadedExcludeData.ExcludeFileNameList[i]);
                }
            }

            if (folderListSize != 0)
            {
                for (int i = 0; i < folderListSize; i++)
                {
                    excludeFolders.Add(loadedExcludeData.ExcludeFolderList[i]);
                }
            }

            if ((fileTypeListSize != 0) || (fileNameListSize != 0) || (folderListSize != 0))
            {
                if (HintText.Visibility == Visibility.Visible)
                {
                    HintText.Visibility       = Visibility.Collapsed;
                    HintIcon.Visibility       = Visibility.Collapsed;
                    ListBoxExclude.Visibility = Visibility.Visible;
                }
                ClearListBox();
                UpdateListBox();
            }
        }
        /// <summary>
        /// This method is called when user click on the sync button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonSync_Click(object sender, RoutedEventArgs e)
        {
            debugLogger.LogMessage(actualLeftPath, actualRightPath, "ButtonSync_Click()", "Sync button clicked");

            // do a check one more time
            // handle the situation when after a sync job is setup,
            // user deletes the 2 folders n click sync again
            if (!ShowSync())
                return;

            bool leftDirectoryAccessible = IsDirectoryAccessible(actualLeftPath);
            bool rightDirectoryAccessible = IsDirectoryAccessible(actualRightPath);
            if (leftDirectoryAccessible && rightDirectoryAccessible)
            {

                // Check if exclude window is enabled in settings
                int excludeWindowStatus = settingsManager.GetExcludeWindowStatus(); // 0 for disabled, 1 for enabled, -1 for error
                if (excludeWindowStatus == 1)
                {
                    debugLogger.LogMessage(actualLeftPath, actualRightPath, "ButtonSync_Click()", "Opening Exclude Window");

                    EnableInterface(false);
                    excludeWindow = new ExcludeWindow();
                    excludeWindow.Closing += new CancelEventHandler(excludeWindow_Closing);
                    excludeWindow.LeftPath = actualLeftPath;
                    excludeWindow.RightPath = actualRightPath;
                    excludeWindow.Owner = mainWindow;
                    excludeWindow.LoadExcludeData();
                    excludeWindow.LogError += new ExcludeWindow.LogHandler(excludeWindow_LogError);
                    mainWindow.Opacity = 0.2;
                    excludeWindow.ShowDialog();
                }
                else if (excludeWindowStatus == 0)
                {
                    // Make necessary changes to UI to prepare for sync
                    LeftListBox.Visibility = Visibility.Hidden;
                    RightListBox.Visibility = Visibility.Hidden;
                    LabelProgress.Visibility = Visibility.Visible;
                    LabelProgress.Content = MESSAGE_PREPARING_FOLDERS;
                    EnableInterface(false);

                    // Feed the actualleftpath and actualrightpath into SyncEngine again
                    // Safety precaution
                    synchronizer.LeftPath = actualLeftPath;
                    synchronizer.RightPath = actualRightPath;

                    // Do PreSync Calculations: count how many changes need to be done
                    // If not enough disk space, return
                    // If enough, continue to start the real sync
                    excludeData = new ExcludeData();
                    synchronizer.ExcludeData = excludeData;
                    synchronizer.PreSync();
                }
                else
                {
                    //Do nothing if -1
                }
            }
            else
            {
                string rightsString = nsync.Properties.Resources.accessRightsInsufficient;
                if (!leftDirectoryAccessible)
                    rightsString += "\n" + ShortenPath(actualLeftPath, 50);
                if (!rightDirectoryAccessible)
                    rightsString += "\n" + ShortenPath(actualRightPath, 50);
                helper.Show(rightsString, HELPER_WINDOW_HIGH_PRIORITY, HelperWindow.windowStartPosition.windowTop);
                LabelProgress.Content = MESSAGE_ERROR_DETECTED;
                LabelProgress.Visibility = Visibility.Visible;
            }
        }
        /// <summary>
        /// This method is called when when synchronization is completed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void backgroundWorkerForSync_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            EnableInterface(true);

            // This condition eliminates the scenario when e.Result throws an exception when the background
            // worker encounters an error or gets cancelled
            if (e.Cancelled || e.Error != null)
            {
                // This condition is met when the background worker is cancelled
                if (e.Cancelled)
                {
                    helper.Show(nsync.Properties.Resources.syncTerminated, HELPER_WINDOW_HIGH_PRIORITY, HelperWindow.windowStartPosition.windowTop);
                    LabelProgress.Content = MESSAGE_SYNC_TERMINATED;
                    ButtonSync.Visibility = Visibility.Visible;
                    ButtonPreview.Visibility = Visibility.Visible;
                }
                // This condition is met when the background worker encounters an error
                else if (e.Error != null)
                {
                    helper.Show(nsync.Properties.Resources.defaultErrorMessage, HELPER_WINDOW_HIGH_PRIORITY, HelperWindow.windowStartPosition.windowTop);
                    LabelProgress.Content = MESSAGE_ERROR_DETECTED;
                    ButtonSync.Visibility = Visibility.Hidden;
                    ButtonPreview.Visibility = Visibility.Hidden;
                }
                LabelProgressPercent.Visibility = Visibility.Hidden;
                ImageTeam14Over.OpacityMask = blankOpacityMask;

                IsFolderExist();
                return;
            }
            else
            {
                if (!(bool)e.Result)
                {
                    debugLogger.LogMessage(actualLeftPath, actualRightPath, "backgroundWorkerForSync_RunWorkerCompleted()", nsync.Properties.Resources.insufficientDiskSpace);
                    helper.Show(nsync.Properties.Resources.insufficientDiskSpace, HELPER_WINDOW_HIGH_PRIORITY, HelperWindow.windowStartPosition.windowTop);
                    LabelProgress.Content = MESSAGE_ERROR_DETECTED;
                    LabelProgressPercent.Visibility = Visibility.Hidden;
                    ImageTeam14Over.OpacityMask = blankOpacityMask;
                    ButtonSync.Visibility = Visibility.Hidden;
                    ButtonPreview.Visibility = Visibility.Hidden;
                    IsFolderExist();
                    return;
                }

                if (synchronizer.IsFoldersSync())
                {
                    ImageTeam14Over.OpacityMask = blankOpacityMask;

                    // Update MRU
                    SaveFolderPaths();
                    ReloadFolderPaths();

                    LabelProgress.Content = MESSAGE_SYNC_COMPLETED;
                    LabelProgressPercent.Content = "100 %";
                    helper.Show(nsync.Properties.Resources.synchronizedFolders, HELPER_WINDOW_HIGH_PRIORITY, HelperWindow.windowStartPosition.windowTop);
                    debugLogger.LogMessage(actualLeftPath, actualRightPath, "backgroundWorkerForSync_RunWorkerCompleted()", nsync.Properties.Resources.synchronizedFolders);
                    return;
                }

                previewSync = new Preview();
                previewSync.LeftPath = actualLeftPath;
                previewSync.RightPath = actualRightPath;
                // If filters were activated, let summary sync know as well.
                int excludeWindowStatus = settingsManager.GetExcludeWindowStatus();
                if (excludeWindowStatus == 1)
                {
                    previewSync.ExcludeData = excludeData;
                }
                else if (excludeWindowStatus == 0)
                {
                    excludeData = new ExcludeData();
                    previewSync.ExcludeData = excludeData;
                }

                debugLogger.LogMessage(actualLeftPath, actualRightPath, "backgroundWorkerForSync_RunWorkerCompleted()", "Sync done!");

                previewSync.backgroundWorkerForSummary.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorkerForSummary_RunWorkerCompleted);
                previewSync.SummarySync();
            }
        }
        /// <summary>
        /// This method is called when exclude window closes
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void excludeWindow_Closing(object sender, CancelEventArgs e)
        {
            if (excludeWindow.Cancel == true)
            {
                mainWindow.Opacity = 1;
                EnableInterface(true);
                excludeData = new ExcludeData();
                excludeData.ExcludeFileNameList = excludeWindow.GetFileNameList();
                excludeData.ExcludeFileTypeList = excludeWindow.GetFileTypeList();
                excludeData.ExcludeFolderList = excludeWindow.GetFolderList();
            }
            else
            {
                // Make necessary changes to UI to prepare for sync
                mainWindow.Opacity = 1;
                LeftListBox.Visibility = Visibility.Hidden;
                RightListBox.Visibility = Visibility.Hidden;
                LabelProgress.Visibility = Visibility.Visible;
                LabelProgress.Content = MESSAGE_PREPARING_FOLDERS;
                EnableInterface(false);

                // Feed the actualleftpath and actualrightpath into SyncEngine again
                // Safety precaution
                synchronizer.LeftPath = actualLeftPath;
                synchronizer.RightPath = actualRightPath;

                // Do PreSync Calculations: count how many changes need to be done
                // If not enough disk space, return
                // If enough, continue to start the real sync
                excludeData = new ExcludeData();
                excludeData.ExcludeFileNameList = excludeWindow.GetFileNameList();
                excludeData.ExcludeFileTypeList = excludeWindow.GetFileTypeList();
                excludeData.ExcludeFolderList = excludeWindow.GetFolderList();
                synchronizer.ExcludeData = excludeData;
                synchronizer.PreSync();
            }

            debugLogger.LogMessage(actualLeftPath, actualRightPath, "excludeWindow_Closing()", "User closes exclude window");
        }
        /// <summary>
        /// Saves the current folder paths into settings.xml
        /// </summary>
        /// <param name="leftPath">This parameter will be saved into settings.xml</param>
        /// <param name="rightPath">This parameter will be saved into settings.xml</param>
        public void SaveFolderPaths(string leftPath, string rightPath)
        {
            if (!IsFoldersLocked()) // checks if folders that nsync use are locked.
            {
                string[] tempStorage = new string[10];

                int counter = 0;

                for (int i = 0; i < 10; i++)
                {
                    tempStorage[i] = NULL_STRING;
                }

                IsSettingsFileExists();

                XmlDocument doc = new XmlDocument();
                XmlNode mruNode = SelectNode(doc, PATH_MRU);

                XmlNode filterNode;
                XmlNode excludeFileTypeNode;
                XmlNode excludeFileNameNode;
                XmlNode excludeFolderNode;
                XmlNode newSizeNode;
                ExcludeData[] tempExcludeData = new ExcludeData[5];
                int[] fileTypeListSize = new int[5];
                int[] fileNameListSize = new int[5];
                int[] folderListSize = new int[5];

                for (int i = 0; i < NUMBER_OF_MOST_RECENT; i++)
                    tempExcludeData[i] = new ExcludeData();

                // Backup of all settings.xml saved MRU
                #region Backup settings.xml MRU
                for (int i = 1; i <= NUMBER_OF_MOST_RECENT; i++)
                {
                    // Backup stored left and right paths from xml file
                    tempStorage[counter++] = mruNode["left" + i.ToString()].InnerText;
                    tempStorage[counter++] = mruNode["right" + i.ToString()].InnerText;

                    filterNode = mruNode.SelectSingleNode("filter" + i.ToString());

                    // Backup Exclude File Type from settings.xml file
                    excludeFileTypeNode = filterNode.SelectSingleNode("excludeFileTypes");

                    fileTypeListSize[i - 1] = int.Parse(excludeFileTypeNode["size"].InnerText);
                    if (fileTypeListSize[i - 1] != 0)
                    {
                        for (int j = 0; j < fileTypeListSize[i - 1]; j++)
                        {
                            tempExcludeData[i - 1].AddExcludeFileType(excludeFileTypeNode["fileType" + j.ToString()].InnerText);
                        }
                    }

                    // Backup Exclude File Name from settings.xml file
                    excludeFileNameNode = filterNode.SelectSingleNode("excludeFileNames");

                    fileNameListSize[i - 1] = int.Parse(excludeFileNameNode["size"].InnerText);
                    if (fileNameListSize[i - 1] != 0)
                    {
                        for (int j = 0; j < fileNameListSize[i - 1]; j++)
                        {
                            tempExcludeData[i - 1].AddExcludeFileName(excludeFileNameNode["fileName" + j.ToString()].InnerText);
                        }
                    }

                    // Backup Exclude Folder from settings.xml file
                    excludeFolderNode = filterNode.SelectSingleNode("excludeFolders");

                    folderListSize[i - 1] = int.Parse(excludeFolderNode["size"].InnerText);
                    if (folderListSize[i - 1] != 0)
                    {
                        for (int j = 0; j < folderListSize[i - 1]; j++)
                        {
                            tempExcludeData[i - 1].AddExcludeFolder(excludeFolderNode["folder" + j.ToString()].InnerText);
                        }
                    }
                }
                #endregion

                // Storing of current settings
                #region Store current settings
                // Store left and right path
                mruNode["left1"].InnerText = leftPath;
                mruNode["right1"].InnerText = rightPath;

                // Change the nodes to the first filter
                filterNode = mruNode.SelectSingleNode("filter1");

                excludeFileTypeNode = filterNode.SelectSingleNode("excludeFileTypes");
                excludeFileNameNode = filterNode.SelectSingleNode("excludeFileNames");
                excludeFolderNode = filterNode.SelectSingleNode("excludeFolders");

                // storing exclude for File Types
                // Clearing the first node for space of new node
                excludeFileTypeNode.RemoveAll();
                newSizeNode = doc.CreateElement("size");
                newSizeNode.InnerText = excludeData.ExcludeFileTypeList.Count.ToString();
                excludeFileTypeNode.AppendChild(newSizeNode);

                if (excludeData.ExcludeFileTypeList.Count != 0)
                {
                    for (int i = 0; i < excludeData.ExcludeFileTypeList.Count; i++)
                    {
                        XmlNode newFileTypeNode = doc.CreateElement("fileType" + i.ToString());
                        newFileTypeNode.InnerText = excludeData.ExcludeFileTypeList[i];
                        excludeFileTypeNode.AppendChild(newFileTypeNode);
                    }
                }

                // storing exclude for File Names
                // Clearing the first node for space of new node
                excludeFileNameNode.RemoveAll();
                newSizeNode = doc.CreateElement("size");
                newSizeNode.InnerText = excludeData.ExcludeFileNameList.Count.ToString();
                excludeFileNameNode.AppendChild(newSizeNode);

                if (excludeData.ExcludeFileNameList.Count != 0)
                {
                    for (int i = 0; i < excludeData.ExcludeFileNameList.Count; i++)
                    {
                        XmlNode newFileNameNode = doc.CreateElement("fileName" + i.ToString());
                        newFileNameNode.InnerText = excludeData.ExcludeFileNameList[i];
                        excludeFileNameNode.AppendChild(newFileNameNode);
                    }
                }

                // storing exclude for Folder
                // Clearing the first node for space of new node
                excludeFolderNode.RemoveAll();
                newSizeNode = doc.CreateElement("size");
                newSizeNode.InnerText = excludeData.ExcludeFolderList.Count.ToString();
                excludeFolderNode.AppendChild(newSizeNode);

                if (excludeData.ExcludeFolderList.Count != 0)
                {
                    for (int i = 0; i < excludeData.ExcludeFolderList.Count; i++)
                    {
                        XmlNode newFolderNode = doc.CreateElement("folder" + i.ToString());
                        newFolderNode.InnerText = excludeData.ExcludeFolderList[i];
                        excludeFolderNode.AppendChild(newFolderNode);
                    }
                }
                #endregion

                // Check if current settings exist in stored settings
                // Replaces the old stored, since the new settings is in the first node
                for (int i = 0; i < 10; i += 2)
                {
                    if (tempStorage[i] == leftPath && tempStorage[i + 1] == rightPath)
                    {
                        tempStorage[i] = tempStorage[i + 1] = "REPLACED";
                        break;
                    }
                }

                // Stores the backup to the right nodes in settings.xml
                #region Store backup to settings.xml
                counter = 0;
                for (int i = 2; i <= NUMBER_OF_MOST_RECENT; i++)
                {
                    while (tempStorage[counter] == "REPLACED" && tempStorage[counter + 1] == "REPLACED")
                        counter += 2;

                    // Stores the temp File Type into 2, 3, 4, 5th filters
                    if (fileTypeListSize[counter / 2] != 0)
                    {
                        filterNode = mruNode.SelectSingleNode("filter" + i.ToString());
                        excludeFileTypeNode = filterNode.SelectSingleNode("excludeFileTypes");

                        excludeFileTypeNode.RemoveAll();
                        newSizeNode = doc.CreateElement("size");
                        newSizeNode.InnerText = fileTypeListSize[counter / 2].ToString();
                        excludeFileTypeNode.AppendChild(newSizeNode);

                        for (int j = 0; j < fileTypeListSize[counter / 2]; j++)
                        {
                            XmlNode newFileTypeNode = doc.CreateElement("fileType" + j.ToString());
                            newFileTypeNode.InnerText = tempExcludeData[counter / 2].ExcludeFileTypeList[j];
                            excludeFileTypeNode.AppendChild(newFileTypeNode);
                        }
                    }

                    // Stores the temp File Name into 2, 3, 4, 5th filters
                    if (fileNameListSize[counter / 2] != 0)
                    {
                        filterNode = mruNode.SelectSingleNode("filter" + i.ToString());
                        excludeFileNameNode = filterNode.SelectSingleNode("excludeFileNames");

                        excludeFileNameNode.RemoveAll();
                        newSizeNode = doc.CreateElement("size");
                        newSizeNode.InnerText = fileNameListSize[counter / 2].ToString();
                        excludeFileNameNode.AppendChild(newSizeNode);

                        for (int j = 0; j < fileNameListSize[counter / 2]; j++)
                        {
                            XmlNode newFileNameNode = doc.CreateElement("fileName" + j.ToString());
                            newFileNameNode.InnerText = tempExcludeData[counter / 2].ExcludeFileNameList[j];
                            excludeFileNameNode.AppendChild(newFileNameNode);
                        }
                    }

                    // Stores the temp Folder into 2, 3, 4, 5th filters
                    if (folderListSize[counter / 2] != 0)
                    {
                        filterNode = mruNode.SelectSingleNode("filter" + i.ToString());
                        excludeFolderNode = filterNode.SelectSingleNode("excludeFolders");

                        excludeFolderNode.RemoveAll();
                        newSizeNode = doc.CreateElement("size");
                        newSizeNode.InnerText = folderListSize[counter / 2].ToString();
                        excludeFolderNode.AppendChild(newSizeNode);

                        for (int j = 0; j < folderListSize[counter / 2]; j++)
                        {
                            XmlNode newFolderNode = doc.CreateElement("folder" + j.ToString());
                            newFolderNode.InnerText = tempExcludeData[counter / 2].ExcludeFolderList[j];
                            excludeFolderNode.AppendChild(newFolderNode);
                        }
                    }

                    mruNode["left" + i.ToString()].InnerText = tempStorage[counter];
                    mruNode["right" + i.ToString()].InnerText = tempStorage[counter + 1];

                    counter += 2;
                }
                #endregion

                SaveFile(doc, settingsFile);
            }
        }
        /// <summary>
        /// Load ExcludeData from settings.xml
        /// </summary>
        /// <param name="leftPath">Left path from the current sync job</param>
        /// <param name="rightPath">Right path from the current sync job</param>
        /// <returns>Saved exclude data</returns>
        public ExcludeData LoadExcludeData(string leftPath, string rightPath)
        {
            ExcludeData loadedExcludeData = new ExcludeData();

            if (!IsFoldersLocked()) // checks if folders that nsync use are locked.
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(settingsFile);

                XmlNode mruNode = SelectNode(doc, PATH_MRU);

                // finds the corresponding paths to which filters are stored.
                int filterIndex = 0;
                for (int i = 1; i <= NUMBER_OF_MOST_RECENT; i++)
                {
                    if (mruNode["left" + i.ToString()].InnerText.Equals(leftPath))
                        if (mruNode["right" + i.ToString()].InnerText.Equals(rightPath))
                            filterIndex = i;
                }

                if (filterIndex != 0) // checks if there are any filters
                {
                    XmlNode filterNode = mruNode.SelectSingleNode("filter" + filterIndex.ToString());
                    XmlNode excludeFileTypeNode = filterNode.SelectSingleNode("excludeFileTypes");
                    XmlNode excludeFileNameNode = filterNode.SelectSingleNode("excludeFileNames");
                    XmlNode excludeFolderNode = filterNode.SelectSingleNode("excludeFolders");

                    // adds exclude File Types
                    for (int i = 0; i < int.Parse(excludeFileTypeNode["size"].InnerText); i++)
                    {
                        loadedExcludeData.AddExcludeFileType(excludeFileTypeNode["fileType" + i.ToString()].InnerText);
                    }

                    // adds exclude File Names
                    for (int i = 0; i < int.Parse(excludeFileNameNode["size"].InnerText); i++)
                    {
                        loadedExcludeData.AddExcludeFileName(excludeFileNameNode["fileName" + i.ToString()].InnerText);
                    }

                    // adds exclude Folders
                    for (int i = 0; i < int.Parse(excludeFolderNode["size"].InnerText); i++)
                    {
                        loadedExcludeData.AddExcludeFolder(excludeFolderNode["folder" + i.ToString()].InnerText);
                    }
                }

                return loadedExcludeData;
            }

            return loadedExcludeData; // return a default to nsync if it lock. This return is to prevent error.
        }