package org.javwer;
import java.util.Iterator;
import java.util.Map;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.events.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.jivesoftware.smack.*;
import org.jivesoftware.smackx.*;
import org.jivesoftware.smack.packet.*;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smackx.ServiceDiscoveryManager;
import org.jivesoftware.smackx.packet.DiscoverInfo;
import org.jivesoftware.smackx.packet.DiscoverItems;
import org.jivesoftware.smackx.ReportedData;
import org.jivesoftware.smackx.search.*;
public class ServiceDiscovery {
private JRoster parent;
private ServiceDiscoveryManager manager;
private Tree tree;
private Shell discoShell;
ServiceDiscovery( JRoster fParent, String entityID ) {
parent = fParent;
discoShell = new Shell( parent.getDisplay(), SWT.SHELL_TRIM );
discoShell.setText( "Service Discovery at " + entityID );
discoShell.setLayout( new FillLayout() );
tree = new Tree( discoShell, SWT.SINGLE | SWT.BORDER );
tree.setHeaderVisible( true );
TreeColumn idColumn, nameColumn , nodeColumn, infoColumn;
idColumn = new TreeColumn( tree, SWT.LEFT, 0 );
nameColumn = new TreeColumn( tree, SWT.LEFT );
nodeColumn = new TreeColumn( tree, SWT.LEFT );
infoColumn = new TreeColumn( tree, SWT.LEFT );
idColumn.setText( "ID" );
idColumn.setWidth( 150 );
idColumn.setResizable( true );
nameColumn.setText( "Name" );
nameColumn.setWidth( 100 );
nameColumn.setResizable( true );
nodeColumn.setText( "Node" );
nodeColumn.setWidth( 100 );
nodeColumn.setResizable( true );
infoColumn.setText( "Type > Category > Name" );
infoColumn.setWidth( 100 );
infoColumn.setResizable( true );
manager = ServiceDiscoveryManager.getInstanceFor( parent.getJavwerManager().getConnection() );
TreeItem item = new TreeItem( tree, SWT.NONE );
try {
DiscoverInfo info = manager.discoverInfo( entityID );
item.setData( "jabber:iq:register", Boolean.toString( info.containsFeature( "jabber:iq:register" ) ) );
item.setData( "jabber:iq:search", Boolean.toString( info.containsFeature( "jabber:iq:search" ) ) );
item.setData( "http://jabber.org/protocol/muc", Boolean.toString( info.containsFeature( "http://jabber.org/protocol/muc" ) ) );
} catch( Exception e ) {}
//item.setData( "jabber:iq:register", "true" );
item.setText( entityID );
displayEntityTree( item, entityID, null );
// Contex-Menu
Menu contextMenu = new Menu( discoShell, SWT.POP_UP );
final MenuItem registerItem = new MenuItem( contextMenu, SWT.PUSH );
registerItem.setText( "&Register" );
registerItem.addSelectionListener( new SelectionAdapter() {
public void widgetSelected( SelectionEvent ev ) {
showRegisterDialog( parent.getJavwerManager(), tree.getSelection()[ 0 ].getText( 0 ) );
}
});
final MenuItem searchItem = new MenuItem( contextMenu, SWT.PUSH );
searchItem.setText( "&Search" );
searchItem.addSelectionListener( new SelectionAdapter() {
public void widgetSelected( SelectionEvent ev ) {
showSearchDialog( tree.getSelection()[ 0 ].getText( 0 ) );
}
});
final MenuItem groupItem = new MenuItem( contextMenu, SWT.PUSH );
groupItem.setText( "&Join" );
groupItem.addSelectionListener( new SelectionAdapter() {
public void widgetSelected( SelectionEvent ev ) {
parent.createGroupChat( "blubb@" + tree.getSelection()[ 0 ].getText( 0 ), null, null );
}
});
contextMenu.addMenuListener( new MenuListener() {
public void menuHidden( MenuEvent arg0 ) {
}
public void menuShown( MenuEvent arg0 ) {
registerItem.setEnabled( ( "true" == (String) tree.getSelection()[ 0 ].getData( "jabber:iq:register" ) ) ? true : false );
searchItem.setEnabled( ( "true" == (String) tree.getSelection()[ 0 ].getData( "jabber:iq:search" ) ) ? true : false );
groupItem.setEnabled( ( "true" == (String) tree.getSelection()[ 0 ].getData( "http://jabber.org/protocol/muc" ) ) ? true : false );
}
});
tree.setMenu( contextMenu );
discoShell.open();
}
private void displayEntityTree( TreeItem parent, String entityID, String node ) {
try {
DiscoverItems items;
if( node == null )
items = manager.discoverItems( entityID );
else
items = manager.discoverItems( entityID, node );
//System.out.println("displaying " + entityID + "... ");
// if ( items.getItems().hasNext() )
// System.out.println(entityID + " has children :)\n" );
for( Iterator i = items.getItems(); i.hasNext(); ) {
DiscoverItems.Item item = (DiscoverItems.Item) i.next();
TreeItem treeItem = new TreeItem( parent, SWT.NONE );
parent.setExpanded( true );
String text[] = { item.getEntityID(), item.getName(), item.getNode(), item.getAction() };
treeItem.setText( text );
DiscoverInfo info = manager.discoverInfo( item.getEntityID() );
treeItem.setData( "jabber:iq:register", Boolean.toString( info.containsFeature( "jabber:iq:register" ) ) );
treeItem.setData( "jabber:iq:search", Boolean.toString( info.containsFeature( "jabber:iq:search" ) ) );
treeItem.setData( "http://jabber.org/protocol/muc", Boolean.toString( info.containsFeature( "http://jabber.org/protocol/muc" ) ) );
for( Iterator j = info.getIdentities(); j.hasNext(); ) {
DiscoverInfo.Identity ident = (DiscoverInfo.Identity) j.next();
treeItem.setText( 3, ident.getType() + " > " + ident.getCategory() + " > " + ident.getName() );
}
/*
for( Iterator j = manager.discoverInfo( item.getEntityID() ).getIdentities(); j.hasNext(); ) {
DiscoverInfo.Identity identity = (DiscoverInfo.Identity) j.next();
System.out.println( item.getName() + ": " + identity.getName() + " - " + identity.getCategory() + " - " + identity.getType() );
}
*/
displayEntityTree( treeItem, item.getEntityID(), item.getNode() );
}
} catch( XMPPException ex ) { System.err.println( "er��r" ); }
}
private void showSearchDialog( final String jid ) {
UserSearch userSearch = new UserSearch();
try {
Form form = userSearch.getSearchForm( parent.getJavwerManager().getConnection(), jid );
Form request = JavwerManager.displayForm( parent.getDisplay(), form );
ReportedData result = userSearch.sendSearchForm( parent.getJavwerManager().getConnection(), request, jid );
if( result != null ) {
Shell resultShell = new Shell( parent.getDisplay() );
if( result.getTitle() != null )
resultShell.setText( result.getTitle() );
resultShell.setLayout( new FillLayout() );
final Table table = new Table( resultShell, SWT.SINGLE );
table.setHeaderVisible( true );
for( Iterator i = result.getColumns(); i.hasNext(); ) {
ReportedData.Column dataColumn = (ReportedData.Column) i.next();
final TableColumn tableColumn = new TableColumn( table, SWT.LEFT );
if( dataColumn.getLabel() != null )
tableColumn.setText( dataColumn.getLabel() );
else
tableColumn.setText( "blubb" );
tableColumn.setWidth( 100 );
tableColumn.setResizable( true );
tableColumn.setMoveable( true );
/*
tableColumn.addSelectionListener( new SelectionAdapter() {
public void widgetSelected( SelectionEvent ev ) {
table.setSortColumn( tableColumn );
table.setSortDirection( SWT.DOWN );
TableItem items[] = table.getItems();
Arrays.sort( items, new Comparator<TableItem>() {
public int compare( TableItem o1, TableItem o2 ) {
int col = 0;
for( int i = 0; i < table.getColumnCount(); i++ )
if( table.getSortColumn().equals( table.getColumn( i ) ) )
col = i;
return o1.getText( col ).compareTo( o2.getText( col ) );
}
} );
for( int i = 0; i < items.length; i++ ) {
items[ i ].set
}
}
});
*/
}
for( Iterator i = result.getRows(); i.hasNext(); ) {
ReportedData.Row dataRow = (ReportedData.Row) i.next();
TableItem tableItem = new TableItem( table, SWT.NONE );
//int x = 0;
for( int x = 0; x < table.getColumnCount(); x++ ) {
String text = (String) dataRow.getValues( table.getColumn( x ).getText() ).next();
if( text != null )
tableItem.setText( x, text );
else
tableItem.setText( x, "#" );
}
}
resultShell.open();
}
} catch( XMPPException ex ) { System.err.println( "er " + ex ); }
}
public static void showRegisterDialog( final JavwerManager javwerManager, final String jid ) {
Registration request = new Registration();
request.setType( IQ.Type.GET );
request.setTo( jid );
final XMPPConnection connection = javwerManager.getConnection();
connection.addPacketListener(
new PacketListener() {
public void processPacket( final Packet p ) {
javwerManager.getDisplay().syncExec( new Runnable() {
public void run() {
if( Form.getFormFrom( p ) != null ) {
Form answerForm = JavwerManager.displayForm( javwerManager.getDisplay(), Form.getFormFrom( p ) );
Registration answerRegistration = new Registration();
answerRegistration.setTo( jid );
answerRegistration.setType( IQ.Type.SET );
answerRegistration.addExtension( answerForm.getDataFormToSend() );
connection.sendPacket( answerRegistration);
}
else {
Registration form = (Registration) p;
final Shell registerShell = new Shell( javwerManager.getDisplay() );
GridLayout layout = new GridLayout();
layout.numColumns = 2;
layout.makeColumnsEqualWidth = true;
if( form.getInstructions() != null )
registerShell.setText( form.getInstructions() );
registerShell.setLayout( layout );
Label instructionsLabel = new Label( registerShell, SWT.WRAP );
if( form.getInstructions() != null )
instructionsLabel.setText( form.getInstructions() );
GridData data = new GridData();
data.horizontalSpan = 2;
instructionsLabel.setLayoutData( data );
//final Map<Object, String> attributes = (Mapform.getAttributes();
final Map attributes = form.getAttributes();
final Label labels[] = new Label[ attributes.keySet().size() ];
final Text texts[] = new Text[ attributes.keySet().size() ];
int x = 0;
for( Iterator i = attributes.keySet().iterator(); i.hasNext(); x++ ) {
String key = (String) i.next();
String value = (String) attributes.get( key );
labels[ x ] = new Label( registerShell, SWT.WRAP );
labels[ x ].setText( key );
texts[ x ] = new Text( registerShell, SWT.NONE );
texts[ x ].setLayoutData( new GridData( GridData.FILL_HORIZONTAL ) );
if( value != null )
texts[ x ].setText( value );
}
Button okButton = new Button( registerShell, SWT.PUSH );
okButton.addSelectionListener( new SelectionAdapter() {
public void widgetSelected( SelectionEvent ev ) {
int x = 0;
for( Iterator i = attributes.keySet().iterator(); i.hasNext(); x++ ) {
attributes.put( (String) i.next(), (String) texts[ x ].getText() );
}
Registration answer = new Registration();
answer.setAttributes( attributes );
answer.setType( IQ.Type.SET );
answer.setTo( jid );
connection.sendPacket( answer );
registerShell.close();
}
});
okButton.setText( "&OK" );
Button cancelButton = new Button( registerShell, SWT.PUSH );
cancelButton.addSelectionListener( new SelectionAdapter() {
public void widgetSelected( SelectionEvent ev ) {
registerShell.close();
}
});
cancelButton.setText( "&Cancel" );
registerShell.pack();
registerShell.open();
}
}
});
//Form answerForm = JavwerManager.displayForm( parent.getDisplay(), Form.getFormFrom( p ) );
//Registration answer = new Registration();
////answer.addExtension( answerForm.getDataFormToSend() );
//answer.setType( IQ.Type.SET );
//answer.setTo( jid );
//connection.sendPacket( answer );
}
},
new AndFilter( new PacketIDFilter( request.getPacketID() ), new PacketTypeFilter( Registration.class ) )
);
connection.sendPacket( request );
}
}