Exemple #1
0
        /// <summary>
        /// Action: Browse
        /// </summary>
        /// <param name="objectId">Associated State Variable: A_ARG_TYPE_ObjectID</param>
        /// <param name="browseFlag">Associated State Variable: A_ARG_TYPE_BrowseFlag</param>
        /// <param name="filter">Associated State Variable: A_ARG_TYPE_Filter</param>
        /// <param name="startingIndex">Associated State Variable: A_ARG_TYPE_Index</param>
        /// <param name="requestedCount">Associated State Variable: A_ARG_TYPE_Count</param>
        /// <param name="sortCriteria">Associated State Variable: A_ARG_TYPE_SortCriteria</param>
        /// <param name="result">Associated State Variable: A_ARG_TYPE_Result</param>
        /// <param name="numberReturned">Associated State Variable: A_ARG_TYPE_Count</param>
        /// <param name="totalMatches">Associated State Variable: A_ARG_TYPE_Count</param>
        /// <param name="updateID">Associated State Variable: A_ARG_TYPE_UpdateID</param>
        public override void Browse(String objectId, Enum_A_ARG_TYPE_BrowseFlag browseFlag, String filter, UInt32 startingIndex,
                                    UInt32 requestedCount, String sortCriteria, out String result, out UInt32 numberReturned, out UInt32 totalMatches,
                                    out UInt32 updateID)
        {
            updateID       = 0;
            numberReturned = 0;
            totalMatches   = 0;
            StringBuilder sb = new StringBuilder();

            sb.AppendLine(@"<DIDL-Lite xmlns:dc=""http://purl.org/dc/elements/1.1/"" " +
                          @"xmlns:upnp=""urn:schemas-upnp-org:metadata-1-0/upnp/"" " +
                          @"xmlns=""urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/"">");
            try
            {
                FolderBase folder    = (objectId == "0" || objectId == "-1") ? new MainEntry() : GetFolder(objectId);
                var        userAgent = GetUserAgent();

                switch (browseFlag)
                {
                case Enum_A_ARG_TYPE_BrowseFlag.BROWSEDIRECTCHILDREN:
                    bool isFreeboxV5 = (userAgent == UserAgent.FreeboxV5);
                    if (isFreeboxV5)
                    {
                        // In order to enchain the audio files on the Freebox V5
                        IPAddress address = GetCaller().Address;
                        if (Files.ContainsKey(address))
                        {
                            Files.Remove(address);
                        }
                    }

                    FolderBase   subFolder;
                    BrowseResult browseResult;
                    bool         exit = false;
                    do
                    {
                        // Browse the folder
                        browseResult = folder.Browse(startingIndex, requestedCount, userAgent);

                        if (browseResult != null && browseResult.TotalCount == 1 && browseResult.Entries.Count > 0)
                        {
                            // If there is only 1 folder, we will browse the subfolder
                            subFolder = browseResult.Entries[0] as FolderBase;
                            if (subFolder == null)
                            {
                                exit = true;
                            }
                            else
                            {
                                folder = subFolder;
                            }
                        }
                        else
                        {
                            exit = true;
                        }
                    }while (!exit);

                    if (browseResult != null)
                    {
                        bool   v6Workaround = (userAgent == UserAgent.FreeboxV6 && !folder.UsePathAsId && String.IsNullOrEmpty(folder.Path));
                        string line;
                        string dcTitle = "<dc:title>";
                        var    format  = dcTitle + Podcast.GetLinesNumberingFormat(browseResult.TotalCount);
                        int    i       = 1;
                        foreach (var entry in browseResult.Entries)
                        {
                            if (entry is Shutdown && Application.IsMono)
                            {
                                // Unable to shutdown on Linux, Mac
                                browseResult.TotalCount -= 1;
                                continue;
                            }

                            line = entry.ToString(GetReceiver(), objectId, userAgent);
                            if (line == null)
                            {
                                browseResult.TotalCount -= 1;
                                continue;
                            }

                            if (v6Workaround)
                            {
                                // To keep the order of the folders
                                line = line.Replace(dcTitle, String.Format(format, i + startingIndex, String.Empty));
                                i   += 1;
                            }
                            sb.AppendLine(line);
                            numberReturned += 1;
                            AddToFileList(entry, isFreeboxV5);
                        }
                        totalMatches = (uint)browseResult.TotalCount;
                    }
                    break;

                default:
                    sb.AppendLine((folder ?? (Entry) new Entries.File()
                    {
                        Path = HttpUtility.UrlDecode(objectId)
                    }).ToString(GetReceiver(), "-1", userAgent));
                    numberReturned = 1;
                    totalMatches   = 1;
                    break;
                }
            }
            catch (Exception ex)
            {
                Utils.WriteException(ex);
            }
            sb.Append(@"</DIDL-Lite>");
            result = sb.ToString();
        }