private bool TryParseCommandLine(CommandLineActivatedEventArgs args, out int id, out bool test)
        {
#if !DEBUG
            if (args.PreviousExecutionState != ApplicationExecutionState.Terminated)
            {
                id   = 0;
                test = false;
                return(false);
            }
#endif

            try
            {
                var v_id   = 0;
                var v_test = false;

                var p = new OptionSet()
                {
                    { "i|id=", (int v) => v_id = v },
                    { "t|test", v => v_test = v != null },
                };

                var extra = p.Parse(args.Operation.Arguments.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));

                id   = v_id;
                test = v_test;
                return(true);
            }
            catch
            {
                id   = 0;
                test = false;
                return(false);
            }
        }
Exemple #2
0
        protected override void OnActivated(IActivatedEventArgs args)
        {
            string additionParameter = "";

            switch (args.Kind)
            {
            case ActivationKind.CommandLineLaunch:
                CommandLineActivatedEventArgs cmdLineArgs =
                    args as CommandLineActivatedEventArgs;
                CommandLineActivationOperation operation = cmdLineArgs.Operation;
                string cmdLineString  = operation.Arguments;
                string activationPath = operation.CurrentDirectoryPath;
                break;

            case ActivationKind.Protocol:
                ProtocolActivatedEventArgs uriArg = args as ProtocolActivatedEventArgs;
                additionParameter = GetParameterURI(uriArg.Uri.OriginalString);
                break;
            }
            //Navigate
            Frame rootFrame = Window.Current.Content as Frame;

            if (rootFrame == null)
            {
                rootFrame = new Frame();
                Window.Current.Content = rootFrame;
            }
            rootFrame.Navigate(typeof(MainPage), additionParameter);

            Window.Current.Activate();
        }
Exemple #3
0
        protected override void OnActivated(IActivatedEventArgs args)
        {
            switch (args.Kind)
            {
            case ActivationKind.CommandLineLaunch:
                CommandLineActivatedEventArgs  cmdLineArgs = args as CommandLineActivatedEventArgs;
                CommandLineActivationOperation operation   = cmdLineArgs.Operation;
                Organization = operation.Arguments.TrimEnd();
                break;

            case ActivationKind.StartupTask:
                // handle auto-launch specific here
                break;

            default:
                break;
            }

            Frame rootFrame = Window.Current.Content as Frame;

            if (rootFrame == null)
            {
                rootFrame = new Frame();
                Window.Current.Content = rootFrame;
            }
            rootFrame.Navigate(typeof(MainPage));

            Window.Current.Activate();
        }
Exemple #4
0
        protected async override void OnActivated(IActivatedEventArgs args)
        {
            switch (args.Kind)
            {
            case ActivationKind.CommandLineLaunch:
                CommandLineActivatedEventArgs  cmdLineArgs = args as CommandLineActivatedEventArgs;
                CommandLineActivationOperation operation   = cmdLineArgs.Operation;
                string cmdLineString  = operation.Arguments;
                string activationPath = operation.CurrentDirectoryPath;

                Frame rootFrame = Window.Current.Content as Frame;
                if (rootFrame == null)
                {
                    rootFrame = new Frame();
                    Window.Current.Content = rootFrame;
                }

                string[] argz = cmdLineString.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                if (argz.Length > 1 && argz[1].Length != 0)
                {
                    string arg1 = argz[1];
                    rootFrame.Navigate(typeof(MainPage), arg1);
                }
                else
                {
                    rootFrame.Navigate(typeof(MainPage), "https://google.com");
                }

                Window.Current.Activate();
                break;
            }
        }
