ServerNotifyIcon.cs 7.1 KB

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