Skip to content

Commit

Permalink
Fallback, NSI update, grouping messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Advaith3600 committed Sep 27, 2024
1 parent 95b8508 commit 6a630eb
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<AssemblyName>Community.PowerToys.Run.Plugin.CurrencyConverter</AssemblyName>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
<Version>1.2.1</Version>
<Version>1.3.0</Version>
<Platforms>x64;ARM64</Platforms>
<PlatformTarget>$(Platform)</PlatformTarget>
</PropertyGroup>
Expand Down
43 changes: 35 additions & 8 deletions Community.PowerToys.Run.Plugin.CurrencyConverter/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class Main : IPlugin, IContextMenu, ISettingProvider, IDisposable, IDelay
private const int CacheExpirationHours = 3;
private const string AliasFileName = "alias.json";
private const string DefaultAliasResourceName = "Community.PowerToys.Run.Plugin.CurrencyConverter.alias.default.json";
private const string GithubReadmeURL = "https://github.com/Advaith3600/PowerToys-Run-Currency-Converter?tab=readme-ov-file#aliasing";

public IEnumerable<PluginAdditionalOption> AdditionalOptions => new List<PluginAdditionalOption>
{
Expand Down Expand Up @@ -138,9 +139,22 @@ private double GetConversionRateSync(string fromCurrency, string toCurrency)

if (!response.IsSuccessStatusCode)
{
throw response.StatusCode == System.Net.HttpStatusCode.NotFound
? new Exception($"{fromCurrency.ToUpper()} is not a valid currency")
: new Exception("Something went wrong while fetching the conversion rate");
if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
{
throw new Exception($"{fromCurrency.ToUpper()} is not a valid currency");
}
else
{
var fallbackUrl = $"https://latest.currency-api.pages.dev/v1/currencies/{fromCurrency}.min.json";
response = _httpClient.GetAsync(fallbackUrl).Result;

if (!response.IsSuccessStatusCode)
{
throw response.StatusCode == System.Net.HttpStatusCode.NotFound
? new Exception($"{fromCurrency.ToUpper()} is not a valid currency")
: new Exception("Something went wrong while fetching the conversion rate");
}
}
}

var content = response.Content.ReadAsStringAsync().Result;
Expand Down Expand Up @@ -411,7 +425,18 @@ public List<Result> Query(Query query)
{
return string.IsNullOrEmpty(query.ActionKeyword) || string.IsNullOrEmpty(query.Search.Trim())
? new List<Result>()
: new List<Result> { new Result { Title = "Loading...", SubTitle = "Please open an issue if needed.", IcoPath = _iconPath } };
: new List<Result>
{
new Result
{
Title = "Loading...",
SubTitle = "Please open an issue if needed.",
IcoPath = _iconPath,
ContextData = new Dictionary<string, string> { { "externalLink", GithubReadmeURL } },
Action = _ => PerformAction("externalLink", GithubReadmeURL)
}
};

}

public List<Result> Query(Query query, bool isDelayedExecution)
Expand All @@ -432,19 +457,21 @@ public List<Result> Query(Query query, bool isDelayedExecution)
}
catch (Exception ex) when (ex is FileNotFoundException || ex is JsonException)
{
const string link = "https://github.com/Advaith3600/PowerToys-Run-Currency-Converter?tab=readme-ov-file#aliasing";
results.Add(new Result
{
Title = ex.Message,
SubTitle = "Press enter or click to see how to fix this issue",
IcoPath = _warningIconPath,
ContextData = new Dictionary<string, string> { { "externalLink", link } },
Action = _ => PerformAction("externalLink", link)
ContextData = new Dictionary<string, string> { { "externalLink", GithubReadmeURL } },
Action = _ => PerformAction("externalLink", GithubReadmeURL)
});
}
}

return results;
return results
.GroupBy(r => new { r.Title, r.SubTitle })
.Select(g => g.First())
.ToList();
}

