From 5f354be3b0a5d335f83a2885dd68cbd418cb88a0 Mon Sep 17 00:00:00 2001 From: "Zeeshan Ali (Khattak)" Date: Wed, 28 Jan 2009 12:32:55 +0000 Subject: [PATCH] Separate out search and serialization in DVB plugin. svn path=/trunk/; revision=490 --- src/plugins/dvb/rygel-dvb-channel-group.vala | 20 ++-- src/plugins/dvb/rygel-dvb-content-dir.vala | 143 ++++++++++++++++++++------- 2 files changed, 117 insertions(+), 46 deletions(-) diff --git a/src/plugins/dvb/rygel-dvb-channel-group.vala b/src/plugins/dvb/rygel-dvb-channel-group.vala index 2e0e3ab..3063abe 100644 --- a/src/plugins/dvb/rygel-dvb-channel-group.vala +++ b/src/plugins/dvb/rygel-dvb-channel-group.vala @@ -64,17 +64,19 @@ public class Rygel.DVBChannelGroup : MediaContainer { this.http_server.item_requested += this.on_item_requested; } - public uint add_channels (DIDLLiteWriter didl_writer, - uint index, - uint requested_count, - out uint total_matches) throws GLib.Error { - foreach (var channel in channels.get_values ()) { - channel.serialize (didl_writer); - } + public ArrayList get_channels (uint offset, + uint max_count, + out uint child_count) + throws GLib.Error { + child_count = this.channels.size; + + var channels = new ArrayList (); - total_matches = channels.size; + foreach (var channel in this.channels.get_values ()) { + channels.add (channel); + } - return total_matches; + return channels; } public DVBChannel find_channel (string id) { diff --git a/src/plugins/dvb/rygel-dvb-content-dir.vala b/src/plugins/dvb/rygel-dvb-content-dir.vala index 20e684b..52867a1 100644 --- a/src/plugins/dvb/rygel-dvb-content-dir.vala +++ b/src/plugins/dvb/rygel-dvb-content-dir.vala @@ -31,6 +31,8 @@ using Gee; * Implementation of DVB ContentDirectory service. */ public class Rygel.DVBContentDir : ContentDirectory { + public static const int MAX_REQUESTED_COUNT = 128; + // class-wide constants private const string DVB_SERVICE = "org.gnome.DVB"; private const string MANAGER_PATH = "/org/gnome/DVB/Manager"; @@ -102,41 +104,29 @@ public class Rygel.DVBContentDir : ContentDirectory { public override void add_children_metadata (DIDLLiteWriter didl_writer, BrowseArgs args) throws GLib.Error { - DVBChannelGroup group; + if (args.requested_count == 0) + args.requested_count = MAX_REQUESTED_COUNT; - group = this.find_group_by_id (args.object_id); - if (group == null) { - throw new ContentDirectoryError.NO_SUCH_OBJECT ("No such object"); + ArrayList children; + + children = this.get_children (args.object_id, + args.index, + args.requested_count, + out args.total_matches); + args.number_returned = children.size; + + /* Iterate through all items */ + for (int i = 0; i < children.size; i++) { + children[i].serialize (didl_writer); } - args.number_returned = group.add_channels (didl_writer, - args.index, - args.requested_count, - out args.total_matches); args.update_id = uint32.MAX; } public override void add_metadata (DIDLLiteWriter didl_writer, BrowseArgs args) throws GLib.Error { - bool found = false; - - DVBChannelGroup group; - - // First try groups - group = find_group_by_id (args.object_id); - - if (group != null) { - group.serialize (didl_writer); - - found = true; - } else { - // Now try channels - found = this.add_channel (didl_writer, args.object_id); - } - - if (!found) { - throw new ContentDirectoryError.NO_SUCH_OBJECT ("No such object"); - } + MediaObject media_object = find_object_by_id (args.object_id); + media_object.serialize (didl_writer); args.update_id = uint32.MAX; } @@ -144,10 +134,14 @@ public class Rygel.DVBContentDir : ContentDirectory { public override void add_root_children_metadata (DIDLLiteWriter didl_writer, BrowseArgs args) throws GLib.Error { - foreach (DVBChannelGroup group in this.groups) - group.serialize (didl_writer); + var children = get_root_children (args.index, + args.requested_count, + out args.total_matches); + foreach (var child in children) { + child.serialize (didl_writer); + } - args.total_matches = args.number_returned = this.groups.size; + args.number_returned = children.size; args.update_id = uint32.MAX; } @@ -166,20 +160,95 @@ public class Rygel.DVBContentDir : ContentDirectory { return group; } - private bool add_channel (DIDLLiteWriter didl_writer, - string id) throws GLib.Error { - bool found = false; + private DVBChannel find_channel_by_id (string id) throws GLib.Error { + DVBChannel channel = null; foreach (DVBChannelGroup group in this.groups) { - var channel = group.find_channel (id); + channel = group.find_channel (id); if (channel != null) { - channel.serialize (didl_writer); - found = true; break; } } - return found; + return channel; + } + + private MediaObject find_object_by_id (string object_id) throws GLib.Error { + // First try groups + MediaObject media_object = find_group_by_id (object_id); + + if (media_object == null) { + media_object = find_channel_by_id (object_id); + } + + if (media_object == null) { + throw new ContentDirectoryError.NO_SUCH_OBJECT ("No such object"); + } + + return media_object; + } + + private ArrayList get_children (string container_id, + uint offset, + uint max_count, + out uint child_count) + throws GLib.Error { + var group = this.find_group_by_id (container_id); + if (group == null) { + throw new ContentDirectoryError.NO_SUCH_OBJECT ("No such object"); + } + + var channels = group.get_channels (offset, + max_count, + out child_count); + if (max_count == 0 && offset == 0) { + return channels; + } else { + return slice_object_list (channels, + offset, + max_count); + } + } + + private ArrayList get_root_children (uint offset, + uint max_count, + out uint child_count) + throws GLib.Error { + child_count = this.groups.size; + + ArrayList children; + + if (max_count == 0 && offset == 0) { + children = this.groups; + } else if (offset >= child_count) { + throw new ContentDirectoryError.NO_SUCH_OBJECT ("No such object"); + } else { + children = slice_object_list (this.groups, + offset, + max_count); + } + + return children; + } + + private ArrayList slice_object_list ( + ArrayList list, + uint offset, + uint max_count) { + uint total = list.size; + + var slice = new ArrayList (); + + if (max_count == 0 || max_count > (total - offset)) { + max_count = total - offset; + } + + slice = new ArrayList (); + for (uint i = offset; i < total; i++) { + slice.add (list[(int) i]); + } + + return slice; } } -- 2.7.4