Exemple #5
0
        protected override void OnActivated(IActivatedEventArgs args)
        {
            switch (args.Kind)
            {
            case ActivationKind.CommandLineLaunch:
                CommandLineActivatedEventArgs cmdLineArgs =
                    args as CommandLineActivatedEventArgs;
                CommandLineActivationOperation operation = cmdLineArgs.Operation;
                string cmdLineString  = operation.Arguments;
                string activationPath = operation.CurrentDirectoryPath;

                Frame rootFrame = Window.Current.Content as Frame;
                if (rootFrame == null)
                {
                    rootFrame = new Frame();
                    Window.Current.Content = rootFrame;
                }

                ParsedCommands parsedCommands =
                    CommandLineParser.ParseUntrustedArgs(cmdLineString);
                if (parsedCommands != null && parsedCommands.Count > 0)
                {
                    foreach (ParsedCommand command in parsedCommands)
                    {
                        switch (command.Type)
                        {
                        case ParsedCommandType.SelectItem:
                            rootFrame.Navigate(typeof(SelectItemPage), command.Payload);
                            break;

                        case ParsedCommandType.LoadConfig:
                            rootFrame.Navigate(typeof(LoadConfigPage), command.Payload);
                            break;

                        case ParsedCommandType.LoadFile:
                            string filePath = Path.Combine(activationPath, command.Payload);
                            rootFrame.Navigate(typeof(LoadFilePage), filePath);
                            break;

                        case ParsedCommandType.Unknown:
                            rootFrame.Navigate(typeof(HelpPage), cmdLineString);
                            break;
                        }
                    }
                }
                else
                {
                    rootFrame.Navigate(typeof(MainPage));
                }

                Window.Current.Activate();
                break;
            }
        }
        protected override void OnActivated(IActivatedEventArgs args)
        {
            switch (args.Kind)
            {
            case ActivationKind.CommandLineLaunch:
                CommandLineActivatedEventArgs  cmdLineArgs = args as CommandLineActivatedEventArgs;
                CommandLineActivationOperation operation   = cmdLineArgs.Operation;
                string activationPath = operation.CurrentDirectoryPath;

                Frame rootFrame = Window.Current.Content as Frame;
                if (rootFrame == null)
                {
                    rootFrame = new Frame();
                    Window.Current.Content = rootFrame;
                }
                rootFrame.Navigate(typeof(MainPage), activationPath);
                Window.Current.Activate();
                break;
            }
        }
Exemple #7
0
        private static async Task CommandActivated(Frame rootFrame, CommandLineActivatedEventArgs commandLineActivatedEventArgs)
        {
            LoggingService.LogInfo($"[CommandActivated] CurrentDirectoryPath: {commandLineActivatedEventArgs.Operation.CurrentDirectoryPath} " +
                                   $"Arguments: {commandLineActivatedEventArgs.Operation.Arguments}");

            if (rootFrame.Content == null)
            {
                rootFrame.Navigate(typeof(NotepadsMainPage), commandLineActivatedEventArgs);
            }
            else if (rootFrame.Content is NotepadsMainPage mainPage)
            {
                var file = await FileSystemUtility.OpenFileFromCommandLine(
                    commandLineActivatedEventArgs.Operation.CurrentDirectoryPath,
                    commandLineActivatedEventArgs.Operation.Arguments);

                if (file != null)
                {
                    await mainPage.OpenFile(file);
                }
            }
        }
Exemple #8
0
        //Reference : https://docs.microsoft.com/en-us/uwp/api/windows.applicationmodel.activation.commandlineactivatedeventargs
        protected override void OnActivated(IActivatedEventArgs args)
        {
            string activationArgString = string.Empty;
            string activationPath      = string.Empty;
            string cmdLineString       = string.Empty;

            switch (args.Kind)
            {
            case ActivationKind.Launch:
                var launchArgs = args as LaunchActivatedEventArgs;
                activationArgString = launchArgs.Arguments;
                break;

            // A new ActivationKind for console activation of a windowed app.
            case ActivationKind.CommandLineLaunch:
                CommandLineActivatedEventArgs  cmdLineArgs = args as CommandLineActivatedEventArgs;
                CommandLineActivationOperation operation   = cmdLineArgs.Operation;
                cmdLineString  = operation.Arguments;
                activationPath = operation.CurrentDirectoryPath;
                break;
            }
        }
