Jackett/src/Jackett.Tray/Main.cs

172 lines
5.3 KiB
C#

using Jackett;
using Jackett.Utils;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace JackettTray
{
public partial class Main : Form
{
public Main()
{
Hide();
InitializeComponent();
toolStripMenuItemAutoStart.Checked = AutoStart;
toolStripMenuItemAutoStart.CheckedChanged += toolStripMenuItemAutoStart_CheckedChanged;
toolStripMenuItemWebUI.Click += toolStripMenuItemWebUI_Click;
toolStripMenuItemShutdown.Click += toolStripMenuItemShutdown_Click;
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();
}
}
void toolStripMenuItemWebUI_Click(object sender, EventArgs e)
{
Process.Start("http://127.0.0.1:" + Engine.Server.Config.Port);
}
void toolStripMenuItemShutdown_Click(object sender, EventArgs e)
{
Process.GetCurrentProcess().Kill();
}
void toolStripMenuItemAutoStart_CheckedChanged(object sender, EventArgs e)
{
AutoStart = toolStripMenuItemAutoStart.Checked;
}
string ProgramTitle
{
get
{
return Assembly.GetExecutingAssembly().GetName().Name;
}
}
bool AutoStart
{
get
{
return File.Exists(ShortcutPath);
}
set
{
if (value && !AutoStart)
{
CreateShortcut();
}
else if (!value && AutoStart)
{
File.Delete(ShortcutPath);
}
}
}
public string ShortcutPath
{
get
{
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup), "Jackett.lnk");
}
}
private void CreateShortcut()
{
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);
}
}
}
}
}
}