2015-07-22 22:00:52 +00:00
|
|
|
|
using Jackett;
|
|
|
|
|
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;
|
|
|
|
|
|
2015-04-20 05:10:50 +00:00
|
|
|
|
toolStripMenuItemWebUI.Click += toolStripMenuItemWebUI_Click;
|
2015-04-19 07:04:08 +00:00
|
|
|
|
toolStripMenuItemShutdown.Click += toolStripMenuItemShutdown_Click;
|
2015-07-30 17:50:46 +00:00
|
|
|
|
#if __MonoCS__
|
|
|
|
|
// No shortcuts on linux
|
|
|
|
|
#else
|
|
|
|
|
toolStripMenuItemAutoStart.Visible = true;
|
|
|
|
|
#endif
|
2015-04-19 07:04:08 +00:00
|
|
|
|
|
2015-07-22 22:00:52 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2015-04-20 05:10:50 +00:00
|
|
|
|
void toolStripMenuItemWebUI_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
2015-07-22 22:00:52 +00:00
|
|
|
|
Process.Start("http://127.0.0.1:" + Engine.Server.Config.Port);
|
2015-04-20 05:10:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-19 07:04:08 +00:00
|
|
|
|
void toolStripMenuItemShutdown_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
2015-04-24 02:21:19 +00:00
|
|
|
|
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()
|
|
|
|
|
{
|
2015-07-30 17:50:46 +00:00
|
|
|
|
#if __MonoCS__
|
|
|
|
|
// No shortcuts on linux
|
|
|
|
|
#else
|
2015-07-22 22:00:52 +00:00
|
|
|
|
var appPath = Assembly.GetExecutingAssembly().Location;
|
2015-07-11 16:42:00 +00:00
|
|
|
|
var shell = new IWshRuntimeLibrary.WshShell();
|
|
|
|
|
var shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(ShortcutPath);
|
|
|
|
|
shortcut.Description = Assembly.GetExecutingAssembly().GetName().Name;
|
|
|
|
|
shortcut.TargetPath = appPath;
|
2015-07-22 22:00:52 +00:00
|
|
|
|
shortcut.Save();
|
2015-07-30 17:50:46 +00:00
|
|
|
|
#endif
|
2015-07-22 22:00:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
{
|
2015-07-23 23:07:36 +00:00
|
|
|
|
Engine.ProcessService.StartProcessAndLog(consolePath, "--Stop", true);
|
2015-07-22 22:00:52 +00:00
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
{
|
2015-07-23 23:07:36 +00:00
|
|
|
|
Engine.ProcessService.StartProcessAndLog(consolePath, "--Start", true);
|
2015-07-22 22:00:52 +00:00
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|