-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathApp.xaml.cs
More file actions
300 lines (242 loc) · 10.2 KB
/
Copy pathApp.xaml.cs
File metadata and controls
300 lines (242 loc) · 10.2 KB
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
namespace SyncPro.UI
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using Hardcodet.Wpf.TaskbarNotification;
using SyncPro.Adapters;
using SyncPro.Adapters.BackblazeB2;
using SyncPro.Adapters.GoogleDrive;
using SyncPro.Adapters.MicrosoftAzureStorage;
using SyncPro.Adapters.MicrosoftOneDrive;
using SyncPro.Adapters.WindowsFileSystem;
using SyncPro.Counters;
using SyncPro.Data;
using SyncPro.Runtime;
using SyncPro.Tracing;
using SyncPro.UI.Dialogs;
using SyncPro.UI.Framework.MVVM;
using SyncPro.UI.Navigation;
using SyncPro.UI.Navigation.ViewModels;
using SyncPro.UI.ViewModels;
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App
{
private Dispatcher localDispatcher;
internal new static App Current { get; private set; }
public MainWindowViewModel MainWindowsViewModel =>
this.mainWindowViewModel;
private MainWindowViewModel mainWindowViewModel;
private TaskbarIcon notifyIcon;
//public SettingsFile Settings { get; private set; }
public string AppDataRoot { get; private set; }
//private string settingsFilePath;
public ICommand ShowConfigureWindowCommand { get; }
public ICommand ShutdownApplicationCommand { get; }
private bool testMode;
public bool TestMode
{
get => this.testMode;
set
{
this.testMode = value;
this.MainWindowsViewModel.UpdateWindowTitle();
if (value)
{
LogViewerHelper.LaunchLogViewer(null, false);
}
}
}
public App()
{
InitializeComponent();
this.ShutdownMode = ShutdownMode.OnExplicitShutdown;
this.ShowConfigureWindowCommand = new DelegatedCommand(o => ShowMainWindow());
this.ShutdownApplicationCommand = new DelegatedCommand(o => Shutdown());
}
internal static void Start(Dictionary<string, string> args)
{
bool testMode = Keyboard.IsKeyDown(Key.LeftShift);
Global.Initialize(testMode);
if (testMode)
{
LogViewerHelper.LaunchLogViewer(null, false);
}
// Start the counter manager before initializing the adapters in case the initialization
// causes any counters to be emitted.
CounterManager.Start();
CounterManager.LogCounter("AppStart", 1);
AdapterRegistry.RegisterAdapter(
BackblazeB2Adapter.TargetTypeId,
typeof(BackblazeB2Adapter),
typeof(BackblazeB2AdapterConfiguration));
AdapterRegistry.RegisterAdapter(
GoogleDriveAdapter.TargetTypeId,
typeof(GoogleDriveAdapter),
typeof(GoogleDriveAdapterConfiguration));
AdapterRegistry.RegisterAdapter(
OneDriveAdapter.TargetTypeId,
typeof(OneDriveAdapter),
typeof(OneDriveAdapterConfiguration));
AdapterRegistry.RegisterAdapter(
WindowsFileSystemAdapter.TargetTypeId,
typeof(WindowsFileSystemAdapter),
typeof(WindowsFileSystemAdapterConfiguration));
AdapterRegistry.RegisterAdapter(
AzureStorageAdapter.TargetTypeId,
typeof(AzureStorageAdapter),
typeof(AzureStorageAdapterConfiguration));
// Enable property validation logging if trying to debug validation problems
// LoggerExtensions.LogPropertyValidation = true;
App app = new App();
Current = app;
Current.testMode = testMode;
bool runInBackground = args.ContainsKey("runInBackground");
if (runInBackground)
{
// ReSharper disable once UseObjectOrCollectionInitializer
app.notifyIcon = new TaskbarIcon();
app.notifyIcon.IconSource =
new BitmapImage(new Uri("pack://application:,,,/SyncPro.UI;component/Resources/Graphics/SyncProIcon.ico"));
app.notifyIcon.ContextMenu = (ContextMenu)app.FindResource("TaskbarIconMenu");
app.notifyIcon.DoubleClickCommand = app.ShowConfigureWindowCommand;
}
app.localDispatcher = Dispatcher.CurrentDispatcher;
app.mainWindowViewModel = new MainWindowViewModel();
if (!runInBackground)
{
app.mainWindowViewModel.RequestClose += (s, a) =>
{
Application.Current.Shutdown(0);
};
}
// Load settings from disk
app.LoadSettings();
if (runInBackground && app.mainWindowViewModel.SyncRelationships.OfType<SyncRelationshipViewModel>().Any())
{
app.notifyIcon.ShowBalloonTip(
"SyncPro Running",
string.Format(
"Loaded {0} sync relationships.",
app.mainWindowViewModel.SyncRelationships.OfType<SyncRelationshipViewModel>().Count()),
BalloonIcon.Info);
}
using (Task task = new Task(app.Initialize, runInBackground))
{
task.Start();
app.Run();
}
if (runInBackground)
{
app.notifyIcon.Visibility = Visibility.Collapsed;
app.notifyIcon.Dispose();
}
CounterManager.Stop();
Global.SaveAppConfig();
}
private void LoadSettings()
{
Logger.InitializeApplicationStart();
string localAppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
this.AppDataRoot = Path.Combine(localAppDataPath, "SyncPro");
if (!Directory.Exists(this.AppDataRoot))
{
Directory.CreateDirectory(this.AppDataRoot);
}
DirectoryInfo appDataRootDir = new DirectoryInfo(this.AppDataRoot);
this.mainWindowViewModel.NavigationItems.Add(new DashboardNodeViewModel(null, new DashboardViewModel()));
List<SyncRelationshipViewModel> relationships = new List<SyncRelationshipViewModel>();
foreach (DirectoryInfo relationshipDir in appDataRootDir.GetDirectories())
{
Guid guid;
if (Guid.TryParse(relationshipDir.Name, out guid))
{
SyncRelationship relationship;
try
{
relationship = SyncRelationship.Load(guid);
}
catch (Exception e)
{
Logger.Error("Failed to load relationship " + guid.ToString("D") + ": " + e);
continue;
}
Global.SyncRelationships.Add(relationship);
SyncDatabase.UpdateIfNeeded(relationship.Configuration.RelationshipId);
//relationship.BeginInitialize();
// Disable the warning about not awaiting the sync call to InitializeAsync, since we want it to execute
// asynchronously while we continue with the view model initialization.
#pragma warning disable 4014
relationship.InitializeAsync().ConfigureAwait(false);
#pragma warning restore 4014
SyncRelationshipViewModel relationshipViewModel = new SyncRelationshipViewModel(relationship, true);
relationships.Add(relationshipViewModel);
}
}
// Create the navigation nodes for each of the relationships
foreach (var relationship in relationships.OrderBy(r => r.Name))
{
this.mainWindowViewModel.SyncRelationships.Add(relationship);
this.mainWindowViewModel.NavigationItems.Add(new SyncRelationshipNodeViewModel(null, relationship));
}
if (this.mainWindowViewModel.NavigationItems.Any())
{
this.mainWindowViewModel.NavigationItems.First().IsSelected = true;
}
Logger.InitializeApplicationStop();
}
private void Initialize(object runInBackground)
{
// If there are no sync relationships, show the main window
if (!this.mainWindowViewModel.SyncRelationships.Any() || !(bool)runInBackground)
{
this.localDispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(ShowMainWindow));
}
this.localDispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(ShowFirstRunDialog));
}
private void ShowMainWindow()
{
if (this.MainWindow == null)
{
this.MainWindow = new MainWindow { DataContext = this.mainWindowViewModel };
this.MainWindow.Show();
}
}
private void ShowFirstRunDialog()
{
if (Global.AppConfig.AcceptUsage)
{
return;
}
FirstRunDialogViewModel viewModel = new FirstRunDialogViewModel();
var firstRunDialog = new FirstRunDialog()
{
DataContext = viewModel
};
//Current.localDispatcher.Invoke(() => { firstRunDialog.ShowDialog(); });
firstRunDialog.ShowDialog();
if (viewModel.AcceptUsage)
{
Global.AppConfig.AcceptUsage = true;
Global.SaveAppConfig();
}
if (!Global.AppConfig.AcceptUsage)
{
Application.Current.Shutdown();
}
}
public static void DispatcherInvoke(Action action)
{
Current.localDispatcher.Invoke(action);
}
}
}