Skip to content

Commit 78bef53

Browse files
lampenlampenyaira2
authored andcommitted
CommandPrompt Activation (#364)
* Add AppExecutionAlias * comment untested code * new CommandLineParser * Delete unused code
1 parent 6695eb8 commit 78bef53

File tree

7 files changed

+227
-23
lines changed

7 files changed

+227
-23
lines changed

Files.Package/Package.appxmanifest

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities" xmlns:desktop4="http://schemas.microsoft.com/appx/manifest/desktop/windows10/4" xmlns:desktop="http://schemas.microsoft.com/appx/manifest/desktop/windows10" xmlns:uap5="http://schemas.microsoft.com/appx/manifest/uap/windows10/5" IgnorableNamespaces="uap mp rescap desktop4 desktop">
2+
<Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities" xmlns:desktop4="http://schemas.microsoft.com/appx/manifest/desktop/windows10/4" xmlns:desktop="http://schemas.microsoft.com/appx/manifest/desktop/windows10" xmlns:uap5="http://schemas.microsoft.com/appx/manifest/uap/windows10/5" IgnorableNamespaces="uap uap5 mp rescap desktop4 desktop">
33
<Identity Name="49306atecsolution.FilesUWP" Publisher="CN=53EC4384-7F5B-4CF6-8C23-513FFE9D1AB7" Version="0.7.0.0" />
44
<Properties>
55
<DisplayName>Files</DisplayName>
@@ -36,6 +36,14 @@
3636
<uap:Extension Category="windows.protocol">
3737
<uap:Protocol ReturnResults="none" Name="files-uwp"/>
3838
</uap:Extension>
39+
<uap5:Extension
40+
Category="windows.appExecutionAlias"
41+
Executable="files.exe"
42+
EntryPoint="files.App">
43+
<uap5:AppExecutionAlias>
44+
<uap5:ExecutionAlias Alias="files.exe"/>
45+
</uap5:AppExecutionAlias>
46+
</uap5:Extension>
3947
</Extensions>
4048
</Application>
4149
</Applications>

Files/App.xaml.cs

Lines changed: 65 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
using System.Threading.Tasks;
3434
using Windows.ApplicationModel.AppService;
3535
using Windows.ApplicationModel.AppExtensions;
36+
using Files.CommandLine;
3637
using Files.DataModels;
3738
using Newtonsoft.Json;
3839

@@ -866,30 +867,72 @@ protected override void OnActivated(IActivatedEventArgs args)
866867
Window.Current.Content = rootFrame;
867868
}
868869

869-
if (args.Kind == ActivationKind.Protocol)
870+
switch (args.Kind)
870871
{
871-
var eventArgs = args as ProtocolActivatedEventArgs;
872+
case ActivationKind.Protocol:
873+
var eventArgs = args as ProtocolActivatedEventArgs;
872874

873-
if (eventArgs.Uri.AbsoluteUri == "files-uwp:")
874-
{
875-
rootFrame.Navigate(typeof(InstanceTabsView), null, new SuppressNavigationTransitionInfo());
876-
}
877-
else
878-
{
879-
var trimmedPath = eventArgs.Uri.OriginalString.Split('=')[1];
880-
rootFrame.Navigate(typeof(InstanceTabsView), @trimmedPath, new SuppressNavigationTransitionInfo());
881-
}
882-
// Ensure the current window is active.
883-
watcher = DeviceInformation.CreateWatcher(StorageDevice.GetDeviceSelector());
884-
watcher.Added += DeviceAdded;
885-
watcher.Removed += DeviceRemoved;
886-
watcher.Updated += DeviceUpdated;
887-
watcher.EnumerationCompleted += Watcher_EnumerationCompleted;
888-
watcher.Start();
889-
Window.Current.Activate();
890-
Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
891-
Window.Current.CoreWindow.Dispatcher.AcceleratorKeyActivated += Dispatcher_AcceleratorKeyActivated;
892-
return;
875+
if (eventArgs.Uri.AbsoluteUri == "files-uwp:")
876+
{
877+
rootFrame.Navigate(typeof(InstanceTabsView), null, new SuppressNavigationTransitionInfo());
878+
}
879+
else
880+
{
881+
var trimmedPath = eventArgs.Uri.OriginalString.Split('=')[1];
882+
rootFrame.Navigate(typeof(InstanceTabsView), @trimmedPath, new SuppressNavigationTransitionInfo());
883+
}
884+
// Ensure the current window is active.
885+
watcher = DeviceInformation.CreateWatcher(StorageDevice.GetDeviceSelector());
886+
watcher.Added += DeviceAdded;
887+
watcher.Removed += DeviceRemoved;
888+
watcher.Updated += DeviceUpdated;
889+
watcher.EnumerationCompleted += Watcher_EnumerationCompleted;
890+
watcher.Start();
891+
Window.Current.Activate();
892+
Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
893+
Window.Current.CoreWindow.Dispatcher.AcceleratorKeyActivated += Dispatcher_AcceleratorKeyActivated;
894+
return;
895+
896+
case ActivationKind.CommandLineLaunch:
897+
var cmdLineArgs = args as CommandLineActivatedEventArgs;
898+
var operation = cmdLineArgs.Operation;
899+
var cmdLineString = operation.Arguments;
900+
var activationPath = operation.CurrentDirectoryPath;
901+
902+
var parsedCommands = CommandLineParser.ParseUntrustedCommands(cmdLineString);
903+
904+
if (parsedCommands != null && parsedCommands.Count > 0)
905+
{
906+
foreach (var command in parsedCommands)
907+
{
908+
switch (command.Type)
909+
{
910+
case ParsedCommandType.OpenDirectory:
911+
// TODO Open Directory
912+
913+
rootFrame.Navigate(typeof(InstanceTabsView), command.Payload, new SuppressNavigationTransitionInfo());
914+
915+
// Ensure the current window is active.
916+
watcher = DeviceInformation.CreateWatcher(StorageDevice.GetDeviceSelector());
917+
watcher.Added += DeviceAdded;
918+
watcher.Removed += DeviceRemoved;
919+
watcher.Updated += DeviceUpdated;
920+
watcher.EnumerationCompleted += Watcher_EnumerationCompleted;
921+
watcher.Start();
922+
Task.Delay(5000);
923+
Window.Current.Activate();
924+
Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
925+
Window.Current.CoreWindow.Dispatcher.AcceleratorKeyActivated += Dispatcher_AcceleratorKeyActivated;
926+
927+
928+
return;
929+
case ParsedCommandType.Unkwon:
930+
rootFrame.Navigate(typeof(InstanceTabsView), null, new SuppressNavigationTransitionInfo());
931+
break;
932+
}
933+
}
934+
}
935+
break;
893936
}
894937

