Пример #1
0
        public async Task<EditablePlaylistViewModel> GetEditablePlaylistAsync(PlaylistViewModel playlistViewModel)
        {
            // Assume new playlist
            var editablePlaylist = new EditablePlaylistViewModel(
                await this.GetUniquePlaylistNameAsync(ResourceUtils.GetString("Language_New_Playlist")),
                PlaylistType.Static
                );

            // Not a new playlist
            if (playlistViewModel != null)
            {
                editablePlaylist.Path = playlistViewModel.Path;
                editablePlaylist.PlaylistName = playlistViewModel.Name;
                editablePlaylist.Type = playlistViewModel.Type;

                if (playlistViewModel.Type.Equals(PlaylistType.Smart))
                {
                    await Task.Run(() =>
                    {
                        var decoder = new SmartPlaylistDecoder();
                        DecodeSmartPlaylistResult result = decoder.DecodePlaylist(playlistViewModel.Path);

                        editablePlaylist.Limit = new SmartPlaylistLimitViewModel(result.Limit.Type, result.Limit.Value);
                        editablePlaylist.Limit.IsEnabled = result.Limit.Value > 0;
                        editablePlaylist.SelectedLimitType = editablePlaylist.LimitTypes.Where(x => x.Type.Equals(result.Limit.Type)).FirstOrDefault();
                        editablePlaylist.MatchAnyRule = result.Match.Equals(SmartPlaylistDecoder.MatchAny) ? true : false;

                        var ruleViewModels = new ObservableCollection<SmartPlaylistRuleViewModel>();

                        foreach (SmartPlaylistRule rule in result.Rules)
                        {
                            var ruleViewModel = new SmartPlaylistRuleViewModel();
                            ruleViewModel.SelectedField = ruleViewModel.Fields.Where(x => x.Name.Equals(rule.Field)).FirstOrDefault();

                            // If a invalid field was provided in the xml file, just take the first field.
                            if (ruleViewModel.SelectedField == null)
                            {
                                ruleViewModel.SelectedField = ruleViewModel.Fields.First();
                            }

                            ruleViewModel.SelectedOperator = ruleViewModel.Operators.Where(x => x.Name.Equals(rule.Operator)).FirstOrDefault();

                            // If a invalid operator was provided in the xml file, just take the first operator.
                            if (ruleViewModel.SelectedOperator == null)
                            {
                                ruleViewModel.SelectedOperator = ruleViewModel.Operators.First();
                            }

                            ruleViewModel.Value = rule.Value;

                            ruleViewModels.Add(ruleViewModel);
                        }

                        editablePlaylist.Rules = ruleViewModels;
                    });
                }
            }

            return editablePlaylist;
        }
Пример #2
0
        private async Task<EditPlaylistResult> EditStaticPlaylistAsync(EditablePlaylistViewModel editablePlaylistViewModel)
        {
            if (!System.IO.File.Exists(editablePlaylistViewModel.Path))
            {
                LogClient.Error("Error while renaming playlist. The playlist '{0}' could not be found", editablePlaylistViewModel.Path);
                return EditPlaylistResult.Error;
            }

            string sanitizedNewPlaylistName = this.SanitizePlaylistFilename(editablePlaylistViewModel.PlaylistName);
            string newFilename = this.CreatePlaylistFilePath(sanitizedNewPlaylistName, PlaylistType.Static);

            if (System.IO.File.Exists(newFilename))
            {
                return EditPlaylistResult.Duplicate;
            }

            EditPlaylistResult result = EditPlaylistResult.Success;

            await Task.Run(() =>
            {
                try
                {
                    System.IO.File.Move(editablePlaylistViewModel.Path, newFilename);
                }
                catch (Exception ex)
                {
                    LogClient.Error($"Error while editing static playlist '{editablePlaylistViewModel.Path}'. Exception: {ex.Message}");
                    result = EditPlaylistResult.Error;
                }
            });

            return result;
        }
Пример #3
0
        private async Task<PlaylistViewModel> CreateNewStaticPlaylistAsync(EditablePlaylistViewModel editablePlaylist)
        {
            if (editablePlaylist == null)
            {
                LogClient.Error($"{nameof(editablePlaylist)} is null");
                return null;
            }

            if (string.IsNullOrEmpty(editablePlaylist.Path))
            {
                LogClient.Error($"{nameof(editablePlaylist.Path)} is null or empty");
                return null;
            }

            try
            {
                await Task.Run(() =>
                {
                    // Close() prevents file in use issues
                    System.IO.File.Create(editablePlaylist.Path).Close();
                });
            }
            catch (Exception ex)
            {
                LogClient.Error($"Could not create playlist '{editablePlaylist.PlaylistName}' with filename '{editablePlaylist.Path}'. Exception: {ex.Message}");
                return null;
            }

            return new PlaylistViewModel(editablePlaylist.PlaylistName, editablePlaylist.Path, PlaylistType.Static);
        }
Пример #4
0
        private async Task<PlaylistViewModel> UpdateSmartPlaylistAsync(EditablePlaylistViewModel editablePlaylist)
        {
            if (editablePlaylist == null)
            {
                LogClient.Error($"{nameof(editablePlaylist)} is null");
                return null;
            }

            if (string.IsNullOrEmpty(editablePlaylist.Path))
            {
                LogClient.Error($"{nameof(editablePlaylist.Path)} is null or empty");
                return null;
            }

            // Delete the old smart playlist file
            await Task.Run(() =>
            {
                if (System.IO.File.Exists(editablePlaylist.Path))
                {
                    System.IO.File.Delete(editablePlaylist.Path);
                }
            });

            // Update the path of the smart playlist to create
            string sanitizedPlaylistName = this.SanitizePlaylistFilename(editablePlaylist.PlaylistName);
            editablePlaylist.Path = this.CreatePlaylistFilePath(sanitizedPlaylistName, editablePlaylist.Type);

            // Create a new smart playlist file
            return await this.CreateNewSmartPlaylistAsync(editablePlaylist);
        }
Пример #5
0
        private async Task<PlaylistViewModel> CreateNewSmartPlaylistAsync(EditablePlaylistViewModel editablePlaylist)
        {
            if (editablePlaylist == null)
            {
                LogClient.Error($"{nameof(editablePlaylist)} is null");
                return null;
            }

            if (string.IsNullOrEmpty(editablePlaylist.Path))
            {
                LogClient.Error($"{nameof(editablePlaylist.Path)} is null or empty");
                return null;
            }

            try
            {
                await Task.Run(() =>
                {
                    var decoder = new SmartPlaylistDecoder();
                    XDocument smartPlaylistDocument = decoder.EncodeSmartPlaylist(
                        editablePlaylist.PlaylistName,
                        editablePlaylist.MatchAnyRule,
                        editablePlaylist.Limit.ToSmartPlaylistLimit(),
                        editablePlaylist.Rules.Select(x => x.ToSmartPlaylistRule()).ToList());
                    smartPlaylistDocument.Save(editablePlaylist.Path);
                });
            }
            catch (Exception ex)
            {
                LogClient.Error($"Could not create playlist '{editablePlaylist.PlaylistName}' with filename '{editablePlaylist.Path}'. Exception: {ex.Message}");
                return null;
            }

            return new PlaylistViewModel(editablePlaylist.PlaylistName, editablePlaylist.Path, PlaylistType.Smart);
        }
        private async Task CreateNewPlaylistAsync(EditablePlaylistViewModel editablePlaylist)
        {
            CreateNewPlaylistResult result = await this.playlistService.CreateNewPlaylistAsync(editablePlaylist);

            switch (result)
            {
            case CreateNewPlaylistResult.Duplicate:
                this.dialogService.ShowNotification(
                    0xe711,
                    16,
                    ResourceUtils.GetString("Language_Already_Exists"),
                    ResourceUtils.GetString("Language_Already_Playlist_With_That_Name").Replace("{playlistname}", editablePlaylist.PlaylistName),
                    ResourceUtils.GetString("Language_Ok"),
                    false,
                    string.Empty);
                break;

            case CreateNewPlaylistResult.Error:
                this.dialogService.ShowNotification(
                    0xe711,
                    16,
                    ResourceUtils.GetString("Language_Error"),
                    ResourceUtils.GetString("Language_Error_Adding_Playlist"),
                    ResourceUtils.GetString("Language_Ok"),
                    true,
                    ResourceUtils.GetString("Language_Log_File"));
                break;

            case CreateNewPlaylistResult.Blank:
                this.dialogService.ShowNotification(
                    0xe711,
                    16,
                    ResourceUtils.GetString("Language_Error"),
                    ResourceUtils.GetString("Language_Provide_Playlist_Name"),
                    ResourceUtils.GetString("Language_Ok"),
                    false,
                    string.Empty);
                break;

            default:
                // Never happens
                break;
            }
        }
