MainForm.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using MediaBrowser.Controller;
  2. using MediaBrowser.Controller.Configuration;
  3. using MediaBrowser.Controller.Library;
  4. using MediaBrowser.Controller.Persistence;
  5. using MediaBrowser.Model.Logging;
  6. using MediaBrowser.Model.Serialization;
  7. using MediaBrowser.ServerApplication.Logging;
  8. using MediaBrowser.ServerApplication.Native;
  9. using System;
  10. using System.Diagnostics;
  11. using System.Windows.Forms;
  12. namespace MediaBrowser.ServerApplication
  13. {
  14. public partial class MainForm : Form
  15. {
  16. private readonly ILogger _logger;
  17. private readonly IServerApplicationHost _appHost;
  18. private readonly ILogManager _logManager;
  19. private readonly IServerConfigurationManager _configurationManager;
  20. private readonly IUserManager _userManager;
  21. private readonly ILibraryManager _libraryManager;
  22. private readonly IJsonSerializer _jsonSerializer;
  23. private readonly IDisplayPreferencesRepository _displayPreferencesManager;
  24. private readonly IItemRepository _itemRepository;
  25. private LogForm _logForm;
  26. public MainForm(ILogManager logManager, IServerApplicationHost appHost, IServerConfigurationManager configurationManager, IUserManager userManager, ILibraryManager libraryManager, IJsonSerializer jsonSerializer, IDisplayPreferencesRepository displayPreferencesManager, IItemRepository itemRepo)
  27. {
  28. InitializeComponent();
  29. _logger = logManager.GetLogger("MainWindow");
  30. _itemRepository = itemRepo;
  31. _appHost = appHost;
  32. _logManager = logManager;
  33. _configurationManager = configurationManager;
  34. _userManager = userManager;
  35. _libraryManager = libraryManager;
  36. _jsonSerializer = jsonSerializer;
  37. _displayPreferencesManager = displayPreferencesManager;
  38. cmdExit.Click += cmdExit_Click;
  39. cmdRestart.Click += cmdRestart_Click;
  40. cmdLogWindow.Click += cmdLogWindow_Click;
  41. cmdConfigure.Click += cmdConfigure_Click;
  42. cmdCommunity.Click += cmdCommunity_Click;
  43. cmdBrowse.Click += cmdBrowse_Click;
  44. cmdLibraryExplorer.Click += cmdLibraryExplorer_Click;
  45. cmdSwagger.Click += cmdSwagger_Click;
  46. cmdStandardDocs.Click += cmdStandardDocs_Click;
  47. cmdGtihub.Click += cmdGtihub_Click;
  48. }
  49. protected override void OnLoad(EventArgs e)
  50. {
  51. base.OnLoad(e);
  52. LoadLogWindow(null, EventArgs.Empty);
  53. _logManager.LoggerLoaded += LoadLogWindow;
  54. _configurationManager.ConfigurationUpdated += Instance_ConfigurationUpdated;
  55. if (_appHost.IsFirstRun)
  56. {
  57. Action action = () => notifyIcon1.ShowBalloonTip(5000, "Media Browser", "Welcome to Media Browser Server!", ToolTipIcon.Info);
  58. Invoke(action);
  59. }
  60. }
  61. /// <summary>
  62. /// Handles the ConfigurationUpdated event of the Instance control.
  63. /// </summary>
  64. /// <param name="sender">The source of the event.</param>
  65. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  66. void Instance_ConfigurationUpdated(object sender, EventArgs e)
  67. {
  68. Action action = () =>
  69. {
  70. var isLogWindowOpen = _logForm != null;
  71. if ((!isLogWindowOpen && _configurationManager.Configuration.ShowLogWindow) ||
  72. (isLogWindowOpen && !_configurationManager.Configuration.ShowLogWindow))
  73. {
  74. _logManager.ReloadLogger(_configurationManager.Configuration.EnableDebugLevelLogging
  75. ? LogSeverity.Debug
  76. : LogSeverity.Info);
  77. }
  78. };
  79. Invoke(action);
  80. }
  81. /// <summary>
  82. /// Loads the log window.
  83. /// </summary>
  84. /// <param name="sender">The sender.</param>
  85. /// <param name="args">The <see cref="EventArgs" /> instance containing the event data.</param>
  86. void LoadLogWindow(object sender, EventArgs args)
  87. {
  88. CloseLogWindow();
  89. Action action = () =>
  90. {
  91. // Add our log window if specified
  92. if (_configurationManager.Configuration.ShowLogWindow)
  93. {
  94. _logForm = new LogForm(_logManager);
  95. Trace.Listeners.Add(new WindowTraceListener(_logForm));
  96. }
  97. else
  98. {
  99. Trace.Listeners.Remove("MBLogWindow");
  100. }
  101. // Set menu option indicator
  102. cmdLogWindow.Checked = _configurationManager.Configuration.ShowLogWindow;
  103. };
  104. Invoke(action);
  105. }
  106. /// <summary>
  107. /// Closes the log window.
  108. /// </summary>
  109. void CloseLogWindow()
  110. {
  111. if (_logForm != null)
  112. {
  113. _logForm.ShutDown();
  114. }
  115. }
  116. void cmdBrowse_Click(object sender, EventArgs e)
  117. {
  118. BrowserLauncher.OpenWebClient(_userManager, _configurationManager, _appHost, _logger);
  119. }
  120. void cmdCommunity_Click(object sender, EventArgs e)
  121. {
  122. BrowserLauncher.OpenCommunity(_logger);
  123. }
  124. void cmdConfigure_Click(object sender, EventArgs e)
  125. {
  126. BrowserLauncher.OpenDashboard(_userManager, _configurationManager, _appHost, _logger);
  127. }
  128. void cmdLogWindow_Click(object sender, EventArgs e)
  129. {
  130. _configurationManager.Configuration.ShowLogWindow = !_configurationManager.Configuration.ShowLogWindow;
  131. _configurationManager.SaveConfiguration();
  132. LoadLogWindow(sender, e);
  133. }
  134. void cmdLibraryExplorer_Click(object sender, EventArgs e)
  135. {
  136. new LibraryViewer(_jsonSerializer, _userManager, _libraryManager, _displayPreferencesManager, _itemRepository).Show();
  137. }
  138. void cmdRestart_Click(object sender, EventArgs e)
  139. {
  140. _appHost.Restart();
  141. }
  142. void cmdExit_Click(object sender, EventArgs e)
  143. {
  144. Close();
  145. _appHost.Shutdown();
  146. }
  147. void cmdGtihub_Click(object sender, EventArgs e)
  148. {
  149. BrowserLauncher.OpenGithub(_logger);
  150. }
  151. void cmdStandardDocs_Click(object sender, EventArgs e)
  152. {
  153. BrowserLauncher.OpenStandardApiDocumentation(_configurationManager, _appHost, _logger);
  154. }
  155. void cmdSwagger_Click(object sender, EventArgs e)
  156. {
  157. BrowserLauncher.OpenSwagger(_configurationManager, _appHost, _logger);
  158. }
  159. }
  160. }