Jackett/src/Jackett.Tray/Main.cs

189 lines
5.9 KiB
C#
Raw Normal View History

using Jackett;
using Jackett.Common.Models.Config;
using Jackett.Utils;
using Microsoft.Win32;
2015-04-19 07:04:08 +00:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
2015-07-11 16:42:00 +00:00
using System.IO;
2015-04-19 07:04:08 +00:00
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
2015-07-19 00:27:41 +00:00
namespace JackettTray
2015-04-19 07:04:08 +00:00
{
public partial class Main : Form
{
public Main()
{
Hide();
InitializeComponent();
toolStripMenuItemAutoStart.Checked = AutoStart;
toolStripMenuItemAutoStart.CheckedChanged += toolStripMenuItemAutoStart_CheckedChanged;
toolStripMenuItemWebUI.Click += toolStripMenuItemWebUI_Click;
2015-04-19 07:04:08 +00:00
toolStripMenuItemShutdown.Click += toolStripMenuItemShutdown_Click;
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
toolStripMenuItemAutoStart.Visible = true;
}
Engine.BuildContainer(new RuntimeSettings(),new WebApi2Module());
Engine.Server.Initalize();
if (!Engine.ServiceConfig.ServiceExists())
{
// We are not installed as a service so just the web server too and run from the tray.
Engine.Logger.Info("Starting server from tray");
Engine.Server.Start();
}
2015-11-18 19:36:37 +00:00
Task.Factory.StartNew(WaitForEvent);
}
private void WaitForEvent()
{
Engine.LockService.WaitForSignal();
Application.Exit();
2015-04-19 07:04:08 +00:00
}
void toolStripMenuItemWebUI_Click(object sender, EventArgs e)
{
Process.Start("http://127.0.0.1:" + Engine.ServerConfig.Port);
}
2015-04-19 07:04:08 +00:00
void toolStripMenuItemShutdown_Click(object sender, EventArgs e)
{
Process.GetCurrentProcess().Kill();
2015-04-19 07:04:08 +00:00
}
void toolStripMenuItemAutoStart_CheckedChanged(object sender, EventArgs e)
{
AutoStart = toolStripMenuItemAutoStart.Checked;
}
string ProgramTitle
{
get
{
return Assembly.GetExecutingAssembly().GetName().Name;
}
}
bool AutoStart
{
get
{
2015-07-11 16:42:00 +00:00
return File.Exists(ShortcutPath);
2015-04-19 07:04:08 +00:00
}
set
{
if (value && !AutoStart)
2015-07-11 16:42:00 +00:00
{
CreateShortcut();
}
2015-04-19 07:04:08 +00:00
else if (!value && AutoStart)
2015-07-11 16:42:00 +00:00
{
File.Delete(ShortcutPath);
}
2015-04-19 07:04:08 +00:00
}
}
2015-07-11 16:42:00 +00:00
public string ShortcutPath
{
get
{
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup), "Jackett.lnk");
}
}
private void CreateShortcut()
{
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
var appPath = Assembly.GetExecutingAssembly().Location;
var shell = new IWshRuntimeLibrary.WshShell();
var shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(ShortcutPath);
shortcut.Description = Assembly.GetExecutingAssembly().GetName().Name;
shortcut.TargetPath = appPath;
shortcut.Save();
}
}
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
if (Engine.ServiceConfig.ServiceExists())
{
backgroundMenuItem.Visible = true;
serviceControlMenuItem.Visible = true;
toolStripSeparator1.Visible = true;
toolStripSeparator2.Visible = true;
if (Engine.ServiceConfig.ServiceRunning())
{
serviceControlMenuItem.Text = "Stop background service";
} else
{
serviceControlMenuItem.Text = "Start background service";
}
toolStripMenuItemShutdown.Text = "Close tray icon";
} else
{
backgroundMenuItem.Visible = false;
serviceControlMenuItem.Visible = false;
toolStripSeparator1.Visible = false;
toolStripSeparator2.Visible = false;
toolStripMenuItemShutdown.Text = "Shutdown";
}
}
private void serviceControlMenuItem_Click(object sender, EventArgs e)
{
var consolePath = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "JackettConsole.exe");
if (Engine.ServiceConfig.ServiceRunning())
{
if (ServerUtil.IsUserAdministrator())
{
Engine.ServiceConfig.Stop();
} else
{
try
{
Engine.ProcessService.StartProcessAndLog(consolePath, "--Stop", true);
}
catch
{
MessageBox.Show("Failed to get admin rights to stop the service.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
else
{
if (ServerUtil.IsUserAdministrator())
{
Engine.ServiceConfig.Start();
}
else
{
try
{
Engine.ProcessService.StartProcessAndLog(consolePath, "--Start", true);
}
catch
{
MessageBox.Show("Failed to get admin rights to start the service.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
2015-07-11 16:42:00 +00:00
}
2015-04-19 07:04:08 +00:00
}
2015-07-19 17:18:54 +00:00
}