895938
rootFrame.Navigate(typeof(InstanceTabsView), null, new SuppressNavigationTransitionInfo());
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace Files.CommandLine
9+
{
10+
class CommandLineParser
11+
{
12+
public static ParsedCommands ParseUntrustedCommands(string cmdLineString)
13+
{
14+
var commands = new ParsedCommands();
15+
16+
var parsedArgs = Parse(cmdLineString);
17+
18+
foreach (var kvp in parsedArgs)
19+
{
20+
Debug.WriteLine("arg {0} = {1}", kvp.Key, kvp.Value);
21+
22+
var command = new ParsedCommand();
23+
24+
switch (kvp.Key)
25+
{
26+
case "-Directory":
27+
command.Type = ParsedCommandType.OpenDirectory;
28+
break;
29+
default:
30+
command.Type = ParsedCommandType.Unkwon;
31+
break;
32+
}
33+
34+
command.Payload = kvp.Value;
35+
commands.Add(command);
36+
}
37+
38+
return commands;
39+
}
40+
41+
public static List<KeyValuePair<string, string>> Parse(string argString = null)
42+
{
43+
var parsedArgs = new List<KeyValuePair<string, string>>();
44+
45+
string[] args = argString.Split(" ");
46+
47+
if (args != null)
48+
{
49+
for (int i = 0; i < args.Length; i++)
50+
{
51+
if (args[i].StartsWith("-") || args[i].StartsWith("/"))
52+
{
53+
var data = ParseData(args, i);
54+
55+
if (data.Key != null)
56+
{
57+
for (int j = 0; j < parsedArgs.Count; j++)
58+
{
59+
if (parsedArgs[j].Key == data.Key)
60+
{
61+
parsedArgs.RemoveAt(j);
62+
}
63+
}
64+
65+
parsedArgs.Add(data);
66+
}
67+
}
68+
}
69+
}
70+
71+
return parsedArgs;
72+
}
73+
74+
private static KeyValuePair<string, string> ParseData(string[] args, int index)
75+
{
76+
string key = null;
77+
string val = null;
78+
if (args[index].StartsWith("-") || args[index].StartsWith("/"))
79+
{
80+
if (args[index].Contains(":"))
81+
{
82+
string argument = args[index];
83+
int endIndex = argument.IndexOf(':');
84+
key = argument.Substring(1, endIndex - 1); // trim the '/' and the ':'.
85+
int valueStart = endIndex + 1;
86+
val = valueStart < argument.Length ? argument.Substring(
87+
valueStart, argument.Length - valueStart) : null;
88+
}
89+
else
90+
{
91+
key = args[index];
92+
int argIndex = 1 + index;
93+
if (argIndex < args.Length && !(args[argIndex].StartsWith("-") || args[argIndex].StartsWith("/")))
94+
{
95+
val = args[argIndex];
96+
}
97+
else
98+
{
99+
val = null;
100+
}
101+
}
102+
}
103+
104+
return key != null ? new KeyValuePair<string, string>(key, val) : default(KeyValuePair<string, string>);
105+
}
106+
}
107+
}

Files/CommandLine/ParsedCommand.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Files.CommandLine
8+
{
9+
class ParsedCommand
10+
{
11+
public ParsedCommandType Type { get; set; }
12+
13+
public string Payload { get; set; }
14+
}
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Files.CommandLine
8+
{
9+
enum ParsedCommandType
10+
{
11+
Unkwon,
12+
OpenDirectory,
13+
}
14+
}

Files/CommandLine/ParsedCommands.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Files.CommandLine
8+
{
9+
class ParsedCommands : List<ParsedCommand>
10+
{
11+
}
12+
}

Files/Files.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@
136136
<DependentUpon>App.xaml</DependentUpon>
137137
</Compile>
138138
<Compile Include="BaseLayout.cs" />
139+
<Compile Include="CommandLine\CommandLineParser.cs" />
140+
<Compile Include="CommandLine\ParsedCommand.cs" />
141+
<Compile Include="CommandLine\ParsedCommands.cs" />
142+
<Compile Include="CommandLine\ParsedCommandType.cs" />
139143
<Compile Include="Controls\FileViewControl.xaml.cs">
140144
<DependentUpon>FileViewControl.xaml</DependentUpon>
141145
</Compile>
@@ -404,6 +408,7 @@
404408
<SubType>Designer</SubType>
405409
</AppxManifest>
406410
</ItemGroup>
411+
<ItemGroup />
407412
<PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' &lt; '14.0' ">
408413
<VisualStudioVersion>14.0</VisualStudioVersion>
409414
</PropertyGroup>

0 commit comments

Comments
 (0)