crsyncfiles /CRSyncFiles/CrSyncFilesUtils/CrSyncFilesUtility.cs

Language C# Lines 905
MD5 Hash 29c61ef6bc66d5febe84bd3d4d9402fd Estimated Cost $16,217 (why?)
Repository https://hg.codeplex.com/crsyncfiles View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
/*
* Copyright (c) 2011 Connection Road.  All rights reserved.
* This program and the accompanying material are made available
* under the terms of the Eclipse Public Licnese v1.0 which is
* available at http://www.eclipse.org/legal/epl-v10.html
*/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using CrSyncFilesUtils.Enums;
using Microsoft.Win32;
using SyncToy;

namespace CrSyncFilesUtils
{
    /// <summary>
    /// A collection of static methods for CrSyncFiles.
    /// </summary>
    public partial class CrSyncFilesUtility
    {

        [DllImport("user32.dll", CharSet=CharSet.Auto)]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); // Works but hard to debug on different arch

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr BringWindowToTop(IntPtr hwnd); // Does not work

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern void SwitchToThisWindow(IntPtr hwnd, bool altTab); // Too annoying

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern bool SetForegroundWindow(IntPtr hWnd); // Works

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr DestroyWindow(IntPtr hwnd); //Works but applicable only to child window.

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr EndTask(IntPtr hwnd, bool ignore, bool force);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr GetForegroundWindow();

        [DllImport("user32")]
        public static extern IntPtr RemoveMenu(int systemMenu, int itemPosition, int flag); // Removes control box. .Net have already have them.

        public static string SyncToyRegKey = @"Software\Microsoft\Synctoy";
        public static string SyncFramework32RegKey = @"SOFTWARE\Microsoft\Microsoft Sync Framework\v2.1";
        public static string SyncFramework64RegKey = @"SOFTWARE\Wow6432Node\Microsoft\Microsoft Sync Framework\v2.1";
        //public static string SyncFrameworkAssemblyName = "Microsoft.Synchronization, Version=2.1.0.0, Culture=neutral, PublicKeyToken=89845DCD8080CC91";
        //public static string SyncFileProviderAssemblyName = "Microsoft.Synchronization.Files, Version=2.1.0.0, Culture=neutral, PublicKeyToken=89845DCD8080CC91";
        public static string DefaultHost = "www.connectionroad.com";


