ServerNotifyIcon.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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.Windows.Forms;
  8. namespace MediaBrowser.ServerApplication
  9. {
  10. public class ServerNotifyIcon : IDisposable
  11. {
  12. bool IsDisposing = false;
  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 readonly ILogger _logger;
  23. private readonly IServerApplicationHost _appHost;
  24. private readonly IServerConfigurationManager _configurationManager;
  25. private readonly ILocalizationManager _localization;
  26. public bool Visible
  27. {
  28. get
  29. {
  30. return notifyIcon1.Visible;
  31. }
  32. set
  33. {
  34. Action act = () => notifyIcon1.Visible = false;
  35. contextMenuStrip1.Invoke(act);
  36. }
  37. }
  38. public ServerNotifyIcon(ILogManager logManager,
  39. IServerApplicationHost appHost,
  40. IServerConfigurationManager configurationManager,
  41. ILocalizationManager localization)
  42. {
  43. _logger = logManager.GetLogger("MainWindow");
  44. _localization = localization;
  45. _appHost = appHost;
  46. _configurationManager = configurationManager;
  47. var components = new System.ComponentModel.Container();
  48. var resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
  49. contextMenuStrip1 = new ContextMenuStrip(components);
  50. notifyIcon1 = new NotifyIcon(components);
  51. cmdExit = new ToolStripMenuItem();
  52. cmdCommunity = new ToolStripMenuItem();
  53. toolStripSeparator1 = new ToolStripSeparator();
  54. cmdRestart = new ToolStripMenuItem();
  55. toolStripSeparator2 = new ToolStripSeparator();
  56. cmdConfigure = new ToolStripMenuItem();
  57. cmdBrowse = new ToolStripMenuItem();
  58. //
  59. // notifyIcon1
  60. //
  61. notifyIcon1.ContextMenuStrip = contextMenuStrip1;
  62. notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
  63. notifyIcon1.Text = "Emby";
  64. notifyIcon1.Visible = true;
  65. //
  66. // contextMenuStrip1
  67. //
  68. contextMenuStrip1.Items.AddRange(new ToolStripItem[] {
  69. cmdBrowse,
  70. cmdConfigure,
  71. toolStripSeparator2,
  72. cmdRestart,
  73. toolStripSeparator1,
  74. cmdCommunity,
  75. cmdExit});
  76. contextMenuStrip1.Name = "contextMenuStrip1";
  77. contextMenuStrip1.ShowCheckMargin = true;
  78. contextMenuStrip1.ShowImageMargin = false;
  79. contextMenuStrip1.Size = new System.Drawing.Size(209, 214);
  80. //
  81. // cmdExit
  82. //
  83. cmdExit.Name = "cmdExit";
  84. cmdExit.Size = new System.Drawing.Size(208, 22);
  85. //
  86. // cmdCommunity
  87. //
  88. cmdCommunity.Name = "cmdCommunity";
  89. cmdCommunity.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. cmdBrowse.Click += cmdBrowse_Click;
  120. _configurationManager.ConfigurationUpdated += Instance_ConfigurationUpdated;
  121. LocalizeText();
  122. if (_appHost.IsFirstRun)
  123. {
  124. Action action = () => notifyIcon1.ShowBalloonTip(5000, "Emby", "Welcome to Emby Server!", ToolTipIcon.Info);
  125. contextMenuStrip1.Invoke(action);
  126. }
  127. notifyIcon1.DoubleClick += notifyIcon1_DoubleClick;
  128. Application.ApplicationExit += Application_ApplicationExit;
  129. }
  130. void Application_ApplicationExit(object sender, EventArgs e)
  131. {
  132. try
  133. {
  134. notifyIcon1.Visible = false;
  135. }
  136. catch
  137. {
  138. }
  139. }
  140. void notifyIcon1_DoubleClick(object sender, EventArgs e)
  141. {
  142. BrowserLauncher.OpenDashboard(_appHost, _logger);
  143. }
  144. private void LocalizeText()
  145. {
  146. _uiCulture = _configurationManager.Configuration.UICulture;
  147. cmdExit.Text = _localization.GetLocalizedString("LabelExit");
  148. cmdCommunity.Text = _localization.GetLocalizedString("LabelVisitCommunity");
  149. cmdBrowse.Text = _localization.GetLocalizedString("LabelBrowseLibrary");
  150. cmdConfigure.Text = _localization.GetLocalizedString("LabelConfigureServer");
  151. cmdRestart.Text = _localization.GetLocalizedString("LabelRestartServer");
  152. }
  153. private string _uiCulture;
  154. /// <summary>
  155. /// Handles the ConfigurationUpdated event of the Instance control.
  156. /// </summary>
  157. /// <param name="sender">The source of the event.</param>
  158. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  159. void Instance_ConfigurationUpdated(object sender, EventArgs e)
  160. {
  161. if (!string.Equals(_configurationManager.Configuration.UICulture, _uiCulture,
  162. StringComparison.OrdinalIgnoreCase))
  163. {
  164. LocalizeText();
  165. }
  166. }
  167. void cmdBrowse_Click(object sender, EventArgs e)
  168. {
  169. BrowserLauncher.OpenWebClient(_appHost, _logger);
  170. }
  171. void cmdCommunity_Click(object sender, EventArgs e)
  172. {
  173. BrowserLauncher.OpenCommunity(_logger);
  174. }
  175. void cmdConfigure_Click(object sender, EventArgs e)
  176. {
  177. BrowserLauncher.OpenDashboard(_appHost, _logger);
  178. }
  179. void cmdRestart_Click(object sender, EventArgs e)
  180. {
  181. _appHost.Restart();
  182. }
  183. void cmdExit_Click(object sender, EventArgs e)
  184. {
  185. _appHost.Shutdown();
  186. }
  187. ~ServerNotifyIcon()
  188. {
  189. Dispose();
  190. }
  191. public void Dispose()
  192. {
  193. if (!IsDisposing)
  194. {
  195. IsDisposing = true;
  196. }
  197. }
  198. }
  199. }