ServerNotifyIcon.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using MediaBrowser.Controller;
  2. using MediaBrowser.Controller.Configuration;
  3. using MediaBrowser.Model.Logging;
  4. using System;
  5. using System.ComponentModel;
  6. using System.Windows.Forms;
  7. using Emby.Server.Implementations.Browser;
  8. using MediaBrowser.Model.Globalization;
  9. namespace MediaBrowser.ServerApplication
  10. {
  11. public class ServerNotifyIcon : IDisposable
  12. {
  13. private NotifyIcon notifyIcon1;
  14. private ContextMenuStrip contextMenuStrip1;
  15. private ToolStripMenuItem cmdExit;
  16. private ToolStripMenuItem cmdBrowse;
  17. private ToolStripMenuItem cmdConfigure;
  18. private ToolStripSeparator toolStripSeparator2;
  19. private ToolStripMenuItem cmdRestart;
  20. private ToolStripSeparator toolStripSeparator1;
  21. private ToolStripMenuItem cmdCommunity;
  22. private ToolStripMenuItem cmdPremiere;
  23. private Container components;
  24. private readonly ILogger _logger;
  25. private readonly IServerApplicationHost _appHost;
  26. private readonly IServerConfigurationManager _configurationManager;
  27. private readonly ILocalizationManager _localization;
  28. public void Invoke(Action action)
  29. {
  30. contextMenuStrip1.Invoke(action);
  31. }
  32. public ServerNotifyIcon(ILogManager logManager,
  33. IServerApplicationHost appHost,
  34. IServerConfigurationManager configurationManager,
  35. ILocalizationManager localization)
  36. {
  37. _logger = logManager.GetLogger("MainWindow");
  38. _localization = localization;
  39. _appHost = appHost;
  40. _configurationManager = configurationManager;
  41. components = new System.ComponentModel.Container();
  42. contextMenuStrip1 = new ContextMenuStrip(components);
  43. notifyIcon1 = new NotifyIcon(components);
  44. cmdExit = new ToolStripMenuItem();
  45. cmdCommunity = new ToolStripMenuItem();
  46. cmdPremiere = new ToolStripMenuItem();
  47. toolStripSeparator1 = new ToolStripSeparator();
  48. cmdRestart = new ToolStripMenuItem();
  49. toolStripSeparator2 = new ToolStripSeparator();
  50. cmdConfigure = new ToolStripMenuItem();
  51. cmdBrowse = new ToolStripMenuItem();
  52. //
  53. // notifyIcon1
  54. //
  55. notifyIcon1.ContextMenuStrip = contextMenuStrip1;
  56. notifyIcon1.Icon = new System.Drawing.Icon(GetType().Assembly.GetManifestResourceStream(GetType().Namespace + ".Icon.ico"));
  57. notifyIcon1.Text = "Emby";
  58. notifyIcon1.Visible = true;
  59. //
  60. // contextMenuStrip1
  61. //
  62. contextMenuStrip1.Items.AddRange(new ToolStripItem[] {
  63. cmdBrowse,
  64. cmdConfigure,
  65. cmdPremiere,
  66. toolStripSeparator2,
  67. cmdRestart,
  68. toolStripSeparator1,
  69. cmdCommunity,
  70. cmdExit});
  71. contextMenuStrip1.Name = "contextMenuStrip1";
  72. contextMenuStrip1.ShowCheckMargin = true;
  73. contextMenuStrip1.ShowImageMargin = false;
  74. contextMenuStrip1.Size = new System.Drawing.Size(209, 214);
  75. //
  76. // cmdExit
  77. //
  78. cmdExit.Name = "cmdExit";
  79. cmdExit.Size = new System.Drawing.Size(208, 22);
  80. //
  81. // cmdCommunity
  82. //
  83. cmdCommunity.Name = "cmdCommunity";
  84. cmdCommunity.Size = new System.Drawing.Size(208, 22);
  85. //
  86. // cmdPremiere
  87. //
  88. cmdPremiere.Name = "cmdPremiere";
  89. cmdPremiere.Size = new System.Drawing.Size(208, 22);
  90. //
  91. // toolStripSeparator1
  92. //
  93. toolStripSeparator1.Name = "toolStripSeparator1";
  94. toolStripSeparator1.Size = new System.Drawing.Size(205, 6);
  95. //
  96. // cmdRestart
  97. //
  98. cmdRestart.Name = "cmdRestart";
  99. cmdRestart.Size = new System.Drawing.Size(208, 22);
  100. //
  101. // toolStripSeparator2
  102. //
  103. toolStripSeparator2.Name = "toolStripSeparator2";
  104. toolStripSeparator2.Size = new System.Drawing.Size(205, 6);
  105. //
  106. // cmdConfigure
  107. //
  108. cmdConfigure.Name = "cmdConfigure";
  109. cmdConfigure.Size = new System.Drawing.Size(208, 22);
  110. //
  111. // cmdBrowse
  112. //
  113. cmdBrowse.Name = "cmdBrowse";
  114. cmdBrowse.Size = new System.Drawing.Size(208, 22);
  115. cmdExit.Click += cmdExit_Click;
  116. cmdRestart.Click += cmdRestart_Click;
  117. cmdConfigure.Click += cmdConfigure_Click;
  118. cmdCommunity.Click += cmdCommunity_Click;
  119. cmdPremiere.Click += cmdPremiere_Click;
  120. cmdBrowse.Click += cmdBrowse_Click;
  121. _configurationManager.ConfigurationUpdated += Instance_ConfigurationUpdated;
  122. LocalizeText();
  123. notifyIcon1.DoubleClick += notifyIcon1_DoubleClick;
  124. }
  125. void notifyIcon1_DoubleClick(object sender, EventArgs e)
  126. {
  127. BrowserLauncher.OpenDashboard(_appHost);
  128. }
  129. private void LocalizeText()
  130. {
  131. _uiCulture = _configurationManager.Configuration.UICulture;
  132. cmdExit.Text = _localization.GetLocalizedString("LabelExit");
  133. cmdCommunity.Text = _localization.GetLocalizedString("LabelVisitCommunity");
  134. cmdPremiere.Text = _localization.GetLocalizedString("Emby Premiere");
  135. cmdBrowse.Text = _localization.GetLocalizedString("LabelBrowseLibrary");
  136. cmdConfigure.Text = _localization.GetLocalizedString("LabelConfigureServer");
  137. cmdRestart.Text = _localization.GetLocalizedString("LabelRestartServer");
  138. }
  139. private string _uiCulture;
  140. /// <summary>
  141. /// Handles the ConfigurationUpdated event of the Instance control.
  142. /// </summary>
  143. /// <param name="sender">The source of the event.</param>
  144. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  145. void Instance_ConfigurationUpdated(object sender, EventArgs e)
  146. {
  147. if (!string.Equals(_configurationManager.Configuration.UICulture, _uiCulture,
  148. StringComparison.OrdinalIgnoreCase))
  149. {
  150. LocalizeText();
  151. }
  152. }
  153. void cmdBrowse_Click(object sender, EventArgs e)
  154. {
  155. BrowserLauncher.OpenWebClient(_appHost);
  156. }
  157. void cmdPremiere_Click(object sender, EventArgs e)
  158. {
  159. BrowserLauncher.OpenEmbyPremiere(_appHost);
  160. }
  161. void cmdCommunity_Click(object sender, EventArgs e)
  162. {
  163. BrowserLauncher.OpenCommunity(_appHost);
  164. }
  165. void cmdConfigure_Click(object sender, EventArgs e)
  166. {
  167. BrowserLauncher.OpenDashboard(_appHost);
  168. }
  169. void cmdRestart_Click(object sender, EventArgs e)
  170. {
  171. _appHost.Restart();
  172. }
  173. void cmdExit_Click(object sender, EventArgs e)
  174. {
  175. _appHost.Shutdown();
  176. }
  177. public void Dispose()
  178. {
  179. Dispose(true);
  180. }
  181. protected virtual void Dispose(bool disposing)
  182. {
  183. if (disposing)
  184. {
  185. if (notifyIcon1 != null)
  186. {
  187. notifyIcon1.Visible = false;
  188. notifyIcon1.Dispose();
  189. notifyIcon1 = null;
  190. }
  191. if (components != null)
  192. {
  193. components.Dispose();
  194. }
  195. }
  196. }
  197. }
  198. }