<commit_before>/*
 * Copyright (C) 2013  Cable Television Laboratories, Inc.
 *
 * Author: <NAME> <<EMAIL>>
 *
 * This file is part of Rygel.
 *
 * Rygel is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

using GUPnP;
using Gee;

/**
 * Represents a media resource (Music, Video, Image, etc).
 */
public class Rygel.MediaResource : GLib.Object {
    private string name;

    // Res block fields
    public string uri { get; set; }
    public string import_uri { get; set; }
    public string extension { get; set; default = null; }
    public int64 size { get; set; default = -1; } // In bytes
    public int64 cleartext_size { get; set; default = -1; } // In bytes
    public long duration { get; set; default = -1; } // In seconds
    public int bitrate { get; set; default = -1; } // In bits per second
    public int bits_per_sample { get; set; default = -1; }
    public int color_depth { get; set; default = -1; } // In bits
    public int width { get; set; default = -1; } // In pixels
    public int height { get; set; default = -1; } // In pixels
    public int audio_channels { get; set; default = -1; }
    public int sample_freq { get; set; default = -1; } // In Hz

    // ProtocolInfo fields
    public string protocol { get; set; default = null; }
    public string mime_type  { get; set; default = null; }
    public string dlna_profile  { get; set; default = null; }
    public string network  { get; set; default = null; }
    public string[] play_speeds = null;
    public DLNAConversion dlna_conversion { get; set;
                                            default = DLNAConversion.NONE; }
    public DLNAFlags dlna_flags { get; set; default = DLNAFlags.NONE; }
    public DLNAOperation dlna_operation { get; set;
                                          default = DLNAOperation.NONE; }

    // I know gupnp-av DIDLLiteResource and ProtocolInfo structures have the
    // above fields.  But both proved to be problematic in their current form
    // when used in a variety of containers (appears to be issues with
    // reference management causing (incomplete) copies of
    // DIDLLiteResource/ProtocolInfo to be made). This class can be refactored
    // if/when these classes are made more flexible.

    public MediaResource (string name) {
        this.name = name;
    }

    /**
     * Copy constructor
     */
    public MediaResource.from_resource (string name, MediaResource that) {
        this.name = name;
        // res block
        this.uri = that.uri;
        this.import_uri = that.import_uri;
        this.extension = that.extension;
        this.size = that.size;
        this.cleartext_size = that.cleartext_size;
        this.duration = that.duration;
        this.bitrate = that.bitrate;
        this.bits_per_sample = that.bits_per_sample;
        this.color_depth = that.color_depth;
        this.width = that.width;
        this.height = that.height;
        this.audio_channels = that.audio_channels;
        this.sample_freq = that.sample_freq;
        // protocol info
        this.protocol = that.protocol;
        this.mime_type = that.mime_type;
        this.dlna_profile = that.dlna_profile;
        this.network = that.network;
        this.play_speeds = that.play_speeds;
        this.dlna_conversion = that.dlna_conversion;
        this.dlna_flags = that.dlna_flags;
        this.dlna_operation = that.dlna_operation;
    }

    public MediaResource.from_didl_lite_resource (string name,
                                                  DIDLLiteResource didl_resource) {
        // Create a MediaResource from the given DIDLLiteResource
        // Note: For a DIDLLiteResource, a value of -1/null also signals "not
        // set"
        this.name = name;
        // res block
        this.uri = didl_resource.uri;
        this.size = didl_resource.size64;
        this.cleartext_size = didl_resource.cleartext_size;
        this.duration = didl_resource.duration;
        this.bitrate = didl_resource.bitrate;
        this.bits_per_sample = didl_resource.bits_per_sample;
        this.color_depth = didl_resource.color_depth;
        this.width = didl_resource.width;
        this.height = didl_resource.height;
        this.audio_channels = didl_resource.audio_channels;
        this.sample_freq = didl_resource.sample_freq;
        // protocol info
        if (didl_resource.protocol_info != null) {
            this.protocol = didl_resource.protocol_info.protocol;
            this.mime_type = didl_resource.protocol_info.mime_type;
            this.dlna_profile = didl_resource.protocol_info.dlna_profile;
            this.network = didl_resource.protocol_info.network;
            this.play_speeds = didl_resource.protocol_info.play_speeds;
            this.dlna_conversion = didl_resource.protocol_info.dlna_conversion;
            this.dlna_flags = didl_resource.protocol_info.dlna_flags;
            this.dlna_operation = didl_resource.protocol_info.dlna_operation;
        }
    }

    public MediaResource dup () {
        return new MediaResource.from_resource (this.get_name (), this);
    }

    public string get_name () {
        return this.name;
    }

