/
Error_Handling.cs
259 lines (228 loc) · 8.49 KB
/
Error_Handling.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
using PicView.ChangeTitlebar;
using PicView.FileHandling;
using PicView.ImageHandling;
using PicView.PicGallery;
using PicView.Properties;
using PicView.SystemIntegration;
using PicView.UILogic;
using PicView.UILogic.Sizing;
using System.IO;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using static PicView.ChangeImage.Navigation;
using static PicView.FileHandling.DeleteFiles;
namespace PicView.ChangeImage;
internal static class ErrorHandling
{
/// <summary>
/// Returns true, if navigating will result in out of rang exception
/// </summary>
/// <returns></returns>
internal static bool CheckOutOfRange()
{
bool value = true;
ConfigureWindows.GetMainWindow.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() =>
{
value = Pics.Count < FolderIndex || Pics.Count < 1 ||
UC.GetCroppingTool is { IsVisible: true } || (UC.GetQuickResize?.Opacity > 0);
}));
return value;
}
internal static void UnexpectedError()
{
ConfigureWindows.GetMainWindow.Dispatcher.Invoke(DispatcherPriority.Render, () =>
{
Unload(true);
Tooltip.ShowTooltipMessage(Application.Current.Resources["UnexpectedError"]);
ConfigureWindows.GetMainWindow.Title = (string)Application.Current.Resources["UnexpectedError"] + " - PicView";
ConfigureWindows.GetMainWindow.TitleText.Text = (string)Application.Current.Resources["UnexpectedError"];
ConfigureWindows.GetMainWindow.TitleText.ToolTip = (string)Application.Current.Resources["UnexpectedError"];
ConfigureWindows.GetMainWindow.MainImage.Cursor = Cursors.Arrow;
});
}
internal static bool CheckDirectoryChangeAndPicGallery(FileInfo fileInfo)
{
bool folderChanged = false;
// If count not correct or just started, get values
if (Pics?.Count <= FolderIndex || FolderIndex < 0)
{
folderChanged = true;
}
// If the file is in the same folder, navigate to it. If not, start manual loading procedure.
else if (!string.IsNullOrWhiteSpace(Pics?[FolderIndex]) && fileInfo.Directory.FullName != Path.GetDirectoryName(Pics[FolderIndex]))
{
// Reset old values and get new
ChangeFolder(true);
folderChanged = true;
}
if (Pics.Contains(fileInfo.FullName) == false)
{
folderChanged = true;
}
if (UC.GetPicGallery is null || folderChanged is false) { return folderChanged; }
if (Settings.Default.FullscreenGallery)
{
ConfigureWindows.GetMainWindow.Dispatcher.Invoke(DispatcherPriority.Render, (Action)(() =>
{
// Remove children before loading new
if (UC.GetPicGallery.Container.Children.Count > 0)
{
UC.GetPicGallery.Container.Children.Clear();
}
}));
}
return folderChanged;
}
/// <summary>
/// If url returns "web", if base64 returns "base64" if file, returns file path, if directory returns "directory" else returns empty
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
internal static string CheckIfLoadableString(string s)
{
if (!string.IsNullOrWhiteSpace(s.GetURL()))
return "web";
if (Base64.IsBase64String(s))
return "base64";
if (File.Exists(s))
return Path.GetExtension(s).IsArchive() ? "zip" : s;
s = s.Trim().Replace("\"", "");
if (File.Exists(s))
return s;
return Directory.Exists(s) ? "directory" : string.Empty;
}
/// <summary>
/// Clears data, to free objects no longer necessary to store in memory and allow changing folder without error.
/// </summary>
internal static void ChangeFolder(bool backup = false)
{
if (Pics?.Count > 0 && Pics.Count > FolderIndex && backup)
{
// Make a backup of xPicPath and FolderIndex
if (!string.IsNullOrWhiteSpace(Pics[FolderIndex]))
{
BackupPath = Pics[FolderIndex];
}
}
Pics.Clear();
if (ConfigureWindows.GetMainWindow.CheckAccess())
{
GalleryFunctions.Clear();
}
else
{
ConfigureWindows.GetMainWindow.Dispatcher.Invoke(DispatcherPriority.Background, new Action(GalleryFunctions.Clear));
}
PreLoader.Clear();
DeleteTempFiles();
}
/// <summary>
/// Refresh the current list of pics and reload them if there is some missing or changes.
/// </summary>
internal static async Task ReloadAsync(bool fromBackup = false)
{
var path = fromBackup ? BackupPath ?? null : GetReloadPath();
if (path == null)
{
UnexpectedError();
}
if (File.Exists(path))
{
var fileInfo = new FileInfo(path);
await ResetValues(fileInfo).ConfigureAwait(false);
await LoadPic.LoadPiFromFileAsync(null, fileInfo).ConfigureAwait(false);
}
else if (Directory.Exists(path))
{
var fileInfo = new FileInfo(path);
await ResetValues(fileInfo).ConfigureAwait(false);
await LoadPic.LoadPicFromFolderAsync(fileInfo, FolderIndex).ConfigureAwait(false);
}
else if (Base64.IsBase64String(path))
{
await UpdateImage.UpdateImageFromBase64PicAsync(path).ConfigureAwait(false);
}
else if (Clipboard.ContainsImage())
{
await UpdateImage.UpdateImageAsync((string)Application.Current.Resources["ClipboardImage"], Clipboard.GetImage()).ConfigureAwait(false);
}
else if (Uri.IsWellFormedUriString(path, UriKind.Absolute)) // Check if from web
{
await HttpFunctions.LoadPicFromUrlAsync(path).ConfigureAwait(false);
}
else
{
UnexpectedError();
}
string? GetReloadPath()
{
if (CheckOutOfRange())
{
return ConfigureWindows.GetMainWindow?.Dispatcher.Invoke(() =>
{
var fileName = Path.GetFileName(ConfigureWindows.GetMainWindow.TitleText.Text);
return fileName == (string) Application.Current.Resources["Loading"] ? InitialPath : fileName;
});
}
if (string.IsNullOrWhiteSpace(InitialPath) == false
&& Settings.Default.IncludeSubDirectories
&& Path.GetDirectoryName(InitialPath) != Path.GetDirectoryName(Pics[FolderIndex]))
{
return InitialPath;
}
return Pics[FolderIndex];
}
}
private static async Task ResetValues(FileInfo fileInfo)
{
PreLoader.Clear();
Pics = FileLists.FileList(fileInfo);
bool containerCheck = false;
await ConfigureWindows.GetMainWindow.Dispatcher.InvokeAsync(() =>
{
if (UC.GetPicGallery?.Container?.Children?.Count > 0)
{
containerCheck = true;
}
});
if (containerCheck)
{
await GalleryLoad.LoadAsync().ConfigureAwait(false);
}
}
/// <summary>
/// Reset to default state
/// </summary>
internal static void Unload(bool showStartup)
{
ConfigureWindows.GetMainWindow.Dispatcher.Invoke((Action)(() =>
{
ConfigureWindows.GetMainWindow.TitleText.ToolTip = ConfigureWindows.GetMainWindow.TitleText.Text =
(string)Application.Current.Resources["NoImage"];
ConfigureWindows.GetMainWindow.Title = Application.Current.Resources["NoImage"] + " - " + SetTitle.AppName;
ConfigureWindows.GetMainWindow.MainImage.Source = null;
ConfigureWindows.GetMainWindow.MainImage.Width = 0;
ConfigureWindows.GetMainWindow.MainImage.Height = 0;
WindowSizing.SetWindowBehavior();
UC.ToggleStartUpUC(!showStartup);
}));
Pics?.Clear();
PreLoader.Clear();
GalleryFunctions.Clear();
ScaleImage.XWidth = ScaleImage.XHeight = 0;
if (!string.IsNullOrWhiteSpace(ArchiveExtraction.TempFilePath))
{
DeleteTempFiles();
ArchiveExtraction.TempFilePath = string.Empty;
}
try
{
Taskbar.NoProgress();
}
catch
{
// ignored
}
}
}