Class: MTP::Object

Inherits:
Object
  • Object
show all
Defined in:
lib/mtp/object.rb

Direct Known Subclasses

Playlist, Track

Constant Summary collapse

PROTECTION_STATUS =
{ 0x0000 => "No protection", 0x0001 => "Read-only", 0x8002 => "Read-only Data", 0x8003 => "Non-transferable data"}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeObject

Returns a new instance of Object.



11
12
13
# File 'lib/mtp/object.rb', line 11

def initialize
  @properties = Properties.new(self)
end

Instance Attribute Details

#associationObject

Returns the value of attribute association.



7
8
9
# File 'lib/mtp/object.rb', line 7

def association
  @association
end

#compressed_sizeObject

Returns the value of attribute compressed_size.



4
5
6
# File 'lib/mtp/object.rb', line 4

def compressed_size
  @compressed_size
end

#dataObject

Returns the value of attribute data.



7
8
9
# File 'lib/mtp/object.rb', line 7

def data
  @data
end

#date_createdObject

Returns the value of attribute date_created.



7
8
9
# File 'lib/mtp/object.rb', line 7

def date_created
  @date_created
end

#date_modifiedObject

Returns the value of attribute date_modified.



7
8
9
# File 'lib/mtp/object.rb', line 7

def date_modified
  @date_modified
end

#filenameObject

Returns the value of attribute filename.



7
8
9
# File 'lib/mtp/object.rb', line 7

def filename
  @filename
end

#formatObject

Returns the value of attribute format.



4
5
6
# File 'lib/mtp/object.rb', line 4

def format
  @format
end

#idObject (readonly)

Returns the value of attribute id.



4
5
6
# File 'lib/mtp/object.rb', line 4

def id
  @id
end

#image_bit_depthObject (readonly)

Returns the value of attribute image_bit_depth.



4
5
6
# File 'lib/mtp/object.rb', line 4

def image_bit_depth
  @image_bit_depth
end

#image_pix_heightObject (readonly)

Returns the value of attribute image_pix_height.



4
5
6
# File 'lib/mtp/object.rb', line 4

def image_pix_height
  @image_pix_height
end

#image_pix_withObject (readonly)

Returns the value of attribute image_pix_with.



4
5
6
# File 'lib/mtp/object.rb', line 4

def image_pix_with
  @image_pix_with
end

#keywordsObject

Returns the value of attribute keywords.



7
8
9
# File 'lib/mtp/object.rb', line 7

def keywords
  @keywords
end

#phObject (readonly)

Returns the value of attribute ph.



4
5
6
# File 'lib/mtp/object.rb', line 4

def ph
  @ph
end

#propertiesObject (readonly)

Returns the value of attribute properties.



4
5
6
# File 'lib/mtp/object.rb', line 4

def properties
  @properties
end

#protection_statusObject (readonly)

Returns the value of attribute protection_status.



4
5
6
# File 'lib/mtp/object.rb', line 4

def protection_status
  @protection_status
end

#sequence_numberObject (readonly)

Returns the value of attribute sequence_number.



4
5
6
# File 'lib/mtp/object.rb', line 4

def sequence_number
  @sequence_number
end

#thumb_compressed_sizeObject (readonly)

Returns the value of attribute thumb_compressed_size.



4
5
6
# File 'lib/mtp/object.rb', line 4

def thumb_compressed_size
  @thumb_compressed_size
end

#thumb_formatObject (readonly)

Returns the value of attribute thumb_format.



4
5
6
# File 'lib/mtp/object.rb', line 4

def thumb_format
  @thumb_format
end

#thumb_pix_heightObject (readonly)

Returns the value of attribute thumb_pix_height.



4
5
6
# File 'lib/mtp/object.rb', line 4

def thumb_pix_height
  @thumb_pix_height
end

#thumb_pix_widthObject (readonly)

Returns the value of attribute thumb_pix_width.



4
5
6
# File 'lib/mtp/object.rb', line 4

def thumb_pix_width
  @thumb_pix_width
end

Class Method Details

.load(ph, id, storage = nil) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/mtp/object.rb', line 46

