1
0
mirror of https://github.com/moparisthebest/Yaaic synced 2024-08-13 16:53:50 -04:00

Moved IRCBinder.connect() to IRCService.connect()

This commit is contained in:
Sebastian Kaspari 2010-04-17 23:06:22 +02:00
parent e15c5d6c19
commit b05154137f
2 changed files with 64 additions and 59 deletions

View File

@ -20,16 +20,8 @@ along with Yaaic. If not, see <http://www.gnu.org/licenses/>.
*/ */
package org.yaaic.irc; 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.Server;
import org.yaaic.model.ServerInfo;
import org.yaaic.model.Status;
import android.content.Intent;
import android.os.Binder; import android.os.Binder;
/** /**
@ -60,57 +52,7 @@ public class IRCBinder extends Binder
*/ */
public void connect(final Server server) public void connect(final Server server)
{ {
new Thread() { service.connect(server);
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();
} }
/** /**

View File

@ -25,13 +25,18 @@ import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import org.jibble.pircbot.IrcException;
import org.jibble.pircbot.NickAlreadyInUseException;
import org.yaaic.R; import org.yaaic.R;
import org.yaaic.Yaaic; import org.yaaic.Yaaic;
import org.yaaic.activity.ServersActivity; import org.yaaic.activity.ServersActivity;
import org.yaaic.db.Database; import org.yaaic.db.Database;
import org.yaaic.model.Broadcast; import org.yaaic.model.Broadcast;
import org.yaaic.model.Message;
import org.yaaic.model.Server; import org.yaaic.model.Server;
import org.yaaic.model.ServerInfo;
import org.yaaic.model.Settings; import org.yaaic.model.Settings;
import org.yaaic.model.Status;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationManager; import android.app.NotificationManager;
@ -240,6 +245,64 @@ public class IRCService extends Service
} }
} }
/**
* 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 * Get connection for given server
* *