Exemplo n.º 1
0
        public MainWindow()
        {
            InitializeComponent();

            NextCommand.InputGestures.Add(new KeyGesture(Key.N, ModifierKeys.Alt));
            PreviousCommand.InputGestures.Add(new KeyGesture(Key.P, ModifierKeys.Alt));
            PlayPauseCommand.InputGestures.Add(new KeyGesture(Key.Space, ModifierKeys.Alt));

            try
            {
                String appData = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), AppName);
                var appDir = new DirectoryInfo(appData);
                
                if (!appDir.Exists)
                    System.IO.Directory.CreateDirectory(appData);
                
                String logFile = Path.Combine(appData, "ultrasonic.log");

                FileLogger = new FileLogger(logFile, Subsonic.Client.Common.Enums.LoggingLevel.Verbose);
                FileLogger.Log("UltraSonic Started", Subsonic.Client.Common.Enums.LoggingLevel.Basic);
                
                WindowStartupLocation = WindowStartupLocation.Manual;

                FileLogger.Log(string.Format("WindowLeft: {0}", Settings.Default.WindowLeft), Subsonic.Client.Common.Enums.LoggingLevel.Verbose);
                Left = Settings.Default.WindowLeft;

                FileLogger.Log(string.Format("WindowTop: {0}", Settings.Default.WindowTop), Subsonic.Client.Common.Enums.LoggingLevel.Verbose);
                Top = Settings.Default.WindowTop;
                
                FileLogger.Log(string.Format("WindowHeight: {0}", Settings.Default.WindowHeight), Subsonic.Client.Common.Enums.LoggingLevel.Verbose);
                Height = Settings.Default.WindowHeight;

                FileLogger.Log(string.Format("WindowWidth: {0}", Settings.Default.WindowWidth), Subsonic.Client.Common.Enums.LoggingLevel.Verbose);
                Width = Settings.Default.WindowWidth;

                FileLogger.Log(string.Format("WindowMaximized: {0}", Settings.Default.WindowMaximized), Subsonic.Client.Common.Enums.LoggingLevel.Verbose);
                
                if (Settings.Default.WindowMaximized)
                    WindowState = WindowState.Maximized;

                PopulateSettings();
                MusicPlayStatusLabel.Content = "Stopped";
                ArtistTreeView.DataContext = ArtistItems;
                //DataGridDragAndDrop<TrackItem> playlistTrackDragAndDrop = DataGridDragAndDrop<TrackItem>.Create(_playlistTrackItems, PlaylistTrackGrid, this, PlaylistDragPopup);

                //PlaylistTrackGrid.BeginningEdit += playlistTrackDragAndDrop.DataGridOnBeginEdit;
                //PlaylistTrackGrid.CellEditEnding += playlistTrackDragAndDrop.DataGridOnEndEdit;
                //PlaylistTrackGrid.PreviewMouseLeftButtonDown += playlistTrackDragAndDrop.DataGridOnMouseLeftButtonDown;
                //MainGrid.MouseLeftButtonUp += playlistTrackDragAndDrop.DataGridOnMouseLeftButtonUp;
                //MainGrid.MouseMove += playlistTrackDragAndDrop.DataGridOnMouseMove;

                if (StreamProxy == null)
                {
                    FileLogger.Log("Creating StreamProxy", Subsonic.Client.Common.Enums.LoggingLevel.Information);

                    StreamProxy = new StreamProxy();
                    StreamProxy.Start();

                    FileLogger.Log(string.Format("StreamProxy Port: {0}", StreamProxy.GetPort()), Subsonic.Client.Common.Enums.LoggingLevel.Information);
                }

                if (!string.IsNullOrWhiteSpace(Username) && !string.IsNullOrWhiteSpace(Password) && !string.IsNullOrWhiteSpace(ServerUrl))
                {
                    InitSubsonicApi();

                    if (SubsonicClient != null)
                    {
                        License license = SubsonicClient.GetLicense();
                        UpdateLicenseInformation(license);

                        if (!license.Valid)
                        {
                            MessageBox.Show(string.Format("You must have a valid REST API license to use {0}", AppName));
                        }
                        else
                        {
                            UpdateArtists();
                            PopulatePlaylist();
                            PopulatePlaybackList();
                            UpdatePlaylists();
                        }
                    }
                }
                else
                {
                    SettingsTab.IsSelected = true;
                    ServerSettingsExpander.IsExpanded = true;
                    SettingsServerAddressTextBox.Focus();
                }

                _mainTimer.Interval = TimeSpan.FromMilliseconds(500);
                _mainTimer.Tick += (o, s) => Ticktock();
                _mainTimer.Start();

                if (_nowPlayingInterval > 0)
                    UpdateNowPlaying();

                _nowPlayingTimer.Interval = TimeSpan.FromSeconds(_nowPlayingInterval);
                _nowPlayingTimer.Tick += (o, s) => UpdateNowPlaying();
                _nowPlayingTimer.Start();

                if (_chatMessagesInterval > 0)
                    UpdateChatMessages();

                _chatMessagesTimer.Interval = TimeSpan.FromSeconds(_chatMessagesInterval);
                _chatMessagesTimer.Tick += (o, s) => UpdateChatMessages();
                _chatMessagesTimer.Start();

                MediaPlayer.MediaEnded += (o, args) => PlayNextTrack();
                MediaPlayer.Volume = Settings.Default.Volume;
                MediaPlayer.IsMuted = Settings.Default.VolumeMuted;
                VolumeSlider.Value = MediaPlayer.Volume*10;

                AlbumDataGrid.ItemsSource = _albumItems;
                NowPlayingDataGrid.ItemsSource = _nowPlayingItems;
                ChatListView.ItemsSource = _chatMessages;
                PlaylistTrackGrid.ItemsSource = _playlistTrackItems;
                PlaybackTrackGrid.ItemsSource = _playbackTrackItems;
                PlaylistsDataGrid.ItemsSource = _playlistItems;
                TrackDataGrid.ItemsSource = _trackItems;
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("Exception:\n\n{0}\n{1}", ex.Message, ex.StackTrace), AppName, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }