From b05154137f883831df1c246166c08795e5db2724 Mon Sep 17 00:00:00 2001 From: Sebastian Kaspari Date: Sat, 17 Apr 2010 23:06:22 +0200 Subject: [PATCH] Moved IRCBinder.connect() to IRCService.connect() --- src/org/yaaic/irc/IRCBinder.java | 60 +---------------------------- src/org/yaaic/irc/IRCService.java | 63 +++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 59 deletions(-) diff --git a/src/org/yaaic/irc/IRCBinder.java b/src/org/yaaic/irc/IRCBinder.java index e1c1ef6..61d02f5 100644 --- a/src/org/yaaic/irc/IRCBinder.java +++ b/src/org/yaaic/irc/IRCBinder.java @@ -20,16 +20,8 @@ along with Yaaic. If not, see . */ package org.yaaic.irc; -import org.jibble.pircbot.IrcException; -import org.jibble.pircbot.NickAlreadyInUseException; -import org.yaaic.R; -import org.yaaic.model.Broadcast; -import org.yaaic.model.Message; import org.yaaic.model.Server; -import org.yaaic.model.ServerInfo; -import org.yaaic.model.Status; -import android.content.Intent; import android.os.Binder; /** @@ -60,57 +52,7 @@ public class IRCBinder extends Binder */ public void connect(final Server server) { - new Thread() { - public void run() { - try { - IRCConnection connection = getService().getConnection(server.getId()); - - connection.setNickname(server.getIdentity().getNickname()); - connection.setIdent(server.getIdentity().getIdent()); - connection.setRealName(server.getIdentity().getRealName()); - connection.setUseSSL(server.useSSL()); - - if (server.getCharset() != null) { - connection.setEncoding(server.getCharset()); - } - - if (server.getPassword() != "") { - connection.connect(server.getHost(), server.getPort(), server.getPassword()); - } else { - connection.connect(server.getHost(), server.getPort()); - } - } - catch (Exception e) { - server.setStatus(Status.DISCONNECTED); - - Intent sIntent = Broadcast.createServerIntent(Broadcast.SERVER_UPDATE, server.getId()); - service.sendBroadcast(sIntent); - - IRCConnection connection = getService().getConnection(server.getId()); - - Message message; - - if (e instanceof NickAlreadyInUseException) { - message = new Message("Nickname " + connection.getNick() + " already in use"); - } else if (e instanceof IrcException) { - message = new Message("Could not log into the IRC server " + server.getHost() + ":" + server.getPort()); - } else { - message = new Message("Could not connect to " + server.getHost() + ":" + server.getPort()); - } - - message.setColor(Message.COLOR_RED); - message.setIcon(R.drawable.error); - server.getConversation(ServerInfo.DEFAULT_NAME).addMessage(message); - - Intent cIntent = Broadcast.createConversationIntent( - Broadcast.CONVERSATION_MESSAGE, - server.getId(), - ServerInfo.DEFAULT_NAME - ); - service.sendBroadcast(cIntent); - } - } - }.start(); + service.connect(server); } /** diff --git a/src/org/yaaic/irc/IRCService.java b/src/org/yaaic/irc/IRCService.java index 0100b25..dce44a5 100644 --- a/src/org/yaaic/irc/IRCService.java +++ b/src/org/yaaic/irc/IRCService.java @@ -25,13 +25,18 @@ import java.lang.reflect.Method; import java.util.ArrayList; import java.util.HashMap; +import org.jibble.pircbot.IrcException; +import org.jibble.pircbot.NickAlreadyInUseException; import org.yaaic.R; import org.yaaic.Yaaic; import org.yaaic.activity.ServersActivity; import org.yaaic.db.Database; import org.yaaic.model.Broadcast; +import org.yaaic.model.Message; import org.yaaic.model.Server; +import org.yaaic.model.ServerInfo; import org.yaaic.model.Settings; +import org.yaaic.model.Status; import android.app.Notification; import android.app.NotificationManager; @@ -239,6 +244,64 @@ public class IRCService extends Service setForeground(false); } } + + /** + * Connect to the given server + */ + public void connect(final Server server) + { + new Thread() { + public void run() { + try { + IRCConnection connection = getConnection(server.getId()); + + connection.setNickname(server.getIdentity().getNickname()); + connection.setIdent(server.getIdentity().getIdent()); + connection.setRealName(server.getIdentity().getRealName()); + connection.setUseSSL(server.useSSL()); + + if (server.getCharset() != null) { + connection.setEncoding(server.getCharset()); + } + + if (server.getPassword() != "") { + connection.connect(server.getHost(), server.getPort(), server.getPassword()); + } else { + connection.connect(server.getHost(), server.getPort()); + } + } + catch (Exception e) { + server.setStatus(Status.DISCONNECTED); + + Intent sIntent = Broadcast.createServerIntent(Broadcast.SERVER_UPDATE, server.getId()); + sendBroadcast(sIntent); + + IRCConnection connection = getConnection(server.getId()); + + Message message; + + if (e instanceof NickAlreadyInUseException) { + message = new Message("Nickname " + connection.getNick() + " already in use"); + } else if (e instanceof IrcException) { + message = new Message("Could not log into the IRC server " + server.getHost() + ":" + server.getPort()); + } else { + message = new Message("Could not connect to " + server.getHost() + ":" + server.getPort()); + } + + message.setColor(Message.COLOR_RED); + message.setIcon(R.drawable.error); + server.getConversation(ServerInfo.DEFAULT_NAME).addMessage(message); + + Intent cIntent = Broadcast.createConversationIntent( + Broadcast.CONVERSATION_MESSAGE, + server.getId(), + ServerInfo.DEFAULT_NAME + ); + sendBroadcast(cIntent); + } + } + }.start(); + } /** * Get connection for given server