MainWindow.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. using MahApps.Metro.Controls;
  2. using MediaBrowser.Common;
  3. using MediaBrowser.Controller;
  4. using MediaBrowser.Controller.Configuration;
  5. using MediaBrowser.Controller.Entities;
  6. using MediaBrowser.Controller.Library;
  7. using MediaBrowser.Model.Logging;
  8. using MediaBrowser.Model.Serialization;
  9. using MediaBrowser.ServerApplication.Logging;
  10. using System;
  11. using System.ComponentModel;
  12. using System.Diagnostics;
  13. using System.Linq;
  14. using System.Windows;
  15. using System.Windows.Threading;
  16. namespace MediaBrowser.ServerApplication
  17. {
  18. /// <summary>
  19. /// Interaction logic for MainWindow.xaml
  20. /// </summary>
  21. public partial class MainWindow : Window, INotifyPropertyChanged
  22. {
  23. /// <summary>
  24. /// The _logger
  25. /// </summary>
  26. private readonly ILogger _logger;
  27. /// <summary>
  28. /// The _app host
  29. /// </summary>
  30. private readonly IApplicationHost _appHost;
  31. /// <summary>
  32. /// The _log manager
  33. /// </summary>
  34. private readonly ILogManager _logManager;
  35. /// <summary>
  36. /// The _configuration manager
  37. /// </summary>
  38. private readonly IServerConfigurationManager _configurationManager;
  39. private readonly IUserManager _userManager;
  40. private readonly ILibraryManager _libraryManager;
  41. private readonly IJsonSerializer _jsonSerializer;
  42. private readonly IDisplayPreferencesManager _displayPreferencesManager;
  43. /// <summary>
  44. /// Initializes a new instance of the <see cref="MainWindow" /> class.
  45. /// </summary>
  46. /// <param name="logManager">The log manager.</param>
  47. /// <param name="appHost">The app host.</param>
  48. /// <param name="configurationManager">The configuration manager.</param>
  49. /// <param name="userManager">The user manager.</param>
  50. /// <param name="libraryManager">The library manager.</param>
  51. /// <param name="jsonSerializer">The json serializer.</param>
  52. /// <param name="displayPreferencesManager">The display preferences manager.</param>
  53. /// <exception cref="System.ArgumentNullException">logger</exception>
  54. public MainWindow(ILogManager logManager, IApplicationHost appHost, IServerConfigurationManager configurationManager, IUserManager userManager, ILibraryManager libraryManager, IJsonSerializer jsonSerializer, IDisplayPreferencesManager displayPreferencesManager)
  55. {
  56. if (logManager == null)
  57. {
  58. throw new ArgumentNullException("logManager");
  59. }
  60. if (appHost == null)
  61. {
  62. throw new ArgumentNullException("appHost");
  63. }
  64. if (configurationManager == null)
  65. {
  66. throw new ArgumentNullException("configurationManager");
  67. }
  68. _logger = logManager.GetLogger("MainWindow");
  69. _appHost = appHost;
  70. _logManager = logManager;
  71. _configurationManager = configurationManager;
  72. _userManager = userManager;
  73. _libraryManager = libraryManager;
  74. _jsonSerializer = jsonSerializer;
  75. _displayPreferencesManager = displayPreferencesManager;
  76. InitializeComponent();
  77. Loaded += MainWindowLoaded;
  78. }
  79. /// <summary>
  80. /// Mains the window loaded.
  81. /// </summary>
  82. /// <param name="sender">The sender.</param>
  83. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  84. void MainWindowLoaded(object sender, RoutedEventArgs e)
  85. {
  86. DataContext = this;
  87. UpdateButtons();
  88. LoadLogWindow(null, EventArgs.Empty);
  89. _logManager.LoggerLoaded += LoadLogWindow;
  90. _configurationManager.ConfigurationUpdated += Instance_ConfigurationUpdated;
  91. if (_appHost.IsFirstRun)
  92. {
  93. Dispatcher.InvokeAsync(() => MbTaskbarIcon.ShowBalloonTip("Media Browser", "Welcome to Media Browser Server!", Hardcodet.Wpf.TaskbarNotification.BalloonIcon.None));
  94. }
  95. }
  96. /// <summary>
  97. /// Handles the ConfigurationUpdated event of the Instance control.
  98. /// </summary>
  99. /// <param name="sender">The source of the event.</param>
  100. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  101. void Instance_ConfigurationUpdated(object sender, EventArgs e)
  102. {
  103. UpdateButtons();
  104. Dispatcher.InvokeAsync(() =>
  105. {
  106. var logWindow = App.Instance.Windows.OfType<LogWindow>().FirstOrDefault();
  107. if ((logWindow == null && _configurationManager.Configuration.ShowLogWindow) || (logWindow != null && !_configurationManager.Configuration.ShowLogWindow))
  108. {
  109. _logManager.ReloadLogger(_configurationManager.Configuration.EnableDebugLevelLogging ? LogSeverity.Debug : LogSeverity.Info);
  110. }
  111. });
  112. }
  113. private void UpdateButtons()
  114. {
  115. Dispatcher.InvokeAsync(() =>
  116. {
  117. var developerToolsVisibility = _configurationManager.Configuration.EnableDeveloperTools
  118. ? Visibility.Visible
  119. : Visibility.Collapsed;
  120. separatorDeveloperTools.Visibility = developerToolsVisibility;
  121. cmdReloadServer.Visibility = developerToolsVisibility;
  122. cmOpenExplorer.Visibility = developerToolsVisibility;
  123. });
  124. }
  125. /// <summary>
  126. /// Loads the log window.
  127. /// </summary>
  128. /// <param name="sender">The sender.</param>
  129. /// <param name="args">The <see cref="EventArgs" /> instance containing the event data.</param>
  130. void LoadLogWindow(object sender, EventArgs args)
  131. {
  132. CloseLogWindow();
  133. Dispatcher.InvokeAsync(() =>
  134. {
  135. // Add our log window if specified
  136. if (_configurationManager.Configuration.ShowLogWindow)
  137. {
  138. Trace.Listeners.Add(new WindowTraceListener(new LogWindow(_logManager)));
  139. }
  140. else
  141. {
  142. Trace.Listeners.Remove("MBLogWindow");
  143. }
  144. // Set menu option indicator
  145. cmShowLogWindow.IsChecked = _configurationManager.Configuration.ShowLogWindow;
  146. }, DispatcherPriority.Normal);
  147. }
  148. /// <summary>
  149. /// Closes the log window.
  150. /// </summary>
  151. void CloseLogWindow()
  152. {
  153. Dispatcher.InvokeAsync(() =>
  154. {
  155. foreach (var win in Application.Current.Windows.OfType<LogWindow>())
  156. {
  157. win.Close();
  158. }
  159. });
  160. }
  161. /// <summary>
  162. /// Handles the Click event of the cmdApiDocs control.
  163. /// </summary>
  164. /// <param name="sender">The source of the event.</param>
  165. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  166. void cmdApiDocs_Click(object sender, EventArgs e)
  167. {
  168. App.OpenUrl("http://localhost:" + _configurationManager.Configuration.HttpServerPortNumber + "/" +
  169. Kernel.Instance.WebApplicationName + "/metadata");
  170. }
  171. void cmdSwaggerApiDocs_Click(object sender, EventArgs e)
  172. {
  173. App.OpenUrl("http://localhost:" + _configurationManager.Configuration.HttpServerPortNumber + "/" +
  174. Kernel.Instance.WebApplicationName + "/swagger-ui/index.html");
  175. }
  176. void cmdGithubWiki_Click(object sender, EventArgs e)
  177. {
  178. App.OpenUrl("https://github.com/MediaBrowser/MediaBrowser/wiki");
  179. }
  180. /// <summary>
  181. /// Occurs when [property changed].
  182. /// </summary>
  183. public event PropertyChangedEventHandler PropertyChanged;
  184. /// <summary>
  185. /// Called when [property changed].
  186. /// </summary>
  187. /// <param name="info">The info.</param>
  188. public void OnPropertyChanged(String info)
  189. {
  190. if (PropertyChanged != null)
  191. {
  192. try
  193. {
  194. PropertyChanged(this, new PropertyChangedEventArgs(info));
  195. }
  196. catch (Exception ex)
  197. {
  198. _logger.ErrorException("Error in event handler", ex);
  199. }
  200. }
  201. }
  202. #region Context Menu events
  203. /// <summary>
  204. /// Handles the click event of the cmOpenExplorer control.
  205. /// </summary>
  206. /// <param name="sender">The source of the event.</param>
  207. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  208. private void cmOpenExplorer_click(object sender, RoutedEventArgs e)
  209. {
  210. new LibraryExplorer(_jsonSerializer, _logger, _appHost, _userManager, _libraryManager, _displayPreferencesManager).Show();
  211. }
  212. /// <summary>
  213. /// Handles the click event of the cmOpenDashboard control.
  214. /// </summary>
  215. /// <param name="sender">The source of the event.</param>
  216. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  217. private void cmOpenDashboard_click(object sender, RoutedEventArgs e)
  218. {
  219. var user = _userManager.Users.FirstOrDefault(u => u.Configuration.IsAdministrator);
  220. OpenDashboard(user);
  221. }
  222. /// <summary>
  223. /// Opens the dashboard.
  224. /// </summary>
  225. private void OpenDashboard(User loggedInUser)
  226. {
  227. App.OpenDashboardPage("dashboard.html", loggedInUser, _configurationManager);
  228. }
  229. /// <summary>
  230. /// Handles the click event of the cmVisitCT control.
  231. /// </summary>
  232. /// <param name="sender">The source of the event.</param>
  233. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  234. private void cmVisitCT_click(object sender, RoutedEventArgs e)
  235. {
  236. App.OpenUrl("http://community.mediabrowser.tv/");
  237. }
  238. /// <summary>
  239. /// Handles the click event of the cmdBrowseLibrary control.
  240. /// </summary>
  241. /// <param name="sender">The source of the event.</param>
  242. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  243. private void cmdBrowseLibrary_click(object sender, RoutedEventArgs e)
  244. {
  245. var user = _userManager.Users.FirstOrDefault(u => u.Configuration.IsAdministrator);
  246. App.OpenDashboardPage("index.html", user, _configurationManager);
  247. }
  248. /// <summary>
  249. /// Handles the click event of the cmExit control.
  250. /// </summary>
  251. /// <param name="sender">The source of the event.</param>
  252. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  253. private void cmExit_click(object sender, RoutedEventArgs e)
  254. {
  255. Application.Current.Shutdown();
  256. }
  257. /// <summary>
  258. /// Handles the click event of the cmdReloadServer control.
  259. /// </summary>
  260. /// <param name="sender">The source of the event.</param>
  261. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  262. private void cmdReloadServer_click(object sender, RoutedEventArgs e)
  263. {
  264. App.Instance.Restart();
  265. }
  266. /// <summary>
  267. /// Handles the click event of the CmShowLogWindow control.
  268. /// </summary>
  269. /// <param name="sender">The source of the event.</param>
  270. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  271. private void CmShowLogWindow_click(object sender, RoutedEventArgs e)
  272. {
  273. _configurationManager.Configuration.ShowLogWindow = !_configurationManager.Configuration.ShowLogWindow;
  274. _configurationManager.SaveConfiguration();
  275. LoadLogWindow(sender, e);
  276. }
  277. #endregion
  278. }
  279. }