예제 #1
0
        public void PlayFreelist(Playlist plist)
        {
            if (plist == null)
            {
                throw new ArgumentNullException(nameof(plist));
            }

            freeList.Clear();
            freeList.AddRange(plist.AsEnumerable());
            Reset();
        }
예제 #2
0
        public R SavePlaylist(Playlist plist)
        {
            if (plist == null)
            {
                throw new ArgumentNullException(nameof(plist));
            }

            if (!IsNameValid(plist.Name))
            {
                return("Invalid playlist name.");
            }

            var di = new DirectoryInfo(data.PlaylistPath);

            if (!di.Exists)
            {
                return("No playlist directory has been set up.");
            }

            var fi = GetFileInfo(plist.Name);

            if (fi.Exists)
            {
                var tempList = LoadPlaylist(plist.Name, true);
                if (!tempList)
                {
                    return("Existing playlist ist corrupted, please use another name or repair the existing.");
                }
                if (tempList.Value.CreatorDbId.HasValue && tempList.Value.CreatorDbId != plist.CreatorDbId)
                {
                    return("You cannot overwrite a playlist which you dont own.");
                }
            }

            using (var sw = new StreamWriter(fi.Open(FileMode.Create, FileAccess.Write, FileShare.Read), FileEncoding))
            {
                sw.WriteLine("version:1");

                if (plist.CreatorDbId.HasValue)
                {
                    sw.Write("owner:");
                    sw.Write(plist.CreatorDbId.Value);
                    sw.WriteLine();
                }

                sw.WriteLine();

                foreach (var pli in plist.AsEnumerable())
                {
                    if (pli.HistoryId.HasValue)
                    {
                        sw.Write("id:");
                        if (pli.Meta.ResourceOwnerDbId.HasValue)
                        {
                            sw.Write(pli.Meta.ResourceOwnerDbId.Value);
                        }
                        sw.Write(":");
                        sw.Write(pli.HistoryId.Value);
                    }
                    else if (!string.IsNullOrWhiteSpace(pli.Link))
                    {
                        sw.Write("ln:");
                        if (pli.Meta.ResourceOwnerDbId.HasValue)
                        {
                            sw.Write(pli.Meta.ResourceOwnerDbId.Value);
                        }
                        sw.Write(":");
                        if (pli.AudioType.HasValue)
                        {
                            sw.Write(pli.AudioType.Value);
                        }
                        sw.Write(",");
                        sw.Write(Uri.EscapeDataString(pli.Link));
                    }
                    else if (pli.Resource != null)
                    {
                        sw.Write("rs:");
                        if (pli.Meta.ResourceOwnerDbId.HasValue)
                        {
                            sw.Write(pli.Meta.ResourceOwnerDbId.Value);
                        }
                        sw.Write(":");
                        sw.Write(pli.Resource.AudioType);
                        sw.Write(",");
                        sw.Write(Uri.EscapeDataString(pli.Resource.ResourceId));
                        sw.Write(",");
                        sw.Write(Uri.EscapeDataString(pli.Resource.ResourceTitle));
                    }
                    else
                    {
                        continue;
                    }

                    sw.WriteLine();
                }
            }

            return(R.OkR);
        }