Skip to content

Commit

Permalink
Assembly updates
Browse files Browse the repository at this point in the history
  • Loading branch information
antikmozib committed May 6, 2021
1 parent 9a00e98 commit 6fc78a7
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 82 deletions.
15 changes: 7 additions & 8 deletions AM Downloader/AM Downloader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@
<ApplicationIcon>Icons\app.ico</ApplicationIcon>
<Company>Antik Mozib</Company>
<Description>A modern download manager for Windows.</Description>
<Copyright>Copyright © Antik Mozib.</Copyright>
<Copyright>Copyright © 2021 Antik Mozib. All Rights Reserved.</Copyright>
<StartupObject />
<PackageIcon>app.ico</PackageIcon>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<AssemblyVersion>1.3.0.0</AssemblyVersion>
<Version>1.3</Version>
<AssemblyVersion>1.3.0.0</AssemblyVersion>
<Platforms>AnyCPU;x86;x64</Platforms>
<Authors>Antik Mozib</Authors>
<FileVersion>1.3.0.0</FileVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
Expand Down Expand Up @@ -72,9 +70,11 @@
<ItemGroup>
<Page Remove="Converters.xaml" />
<Page Remove="Icons\AddRow_16x.xaml" />
<Page Remove="Icons\ASPWebSite_16x.xaml" />
<Page Remove="Icons\Cancel_16x.xaml" />
<Page Remove="Icons\Checklist_16x.xaml" />
<Page Remove="Icons\CopyLongTextToClipboard_16x.xaml" />
<Page Remove="Icons\GetWebSite_16x.xaml" />
<Page Remove="Icons\OpenFolder_16x.xaml" />
<Page Remove="Icons\Open_16x.xaml" />
<Page Remove="Icons\Pause_16x.xaml" />
Expand All @@ -84,7 +84,6 @@
<Page Remove="Icons\Run_16x.xaml" />
<Page Remove="Icons\Settings_16x.xaml" />
<Page Remove="Icons\ShutDown_16x.xaml" />
<Page Remove="Icons\StatusInformation_16x.xaml" />
<Page Remove="Icons\StatusNotStarted_16x.xaml" />
<Page Remove="Icons\StatusNo_16x.xaml" />
<Page Remove="Icons\StatusOK_16x.xaml" />
Expand Down Expand Up @@ -124,6 +123,9 @@
<Resource Include="Icons\CopyLongTextToClipboard_16x.xaml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Resource>
<Resource Include="Icons\GetWebSite_16x.xaml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Resource>
<Resource Include="Icons\OpenFolder_16x.xaml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Resource>
Expand Down Expand Up @@ -151,9 +153,6 @@
<Resource Include="Icons\ShutDown_16x.xaml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Resource>
<Resource Include="Icons\StatusInformation_16x.xaml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Resource>
<Resource Include="Icons\StatusNotStarted_16x.xaml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Resource>
Expand Down
42 changes: 25 additions & 17 deletions AM Downloader/Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ internal static class AppConstants

internal static class AppPaths
{
public static string LocalAppData => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), Assembly.GetExecutingAssembly().GetName().Name);
public static string LocalAppData =>
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), Assembly.GetExecutingAssembly().GetName().Name);
public static string DownloadsHistoryFile => Path.Combine(LocalAppData, "History.xml");
public static string SavedLocationsFile => Path.Combine(LocalAppData, "SavedLocations.xml");
public static string DownloadsFolder => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads");
Expand All @@ -47,7 +48,8 @@ public static string GetFreshFilename(string path)

while (File.Exists(result) || File.Exists(result + AppConstants.DownloaderSplitedPartExtension))
{
result = dirName + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(fileName) + " (" + ++i + ")" + Path.GetExtension(fileName);
result = dirName + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(fileName) +
" (" + ++i + ")" + Path.GetExtension(fileName);
};

return result;
Expand Down Expand Up @@ -91,27 +93,33 @@ public static string DriveLetterToName(string rootPath)
return string.Empty;
}
}
internal static class CheckForUpdates

