Skip to content

Commit

Permalink
🐛 Fix download not working on some machine
Browse files Browse the repository at this point in the history
  • Loading branch information
Vysp3r committed Jan 2, 2023
1 parent a01cb6d commit 7fd6d15
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 18 deletions.
6 changes: 5 additions & 1 deletion src/Stores/Main.vala
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ namespace Stores {
public Models.Preference Preference;

public double ProgressBarValue;
public bool ProgressBarIsDone;
public bool IsInstallationCancelled;
public bool IsDownloadCompleted;
public bool IsExtractionCompleted;

public List<Release> Releases;
public bool ReleaseRequestIsDone;

public bool close;

static Once<Main> _instance;

public static unowned Main get_instance () {
Expand Down
11 changes: 6 additions & 5 deletions src/Utils/File.vala
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ namespace Utils {
r = ext.finish_entry ();
if (r < Archive.Result.OK) stderr.printf (ext.error_string ());
if (r < Archive.Result.WARN) return "";

if (Stores.Main.get_instance ().IsInstallationCancelled) {
Delete (install_location + sourcePath);
break;
}
}

archive.close ();
Expand Down Expand Up @@ -81,11 +86,7 @@ namespace Utils {
}

public static void Rename (string sourcePath, string destinationPath) {
try {
GLib.FileUtils.rename (sourcePath, destinationPath);
} catch (GLib.Error e) {
stderr.printf (e.message + "\n");
}
GLib.FileUtils.rename (sourcePath, destinationPath);
}

public static void Write (string path, string content) {
Expand Down
45 changes: 37 additions & 8 deletions src/Utils/Web.vala
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,47 @@ namespace Utils {
}
}

public static int Download (string download_url, string download_path) {
public static void Download (string url, string path) {
var client = new Soup.Session ();

var request = new Soup.Message ("GET", url);

try {
var file_from_http = GLib.File.new_for_uri (download_url);
GLib.File local_file = GLib.File.new_for_path (download_path);
file_from_http.copy (local_file, FileCopyFlags.OVERWRITE, null, (current, total) => {
Stores.Main.get_instance ().ProgressBarValue = (current + 0.0d) / (total + 0.0d);
});
return 0;
var input_stream = client.send (request);

if (request.status_code != 200) {
stderr.printf (request.reason_phrase + "\n");
Stores.Main.get_instance ().IsInstallationCancelled = true;
return;
}

var file = GLib.File.new_for_path (path);
if (file.query_exists ()) file.delete ();
FileOutputStream output_stream = file.create (FileCreateFlags.REPLACE_DESTINATION);

int64 content_length = request.response_headers.get_content_length ();

const int chunk_size = 1024;
ulong bytes_downloaded = 0;
while (bytes_downloaded < content_length) {
if (Stores.Main.get_instance ().IsInstallationCancelled) {
if (file.query_exists ()) file.delete ();
break;
}

ulong bytes_read = output_stream.write (input_stream.read_bytes (chunk_size).get_data ());

bytes_downloaded += bytes_read;
Stores.Main.get_instance ().ProgressBarValue = (double) bytes_downloaded / content_length;
}

output_stream.close ();
} catch (GLib.Error e) {
stderr.printf (e.message + "\n");
return 1;
Stores.Main.get_instance ().IsInstallationCancelled = true;
}

client.abort ();
}
}
}
2 changes: 2 additions & 0 deletions src/Widgets/ProtonMessageDialog.vala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace Widgets {

public ProtonMessageDialog (Gtk.Window window, string? heading, string? body, MessageDialogType type, ResponseCallback? responseCallback) {
set_transient_for (window);
set_modal (true);

if (heading != null) set_heading (heading);
if (body != null) set_body (body);

Expand Down
47 changes: 43 additions & 4 deletions src/Windows/Installer.vala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ namespace Windows {
currentLauncher = launcher;
mainStore = Stores.Main.get_instance ();

// Reset stores values
mainStore.IsInstallationCancelled = false;
mainStore.IsDownloadCompleted = false;
mainStore.IsExtractionCompleted = false;
mainStore.ProgressBarValue = 0;

// Initialize shared widgets
crTools = new Widgets.ProtonComboRow (_ ("Compatibility Tool"), Models.Tool.GetStore (launcher.Tools));
crReleases = new Widgets.ProtonComboRow (_ ("Version"));
Expand Down Expand Up @@ -84,49 +90,82 @@ namespace Windows {
progressBarDownload.set_visible (false);
boxMain.append (progressBarDownload);

this.close_request.connect (onClose);

// Show the window
show ();
}

void Download () {
progressBarDownload.set_text (_ ("Downloading..."));
new Thread<int> ("download", () => Utils.Web.Download (currentRelease.Download_URL, currentLauncher.Directory + "/" + currentRelease.Title + ".tar.gz"));

new Thread<void> ("download", () => {
Utils.Web.Download (currentRelease.Download_URL, currentLauncher.Directory + "/" + currentRelease.Title + ".tar.gz");
Stores.Main.get_instance ().IsDownloadCompleted = true;
});

GLib.Timeout.add (75, () => {
if (mainStore.IsInstallationCancelled) {
CancelInstallation ();
return false;
}

progressBarDownload.set_fraction (mainStore.ProgressBarValue);
if (mainStore.ProgressBarValue == 1) {
if (mainStore.IsDownloadCompleted) {
Extract ();
return false;
}

return true;
}, 1);
}

void Extract () {
progressBarDownload.set_text (_ ("Extracting..."));
progressBarDownload.set_pulse_step (1);

new Thread<void> ("extract", () => {
string sourcePath = Utils.File.Extract (currentLauncher.Directory + "/", currentRelease.Title);
if (currentTool.Type != Models.Tool.TitleType.NONE) Utils.File.Rename (sourcePath, currentLauncher.Directory + "/" + currentRelease.GetFolderTitle (currentLauncher, currentTool));
Stores.Main.get_instance ().ProgressBarIsDone = true;
Stores.Main.get_instance ().IsExtractionCompleted = true;
});

GLib.Timeout.add (500, () => {
if (mainStore.IsInstallationCancelled) {
CancelInstallation ();
return false;
}

progressBarDownload.pulse ();
if (mainStore.ProgressBarIsDone == true) {
if (mainStore.IsExtractionCompleted == true) {
progressBarDownload.set_text (_ ("Done!"));
progressBarDownload.set_fraction (mainStore.ProgressBarValue = 0);
btnInstall.set_sensitive (true);
response (Gtk.ResponseType.APPLY);
return false;
}

return true;
}, 1);
}

void CancelInstallation () {
progressBarDownload.set_fraction (0);
progressBarDownload.set_text (_ ("Cancelled!"));
btnInstall.set_sensitive (true);
}

// Events
bool onClose () {
mainStore.IsInstallationCancelled = true;
return false;
}

void btnInstall_Clicked () {
btnInstall.set_sensitive (false);
progressBarDownload.set_visible (true);
progressBarDownload.set_show_text (true);

Download ();
}

Expand Down

0 comments on commit 7fd6d15

Please sign in to comment.