Package net.sphene.goim.rcp.ui.wizard.addcontact

Source Code of net.sphene.goim.rcp.ui.wizard.addcontact.WizardContactOptions

/*
* File    : WizardContactOptions.java
* Created : 29.12.2005
* By      : kahless
*
* GOIM - Gamers Own Instant Messenger
* Copyright (C) 2005 Herbert Poul (kahless@sphene.net / herbert.poul@gmail.com)
* http://goim.sphene.net
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU 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
*/
package net.sphene.goim.rcp.ui.wizard.addcontact;

import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;

import net.sphene.goim.rcp.beans.GOIMAccount;
import net.sphene.goim.rcp.xmpp.util.JabberErrorConditionMapping;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.RosterGroup;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.RosterPacket.ItemType;
import org.jivesoftware.smackx.packet.VCard;

public class WizardContactOptions extends WizardPage {
  GOIMAccount account;
 
  String lastJid = null;
  boolean gotVCard = false;

  public Text txtNickname;

  public Combo cmbGroup;

  public String authRequestJID;
  public Button buttonAuthorize;
  public boolean askForAddition = true;

  public Button buttonAskForAuthorization;

  public WizardContactOptions(GOIMAccount account, String authRequestJID) {
    this(account);
    this.authRequestJID = authRequestJID;
    this.setTitle("Requesting Authorization");
    this.setDescription(authRequestJID + " is requesting Authorization.");
    RosterEntry entry = account.xmpp.getRoster().getEntry(authRequestJID);
    if(entry == null) {
      askForAddition = true;
    } else {
      askForAddition = entry.getType() != ItemType.BOTH &&
        entry.getType() != ItemType.TO;
    }
  }
  public WizardContactOptions(GOIMAccount account) {
    super("enterJabberId");
    this.account = account;
    this.setTitle("Adding Jabber Contact");
    this.setDescription("Adding a jabber contact to your roster.");
    setPageComplete(false);
  }
 
  public boolean isCurrentPage() { return super.isCurrentPage(); }

  @Override
  public boolean isPageComplete() {
    if(isCurrentPage()) {
      String jid = authRequestJID == null ? ((WizardEnterJabberIdPage)getPreviousPage()).txtJabberId.getText() : authRequestJID;
      if(askForAddition && (!gotVCard || lastJid == null || !lastJid.equals(jid))) {
        gotVCard = false;
        lastJid = jid;
        try {
          getContainer().run(true,true,new IRunnableWithProgress() {
            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
              monitor.beginTask("Retrieving VCard.",2);
              monitor.worked(1);
              VCard vCard = new VCard();
              try {
                vCard.load(account.xmpp.getConnection(),lastJid);
              } catch (XMPPException e) {
                // ignored for now ...
                //throw new RuntimeException(e);
              }
              String nickname = vCard.getNickName();
              if(nickname == null || nickname.equals(""))
                nickname = lastJid;
              final String nick = nickname;
              txtNickname.getDisplay().asyncExec(new Runnable() { public void run() {
                txtNickname.setText(nick);
              }});
              gotVCard = true;
              monitor.worked(1);
              monitor.done();
            } });
        } catch (Exception e) {
//          e.printStackTrace();
          Throwable t = e.getCause();
          String msg = "Error retrieving VCard";
          if(t != null)
            t = t.getCause();
          if(t != null && t instanceof XMPPException) {
            XMPPException x = (XMPPException)t;
            if(x.getXMPPError()!=null) {
              msg = msg + " - " + x.getXMPPError().getCode() + " " + JabberErrorConditionMapping.getMeaning(x.getXMPPError().getCode());
              if(x.getXMPPError().getMessage() != null)
                msg = msg + ": " + x.getXMPPError().getMessage();
            }
          }
          setMessage(msg,WARNING);
//          throw new RuntimeException(msg,e);
        }
      }
    }
    return true;
  }

  public void createControl(Composite parent) {
    Composite topLevel = new Composite(parent,SWT.NULL);
    setControl(topLevel);
   
    topLevel.setLayout(new GridLayout(2,true));
   
    if(authRequestJID != null) {
      Label lblAuthRequestDescr = new Label(topLevel,SWT.NULL);
      lblAuthRequestDescr.setLayoutData(new GridData(SWT.CENTER,SWT.CENTER,true,false,2,1));
      lblAuthRequestDescr.setText(authRequestJID + " is asking for your authorization.");
     
      buttonAuthorize = new Button(topLevel,SWT.CHECK);
      buttonAuthorize.setText("Authorize User");
      buttonAuthorize.setLayoutData(new GridData(SWT.CENTER,SWT.CENTER,true,false,2,1));
      buttonAuthorize.setSelection(true);
     
      if(askForAddition) {
        buttonAskForAuthorization = new Button(topLevel,SWT.CHECK);
        buttonAskForAuthorization.setText("Ask for Authorization");
        buttonAskForAuthorization.setLayoutData(new GridData(SWT.CENTER,SWT.CENTER,true,false,2,1));
        buttonAskForAuthorization.setSelection(true);
      }
    }

    if(askForAddition) {
      Label lblNickname = new Label(topLevel,SWT.NULL);
      lblNickname.setText("Nickname");
      lblNickname.setLayoutData(new GridData(SWT.CENTER,SWT.CENTER,true,false));
     
      txtNickname = new Text(topLevel,SWT.BORDER);
      txtNickname.setLayoutData(new GridData(SWT.FILL,SWT.CENTER,true,false));
     
      Label lblGroup = new Label(topLevel,SWT.NULL);
      lblGroup.setText("Add To Group");
      lblGroup.setLayoutData(new GridData(SWT.CENTER,SWT.CENTER,true,false));
     
      cmbGroup = new Combo(topLevel,SWT.READ_ONLY|SWT.DROP_DOWN);
      cmbGroup.setLayoutData(new GridData(SWT.FILL,SWT.CENTER,true,false));
      cmbGroup.add("Default (No Group)");
      cmbGroup.select(0);
      Iterator i = account.xmpp.getRoster().getGroups();
      while(i.hasNext()) {
        RosterGroup group = (RosterGroup)i.next();
        cmbGroup.add(group.getName());
      }
    }
  }
}
TOP

Related Classes of net.sphene.goim.rcp.ui.wizard.addcontact.WizardContactOptions

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.