Exemple #1
0
        public CustomApplicationContext()
        {
            try
            {
                byte[]        jsonConfigurationUtf8Bytes = File.ReadAllBytes("configuration\\configuration.json");
                Configuration configuration = JsonSerializer.Deserialize <Configuration>(jsonConfigurationUtf8Bytes);

                bool configurationIsValid = true;

                foreach (Configuration.Synchronization synchronization in configuration.synchronizations)
                {
                    if (string.IsNullOrEmpty(synchronization.srcPath))
                    {
                        Logger.log(Logger.Level.Info, "A srcPath is null or empty. Please edit configuration.json to set a valid path.");
                        configurationIsValid = false;
                        break;
                    }
                    if (string.IsNullOrEmpty(synchronization.destPath))
                    {
                        Logger.log(Logger.Level.Info, "A destPath is null or empty. Please edit configuration.json to set a valid path.");
                        configurationIsValid = false;
                        break;
                    }
                }

                if (!configurationIsValid)
                {
                    throw new Exception("Invalid configuration");
                }

                if (!string.IsNullOrEmpty(configuration.logLevel))
                {
                    Logger.setLoglevel((Logger.Level)Enum.Parse(typeof(Logger.Level), configuration.logLevel, true));
                }

                isInError = false;

                greenIcon  = Icon.ExtractAssociatedIcon("resources\\green.ico");
                yellowIcon = Icon.ExtractAssociatedIcon("resources\\yellow.ico");
                redIcon    = Icon.ExtractAssociatedIcon("resources\\red.ico");

                windowsFormsSynchronizationContext = new WindowsFormsSynchronizationContext();

                // Initialize Tray Icon
                trayIcon                  = new NotifyIcon();
                trayIcon.Icon             = yellowIcon;
                trayIcon.Text             = "";
                trayIcon.ContextMenuStrip = new ContextMenuStrip();
                trayIcon.ContextMenuStrip.Items.Add("Show logs", null, onMenuShowLogs);
                trayIcon.ContextMenuStrip.Items.Add("-");
                trayIcon.ContextMenuStrip.Items.Add("Exit", null, onMenuExit);

                trayIcon.Visible = true;

                synchronizationParametersList = new List <SynchronizationParameters>();
                foreach (Configuration.Synchronization synchronization in configuration.synchronizations)
                {
                    Filter filter = null;

                    if (synchronization.filterPatterns == null)
                    {
                        filter = new Filter(synchronization.srcPath, configuration.globalFilterPatterns);
                    }
                    else
                    {
                        filter = new Filter(synchronization.srcPath, synchronization.filterPatterns);
                    }

                    SynchronizationParameters synchronizationParameters = new SynchronizationParameters(
                        synchronization.srcPath,
                        synchronization.destPath,
                        filter);

                    synchronizationParametersList.Add(synchronizationParameters);
                }

                Synchronizer synchronizer = new Synchronizer();
                onSynchronizing();
                foreach (SynchronizationParameters synchronizationParameters in synchronizationParametersList)
                {
                    synchronizer.synchronize(synchronizationParameters.getSrcPath(), synchronizationParameters.getDestPath(), synchronizationParameters.getFilter(), true);
                }
                BlockingCollection <FsEvent> eventList = new BlockingCollection <FsEvent>();
                foreach (SynchronizationParameters synchronizationParameters in synchronizationParametersList)
                {
                    fsListener = new FsListener(synchronizationParameters, eventList);
                }
                eventProcessor = new EventProcessor(eventList, synchronizer, this);
            }
            catch (Exception e)
            {
                clean();
                throw e;
            }
        }