powered by Jive Software

How do set and get vCard info?

Hello all,

How do set and get vCard info?

Thanks.

use XIFFIAN

I wrote a vcard extension which is working, but may need some work to make it cleaner.

If you really search for it, you’ll find it!

Thank you.

I had already implemented.

Could you share it?

I’d be interested in improving my library…

Thanks

ok.

as follows:

/**

*/

package com.dream.wq.core.vcard

{

import org.jivesoftware.xiff.data.Extension;

import org.jivesoftware.xiff.data.IExtension;

import org.jivesoftware.xiff.data.ISerializable;

import org.jivesoftware.xiff.data.ExtensionClassRegistry;

import flash.xml.XMLNode;

public class VCardExtension extends Extension implements IExtension, ISerializable

{

// Static class variables to be overridden in subclasses;

public static var NS:String = “vcard-temp”;

public static var ELEMENT:String = “vCard”;

private var myFields:Object;

private static var staticDepends:Array = ;

public function VCardExtension( parent:XMLNode=null )

{

super( parent );

myFields = new Object();

}

/**

  • Gets the namespace associated with this extension.

  • The namespace for the VCardExtension is “vcard-temp”.

  • @return The namespace

  • @availability Flash Player 7

*/

public function getNS():String

{

return VCardExtension.NS;

}

/**

  • Gets the element name associated with this extension.

  • The element for this extension is “vCard”.

  • @return The element name

  • @availability Flash Player 7

*/

public function getElementName():String

{

return VCardExtension.ELEMENT;

}

/**

  • Performs the registration of this extension into the extension registry.

  • @availability Flash Player 7

*/

public static function enable():void

{

ExtensionClassRegistry.register(VCardExtension);

}

public function getFields():Object{

return myFields;

}

public function getField(name:String):String

{

if(myFields[name].firstChild != null){

return myFields[name].firstChild.nodeValue;

}

return “”;

}

public function setField(name:String, val:String):void

{

myFields[name] = replaceTextNode(getNode(), myFields[name], name, val);

}

/**

  • Serializes the VCardExtension data to XML for sending.
  • @availability Flash Player 7

  • @param parent The parent node that this extension should be serialized into

  • @return An indicator as to whether serialization was successful

*/

public function serialize( parent:XMLNode ):Boolean

{

var node:XMLNode = getNode();

if( !exists( getNode().parentNode ) ) {

parent.appendChild( getNode().cloneNode( true ) );

}

return true;

}

/**

  • Deserializes the VCardExtension data.
  • @availability Flash Player 7

  • @param node The XML node associated this data

  • @return An indicator as to whether deserialization was successful

*/

public function deserialize( node:XMLNode ):Boolean

{

setNode( node );

var children:Array = getNode().childNodes;

for (var i:String in children) {

myFields[children[http://i].nodeName|http://i].nodeName] = children[i];

}

return true;

}

}

}

/**

*/

package com.dream.wq.core.vcard

{

import org.jivesoftware.xiff.core.XMPPConnection;

import org.jivesoftware.xiff.data.IQ;

import org.jivesoftware.xiff.data.XMPPStanza;

import flash.events.EventDispatcher;

public class VCard extends EventDispatcher

{

private var myConnection:XMPPConnection;

public function VCard( aConnection:XMPPConnection = null)

{

if (aConnection != null){

VCardExtension.enable();

myConnection = aConnection;

myConnection.addEventListener( VCardExtension.NS, handleEvent );

}

}

public function setVCard(myjid:String, fieldMap:Object):void

{

var callbackObj:VCard = this;

var callbackMethod:String = “setVCard_result”;

var tempIQ:IQ = new IQ (null, IQ.SET_TYPE, XMPPStanza.generateID(“set_vCard_”), callbackMethod, callbackObj );

var ext:VCardExtension = new VCardExtension( tempIQ.getNode() );

for( var i:String in fieldMap ) {

ext.setField(i, fieldMap[i]);

}

tempIQ.from = myjid;

tempIQ.addExtension( ext );

myConnection.send( tempIQ );

}

public function setVCard_result( resultIQ:IQ ):void

{

trace(resultIQ);

}

public function fetchVCard(myjid:String, fjid:String):void

{

var tempIQ:IQ = new IQ( fjid, IQ.GET_TYPE, XMPPStanza.generateID(“fetch_vCard_”), “fetchVCard_result”, this );

tempIQ.addExtension( new VCardExtension( tempIQ.getNode() ) );

tempIQ.from = myjid;

myConnection.send( tempIQ );

}

public function fetchVCard_result( resultIQ:IQ ):void

{

try

{

var exts:Array = resultIQ.getAllExtensionsByNS( VCardExtension.NS );

var len:int = exts.length;

for (var x:int= 0; x < len; x++){

var ext:VCardExtension = exts[x];

var map:Object = ext.getFields();

var keyValues:Array = new Array();

for( var i:String in map ) {

trace(ext.getField(i));

keyValues[i] = ext.getField(i);

}

var ev: VCardEvent = new VCardEvent(VCardEvent.VCARD_GET, false, false);

ev.jid = resultIQ.from;

ev.data = keyValues;

map = null;

ext = null;

dispatchEvent(ev);

}

}

catch (e:Error)

{

trace(e.getStackTrace());

}

}

private function handleEvent( eventObj:* ):void

{

if( eventObj.type == VCardExtension.NS)

{

try

{

var tempIQ:IQ = eventObj.iq as IQ;

var ext:VCardExtension = tempIQ.getAllExtensionsByNS( VCardExtension.NS )[0] as VCardExtension;

return;

}

catch (e:Error)

{

trace(e.getStackTrace());

}

}

}

public function set connection( aConnection:XMPPConnection ):void

{

myConnection = aConnection;

// Set up listeners

myConnection.addEventListener( VCardExtension.NS, handleEvent );

}

}

}