1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
# =XMPP4R - XMPP Library for Ruby
# License:: Ruby's license (see the LICENSE file) or GNU GPL, at your option.
# Website::http://xmpp4r.github.io
require 'xmpp4r/x'
require 'xmpp4r/jid'
module Jabber
module Dataforms
##
# Data Forms (JEP-0004) implementation
class XData < X
name_xmlns 'x', 'jabber:x:data'
def initialize(type=nil)
super()
self.type = type
end
##
# Search a field by it's var-name
# var:: [String]
# result:: [XDataField] or [nil]
def field(var)
each_element { |xe|
return xe if xe.kind_of?(XDataField) and xe.var == var
}
nil
end
def fields(including_hidden=false)
fields = []
each_element do |xe|
if xe.kind_of?(XDataField) and (including_hidden or
(xe.type != :hidden and xe.type != :fixed))
fields << xe
end
end
fields
end
##
# Type of this Data Form
# result:: * :cancel
# * :form
# * :result
# * :submit
# * nil
def type
case attributes['type']
when 'cancel' then :cancel
when 'form' then :form
when 'result' then :result
when 'submit' then :submit
else nil
end
end
##
# Set the type (see type)
def type=(t)
case t
when :cancel then attributes['type'] = 'cancel'
when :form then attributes['type'] = 'form'
when :result then attributes['type'] = 'result'
when :submit then attributes['type'] = 'submit'
else attributes['type'] = nil
end
end
##
# Get the Data Form title
# return:: [XDataTitle] or nil
def title
first_element('title')
end
##
# Set the Data Form title
# title:: [String]
def title=(title)
delete_elements('title')
add_element(XDataTitle.new(title))
end
##
# Get the Data Form instructions
# return:: [Array] of [XDataInstructions] or nil
def instructions
fields = []
each_element('instructions') do |xe|
fields << xe
end
fields
end
##
# Add Data Form instructions
# i:: [String]
def instructions=(i)
add(XDataInstructions.new(i))
end
end
##
# Child of XData, contains the title of this Data Form
class XDataTitle < XMPPElement
name_xmlns 'title', 'jabber:x:data'
def initialize(title=nil)
super()
add_text(title)
end
def to_s
text.to_s
end
def title
text
end
end
##
# Child of XData, contains the instructions of this Data Form
class XDataInstructions < XMPPElement
name_xmlns 'instructions', 'jabber:x:data'
def initialize(instructions=nil)
super()
add_text(instructions)
end
def to_s
text.to_s
end
def instructions
text
end
end
##
# Child of XData, contains configurable/configured options of this Data Form
class XDataField < XMPPElement
name_xmlns 'field', 'jabber:x:data'
def initialize(var=nil, type=nil)
super()
self.var = var
self.type = type
end
def label
attributes['label']
end
def label=(s)
attributes['label'] = s
end
def var
attributes['var']
end
def var=(s)
attributes['var'] = s
end
##
# Type of this field
# result::
# * :boolean
# * :fixed
# * :hidden
# * :jid_multi
# * :jid_single
# * :list_multi
# * :list_single
# * :text_multi
# * :text_private
# * :text_single
# * nil
def type
case attributes['type']
when 'boolean' then :boolean
when 'fixed' then :fixed
when 'hidden' then :hidden
when 'jid-multi' then :jid_multi
when 'jid-single' then :jid_single
when 'list-multi' then :list_multi
when 'list-single' then :list_single
when 'text-multi' then :text_multi
when 'text-private' then :text_private
when 'text-single' then :text_single
else nil
end
end
##
# Set the type of this field (see type)
def type=(t)
case t
when :boolean then attributes['type'] = 'boolean'
when :fixed then attributes['type'] = 'fixed'
when :hidden then attributes['type'] = 'hidden'
when :jid_multi then attributes['type'] = 'jid-multi'
when :jid_single then attributes['type'] = 'jid-single'
when :list_multi then attributes['type'] = 'list-multi'
when :list_single then attributes['type'] = 'list-single'
when :text_multi then attributes['type'] = 'text-multi'
when :text_private then attributes['type'] = 'text-private'
when :text_single then attributes['type'] = 'text-single'
else attributes['type'] = nil
end
end
##
# Is this field required (has the <required/> child)?
def required?
res = false
each_element('required') { res = true }
res
end
##
# Set if this field is required
# r:: [true] or [false]
def required=(r)
delete_elements('required')
if r
add REXML::Element.new('required')
end
end
##
# Get the values (in a Data Form with type='submit')
def values
res = []
each_element('value') { |e|
res << e.text
}
res
end
##
# Set the values
def values=(ary)
delete_elements('value')
ary.each { |v|
add(REXML::Element.new('value')).text = v
}
end
##
# Get the first value
def value
values.first
end
##
# Remove all and set one value
def value=(val)
self.values = [val]
end
##
# Get the options (in a Data Form with type='form')
def options
res = {}
each_element('option') { |e|
value = nil
e.each_element('value') { |ve| value = ve.text }
res[value] = e.attributes['label']
}
res
end
##
# Set the options
def options=(hsh)
delete_elements('option')
hsh.each { |value,label|
o = add(REXML::Element.new('option'))
o.attributes['label'] = label
o.add(REXML::Element.new('value')).text = value
}
end
end
##
# The <reported/> element, can contain XDataField elements
class XDataReported < XMPPElement
name_xmlns 'reported', 'jabber:x:data'
end
end
end
|