Exemple #9
0
        /// <summary>
        /// Called when the app view is activated. Activates the app's CoreWindow.
        /// </summary>
        private void OnViewActivated(CoreApplicationView sender, IActivatedEventArgs args)
        {
            // command line test
            // https://docs.microsoft.com/en-us/uwp/api/windows.applicationmodel.activation.commandlineactivatedeventargs
            switch (args.Kind)
            {
            case ActivationKind.Launch:
                var launchArgs          = args as LaunchActivatedEventArgs;
                var activationArgString = launchArgs.Arguments;
                break;

            // A new ActivationKind for console activation of a windowed app.
            case ActivationKind.CommandLineLaunch:
                CommandLineActivatedEventArgs  cmdLineArgs = args as CommandLineActivatedEventArgs;
                CommandLineActivationOperation operation   = cmdLineArgs.Operation;
                var cmdLineString  = operation.Arguments;
                var activationPath = operation.CurrentDirectoryPath;
                break;
            }
            //

            // Run() won't start until the CoreWindow is activated.
            sender.CoreWindow.Activate();
        }
        private async Task CliRunAsync(object verb, CommandLineActivatedEventArgs commandLineActivated)
        {
            if (verb is SettingsVerb settingsVerb)
            {
                if (!settingsVerb.Import && !settingsVerb.Export)
                {
                    await ShowSettingsAsync();
                }
                else if (settingsVerb.Export)
                {
                    var exportFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("config.json", CreationCollisionOption.OpenIfExists);

                    var settings = _settingsService.ExportSettings();
                    await FileIO.WriteTextAsync(exportFile, settings);

                    await new MessageDialog($"{I18N.Translate("SettingsExported")} {exportFile.Path}").ShowAsync();
                }
                else if (settingsVerb.Import)
                {
                    var file = await ApplicationData.Current.LocalFolder.GetFileAsync("config.json");

                    var content = await FileIO.ReadTextAsync(file);

                    _settingsService.ImportSettings(content);
                    await new MessageDialog($"{I18N.Translate("SettingsImported")} {file.Path}").ShowAsync();
                }
            }
            else if (verb is NewVerb newVerb)
            {
                var profile = default(ShellProfile);
                if (!string.IsNullOrWhiteSpace(newVerb.Profile))
                {
                    profile = _settingsService.GetShellProfiles().FirstOrDefault(x => x.Name.Equals(newVerb.Profile, StringComparison.CurrentCultureIgnoreCase));
                }

                if (profile == null)
                {
                    profile = _settingsService.GetDefaultShellProfile();
                }

                if (!string.IsNullOrWhiteSpace(newVerb.Directory) && newVerb.Directory != ".")
                {
                    profile.WorkingDirectory = newVerb.Directory;
                }
                else
                {
                    profile.WorkingDirectory = commandLineActivated.Operation.CurrentDirectoryPath;
                }

                var location = newVerb.Target == Target.Default ? _applicationSettings.NewTerminalLocation
                    : newVerb.Target == Target.Tab ? NewTerminalLocation.Tab
                    : NewTerminalLocation.Window;

                await CreateTerminalAsync(profile, location);
            }
            else if (verb is RunVerb runVerb)
            {
                var profile = new ShellProfile
                {
                    Id               = Guid.Empty,
                    Location         = null,
                    Arguments        = runVerb.Command,
                    WorkingDirectory = runVerb.Directory
                };

                if (!string.IsNullOrEmpty(runVerb.Buffer) && bool.TryParse(runVerb.Buffer, out var useBuffer))
                {
                    profile.UseBuffer = useBuffer;
                }

                if (!string.IsNullOrWhiteSpace(runVerb.Theme))
                {
                    var theme = _settingsService.GetThemes().FirstOrDefault(x => x.Name.Equals(runVerb.Theme, StringComparison.CurrentCultureIgnoreCase));
                    if (theme != null)
                    {
                        profile.TerminalThemeId = theme.Id;
                    }
                }

                if (string.IsNullOrWhiteSpace(profile.WorkingDirectory))
                {
                    profile.WorkingDirectory = commandLineActivated.Operation.CurrentDirectoryPath;
                }

                var location = runVerb.Target == Target.Default ? _applicationSettings.NewTerminalLocation
                    : runVerb.Target == Target.Tab ? NewTerminalLocation.Tab
                    : NewTerminalLocation.Window;

                await CreateTerminalAsync(profile, location);
            }
        }