internal static class AppUpdateService
{
public static async Task<string> GetUpdateUrl(HttpClient client, string url, string appName, string appVersion)
public static async Task<string> GetUpdateUrl(string url, string appName, string appVersion)
{
client.BaseAddress = new Uri(url);

var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string,string>("appname",appName),
new KeyValuePair<string, string>("version",appVersion)
});

var response = await client.PostAsync("", content);

if (!response.IsSuccessStatusCode)
new KeyValuePair<string,string>("appname",appName),
new KeyValuePair<string, string>("version",appVersion)
});
using (var c = new HttpClient())
{
return string.Empty;
try
{
c.BaseAddress = new Uri(url);
var response = await c.PostAsync("", content);
if (response.IsSuccessStatusCode)
{
return await response.Content.ReadAsStringAsync();
}
return string.Empty;
}
catch (Exception)
{
return string.Empty;
}
}

string result = await response.Content.ReadAsStringAsync();
return result;
}
}
}
9 changes: 8 additions & 1 deletion AM Downloader/DownloaderObjectModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -462,11 +462,17 @@ private async Task<DownloadStatus> ProcessStreamsAsync(long bytesDownloadedPrevi
IProgress<int> streamProgress = new Progress<int>(async (value) =>
{
await semaphoreProgress.WaitAsync();
this.BytesDownloadedThisSession += value;
this.TotalBytesCompleted = this.BytesDownloadedThisSession + bytesDownloadedPreviously;
if (!this.SupportsResume) this.TotalBytesToDownload = this.TotalBytesCompleted;
_reportBytesProgress.Report(value);
if (this.SupportsResume && (this.TotalBytesCompleted >= nextProgressReportAt || this.TotalBytesCompleted > this.TotalBytesToDownload - progressReportingFrequency))
if (this.SupportsResume &&
(this.TotalBytesCompleted >= nextProgressReportAt ||
this.TotalBytesCompleted > this.TotalBytesToDownload - progressReportingFrequency))
{
double progress = (double)this.TotalBytesCompleted / (double)this.TotalBytesToDownload * 100;
this.Progress = (int)progress;
Expand All @@ -479,6 +485,7 @@ private async Task<DownloadStatus> ProcessStreamsAsync(long bytesDownloadedPrevi
RaisePropertyChanged(nameof(this.TotalBytesCompleted));
RaisePropertyChanged(nameof(this.TotalBytesToDownload));
}
semaphoreProgress.Release();
});

Expand Down
99 changes: 74 additions & 25 deletions AM Downloader/DownloaderViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ namespace AMDownloader

internal delegate void ShowPreviewDelegate(string preview);

internal delegate MessageBoxResult DisplayMessageDelegate(string message, string title = "", MessageBoxButton button = MessageBoxButton.OK, MessageBoxImage image = MessageBoxImage.Information, MessageBoxResult defaultResult = MessageBoxResult.OK);
internal delegate MessageBoxResult DisplayMessageDelegate(
string message, string title = "",
MessageBoxButton button = MessageBoxButton.OK,
MessageBoxImage image = MessageBoxImage.Information,
MessageBoxResult defaultResult = MessageBoxResult.OK);

internal delegate void ShowUrlsDelegate(List<string> urls, string caption, string infoLabel);

Expand Down Expand Up @@ -176,8 +180,9 @@ public DownloaderViewModel(DisplayMessageDelegate displayMessage, ShowUrlsDelega
RedownloadCommand = new RelayCommand<object>(Redownload);
CopyLinkToClipboardCommand = new RelayCommand<object>(CopyLinkToClipboard);
ClearFinishedDownloadsCommand = new RelayCommand<object>(ClearFinishedDownloads);
CancelBackgroundTaskCommand = new RelayCommand<object>(CancelBackgroundTask, CancelBackgroundTask_CanExecute);
CheckForUpdatesCommand = new RelayCommand<object>(TriggerUpdateCheck);
CancelBackgroundTaskCommand = new RelayCommand<object>(
CancelBackgroundTask, CancelBackgroundTask_CanExecute);
CheckForUpdatesCommand = new RelayCommand<object>(CheckForUpdates);
foreach (Categories cat in (Categories[])Enum.GetValues(typeof(Categories)))
{
CategoriesList.Add(cat);
Expand All @@ -186,7 +191,7 @@ public DownloaderViewModel(DisplayMessageDelegate displayMessage, ShowUrlsDelega
// Check for updates
if (Settings.Default.AutoCheckForUpdates)
{
Task.Run(async () => await CheckForUpdatesAsync(silent: true));
Task.Run(async () => await TriggerUpdateCheckAsync(true));
}

// Populate history
Expand Down Expand Up @@ -459,7 +464,10 @@ internal void Open(object obj)
{
if (obj == null) return;
var items = (obj as ObservableCollection<object>).Cast<DownloaderObjectModel>().ToList();
var itemsFinished = from item in items where item.Status == DownloadStatus.Finished where new FileInfo(item.Destination).Exists select item;
var itemsFinished = from item in items
where item.Status == DownloadStatus.Finished
where new FileInfo(item.Destination).Exists
select item;
if (itemsFinished.Count() > 1)
{
var r = _displayMessage.Invoke(
Expand All @@ -480,12 +488,17 @@ internal void OpenContainingFolder(object obj)
{
if (obj == null) return;
var items = (obj as ObservableCollection<object>).Cast<DownloaderObjectModel>().ToList();
var itemsOpenable = from item in items where File.Exists(item.Destination) || Directory.Exists(Path.GetDirectoryName(item.Destination)) select item;
var itemsOpenable = from item in items
where File.Exists(item.Destination) ||
Directory.Exists(Path.GetDirectoryName(item.Destination))
select item;
if (itemsOpenable.Count() > 1)
{
var result = _displayMessage.Invoke("You have selected to open " + items.Count + " folders.\n\n" +
"Opening too many folders at the same time may cause the system to crash.\n\nDo you wish to proceed?",
"Open Folder", MessageBoxButton.YesNo, MessageBoxImage.Exclamation, MessageBoxResult.No);
var result = _displayMessage.Invoke(
"You have selected to open " + items.Count + " folders.\n\n" +
"Opening too many folders at the same time may cause the system to crash.\n\n" +
"Do you wish to proceed?", "Open Folder",
MessageBoxButton.YesNo, MessageBoxImage.Exclamation, MessageBoxResult.No);

if (result == MessageBoxResult.No)
{
Expand Down Expand Up @@ -529,7 +542,9 @@ internal void CloseApp(object obj)

if (_ctsUpdatingList != null)
{
if (_displayMessage.Invoke("Background operation in progress. Cancel and exit program?", "Exit", MessageBoxButton.YesNo, MessageBoxImage.Exclamation, MessageBoxResult.No) == MessageBoxResult.No)
if (_displayMessage.Invoke(
"Background operation in progress. Cancel and exit program?", "Exit",
MessageBoxButton.YesNo, MessageBoxImage.Exclamation, MessageBoxResult.No) == MessageBoxResult.No)
{
return;
}
Expand Down Expand Up @@ -773,7 +788,8 @@ internal void Download_Stopped(object sender, EventArgs e)
Monitor.Enter(_lockBytesTransferredOverLifetime);
try
{
Settings.Default.BytesTransferredOverLifetime += (ulong)(sender as DownloaderObjectModel).BytesDownloadedThisSession;
Settings.Default.BytesTransferredOverLifetime +=
(ulong)(sender as DownloaderObjectModel).BytesDownloadedThisSession;
}
finally
{
Expand Down Expand Up @@ -965,11 +981,41 @@ private async Task AddItemsAsync(string destination, bool enqueue, bool start =
DownloaderObjectModel item;
if (forceEnqueue || enqueue)
{
item = new DownloaderObjectModel(ref _client, urls[i], fileName, enqueue: true, Download_Created, Download_Verifying, Download_Verified, Download_Started, Download_Stopped, Download_Enqueued, Download_Dequeued, Download_Finished, Download_PropertyChanged, ProgressReporter, ref _requestThrottler);
item = new DownloaderObjectModel(
ref _client,
urls[i],
fileName,
enqueue: true,
Download_Created,
Download_Verifying,
Download_Verified,
Download_Started,
Download_Stopped,
Download_Enqueued,
Download_Dequeued,
Download_Finished,
Download_PropertyChanged,
ProgressReporter,
ref _requestThrottler);
}
else
{
item = new DownloaderObjectModel(ref _client, urls[i], fileName, enqueue: false, Download_Created, Download_Verifying, Download_Verified, Download_Started, Download_Stopped, Download_Enqueued, Download_Dequeued, Download_Finished, Download_PropertyChanged, ProgressReporter, ref _requestThrottler);
item = new DownloaderObjectModel(
ref _client,
urls[i],
fileName,
enqueue: false,
Download_Created,
Download_Verifying,
Download_Verified,
Download_Started,
Download_Stopped,
Download_Enqueued,
Download_Dequeued,
Download_Finished,
Download_PropertyChanged,
ProgressReporter,
ref _requestThrottler);
if (start)
{
tasks.Add(item.StartAsync());
Expand Down Expand Up @@ -1008,7 +1054,9 @@ private async Task AddItemsAsync(string destination, bool enqueue, bool start =
{
if (skipping.Count > 0)
{
_showUrls(skipping, "Duplicate Entries", "The following URLs were not added because they are already in the list:");
_showUrls(
skipping, "Duplicate Entries",
"The following URLs were not added because they are already in the list:");
}

if ((enqueue && start) || forceEnqueue)
Expand Down Expand Up @@ -1092,7 +1140,10 @@ private async Task RemoveObjectsAsync(bool delete, params DownloaderObjectModel[
{
if (objects[i].Status == DownloadStatus.Finished)
{
FileSystem.DeleteFile(objects[i].Destination, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
FileSystem.DeleteFile(
objects[i].Destination,
UIOption.OnlyErrorDialogs,
RecycleOption.SendToRecycleBin);
}
else
{
Expand Down Expand Up @@ -1190,32 +1241,30 @@ internal bool CancelBackgroundTask_CanExecute(object obj)
return this.IsBackgroundWorking;
}

internal void TriggerUpdateCheck(object obj)
internal void CheckForUpdates(object obj)
{
Task.Run(async () =>
{
await CheckForUpdatesAsync();
});
Task.Run(async () => await TriggerUpdateCheckAsync());
}

private async Task CheckForUpdatesAsync(bool silent = false)
private async Task TriggerUpdateCheckAsync(bool silent = false)
{
string url = await Common.CheckForUpdates.GetUpdateUrl(
_client, @"https://mozib.io/downloads/update.php",
string url = await AppUpdateService.GetUpdateUrl(
@"https://mozib.io/downloads/update.php",
Assembly.GetExecutingAssembly().GetName().Name,
Assembly.GetExecutingAssembly().GetName().Version.ToString());

if (string.IsNullOrEmpty(url))
{
if (!silent)
{
_displayMessage.Invoke("No new updates are available.", "Check For Updates", MessageBoxButton.OK, MessageBoxImage.Information);
_displayMessage.Invoke(
"No new updates are available.", "Update", MessageBoxButton.OK, MessageBoxImage.Information);
}
return;
}

if (_displayMessage.Invoke(
"An update is available.\n\nWould you like to download it now?", "Check For Updates",
"An update is available.\n\nWould you like to download it now?", "Update",
MessageBoxButton.YesNo, MessageBoxImage.Information, MessageBoxResult.Yes) == MessageBoxResult.Yes)
{
Process.Start("explorer.exe", url);
Expand Down
Loading

0 comments on commit 6fc78a7

Please sign in to comment.