aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Server.Mac/AppController.cs
blob: 2da1de5b522cb5d7cee7f4502516a25ef5d7f1e5 (plain)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using MediaBrowser.Controller;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Localization;
using MediaBrowser.Model.Logging;
using MediaBrowser.Server.Startup.Common.Browser;
using System;
using MonoMac.Foundation;
using MonoMac.AppKit;

namespace MediaBrowser.Server.Mac
{
	[Register("AppController")]
	public partial class AppController : NSObject
	{
		private NSMenuItem browseMenuItem;
		private NSMenuItem configureMenuItem;
		private NSMenuItem developerMenuItem;
		private NSMenuItem quitMenuItem;
		private NSMenuItem githubMenuItem;
		private NSMenuItem apiMenuItem;
		private NSMenuItem communityMenuItem;

		public static AppController Instance;

		public AppController()
		{
			Instance = this;
			MainClass.AddDependencies (this);
			ConfigurationManager.ConfigurationUpdated += Instance_ConfigurationUpdated;
		}

		public override void AwakeFromNib()
		{
			var statusItem = NSStatusBar.SystemStatusBar.CreateStatusItem(30);
			statusItem.Menu = statusMenu;
			statusItem.Image = NSImage.ImageNamed("statusicon");
			statusItem.HighlightMode = true;

			statusMenu.RemoveAllItems ();

			browseMenuItem = new NSMenuItem ("Browse Media Library", "b", delegate {
				Browse (this);
			});
			statusMenu.AddItem (browseMenuItem);

			configureMenuItem = new NSMenuItem ("Configure Media Browser", "c", delegate {
				Configure (this);
			});
			statusMenu.AddItem (configureMenuItem);

			developerMenuItem = new NSMenuItem ("Developer Resources");
			statusMenu.AddItem (developerMenuItem);

			var developerMenu = new NSMenu ();
			developerMenuItem.Submenu = developerMenu;

			apiMenuItem = new NSMenuItem ("Api Documentation", "a", delegate {
				ApiDocs (this);
			});
			developerMenu.AddItem (apiMenuItem);

			githubMenuItem = new NSMenuItem ("Github", "g", delegate {
				Github (this);
			});
			developerMenu.AddItem (githubMenuItem);

			communityMenuItem = new NSMenuItem ("Visit Community", "v", delegate {
				Community (this);
			});
			statusMenu.AddItem (communityMenuItem);

			quitMenuItem = new NSMenuItem ("Quit", "q", delegate {
				Quit (this);
			});
			statusMenu.AddItem (quitMenuItem);

			LocalizeText ();
		}

		public IServerApplicationHost AppHost{ get; set;}
		public IServerConfigurationManager ConfigurationManager { get; set;}
		public ILogger Logger{ get; set;}
		public ILocalizationManager Localization { get; set;}

		private void Quit(NSObject sender)
		{
			AppHost.Shutdown();
		}

		private void Community(NSObject sender)
		{
			BrowserLauncher.OpenCommunity(Logger);
		}

		private void Configure(NSObject sender)
		{
			BrowserLauncher.OpenDashboard(AppHost, Logger);
		}

		private void Browse(NSObject sender)
		{
			BrowserLauncher.OpenWebClient(AppHost, Logger);
		}

		private void Github(NSObject sender)
		{
			BrowserLauncher.OpenGithub(Logger);
		}

		private void ApiDocs(NSObject sender)
		{
			BrowserLauncher.OpenSwagger(AppHost, Logger);
		}

		public void Terminate() 
		{
			InvokeOnMainThread (() => NSApplication.SharedApplication.Terminate(this));
		}

		private string _uiCulture;
		/// <summary>
		/// Handles the ConfigurationUpdated event of the Instance control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
		void Instance_ConfigurationUpdated(object sender, EventArgs e)
		{
			if (!string.Equals(ConfigurationManager.Configuration.UICulture, _uiCulture,
				StringComparison.OrdinalIgnoreCase))
			{
				LocalizeText();
			}
		}

		private void LocalizeText()
		{
			_uiCulture = ConfigurationManager.Configuration.UICulture;

			BeginInvokeOnMainThread (LocalizeInternal);
		}

		private void LocalizeInternal() {

			quitMenuItem.Title = Localization.GetLocalizedString("LabelExit");
			communityMenuItem.Title = Localization.GetLocalizedString("LabelVisitCommunity");
			githubMenuItem.Title = Localization.GetLocalizedString("LabelGithub");
			apiMenuItem.Title = Localization.GetLocalizedString("LabelApiDocumentation");
			developerMenuItem.Title = Localization.GetLocalizedString("LabelDeveloperResources");
			browseMenuItem.Title = Localization.GetLocalizedString("LabelBrowseLibrary");
			configureMenuItem.Title = Localization.GetLocalizedString("LabelConfigureMediaBrowser");
		}
	}
}