Exemple #11
0
        private async Task ParseCommandLineArgumentsAsync(object verb, CommandLineActivatedEventArgs commandLineActivated)
        {
            using var deferral = commandLineActivated.Operation.GetDeferral();

            if (verb is SettingsVerb settingsVerb)
            {
                if (string.IsNullOrWhiteSpace(settingsVerb.Import) && !settingsVerb.Export)
                {
                    await ShowSettingsAsync();
                }
                else if (settingsVerb.Export)
                {
                    var settings = _settingsService.ExportSettings();
                    var path     = Path.Combine(commandLineActivated.Operation.CurrentDirectoryPath, "config.json");
                    try
                    {
                        await StartSystemTray().ConfigureAwait(false);

                        await _trayProcessCommunicationService.SaveTextFileAsync(path, settings).ConfigureAwait(false);

                        _notificationService.ShowNotification("Export settings", $"Settings were exported to {path}.");
                    }
                    catch (Exception e)
                    {
                        _notificationService.ShowNotification("Export settings", $"Failed with error: {e.Message}");
                        commandLineActivated.Operation.ExitCode = 1;
                    }

                    if (!_isLaunching && !_alreadyLaunched)
                    {
                        Exit();
                    }
                }
                else if (!string.IsNullOrWhiteSpace(settingsVerb.Import))
                {
                    var path = Path.Combine(commandLineActivated.Operation.CurrentDirectoryPath, settingsVerb.Import.Replace(".\\", string.Empty));
                    try
                    {
                        await StartSystemTray().ConfigureAwait(false);

                        var content = await _trayProcessCommunicationService.ReadTextFileAsync(path).ConfigureAwait(false);

                        _settingsService.ImportSettings(content);
                        _notificationService.ShowNotification("Import settings", $"Successfully imported settings from {path}.");
                    }
                    catch (Exception e)
                    {
                        _notificationService.ShowNotification("Import settings", $"Failed with error: {e.Message}");
                        commandLineActivated.Operation.ExitCode = 1;
                    }

                    if (!_isLaunching && !_alreadyLaunched)
                    {
                        Exit();
                    }
                }
            }
            else if (verb is NewVerb newVerb)
            {
                var profile = default(ShellProfile);
                if (!string.IsNullOrWhiteSpace(newVerb.Profile))
                {
                    profile = _settingsService.GetShellProfiles().FirstOrDefault(x => x.Name.Equals(newVerb.Profile, StringComparison.CurrentCultureIgnoreCase));
                }

                if (profile == null)
                {
                    profile = _settingsService.GetDefaultShellProfile();
                }

                if (!string.IsNullOrWhiteSpace(newVerb.Directory) && newVerb.Directory != ".")
                {
                    profile.WorkingDirectory = newVerb.Directory;
                }
                else
                {
                    profile.WorkingDirectory = commandLineActivated.Operation.CurrentDirectoryPath;
                }

                var location = newVerb.Target == Target.Default ? _applicationSettings.NewTerminalLocation
                    : newVerb.Target == Target.Tab ? NewTerminalLocation.Tab
                    : NewTerminalLocation.Window;

                await CreateTerminalAsync(profile, location);
            }
            else if (verb is RunVerb runVerb)
            {
                var profile = new ShellProfile
                {
                    Id               = Guid.Empty,
                    Location         = null,
                    Arguments        = runVerb.Command,
                    WorkingDirectory = runVerb.Directory
                };

                if (runVerb.SmartBuffer.HasValue)
                {
                    profile.UseBuffer = runVerb.SmartBuffer.Value;
                }

                if (!string.IsNullOrWhiteSpace(runVerb.Theme))
                {
                    var theme = _settingsService.GetThemes().FirstOrDefault(x => x.Name.Equals(runVerb.Theme, StringComparison.CurrentCultureIgnoreCase));
                    if (theme != null)
                    {
                        profile.TerminalThemeId = theme.Id;
                    }
                }

                if (string.IsNullOrWhiteSpace(profile.WorkingDirectory))
                {
                    profile.WorkingDirectory = commandLineActivated.Operation.CurrentDirectoryPath;
                }

                var location = runVerb.Target == Target.Default ? _applicationSettings.NewTerminalLocation
                    : runVerb.Target == Target.Tab ? NewTerminalLocation.Tab
                    : NewTerminalLocation.Window;

                await CreateTerminalAsync(profile, location);
            }
        }
