/ src / common / utils / modulesRegistry.h
modulesRegistry.h
  1  #pragma once
  2  
  3  #include "registry.h"
  4  
  5  #include <common/utils/json.h>
  6  
  7  #include <filesystem>
  8  
  9  namespace fs = std::filesystem;
 10  
 11  namespace NonLocalizable
 12  {
 13      const static wchar_t* MONACO_LANGUAGES_FILE_NAME = L"Assets\\Monaco\\monaco_languages.json";
 14      const static wchar_t* ListID = L"list";
 15      const static wchar_t* ExtensionsID = L"extensions";
 16      const static std::vector<std::wstring> ExtSVG      = { L".svg" };
 17      const static std::vector<std::wstring> ExtMarkdown = { L".md", L".markdown", L".mdown", L".mkdn", L".mkd", L".mdwn", L".mdtxt", L".mdtext" };
 18      const static std::vector<std::wstring> ExtPDF      = { L".pdf" };
 19      const static std::vector<std::wstring> ExtGCode    = { L".gcode" };
 20      const static std::vector<std::wstring> ExtBGCode   = { L".bgcode" };
 21      const static std::vector<std::wstring> ExtSTL      = { L".stl" };
 22      const static std::vector<std::wstring> ExtQOI      = { L".qoi" };
 23      const static std::vector<std::wstring> ExtNoNoNo   = {
 24          L".svgz" //Monaco cannot handle this file type at all; it's a binary file.
 25      };
 26  }
 27  
 28  inline registry::ChangeSet getSvgPreviewHandlerChangeSet(const std::wstring installationDir, const bool perUser)
 29  {
 30      using namespace registry::shellex;
 31      return generatePreviewHandler(PreviewHandlerType::preview,
 32                                    perUser,
 33                                    L"{FCDD4EED-41AA-492F-8A84-31A1546226E0}",
 34                                    get_std_product_version(),
 35                                    (fs::path{ installationDir } /
 36                                     LR"d(PowerToys.SvgPreviewHandlerCpp.dll)d")
 37                                        .wstring(),
 38                                    L"SvgPreviewHandler",
 39                                    L"Svg Preview Handler",
 40                                    NonLocalizable::ExtSVG);
 41  }
 42  
 43  inline registry::ChangeSet getMdPreviewHandlerChangeSet(const std::wstring installationDir, const bool perUser)
 44  {
 45      using namespace registry::shellex;
 46      return generatePreviewHandler(PreviewHandlerType::preview,
 47                                    perUser,
 48                                    L"{60789D87-9C3C-44AF-B18C-3DE2C2820ED3}",
 49                                    get_std_product_version(),
 50                                    (fs::path{ installationDir } / LR"d(PowerToys.MarkdownPreviewHandlerCpp.dll)d").wstring(),
 51                                    L"MarkdownPreviewHandler",
 52                                    L"Markdown Preview Handler",
 53                                    NonLocalizable::ExtMarkdown);
 54  }
 55  
 56  inline registry::ChangeSet getMonacoPreviewHandlerChangeSet(const std::wstring installationDir, const bool perUser)
 57  {
 58      using namespace registry::shellex;
 59  
 60      // Set up a list of extensions for the preview handler to take over
 61      std::vector<std::wstring> extensions;
 62  
 63      // Set up a list of extensions that Monaco support but the preview handler shouldn't take over
 64      std::vector<std::wstring> ExtExclusions;
 65      ExtExclusions.insert(ExtExclusions.end(), NonLocalizable::ExtMarkdown.begin(), NonLocalizable::ExtMarkdown.end());
 66      ExtExclusions.insert(ExtExclusions.end(), NonLocalizable::ExtSVG.begin(), NonLocalizable::ExtSVG.end());
 67      ExtExclusions.insert(ExtExclusions.end(), NonLocalizable::ExtNoNoNo.begin(), NonLocalizable::ExtNoNoNo.end());
 68      bool IsExcluded = false;
 69  
 70      std::wstring languagesFilePath = fs::path{ installationDir } / NonLocalizable::MONACO_LANGUAGES_FILE_NAME;
 71      auto json = json::from_file(languagesFilePath);
 72  
 73      if (json)
 74      {
 75          try
 76          {
 77              auto list = json->GetNamedArray(NonLocalizable::ListID);
 78              for (uint32_t i = 0; i < list.Size(); ++i)
 79              {
 80                  auto entry = list.GetObjectAt(i);
 81                  if (entry.HasKey(NonLocalizable::ExtensionsID))
 82                  {
 83                      auto extensionsList = entry.GetNamedArray(NonLocalizable::ExtensionsID);
 84  
 85                      for (uint32_t j = 0; j < extensionsList.Size(); ++j)
 86                      {
 87                          auto extension = extensionsList.GetStringAt(j);
 88  
 89                          // Ignore extensions in the exclusion list
 90                          IsExcluded = false;
 91  
 92                          for (std::wstring k : ExtExclusions)
 93                          {
 94                              if (std::wstring{ extension } == k)
 95                              {
 96                                  IsExcluded = true;
 97                                  break;
 98                              }
 99                          }
100                          if (IsExcluded)
101                          {
102                              continue;
103                          }
104                          extensions.push_back(std::wstring{ extension });
105                      }
106                  }
107              }
108          }
109          catch (...)
110          {
111          }
112      }
113  
114      return generatePreviewHandler(PreviewHandlerType::preview,
115                                    perUser,
116                                    L"{D8034CFA-F34B-41FE-AD45-62FCBB52A6DA}",
117                                    get_std_product_version(),
118                                    (fs::path{ installationDir } / LR"d(PowerToys.MonacoPreviewHandlerCpp.dll)d").wstring(),
119                                    L"MonacoPreviewHandler",
120                                    L"Monaco Preview Handler",
121                                    extensions);
122  }
123  
124  inline registry::ChangeSet getPdfPreviewHandlerChangeSet(const std::wstring installationDir, const bool perUser)
125  {
126      using namespace registry::shellex;
127      return generatePreviewHandler(PreviewHandlerType::preview,
128                                    perUser,
129                                    L"{A5A41CC7-02CB-41D4-8C9B-9087040D6098}",
130                                    get_std_product_version(),
131                                    (fs::path{ installationDir } / LR"d(PowerToys.PdfPreviewHandlerCpp.dll)d").wstring(),
132                                    L"PdfPreviewHandler",
133                                    L"Pdf Preview Handler",
134                                    NonLocalizable::ExtPDF);
135  }
136  
137  inline registry::ChangeSet getGcodePreviewHandlerChangeSet(const std::wstring installationDir, const bool perUser)
138  {
139      using namespace registry::shellex;
140      return generatePreviewHandler(PreviewHandlerType::preview,
141                                    perUser,
142                                    L"{A0257634-8812-4CE8-AF11-FA69ACAEAFAE}",
143                                    get_std_product_version(),
144                                    (fs::path{ installationDir } / LR"d(PowerToys.GcodePreviewHandlerCpp.dll)d").wstring(),
145                                    L"GcodePreviewHandler",
146                                    L"G-code Preview Handler",
147                                    NonLocalizable::ExtGCode);
148  }
149  
150  inline registry::ChangeSet getBgcodePreviewHandlerChangeSet(const std::wstring installationDir, const bool perUser)
151  {
152      using namespace registry::shellex;
153      return generatePreviewHandler(PreviewHandlerType::preview,
154                                    perUser,
155                                    L"{0e6d5bdd-d5f8-4692-a089-8bb88cdd37f4}",
156                                    get_std_product_version(),
157                                    (fs::path{ installationDir } / LR"d(PowerToys.BgcodePreviewHandlerCpp.dll)d").wstring(),
158                                    L"BgcodePreviewHandler",
159                                    L"Binary G-code Preview Handler",
160                                    NonLocalizable::ExtBGCode);
161  }
162  
163  inline registry::ChangeSet getQoiPreviewHandlerChangeSet(const std::wstring installationDir, const bool perUser)
164  {
165      using namespace registry::shellex;
166      return generatePreviewHandler(PreviewHandlerType::preview,
167                                    perUser,
168                                    L"{729B72CD-B72E-4FE9-BCBF-E954B33FE699}",
169                                    get_std_product_version(),
170                                    (fs::path{ installationDir } / LR"d(PowerToys.QoiPreviewHandlerCpp.dll)d").wstring(),
171                                    L"QoiPreviewHandler",
172                                    L"Qoi Preview Handler",
173                                    NonLocalizable::ExtQOI);
174  }
175  
176  inline registry::ChangeSet getSvgThumbnailHandlerChangeSet(const std::wstring installationDir, const bool perUser)
177  {
178      using namespace registry::shellex;
179      return generatePreviewHandler(PreviewHandlerType::thumbnail,
180                                    perUser,
181                                    L"{10144713-1526-46C9-88DA-1FB52807A9FF}",
182                                    get_std_product_version(),
183                                    (fs::path{ installationDir } / LR"d(PowerToys.SvgThumbnailProviderCpp.dll)d").wstring(),
184                                    L"SvgThumbnailProvider",
185                                    L"Svg Thumbnail Provider",
186                                    NonLocalizable::ExtSVG,
187                                    L"image",
188                                    L"Picture");
189  }
190  
191  inline registry::ChangeSet getPdfThumbnailHandlerChangeSet(const std::wstring installationDir, const bool perUser)
192  {
193      using namespace registry::shellex;
194      return generatePreviewHandler(PreviewHandlerType::thumbnail,
195                                    perUser,
196                                    L"{D8BB9942-93BD-412D-87E4-33FAB214DC1A}",
197                                    get_std_product_version(),
198                                    (fs::path{ installationDir } / LR"d(PowerToys.PdfThumbnailProviderCpp.dll)d").wstring(),
199                                    L"PdfThumbnailProvider",
200                                    L"Pdf Thumbnail Provider",
201                                    NonLocalizable::ExtPDF);
202  }
203  
204  inline registry::ChangeSet getGcodeThumbnailHandlerChangeSet(const std::wstring installationDir, const bool perUser)
205  {
206      using namespace registry::shellex;
207      return generatePreviewHandler(PreviewHandlerType::thumbnail,
208                                    perUser,
209                                    L"{F2847CBE-CD03-4C83-A359-1A8052C1B9D5}",
210                                    get_std_product_version(),
211                                    (fs::path{ installationDir } / LR"d(PowerToys.GcodeThumbnailProviderCpp.dll)d").wstring(),
212                                    L"GcodeThumbnailProvider",
213                                    L"G-code Thumbnail Provider",
214                                    NonLocalizable::ExtGCode);
215  }
216  
217  inline registry::ChangeSet getBgcodeThumbnailHandlerChangeSet(const std::wstring installationDir, const bool perUser)
218  {
219      using namespace registry::shellex;
220      return generatePreviewHandler(PreviewHandlerType::thumbnail,
221                                    perUser,
222                                    L"{5c93a1e4-99d0-4fb3-991c-6c296a27be21}",
223                                    get_std_product_version(),
224                                    (fs::path{ installationDir } / LR"d(PowerToys.BgcodeThumbnailProviderCpp.dll)d").wstring(),
225                                    L"BgcodeThumbnailProvider",
226                                    L"Binary G-code Thumbnail Provider",
227                                    NonLocalizable::ExtBGCode);
228  }
229  
230  inline registry::ChangeSet getStlThumbnailHandlerChangeSet(const std::wstring installationDir, const bool perUser)
231  {
232      using namespace registry::shellex;
233      return generatePreviewHandler(PreviewHandlerType::thumbnail,
234                                    perUser,
235                                    L"{77257004-6F25-4521-B602-50ECC6EC62A6}",
236                                    get_std_product_version(),
237                                    (fs::path{ installationDir } / LR"d(PowerToys.StlThumbnailProviderCpp.dll)d").wstring(),
238                                    L"StlThumbnailProvider",
239                                    L"Stl Thumbnail Provider",
240                                    NonLocalizable::ExtSTL);
241  }
242  
243  inline registry::ChangeSet getQoiThumbnailHandlerChangeSet(const std::wstring installationDir, const bool perUser)
244  {
245      using namespace registry::shellex;
246      return generatePreviewHandler(PreviewHandlerType::thumbnail,
247                                    perUser,
248                                    L"{AD856B15-D25E-4008-AFB7-AFAA55586188}",
249                                    get_std_product_version(),
250                                    (fs::path{ installationDir } / LR"d(PowerToys.QoiThumbnailProviderCpp.dll)d").wstring(),
251                                    L"QoiThumbnailProvider",
252                                    L"Qoi Thumbnail Provider",
253                                    NonLocalizable::ExtQOI,
254                                    L"image",
255                                    L"Picture");
256  }
257  
258  inline registry::ChangeSet getRegistryPreviewSetDefaultAppChangeSet(const std::wstring installationDir, const bool perUser)
259  {
260      const HKEY scope = perUser ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE;
261  
262      using vec_t = std::vector<registry::ValueChange>;
263      vec_t changes;
264  
265      std::wstring appName = L"Registry Preview";
266      std::wstring fullAppName = L"PowerToys.RegistryPreview";
267      std::wstring registryKeyPrefix = L"Software\\Classes\\";
268  
269      std::wstring appPath = installationDir + L"\\WinUI3Apps\\PowerToys.RegistryPreview.exe";
270      std::wstring command = appPath + L" \"----ms-protocol:ms-encodedlaunch:App?ContractId=Windows.File&Verb=open&File=%1\"";
271  
272      changes.push_back({ scope, registryKeyPrefix + fullAppName + L"\\" + L"Application", L"ApplicationName", appName });
273      changes.push_back({ scope, registryKeyPrefix + fullAppName + L"\\" + L"DefaultIcon", std::nullopt, appPath });
274      changes.push_back({ scope, registryKeyPrefix + fullAppName + L"\\" + L"shell\\open\\command", std::nullopt, command });
275      changes.push_back({ scope, registryKeyPrefix + L".reg\\OpenWithProgIDs", fullAppName, L"" });
276  
277      return { changes };
278  }
279  
280  inline registry::ChangeSet getRegistryPreviewChangeSet(const std::wstring installationDir,const bool perUser)
281  {
282      const HKEY scope = perUser ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE;
283  
284      using vec_t = std::vector<registry::ValueChange>;
285      vec_t changes;
286  
287      std::wstring command = installationDir;
288      command.append(L"\\WinUI3Apps\\PowerToys.RegistryPreview.exe \"%1\"");
289      changes.push_back({ scope, L"Software\\Classes\\regfile\\shell\\preview\\command", std::nullopt, command });
290  
291      std::wstring icon_path = installationDir;
292      icon_path.append(L"\\WinUI3Apps\\Assets\\RegistryPreview\\RegistryPreview.ico");
293      changes.push_back({ scope, L"Software\\Classes\\regfile\\shell\\preview", L"icon", icon_path });
294  
295      return { changes };
296  }
297  
298  inline std::vector<registry::ChangeSet> getAllOnByDefaultModulesChangeSets(const std::wstring installationDir)
299  {
300      constexpr bool PER_USER = true;
301      return { getSvgPreviewHandlerChangeSet(installationDir, PER_USER),
302               getMdPreviewHandlerChangeSet(installationDir, PER_USER),
303               getMonacoPreviewHandlerChangeSet(installationDir, PER_USER),
304               getGcodePreviewHandlerChangeSet(installationDir, PER_USER),
305               getBgcodePreviewHandlerChangeSet(installationDir, PER_USER),
306               getQoiPreviewHandlerChangeSet(installationDir, PER_USER),
307               getSvgThumbnailHandlerChangeSet(installationDir, PER_USER),
308               getGcodeThumbnailHandlerChangeSet(installationDir, PER_USER),
309               getBgcodeThumbnailHandlerChangeSet(installationDir, PER_USER),
310               getStlThumbnailHandlerChangeSet(installationDir, PER_USER),
311               getQoiThumbnailHandlerChangeSet(installationDir, PER_USER),
312               getRegistryPreviewChangeSet(installationDir, PER_USER) };
313  }
314  
315  inline std::vector<registry::ChangeSet> getAllModulesChangeSets(const std::wstring installationDir)
316  {
317      constexpr bool PER_USER = true;
318      return { getSvgPreviewHandlerChangeSet(installationDir, PER_USER),
319               getMdPreviewHandlerChangeSet(installationDir, PER_USER),
320               getMonacoPreviewHandlerChangeSet(installationDir, PER_USER),
321               getPdfPreviewHandlerChangeSet(installationDir, PER_USER),
322               getGcodePreviewHandlerChangeSet(installationDir, PER_USER),
323               getBgcodePreviewHandlerChangeSet(installationDir, PER_USER),
324               getQoiPreviewHandlerChangeSet(installationDir, PER_USER),
325               getSvgThumbnailHandlerChangeSet(installationDir, PER_USER),
326               getPdfThumbnailHandlerChangeSet(installationDir, PER_USER),
327               getGcodeThumbnailHandlerChangeSet(installationDir, PER_USER),
328               getBgcodeThumbnailHandlerChangeSet(installationDir, PER_USER),
329               getStlThumbnailHandlerChangeSet(installationDir, PER_USER),
330               getQoiThumbnailHandlerChangeSet(installationDir, PER_USER),
331               getRegistryPreviewChangeSet(installationDir, PER_USER),
332               getRegistryPreviewSetDefaultAppChangeSet(installationDir, PER_USER) };
333  }