    public DIDLLiteResource serialize
                                     (DIDLLiteResource didl_resource,
                                      HashTable<string, string>? replacements) {
        // Note: For a DIDLLiteResource, a values -1/null also signal "not set"
        if (replacements == null) {
            didl_resource.uri = this.uri;
        } else {
            didl_resource.uri = MediaObject.apply_replacements (replacements,
                                                                this.uri);
        }
        didl_resource.size64 = this.size;
        didl_resource.cleartext_size = this.cleartext_size;
        didl_resource.duration = this.duration;
        didl_resource.bitrate = this.bitrate;
        didl_resource.bits_per_sample = this.bits_per_sample;
        didl_resource.color_depth = this.color_depth;
        didl_resource.width = this.width;
        didl_resource.height = this.height;<commit_msg>server: Serialize import_uri in MediaResource<commit_after>/*
 * Copyright (C) 2013  Cable Television Laboratories, Inc.
 *
 * Author: <NAME> <<EMAIL>>
 *
 * This file is part of Rygel.
 *
 * Rygel is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

using GUPnP;
using Gee;

/**
 * Represents a media resource (Music, Video, Image, etc).
 */
public class Rygel.MediaResource : GLib.Object {
    private string name;

    // Res block fields
    public string uri { get; set; }
    public string import_uri { get; set; }
    public string extension { get; set; default = null; }
    public int64 size { get; set; default = -1; } // In bytes
    public int64 cleartext_size { get; set; default = -1; } // In bytes
    public long duration { get; set; default = -1; } // In seconds
    public int bitrate { get; set; default = -1; } // In bits per second
    public int bits_per_sample { get; set; default = -1; }
    public int color_depth { get; set; default = -1; } // In bits
    public int width { get; set; default = -1; } // In pixels
    public int height { get; set; default = -1; } // In pixels
    public int audio_channels { get; set; default = -1; }
    public int sample_freq { get; set; default = -1; } // In Hz

    // ProtocolInfo fields
    public string protocol { get; set; default = null; }
    public string mime_type  { get; set; default = null; }
    public string dlna_profile  { get; set; default = null; }
    public string network  { get; set; default = null; }
    public string[] play_speeds = null;
    public DLNAConversion dlna_conversion { get; set;
                                            default = DLNAConversion.NONE; }
    public DLNAFlags dlna_flags { get; set; default = DLNAFlags.NONE; }
    public DLNAOperation dlna_operation { get; set;
                                          default = DLNAOperation.NONE; }

    // I know gupnp-av DIDLLiteResource and ProtocolInfo structures have the
    // above fields.  But both proved to be problematic in their current form
    // when used in a variety of containers (appears to be issues with
    // reference management causing (incomplete) copies of
    // DIDLLiteResource/ProtocolInfo to be made). This class can be refactored
    // if/when these classes are made more flexible.

    public MediaResource (string name) {
        this.name = name;
    }

    /**
     * Copy constructor
     */
    public MediaResource.from_resource (string name, MediaResource that) {
        this.name = name;
        // res block
        this.uri = that.uri;
        this.import_uri = that.import_uri;
        this.extension = that.extension;
        this.size = that.size;
        this.cleartext_size = that.cleartext_size;
        this.duration = that.duration;
        this.bitrate = that.bitrate;
        this.bits_per_sample = that.bits_per_sample;
        this.color_depth = that.color_depth;
        this.width = that.width;
        this.height = that.height;
        this.audio_channels = that.audio_channels;
        this.sample_freq = that.sample_freq;
        // protocol info
        this.protocol = that.protocol;
        this.mime_type = that.mime_type;
        this.dlna_profile = that.dlna_profile;
        this.network = that.network;
        this.play_speeds = that.play_speeds;
        this.dlna_conversion = that.dlna_conversion;
        this.dlna_flags = that.dlna_flags;
        this.dlna_operation = that.dlna_operation;
    }

    public MediaResource.from_didl_lite_resource (string name,
                                                  DIDLLiteResource didl_resource) {
        // Create a MediaResource from the given DIDLLiteResource
        // Note: For a DIDLLiteResource, a value of -1/null also signals "not
        // set"
        this.name = name;
        // res block
        this.uri = didl_resource.uri;
        this.size = didl_resource.size64;
        this.cleartext_size = didl_resource.cleartext_size;
        this.duration = didl_resource.duration;
        this.bitrate = didl_resource.bitrate;
        this.bits_per_sample = didl_resource.bits_per_sample;
        this.color_depth = didl_resource.color_depth;
        this.width = didl_resource.width;
        this.height = didl_resource.height;
        this.audio_channels = didl_resource.audio_channels;
        this.sample_freq = didl_resource.sample_freq;
        // protocol info
        if (didl_resource.protocol_info != null) {
            this.protocol = didl_resource.protocol_info.protocol;
            this.mime_type = didl_resource.protocol_info.mime_type;
            this.dlna_profile = didl_resource.protocol_info.dlna_profile;
            this.network = didl_resource.protocol_info.network;
            this.play_speeds = didl_resource.protocol_info.play_speeds;
            this.dlna_conversion = didl_resource.protocol_info.dlna_conversion;
            this.dlna_flags = didl_resource.protocol_info.dlna_flags;
            this.dlna_operation = didl_resource.protocol_info.dlna_operation;
        }
    }

    public MediaResource dup () {
        return new MediaResource.from_resource (this.get_name (), this);
    }

    public string get_name () {
        return this.name;
    }

    public DIDLLiteResource serialize
                                     (DIDLLiteResource didl_resource,
                                      HashTable<string, string>? replacements) {
        // Note: For a DIDLLiteResource, a values -1/null also signal "not set"
        if (replacements == null) {
            if (this.import_uri != null) {
                didl_resource.import_uri = this.import_uri;
            } else {
                didl_resource.uri = this.uri;
            }
        } else {
            if (this.import_uri != null) {
                didl_resource.import_uri = MediaObject.apply_replacements
                    (replacements, this.import_uri);
            } else {
                didl_resource.uri = MediaObject.apply_replacements (replacements,
                                                                    this.uri);
            }
        }
        didl_resource.size64 = this.size;
        didl_resource.cleartext_size = this.cleartext_size;
        didl_resource.duration = this.duration;
        didl_resource.bitrate = this.bitrate;
        didl_resource.bits_per_sample = this.bits_per_sample;
        didl_resource.color_depth = this.color_depth;
        didl_resource.width = this.width;
        didl_resource.height = this.height;
{'fraction_non_alphanumeric': 0.0856330865305512, 'fraction_numerical': 0.0053847879739735245, 'mean_word_length': 2.239341085271318, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7478258', 'n_tokens_mistral': 3858, 'n_tokens_neox': 3761, 'n_words': 1061}