Exemple #1
0
        public static void Main(string[] args)
        {
            if (Environment.OSVersion.Platform != PlatformID.Win32NT)
            {
                MessageBox.Show(Properties.Resources.MessageApplicationCannotRun, GetTitle() + Constants.Space + Constants.EnDash + Constants.Space + Properties.Resources.CaptionError, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            Settings settings = new Settings();

            if (!settings.DisableThemes)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                settings.RenderWithVisualStyles = Application.RenderWithVisualStyles;
            }
            ArgumentParser argumentParser = new ArgumentParser();

            try {
                argumentParser.Arguments = args;
            } catch (Exception exception) {
                Debug.WriteLine(exception);
                ErrorLog.WriteLine(exception);
                MessageBox.Show(exception.Message, GetTitle() + Constants.Space + Constants.EnDash + Constants.Space + Properties.Resources.CaptionError, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (argumentParser.HasArguments)
            {
                if (argumentParser.IsHelp)
                {
                    StringBuilder stringBuilder = new StringBuilder();
                    stringBuilder.AppendLine(Properties.Resources.HelpLine1.Replace("\\t", "\t")).AppendLine();
                    stringBuilder.AppendLine(Properties.Resources.HelpLine2.Replace("\\t", "\t"));
                    stringBuilder.AppendLine(Properties.Resources.HelpLine3.Replace("\\t", "\t"));
                    stringBuilder.AppendLine(Properties.Resources.HelpLine4.Replace("\\t", "\t")).AppendLine();
                    stringBuilder.AppendLine(Properties.Resources.HelpLine5.Replace("\\t", "\t"));
                    stringBuilder.AppendLine(Properties.Resources.HelpLine6.Replace("\\t", "\t")).AppendLine();
                    stringBuilder.AppendLine(Properties.Resources.HelpLine7.Replace("\\t", "\t"));
                    stringBuilder.AppendLine(Properties.Resources.HelpLine8.Replace("\\t", "\t")).AppendLine();
                    stringBuilder.AppendLine(Properties.Resources.HelpLine9.Replace("\\t", "\t"));
                    stringBuilder.AppendLine(Properties.Resources.HelpLine10.Replace("\\t", "\t")).AppendLine();
                    stringBuilder.AppendLine(Properties.Resources.HelpLine11.Replace("\\t", "\t"));
                    stringBuilder.AppendLine(Properties.Resources.HelpLine12.Replace("\\t", "\t")).AppendLine();
                    stringBuilder.AppendLine(Properties.Resources.HelpLine13.Replace("\\t", "\t")).AppendLine();
                    stringBuilder.AppendLine(Properties.Resources.HelpLine14.Replace("\\t", "\t"));
                    stringBuilder.AppendLine(Properties.Resources.HelpLine15.Replace("\\t", "\t")).AppendLine();
                    stringBuilder.AppendLine(Properties.Resources.HelpLine16.Replace("\\t", "\t")).AppendLine();
                    stringBuilder.AppendLine(Properties.Resources.HelpLine17.Replace("\\t", "\t")).AppendLine();
                    stringBuilder.AppendLine(Properties.Resources.HelpLine18.Replace("\\t", "\t"));
                    MessageBox.Show(stringBuilder.ToString(), GetTitle() + Constants.Space + Constants.EnDash + Constants.Space + Properties.Resources.CaptionHelp, MessageBoxButtons.OK, MessageBoxIcon.Question);
                }
                else if (argumentParser.IsTest)
                {
                    try {
                        Application.Run(new TestForm(args));
                    } catch (Exception exception) {
                        Debug.WriteLine(exception);
                        ErrorLog.WriteLine(exception);
                        MessageBox.Show(exception.Message, GetTitle() + Constants.Space + Constants.EnDash + Constants.Space + Properties.Resources.CaptionError, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        MessageBox.Show(Properties.Resources.MessageApplicationError, GetTitle(), MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                else if (argumentParser.IsThisTest)
                {
                    try {
                        Application.Run(new ArgumentParserForm());
                    } catch (Exception exception) {
                        Debug.WriteLine(exception);
                        ErrorLog.WriteLine(exception);
                        MessageBox.Show(exception.Message, GetTitle() + Constants.Space + Constants.EnDash + Constants.Space + Properties.Resources.CaptionError, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        MessageBox.Show(Properties.Resources.MessageApplicationError, GetTitle(), MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                else
                {
                    try {
                        LauncherAsDate launcherAsDate = new LauncherAsDate()
                        {
                            ApplicationFilePath   = argumentParser.ApplicationFilePath,
                            DateTime              = argumentParser.DateTime.Value,
                            Arguments             = argumentParser.ApplicationArguments,
                            WorkingFolderPath     = argumentParser.WorkingFolderPath,
                            OneInstance           = argumentParser.OneInstance,
                            Interval              = argumentParser.Interval,
                            DisableTimeCorrection = settings.DisableTimeCorrection,
                            ForceTimeCorrection   = settings.ForceTimeCorrection
                        };
                        launcherAsDate.Launch();
                    } catch (Exception exception) {
                        Debug.WriteLine(exception);
                        ErrorLog.WriteLine(exception);
                        MessageBox.Show(exception.Message, GetTitle() + Constants.Space + Constants.EnDash + Constants.Space + Properties.Resources.CaptionError, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            else
            {
                try {
                    SingleMainForm.Run(new MainForm(settings));
                } catch (Exception exception) {
                    Debug.WriteLine(exception);
                    ErrorLog.WriteLine(exception);
                    MessageBox.Show(exception.Message, GetTitle() + Constants.Space + Constants.EnDash + Constants.Space + Properties.Resources.CaptionError, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    MessageBox.Show(Properties.Resources.MessageApplicationError, GetTitle(), MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
Exemple #2
0
        private void Evaluate()
        {
            DateTime systemDateTime = LauncherAsDate.GetSystemTime();

            foreach (string arg in arguments)
            {
                string argument = arg;
                hasArguments = true;
                if (argument == "-i" || argument == "/i")               //Input file path: Application to launch.
                {
                    if (filePathSet || expectingFilePath)
                    {
                        throw new ApplicationException(Properties.Resources.ExceptionMessageI);
                    }
                    if (expectingDate || expectingSpan || expectingArguments || expectingInterval || expectingFolderPath || testSet || helpSet || thisTestSet)
                    {
                        throw new ApplicationException(Properties.Resources.ExceptionMessageM);
                    }
                    expectingFilePath = true;
                }
                else if (argument == "-d" || argument == "/d")          //Absolute date in format yyyy-mm-dd.
                {
                    if (dateSet || expectingDate)
                    {
                        throw new ApplicationException(Properties.Resources.ExceptionMessageD);
                    }
                    if (dateTime.HasValue)
                    {
                        throw new ApplicationException(Properties.Resources.ExceptionMessageT);
                    }
                    if (expectingFilePath || expectingSpan || expectingArguments || expectingInterval || expectingFolderPath || testSet || helpSet || thisTestSet)
                    {
                        throw new ApplicationException(Properties.Resources.ExceptionMessageM);
                    }
                    expectingDate = true;
                }
                else if (argument == "-r" || argument == "/r")          //Relative time span in format for example -9day, -1year, +2month.
                {
                    if (spanSet || expectingSpan)
                    {
                        throw new ApplicationException(Properties.Resources.ExceptionMessageQ);
                    }
                    if (dateTime.HasValue)
                    {
                        throw new ApplicationException(Properties.Resources.ExceptionMessageT);
                    }
                    if (expectingFilePath || expectingDate || expectingArguments || expectingInterval || expectingFolderPath || testSet || helpSet || thisTestSet)
                    {
                        throw new ApplicationException(Properties.Resources.ExceptionMessageM);
                    }
                    expectingSpan = true;
                }
                else if (argument == "-a" || argument == "/a")          //Arguments passed to the launched application.
                {
                    if (argumentsSet || expectingArguments)
                    {
                        throw new ApplicationException(Properties.Resources.ExceptionMessageA);
                    }
                    if (expectingFilePath || expectingDate || expectingSpan || expectingInterval || expectingFolderPath || testSet || helpSet || thisTestSet)
                    {
                        throw new ApplicationException(Properties.Resources.ExceptionMessageM);
                    }
                    expectingArguments = true;
                }
                else if (argument == "-s" || argument == "/s")          //Interval in seconds to return to the current date.
                {
                    if (intervalSet || expectingInterval)
                    {
                        throw new ApplicationException(Properties.Resources.ExceptionMessageS);
                    }
                    if (expectingFilePath || expectingDate || expectingSpan || expectingArguments || expectingFolderPath || testSet || helpSet || thisTestSet)
                    {
                        throw new ApplicationException(Properties.Resources.ExceptionMessageM);
                    }
                    expectingInterval = true;
                }
                else if (argument == "-w" || argument == "/w")          //Working folder path.
                {
                    if (folderPathSet || expectingFolderPath)
                    {
                        throw new ApplicationException(Properties.Resources.ExceptionMessageW);
                    }
                    if (expectingFilePath || expectingDate || expectingSpan || expectingArguments || expectingInterval || testSet || helpSet || thisTestSet)
                    {
                        throw new ApplicationException(Properties.Resources.ExceptionMessageM);
                    }
                    expectingFolderPath = true;
                }
                else if (argument == "-o" || argument == "/o")          //Allows only one instance.
                {
                    if (oneInstanceSet || testSet || helpSet || thisTestSet || expectingFilePath || expectingDate || expectingSpan || expectingArguments || expectingInterval || expectingFolderPath)
                    {
                        throw new ApplicationException(Properties.Resources.ExceptionMessageM);
                    }
                    oneInstanceSet = true;
                }
                else if (argument == "-t" || argument == "/t")          //Test mode (will show form with the date launched).
                {
                    if (filePathSet || dateSet || spanSet || argumentsSet || intervalSet || oneInstanceSet || testSet || helpSet || thisTestSet || expectingFilePath || expectingDate || expectingSpan || expectingArguments || expectingInterval || expectingFolderPath)
                    {
                        throw new ApplicationException(Properties.Resources.ExceptionMessageM);
                    }
                    testSet = true;
                }
                else if (argument == "-h" || argument == "/h" || argument == "-?" || argument == "/?")          //Will show help.
                {
                    if (filePathSet || dateSet || spanSet || argumentsSet || intervalSet || oneInstanceSet || testSet || helpSet || thisTestSet || expectingFilePath || expectingDate || expectingSpan || expectingArguments || expectingInterval || expectingFolderPath)
                    {
                        throw new ApplicationException(Properties.Resources.ExceptionMessageM);
                    }
                    helpSet = true;
                }
                else if (argument == "-T" || argument == "/T")          //Test mode (ArgumentParser test).
                {
                    if (filePathSet || dateSet || spanSet || argumentsSet || intervalSet || oneInstanceSet || testSet || helpSet || thisTestSet || expectingFilePath || expectingDate || expectingSpan || expectingArguments || expectingInterval || expectingFolderPath)
                    {
                        throw new ApplicationException(Properties.Resources.ExceptionMessageM);
                    }
                    thisTestSet = true;
                }
                else if (expectingFilePath)
                {
                    applicationFilePath = argument;
                    expectingFilePath   = false;
                    filePathSet         = true;
                }
                else if (expectingDate)
                {
                    if (dateTime.HasValue)
                    {
                        throw new ApplicationException(Properties.Resources.ExceptionMessageT);
                    }
                    dateTime      = System.DateTime.Parse(argument).Add(systemDateTime.TimeOfDay);
                    expectingDate = false;
                    dateSet       = true;
                }
                else if (expectingSpan)
                {
                    if (dateTime.HasValue)
                    {
                        throw new ApplicationException(Properties.Resources.ExceptionMessageT);
                    }
                    if (!spanRegex.IsMatch(argument))
                    {
                        throw new ApplicationException(Properties.Resources.ExceptionMessageF);
                    }
                    string[] span = spanRegex.Split(argument);
                    if (int.Parse(span[2]) == 0)
                    {
                        throw new ApplicationException(Properties.Resources.ExceptionMessageZ);
                    }
                    if (span[3].Equals("year", StringComparison.OrdinalIgnoreCase) || span[3].Equals("years", StringComparison.OrdinalIgnoreCase))
                    {
                        dateTime = systemDateTime.AddYears(span[1] == "-" ? 0 - int.Parse(span[2]) : int.Parse(span[2]));
                    }
                    else if (span[3].Equals("month", StringComparison.OrdinalIgnoreCase) || span[3].Equals("months", StringComparison.OrdinalIgnoreCase))
                    {
                        dateTime = systemDateTime.AddMonths(span[1] == "-" ? 0 - int.Parse(span[2]) : int.Parse(span[2]));
                    }
                    else
                    {
                        dateTime = systemDateTime.AddDays(span[1] == "-" ? 0 - double.Parse(span[2]) : double.Parse(span[2]));
                    }
                    expectingSpan = false;
                    spanSet       = true;
                }
                else if (expectingArguments)
                {
                    applicationArguments = argument;
                    expectingArguments   = false;
                    argumentsSet         = true;
                }
                else if (expectingFolderPath)
                {
                    workingFolderPath   = argument;
                    expectingFolderPath = false;
                    folderPathSet       = true;
                }
                else if (expectingInterval)
                {
                    interval = int.Parse(argument);
                    if (interval < Constants.IntervalMinimum || interval > Constants.IntervalMaximum)
                    {
                        throw new ApplicationException(string.Format(Properties.Resources.ExceptionMessageN, Constants.IntervalMaximum));
                    }
                    expectingInterval = false;
                    intervalSet       = true;
                }
                else if (argument.StartsWith("-") || argument.StartsWith("/"))
                {
                    throw new ApplicationException(Properties.Resources.ExceptionMessageU);
                }
                else
                {
                    throw new ApplicationException(Properties.Resources.ExceptionMessageM);
                }
            }
            if (expectingFilePath || expectingDate || expectingSpan || expectingArguments || expectingInterval || expectingFolderPath)
            {
                throw new ApplicationException(Properties.Resources.ExceptionMessageM);
            }
            if (hasArguments && !testSet && !helpSet && !thisTestSet)
            {
                if (!dateTime.HasValue && interval == 0)
                {
                    throw new ApplicationException(Properties.Resources.ExceptionMessageJ);
                }
                if (!dateTime.HasValue)
                {
                    throw new ApplicationException(Properties.Resources.ExceptionMessageK);
                }
                if (interval == 0)
                {
                    throw new ApplicationException(Properties.Resources.ExceptionMessageL);
                }
            }
        }