def self.load(ph, id, storage = nil)
  object = nil
  t = ph.transaction.get_object_info(id)
  t.expect("OK")
  storage_id, format = t.data.payload.unpack("VK")
  object = @@format_classes[format.to_i].new
  object.load(ph, id, t.data.payload)
end

.property_accessor(*properties) ⇒ Object



35
36
37
38
# File 'lib/mtp/object.rb', line 35

def self.property_accessor(*properties)
  Object.property_reader(*properties)
  Object.property_writer(*properties)
end

.property_reader(*properties) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/mtp/object.rb', line 25

def self.property_reader(*properties)
  properties.each do |property|
    Object.class_eval %{
      def #{property}
        properties.#{property}
      end
    }
  end
end

.property_writer(*properties) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/mtp/object.rb', line 15

def self.property_writer(*properties)
  properties.each do |property|
    Object.class_eval %{
      def #{property}=(prop)
        properties.#{property} = prop
      end
    }
  end
end

.register_format(klass, code) ⇒ Object



40
41
42
43
# File 'lib/mtp/object.rb', line 40

def self.register_format(klass, code)
  @@format_classes ||= Hash.new { |h,k| h[k] = Object }
  @@format_classes[code.to_i] = klass
end

Instance Method Details

#<=>(object) ⇒ Object



114
115
116
# File 'lib/mtp/object.rb', line 114

def <=>(object)
  @id <=> object.id 
end

#after_sendingObject



110
111
112
# File 'lib/mtp/object.rb', line 110

def after_sending
  @sent = true
end

#before_sending(ph) ⇒ Object



106
107
108
# File 'lib/mtp/object.rb', line 106

def before_sending(ph)
  @ph = ph
end

#dump_propertiesObject



73
74
75
76
77
78
79
# File 'lib/mtp/object.rb', line 73

def dump_properties
  puts "0x%04x:%16s: %64s %s" % [ 0, "id", @id, "ro" ]
  properties.supported.each do |property|
    p = properties.get_value(property)
    puts "0x%04x:%16s: %64s %s" % [ p.code.to_i, property, p.value, (p.writable? ? 'rw' : 'ro') ]
  end
end

#inspectObject



69
70
71
# File 'lib/mtp/object.rb', line 69

def inspect
  "#<MTP::Object:0x%08x @format=#{@format.name.inspect} @filename=#{@filename.inspect} @parent_id=0x%08x>" % [ @id, @parent_id ]
end

#load(ph, id, payload, storage = nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/mtp/object.rb', line 55

def load(ph, id, payload, storage = nil)
  @id = id
  @ph = ph
  @storage = storage
  @sent = true
  storage_id, @format, @protection_status, @compressed_size, @thumb_format,
  @thumb_compressed_size, @thumb_pix_width, @thumb_pix_height, @image_pix_width, @image_pix_height, @image_bit_depth,
  @parent_id, association_code, association_desc, @sequence_number, @filename, @date_created, @date_modified, @keywords =
    payload.unpack("VKSIKIIIIIIISIIJDDJ")
  @protection_status = Datacode.new(@protection_status, PROTECTION_STATUS)
  @association = Association.load(association_code, association_desc)
  self
end

#new_object?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/mtp/object.rb', line 102

def new_object?
  !@sent
end

#packObject



94
95
96
97
98
99
100
# File 'lib/mtp/object.rb', line 94

def pack
  [ 0, @format, @protection_status || 0, @compressed_size || 0, @thumb_format || 0, # VKSIK
    @thumb_compressed_size || 0, @thumb_pix_width || 0, @thumb_pix_height || 0,     #III
    @image_pix_width || 0, @image_pix_height || 0, @image_bit_depth || 0,           #III
    @parent_id || 0, association.code, association.desc, @sequence_number || 0,     #IKII
    @filename, @date_created, @date_modified, @keywords || "" ].pack("VKSIKIIIIIIIKIIJDDJ")
end

#valid?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/mtp/object.rb', line 89

def valid?
  !@format.nil? and !@compressed_size.nil?
end