Esempio n. 1
0
 private void SendMessage(CompareSyncMessageEventArgs args)
 {
     if (SyncActionOccurred != null)
     {
         SyncActionOccurred(this, args);
     }
 }
Esempio n. 2
0
        private void DoWork()
        {
            List <string>               lstLeftPaths, lstRightPaths;
            List <CompareItem>          lstItems = new List <CompareItem>();
            List <ItemEntry>            lstLeftItems, lstRightItems;
            CompareItem                 compItem;
            CompareSyncMessageEventArgs syncMessageArgs;
            bool blnSkipFile;

            lstLeftPaths = Directory.GetFiles(LeftCompareRootPath, "*.*", SearchOption.AllDirectories).ToList();
            if (Directory.Exists(RightCompareRootPath))
            {
                lstRightPaths = Directory.GetFiles(RightCompareRootPath, "*.*", SearchOption.AllDirectories).ToList();
            }
            else
            {
                lstRightPaths = new List <string>();
            }

            lstLeftItems  = lstLeftPaths.ConvertAll <ItemEntry>(delegate(string path) { return(new ItemEntry(LeftCompareRootPath, path)); });
            lstRightItems = lstRightPaths.ConvertAll <ItemEntry>(delegate(string path) { return(new ItemEntry(RightCompareRootPath, path)); });

            // Build initial comparison pair list from left items
            foreach (ItemEntry leftItem in lstLeftItems)
            {
                compItem      = new CompareItem();
                compItem.Left = leftItem;
                // Try to load the matching right item (since this is windows, we can lowerize the paths, case matters not ... for now.)
                compItem.Right = lstRightItems.SingleOrDefault(i => i.RelativePath.ToLower() == leftItem.RelativePath.ToLower());
                if (compItem.Right != null)
                {
                    // Match found, remove from right list.
                    lstRightItems.Remove(compItem.Right);
                }
                lstItems.Add(compItem);
            }

            // Put remaining items from the right list into the comparison pair list (as right-orphans)
            foreach (ItemEntry item in lstRightItems)
            {
                compItem       = new CompareItem();
                compItem.Right = item;
                lstItems.Add(compItem);
            }

            // Set the IsFiltered flag for those items that match any exclusion filter
            lstItems.ForEach(i => i.IsFiltered = Exclusions.MatchesAny(i.RelativePath));

            //if(Files != null && Files.Length > 0)
            //{
            //    lstItems = lstItems.Where(i => Files.Contains(i.RelativePath)).ToList();
            //    //if(!Files.Contains(item.RelativePath))
            //    //{
            //    //    blnSkipFile = true;
            //    //}
            //}

            // For all comparison pairs
            foreach (var item in lstItems)
            {
                blnSkipFile = false;

                if (Files != null && Files.Count() > 0)
                {
                    if (!Files.Contains(item.RelativePath))
                    {
                        blnSkipFile = true;
                    }
                }

                if (!blnSkipFile)
                {
                    // Do comparison
                    if (item.Left == null)
                    {
                        item.CompareState = CompareStateType.RightOrphan;
                        item.Left         = new ItemEntry(LeftCompareRootPath, Path.Combine(LeftCompareRootPath, item.Right.RelativePath));
                    }
                    else if (item.Right == null)
                    {
                        item.CompareState = CompareStateType.LeftOrphan;
                        item.Right        = new ItemEntry(RightCompareRootPath, Path.Combine(RightCompareRootPath, item.Left.RelativePath));
                    }
                    else
                    {
                        // If necessary, this basic comparison logic could be refactored using
                        // a behavior pattern to support different comparison algorithms.
                        item.CompareState = FileComparer.CompareFiles(item.Left.FullPath, item.Right.FullPath);
                    }

                    // Call sync action work method
                    if (Exclusions.MatchesAny(item.RelativePath))
                    {
                        syncMessageArgs = new CompareSyncMessageEventArgs()
                        {
                            SyncAction     = SyncActionType.Ignored,
                            SyncActionText = "Item skipped due to filter."
                        };
                    }
                    else
                    {
                        syncMessageArgs = _syncBehavior.SyncItem(item);
                    }
                    syncMessageArgs.Item = item;
                    SendMessage(syncMessageArgs);
                }
            }
        }