Skip to content

Commit

Permalink
Attempt to improve error handling during theme load process
Browse files Browse the repository at this point in the history
  • Loading branch information
t1m0thyj committed Apr 15, 2019
1 parent 0fd1c0b commit 330612e
Show file tree
Hide file tree
Showing 7 changed files with 382 additions and 404 deletions.
132 changes: 0 additions & 132 deletions src/Compatibility.cs

This file was deleted.

20 changes: 14 additions & 6 deletions src/JsonConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,29 @@ public static void LoadConfig()

public static ThemeConfig LoadTheme(string name)
{
string themeJson;
string jsonText;
ThemeConfig theme;

if (ThemeManager.defaultThemes.Contains(name))
{
themeJson = Encoding.UTF8.GetString((byte[])Properties.Resources.ResourceManager.
GetObject(name + "_json"));
jsonText = Encoding.UTF8.GetString(
(byte[])Properties.Resources.ResourceManager.GetObject(name + "_json"));
}
else
{
themeJson = File.ReadAllText(Path.Combine("themes", name, "theme.json"));
jsonText = File.ReadAllText(Path.Combine("themes", name, "theme.json"));
}

ThemeConfig theme = JsonConvert.DeserializeObject<ThemeConfig>(themeJson);
theme.themeId = name;
try
{
theme = JsonConvert.DeserializeObject<ThemeConfig>(jsonText);
}
catch (JsonException)
{
return null;
}

theme.themeId = name;
return theme;
}

Expand Down
114 changes: 73 additions & 41 deletions src/ProgressDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,9 @@ public partial class ProgressDialog : Form
private Queue<ThemeConfig> downloadQueue;
private Queue<string> importQueue;
private int numJobs;
private IntPtr taskbarHandle;

private WebClient wc = new WebClient();
private Stopwatch stopwatch = new Stopwatch();

public bool isUriEmpty = false;
private WebClient wc = new WebClient();

public ProgressDialog()
{
Expand All @@ -36,7 +33,7 @@ public ProgressDialog()

this.Font = SystemFonts.MessageBoxFont;
this.FormClosing += OnFormClosing;
taskbarHandle = this.Handle;
ThemeLoader.taskbarHandle = this.Handle;

wc.DownloadProgressChanged += OnDownloadProgressChanged;
wc.DownloadFileCompleted += OnDownloadFileCompleted;
Expand Down Expand Up @@ -69,33 +66,30 @@ private void DownloadNext()
if (downloadQueue.Count > 0)
{
ThemeConfig theme = downloadQueue.Peek();
this.Invoke(new Action(() => UpdateDownloadStatus(theme)));

if (theme.imagesZipUri.StartsWith("file://"))
{
string themePath = (new Uri(theme.imagesZipUri)).LocalPath;
ThemeManager.CopyLocalTheme(theme, themePath,
this.UpdateTotalPercentage);

downloadQueue.Dequeue();
Task.Run(() => {
bool success = ThemeLoader.CopyLocalTheme(theme, themePath,
this.UpdateTotalPercentage);
if (!ThemeManager.importMode)
{
DownloadNext();
}
if (!success)
{
this.Invoke(new Action(() => ThemeLoader.HandleError(theme.themeId)));
}
downloadQueue.Dequeue();
this.Invoke(new Action(() => DownloadNext()));
});
}
else
else if (!string.IsNullOrEmpty(theme.imagesZipUri))
{
List<string> imagesZipUris = theme.imagesZipUri.Split('|').ToList();

try
{
wc.DownloadFileAsync(new Uri(imagesZipUris.First()),
wc.DownloadFileAsync(new Uri(imagesZipUris.First()),
theme.themeId + "_images.zip", imagesZipUris.Skip(1).ToList());
}
catch (Exception e) {
isUriEmpty = true;
}

}
}
else if (!ThemeManager.importMode)
Expand All @@ -104,6 +98,12 @@ private void DownloadNext()
}
}

private void UpdateDownloadStatus(ThemeConfig theme)
{
label1.Text = string.Format(_("Downloading images for '{0}'..."),
ThemeManager.GetThemeName(theme));
}