private void ValidateJsonFormat(string jsonContent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"ActionKeyword": "$$",
"Name": "Currency Converter",
"Author": "advaith3600",
"Version": "1.2.1",
"Version": "1.3.0",
"Language": "csharp",
"Website": "https://github.com/advaith3600/PowerToys-Run-Currency-Converter",
"ExecuteFileName": "Community.PowerToys.Run.Plugin.CurrencyConverter.dll",
Expand Down
94 changes: 57 additions & 37 deletions exe/CurrencyConverter.nsi
Original file line number Diff line number Diff line change
@@ -1,50 +1,70 @@
!define CC "CurrencyConverter"
!include "MUI2.nsh"

LoadLanguageFile "${NSISDIR}\Contrib\Language files\English.nlf"

;--------------------------------
;Version Information
VIProductVersion "${ver}.0"
VIAddVersionKey /LANG=${LANG_ENGLISH} "ProductName" "${CC} Setup"
VIAddVersionKey /LANG=${LANG_ENGLISH} "CompanyName" "advaith3600"
VIAddVersionKey /LANG=${LANG_ENGLISH} "LegalCopyright" "advaith3600"
VIAddVersionKey /LANG=${LANG_ENGLISH} "FileDescription" "Currency Converter plugin for PowerToys Run"
VIAddVersionKey /LANG=${LANG_ENGLISH} "FileVersion" "${ver}"
VIAddVersionKey /LANG=${LANG_ENGLISH} "ProductVersion" "${ver}"
;--------------------------------

BrandingText "${CC} v${ver}"
CRCCheck force
FileErrorText "Can't write: $\r$\n$\r$\n$0$\r$\n$\r$\nPowerToys is probably still running, please close it and retry."
Icon icon.ico
; General Settings
Name "CurrencyConverter ${ver} ${platform}"
OutFile ".\..\bin\CurrencyConverter-${ver}-${platform}.exe"
InstallDir "$LOCALAPPDATA\Microsoft\PowerToys\PowerToys Run\Plugins\CurrencyConverter"
Name "${CC}"
OutFile ".\..\bin\${CC}-${ver}-${platform}.exe"
RequestExecutionLevel user
SetCompressor /SOLID /FINAL lzma
Icon "icon.ico"
UninstallIcon "icon.ico"
LicenseData "..\LICENSE"
BrandingText "CurrencyConverter ${ver} ${platform}"

;--------------------------------
; Interface Settings
!define MUI_ICON "icon.ico"
!define MUI_UNICON "icon.ico"

;--------------------------------
; Pages
!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_LICENSE "..\LICENSE"
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH

!insertmacro MUI_UNPAGE_WELCOME
!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES
!insertmacro MUI_UNPAGE_FINISH

Page license
Page instfiles
!insertmacro MUI_LANGUAGE "English"

;--------------------------------
; Version Information
VIProductVersion "${ver}.0"
VIAddVersionKey /LANG=${LANG_ENGLISH} "ProductName" "CurrencyConverter Setup"
VIAddVersionKey /LANG=${LANG_ENGLISH} "CompanyName" "advaith3600"
VIAddVersionKey /LANG=${LANG_ENGLISH} "LegalCopyright" "advaith3600"
VIAddVersionKey /LANG=${LANG_ENGLISH} "FileDescription" "Currency Converter plugin for PowerToys Run"
VIAddVersionKey /LANG=${LANG_ENGLISH} "FileVersion" "${ver}"
VIAddVersionKey /LANG=${LANG_ENGLISH} "ProductVersion" "${ver}"

;--------------------------------
; Installer Sections
Section ""
ClearErrors
; Check if already installed
ReadRegStr $0 HKCU "Software\advaith3600\CurrencyConverter" "InstallPath"
IfFileExists "$0\uninstall.exe" 0 +7
MessageBox MB_YESNO "CurrencyConverter is already installed. Do you want to uninstall it first?" IDYES uninst IDNO abort
abort:
Abort
uninst:
; Execute the uninstaller and wait for it to complete
ExecWait '"$0\uninstall.exe"'
; Check if the uninstaller process has finished
Sleep 100
IfFileExists "$0\uninstall.exe" loop
loop:
Sleep 100
IfFileExists "$0\uninstall.exe" loop

SetOutPath $INSTDIR
GetFullPathName $0 "$EXEDIR\"
GetFullPathName $0 $0
File /r "${direct}\*"

WriteUninstaller "$INSTDIR\uninstall.exe"

IfErrors 0 +5
SetErrorlevel 1
IfSilent +2
MessageBox MB_ICONEXCLAMATION "Unable to install, PowerToys is probably still running, please close it manually before install."
Abort
SectionEnd

;--------------------------------
Expand All @@ -54,19 +74,19 @@ Section "Add Registry Entries"
WriteRegStr HKCU "Software\advaith3600\CurrencyConverter" "InstallPath" "$INSTDIR"

; Register for uninstallation
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${CC}" "DisplayName" "${CC}"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${CC}" "UninstallString" "$INSTDIR\uninstall.exe"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${CC}" "InstallLocation" "$INSTDIR"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${CC}" "DisplayVersion" "${ver}"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${CC}" "Publisher" "advaith3600"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\CurrencyConverter" "DisplayName" "CurrencyConverter"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\CurrencyConverter" "UninstallString" "$INSTDIR\uninstall.exe"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\CurrencyConverter" "InstallLocation" "$INSTDIR"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\CurrencyConverter" "DisplayVersion" "${ver}"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\CurrencyConverter" "Publisher" "advaith3600"
SectionEnd

;--------------------------------
; Uninstaller
Section "Uninstall"
Delete "$INSTDIR\*.*"
RMDir "$INSTDIR"
DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${CC}"
DeleteRegKey HKCU "Software\advaith3600\CurrencyConverter"
DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\CurrencyConverter"
SectionEnd

UninstallIcon icon.ico

0 comments on commit 6a630eb

Please sign in to comment.