Exemple #12
0
        protected override async void OnActivated(IActivatedEventArgs args)
        {
            switch (args.Kind)
            {
            case ActivationKind.CommandLineLaunch:
                CommandLineActivatedEventArgs cmdLineArgs =
                    args as CommandLineActivatedEventArgs;
                CommandLineActivationOperation operation = cmdLineArgs.Operation;
                string cmdLineString  = operation.Arguments;
                string activationPath = operation.CurrentDirectoryPath;

                string[] cmdArgs = cmdLineString.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);

                var mainPage = MainPage.Current;

                if (cmdArgs.Count() != 2)
                {
                    mainPage.NotifyUser("Invalid number of arguments. Usage: RadioControl.exe RADIO_INDEX_OR_NAME ON|OFF", MainPage.NotifyType.ErrorMessage);
                    return;
                }

                bool   isDeviceNumeric = false;
                int    deviceNumber    = 0;
                string deviceName      = "";
                try
                {
                    deviceNumber    = int.Parse(cmdArgs[0]);
                    isDeviceNumeric = true;
                } catch (FormatException)
                {
                    deviceName = cmdArgs[0];
                }

                bool actionEnable = false;
                if (cmdArgs[1].ToLower() == "on")
                {
                    actionEnable = true;
                }
                else if (cmdArgs[1].ToLower() != "off")
                {
                    mainPage.NotifyUser("Unsupported action '" + cmdArgs[1] + "'. Only 'on' and 'off' are supported.", MainPage.NotifyType.ErrorMessage);
                    return;
                }

                Debug.WriteLine("Will turn device " + (isDeviceNumeric ? deviceNumber.ToString() : deviceName) + " " + (actionEnable ? "on" : "off"));

                var accessLevel = await Radio.RequestAccessAsync();

                if (accessLevel != RadioAccessStatus.Allowed)
                {
                    mainPage.NotifyUser("Could not get access to radios. Allow it in Windows settings.", MainPage.NotifyType.ErrorMessage);
                    return;
                }
                else
                {
                    var radios = mainPage.radios;
                    for (int i = 0; i < radios.Count; i++)
                    {
                        var radio = radios[i];
                        if ((isDeviceNumeric && i == deviceNumber) || (!isDeviceNumeric && radio.Name.ToLower() == deviceName.ToLower()))
                        {
                            radio.IsRadioOn = actionEnable;
                            break;
                        }
                    }
                }

                break;
            }

            base.OnActivated(args);
        }
Exemple #13
0
 protected virtual Task HandleActivation(CommandLineActivatedEventArgs args)
 {
     return(Task.CompletedTask);
 }
Exemple #14
0
        protected override async void OnActivated(IActivatedEventArgs args)
        {
            switch (args.Kind)
            {
            case ActivationKind.CommandLineLaunch:
                CommandLineActivatedEventArgs cmdLineArgs =
                    args as CommandLineActivatedEventArgs;
                CommandLineActivationOperation operation = cmdLineArgs.Operation;
                string cmdLineString  = operation.Arguments;
                string activationPath = operation.CurrentDirectoryPath;

                string[] cmdArgs = cmdLineString.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);

                if (cmdArgs.Count() != 2)
                {
                    ShowError("Invalid number of arguments. Usage: RadioControl.exe RADIO_INDEX_OR_NAME ON|OFF");
                    return;
                }

                bool   isDeviceNumeric = false;
                int    deviceNumber    = 0;
                string deviceName      = "";
                try
                {
                    deviceNumber    = int.Parse(cmdArgs[0]);
                    isDeviceNumeric = true;
                } catch (FormatException)
                {
                    deviceName = cmdArgs[0];
                }

                bool actionEnable = false;
                if (cmdArgs[1].ToLower() == "on")
                {
                    actionEnable = true;
                }
                else if (cmdArgs[1].ToLower() != "off")
                {
                    ShowError("Unsupported action '" + cmdArgs[1] + "'. Only 'on' and 'off' are supported.");
                    return;
                }

                var accessLevel = await Radio.RequestAccessAsync();

                if (accessLevel != RadioAccessStatus.Allowed)
                {
                    ShowError("Could not get access to radios. Allow it in Windows settings.");
                    return;
                }
                else
                {
                    var radios = await Radio.GetRadiosAsync();

                    for (int i = 0; i < radios.Count; i++)
                    {
                        var radio = radios[i];
                        if ((isDeviceNumeric && i == deviceNumber) || (!isDeviceNumeric && radio.Name.ToLower() == deviceName.ToLower()))
                        {
                            var radioState = actionEnable ? RadioState.On : RadioState.Off;
                            await radio.SetStateAsync(radioState);

                            CoreApplication.Exit();
                            return;
                        }
                    }

                    ShowError("Radio " + deviceName + "/" + deviceNumber + " was not found.");
                    return;
                }

                break;
            }
        }
        public static async Task <CommandLinePayload> GetPayload(this CommandLineActivatedEventArgs args)
        {
            var currentDirectory = await StorageFolder.GetFolderFromPathAsync(args.Operation.CurrentDirectoryPath);

            return(new CommandLinePayload(args.Operation, currentDirectory));
        }