private void ImportNext()
{
if (ThemeManager.importPaths.Count > 0)
Expand All @@ -122,15 +122,17 @@ private void ImportNext()
if (importQueue.Count > 0)
{
string themePath = importQueue.Peek();

ThemeConfig theme = ThemeManager.ImportTheme(themePath, taskbarHandle);
this.Invoke(new Action(() => UpdateImportStatus(themePath)));
ThemeConfig theme = ThemeManager.ImportTheme(themePath);
this.Invoke(new Action(() => ThemeLoader.HandleError(theme.themeId)));

if (theme != null)
{
if (Path.GetExtension(themePath) == ".json")
{
downloadQueue = new Queue<ThemeConfig>(new List<ThemeConfig>() { theme });
DownloadNext();
downloadQueue = new Queue<ThemeConfig>(
new List<ThemeConfig>() { theme });
DownloadNext(); // TODO Test if this works
}

ThemeManager.importedThemes.Add(theme);
Expand All @@ -146,59 +148,89 @@ private void ImportNext()
}
}

private void UpdateImportStatus(string themePath)
{
label1.Text = string.Format(_("Importing theme from {0}..."),
Path.GetFileName(themePath));
}

private void UpdateTotalPercentage(int themePercentage)
{
int numRemaining = ThemeManager.importMode ? importQueue.Count : downloadQueue.Count;
int percentage = ((numJobs - numRemaining) * 100 + themePercentage) / numJobs;

stopwatch.Start();
this.progressBar1.BeginInvoke((MethodInvoker)delegate ()
progressBar1.Invoke(new Action(() =>
{
progressBar1.Value = percentage;
progressBar1.Refresh();
TaskbarProgress.SetValue(this.Handle, percentage, 100);
});
}));
}

private void OnDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
UpdateTotalPercentage(e.ProgressPercentage);

fileTransferSpeedLabel.Text = string.Format("{0} MB/s",
fileTransferSpeedLabel.Text = string.Format(_("{0} MB/s"),
(e.BytesReceived / 1024f / 1024f / stopwatch.Elapsed.TotalSeconds).ToString("0.#"));

fileSizeProgressLabel.Text = string.Format("{0} MB of {1} MB",
fileSizeProgressLabel.Text = string.Format(_("{0} MB of {1} MB"),
(e.BytesReceived / 1024d / 1024d).ToString("0.#"),
(e.TotalBytesToReceive / 1024d / 1024d).ToString("0.#"));
}

public void OnDownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
public async void OnDownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
stopwatch.Stop();

List<string> imagesZipUris = (List<string>)e.UserState;

if (e.Error != null && imagesZipUris.Count > 0)
if (e.Error == null)
{
ThemeConfig theme = downloadQueue.Peek();
wc.DownloadFileAsync(new Uri(imagesZipUris.First()),
theme.themeId + "_images.zip", imagesZipUris.Skip(1).ToList());
ThemeConfig theme = downloadQueue.Dequeue();

await Task.Run(() => ThemeLoader.ExtractTheme(theme.themeId + "_images.zip",
theme.themeId, true));

ThemeLoader.HandleError(theme.themeId);
DownloadNext();
}
else
{
ThemeConfig theme = downloadQueue.Dequeue();
List<string> imagesZipUris = (List<string>)e.UserState;
ThemeConfig theme = downloadQueue.Peek();

if (e.Error == null)
if (imagesZipUris.Count == 0)
{
ThemeManager.ExtractTheme(theme.themeId + "_images.zip", theme.themeId, true);
bool shouldRetry = ThemeLoader.PromptDialog(string.Format(_("Failed to " +
"download images for the '{0}' theme. Do you want to try again?"),
theme.themeId));

if (shouldRetry)
{
imagesZipUris = theme.imagesZipUri.Split('|').ToList();
}
else
{
ThemeManager.DisableTheme(theme.themeId); // TODO Handle error here for failed download
}
}

DownloadNext();
if (imagesZipUris.Count > 0)
{
wc.DownloadFileAsync(new Uri(imagesZipUris.First()),
theme.themeId + "_images.zip", imagesZipUris.Skip(1).ToList());
}
else
{
downloadQueue.Dequeue();
DownloadNext();
}
}
}

private void OnFormClosing(object sender, FormClosingEventArgs e)
{
ThemeLoader.taskbarHandle = IntPtr.Zero;
wc?.Dispose();
}
}
Expand Down
Loading

0 comments on commit 330612e

Please sign in to comment.