        /// <summary>
        /// Verifies if the Sync framework is installed
        /// </summary>
        /// <returns></returns>
        public static bool IsSyncFrameworkInstalled()
        {
            try
            {
                RegistryKey registrySyncFramework;

                switch (GetArch())
                {
                    case CrSyncArchs.X86:
                        registrySyncFramework = Registry.LocalMachine.OpenSubKey(SyncFramework32RegKey);
                        break;
                    case CrSyncArchs.X64:
                        registrySyncFramework = Registry.LocalMachine.OpenSubKey(SyncFramework64RegKey);
                        break;
                    default:
                        registrySyncFramework = Registry.LocalMachine.OpenSubKey(SyncFramework32RegKey);
                        break;
                }
                    

                return registrySyncFramework != null;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool IsSyncToyInstalled()
        {
            var synctoyRegKey = Registry.CurrentUser.OpenSubKey(SyncToyRegKey);
            return synctoyRegKey != null;
        }


        /// <summary>
        /// Gets the absolute path of SyncToy's database file.
        /// </summary>
        /// <returns></returns>
        public static string GetSyncToyStorageFilePath()
        {
            return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Microsoft\\SyncToy\\2.0\\SyncToyDirPairs.bin");
        }

        public static string GetSyncToyMetaDataDirectory()
        {
            return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Microsoft\\SyncToy\\2.0");
        }

        /// <summary>
        /// Get a lists of available SyncToy folder pairs.
        /// </summary>
        /// <exception cref="ApplicationException"></exception>
        /// <returns></returns>
        public static List<SyncEngineConfig> GetSyncToyFolderPairs()
        {
            var synctoyLocalAppData = GetSyncToyStorageFilePath();
            var configfiles = new List<SyncEngineConfig>();
            if (File.Exists(synctoyLocalAppData))
            {
                IFormatter binaryFormmater = new BinaryFormatter();
                using (Stream fileStream = new FileStream(synctoyLocalAppData, FileMode.Open))
                {
                    while (fileStream.Position < fileStream.Length)
                    {
                        var currentConfig = (SyncEngineConfig)binaryFormmater.Deserialize(fileStream);
                        configfiles.Add(currentConfig);
                    }
                }

            }

            return configfiles;
        }

        /// <summary>
        /// Generally saves ( actually overwrites SyncToy's store file ) the given lists of SynToy folder pairs.
        /// </summary>
        /// <exception cref="ApplicationException"></exception>
        /// <param name="synctoyPairs"></param>
        public static void SetSyncToyFolderPairs(List<SyncEngineConfig> synctoyPairs)
        {
            var synctoyLocalAppData = GetSyncToyStorageFilePath();
            IFormatter formatter = new BinaryFormatter();
            using (Stream fileStream = new FileStream(synctoyLocalAppData, FileMode.Create))
            {
                foreach (var pair in synctoyPairs)
                {
                    formatter.Serialize(fileStream, pair);
                }
            }
        }


        /// <summary>
        /// Converts  a SyncToy folder pair to CrSyncFiles compatible folder pair. All meta files import are taken care by this method.
        /// </summary>
        /// <param name="synctoyPair"></param>
        /// <param name="prefix"></param>
        /// <param name="metaDataDirectory"></param>
        /// <returns></returns>
        public static FileSyncPairInfo ConvertToCrSyncFile(SyncEngineConfig synctoyPair, string prefix, string metaDataDirectory)
        {
            
            var newFolderPair = FileSyncPairManager.CreateFolderPair(synctoyPair.Name);
            newFolderPair.ActiveForRunAll = synctoyPair.Active;
            newFolderPair.DateTimeLastSync = synctoyPair.DateTimeLastSync;
            newFolderPair.LeftFolder = FileSyncPairManager.CreateFileLocation(synctoyPair.LeftDriveType,synctoyPair.LeftDir,
                                                                              prefix,
                                                                              synctoyPair.LeftGuid.ToString(),
                                                                              metaDataDirectory);

            newFolderPair.RightFolder = FileSyncPairManager.CreateFileLocation(synctoyPair.RightDriveType,synctoyPair.RightDir,
                                                                               prefix,
                                                                               synctoyPair.RightGuid.ToString(),
                                                                               metaDataDirectory);

            newFolderPair.OptionsCompareFileStreams = synctoyPair.ComputeHash;

            newFolderPair.OptionsBackupOlderFiles = synctoyPair.BackupOlderFile;
            


            // Get excluded subfodlers)
            if (synctoyPair.ExceptionalDirs != null)
            {
                var synctoySubfolders = synctoyPair.ExceptionalDirs.LeftDirectories;
                synctoySubfolders.AddRange(synctoyPair.ExceptionalDirs.RightDirectories);
                foreach (var relativeSubFolderPath in from string synctoySubfolder in synctoySubfolders
                                                      select new FileNode(synctoySubfolder)
                                                      into fileNode select fileNode.GetRelativePathFromFolderPair(newFolderPair)
                                                      into relativeSubFolderPath where relativeSubFolderPath != null where !newFolderPair.SubDirectoryExclusionList.Contains(relativeSubFolderPath) select relativeSubFolderPath)
                {
                    newFolderPair.SubDirectoryExclusionList.Add(relativeSubFolderPath);
                }
            }

            // Excluded files
            newFolderPair.SetSearchPattern(synctoyPair.SearchPattern);

            // Included files
            newFolderPair.SetExcludePattern(synctoyPair.ExcludedPattern);

            


            switch (synctoyPair.SyncMode)
            {
                case SyncMode.Synchronize:
                    newFolderPair.FolderPairActionType = FolderPairActionTypeEnum.Synchronize;
                    break;
                case SyncMode.Echo:
                    newFolderPair.FolderPairActionType = FolderPairActionTypeEnum.Echo;
                    break;
                case SyncMode.Contribute:
                    newFolderPair.FolderPairActionType = FolderPairActionTypeEnum.Contribute;
                    break;
                default:
                    newFolderPair.FolderPairActionType = FolderPairActionTypeEnum.Synchronize;
                    break;
            }


            // Import synctoy meta data
            var syncToyLeftMetaData = GetSyncToyMetaFilePath(synctoyPair.LeftGuid);
            var syncToyRightMetaData = GetSyncToyMetaFilePath(synctoyPair.RightGuid);

            // Generate the absolute path for our metadata
            var crSyncLeftMetaFile = newFolderPair.LeftFolder.MetaFilePath;
            var crSyncRightMetaFile = newFolderPair.RightFolder.MetaFilePath;

            // Create markers when necessary
            newFolderPair.LeftFolder.CreateMarker();
            newFolderPair.RightFolder.CreateMarker();


            if (syncToyLeftMetaData != crSyncLeftMetaFile)
            {
                if (File.Exists(syncToyLeftMetaData) && (!File.Exists(crSyncLeftMetaFile)))
                {
                    File.Copy(syncToyLeftMetaData, crSyncLeftMetaFile,true);
                }
            }

            if (syncToyRightMetaData != crSyncRightMetaFile)
            {
                if (File.Exists(syncToyRightMetaData) && (!File.Exists(crSyncRightMetaFile)))
                {
                    File.Copy(syncToyRightMetaData, crSyncRightMetaFile,true);
                }
            }

            if ((synctoyPair.ExcludedFileAttributes & FileAttributes.ReadOnly) != 0)
            {
                newFolderPair.FileExcludeAttrReadOnly = true;
            }

            if ((synctoyPair.ExcludedFileAttributes & FileAttributes.Hidden) != 0)
            {
                newFolderPair.FileExcludeAttrHidden = true;
            }

            if ((synctoyPair.ExcludedFileAttributes & FileAttributes.System) != 0)
            {
                newFolderPair.FileExcludeAttrSystem = true;
            }

            newFolderPair.ImportedFromSyncToy = true;

            return newFolderPair;
        }

        public static SyncEngineConfig ConvertToSyncToy(FileSyncPairInfo crsyncPair)
        {
            var synctoyPair = new SyncEngineConfig
                                  {
                                      Name = crsyncPair.FolderPairName,
                                      Active = crsyncPair.ActiveForRunAll,
                                      DateTimeLastSync = crsyncPair.DateTimeLastSync
                                  };

            // Synchronize some options
            crsyncPair.RemoveMarkerFiles();
            crsyncPair.RemoveMetaData();
            crsyncPair.LeftFolder.FileName = string.Format("SyncToy_{0}.dat",synctoyPair.LeftGuid);
            crsyncPair.RightFolder.FileName = string.Format("SyncToy_{0}.dat", synctoyPair.RightGuid);
            crsyncPair.LeftFolder.UniqueIdentifier = string.Format("{0}", synctoyPair.LeftGuid);
            crsyncPair.RightFolder.UniqueIdentifier = string.Format("{0}", synctoyPair.RightGuid);
            synctoyPair.LeftDir = crsyncPair.LeftFolder.Path;
            synctoyPair.RightDir = crsyncPair.RightFolder.Path;
            synctoyPair.ComputeHash = crsyncPair.OptionsCompareFileStreams;
            synctoyPair.BackupOlderFile = crsyncPair.OptionsBackupOlderFiles;

            // File filters
            synctoyPair.SearchPattern = crsyncPair.GetSearchPattern();
            synctoyPair.ExcludedPattern = crsyncPair.GetExcludePattern();


            crsyncPair.ExtendedInfo.AddObjectForKey(true,"HAS_MIRROR");
            
            switch (crsyncPair.FolderPairActionType)
            {
                case FolderPairActionTypeEnum.Synchronize:
                    synctoyPair.SyncMode = SyncMode.Synchronize;
                    break;
                case FolderPairActionTypeEnum.Echo:
                    synctoyPair.SyncMode = SyncMode.Echo;
                    break;
                case FolderPairActionTypeEnum.Contribute:
                    synctoyPair.SyncMode = SyncMode.Contribute;
                    break;
                default:
                    synctoyPair.SyncMode = SyncMode.Synchronize;
                    break;
            }
            
            return synctoyPair;

        }


        /// <summary>
        /// Ensures that the destination object pair contains the most recent data from the target object pair.
        /// </summary>
        /// <param name="crsyncPair">Destination</param>
        /// <param name="synctoyPair">Target</param>
        public static void SynchronizePairsFrom(FileSyncPairInfo crsyncPair, SyncEngineConfig synctoyPair)
        {
            crsyncPair.FolderPairName = synctoyPair.Name;
            crsyncPair.ActiveForRunAll = synctoyPair.Active;
            crsyncPair.DateTimeLastSync = synctoyPair.DateTimeLastSync;
            crsyncPair.OptionsCompareFileStreams = synctoyPair.ComputeHash;

            crsyncPair.OptionsBackupOlderFiles = synctoyPair.BackupOlderFile;

            // Get excluded subfolders
            crsyncPair.SubDirectoryExclusionList.Clear();
            
            if(synctoyPair.ExceptionalDirs == null)
            {
                crsyncPair.SubDirectoryExclusionList = new List<string>();
            }
            else
            {
                var excludedSynctoyFolders = new ArrayList();
                if ((synctoyPair.ExceptionalDirs.LeftDirectories != null) && (synctoyPair.ExceptionalDirs.LeftDirectories.Count >= 1))
                {
                    excludedSynctoyFolders.AddRange(synctoyPair.ExceptionalDirs.LeftDirectories);
                }
                if ((synctoyPair.ExceptionalDirs.RightDirectories != null) && (synctoyPair.ExceptionalDirs.RightDirectories.Count >= 1))
                {
                    excludedSynctoyFolders.AddRange(synctoyPair.ExceptionalDirs.RightDirectories);
                }

                if(excludedSynctoyFolders.Count >= 1)
                {
                    foreach (var relativePath in excludedSynctoyFolders.Cast<string>().Select(subfolder => new FileNode(subfolder)).Select(fileNode => fileNode.GetRelativePathFromFolderPair(crsyncPair)).Where(relativePath => !crsyncPair.SubDirectoryExclusionList.Contains(relativePath)))
                    {
                        crsyncPair.SubDirectoryExclusionList.Add(relativePath);
                    }
                }
                else
                {
                    crsyncPair.SubDirectoryExclusionList = new List<string>();
                }

            }


            crsyncPair.SetSearchPattern(synctoyPair.SearchPattern);
            crsyncPair.SetExcludePattern(synctoyPair.ExcludedPattern);

            // Sync action);
            switch (synctoyPair.SyncMode)
            {
                case SyncMode.Synchronize:
                    crsyncPair.FolderPairActionType = FolderPairActionTypeEnum.Synchronize;
                    break;
                case SyncMode.Echo:
                    crsyncPair.FolderPairActionType = FolderPairActionTypeEnum.Echo;
                    break;
                case SyncMode.Contribute:
                    crsyncPair.FolderPairActionType = FolderPairActionTypeEnum.Contribute;
                    break;
                default:
                    crsyncPair.FolderPairActionType = FolderPairActionTypeEnum.Synchronize;
                    break;
            }

            // Attribute files

            crsyncPair.FileExcludeAttrReadOnly = (synctoyPair.ExcludedFileAttributes & FileAttributes.ReadOnly) != 0;

            crsyncPair.FileExcludeAttrHidden = (synctoyPair.ExcludedFileAttributes & FileAttributes.Hidden) != 0;

            crsyncPair.FileExcludeAttrSystem = (synctoyPair.ExcludedFileAttributes & FileAttributes.System) != 0;
            
        }

        /// <summary>
        /// Ensures that the destination object pair contains the most recent data from the target object pair.
        /// </summary>
        /// <param name="synctoyPair">Destination</param>
        /// <param name="crsyncPair">Target</param>
        public static void SynchronizePairsFrom(SyncEngineConfig synctoyPair, FileSyncPairInfo crsyncPair)
        {
            synctoyPair.Name = crsyncPair.FolderPairName;
            synctoyPair.Active = crsyncPair.ActiveForRunAll;
            synctoyPair.DateTimeLastSync = crsyncPair.DateTimeLastSync;
            synctoyPair.ComputeHash = crsyncPair.OptionsCompareFileStreams;
            synctoyPair.BackupOlderFile = crsyncPair.OptionsBackupOlderFiles;
            
            // Get excluded subfodlers
            if(crsyncPair.SubDirectoryExclusionList.Count >= 1)
            {
                synctoyPair.ExceptionalDirs = new ExceptionalDirList();
                var excludedSubfolders = crsyncPair.SubDirectoryExclusionList;
                foreach (var excludedSubfolder in excludedSubfolders)
                {
                    var leftSubfolder = new FileNode(crsyncPair.LeftFolder.Path).Join(excludedSubfolder).Path;
                    var rightSubfolder = new FileNode(crsyncPair.RightFolder.Path).Join(excludedSubfolder).Path;
                    synctoyPair.ExceptionalDirs.LeftDirectories.Add(leftSubfolder);
                    synctoyPair.ExceptionalDirs.RightDirectories.Add(rightSubfolder);
                }
            }
            else
            {
                synctoyPair.ExceptionalDirs = new ExceptionalDirList();
            }


            synctoyPair.SearchPattern = crsyncPair.GetSearchPattern();
            synctoyPair.ExcludedPattern = crsyncPair.GetExcludePattern();
            

            switch (crsyncPair.FolderPairActionType)
            {
                case FolderPairActionTypeEnum.Synchronize:
                    synctoyPair.SyncMode = SyncMode.Synchronize;
                    break;
                case FolderPairActionTypeEnum.Echo:
                    synctoyPair.SyncMode = SyncMode.Echo;
                    break;
                case FolderPairActionTypeEnum.Contribute:
                    synctoyPair.SyncMode = SyncMode.Contribute;
                    break;
                default:
                    synctoyPair.SyncMode = SyncMode.Synchronize;
                    break;
            }

            var excludeFileAttr = (FileAttributes)0;
            if (crsyncPair.FileExcludeAttrReadOnly)
            {
                excludeFileAttr = excludeFileAttr | FileAttributes.ReadOnly;
            }

            if (crsyncPair.FileExcludeAttrSystem)
            {
                excludeFileAttr = excludeFileAttr | FileAttributes.System;
            }

            if (crsyncPair.FileExcludeAttrHidden)
            {
                excludeFileAttr = excludeFileAttr | FileAttributes.Hidden;
            }

            synctoyPair.ExcludedFileAttributes = excludeFileAttr;
        }


        /// <summary>
        /// Ensures that CRSyncFiles has the most recent copy of SyncToy's folder pair.
        /// </summary>
        /// <param name="prefix"></param>
        /// <param name="metaDataDirectory"></param>
        /// <param name="crsyncPairManager"></param>
        /// <param name="synctoyPairManager"></param>
        public static void ImportAvailableSyncToyFolderPairs(string prefix,string metaDataDirectory, FileSyncPairManager crsyncPairManager, CrSyncToyManager synctoyPairManager)
        {
            if (IsSyncToyInstalled())
            {

                var synctoyFolderPairs = GetSyncToyFolderPairs();
                foreach (var synctoyFolderPair in synctoyFolderPairs)
                {

                    
                    if (crsyncPairManager.HasFolderPair(synctoyFolderPair))
                    {
                        // Update CRSyncFiles folder pair
                        var crsyncPair = crsyncPairManager.CurrentFolderPair;
                        
                        SynchronizePairsFrom(crsyncPair, synctoyFolderPair);
                        
                        crsyncPairManager.UpdateFolderPair(crsyncPair);
                        synctoyPairManager.UpdateFolderPair(synctoyFolderPair);
                        
                    }
                    else
                    {
                        // Create new CRSyncFiles folder pair
                        
                        var newFolderPair = ConvertToCrSyncFile(synctoyFolderPair, prefix,metaDataDirectory);

                        // Add only if folder pair name is different from the one already exsits.
                        if (!crsyncPairManager.HasFolderPairNamed(newFolderPair.FolderPairName))
                        {
                            
                            var availablePairs = crsyncPairManager.AvailableFolderPairs();
                            
                            availablePairs.Add(newFolderPair.UniqueIdentifier, newFolderPair);
                            
                            crsyncPairManager.UpdateFolderPairs(availablePairs);
                            synctoyPairManager.UpdateFolderPair(synctoyFolderPair);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Auto import and synchronizes existing CrSyncFiles' folder pairs to SyncToy's database.
        /// This ensures that both store files contain the most recent data to prevent any metadata corruption.
        /// </summary>
        /// <param name="metaDataDirectory"></param>
        /// <param name="crsyncPairManager"></param>
        /// <param name="synctoyPairManager"></param>
        public static void ImportAvailableCrSyncFiles(string metaDataDirectory, FileSyncPairManager crsyncPairManager, CrSyncToyManager synctoyPairManager)
        {
            if(IsSyncToyInstalled())
            {
                var crsynFolderPairs = crsyncPairManager.AvailableFolderPairs();
                var synctoyFolderPairs = GetSyncToyFolderPairs();
                if (synctoyFolderPairs.Count >= 1)
                {
                    foreach (var fileSyncPairInfo in crsynFolderPairs.Where(fileSyncPairInfo => !synctoyPairManager.HasFolderPair(fileSyncPairInfo.Value)))
                    {
                        // Since SyncToy doesn't have it.. we should probably delete it.
                        crsyncPairManager.RemoveFolderPair(fileSyncPairInfo.Value);
                    }
                }
                else
                {
                    // Synctoy's database is empty so we should copy everything in CrSyncFiles
                    foreach (var fileSyncPairInfo in crsynFolderPairs)
                    {
                        synctoyPairManager.AddFolderPair(fileSyncPairInfo.Value);
                    }
                }
            }
        }


        /// <summary>
        /// This is a supplementary method after calling various synchronization method for synchronizing both SyncToy and CrSyncFiles folder pairs.
        /// While the other method ensures the both store files must contain what others doesn't have, this ensures no orphaned pairs are left in both stores.
        /// </summary>
        /// <param name="metaDataDirectory"></param>
        /// <param name="crsyncPairManager"></param>
        /// <param name="synctoyManager"></param>
        public static void CheckForUnavailableSyncToyFolderPairs(string metaDataDirectory, FileSyncPairManager crsyncPairManager, CrSyncToyManager synctoyManager)
        {
            if(IsSyncToyInstalled())
            {
                var synctoyFolderPairs = GetSyncToyFolderPairs();
                if(synctoyFolderPairs.Count >= 1)
                {
                    var crsyncFolderPairs = crsyncPairManager.AvailableFolderPairs();
                    foreach(var pairInfo in crsyncFolderPairs)
                    {
                        var hasFolderPair = synctoyManager.HasFolderPair(pairInfo.Value);
                        if(!hasFolderPair)
                        {
                            crsyncPairManager.RemoveFolderPair(pairInfo.Value);
                        }
                    }
                }
                else
                {
                    // SyncToy's store file is empty
                    var allCrSyncFolderPairs = new List<FileSyncPairInfo>();
                    crsyncPairManager.UpdateFolderPairs(allCrSyncFolderPairs);
                }
            }
        }

        /// <summary>
        /// Just a helper method to delete marker files and meta data generated by SyncToy.
        /// </summary>
        /// <param name="synctoyPair"></param>
        public static void RemoveSupplementaryFiles(SyncEngineConfig synctoyPair)
        {
            // Remove marker files
            var leftMarker = GetSyncToyMarkerFilePath(synctoyPair.LeftDir, synctoyPair.LeftGuid);
            var rigthMarker = GetSyncToyMarkerFilePath(synctoyPair.RightDir, synctoyPair.RightGuid);

            var leftMetaFile = new FileNode(GetSyncToyMetaFilePath(synctoyPair.LeftGuid));
            var rightMetaFile = new FileNode(GetSyncToyMetaFilePath(synctoyPair.RightGuid));

            if (File.Exists(leftMarker))
            {
                File.Delete(leftMarker);
            }

            if (File.Exists(rigthMarker))
            {
                File.Delete(rigthMarker);
            }

            if(File.Exists(leftMetaFile.Path))
            {
                File.Delete(leftMetaFile.Path);
            }

            if(File.Exists(rightMetaFile.Path))
            {
                File.Delete(rightMetaFile.Path);
            }
        }

        public static void WriteLog(string message, string fileName)
        {
            using (var streamWriter = !File.Exists(fileName) ? new StreamWriter(fileName) : File.AppendText(fileName))
            {
                var dateTime = string.Format("{0:MM/dd/yyyy hh:mm:ss tt}", DateTime.Now);
                if(message.Trim().Length >= 1)
                {
                    streamWriter.WriteLine("[" + dateTime + "]  " + message);
                }
                else
                {
                    streamWriter.WriteLine(message);
                }
                streamWriter.Close();    
            }
        }

        public static string GetSyncToyMarkerFilePath(string rootReplica,Guid guid)
        {
           return Path.Combine(rootReplica, string.Format("SyncToy_{0}.dat", guid));
        }

        public static string GetSyncToyMetaFilePath(Guid guid)
        {
            return new FileNode(GetSyncToyMetaDataDirectory()).Join(string.Format("SyncToy_{0}.dat", guid)).Path;
        }

        
        

        
        public static bool IsSyncToyWindowPresent()
        {
            var processes = Process.GetProcessesByName("SyncToy");
            return processes.Length >= 1;
        }


        


        /// <summary>
        /// Retrieves synctoy's last selected/ran folder pair name.
        /// </summary>
        /// <returns></returns>
        public static string GetSyncToyLastFolderPair()
        {
            try
            {
                var syncToyReg = Registry.CurrentUser.OpenSubKey(SyncToyRegKey);
                if (syncToyReg != null)
                {
                    return (string)syncToyReg.GetValue("LastFolderPair");
                }
                return null;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static bool SetSyncToyLastFolderPair(string lastFolderPair)
        {
            try
            {
                var syncToyReg = Registry.CurrentUser.CreateSubKey(SyncToyRegKey);
                if (syncToyReg != null)
                {
                    syncToyReg.SetValue("LastFolderPair", lastFolderPair);
                    return true;
                }
                return false;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// Convenience method for getting the CRSyncFile's store file.
        /// </summary>
        /// <param name="companyName"></param>
        /// <param name="version"></param>
        /// <returns></returns>
        public static string GetStoreFilePath(string companyName, string version)
        {
            return new FileNode(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)).Join(companyName).Join("CrSyncFiles\\" + version).Join("CrSyncFiles.xml").Path;
        }

        public static string FormatBytes(long bytes)
        {
            // 1000 is no longer used by the server.
            const int scale = 1024;

            var orders = new[] { "GB", "MB", "KB", "Bytes" };

            var max = (long)Math.Pow(scale, orders.Length - 1);

            foreach (string order in orders)
            {

                if (bytes > max)
                {

                    var  formatedString =  string.Format("{0:##.##} {1}", decimal.Divide(bytes, max), order);
                    return formatedString;
                }

                max /= scale;

            }

            return "0 Bytes";

        }

        /// <summary>
        ///  This Function is specifically handling the Product Offerings of 50MB, 2GB, 5GB and 30GB.
        ///  This Function will be cleaned up later, when rounding rules are determined. (Miles)
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string FormatBytesProducts(long bytes)
        {
            // 1000 is no longer used by the server
            const int scale = 1024;

            var orders = new[] { "GB", "MB", "KB", "Bytes" };

            var max = (long)Math.Pow(scale, orders.Length - 1);

            foreach (string order in orders)
            {

                if (bytes > max)
                {
                    var totalBytes = decimal.Divide(bytes, max);

                    // 2GB Plan
                    if (totalBytes >= 1 && totalBytes <= 3)
                    {
                        totalBytes = 2;
                    }

                    // 5GB Plan
                    if (totalBytes >= 4 && totalBytes <= 6)
                    {
                        totalBytes = 5;
                    }

                    // 30GB Plan
                    if (totalBytes >= 29 && totalBytes <= 32)
                    {
                        totalBytes = 30;
                    }

                    // Free 50MB Plan
                    if (totalBytes >= 49 && totalBytes <= 51)
                    {
                        totalBytes = 50;
                    }

                    var formatedString = string.Format("{0:##} {1}", totalBytes , order);
                    return formatedString;
                }

                max /= scale;

            }

            return "0 Bytes";

        }


        /// <summary>
        /// Converts a string to hexadecimal values in form of string separated by a dot "."
        /// </summary>
        /// <param name="stringToConvert"></param>
        /// <param name="separator"></param>
        /// <returns></returns>
        public static string ConvertToHex(string stringToConvert, string separator = ".")
        {
            try
            {
                var hextString = "";
                var charArray = stringToConvert.ToCharArray();
                
                foreach(var c in charArray)
                {
                    var num = Convert.ToInt32(c);
                    hextString += string.Format("{0:X}{1}", num + 1, separator);
                }

                if (hextString.StartsWith(separator))
                {
                    hextString = hextString.Substring(0);
                }

                if (hextString.EndsWith(separator))
                {
                    hextString = hextString.Substring(0, hextString.Length - 1);
                }

                return hextString;
            }
            catch (Exception)
            {
                return "";
            }
        }


        /// <summary>
        /// Converts a series of hexadecimal separated by "." back to the actual string value.
        /// </summary>
        /// <param name="stringOfHex"></param>
        /// <param name="separator"></param>
        /// <returns></returns>
        public static string ConvertHexToString(string stringOfHex, char separator = '.')
        {
            try
            {
                if (!string.IsNullOrEmpty(stringOfHex))
                {
                    var splitHex = stringOfHex.Split(separator);
                    //var stringValue = splitHex.Select(s => (char)int.Parse(s, NumberStyles.HexNumber)).Aggregate("", (current, c) => current + string.Format("{0}", c));

                    var stringValue = "";
                    foreach (var s in splitHex)
                    {
                        var num = int.Parse(s, NumberStyles.HexNumber);
                        var c = (char)(num - 1);
                        stringValue += string.Format("{0}", c);
                    }

                    return stringValue;
                }
                return "";
            }
            catch (Exception)
            {
                return "";
            }
        }

        public static CrSyncArchs GetArch()
        {
            return IntPtr.Size >= 8 ? CrSyncArchs.X64 : CrSyncArchs.X86;
        }



        public static void WriteEventLog(string message, EventLogEntryType entryType)
        {
            // Check if we have registered an event log name and source
            var regKey = Registry.LocalMachine.OpenSubKey(@"\SYSTEM\CurrentControlSet\services\eventlog\ConnectionRoad");
            if(regKey == null)
            {
                Registry.LocalMachine.CreateSubKey(@"\SYSTEM\CurrentControlSet\services\eventlog\ConnectionRoad");
            }

            const string eventSource = "CRSyncFiles";
            const string eventLogName = "ConnectionRoad";
            if (!EventLog.SourceExists(eventSource))
            {
                EventLog.CreateEventSource(eventSource, eventLogName);
            }

            //Logs
            EventLog.WriteEntry(eventSource, message, entryType);
        }
    }
    
}
Back to Top