|
| 1 | +' WinNUT-Client is a NUT windows client for monitoring your ups hooked up to your favorite linux server. |
| 2 | +' Copyright (C) 2019-2021 Gawindx (Decaux Nicolas) |
| 3 | +' |
| 4 | +' This program is free software: you can redistribute it and/or modify it under the terms of the |
| 5 | +' GNU General Public License as published by the Free Software Foundation, either version 3 of the |
| 6 | +' License, or any later version. |
| 7 | +' |
| 8 | +' This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY |
| 9 | + |
| 10 | +Imports System.ComponentModel |
| 11 | +Imports System.Threading |
| 12 | +Imports Octokit |
| 13 | + |
| 14 | +Namespace Updater |
| 15 | + |
| 16 | + Public Class GitHubUpdateProvider |
| 17 | + Implements IUpdateProvider |
| 18 | + |
| 19 | + Private Const REPOSITORY_OWNER = "nutdotnet" |
| 20 | + Private Const REPOSITORY_NAME = "WinNUT-Client" |
| 21 | + |
| 22 | + Private onCompletedDelegate As SendOrPostCallback |
| 23 | + Private Delegate Sub CheckForUpdateDelegate() |
| 24 | + ' Only one instance of a check or install procedure at a time. |
| 25 | + Private asyncOpInstance As AsyncOperation |
| 26 | + Private disposedValue As Boolean |
| 27 | + Private Shared _gitHubClient As GitHubClient |
| 28 | + |
| 29 | + Public Event OnCheckForUpdateCompleted(sender As IUpdateProvider, eventArgs As CheckForUpdateCompletedEventArgs) _ |
| 30 | + Implements IUpdateProvider.OnCheckForUpdateCompleted |
| 31 | + |
| 32 | + Public Event Disposed As EventHandler Implements IComponent.Disposed |
| 33 | + |
| 34 | + Protected Overridable Sub InitializeDelegates() |
| 35 | + onCompletedDelegate = New SendOrPostCallback(AddressOf CheckForUpdateAsyncCompleted) |
| 36 | + End Sub |
| 37 | + |
| 38 | + Public Function CheckForUpdate() As Release Implements IUpdateProvider.CheckForUpdate |
| 39 | + Dim checkTask = _gitHubClient.Repository.Release.GetLatest(REPOSITORY_OWNER, REPOSITORY_NAME) |
| 40 | + |
| 41 | + checkTask.RunSynchronously() |
| 42 | + Return checkTask.Result |
| 43 | + End Function |
| 44 | + |
| 45 | + Public Sub CheckForUpdateAsync() Implements IUpdateProvider.CheckForUpdateAsync |
| 46 | + If asyncOpInstance IsNot Nothing Then |
| 47 | + Throw New InvalidOperationException("Instance of AsyncOperation already exists.") |
| 48 | + End If |
| 49 | + |
| 50 | + asyncOpInstance = AsyncOperationManager.CreateOperation(Nothing) |
| 51 | + Dim checkDelegate As New CheckForUpdateDelegate(AddressOf CheckForUpdateWorker) |
| 52 | + checkDelegate.BeginInvoke(Nothing, Nothing) |
| 53 | + End Sub |
| 54 | + |
| 55 | + Private Sub CheckForUpdateWorker() Implements IUpdateProvider.CheckForUpdateWorker |
| 56 | + Dim checkCompleteEvent = New CheckForUpdateCompletedEventArgs(CheckForUpdate()) |
| 57 | + CheckForUpdateAsyncCompleted(checkCompleteEvent) |
| 58 | + End Sub |
| 59 | + |
| 60 | + ' Doesn't really do anything. May need to implement real cancellation logic. |
| 61 | + Public Sub CheckForUpdateAsyncCancel() Implements IUpdateProvider.CheckForUpdateAsyncCancel |
| 62 | + If asyncOpInstance IsNot Nothing Then |
| 63 | + asyncOpInstance = Nothing |
| 64 | + End If |
| 65 | + End Sub |
| 66 | + |
| 67 | + Protected Sub CheckForUpdateAsyncCompleted(eventArguments As CheckForUpdateCompletedEventArgs) _ |
| 68 | + Implements IUpdateProvider.CheckForUpdateAsyncCompleted |
| 69 | + |
| 70 | + RaiseEvent OnCheckForUpdateCompleted(Me, eventArguments) |
| 71 | + End Sub |
| 72 | + |
| 73 | + Public Function UpdateWorker() As Boolean Implements IUpdateProvider.UpdateWorker |
| 74 | + Throw New NotImplementedException() |
| 75 | + End Function |
| 76 | + |
| 77 | + Public Function Update() As Boolean Implements IUpdateProvider.Update |
| 78 | + Throw New NotImplementedException() |
| 79 | + End Function |
| 80 | + |
| 81 | + Public Sub UpdateAsync() Implements IUpdateProvider.UpdateAsync |
| 82 | + Throw New NotImplementedException() |
| 83 | + End Sub |
| 84 | + |
| 85 | + Public Sub UpdateAsyncCancel() Implements IUpdateProvider.UpdateAsyncCancel |
| 86 | + Throw New NotImplementedException() |
| 87 | + End Sub |
| 88 | + |
| 89 | + Sub New() |
| 90 | + InitializeDelegates() |
| 91 | + |
| 92 | + _gitHubClient = New GitHubClient(New ProductHeaderValue(REPOSITORY_NAME)) |
| 93 | + End Sub |
| 94 | + |
| 95 | + Public Property Site As ISite Implements IComponent.Site |
| 96 | + Get |
| 97 | + Throw New NotImplementedException() |
| 98 | + End Get |
| 99 | + Set(value As ISite) |
| 100 | + Throw New NotImplementedException() |
| 101 | + End Set |
| 102 | + End Property |
| 103 | + |
| 104 | + Protected Overridable Sub Dispose(disposing As Boolean) |
| 105 | + If Not disposedValue Then |
| 106 | + If disposing Then |
| 107 | + ' TODO: dispose managed state (managed objects) |
| 108 | + End If |
| 109 | + |
| 110 | + ' TODO: free unmanaged resources (unmanaged objects) and override finalizer |
| 111 | + ' TODO: set large fields to null |
| 112 | + disposedValue = True |
| 113 | + End If |
| 114 | + End Sub |
| 115 | + |
| 116 | + ' ' TODO: override finalizer only if 'Dispose(disposing As Boolean)' has code to free unmanaged resources |
| 117 | + ' Protected Overrides Sub Finalize() |
| 118 | + ' ' Do not change this code. Put cleanup code in 'Dispose(disposing As Boolean)' method |
| 119 | + ' Dispose(disposing:=False) |
| 120 | + ' MyBase.Finalize() |
| 121 | + ' End Sub |
| 122 | + |
| 123 | + Public Sub Dispose() Implements IDisposable.Dispose |
| 124 | + ' Do not change this code. Put cleanup code in 'Dispose(disposing As Boolean)' method |
| 125 | + Dispose(disposing:=True) |
| 126 | + GC.SuppressFinalize(Me) |
| 127 | + End Sub |
| 128 | + End Class |
| 129 | + |
| 130 | +End Namespace |
0 commit comments