PageRenderTime 61ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/Application/GUI/Controls/ControlPanel.xaml.cs

http://yet-another-music-application.googlecode.com/
C# | 1811 lines | 1188 code | 204 blank | 419 comment | 161 complexity | c863f3adef0c7c9a3f31719f86eef2de MD5 | raw file
  1. /**
  2. * ControlPanel.xaml.cs
  3. *
  4. * The "Control Panel" screen used to show all the preferences
  5. * of Stoffi.
  6. *
  7. * * * * * * * * *
  8. *
  9. * This code is part of the Stoffi Music Player Project.
  10. * Visit our website at: stoffiplayer.com
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 3 of the License, or (at your option) any later version.
  16. *
  17. * See stoffiplayer.com/license for more information.
  18. **/
  19. using System;
  20. using System.Collections;
  21. using System.Collections.Generic;
  22. using System.Linq;
  23. using System.Text;
  24. using System.Windows;
  25. using System.Windows.Controls;
  26. using System.Windows.Data;
  27. using System.Windows.Documents;
  28. using System.Windows.Input;
  29. using System.Windows.Media;
  30. using System.Windows.Media.Imaging;
  31. using System.Windows.Navigation;
  32. using System.Windows.Shapes;
  33. using System.Windows.Threading;
  34. using System.Threading;
  35. using System.Globalization;
  36. using Tomers.WPF.Localization;
  37. namespace Stoffi
  38. {
  39. /// <summary>
  40. /// Interaction logic for ControlPanel.xaml
  41. /// </summary>
  42. public partial class ControlPanel : UserControl
  43. {
  44. #region Members
  45. public StoffiWindow ParentWindow;
  46. private OpenAddPolicy openAddPolicy;
  47. private OpenPlayPolicy openFilePlay;
  48. private UpgradePolicy upgradePolicy;
  49. private SearchPolicy searchPolicy;
  50. private Boolean waitingForShortcut = false;
  51. private Button currentShortcutButton = null;
  52. private KeyboardShortcut currentShortcut = null;
  53. private List<Key> currentPressedKeys = new List<Key>();
  54. public List<TextBlock> ShortcutTitles = new List<TextBlock>();
  55. public List<TextBlock> ShortcutDescriptions = new List<TextBlock>();
  56. public List<Label> ShortcutLabels = new List<Label>();
  57. private Hashtable shortcutButtons = new Hashtable();
  58. private Hashtable shortcutCheckBoxes = new Hashtable();
  59. private Hashtable tabs = new Hashtable();
  60. private Hashtable tabLinks = new Hashtable();
  61. private MenuItem menuToggle;
  62. public MenuItem menuRemove;
  63. private ContextMenu sourceMenu;
  64. public Label GlobalLabel = new Label();
  65. #endregion
  66. #region Properties
  67. /// <summary>
  68. ///
  69. /// </summary>
  70. public OpenAddPolicy ExternalClickAdd
  71. {
  72. get { return openAddPolicy; }
  73. set
  74. {
  75. openAddPolicy = value;
  76. SettingsManager.OpenAddPolicy = value;
  77. Save();
  78. }
  79. }
  80. /// <summary>
  81. ///
  82. /// </summary>
  83. public OpenPlayPolicy ExternalClickPlay
  84. {
  85. get { return openFilePlay; }
  86. set
  87. {
  88. openFilePlay = value;
  89. SettingsManager.OpenPlayPolicy = value;
  90. Save();
  91. }
  92. }
  93. /// <summary>
  94. ///
  95. /// </summary>
  96. public UpgradePolicy UpgradePolicy
  97. {
  98. get { return upgradePolicy; }
  99. set
  100. {
  101. upgradePolicy = value;
  102. SettingsManager.UpgradePolicy = value;
  103. PrefDoUpgrade.Visibility = System.Windows.Visibility.Collapsed;
  104. Save();
  105. if (value == Stoffi.UpgradePolicy.Manual)
  106. {
  107. UpgradeManager.Stop();
  108. EnableUpgradeCheck();
  109. }
  110. else // Notify or Automatic
  111. {
  112. PrefCheckForUpgrades.Visibility = System.Windows.Visibility.Collapsed;
  113. UpgradeProgress.Visibility = System.Windows.Visibility.Collapsed;
  114. ThreadStart InitUpgradeManagerThread = delegate()
  115. {
  116. UpgradeManager.Stop();
  117. UpgradeManager.Start();
  118. UpgradeManager.Probe(null, null);
  119. };
  120. Thread um_thread = new Thread(InitUpgradeManagerThread);
  121. um_thread.Priority = ThreadPriority.BelowNormal;
  122. um_thread.Start();
  123. }
  124. }
  125. }
  126. /// <summary>
  127. ///
  128. /// </summary>
  129. public SearchPolicy SearchPolicy
  130. {
  131. get { return searchPolicy; }
  132. set
  133. {
  134. searchPolicy = value;
  135. SettingsManager.SearchPolicy = value;
  136. Save();
  137. }
  138. }
  139. /// <summary>
  140. ///
  141. /// </summary>
  142. public bool ShowOSD
  143. {
  144. get { return SettingsManager.ShowOSD; }
  145. set { SettingsManager.ShowOSD = value; Save(); }
  146. }
  147. /// <summary>
  148. ///
  149. /// </summary>
  150. public bool MinimizeToTray
  151. {
  152. get { return SettingsManager.MinimizeToTray; }
  153. set { SettingsManager.MinimizeToTray = value; Save(); }
  154. }
  155. /// <summary>
  156. /// Gets or sets whether to submit songs when they are played
  157. /// </summary>
  158. public bool SubmitSongs
  159. {
  160. get { return SettingsManager.SubmitSongs; }
  161. set { SettingsManager.SubmitSongs = value; Save(); }
  162. }
  163. /// <summary>
  164. /// Gets or sets whether to submit playlists when they are created
  165. /// </summary>
  166. public bool SubmitPlaylists
  167. {
  168. get { return SettingsManager.SubmitPlaylists; }
  169. set { SettingsManager.SubmitPlaylists = value; Save(); }
  170. }
  171. /// <summary>
  172. /// Gets or sets whether to pause playback while computer is locked
  173. /// </summary>
  174. public bool PauseWhileLocked
  175. {
  176. get { return SettingsManager.PauseWhileLocked; }
  177. set { SettingsManager.PauseWhileLocked = value; Save(); }
  178. }
  179. /// <summary>
  180. /// Gets or sets whether to pause playback while user is logged out
  181. /// </summary>
  182. public bool PauseWhileLoggedOut
  183. {
  184. get { return SettingsManager.PauseWhileLoggedOut; }
  185. set { SettingsManager.PauseWhileLoggedOut = value; Save(); }
  186. }
  187. #endregion
  188. #region Constructor
  189. /// <summary>
  190. /// Creates a control panel
  191. /// </summary>
  192. public ControlPanel()
  193. {
  194. U.L(LogLevel.Debug, "CONTROL PANEL", "Initialize");
  195. InitializeComponent();
  196. U.L(LogLevel.Debug, "CONTROL PANEL", "Initialized");
  197. openAddPolicy = SettingsManager.OpenAddPolicy;
  198. openFilePlay = SettingsManager.OpenPlayPolicy;
  199. upgradePolicy = SettingsManager.UpgradePolicy;
  200. searchPolicy = SettingsManager.SearchPolicy;
  201. this.DataContext = this;
  202. tabs.Add(Tab.General, ControlPanelGeneral);
  203. tabs.Add(Tab.Sources, ControlPanelSources);
  204. tabs.Add(Tab.Services, ControlPanelServices);
  205. tabs.Add(Tab.Shortcuts, ControlPanelShortcuts);
  206. tabs.Add(Tab.About, ControlPanelAbout);
  207. tabLinks.Add(Tab.General, ControlPanelLink_General);
  208. tabLinks.Add(Tab.Sources, ControlPanelLink_Sources);
  209. tabLinks.Add(Tab.Services, ControlPanelLink_Services);
  210. tabLinks.Add(Tab.Shortcuts, ControlPanelLink_Shortcuts);
  211. tabLinks.Add(Tab.About, ControlPanelLink_About);
  212. menuRemove = new MenuItem();
  213. menuRemove.Header = U.T("MenuRemove");
  214. menuRemove.Click += new RoutedEventHandler(menuRemove_Click);
  215. menuToggle = new MenuItem();
  216. menuToggle.Header = U.T("MenuIgnore");
  217. menuToggle.Click += new RoutedEventHandler(menuToggle_Click);
  218. sourceMenu = new ContextMenu();
  219. sourceMenu.Items.Add(menuToggle);
  220. sourceMenu.Items.Add(menuRemove);
  221. SourceList.ContextMenu = sourceMenu;
  222. SettingsManager.PropertyChanged += new PropertyChangedWithValuesEventHandler(SettingsManager_PropertyChanged);
  223. ServiceManager.PropertyChanged += new PropertyChangedWithValuesEventHandler(ServiceManager_PropertyChanged);
  224. ServiceManager.Initialized += new EventHandler(ServiceManager_Initialized);
  225. CloudSyncInterface csi = new CloudSyncInterface();
  226. ServiceBrowser.ObjectForScripting = csi;
  227. U.L(LogLevel.Debug, "CONTROL PANEL", "Created");
  228. }
  229. #endregion
  230. #region Methods
  231. #region Public
  232. /// <summary>
  233. /// Initializes the controls for the shortcuts
  234. /// </summary>
  235. public void InitShortcuts()
  236. {
  237. String[,] categories = new String[4, 3]; // name, translation id (title), translation id (text
  238. categories[0, 0] = "Application";
  239. categories[0, 1] = "ShortcutApplicationTitle";
  240. categories[0, 2] = "ShortcutApplicationText";
  241. categories[1, 0] = "MainWindow";
  242. categories[1, 1] = "ShortcutMainWindowTitle";
  243. categories[1, 2] = "ShortcutMainWindowText";
  244. categories[2, 0] = "MediaCommands";
  245. categories[2, 1] = "ShortcutMediaCommandsTitle";
  246. categories[2, 2] = "ShortcutMediaCommandsText";
  247. categories[3, 0] = "Track";
  248. categories[3, 1] = "ShortcutTrackTitle";
  249. categories[3, 2] = "ShortcutTrackText";
  250. for (int i = 0; i < 4; i++)
  251. {
  252. DockPanel d = new DockPanel() { Margin = new Thickness(25, 15, 0, 5), LastChildFill = true };
  253. TextBlock t = new TextBlock() { Text = U.T(categories[i, 1]) };
  254. t.Tag = categories[i, 1];
  255. DockPanel.SetDock(t, Dock.Left);
  256. ShortcutTitles.Add(t);
  257. d.Children.Add(t);
  258. Separator s = new Separator();
  259. s.Background = Brushes.LightGray;
  260. s.Height = 1;
  261. s.Margin = new Thickness(5, 0, 5, 0);
  262. DockPanel.SetDock(s, Dock.Left);
  263. d.Children.Add(s);
  264. DockPanel.SetDock(d, Dock.Top);
  265. ShortcutPanel.Children.Add(d);
  266. TextBlock tb = new TextBlock();
  267. tb.Margin = new Thickness(50, 5, 0, 5);
  268. tb.TextWrapping = TextWrapping.Wrap;
  269. tb.Inlines.Add(U.T(categories[i, 2]));
  270. tb.Tag = categories[i, 2];
  271. ShortcutDescriptions.Add(tb);
  272. DockPanel.SetDock(tb, Dock.Top);
  273. ShortcutPanel.Children.Add(tb);
  274. GridLengthConverter conv = new GridLengthConverter();
  275. Grid g = new Grid();
  276. g.Margin = new Thickness(50, 5, 0, 5);
  277. g.ColumnDefinitions.Add(new ColumnDefinition() { Width = (GridLength)conv.ConvertFrom(170) });
  278. g.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
  279. g.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
  280. g.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
  281. int j;
  282. KeyboardShortcutProfile profile = SettingsManager.GetCurrentShortcutProfile();
  283. for (j = 0; j < profile.Shortcuts.Count; j++)
  284. g.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
  285. if (categories[i, 0] == "MediaCommands")
  286. {
  287. GlobalLabel.Content = U.T("ShortcutGlobal");
  288. GlobalLabel.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
  289. Grid.SetColumn(GlobalLabel, 2);
  290. Grid.SetRow(GlobalLabel, 0);
  291. g.Children.Add(GlobalLabel);
  292. }
  293. j = 1;
  294. foreach (KeyboardShortcut sc in profile.Shortcuts)
  295. {
  296. // skip now playing
  297. if (sc.Name == "Now playing") continue;
  298. if (sc.Category != categories[i, 0]) continue;
  299. Label l = new Label() { Content = U.T("Shortcut_" + sc.Name.Replace(" ","_")) };
  300. l.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
  301. l.Tag = "Shortcut_" + sc.Name.Replace(" ", "_");
  302. Grid.SetColumn(l, 0);
  303. Grid.SetRow(l, j);
  304. g.Children.Add(l);
  305. ShortcutLabels.Add(l);
  306. Button b = new Button() { Content = sc.Keys, MinWidth = 100 };
  307. b.Name = sc.Category + "_" + sc.Name.Replace(" ", "_");
  308. b.LostFocus += PrefShortcutButton_LostFocus;
  309. b.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
  310. b.Margin = new Thickness(2);
  311. b.Click += PrefShortcutButton_Clicked;
  312. shortcutButtons.Add(b.Name, b);
  313. Grid.SetColumn(b, 1);
  314. Grid.SetRow(b, j);
  315. g.Children.Add(b);
  316. if (categories[i, 0] == "MediaCommands")
  317. {
  318. CheckBox cb = new CheckBox() { IsChecked = sc.IsGlobal, Name = b.Name, ToolTip = U.T("ShortcutGlobal", "ToolTip") };
  319. cb.VerticalAlignment = System.Windows.VerticalAlignment.Center;
  320. cb.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
  321. cb.Margin = new Thickness(10, 0, 10, 0);
  322. cb.Click += PrefShortcutGlobal_Clicked;
  323. shortcutCheckBoxes.Add(b.Name, cb);
  324. Grid.SetColumn(cb, 2);
  325. Grid.SetRow(cb, j);
  326. g.Children.Add(cb);
  327. }
  328. j++;
  329. }
  330. DockPanel.SetDock(g, Dock.Top);
  331. ShortcutPanel.Children.Add(g);
  332. }
  333. ((Grid)ShortcutPanel.Children[ShortcutPanel.Children.Count - 1]).Margin = new Thickness(50, 5, 0, 25);
  334. string selTxt = SettingsManager.CurrentShortcutProfile;
  335. int sel = 0;
  336. foreach (KeyboardShortcutProfile p in SettingsManager.ShortcutProfiles)
  337. {
  338. if (selTxt == p.Name)
  339. sel = PrefShortcutProfile.Items.Count;
  340. PrefShortcutProfile.Items.Add(new ComboBoxItem() { Content = p.Name });
  341. }
  342. PrefShortcutProfile.SelectedIndex = sel;
  343. }
  344. /// <summary>
  345. /// Updates the timestamp for the latest check for upgrades
  346. /// </summary>
  347. public void UpdateUpgradeCheck()
  348. {
  349. try
  350. {
  351. string text = Unix2Date(SettingsManager.UpgradeCheck);
  352. AboutUpgradeCheck.Text = text;
  353. if (UpgradeManager.Pending)
  354. {
  355. AboutUpgradePending.Visibility = System.Windows.Visibility.Visible;
  356. ParentWindow.UpgradeButton.Visibility = Visibility.Visible;
  357. }
  358. }
  359. catch
  360. {
  361. AboutUpgradeCheck.Text = U.T("UpgradeNotChecked");
  362. }
  363. if (SettingsManager.UpgradeCheck == 0)
  364. AboutUpgradeCheck.Text = U.T("UpgradeNotChecked");
  365. }
  366. /// <summary>
  367. ///
  368. /// </summary>
  369. /// <param name="timestamp"></param>
  370. /// <returns></returns>
  371. public String Unix2Date(long timestamp)
  372. {
  373. DateTime dt = new DateTime(1970, 1, 1, 0, 0, 0);
  374. dt = dt.AddSeconds(timestamp);
  375. dt = dt.AddSeconds((DateTime.Now - DateTime.UtcNow).TotalSeconds);
  376. return String.Format("{0:ddd, MMM d, yyy HH:mm}", dt);
  377. }
  378. /// <summary>
  379. ///
  380. /// </summary>
  381. /// <param name="timestamp"></param>
  382. /// <returns></returns>
  383. public String Version2String(long timestamp)
  384. {
  385. int v1 = (int)(timestamp / 1000000000);
  386. int v2 = (int)(timestamp / 10000000) % 100;
  387. int v3 = (int)(timestamp / 10000) % 1000;
  388. int v4 = (int)(timestamp % 10000);
  389. return v1 + "." + v2 + "." + v3 + "." + v4;
  390. }
  391. /// <summary>
  392. ///
  393. /// </summary>
  394. public void EnableUpgradeDo()
  395. {
  396. U.L(LogLevel.Debug, "CONTROL", "Enabling upgrade button");
  397. PrefDoUpgrade.Visibility = System.Windows.Visibility.Visible;
  398. PrefDoUpgrade.IsEnabled = true;
  399. PrefDoUpgrade.Content = U.T("GeneralDoUpgrade", "Content");
  400. PrefCheckForUpgrades.Visibility = System.Windows.Visibility.Collapsed;
  401. Restart.Visibility = System.Windows.Visibility.Collapsed;
  402. UpgradeProgress.Visibility = System.Windows.Visibility.Collapsed;
  403. if (ParentWindow.TaskbarItemInfo != null)
  404. ParentWindow.TaskbarItemInfo.ProgressState = System.Windows.Shell.TaskbarItemProgressState.None;
  405. }
  406. /// <summary>
  407. ///
  408. /// </summary>
  409. public void EnableUpgradeCheck()
  410. {
  411. U.L(LogLevel.Debug, "CONTROL", "Enabling upgrade check button");
  412. PrefCheckForUpgrades.Visibility = System.Windows.Visibility.Visible;
  413. PrefCheckForUpgrades.IsEnabled = true;
  414. PrefCheckForUpgrades.Content = U.T("GeneralDoCheck", "Content");
  415. PrefDoUpgrade.Visibility = System.Windows.Visibility.Collapsed;
  416. Restart.Visibility = System.Windows.Visibility.Collapsed;
  417. UpgradeProgress.Visibility = System.Windows.Visibility.Collapsed;
  418. if (ParentWindow.TaskbarItemInfo != null)
  419. ParentWindow.TaskbarItemInfo.ProgressState = System.Windows.Shell.TaskbarItemProgressState.None;
  420. }
  421. /// <summary>
  422. ///
  423. /// </summary>
  424. public void EnableRestart()
  425. {
  426. U.L(LogLevel.Debug, "CONTROL", "Enabling restart button");
  427. Restart.Visibility = System.Windows.Visibility.Visible;
  428. PrefCheckForUpgrades.Visibility = System.Windows.Visibility.Collapsed;
  429. PrefDoUpgrade.Visibility = System.Windows.Visibility.Collapsed;
  430. UpgradeProgress.Visibility = System.Windows.Visibility.Collapsed;
  431. if (ParentWindow.TaskbarItemInfo != null)
  432. ParentWindow.TaskbarItemInfo.ProgressState = System.Windows.Shell.TaskbarItemProgressState.None;
  433. }
  434. /// <summary>
  435. ///
  436. /// </summary>
  437. /// <param name="tab"></param>
  438. public void SwitchTab(Tab tab)
  439. {
  440. foreach (DictionaryEntry c in tabs)
  441. ((ScrollViewer)c.Value).Visibility = ((Tab)c.Key) == tab ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
  442. foreach (DictionaryEntry c in tabLinks)
  443. ((Button)c.Value).Style = ((Tab)c.Key) == tab ? (Style)FindResource("ControlPanelLinkActiveStyle") : (Style)FindResource("ControlPanelLinkStyle");
  444. }
  445. #endregion
  446. #region Private
  447. /// <summary>
  448. ///
  449. /// </summary>
  450. /// <param name="str"></param>
  451. /// <param name="everyWord"></param>
  452. /// <returns></returns>
  453. private String Capitalize(String str, bool everyWord = false)
  454. {
  455. if (everyWord)
  456. {
  457. string[] r = str.Split(' ');
  458. for (int i = 0; i < r.Count(); i++)
  459. r[i] = Capitalize(r[i], false);
  460. return String.Join(" ", r);
  461. }
  462. return str[0].ToString().ToUpper() + str.Substring(1).ToLower();
  463. }
  464. /// <summary>
  465. ///
  466. /// </summary>
  467. private void ResetShortcutButton()
  468. {
  469. if (!waitingForShortcut) return;
  470. waitingForShortcut = false;
  471. currentShortcutButton.Content = currentShortcut.Keys == "" ? U.T("ShortcutNotUsed") : currentShortcut.Keys;
  472. currentShortcutButton.FontStyle = currentShortcut.Keys == "" ? FontStyles.Italic : FontStyles.Normal;
  473. currentShortcut = null;
  474. currentShortcutButton = null;
  475. }
  476. /// <summary>
  477. ///
  478. /// </summary>
  479. /// <param name="name"></param>
  480. /// <returns></returns>
  481. private KeyboardShortcutProfile CreateShortcutProfile(String name = "")
  482. {
  483. KeyboardShortcutProfile profile = new KeyboardShortcutProfile();
  484. bool copy = false;
  485. if (name == "")
  486. {
  487. // create a "CustomX" name
  488. int i=0;
  489. bool found;
  490. while (true)
  491. {
  492. i++;
  493. found = false;
  494. foreach (KeyboardShortcutProfile scp in SettingsManager.ShortcutProfiles)
  495. if (scp.Name == U.T("ShortcutCustom") + i.ToString())
  496. found = true;
  497. if (found) continue;
  498. name = U.T("ShortcutCustom") + i.ToString();
  499. break;
  500. }
  501. copy = true;
  502. }
  503. SettingsManager.InitShortcutProfile(profile, name, false);
  504. if (copy) // copy shortcuts from current profile
  505. {
  506. KeyboardShortcutProfile currentProfile = SettingsManager.GetCurrentShortcutProfile();
  507. foreach (KeyboardShortcut sc in currentProfile.Shortcuts)
  508. {
  509. KeyboardShortcut newSc = SettingsManager.GetKeyboardShortcut(profile, sc.Category, sc.Name);
  510. newSc.Keys = sc.Keys;
  511. }
  512. }
  513. SettingsManager.ShortcutProfiles.Add(profile);
  514. PrefShortcutProfile.Items.Add(new ComboBoxItem() { Content = profile.Name });
  515. PrefShortcutProfile.SelectedIndex = PrefShortcutProfile.Items.Count - 1;
  516. return profile;
  517. }
  518. /// <summary>
  519. ///
  520. /// </summary>
  521. private void Save()
  522. {
  523. // TODO: Try to save the settings in the background without any hickups
  524. /*
  525. ThreadStart SaveThread = delegate()
  526. {
  527. SettingsManager.Save();
  528. };
  529. Thread save_thread = new Thread(SaveThread);
  530. save_thread.Name = "Save Settings Thread";
  531. save_thread.Priority = ThreadPriority.Lowest;
  532. save_thread.Start();
  533. */
  534. }
  535. #endregion
  536. #region Event handlers
  537. /// <summary>
  538. /// Invoked when the service browser is navigating to a new URL.
  539. /// Will attempt to start linking at ServiceManager if the browser
  540. /// is detected to get redirected back to the designated callback URL.
  541. /// </summary>
  542. /// <param name="sender">The sender of the event</param>
  543. /// <param name="e">The event data</param>
  544. private void ServiceBrowser_Navigating(object sender, NavigatingCancelEventArgs e)
  545. {
  546. // hide browser and show loading indicator
  547. if (BrowserContainer.Children.Contains(ServiceBrowser))
  548. BrowserContainer.Children.Remove(ServiceBrowser);
  549. if (!BrowserContainer.Children.Contains(BrowserLoadingIndicator))
  550. BrowserContainer.Children.Add(BrowserLoadingIndicator);
  551. // going to callback with extra parameters
  552. if (e.Uri.AbsoluteUri.StartsWith(ServiceManager.CallbackURL) &&
  553. e.Uri.AbsoluteUri != ServiceManager.CallbackURL &&
  554. e.Uri.AbsoluteUri != ServiceManager.CallbackURLWithAuthParams)
  555. {
  556. ServiceManager.Link(e.Uri.AbsoluteUri);
  557. }
  558. // prevent going to root url
  559. else if (e.Uri.AbsoluteUri == "http://alpha.stoffiplayer.com/")
  560. {
  561. string url = ServiceManager.RequestURL;
  562. ServiceBrowser.Source = new Uri(url);
  563. }
  564. }
  565. /// <summary>
  566. /// Invoked when the service browser has navigated to a new URL.
  567. /// Will hide the browser loading indicator and show the browser.
  568. /// </summary>
  569. /// <param name="sender">The sender of the event</param>
  570. /// <param name="e">The event data</param>
  571. private void ServiceBrowser_Navigated(object sender, NavigationEventArgs e)
  572. {
  573. if (!BrowserContainer.Children.Contains(ServiceBrowser))
  574. BrowserContainer.Children.Add(ServiceBrowser);
  575. if (BrowserContainer.Children.Contains(BrowserLoadingIndicator))
  576. BrowserContainer.Children.Remove(BrowserLoadingIndicator);
  577. }
  578. /// <summary>
  579. /// Invoked when the user clicks on Delink.
  580. /// </summary>
  581. /// <param name="sender">The sender of the event</param>
  582. /// <param name="e">The event data</param>
  583. private void Delink_Click(object sender, RoutedEventArgs e)
  584. {
  585. ServiceManager.Delink();
  586. }
  587. /// <summary>
  588. /// Invoked when the user changes the device name.
  589. /// Will send the new name to the server.
  590. /// </summary>
  591. /// <param name="sender">The sender of the event</param>
  592. /// <param name="e">The event data</param>
  593. private void DeviceName_Edited(object sender, EditableTextBlockEventArgs e)
  594. {
  595. ServiceManager.DeviceName = e.NewText;
  596. }
  597. /// <summary>
  598. /// Invoked when a property changes in the service manager.
  599. /// </summary>
  600. /// <param name="sender">The sender of the event</param>
  601. /// <param name="e">The event data</param>
  602. private void ServiceManager_PropertyChanged(object sender, PropertyChangedWithValuesEventArgs e)
  603. {
  604. Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate()
  605. {
  606. switch (e.PropertyName)
  607. {
  608. case "DeviceName":
  609. DeviceName.Edited -= DeviceName_Edited;
  610. DeviceName.Text = e.NewValue as string;
  611. DeviceName.Edited += DeviceName_Edited;
  612. break;
  613. case "Linked":
  614. if (ServiceManager.Linked)
  615. {
  616. if (!ServiceRootPanel.Children.Contains(ServicePanel))
  617. ServiceRootPanel.Children.Add(ServicePanel);
  618. Delink.Visibility = System.Windows.Visibility.Visible;
  619. DeviceNamePanel.Visibility = System.Windows.Visibility.Visible;
  620. ServiceBrowser.Source = new Uri(ServiceManager.CallbackURL);
  621. }
  622. else
  623. {
  624. if (ServiceRootPanel.Children.Contains(ServicePanel))
  625. ServiceRootPanel.Children.Remove(ServicePanel);
  626. Delink.Visibility = System.Windows.Visibility.Collapsed;
  627. DeviceNamePanel.Visibility = System.Windows.Visibility.Collapsed;
  628. ServiceBrowser.Source = new Uri(ServiceManager.LogoutURL);
  629. }
  630. break;
  631. case "SyncProfiles":
  632. List<int> existingIDs = new List<int>();
  633. // remove old
  634. for (int i=1; i < SynchronizeProfile.Items.Count; i++)
  635. {
  636. ComboBoxItem cbi = SynchronizeProfile.Items[i] as ComboBoxItem;
  637. int id = (int)cbi.Tag;
  638. if (id > 0)
  639. {
  640. if (ServiceManager.SyncProfiles.ContainsKey(id))
  641. {
  642. cbi.Content = ServiceManager.SyncProfiles[id];
  643. existingIDs.Add(id);
  644. }
  645. else
  646. {
  647. SynchronizeProfile.Items.RemoveAt(i);
  648. i--;
  649. }
  650. }
  651. }
  652. for (int i = 0; i < ConfigPanel.Children.Count - 1; i++)
  653. {
  654. StackPanel sp = ConfigPanel.Children[i] as StackPanel;
  655. int id = (int)sp.Tag;
  656. if (id > 0 && !ServiceManager.SyncProfiles.ContainsKey(id))
  657. {
  658. ConfigPanel.Children.RemoveAt(i);
  659. i--;
  660. }
  661. }
  662. // add new
  663. foreach (KeyValuePair<int, string> profile in ServiceManager.SyncProfiles)
  664. {
  665. if (!existingIDs.Contains(profile.Key))
  666. {
  667. ComboBoxItem cbi = new ComboBoxItem();
  668. cbi.Tag = profile.Key;
  669. cbi.Content = profile.Value;
  670. SynchronizeProfile.Items.Add(cbi);
  671. StackPanel sp = new StackPanel();
  672. sp.Orientation = Orientation.Horizontal;
  673. sp.Tag = profile.Key;
  674. EditableTextBlock etb = new EditableTextBlock();
  675. etb.Text = profile.Value;
  676. etb.ClickToEdit = true;
  677. etb.SimpleHover = true;
  678. etb.Width = 150;
  679. etb.Margin = new Thickness(5);
  680. etb.Tag = profile.Key;
  681. etb.Edited += new EditableTextBlockDelegate(ConfigurationName_Edited);
  682. Image img = new Image();
  683. img.Source = new BitmapImage(new Uri("../Images/Buttons/Close.gif", UriKind.RelativeOrAbsolute));
  684. img.MouseLeftButtonDown += new MouseButtonEventHandler(DeleteConfiguration_MouseLeftButtonDown);
  685. img.Width = 8;
  686. img.Height = 8;
  687. img.Margin = new Thickness(10, 0, 0, 0);
  688. img.Cursor = Cursors.Hand;
  689. img.Tag = profile.Key;
  690. img.VerticalAlignment = System.Windows.VerticalAlignment.Center;
  691. sp.Children.Add(etb);
  692. sp.Children.Add(img);
  693. ConfigPanel.Children.Insert(ConfigPanel.Children.Count - 1, sp);
  694. }
  695. }
  696. // select current
  697. if (SettingsManager.SynchronizeID > 0)
  698. {
  699. foreach (ComboBoxItem cbi in SynchronizeProfile.Items)
  700. {
  701. int id = -1;
  702. try { id = (int)cbi.Tag; }
  703. catch { }
  704. if (id == SettingsManager.SynchronizeID)
  705. {
  706. cbi.IsSelected = true;
  707. break;
  708. }
  709. }
  710. }
  711. break;
  712. }
  713. }));
  714. }
  715. /// <summary>
  716. /// Invoked when the user changes size of the control panel
  717. /// </summary>
  718. /// <param name="sender">The sender of the event</param>
  719. /// <param name="e">The event data</param>
  720. private void ControlPanel_SizeChanged(object sender, SizeChangedEventArgs e)
  721. {
  722. ScrollBarVisibility vis = e.NewSize.Width < 600 ? ScrollBarVisibility.Auto : ScrollBarVisibility.Disabled;
  723. //ControlPanelAbout.HorizontalScrollBarVisibility = vis;
  724. ControlPanelGeneral.HorizontalScrollBarVisibility = vis;
  725. ControlPanelShortcuts.HorizontalScrollBarVisibility = vis;
  726. ControlPanelSources.HorizontalScrollBarVisibility = vis;
  727. }
  728. /// <summary>
  729. /// Invoked when the control panel is loaded
  730. /// </summary>
  731. /// <param name="sender">The sender of the event</param>
  732. /// <param name="e">The event data</param>
  733. private void ControlPanel_Loaded(object sender, RoutedEventArgs e)
  734. {
  735. if (System.Windows.Forms.VisualStyles.VisualStyleInformation.DisplayName == "")
  736. {
  737. ControlPanelMain.Background = SystemColors.WindowBrush;
  738. ControlPanelLeft.Background = SystemColors.WindowBrush;
  739. }
  740. AboutChannel.Text = Capitalize(SettingsManager.Channel);
  741. UpdateUpgradeCheck();
  742. AboutVersion.Text = Capitalize(SettingsManager.Release, true); // release name
  743. AboutRelease.Text = Unix2Date(SettingsManager.Version); // release date
  744. AboutStamp.Text = Version2String(SettingsManager.Version); // release version
  745. AboutArch.Text = SettingsManager.Architecture + "-bit";
  746. SettingsManager.Sources.CollectionChanged +=
  747. new System.Collections.Specialized.NotifyCollectionChangedEventHandler(SourceList.ItemsSource_CollectionChanged);
  748. if (SettingsManager.UpgradePolicy == UpgradePolicy.Manual)
  749. PrefCheckForUpgrades.Visibility = System.Windows.Visibility.Visible;
  750. // create languages
  751. foreach (ComboBoxItem cbi in PrefLanguage.Items)
  752. {
  753. if ((string)cbi.Tag == SettingsManager.Language)
  754. {
  755. PrefLanguage.SelectedItem = cbi;
  756. break;
  757. }
  758. }
  759. UpgradePolicyDescriptionConverter uconv = new UpgradePolicyDescriptionConverter();
  760. string upolicy = (string)uconv.Convert(UpgradePolicy, null, null, null);
  761. SearchPolicyDescriptionConverter sconv = new SearchPolicyDescriptionConverter();
  762. string spolicy = (string)sconv.Convert(SearchPolicy, null, null, null);
  763. OpenFileAddDescriptionConverter aconv = new OpenFileAddDescriptionConverter();
  764. string apolicy = (string)aconv.Convert(ExternalClickAdd, null, null, null);
  765. OpenFilePlayDescriptionConverter pconv = new OpenFilePlayDescriptionConverter();
  766. string ppolicy = (string)pconv.Convert(ExternalClickPlay, null, null, null);
  767. foreach (ComboBoxItem cbi in SearchPolicyCombo.Items)
  768. {
  769. if ((string)cbi.Content == spolicy)
  770. {
  771. SearchPolicyCombo.SelectedItem = cbi;
  772. break;
  773. }
  774. }
  775. foreach (ComboBoxItem cbi in UpgradePolicyCombo.Items)
  776. {
  777. if ((string)cbi.Content == upolicy)
  778. {
  779. UpgradePolicyCombo.SelectedItem = cbi;
  780. break;
  781. }
  782. }
  783. foreach (ComboBoxItem cbi in AddPolicyCombo.Items)
  784. {
  785. if ((string)cbi.Content == apolicy)
  786. {
  787. AddPolicyCombo.SelectedItem = cbi;
  788. break;
  789. }
  790. }
  791. foreach (ComboBoxItem cbi in PlayPolicyCombo.Items)
  792. {
  793. if ((string)cbi.Content == ppolicy)
  794. {
  795. PlayPolicyCombo.SelectedItem = cbi;
  796. break;
  797. }
  798. }
  799. SearchPolicyCombo.SelectionChanged += new SelectionChangedEventHandler(SearchPolicyCombo_SelectionChanged);
  800. UpgradePolicyCombo.SelectionChanged += new SelectionChangedEventHandler(UpgradePolicyCombo_SelectionChanged);
  801. AddPolicyCombo.SelectionChanged += new SelectionChangedEventHandler(AddPolicyCombo_SelectionChanged);
  802. PlayPolicyCombo.SelectionChanged += new SelectionChangedEventHandler(PlayPolicyCombo_SelectionChanged);
  803. PrefLanguage.SelectionChanged += new SelectionChangedEventHandler(PrefLanguage_SelectionChanged);
  804. }
  805. /// <summary>
  806. /// Invoked when the control panel has been initialized.
  807. /// </summary>
  808. /// <param name="sender">The sender of the event</param>
  809. /// <param name="e">The event data</param>
  810. private void ControlPanel_Initialized(object sender, EventArgs e)
  811. {
  812. }
  813. /// <summary>
  814. /// Invoked when the service manager has been fully initialized.
  815. /// </summary>
  816. /// <param name="sender">The sender of the event</param>
  817. /// <param name="e">The event data</param>
  818. private void ServiceManager_Initialized(object sender, EventArgs e)
  819. {
  820. Dispatcher.Invoke(DispatcherPriority.Background, new Action(delegate()
  821. {
  822. ServiceBrowser.Source = new Uri(ServiceManager.Linked ? ServiceManager.CallbackURLWithAuthParams : ServiceManager.RequestURL);
  823. }));
  824. }
  825. /// <summary>
  826. /// Invoked when the user clicks on "Back to music"
  827. /// </summary>
  828. /// <param name="sender">The sender of the event</param>
  829. /// <param name="e">The event data</param>
  830. private void Back_Clicked(object sender, RoutedEventArgs e)
  831. {
  832. ParentWindow.MainContainer.Children.Remove(ParentWindow.ControlPanel);
  833. ParentWindow.MusicPanel.Visibility = System.Windows.Visibility.Visible;
  834. ParentWindow.PlaybackControls.Search.Box.IsEnabled = (SettingsManager.CurrentSelectedNavigation != "NowPlaying");
  835. }
  836. /// <summary>
  837. /// Invoked when the user clicks on General
  838. /// </summary>
  839. /// <param name="sender">The sender of the event</param>
  840. /// <param name="e">The event data</param>
  841. private void General_Clicked(object sender, RoutedEventArgs e)
  842. {
  843. SwitchTab(Tab.General);
  844. }
  845. /// <summary>
  846. /// Invoked when the user clicks on Sources
  847. /// </summary>
  848. /// <param name="sender">The sender of the event</param>
  849. /// <param name="e">The event data</param>
  850. private void Sources_Clicked(object sender, RoutedEventArgs e)
  851. {
  852. SwitchTab(Tab.Sources);
  853. }
  854. /// <summary>
  855. /// Invoked when the user clicks on Services
  856. /// </summary>
  857. /// <param name="sender">The sender of the event</param>
  858. /// <param name="e">The event data</param>
  859. private void Services_Clicked(object sender, RoutedEventArgs e)
  860. {
  861. SwitchTab(Tab.Services);
  862. }
  863. /// <summary>
  864. /// Invoked when the user clicks on Shortcuts
  865. /// </summary>
  866. /// <param name="sender">The sender of the event</param>
  867. /// <param name="e">The event data</param>
  868. private void Shortcuts_Clicked(object sender, RoutedEventArgs e)
  869. {
  870. SwitchTab(Tab.Shortcuts);
  871. }
  872. /// <summary>
  873. /// Invoked when the user clicks on About
  874. /// </summary>
  875. /// <param name="sender">The sender of the event</param>
  876. /// <param name="e">The event data</param>
  877. private void About_Clicked(object sender, RoutedEventArgs e)
  878. {
  879. SwitchTab(Tab.About);
  880. }
  881. /// <summary>
  882. /// Invoked when the user clicks on Website
  883. /// </summary>
  884. /// <param name="sender">The sender of the event</param>
  885. /// <param name="e">The event data</param>
  886. private void Website_Clicked(object sender, RoutedEventArgs e)
  887. {
  888. System.Diagnostics.Process.Start("http://www.stoffiplayer.com/?ref=stoffi");
  889. }
  890. /// <summary>
  891. /// Invoked when the user clicks on Blog
  892. /// </summary>
  893. /// <param name="sender">The sender of the event</param>
  894. /// <param name="e">The event data</param>
  895. private void Blog_Clicked(object sender, RoutedEventArgs e)
  896. {
  897. System.Diagnostics.Process.Start("http://blog.stoffiplayer.com/?ref=stoffi");
  898. }
  899. /// <summary>
  900. /// Invoked when the user clicks on Project
  901. /// </summary>
  902. /// <param name="sender">The sender of the event</param>
  903. /// <param name="e">The event data</param>
  904. private void Project_Clicked(object sender, RoutedEventArgs e)
  905. {
  906. System.Diagnostics.Process.Start("http://dev.stoffiplayer.com/?ref=stoffi");
  907. }
  908. /// <summary>
  909. /// Invoked when the user adds a folder
  910. /// </summary>
  911. /// <param name="sender">The sender of the event</param>
  912. /// <param name="e">The event data</param>
  913. public void AddFolder_Clicked(object sender, RoutedEventArgs e)
  914. {
  915. ParentWindow.AddFolder_Clicked(sender, e);
  916. }
  917. /// <summary>
  918. /// Invoked when the user adds a file
  919. /// </summary>
  920. /// <param name="sender">The sender of the event</param>
  921. /// <param name="e">The event data</param>
  922. public void AddFile_Clicked(object sender, RoutedEventArgs e)
  923. {
  924. ParentWindow.AddFile_Clicked(sender, e);
  925. }
  926. /// <summary>
  927. /// Invoked when the user ignores a folder
  928. /// </summary>
  929. /// <param name="sender">The sender of the event</param>
  930. /// <param name="e">The event data</param>
  931. private void IgnoreFolder_Clicked(object sender, RoutedEventArgs e)
  932. {
  933. ParentWindow.IgnoreFolder_Clicked(sender, e);
  934. }
  935. /// <summary>
  936. /// Invoked when the user ignores a file
  937. /// </summary>
  938. /// <param name="sender">The sender of the event</param>
  939. /// <param name="e">The event data</param>
  940. private void IgnoreFile_Clicked(object sender, RoutedEventArgs e)
  941. {
  942. ParentWindow.IgnoreFile_Clicked(sender, e);
  943. }
  944. /// <summary>
  945. /// Invoked when the user right-click on a source and clicks on Remove
  946. /// </summary>
  947. /// <param name="sender">The sender of the event</param>
  948. /// <param name="e">The event data</param>
  949. private void menuRemove_Click(object sender, RoutedEventArgs e)
  950. {
  951. int index = SourceList.SelectedIndex;
  952. ThreadStart RemoveThread = delegate()
  953. {
  954. Dispatcher.Invoke(DispatcherPriority.Background, new Action(delegate()
  955. {
  956. int keep = 0;
  957. while (SourceList.SelectedItems.Count > keep)
  958. {
  959. SourceData source = SourceList.SelectedItems[keep] as SourceData;
  960. if (source.Type == SourceType.Library)
  961. {
  962. FilesystemManager.ToggleSource(source);
  963. keep++;
  964. }
  965. else
  966. FilesystemManager.RemoveSource(source);
  967. }
  968. if (index >= SourceList.Items.Count)
  969. index = SourceList.Items.Count - 1;
  970. if (keep == 0)
  971. SourceList.SelectedIndex = index;
  972. }));
  973. };
  974. Thread rb_thread = new Thread(RemoveThread);
  975. rb_thread.Name = "Remove Sources";
  976. rb_thread.Priority = ThreadPriority.BelowNormal;
  977. rb_thread.Start();
  978. }
  979. /// <summary>
  980. /// Invoked when the user right-click on a source and clicks on Ignore/Include
  981. /// </summary>
  982. /// <param name="sender">The sender of the event</param>
  983. /// <param name="e">The event data</param>
  984. private void menuToggle_Click(object sender, RoutedEventArgs e)
  985. {
  986. ThreadStart ToggleThread = delegate()
  987. {
  988. Dispatcher.Invoke(DispatcherPriority.Background, new Action(delegate()
  989. {
  990. foreach (SourceData source in SourceList.SelectedItems)
  991. FilesystemManager.ToggleSource(source);
  992. }));
  993. };
  994. Thread tog_thread = new Thread(ToggleThread);
  995. tog_thread.Name = "Toggle Sources";
  996. tog_thread.Priority = ThreadPriority.BelowNormal;
  997. tog_thread.Start();
  998. }
  999. /// <summary>
  1000. /// Invoked when the context menu of the source list is opening
  1001. /// </summary>
  1002. /// <param name="sender">The sender of the event</param>
  1003. /// <param name="e">The event data</param>
  1004. private void SourceList_ContextMenuOpening(object sender, ContextMenuEventArgs e)
  1005. {
  1006. bool hasOnlyLibrary = true;
  1007. bool hasIgnored = false;
  1008. foreach (SourceData s in SourceList.SelectedItems)
  1009. {
  1010. if (s.Type != SourceType.Library)
  1011. hasOnlyLibrary = false;
  1012. if (s.Ignore)
  1013. hasIgnored = true;
  1014. }
  1015. menuRemove.Visibility = (hasOnlyLibrary ? System.Windows.Visibility.Collapsed : System.Windows.Visibility.Visible);
  1016. menuToggle.Header = (hasIgnored ? U.T("MenuInclude") : U.T("MenuIgnore"));
  1017. }
  1018. /// <summary>
  1019. /// Invoked when the user presses a key
  1020. /// </summary>
  1021. /// <param name="sender">The sender of the event</param>
  1022. /// <param name="e">The event data</param>
  1023. private void SourceList_KeyDown(object sender, KeyEventArgs e)
  1024. {
  1025. if (e.Key == Key.Delete)
  1026. {
  1027. List<SourceData> sources = new List<SourceData>();
  1028. foreach (SourceData s in ((ListView)sender).SelectedItems)
  1029. sources.Add(s);
  1030. menuRemove_Click(null, null);
  1031. }
  1032. }
  1033. /// <summary>
  1034. /// Invoked when theuser clicks to check for upgrades
  1035. /// </summary>
  1036. /// <param name="sender">The sender of the event</param>
  1037. /// <param name="e">The event data</param>
  1038. private void CheckForUpgrades_Clicked(object sender, RoutedEventArgs e)
  1039. {
  1040. UpgradeMessageClose_MouseLeftButtonDown(null, null);
  1041. PrefCheckForUpgrades.IsEnabled = false;
  1042. PrefCheckForUpgrades.Content = U.T("UpgradeWait");
  1043. UpgradeProgressLabel.Text = U.T("UpgradeConnecting");
  1044. UpgradeProgressBar.IsIndeterminate = true;
  1045. UpgradeProgress.Visibility = System.Windows.Visibility.Visible;
  1046. ThreadStart InitUpgradeManagerThread = delegate()
  1047. {
  1048. UpgradeManager.Probe(null, null);
  1049. };
  1050. Thread um_thread = new Thread(InitUpgradeManagerThread);
  1051. um_thread.Priority = ThreadPriority.BelowNormal;
  1052. um_thread.Start();
  1053. }
  1054. /// <summary>
  1055. /// Invoked when the user clicks to perform an upgrade
  1056. /// </summary>
  1057. /// <param name="sender">The sender of the event</param>
  1058. /// <param name="e">The event data</param>
  1059. private void PrefDoUpgrade_Clicked(object sender, RoutedEventArgs e)
  1060. {
  1061. UpgradeMessageClose_MouseLeftButtonDown(null, null);
  1062. PrefDoUpgrade.IsEnabled = false;
  1063. PrefDoUpgrade.Content = U.T("UpgradeWait");
  1064. UpgradeProgressLabel.Text = U.T("UpgradeWait");
  1065. UpgradeProgressBar.IsIndeterminate = false;
  1066. UpgradeProgressBar.Value = 0;
  1067. UpgradeProgress.Visibility = System.Windows.Visibility.Visible;
  1068. UpgradeProgressInfo.Visibility = System.Windows.Visibility.Visible;
  1069. ThreadStart InitUpgradeManagerThread = delegate()
  1070. {
  1071. UpgradeManager.ForceDownload = true;
  1072. UpgradeManager.Probe(null, null);
  1073. };
  1074. Thread um_thread = new Thread(InitUpgradeManagerThread);
  1075. um_thread.Priority = ThreadPriority.BelowNormal;
  1076. um_thread.Start();
  1077. }
  1078. /// <summary>
  1079. /// Invoked when the user clicks on the restart button
  1080. /// </summary>
  1081. /// <param name="sender">The sender of the event</param>
  1082. /// <param name="e">The event data</param>
  1083. private void Restart_Click(object sender, RoutedEventArgs e)
  1084. {
  1085. ParentWindow.Restart();
  1086. }
  1087. /// <summary>
  1088. /// Invoked when the user clicks on a shortcut
  1089. /// </summary>
  1090. /// <param name="sender">The sender of the event</param>
  1091. /// <param name="e">The event data</param>
  1092. private void PrefShortcutButton_Clicked(object sender, RoutedEventArgs e)
  1093. {
  1094. currentShortcutButton = sender as Button;
  1095. string[] name = currentShortcutButton.Name.Split(new char[] { '_' }, 2);
  1096. currentShortcut = SettingsManager.GetKeyboardShortcut(SettingsManager.GetCurrentShortcutProfile(), name[0], name[1].Replace("_", " "));
  1097. waitingForShortcut = true;
  1098. U.ListenForShortcut = false;
  1099. currentShortcutButton.Content = U.T("ShortcutPress");
  1100. currentShortcutButton.FontStyle = FontStyles.Italic;
  1101. currentPressedKeys.Clear();
  1102. }
  1103. /// <summary>
  1104. ///
  1105. /// </summary>
  1106. /// <param name="sender"></param>
  1107. /// <param name="e"></param>
  1108. private void PrefShortcutButton_LostFocus(object sender, RoutedEventArgs e)
  1109. {
  1110. ResetShortcutButton();
  1111. }
  1112. /// <summary>
  1113. ///
  1114. /// </summary>
  1115. /// <param name="sender"></param>
  1116. /// <param name="e"></param>
  1117. private void PrefShortcutProfile_SelectionChanged(object sender, SelectionChangedEventArgs e)
  1118. {
  1119. if (!(PrefShortcutProfile.SelectedItem is ComboBoxItem)) return;
  1120. if (!(((ComboBoxItem)PrefShortcutProfile.SelectedItem).Content is string)) return;
  1121. SettingsManager.CurrentShortcutProfile = ((ComboBoxItem)PrefShortcutProfile.SelectedItem).Content as string;
  1122. KeyboardShortcutProfile profile = SettingsManager.GetCurrentShortcutProfile();
  1123. foreach (KeyboardShortcut sc in profile.Shortcuts)
  1124. {
  1125. if (sc.Name == "Now playing") continue;
  1126. Button b = shortcutButtons[sc.Category + "_" + sc.Name.Replace(" ", "_")] as Button;
  1127. b.Content = sc.Keys == "" ? U.T("ShortcutNotUsed") : sc.Keys;
  1128. b.FontStyle = sc.Keys == "" ? FontStyles.Italic : FontStyles.Normal;
  1129. if (sc.Category == "MediaCommands")
  1130. {
  1131. CheckBox cb = shortcutCheckBoxes[sc.Category + "_" + sc.Name.Replace(" ", "_")] as CheckBox;
  1132. cb.IsChecked = sc.IsGlobal;
  1133. }
  1134. }
  1135. System.Windows.Visibility vis = profile.IsProtected ? System.Windows.Visibility.Collapsed : System.Windows.Visibility.Visible;
  1136. PrefRenameShortcutProfile.Visibility = vis;
  1137. PrefDeleteShortcutProfile.Visibility = vis;
  1138. }
  1139. /// <summary>
  1140. /// Invoked when the user changes search policy
  1141. /// </summary>
  1142. /// <param name="sender">The sender of the event</param>
  1143. /// <param name="e">The event data</param>
  1144. private void SearchPolicyCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
  1145. {
  1146. SettingsManager.FileListConfig.Filter = "";
  1147. SettingsManager.HistoryListConfig.Filter = "";
  1148. SettingsManager.QueueListConfig.Filter = "";
  1149. foreach (PlaylistData p in SettingsManager.Playlists)
  1150. p.ListConfig.Filter = "";
  1151. ParentWindow.PlaybackControls.Search.Clear();
  1152. SearchPolicyDescriptionConverter conv = new SearchPolicyDescriptionConverter();
  1153. ComboBoxItem cbi = (ComboBoxItem)SearchPolicyCombo.SelectedValue;
  1154. SearchPolicy = (SearchPolicy)conv.ConvertBack((string)cbi.Content, null, null, null);
  1155. }
  1156. /// <summary>
  1157. /// Invoked when the user changes search policy
  1158. /// </summary>
  1159. /// <param name="sender">The sender of the event</param>
  1160. /// <param name="e">The event data</param>
  1161. private void UpgradePolicyCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
  1162. {
  1163. UpgradeMessage.Visibility = System.Windows.Visibility.Collapsed;
  1164. UpgradePolicyDescriptionConverter conv = new UpgradePolicyDescriptionConverter();
  1165. ComboBoxItem cbi = (ComboBoxItem)UpgradePolicyCombo.SelectedValue;
  1166. UpgradePolicy = (UpgradePolicy)conv.ConvertBack((string)cbi.Content, null, null, null);
  1167. }
  1168. /// <summary>
  1169. /// Invoked when the user changes search policy
  1170. /// </summary>
  1171. /// <param name="sender">The sender of the event</param>
  1172. /// <param name="e">The event data</param>
  1173. private void AddPolicyCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
  1174. {
  1175. OpenFileAddDescriptionConverter conv = new OpenFileAddDescriptionConverter();
  1176. ComboBoxItem cbi = (ComboBoxItem)AddPolicyCombo.SelectedValue;
  1177. ExternalClickAdd = (OpenAddPolicy)conv.ConvertBack((string)cbi.Content, null, null, null);
  1178. }
  1179. /// <summary>
  1180. /// Invoked when the user changes search policy
  1181. /// </summary>
  1182. /// <param name="sender">The sender of the event</param>
  1183. /// <param name="e">The event data</param>
  1184. private void PlayPolicyCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
  1185. {
  1186. OpenFilePlayDescriptionConverter conv = new OpenFilePlayDescriptionConverter();
  1187. ComboBoxItem cbi = (ComboBoxItem)PlayPolicyCombo.SelectedValue;
  1188. ExternalClickPlay = (OpenPlayPolicy)conv.ConvertBack((string)cbi.Content, null, null, null);
  1189. }
  1190. /// <summary>
  1191. ///
  1192. /// </summary>
  1193. /// <param name="sender"></param>
  1194. /// <param name="e"></param>
  1195. private void PrefCreateShortcutProfile_Clicked(object sender, RoutedEventArgs e)
  1196. {
  1197. List<string> occupied = new List<string>();
  1198. foreach (KeyboardShortcutProfile p in SettingsManager.ShortcutProfiles)
  1199. occupied.Add(p.Name);
  1200. NameDialog dialog = new NameDialog(occupied);
  1201. dialog.ShowDialog();
  1202. if (dialog.DialogResult == true)
  1203. CreateShortcutProfile(U.CleanXMLString(dialog.ProfileName.Text));
  1204. }
  1205. /// <summary>
  1206. ///
  1207. /// </summary>
  1208. /// <param name="sender"></param>
  1209. /// <param name="e"></param>
  1210. private void PrefRenameShortcutProfile_Clicked(object sender, RoutedEventArgs e)
  1211. {
  1212. KeyboardShortcutProfile profile = SettingsManager.GetCurrentShortcutProfile();
  1213. List<string> occupied = new List<string>();
  1214. foreach (KeyboardShortcutProfile p in SettingsManager.ShortcutProfiles)
  1215. occupied.Add(p.Name);
  1216. NameDialog dialog = new NameDialog(occupied, profile.Name);
  1217. dialog.ShowDialog();
  1218. if (dialog.DialogResult == true)
  1219. {
  1220. foreach (ComboBoxItem item in PrefShortcutProfile.Items)
  1221. if (profile.Name == (string)item.Content)
  1222. item.Content = dialog.ProfileName.Text;
  1223. profile.Name = U.CleanXMLString(dialog.ProfileName.Text);
  1224. SettingsManager.CurrentShortcutProfile = dialog.ProfileName.Text;
  1225. }
  1226. }
  1227. /// <summary>
  1228. ///
  1229. /// </summary>
  1230. /// <param name="sender"></param>
  1231. /// <param name="e"></param>
  1232. private void PrefDeleteShortcutProfile_Clicked(object sender, RoutedEventArgs e)
  1233. {
  1234. KeyboardShortcutProfile profile = SettingsManager.GetCurrentShortcutProfile();
  1235. ComboBoxItem itemToRemove = PrefShortcutProfile.SelectedItem as ComboBoxItem;
  1236. int index = PrefShortcutProfile.Items.IndexOf(itemToRemove) - 1;
  1237. if (index == -1) index = 0;
  1238. SettingsManager.ShortcutProfiles.Remove(profile);
  1239. PrefShortcutProfile.SelectedIndex = index;
  1240. PrefShortcutProfile.Items.Remove(itemToRemove);
  1241. }
  1242. /// <summary>
  1243. ///
  1244. /// </summary>
  1245. /// <param name="sender"></param>
  1246. /// <param name="e"></param>
  1247. private void PrefShortcutGlobal_Clicked(object sender, RoutedEventArgs e)
  1248. {
  1249. CheckBox cb = sender as CheckBox;
  1250. string[] name = cb.Name.Split(new char[] { '_' }, 2);
  1251. KeyboardShortcut sc = SettingsManager.GetKeyboardShortcut(SettingsManager.GetCurrentShortcutProfile(), name[0], name[1].Replace("_", " "));
  1252. sc.IsGlobal = (bool)cb.IsChecked;
  1253. }
  1254. /// <summary>
  1255. ///
  1256. /// </summary>
  1257. /// <param name="sender"></param>
  1258. /// <param name="e"></param>
  1259. private void ControlPanel_KeyDown(object sender, KeyEventArgs e)
  1260. {
  1261. if (!waitingForShortcut) return;
  1262. switch (e.Key)
  1263. {
  1264. // convert right shift to left shift
  1265. case Key.RightShift:
  1266. if (!currentPressedKeys.Contains(Key.LeftShift)) currentPressedKeys.Add(Key.LeftShift);
  1267. currentShortcutButton.Content = ParentWindow.GetModifiersAsText(currentPressedKeys);
  1268. return;
  1269. // catch modifier keys
  1270. case Key.LeftShift:
  1271. case Key.LeftCtrl:
  1272. case Key.LeftAlt:
  1273. case Key.LWin:
  1274. case Key.RightCtrl:
  1275. case Key.RightAlt:
  1276. case Key.RWin:
  1277. if (!currentPressedKeys.Contains(e.Key)) currentPressedKeys.Add(e.Key);
  1278. currentShortcutButton.Content = ParentWindow.GetModifiersAsText(currentPressedKeys);
  1279. return;
  1280. // catch alt/left ctrl key when disguised as system key
  1281. case Key.System:
  1282. if (e.SystemKey == Key.LeftAlt || e.SystemKey == Key.RightAlt || e.SystemKey == Key.LeftCtrl)
  1283. {
  1284. if (!currentPressedKeys.Contains(e.SystemKey)) currentPressedKeys.Add(e.SystemKey);
  1285. currentShortcutButton.Content = ParentWindow.GetModifiersAsText(currentPressedKeys);
  1286. return;
  1287. }
  1288. break;
  1289. // ignore these keys
  1290. case Key.None:
  1291. case Key.DeadCharProcessed:
  1292. return;
  1293. default:
  1294. break;
  1295. }
  1296. // TODO: Convert Oem keys to nice strings
  1297. String currentKey = e.Key == Key.System ? ParentWindow.KeyToString(e.SystemKey) : ParentWindow.KeyToString(e.Key);
  1298. String txt = ParentWindow.GetModifiersAsText(currentPressedKeys);
  1299. if (txt.Length > 0) txt += "+" + currentKey;
  1300. else txt = currentKey;
  1301. KeyboardShortcutProfile profile = SettingsManager.GetCurrentShortcutProfile();
  1302. // see if shortcut already exists
  1303. bool createdNew = false;
  1304. foreach (KeyboardShortcut sc in profile.Shortcuts)
  1305. {
  1306. if (sc.Keys == txt && sc != currentShortcut)
  1307. {
  1308. string title = U.T("MessageShortcutClash", "Title");
  1309. string message = U.T("MessageShortcutClash", "Message");
  1310. string name = U.T("Shortcut_" + sc.Name.Replace(" ", "_"));
  1311. string category = U.T("Shortcut" + sc.Category);
  1312. message = message.Replace("%name", name);
  1313. message = message.Replace("%category", category);
  1314. if (MessageBox.Show(message, title, MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
  1315. {
  1316. // if current profile is protected we create a copy of it
  1317. KeyboardShortcut _sc = sc;
  1318. if (profile.IsProtected)
  1319. {
  1320. profile = CreateShortcutProfile();
  1321. currentShortcut = SettingsManager.GetKeyboardShortcut(profile, currentShortcut.Keys);
  1322. _sc = SettingsManager.GetKeyboardShortcut(profile, sc.Keys);
  1323. createdNew = true;
  1324. }
  1325. Button b = (Button)shortcutButtons[sc.Category + "_" + sc.Name.Replace(" ", "_")];
  1326. if (b != null)
  1327. {
  1328. b.Content = U.T("ShortcutNotUsed");
  1329. b.FontStyle = FontStyles.Italic;
  1330. }
  1331. _sc.Keys = "";
  1332. break;
  1333. }
  1334. else
  1335. {
  1336. ResetShortcutButton();
  1337. e.Handled = true;
  1338. return;
  1339. }
  1340. }
  1341. }
  1342. // if current profile is protected we create a copy of it (if we haven't already)
  1343. if (profile.IsProtected && !createdNew)
  1344. {
  1345. profile = CreateShortcutProfile();
  1346. currentShortcut = SettingsManager.GetKeyboardShortcut(profile, currentShortcut.Keys);
  1347. }
  1348. // set shortcut and button text
  1349. currentShortcutButton.FontStyle = FontStyles.Normal;
  1350. waitingForShortcut = false;
  1351. currentShortcutButton.Content = txt;
  1352. if (currentShortcut != null) currentShortcut.Keys = txt;
  1353. currentShortcut = null;
  1354. currentShortcutButton = null;
  1355. e.Handled = true;
  1356. }
  1357. /// <summary>
  1358. ///
  1359. /// </summary>
  1360. /// <param name="sender"></param>
  1361. /// <param name="e"></param>
  1362. private void ControlPanel_KeyUp(object sender, KeyEventArgs e)
  1363. {
  1364. if (!waitingForShortcut)
  1365. {
  1366. U.ListenForShortcut = true;
  1367. return;
  1368. }
  1369. // catch alt/left ctrl when disguised as system key
  1370. if (e.Key == Key.System && (e.SystemKey == Key.LeftAlt || e.SystemKey == Key.RightAlt || e.SystemKey == Key.LeftCtrl))
  1371. {
  1372. if (currentPressedKeys.Contains(e.SystemKey)) currentPressedKeys.Remove(e.SystemKey);
  1373. }
  1374. else if (e.Key == Key.RightShift)
  1375. {
  1376. if (currentPressedKeys.Contains(Key.LeftShift)) currentPressedKeys.Remove(Key.LeftShift);
  1377. }
  1378. else
  1379. {
  1380. if (currentPressedKeys.Contains(e.Key)) currentPressedKeys.Remove(e.Key);
  1381. }
  1382. // update button text
  1383. if (currentPressedKeys.Count == 0)
  1384. currentShortcutButton.Content = U.T("ShortcutPress");
  1385. else
  1386. currentShortcutButton.Content = ParentWindow.GetModifiersAsText(currentPressedKeys);
  1387. }
  1388. /// <summary>
  1389. ///
  1390. /// </summary>
  1391. /// <param name="sender"></param>
  1392. /// <param name="e"></param>
  1393. private void Details_Expanded(object sender, RoutedEventArgs e)
  1394. {
  1395. AboutRelease.Visibility = System.Windows.Visibility.Visible;
  1396. AboutReleaseLabel.Visibility = System.Windows.Visibility.Visible;
  1397. AboutStamp.Visibility = System.Windows.Visibility.Visible;
  1398. AboutStampLabel.Visibility = System.Windows.Visibility.Visible;
  1399. AboutChannel.Visibility = System.Windows.Visibility.Visible;
  1400. AboutChannelLabel.Visibility = System.Windows.Visibility.Visible;
  1401. AboutArch.Visibility = System.Windows.Visibility.Visible;
  1402. AboutArchLabel.Visibility = System.Windows.Visibility.Visible;
  1403. }
  1404. /// <summary>
  1405. ///
  1406. /// </summary>
  1407. /// <param name="sender"></param>
  1408. /// <param name="e"></param>
  1409. private void Details_Collapsed(object sender, RoutedEventArgs e)
  1410. {
  1411. AboutRelease.Visibility = System.Windows.Visibility.Collapsed;
  1412. AboutReleaseLabel.Visibility = System.Windows.Visibility.Collapsed;
  1413. AboutStamp.Visibility = System.Windows.Visibility.Collapsed;
  1414. AboutStampLabel.Visibility = System.Windows.Visibility.Collapsed;
  1415. AboutChannel.Visibility = System.Windows.Visibility.Collapsed;
  1416. AboutChannelLabel.Visibility = System.Windows.Visibility.Collapsed;
  1417. AboutArch.Visibility = System.Windows.Visibility.Collapsed;
  1418. AboutArchLabel.Visibility = System.Windows.Visibility.Collapsed;
  1419. }
  1420. /// <summary>
  1421. /// Invoked when the user selects a different language
  1422. /// </summary>
  1423. /// <param name="sender">The sender of the event</param>
  1424. /// <param name="e">The event data</param>
  1425. private void PrefLanguage_SelectionChanged(object sender, SelectionChangedEventArgs e)
  1426. {
  1427. ComboBoxItem cbi = PrefLanguage.SelectedItem as ComboBoxItem;
  1428. SettingsManager.Language = (string)cbi.Tag;
  1429. }
  1430. /// <summary>
  1431. /// Invoked when the user clicks close on a upgrade message
  1432. /// </summary>
  1433. /// <param name="sender">The sender of the event</param>
  1434. /// <param name="e">The event data</param>
  1435. private void UpgradeMessageClose_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  1436. {
  1437. UpgradeMessage.Visibility = System.Windows.Visibility.Collapsed;
  1438. }
  1439. /// <summary>
  1440. /// Invoked when a property of the settings manager changes
  1441. /// </summary>
  1442. /// <param name="sender">The sender of the event</param>
  1443. /// <param name="e">The event data</param>
  1444. private void SettingsManager_PropertyChanged(object sender, PropertyChangedWithValuesEventArgs e)
  1445. {
  1446. Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate()
  1447. {
  1448. switch (e.PropertyName)
  1449. {
  1450. case "SourceListConfig":
  1451. SourceList.Config = SettingsManager.SourceListConfig;
  1452. break;
  1453. case "MinimizeToTray":
  1454. Console.WriteLine("M2T changed to: " + SettingsManager.MinimizeToTray);
  1455. PrefMin2Tray.IsChecked = SettingsManager.MinimizeToTray;
  1456. break;
  1457. case "ShowOSD":
  1458. PrefOSD.IsChecked = SettingsManager.ShowOSD;
  1459. break;
  1460. case "OpenAddPolicy":
  1461. OpenFileAddDescriptionConverter aconv = new OpenFileAddDescriptionConverter();
  1462. string apolicy = (string)aconv.Convert(SettingsManager.OpenAddPolicy, null, null, null);
  1463. foreach (ComboBoxItem cbi in AddPolicyCombo.Items)
  1464. {
  1465. if ((string)cbi.Content == apolicy)
  1466. {
  1467. AddPolicyCombo.SelectedItem = cbi;
  1468. break;
  1469. }
  1470. }
  1471. break;
  1472. case "OpenPlayPolicy":
  1473. OpenFilePlayDescriptionConverter pconv = new OpenFilePlayDescriptionConverter();
  1474. string ppolicy = (string)pconv.Convert(SettingsManager.OpenPlayPolicy, null, null, null);
  1475. foreach (ComboBoxItem cbi in PlayPolicyCombo.Items)
  1476. {
  1477. if ((string)cbi.Content == ppolicy)
  1478. {
  1479. PlayPolicyCombo.SelectedItem = cbi;
  1480. break;
  1481. }
  1482. }
  1483. break;
  1484. case "SearchPolicy":
  1485. SearchPolicyDescriptionConverter sconv = new SearchPolicyDescriptionConverter();
  1486. string spolicy = (string)sconv.Convert(SettingsManager.SearchPolicy, null, null, null);
  1487. foreach (ComboBoxItem cbi in SearchPolicyCombo.Items)
  1488. {
  1489. if ((string)cbi.Content == spolicy)
  1490. {
  1491. SearchPolicyCombo.SelectedItem = cbi;
  1492. break;
  1493. }
  1494. }
  1495. break;
  1496. case "UpgradePolicy":
  1497. UpgradePolicyDescriptionConverter uconv = new UpgradePolicyDescriptionConverter();
  1498. string upolicy = (string)uconv.Convert(SettingsManager.UpgradePolicy, null, null, null);
  1499. foreach (ComboBoxItem cbi in UpgradePolicyCombo.Items)
  1500. {
  1501. if ((string)cbi.Content == upolicy)
  1502. {
  1503. UpgradePolicyCombo.SelectedItem = cbi;
  1504. break;
  1505. }
  1506. }
  1507. break;
  1508. }
  1509. }));
  1510. }
  1511. /// <summary>
  1512. /// Invoked when the "manage configurations" is expanded.
  1513. /// </summary>
  1514. /// <param name="sender">The sender of the event</param>
  1515. /// <param name="e">The event data</param>
  1516. private void ManageConfigs_Expanded(object sender, RoutedEventArgs e)
  1517. {
  1518. ConfigPanel.Visibility = System.Windows.Visibility.Visible;
  1519. }
  1520. /// <summary>
  1521. /// Invoked when the "manage configurations" is collapsed.
  1522. /// </summary>
  1523. /// <param name="sender">The sender of the event</param>
  1524. /// <param name="e">The event data</param>
  1525. private void ManageConfigs_Collapsed(object sender, RoutedEventArgs e)
  1526. {
  1527. ConfigPanel.Visibility = System.Windows.Visibility.Collapsed;
  1528. }
  1529. /// <summary>
  1530. /// Invoked when the user clicks delete on a cloud configuration.
  1531. /// </summary>
  1532. /// <param name="sender">The sender of the event</param>
  1533. /// <param name="e">The event data</param>
  1534. private void DeleteConfiguration_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  1535. {
  1536. Image img = sender as Image;
  1537. int id = (int)img.Tag;
  1538. ServiceManager.RemoveCloudConfiguration(id);
  1539. }
  1540. /// <summary>
  1541. /// Invoked when the user edits the name of a cloud configuration.
  1542. /// </summary>
  1543. /// <param name="sender">The sender of the event</param>
  1544. /// <param name="e">The event data</param>
  1545. private void ConfigurationName_Edited(object sender, EditableTextBlockEventArgs e)
  1546. {
  1547. EditableTextBlock etb = sender as EditableTextBlock;
  1548. int id = (int)etb.Tag;
  1549. ServiceManager.RenameCloudConfiguration(id, e.NewText);
  1550. }
  1551. /// <summary>
  1552. /// Invoked when the user clicks "new configuration".
  1553. /// </summary>
  1554. /// <param name="sender">The sender of the event</param>
  1555. /// <param name="e">The event data</param>
  1556. private void NewConfiguration_Click(object sender, RoutedEventArgs e)
  1557. {
  1558. List<string> occupied = new List<string>();
  1559. foreach (string name in ServiceManager.SyncProfiles.Values)
  1560. occupied.Add(name);
  1561. NameDialog d = new NameDialog(occupied);
  1562. if ((bool)d.ShowDialog())
  1563. ServiceManager.AddCloudConfiguration(d.ProfileName.Text);
  1564. }
  1565. /// <summary>
  1566. /// Invoked when the user changes the current synchronization profile.
  1567. /// </summary>
  1568. /// <param name="sender">The sender of the event</param>
  1569. /// <param name="e">The event data</param>
  1570. private void SynchronizeProfile_SelectionChanged(object sender, SelectionChangedEventArgs e)
  1571. {
  1572. ComboBoxItem cbi = SynchronizeProfile.SelectedItem as ComboBoxItem;
  1573. int id = cbi.Tag != null ? (int)cbi.Tag : -1;
  1574. if (id > 0) ServiceManager.Sync(id);
  1575. }
  1576. #endregion
  1577. #endregion
  1578. #region Enum
  1579. /// <summary>
  1580. /// The tabs of the control panel
  1581. /// </summary>
  1582. public enum Tab
  1583. {
  1584. /// <summary>
  1585. /// General settings
  1586. /// </summary>
  1587. General,
  1588. /// <summary>
  1589. /// Music sources
  1590. /// </summary>
  1591. Sources,
  1592. /// <summary>
  1593. /// The Stoffi services
  1594. /// </summary>
  1595. Services,
  1596. /// <summary>
  1597. /// Keyboard shortcuts
  1598. /// </summary>
  1599. Shortcuts,
  1600. /// <summary>
  1601. /// About Stoffi
  1602. /// </summary>
  1603. About
  1604. }
  1605. #endregion
  1606. }
  1607. }