示例#1
0
        public void Dispose()
        {
            Action action = () =>
            {
                disposed = true;
                if (uiThreadNumActive == 0)
                {
                    notifyIcon.DisposeUnlessNull(ref notifyIcon);
                }
            };

            invokeControl.BeginInvoke(action);
        }
示例#2
0
        private void ShowOnUIThread(string s)
        {
            lastShowed = DateTime.Now;

            Action action = () =>
            {
                if (notifyIcon == null)
                {
                    string  company, product;
                    Version version;
                    VaultEagleSynchronizer.GetVersion(System.Reflection.Assembly.GetExecutingAssembly(), out company, out product, out version);
                    string shortVersion = version.Major + "." + version.Minor;

                    string STR_MCAD_Vault_Eagle_1_0___Workspace_Sync = "{0} {1} {2} - Workspace Sync";
                    string title = string.Format(STR_MCAD_Vault_Eagle_1_0___Workspace_Sync, company, product, shortVersion);

                    notifyIcon = new NotifyIcon()
                    {
                        Text            = title,
                        Icon            = Properties.Resources.MCAD_,
                        BalloonTipIcon  = ToolTipIcon.Info,
                        BalloonTipTitle = title
                    };

                    // forward event
                    notifyIcon.BalloonTipClicked += (sender, e) => BalloonTipClicked(sender, e);

                    //notifyIcon.BalloonTipShown += (sender, e) => active = true;
                    EventHandler onClosed = (sender, e) =>
                    {
                        System.Diagnostics.Debug.WriteLine("notifyIcon.BalloonTipClosed");
                        uiThreadNumActive--;

                        if (disposed && uiThreadNumActive == 0)
                        {
                            notifyIcon.DisposeUnlessNull(ref notifyIcon);
                        }
                    };
                    notifyIcon.BalloonTipClosed  += onClosed;
                    notifyIcon.BalloonTipClicked += onClosed;
                    notifyIcon.Visible            = true;
                }

                notifyIcon.BalloonTipText = s;
                notifyIcon.ShowBalloonTip(10000);
            };

            invokeControl.BeginInvoke(action);
        }
        /**********************************************************************************************************************/
        public List <Tuple <string, string> > Synchronize(string configPath, int networkRetries, string synchronizerConfigurationPath, bool useNotification, Option <MCADCommon.LogCommon.DisposableFileLogger> logger, ref Option <string> mailFrom)
        {
            bool disposeLog = false;
            List <Tuple <string, string> > filesToMove = new List <Tuple <string, string> >();

            try
            {
                NotifyIcon trayIcon = new NotifyIcon();
                trayIcon.Text           = "Fetching files to synchronize.";
                trayIcon.Icon           = VaultEagleLib.Properties.Resources.Eagle;
                trayIcon.BalloonTipText = "Fetching files to synchronize.";

                ContextMenu trayMenu = new ContextMenu();

                trayIcon.ContextMenu = trayMenu;
                trayIcon.Visible     = true;
                if (useNotification)
                {
                    trayIcon.ShowBalloonTip(4000);
                }
                errors = new List <string>();
                // Fetch configuration file, mail on failure.
                Option <ADSK.File> configurationFile = Option.None;
                string             path;
                logger.IfSomeDo(l => l.Trace("Before downloading configuration."));
                if (synchronizerConfigurationPath.StartsWith("$"))
                {
                    configurationFile = DownloadConfiguration(configPath, networkRetries, synchronizerConfigurationPath, logger, errors);
                    if (configurationFile.IsNone)
                    {
                        return(filesToMove);
                    }
                    path = Path.Combine(configPath, configurationFile.Get().Name);
                }
                else
                {
                    path = Environment.ExpandEnvironmentVariables(synchronizerConfigurationPath);
                }
                logger.IfSomeDo(l => l.Trace("After downloading configuration."));


                VaultEagleLib.SynchronizerSettings configuration = VaultEagleLib.SynchronizerSettings.Read(networkRetries, logger, path);
                if (logger.IsNone && configuration.LogFile.IsSome)
                {
                    logger     = new MCADCommon.LogCommon.DisposableFileLogger(MCADCommon.LogCommon.DisposableFileLogger.CreateLogFilePath(configuration.configPath, configuration.LogFile.Get()), configuration.LogLevel).AsOption();
                    disposeLog = true;
                }

                //   Option<Logg
                //  if (configuration.LogFile.IsSome)
                logger.IfSomeDo(l => l.Info("Synchronizing vault files."));

                // Fetch files to sync, mail on failure.
                //  List<FileFolder> filesAndFoldersToUpdate = /*new List<FileFolder>();*/FetchingFilesFromVaultToSynchronize(ref mailInfo, lastSyncTime, logger, errors, configuration);
                if (configuration.OverrideMailSender.IsSome)
                {
                    mailFrom = configuration.OverrideMailSender;
                }
                /*List<Tuple<ADSK.File, string, bool, bool>>*/ List <VaultEagleLib.Model.DataStructures.DownloadItem> filesToUpdate = FindFilesChangedSinceLastSync(connection, configuration.UseLastSync, configuration.LastSyncTime, configuration.VaultRoot, networkRetries, trayIcon, configuration.Items.ToArray(), logger);

                foreach (VaultEagleLib.SynchronizationItem item in configuration.Items)
                {
                    foreach (Tuple <string, string> tempFileAndFinalPath in item.TempFilesAndFinalPath)
                    {
                        filesToMove.Add(tempFileAndFinalPath);
                    }
                }

                string now = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

                logger.IfSomeDo(l => l.Info("Synchronizing: " + filesToUpdate.Count + " files."));
                //  if (filesToUpdate.Count > 0)
                {
                    trayIcon.Text = "Downloaded 0 of " + filesToUpdate.Count + " files.";

                    try
                    {
                        errors.AddRange(SynchronizeFiles(networkRetries, logger, errors, configuration, filesToUpdate, trayIcon));
                        List <VaultEagleLib.Model.DataStructures.DownloadFile> downloadedFiles = filesToUpdate.OfType <VaultEagleLib.Model.DataStructures.DownloadFile>().ToList();
                        foreach (string getAndRun in configuration.GetAndRun)
                        {
                            // string test1 = filesToUpdate[filesToUpdate.Count - 1].Item2.Replace(configuration.VaultRoot, "$").Replace("\\", "/") + "/" + filesToUpdate[filesToUpdate.Count - 1].Item1.Name;
                            if (downloadedFiles.Any(f => getAndRun.Equals(f.VaultFileName(configuration.VaultRoot))))
                            {
                                VaultEagleLib.Model.DataStructures.DownloadFile file = downloadedFiles.Where(f => getAndRun.Equals(f.VaultFileName(configuration.VaultRoot))).First();
                                VaultEagle.VaultUtils.HandleNetworkErrors(() =>
                                {
                                    System.Diagnostics.Process proc = new System.Diagnostics.Process();
                                    proc.StartInfo.FileName         = file.LocalFileName;
                                    proc.Start();
                                    proc.WaitForExit();
                                }, networkRetries);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        logger.IfSomeDo(l => l.Error(ex.Message));
                        errors.Add("Failure synchronizing files.");
                    }

                    if (errors.Count == 0 && configuration.LastSyncFile.IsSome)
                    {
                        if (!VaultUtils.HandleNetworkErrors(() => System.IO.Directory.Exists(configuration.configPath), networkRetries))
                        {
                            VaultUtils.HandleNetworkErrors(() => System.IO.Directory.CreateDirectory(configuration.configPath), networkRetries);
                        }

                        if (VaultUtils.HandleNetworkErrors(() => System.IO.File.Exists(Path.Combine(configuration.configPath, configuration.LastSyncFile.Get() + ".txt")), networkRetries))
                        {
                            VaultUtils.HandleNetworkErrors(() => System.IO.File.Delete(Path.Combine(configuration.configPath, configuration.LastSyncFile.Get() + ".txt")), networkRetries);
                        }

                        try
                        {
                            using (TextWriter tw = new StreamWriter(VaultUtils.HandleNetworkErrors(() => System.IO.File.Create(Path.Combine(configuration.configPath, configuration.LastSyncFile.Get() + ".txt")), networkRetries)))
                                tw.WriteLine(now);
                        }
                        catch { }
                    }
                    if (useNotification)
                    {
                        if (errors.Count > 0)
                        {
                            trayIcon.BalloonTipText = "Errors synchronizing files.";
                            if (useNotification)
                            {
                                trayIcon.ShowBalloonTip(2000);
                            }
                        }
                        else
                        {
                            trayIcon.BalloonTipText = "Finished synchronizing files.";
                            if (useNotification)
                            {
                                trayIcon.ShowBalloonTip(2000);
                            }
                        }
                    }
                }
                trayIcon.DisposeUnlessNull(ref trayIcon);
            }
            catch (Exception ex)
            {
                logger.IfSomeDo(l => l.Error(ex.Message));
            }
            logger.IfSomeDo(l => l.Trace("Synchronizing complete."));
            if (disposeLog && logger.IsSome)
            {
                logger.Get().Dispose();
            }


            return(filesToMove);
        }