Search in sources :

Example 21 with XMPPError

use of org.jivesoftware.smack.packet.XMPPError in project xabber-android by redsolution.

the class MUCManager method joinRoom.

/**
     * Requests to join to the room.
     *
     * @param requested Whether user request to join the room.
     */
public void joinRoom(final String account, final String room, boolean requested) {
    final XMPPConnection xmppConnection;
    final RoomChat roomChat;
    final String nickname;
    final String password;
    final Thread thread;
    roomChat = getRoomChat(account, room);
    if (roomChat == null) {
        Application.getInstance().onError(R.string.ENTRY_IS_NOT_FOUND);
        return;
    }
    RoomState state = roomChat.getState();
    if (state == RoomState.available || state == RoomState.occupation) {
        Application.getInstance().onError(R.string.ALREADY_JOINED);
        return;
    }
    if (state == RoomState.creating || state == RoomState.joining) {
        Application.getInstance().onError(R.string.ALREADY_IN_PROGRESS);
        return;
    }
    nickname = roomChat.getNickname();
    password = roomChat.getPassword();
    requestToWriteRoom(account, room, nickname, password, true);
    ConnectionThread connectionThread = AccountManager.getInstance().getAccount(account).getConnectionThread();
    if (connectionThread == null) {
        Application.getInstance().onError(R.string.NOT_CONNECTED);
        return;
    }
    xmppConnection = connectionThread.getXMPPConnection();
    if (xmppConnection == null) {
        Application.getInstance().onError(R.string.NOT_CONNECTED);
        return;
    }
    final MultiUserChat multiUserChat;
    try {
        multiUserChat = MultiUserChatManager.getInstanceFor(xmppConnection).getMultiUserChat(room);
    } catch (IllegalStateException e) {
        Application.getInstance().onError(R.string.NOT_CONNECTED);
        return;
    }
    roomChat.setState(RoomState.joining);
    roomChat.setMultiUserChat(multiUserChat);
    roomChat.setRequested(requested);
    thread = new Thread("Join to room " + room + " from " + account) {

        @Override
        public void run() {
            try {
                if (roomChat.getMultiUserChat() != multiUserChat) {
                    return;
                }
                multiUserChat.join(nickname, password);
                Application.getInstance().runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        if (roomChat.getMultiUserChat() != multiUserChat) {
                            return;
                        }
                        if (roomChat.getState() == RoomState.joining) {
                            roomChat.setState(RoomState.occupation);
                        }
                        removeAuthorizationError(account, room);
                        RosterManager.getInstance().onContactChanged(account, room);
                    }
                });
                return;
            } catch (final XMPPException.XMPPErrorException e) {
                Application.getInstance().runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        if (roomChat.getMultiUserChat() != multiUserChat) {
                            return;
                        }
                        roomChat.setState(RoomState.error);
                        addAuthorizationError(account, room);
                        XMPPError xmppError = e.getXMPPError();
                        if (xmppError != null && xmppError.getCondition() == XMPPError.Condition.conflict) {
                            Application.getInstance().onError(R.string.NICK_ALREADY_USED);
                        } else if (xmppError != null && xmppError.getCondition() == XMPPError.Condition.not_authorized) {
                            Application.getInstance().onError(R.string.AUTHENTICATION_FAILED);
                        } else {
                            Application.getInstance().onError(R.string.NOT_CONNECTED);
                        }
                        RosterManager.getInstance().onContactChanged(account, room);
                    }
                });
                return;
            } catch (IllegalStateException e) {
            } catch (Exception e) {
                LogManager.exception(this, e);
            }
            Application.getInstance().runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    if (roomChat.getMultiUserChat() != multiUserChat) {
                        return;
                    }
                    roomChat.setState(RoomState.waiting);
                    Application.getInstance().onError(R.string.NOT_CONNECTED);
                    RosterManager.getInstance().onContactChanged(account, room);
                }
            });
        }
    };
    thread.setDaemon(true);
    thread.start();
}
Also used : MultiUserChat(org.jivesoftware.smackx.muc.MultiUserChat) XMPPConnection(org.jivesoftware.smack.XMPPConnection) XMPPError(org.jivesoftware.smack.packet.XMPPError) SmackException(org.jivesoftware.smack.SmackException) NetworkException(com.xabber.android.data.NetworkException) XMPPException(org.jivesoftware.smack.XMPPException) ConnectionThread(com.xabber.android.data.connection.ConnectionThread) ConnectionThread(com.xabber.android.data.connection.ConnectionThread)

Example 22 with XMPPError

use of org.jivesoftware.smack.packet.XMPPError in project Spark by igniterealtime.

the class JoinRoomSwingWorker method construct.

