-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathReLinkMain.cs
96 lines (82 loc) · 3.7 KB
/
ReLinkMain.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
/********************************************************************
Copyright (c) Shameel Ahmed. All rights reserved.
********************************************************************/
using ReLink.Core;
using System;
using System.IO;
using System.Windows.Forms;
namespace ReLink
{
static class ReLinkMain {
internal const string ARG_REGISTER = "--register";
internal const string ARG_UNREGISTER = "--unregister";
internal const string RUNAS_VERB = "runas";
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args) {
try {
var osVersion = NativeHelper.GetOSVersion();
if (osVersion == OSVersion.Win8 || osVersion == OSVersion.Win10) {
Run(args);
} else {
ToastForm.ShowToast($"{Application.ProductName} runs on Windows 8 and later. Your operating system is not supported.");
}
} catch (Exception ex) {
HandleMainException(ex);
}
}
static void Run(string[] args) {
InitializeServices();
if (args.Length > 0) {
//If any arguments are passed, handle it and exit.
//The UI shows only when no arguments are passed.
HandleArgs(args);
return;
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(true);
//Run main form
MainForm mainForm = MainForm.Instance;
Application.Run(mainForm);
}
static void HandleArgs(string[] args) {
foreach (string arg in args) {
if (string.Equals(arg, ARG_REGISTER, StringComparison.OrdinalIgnoreCase)) {
BrowserManager.RegisterRelinkAsBrowser();
} else if (string.Equals(arg, ARG_UNREGISTER, StringComparison.OrdinalIgnoreCase)) {
BrowserManager.UnregisterRelinkAsBrowser();
} else {
BrowserInfo browser = BrowserManager.LaunchUrl(arg);
string urlToLaunch = arg;
if (urlToLaunch.Length > 80) {
urlToLaunch = urlToLaunch.Substring(0, 80) + "...";
}
ToastForm.ShowToast($"Re:Link opened {urlToLaunch} in {browser.Name}", true);
}
}
}
private static void InitializeServices() {
string applicationName = Application.ProductName;
string configDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), applicationName);
PropertyService.InitializeService(configDirectory, Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "data"), applicationName);
PropertyService.Load();
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.Automatic);
Application.ThreadException += Application_ThreadException;
Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
}
private static void ShutdownServices() {
PropertyService.Save();
}
static void HandleMainException(Exception ex) {
MessageBox.Show(ex.ToString(), "Critical error");
}
private static void Application_ApplicationExit(object sender, EventArgs e) {
ShutdownServices();
}
private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) {
MessageBox.Show(e.Exception.ToString(), "Critical error");
}
}
}