master
cpp 89 lines 2.52 KB
Raw
1 /*++
2
3 Copyright (c) Microsoft. All rights reserved.
4
5 Module Name:
6
7 SettingsCommand.cpp
8
9 Abstract:
10
11 Implementation of SettingsCommand command tree.
12
13 --*/
14 #include "Argument.h"
15 #include "SettingsCommand.h"
16 #include "WSLCUserSettings.h"
17 #include "wslutil.h"
18
19 using namespace wsl::windows::common::wslutil;
20 using namespace wsl::windows::wslc::execution;
21 using namespace wsl::shared;
22
23 namespace wsl::windows::wslc {
24
25 // SettingsCommand
26 std::vector<std::unique_ptr<Command>> SettingsCommand::GetCommands() const
27 {
28 std::vector<std::unique_ptr<Command>> commands;
29 commands.push_back(std::make_unique<SettingsResetCommand>(FullName()));
30 return commands;
31 }
32
33 std::vector<Argument> SettingsCommand::GetArguments() const
34 {
35 return {};
36 }
37
38 std::wstring SettingsCommand::ShortDescription() const
39 {
40 return Localization::WSLCCLI_SettingsCommandDesc();
41 }
42
43 std::wstring SettingsCommand::LongDescription() const
44 {
45 return Localization::WSLCCLI_SettingsCommandLongDesc();
46 }
47
48 void SettingsCommand::ExecuteInternal(CLIExecutionContext& context) const
49 {
50 const auto& userSettings = settings::User();
51 userSettings.PrepareToShellExecuteFile();
52 const auto path = userSettings.SettingsFilePath();
53
54 // Some versions of windows will fail if no file extension association exists, other will pop up the dialog
55 // to make the user pick their default.
56 HINSTANCE res = ShellExecuteW(nullptr, nullptr, path.c_str(), nullptr, nullptr, SW_SHOW);
57 if (static_cast<int>(reinterpret_cast<uintptr_t>(res)) <= 32)
58 {
59 // User doesn't have file type association. Default to notepad
60 // Quote the path so that Notepad treats it as a single argument even if it contains spaces.
61 std::filesystem::path notepadPath = std::filesystem::path{wil::GetSystemDirectoryW().get()} / L"notepad.exe";
62 std::wstring quotedPath = L"\"" + path.wstring() + L"\"";
63 ShellExecuteW(nullptr, nullptr, notepadPath.c_str(), quotedPath.c_str(), nullptr, SW_SHOW);
64 }
65 }
66
67 // SettingsResetCommand
68 std::vector<Argument> SettingsResetCommand::GetArguments() const
69 {
70 return {};
71 }
72
73 std::wstring SettingsResetCommand::ShortDescription() const
74 {
75 return Localization::WSLCCLI_SettingsResetDesc();
76 }
77
78 std::wstring SettingsResetCommand::LongDescription() const
79 {
80 return Localization::WSLCCLI_SettingsResetLongDesc();
81 }
82
83 void SettingsResetCommand::ExecuteInternal(CLIExecutionContext& context) const
84 {
85 settings::User().Reset();
86 context.Terminal.Output(L"{}\n", Localization::WSLCCLI_SettingsResetConfirm());
87 }
88
89 } // namespace wsl::windows::wslc