@Override
public Object construct() {
    try {
        groupChat = MultiUserChatManager.getInstanceFor(SparkManager.getConnection()).getMultiUserChat(roomJID);
        // Create a UI component, if one was not yet created. It is important that this happens before the MUC is
        // joined server-side, as the UI component needs to be able to display data that is sent by the server upon
        // joining the room.
        ChatRoom room;
        try {
            final String roomName = XmppStringUtils.parseBareJid(groupChat.getRoom());
            room = SparkManager.getChatManager().getChatContainer().getChatRoom(roomName);
        } catch (ChatRoomNotFoundException e) {
            room = UIComponentRegistry.createGroupChatRoom(groupChat);
            ((GroupChatRoom) room).setPassword(password);
            ((GroupChatRoom) room).setTabTitle(tabTitle);
        }
        // Use the default nickname, if none has been provided.
        if (!ModelUtil.hasLength(nickname)) {
            nickname = SettingsManager.getRelodLocalPreferences().getNickname().trim();
        }
        // Join the MUC server-sided, if we're not already in.
        if (!groupChat.isJoined()) {
            if (password == null && ConferenceUtils.isPasswordRequired(roomJID)) {
                JLabel label = new JLabel(Res.getString("message.enter.room.password"));
                JPasswordField passwordField = new JPasswordField();
                passwordField.addAncestorListener(new RequestFocusListener());
                JOptionPane.showConfirmDialog(null, new Object[] { label, passwordField }, Res.getString("title.password.required"), JOptionPane.OK_CANCEL_OPTION);
                password = new String(passwordField.getPassword());
                if (!ModelUtil.hasLength(password)) {
                    return null;
                }
            }
            if (!ConferenceUtils.confirmToRevealVisibility()) {
                return null;
            }
            if (ModelUtil.hasLength(password)) {
                groupChat.join(nickname, password);
            } else {
                groupChat.join(nickname);
            }
        }
        return room;
    } catch (XMPPException | SmackException ex) {
        Log.error("An exception occurred while trying to join room '" + roomJID + "'.", ex);
        XMPPError error = null;
        if (ex instanceof XMPPException.XMPPErrorException) {
            error = ((XMPPException.XMPPErrorException) ex).getXMPPError();
            if (XMPPError.Condition.conflict.equals(error.getCondition())) {
                final Object userInput = JOptionPane.showInputDialog(SparkManager.getMainWindow(), Res.getString("message.nickname.in.use"), Res.getString("title.change.nickname"), JOptionPane.WARNING_MESSAGE, null, // null selection values implies text field.
                null, nickname);
                if (userInput != null) {
                    Log.debug("Retry joining room '" + roomJID + "', using nickname: " + userInput);
                    this.nickname = (String) userInput;
                    return construct();
                }
            }
        }
        final String errorText = ConferenceUtils.getReason(error);
        errors.add(errorText);
        return null;
    }
}
Also used : SmackException(org.jivesoftware.smack.SmackException) XMPPError(org.jivesoftware.smack.packet.XMPPError) ChatRoom(org.jivesoftware.spark.ui.ChatRoom) GroupChatRoom(org.jivesoftware.spark.ui.rooms.GroupChatRoom) ChatRoomNotFoundException(org.jivesoftware.spark.ui.ChatRoomNotFoundException) RequestFocusListener(org.jivesoftware.spark.ui.RequestFocusListener) XMPPException(org.jivesoftware.smack.XMPPException)

Example 23 with XMPPError

use of org.jivesoftware.smack.packet.XMPPError in project Spark by igniterealtime.

the class VCardManager method reloadVCard.

/**
 * Forces a reload of a <code>VCard</code>. To load a VCard you should use
 * getVCard(String jid) instead. This method will perform a network lookup
 * which could take some time. If you're having problems with request
 * timeout you should also use getVCard(String jid). Use addToQueue(String
 * jid) if you want VCardManager to update the VCard by the given jid. The
 * method will block until the result is available or a timeout occurs.
 *
 * @param jid
 *            the jid of the user.
 *
 * @return the new network vCard or a vCard with an error
 */
