-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.xaml.cs
401 lines (315 loc) · 14.1 KB
/
App.xaml.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using Alphaleonis.Win32.Filesystem;
using JetBrains.Annotations;
using NLog;
using NLog.Config;
using NLog.Targets.Syslog;
using NLog.Targets.Syslog.Settings;
using TVRename.Ipc;
using TVRename.ViewModel;
namespace TVRename
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
/// <summary>
/// Defines the entry point of the application.
/// Checks if the application is already running and if so, performs actions via IPC and exits.
/// </summary>
/// <param name="args">The command line arguments.</param>
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
Logger.Info($"TV Rename {Helpers.DisplayVersion} started with args: {string.Join(" ", e.Args)}");
Logger.Info($"Copyright (C) {DateTime.Now.Year} TV Rename");
Logger.Info(
"This program comes with ABSOLUTELY NO WARRANTY; This is free software, and you are welcome to redistribute it under certain conditions");
// Parse command line arguments
CommandLineArgs clargs = new CommandLineArgs(new ReadOnlyCollection<string>(e.Args));
//Application.EnableVisualStyles();
//Application.SetCompatibleTextRenderingDefault(false);
AppDomain.CurrentDomain.UnhandledException += GlobalExceptionHandler;
if (e.Args.Contains("/?", StringComparer.OrdinalIgnoreCase))
{
Logger.Info(CommandLineArgs.Helptext());
//redirect console output to parent process
//must be before any calls to Console.WriteLine()
//MS: Never got this to work quite right - seems there is no simple way to output
//to the command line console if you are a winforms app
if (NativeMethods.AttachParentConsole())
{
Console.WriteLine(CommandLineArgs.Helptext());
}
else
{
Logger.Info("Could not attach to console");
}
return;
}
// Check if an application instance is already running
Mutex mutex = new Mutex(true, "TVRename", out bool newInstance);
if (!newInstance)
{
// Already running
Logger.Warn("An instance is already running");
// Create an IPC channel to the existing instance
RemoteClient.Proxy();
// Transparent proxy to the existing instance
RemoteClient ipc = new RemoteClient();
// If already running and no command line arguments then bring instance to the foreground and quit
if (e.Args.Length == 0)
{
ipc.FocusWindow();
return;
}
// Send command-line arguments to already running instance
CommandLineArgs.MissingFolderBehavior previousMissingFolderBehavior = ipc.MissingFolderBehavior;
bool previousRenameBehavior = ipc.RenameBehavior;
if (clargs.RenameCheck == false)
{
// Temporarily override behavior for renaming folders
ipc.RenameBehavior = false;
}
if (clargs.MissingFolder != CommandLineArgs.MissingFolderBehavior.ask)
{
// Temporarily override behavior for missing folders
ipc.MissingFolderBehavior = clargs.MissingFolder;
}
// TODO: Unify command line handling between here and in UI.cs (ProcessArgs). Just send in clargs via IPC?
if (clargs.Scan)
{
ipc.Scan();
}
if (clargs.QuickScan)
{
ipc.QuickScan();
}
if (clargs.RecentScan)
{
ipc.RecentScan();
}
if (clargs.DoAll)
{
ipc.ProcessAll();
}
if (clargs.Quit)
{
ipc.Quit();
}
// TODO: Necessary?
ipc.RenameBehavior = previousRenameBehavior;
ipc.MissingFolderBehavior = previousMissingFolderBehavior;
return;
}
try
{
Logger.Info("Starting new instance");
LetsGo(clargs, string.Join(" ", e.Args));
GC.KeepAlive(mutex);
}
catch (Exception ex)
{
Logger.Fatal(ex, "Application exiting with error");
//new ShowException(ex).ShowDialog();
Environment.Exit(1);
}
Logger.Info("Application exiting");
}
private void LetsGo(CommandLineArgs clargs, string commandLine) {
//initialize the splash screen and set it as the application main window
TvRenameSplash splashScreen = new TvRenameSplash();
SplashViewModel vm = new SplashViewModel {Version = Helpers.DisplayVersion};
this.MainWindow = splashScreen;
splashScreen.DataContext = vm;
if (clargs.Hide || !Environment.UserInteractive)
{
splashScreen.Visibility = Visibility.Hidden;
}
else
{
splashScreen.Show();
}
// Update splash screen
vm.Status = "Initializing";
//because we're not on the UI thread, we need to use the Dispatcher
//associated with the splash screen to update the progress bar
vm.Progress = 5;
//in order to ensure the UI stays responsive, we need to
//do the work on a different thread
Task.Factory.StartNew(() =>
{
splashScreen.Dispatcher.Invoke(() => vm.Status = "Loading Settings...");
TVDoc doc = LoadSettings(clargs);
splashScreen.Dispatcher.Invoke(() => vm.Status = "Setup Logging...");
if (TVSettings.Instance.mode == TVSettings.BetaMode.BetaToo || TVSettings.Instance.ShareLogs)
{
SetupLogging(commandLine);
}
splashScreen.Dispatcher.Invoke(() => vm.Status = "Configuring...");
ConvertSeriesTimeZones(doc, TheTVDB.Instance);
// Update RegVersion to bring the WebBrowser up to speed
RegistryHelper.UpdateBrowserEmulationVersion();
splashScreen.Dispatcher.Invoke(() => vm.Status = "Create Main Window...");
//once we're done we need to use the Dispatcher
//to create and show the main window
Dispatcher.Invoke(() =>
{
// Show user interface
MainWindow ui = new MainWindow(doc, vm, splashScreen.Dispatcher, !clargs.Unattended && !clargs.Hide && Environment.UserInteractive);
ui.Title = ui.Title + " " + Helpers.DisplayVersion;
// Bind IPC actions to the form, this allows another instance to trigger form actions
RemoteClient.Bind(ui, doc);
//initialize the main window, set it as the application main window
//and close the splash screen
this.MainWindow = ui;
MainWindow.Show();
splashScreen.Close();
});
});
}
[NotNull]
private static TVDoc LoadSettings([NotNull] CommandLineArgs clargs)
{
bool recover = false;
string recoverText = string.Empty;
// Check arguments for forced recover
if (clargs.ForceRecover)
{
recover = true;
recoverText = "Recover manually requested.";
}
SetupCustomSettings(clargs);
FileInfo tvdbFile = PathManager.TVDBFile;
FileInfo settingsFile = PathManager.TVDocSettingsFile;
TVDoc doc;
do // Loop until files correctly load
{
if (recover) // Recovery required, prompt user
{
/* RecoverXml recoveryForm = new RecoverXml(recoverText);
if (recoveryForm.ShowDialog() == DialogResult.OK)
{
tvdbFile = recoveryForm.DbFile;
settingsFile = recoveryForm.SettingsFile;
}
else
{
Logger.Error("User requested no recovery");
throw new TVRenameOperationInterruptedException();
}*/
}
// Try loading settings file
doc = new TVDoc(settingsFile, clargs);
// Try loading TheTVDB cache file
TheTVDB.Instance.Setup(doc.Library,tvdbFile, PathManager.TVDBFile, clargs);
if (recover)
{
doc.SetDirty();
}
recover = !doc.LoadOk;
// Continue if correctly loaded
if (!recover)
{
continue;
}
// Set recover message
recoverText = string.Empty;
if (!doc.LoadOk && !string.IsNullOrEmpty(doc.LoadErr))
{
recoverText = doc.LoadErr;
}
if (!TheTVDB.Instance.LoadOk && !string.IsNullOrEmpty(TheTVDB.Instance.LoadErr))
{
recoverText += $"{Environment.NewLine}{TheTVDB.Instance.LoadErr}";
}
} while (recover);
return doc;
}
private static void SetupCustomSettings([NotNull] CommandLineArgs clargs)
{
// Check arguments for custom settings path
if (!string.IsNullOrEmpty(clargs.UserFilePath))
{
try
{
PathManager.SetUserDefinedBasePath(clargs.UserFilePath);
}
catch (Exception ex)
{
if (!clargs.Unattended && !clargs.Hide && Environment.UserInteractive)
{
MessageBox.Show($"Error while setting the User-Defined File Path:{Environment.NewLine}{ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
Logger.Error(ex, $"Error while setting the User-Defined File Path - EXITING: {clargs.UserFilePath}");
Environment.Exit(1);
}
}
}
private static void ConvertSeriesTimeZones([NotNull] TVDoc doc, TheTVDB tvdb)
{
//this is just to convert timezones in the TheTVDB into the TVDOC where they should be:
//it should only do anything the first time it is run and then be entirely benign
//can be removed after 1/1/19
foreach (ShowItem si in doc.Library.GetShowItems())
{
string newTimeZone = tvdb.GetSeries(si.TvdbCode)?.TempTimeZone;
if (string.IsNullOrWhiteSpace(newTimeZone))
{
continue;
}
if (newTimeZone == TimeZoneHelper.DefaultTimeZone())
{
continue;
}
if (si.ShowTimeZone != TimeZoneHelper.DefaultTimeZone())
{
continue;
}
si.ShowTimeZone = newTimeZone;
doc.SetDirty();
Logger.Info("Copied timezone:{0} onto series {1}", newTimeZone, si.ShowName);
}
}
private static void GlobalExceptionHandler(object sender, UnhandledExceptionEventArgs args)
{
Exception e = (Exception)args.ExceptionObject;
Logger.Fatal(e, "UNHANDLED ERROR - GLobalExceptionHandler");
Environment.Exit(1);
}
private static void SetupLogging(string commandLine)
{
ConfigurationItemFactory.Default.RegisterItemsFromAssembly(Assembly.Load("NLog.Targets.Syslog"));
LoggingConfiguration config = LogManager.Configuration;
SyslogTarget syslog = new SyslogTarget
{
MessageCreation = { Facility = Facility.Local7 },
MessageSend =
{
Protocol = ProtocolType.Tcp,
Tcp = {Server = "logs7.papertrailapp.com", Port = 13236, Tls = {Enabled = true}}
}
};
config.AddTarget("syslog", syslog);
syslog.Layout = "| " + Helpers.DisplayVersion + " |${level:uppercase=true}| ${message} ${exception:format=toString,Data}";
LoggingRule rule = new LoggingRule("*", LogLevel.Error, syslog);
config.LoggingRules.Add(rule);
LogManager.Configuration = config;
Logger.Fatal($"TV Rename {Helpers.DisplayVersion} logging started on {Environment.OSVersion}, {(Environment.Is64BitOperatingSystem ? "64 Bit OS" : "")}, {(Environment.Is64BitProcess ? "64 Bit Process" : "")} {Environment.Version} {(Environment.UserInteractive ? "Interactive" : "")} with args: {commandLine}");
Logger.Info($"Copyright (C) {DateTime.Now.Year} TV Rename");
Logger.Info("This program comes with ABSOLUTELY NO WARRANTY; This is free software, and you are welcome to redistribute it under certain conditions");
}
}
}