136 lines
5.1 KiB
C#
136 lines
5.1 KiB
C#
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using ModVersionChecker.managers.filesystem;
|
|
using ModVersionChecker.managers.interfaces;
|
|
using ModVersionChecker.managers.litedb;
|
|
using ModVersionChecker.repository.api;
|
|
using ModVersionChecker.service;
|
|
using ModVersionChecker.service.interfaces;
|
|
using ModVersionChecker.ui.forms;
|
|
|
|
namespace ModVersionChecker
|
|
{
|
|
class Program
|
|
{
|
|
[STAThread]
|
|
static void Main()
|
|
{
|
|
var builder = Host.CreateDefaultBuilder();
|
|
var program = new Program();
|
|
|
|
builder.ConfigureServices(services =>
|
|
{
|
|
services.AddSingleton<IConfigRepository, ConfigLiteDb>();
|
|
services.AddSingleton<IAppRepository, AppLiteDb>();
|
|
services.AddSingleton<ISourcesDefManager, SourcesLiteDb>();
|
|
services.AddSingleton<ITypeRepository, TypeLiteDb>();
|
|
services.AddSingleton<ITypeConfigRepository, TypeConfigLiteDb>();
|
|
services.AddSingleton<IFormFactory, FormFactory>();
|
|
services.AddSingleton<IAppStatusRepository, AppStatusManager>();
|
|
services.AddSingleton<INotificationService, NotificationService>();
|
|
services.AddSingleton<IApiService, ApiService>();
|
|
services.AddSingleton<IVersionService, VersionService>();
|
|
services.AddSingleton<IApiRepository, ApiRepository>();
|
|
services.AddSingleton<IStateService, StateService>();
|
|
services.AddSingleton<IAppService, AppService>();
|
|
|
|
services.AddTransient<MainForm>();
|
|
services.AddTransient<AppDetailsForm>();
|
|
services.AddTransient<VersionChecker>();
|
|
});
|
|
|
|
using var host = builder.Build();
|
|
|
|
var lifetime = host.Services.GetRequiredService<IHostApplicationLifetime>();
|
|
lifetime.ApplicationStarted.Register(() =>
|
|
{
|
|
Console.WriteLine("Application is shutting down...");
|
|
});
|
|
lifetime.ApplicationStopping.Register(() =>
|
|
{
|
|
Console.WriteLine("Application is shutting down...");
|
|
});
|
|
|
|
Application.SetHighDpiMode(HighDpiMode.SystemAware);
|
|
Application.EnableVisualStyles();
|
|
Application.SetCompatibleTextRenderingDefault(false);
|
|
|
|
if (!SystemTray.IsSupported())
|
|
{
|
|
MessageBox.Show("System tray not supported", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
|
|
var serviceProvider = host.Services;
|
|
var configForm = serviceProvider.GetService<MainForm>();
|
|
var notifyIconService = serviceProvider.GetRequiredService<INotificationService>();
|
|
var configManager = serviceProvider.GetRequiredService<IConfigRepository>();
|
|
var versionService = serviceProvider.GetRequiredService<IVersionService>();
|
|
var appService = serviceProvider.GetRequiredService<IAppService>();
|
|
var config = configManager.GetConfig();
|
|
|
|
EventHandler openFormHandler = (s, e) =>
|
|
{
|
|
if (configForm == null) return;
|
|
|
|
configForm.UpdateListView();
|
|
if (configForm.Visible)
|
|
{
|
|
configForm.BringToFront();
|
|
return;
|
|
}
|
|
configForm.ShowDialog();
|
|
};
|
|
|
|
using (var notifyIcon = new NotifyIcon())
|
|
{
|
|
notifyIcon.Icon = new Icon("Resources/MVC-Icon.ico"); // Place MVC-Icon.ico in Resources
|
|
notifyIcon.Text = "Update Checker";
|
|
notifyIcon.Visible = true;
|
|
notifyIconService.SetNotifyIcon(notifyIcon);
|
|
|
|
var contextMenu = new ContextMenuStrip();
|
|
contextMenu.Items.Add("Configure", null, openFormHandler);
|
|
contextMenu.Items.Add("Exit", null, (s, e) =>
|
|
{
|
|
notifyIcon.Visible = false;
|
|
Application.Exit();
|
|
lifetime.StopApplication();
|
|
//host.StopAsync().GetAwaiter().GetResult();
|
|
});
|
|
notifyIcon.ContextMenuStrip = contextMenu;
|
|
|
|
notifyIcon.DoubleClick += openFormHandler;
|
|
|
|
bool checkOnInitialStart = config.CheckOnStartup;
|
|
if (checkOnInitialStart)
|
|
{
|
|
|
|
Task.Run(() =>
|
|
{
|
|
appService.CheckAllApps();
|
|
if (configForm != null)
|
|
{
|
|
configForm.UpdateListView();
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
|
|
host.Start();
|
|
|
|
Application.Run(); // Keep app running for tray icon
|
|
|
|
host.StopAsync().GetAwaiter().GetResult();
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|
|
public class SystemTray
|
|
{
|
|
public static bool IsSupported() => System.Windows.Forms.SystemInformation.TerminalServerSession == false;
|
|
}
|
|
} |