public VCard reloadVCard(String jid) {
    jid = XmppStringUtils.parseBareJid(jid);
    VCard vcard = new VCard();
    try {
        vcard.setJabberId(jid);
        vcard.load(SparkManager.getConnection(), jid);
        if (vcard.getNickName() != null && vcard.getNickName().length() > 0) {
            // update nickname.
            ContactItem item = SparkManager.getWorkspace().getContactList().getContactItemByJID(jid);
            item.setNickname(vcard.getNickName());
        // TODO: this doesn't work if someone removes his nickname. If we remove it in that case, it will cause problems with people using another way to manage their nicknames.
        }
        addVCard(jid, vcard);
        persistVCard(jid, vcard);
    } catch (XMPPException | SmackException e) {
        // //System.out.println(jid+" Fehler in reloadVCard ----> null");
        vcard.setError(new XMPPError(XMPPError.Condition.resource_constraint));
        vcard.setJabberId(jid);
        delayedContacts.add(jid);
        return vcard;
    // We dont want cards with error
    // vcard.setError(new XMPPError(XMPPError.Condition.request_timeout));
    // addVCard(jid, vcard);
    }
    return vcard;
}
Also used : ContactItem(org.jivesoftware.spark.ui.ContactItem) SmackException(org.jivesoftware.smack.SmackException) XMPPError(org.jivesoftware.smack.packet.XMPPError) XMPPException(org.jivesoftware.smack.XMPPException) VCard(org.jivesoftware.smackx.vcardtemp.packet.VCard)

Example 24 with XMPPError

use of org.jivesoftware.smack.packet.XMPPError in project Spark by igniterealtime.

the class VCardManager method reloadPersonalVCard.

/**
 * Loads the vcard for this Spark user
 * @return this users VCard.
 */
public void reloadPersonalVCard() {
    try {
        personalVCard.load(SparkManager.getConnection());
        personalVCardAvatar = personalVCard.getAvatar();
        // reload lazy later, when need
        personalVCardHash = null;
        // If VCard is loaded, then save the avatar to the personal folder.
        if (personalVCardAvatar != null && personalVCardAvatar.length > 0) {
            ImageIcon icon = new ImageIcon(personalVCardAvatar);
            icon = VCardManager.scale(icon);
            if (icon.getIconWidth() != -1) {
                BufferedImage image = GraphicUtils.convert(icon.getImage());
                ImageIO.write(image, "PNG", imageFile);
            }
        }
    } catch (Exception e) {
        personalVCard.setError(new XMPPError(XMPPError.Condition.conflict));
        personalVCardAvatar = null;
        personalVCardHash = null;
        Log.error(e);
    }
}
Also used : XMPPError(org.jivesoftware.smack.packet.XMPPError) BufferedImage(java.awt.image.BufferedImage) SmackException(org.jivesoftware.smack.SmackException) MalformedURLException(java.net.MalformedURLException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) XMPPException(org.jivesoftware.smack.XMPPException)

Example 25 with XMPPError

use of org.jivesoftware.smack.packet.XMPPError in project xabber-android by redsolution.

the class ReceiptManager method markAsError.

private void markAsError(final AccountJid account, final Message message) {
    Realm realm = MessageDatabaseManager.getInstance().getNewBackgroundRealm();
    realm.beginTransaction();
    MessageItem first = realm.where(MessageItem.class).equalTo(MessageItem.Fields.ACCOUNT, account.toString()).equalTo(MessageItem.Fields.STANZA_ID, AbstractChat.getStanzaId(message)).findFirst();
    if (first != null) {
        first.setError(true);
        XMPPError error = message.getError();
        if (error != null) {
            String errorStr = error.toString();
            String descr = error.getDescriptiveText(null);
            first.setErrorDescription(errorStr + "\n" + descr);
        }
    }
    realm.commitTransaction();
    realm.close();
    EventBus.getDefault().post(new MessageUpdateEvent(account));
}
Also used : MessageItem(com.xabber.android.data.database.messagerealm.MessageItem) XMPPError(org.jivesoftware.smack.packet.XMPPError) Realm(io.realm.Realm)

Aggregations

XMPPError (org.jivesoftware.smack.packet.XMPPError)36 XMPPException (org.jivesoftware.smack.XMPPException)11 IQ (org.jivesoftware.smack.packet.IQ)10 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)6 SmackException (org.jivesoftware.smack.SmackException)5 XmlPullParser (org.xmlpull.v1.XmlPullParser)5 AdHocCommandData (org.jivesoftware.smackx.packet.AdHocCommandData)3 NetworkException (com.xabber.android.data.NetworkException)2 IOException (java.io.IOException)2 UnknownHostException (java.net.UnknownHostException)2 Form (org.jivesoftware.smackx.Form)2 Action (org.jivesoftware.smackx.commands.AdHocCommand.Action)2 MultiUserChat (org.jivesoftware.smackx.muc.MultiUserChat)2 StreamInitiation (org.jivesoftware.smackx.packet.StreamInitiation)2 AccountItem (com.xabber.android.data.account.AccountItem)1 ConnectionThread (com.xabber.android.data.connection.ConnectionThread)1 MessageItem (com.xabber.android.data.database.messagerealm.MessageItem)1 UserJid (com.xabber.android.data.entity.UserJid)1 Realm (io.realm.Realm)1 BufferedImage (java.awt.image.BufferedImage)1