Пример #7
0
        public async Task<EditPlaylistResult> EditPlaylistAsync(EditablePlaylistViewModel editablePlaylistViewModel)
        {
            if (editablePlaylistViewModel == null)
            {
                LogClient.Error($"{nameof(editablePlaylistViewModel)} is null");
                return EditPlaylistResult.Error;
            }

            if (string.IsNullOrWhiteSpace(editablePlaylistViewModel.Path))
            {
                LogClient.Error($"{nameof(editablePlaylistViewModel.Path)} is null or empty");
                return EditPlaylistResult.Error;
            }

            if (string.IsNullOrWhiteSpace(editablePlaylistViewModel.PlaylistName))
            {
                LogClient.Error($"{nameof(editablePlaylistViewModel.PlaylistName)} is empty");
                return EditPlaylistResult.Blank;
            }

            this.watcher.Suspend(); // Stop watching the playlist folder

            EditPlaylistResult result = EditPlaylistResult.Error;

            if (editablePlaylistViewModel.Type.Equals(PlaylistType.Static))
            {
                result = await this.EditStaticPlaylistAsync(editablePlaylistViewModel);
            }
            else if (editablePlaylistViewModel.Type.Equals(PlaylistType.Smart))
            {
                result = await this.EditSmartPlaylistAsync(editablePlaylistViewModel);
            }

            this.watcher.Resume(); // Start watching the playlist folder

            if (result == EditPlaylistResult.Success)
            {
                this.PlaylistFolderChanged(this, new EventArgs());
            }

            return result;
        }
Пример #8
0
        private async Task <EditPlaylistResult> EditSmartPlaylistAsync(EditablePlaylistViewModel editablePlaylistViewModel)
        {
            if (!System.IO.File.Exists(editablePlaylistViewModel.Path))
            {
                LogClient.Error("Error while renaming playlist. The playlist '{0}' could not be found", editablePlaylistViewModel.Path);
                return(EditPlaylistResult.Error);
            }

            IList <PlaylistViewModel> existingSmartPlaylists = await this.GetSmartPlaylistsAsync();

            string oldSmartPlaylistName = this.GetSmartPlaylistName(editablePlaylistViewModel.Path);

            bool isEditingSamePlaylist     = oldSmartPlaylistName.ToLower().Equals(editablePlaylistViewModel.PlaylistName.ToLower());
            bool playlistNameIsAlreadyUsed = existingSmartPlaylists.Any(x => x.Name.ToLower().Equals(editablePlaylistViewModel.PlaylistName.ToLower()));

            if (!isEditingSamePlaylist & playlistNameIsAlreadyUsed)
            {
                return(EditPlaylistResult.Duplicate);
            }

            EditPlaylistResult result = EditPlaylistResult.Success;

            try
            {
                PlaylistViewModel playlistViewModel = await this.UpdateSmartPlaylistAsync(editablePlaylistViewModel);

                if (playlistViewModel == null)
                {
                    LogClient.Error($"Error while editing smart playlist '{editablePlaylistViewModel.Path}'. {nameof(playlistViewModel)} is null");
                    result = EditPlaylistResult.Error;
                }
            }
            catch (Exception ex)
            {
                LogClient.Error($"Error while editing smart playlist '{editablePlaylistViewModel.Path}'. Exception: {ex.Message}");
                result = EditPlaylistResult.Error;
            }

            return(result);
        }
Пример #9
0
        public async Task <CreateNewPlaylistResult> CreateNewPlaylistAsync(EditablePlaylistViewModel editablePlaylist)
        {
            if (string.IsNullOrWhiteSpace(editablePlaylist.PlaylistName))
            {
                return(CreateNewPlaylistResult.Blank);
            }

            string sanitizedPlaylistName = this.SanitizePlaylistFilename(editablePlaylist.PlaylistName);

            editablePlaylist.Path = this.CreatePlaylistFilePath(sanitizedPlaylistName, editablePlaylist.Type);

            if (System.IO.File.Exists(editablePlaylist.Path))
            {
                return(CreateNewPlaylistResult.Duplicate);
            }

            this.watcher.Suspend(); // Stop watching the playlist folder

            PlaylistViewModel playlistViewModel = null;

            if (editablePlaylist.Type.Equals(PlaylistType.Static))
            {
                playlistViewModel = await this.CreateNewStaticPlaylistAsync(editablePlaylist);
            }
            else if (editablePlaylist.Type.Equals(PlaylistType.Smart))
            {
                playlistViewModel = await this.CreateNewSmartPlaylistAsync(editablePlaylist);
            }

            this.watcher.Resume(); // Start watching the playlist folder

            if (playlistViewModel == null)
            {
                return(CreateNewPlaylistResult.Error);
            }

            this.PlaylistFolderChanged(this, new EventArgs());

            return(CreateNewPlaylistResult.Success);
        }