2009-05-28 16:08:46 -04:00
|
|
|
/*
|
|
|
|
Yaaic - Yet Another Android IRC Client
|
|
|
|
|
2010-03-13 10:52:20 -05:00
|
|
|
Copyright 2009-2010 Sebastian Kaspari
|
2009-05-28 16:08:46 -04:00
|
|
|
|
|
|
|
This file is part of Yaaic.
|
|
|
|
|
|
|
|
Yaaic 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 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Yaaic 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 Yaaic. If not, see <http://www.gnu.org/licenses/>.
|
2011-01-25 15:02:27 -05:00
|
|
|
*/
|
2009-12-17 15:27:57 -05:00
|
|
|
package org.yaaic.db;
|
|
|
|
|
2010-04-25 05:00:58 -04:00
|
|
|
import java.util.ArrayList;
|
2009-12-17 15:27:57 -05:00
|
|
|
import java.util.HashMap;
|
2010-09-06 22:07:40 -04:00
|
|
|
import java.util.List;
|
2009-12-17 15:27:57 -05:00
|
|
|
|
2010-03-13 08:41:29 -05:00
|
|
|
import org.yaaic.model.Identity;
|
2009-12-17 15:27:57 -05:00
|
|
|
import org.yaaic.model.Server;
|
|
|
|
import org.yaaic.model.Status;
|
2009-05-15 11:32:49 -04:00
|
|
|
|
|
|
|
import android.content.ContentValues;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
|
import android.database.sqlite.SQLiteOpenHelper;
|
|
|
|
|
2009-05-28 15:10:18 -04:00
|
|
|
/**
|
2011-01-25 15:02:27 -05:00
|
|
|
* Database Helper for the servers and channels tables
|
2009-05-28 15:10:18 -04:00
|
|
|
*
|
2009-12-17 15:27:57 -05:00
|
|
|
* @author Sebastian Kaspari <sebastian@yaaic.org>
|
2009-05-28 15:10:18 -04:00
|
|
|
*/
|
2009-12-17 15:27:57 -05:00
|
|
|
public class Database extends SQLiteOpenHelper
|
2009-05-15 11:32:49 -04:00
|
|
|
{
|
2010-11-18 12:52:19 -05:00
|
|
|
private static final String DATABASE_NAME = "servers.db";
|
|
|
|
private static final int DATABASE_VERSION = 4;
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
|
|
|
* Create a new helper for database access
|
|
|
|
*
|
|
|
|
* @param context
|
|
|
|
*/
|
|
|
|
public Database(Context context)
|
|
|
|
{
|
|
|
|
super(context, DATABASE_NAME, null, DATABASE_VERSION);
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
|
|
|
* Create all needed tables on first start
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void onCreate(SQLiteDatabase db)
|
|
|
|
{
|
|
|
|
db.execSQL("CREATE TABLE " + ServerConstants.TABLE_NAME + " ( "
|
2011-01-25 15:02:27 -05:00
|
|
|
+ ServerConstants._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
|
|
|
|
+ ServerConstants.TITLE + " TEXT NOT NULL, "
|
|
|
|
+ ServerConstants.HOST + " TEXT NOT NULL, "
|
|
|
|
+ ServerConstants.PORT + " INTEGER, "
|
|
|
|
+ ServerConstants.PASSWORD + " TEXT, "
|
|
|
|
+ ServerConstants.AUTOCONNECT + " BOOLEAN, " // XXX: Does SQLLite support boolean?
|
|
|
|
+ ServerConstants.USE_SSL + " BOOLEAN, "
|
|
|
|
+ ServerConstants.CHARSET + " TEXT, "
|
|
|
|
+ ServerConstants.IDENTITY + " INTEGER"
|
|
|
|
+ ");"
|
2010-11-18 12:52:19 -05:00
|
|
|
);
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
db.execSQL("CREATE TABLE " + ChannelConstants.TABLE_NAME + " ("
|
2011-01-25 15:02:27 -05:00
|
|
|
+ ChannelConstants._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
|
|
|
|
+ ChannelConstants.NAME + " TEXT NOT NULL, "
|
|
|
|
+ ChannelConstants.PASSWORD + " TEXT, "
|
|
|
|
+ ChannelConstants.SERVER + " INTEGER"
|
|
|
|
+ ");"
|
2010-11-18 12:52:19 -05:00
|
|
|
);
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
db.execSQL("CREATE TABLE " + IdentityConstants.TABLE_NAME +" ("
|
2011-01-25 15:02:27 -05:00
|
|
|
+ IdentityConstants._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
|
|
|
|
+ IdentityConstants.NICKNAME + " TEXT NOT NULL,"
|
|
|
|
+ IdentityConstants.IDENT + " TEXT NOT NULL,"
|
|
|
|
+ IdentityConstants.REALNAME + " TEXT NOT NULL"
|
|
|
|
+ ");"
|
2010-11-18 12:52:19 -05:00
|
|
|
);
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
db.execSQL("CREATE TABLE " + CommandConstants.TABLE_NAME + " ("
|
|
|
|
+ CommandConstants._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
|
|
|
|
+ CommandConstants.COMMAND + " TEXT NOT NULL, "
|
|
|
|
+ ChannelConstants.SERVER + " INTEGER"
|
|
|
|
+ ");"
|
|
|
|
);
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
db.execSQL("CREATE TABLE " + AliasConstants.TABLE_NAME + " ("
|
|
|
|
+ AliasConstants._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
|
|
|
|
+ AliasConstants.ALIAS + " TEXT NOT NULL, "
|
|
|
|
+ AliasConstants.IDENTITY + " INTEGER"
|
|
|
|
+ ");"
|
|
|
|
);
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
|
|
|
* Migrate existing databases to
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
|
|
|
|
{
|
|
|
|
// XXX: Do not delete databases (release version)
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
// db.execSQL("DROP TABLE IF EXISTS " + ServerConstants.TABLE_NAME + ";");
|
|
|
|
// db.execSQL("DROP TABLE IF EXISTS " + ChannelConstants.TABLE_NAME + ";");
|
|
|
|
// db.execSQL("DROP TABLE IF EXISTS " + IdentityConstants.TABLE_NAME + ";");
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
// onCreate(db);
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
if (oldVersion == 1) {
|
|
|
|
// Add charset field to server table
|
|
|
|
db.execSQL("ALTER TABLE " + ServerConstants.TABLE_NAME + " ADD " + ServerConstants.CHARSET + " TEXT AFTER " + ServerConstants.USE_SSL + ";");
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
oldVersion = 2; // now do the updates for version 2
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
if (oldVersion == 2) {
|
|
|
|
// Add new commands table (copy&paste from onCreate())
|
|
|
|
db.execSQL("CREATE TABLE " + CommandConstants.TABLE_NAME + " ("
|
|
|
|
+ CommandConstants._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
|
|
|
|
+ CommandConstants.COMMAND + " TEXT NOT NULL, "
|
|
|
|
+ ChannelConstants.SERVER + " INTEGER"
|
|
|
|
+ ");"
|
|
|
|
);
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
oldVersion = 3;
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
if (oldVersion == 3) {
|
|
|
|
db.execSQL("CREATE TABLE " + AliasConstants.TABLE_NAME + " ("
|
|
|
|
+ AliasConstants._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
|
|
|
|
+ AliasConstants.ALIAS + " TEXT NOT NULL, "
|
|
|
|
+ AliasConstants.IDENTITY + " INTEGER"
|
|
|
|
+ ");"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
2011-01-25 15:02:27 -05:00
|
|
|
* Add a new server to the database
|
2010-11-18 12:52:19 -05:00
|
|
|
*
|
|
|
|
* @param title Unique title of the server
|
|
|
|
* @param host Hostname of the server
|
|
|
|
* @param port Port (default: 3337)
|
|
|
|
* @param password Password if needed
|
|
|
|
* @param autoConnect Autoconnect to this server on startup?
|
|
|
|
* @param useSSL Does the server use SSL?
|
|
|
|
* @param identityId The id of the identity record
|
|
|
|
*/
|
|
|
|
public long addServer(String title, String host, int port, String password, boolean autoConnect, boolean useSSL, long identityId, String charset)
|
|
|
|
{
|
|
|
|
ContentValues values = new ContentValues();
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
values.put(ServerConstants.TITLE, title);
|
|
|
|
values.put(ServerConstants.HOST, host);
|
|
|
|
values.put(ServerConstants.PORT, port);
|
|
|
|
values.put(ServerConstants.PASSWORD, password);
|
|
|
|
values.put(ServerConstants.AUTOCONNECT, autoConnect);
|
|
|
|
values.put(ServerConstants.USE_SSL, useSSL);
|
|
|
|
values.put(ServerConstants.IDENTITY, identityId);
|
|
|
|
values.put(ServerConstants.CHARSET, charset);
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
return this.getWritableDatabase().insert(ServerConstants.TABLE_NAME, null, values);
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
|
|
|
* Update the server record in the database
|
|
|
|
*
|
|
|
|
* @param serverId
|
|
|
|
* @param title Unique title of the server
|
|
|
|
* @param host Hostname of the server
|
|
|
|
* @param port Port (default: 3337)
|
|
|
|
* @param password Password if needed
|
|
|
|
* @param autoConnect Autoconnect to this server on startup?
|
|
|
|
* @param useSSL Does the server use SSL?
|
|
|
|
* @param identityId The identity of the server record
|
|
|
|
*/
|
|
|
|
public void updateServer(int serverId, String title, String host, int port, String password, boolean autoConnect, boolean useSSL, long identityId, String charset)
|
|
|
|
{
|
|
|
|
ContentValues values = new ContentValues();
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
values.put(ServerConstants.TITLE, title);
|
|
|
|
values.put(ServerConstants.HOST, host);
|
|
|
|
values.put(ServerConstants.PORT, port);
|
|
|
|
values.put(ServerConstants.PASSWORD, password);
|
|
|
|
values.put(ServerConstants.AUTOCONNECT, autoConnect);
|
|
|
|
values.put(ServerConstants.USE_SSL, useSSL);
|
|
|
|
values.put(ServerConstants.IDENTITY, identityId);
|
|
|
|
values.put(ServerConstants.CHARSET, charset);
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
this.getWritableDatabase().update(
|
|
|
|
ServerConstants.TABLE_NAME,
|
|
|
|
values,
|
|
|
|
ServerConstants._ID + " = " + serverId,
|
|
|
|
null
|
|
|
|
);
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
|
|
|
* Add a channel to the database
|
|
|
|
*
|
|
|
|
* @param serverId Unique id of server
|
|
|
|
* @param name Name of channel
|
|
|
|
* @param password Password to join (if needed)
|
|
|
|
*/
|
|
|
|
public void addChannel(int serverId, String name, String password)
|
|
|
|
{
|
|
|
|
ContentValues values = new ContentValues();
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
values.put(ChannelConstants.NAME, name);
|
|
|
|
values.put(ChannelConstants.PASSWORD, password);
|
|
|
|
values.put(ChannelConstants.SERVER, serverId);
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
this.getWritableDatabase().insert(ChannelConstants.TABLE_NAME, null, values);
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
|
|
|
* Replace list of channels for the given server
|
|
|
|
*
|
|
|
|
* @param serverId Unique id of server
|
|
|
|
* @param channels List of channel names
|
|
|
|
*/
|
|
|
|
public void setChannels(int serverId, ArrayList<String> channels)
|
|
|
|
{
|
|
|
|
// Remove old channels
|
|
|
|
this.getWritableDatabase().delete(
|
|
|
|
ChannelConstants.TABLE_NAME,
|
|
|
|
ChannelConstants.SERVER + " = " + serverId,
|
|
|
|
null
|
|
|
|
);
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
// Add new channels
|
|
|
|
for (String channel : channels) {
|
|
|
|
addChannel(serverId, channel, "");
|
|
|
|
}
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
|
|
|
* Get all commands to execute on connect
|
|
|
|
*
|
|
|
|
* @param serverId Unique id of server
|
|
|
|
* @return List of commands
|
|
|
|
*/
|
|
|
|
public ArrayList<String> getCommandsByServerId(int serverId)
|
|
|
|
{
|
|
|
|
ArrayList<String> commands = new ArrayList<String>();
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
Cursor cursor = this.getReadableDatabase().query(
|
|
|
|
CommandConstants.TABLE_NAME,
|
|
|
|
CommandConstants.ALL,
|
|
|
|
CommandConstants.SERVER + "=" + serverId,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null
|
|
|
|
);
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
while (cursor.moveToNext()) {
|
|
|
|
String command = cursor.getString(cursor.getColumnIndex(CommandConstants.COMMAND));
|
|
|
|
commands.add(command);
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
cursor.close();
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
return commands;
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
|
|
|
* Add a command to a server
|
|
|
|
*
|
|
|
|
* @param serverId Unique id of server
|
|
|
|
* @param command The command to execute after connect
|
|
|
|
*/
|
|
|
|
public void addCommand(int serverId, String command)
|
|
|
|
{
|
|
|
|
ContentValues values = new ContentValues();
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
values.put(CommandConstants.COMMAND, command);
|
|
|
|
values.put(CommandConstants.SERVER, serverId);
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
this.getWritableDatabase().insert(CommandConstants.TABLE_NAME, null, values);
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
|
|
|
* Replace list of commands for the given server
|
|
|
|
*
|
|
|
|
* @param serverId Unique id of server
|
|
|
|
* @param commands List of commands to execute after connect
|
|
|
|
*/
|
|
|
|
public void setCommands(int serverId, ArrayList<String> commands)
|
|
|
|
{
|
|
|
|
// Remove old commands
|
|
|
|
this.getWritableDatabase().delete(
|
|
|
|
CommandConstants.TABLE_NAME,
|
|
|
|
CommandConstants.SERVER + " = " + serverId,
|
|
|
|
null
|
|
|
|
);
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
// Add new commands
|
|
|
|
for (String command : commands) {
|
|
|
|
addCommand(serverId, command);
|
|
|
|
}
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
|
|
|
* Get all servers from database
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public HashMap<Integer, Server> getServers()
|
|
|
|
{
|
|
|
|
HashMap<Integer, Server> servers = new HashMap<Integer, Server>();
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
Cursor cursor = this.getReadableDatabase().query(
|
|
|
|
ServerConstants.TABLE_NAME,
|
|
|
|
ServerConstants.ALL,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
ServerConstants.TITLE + " ASC"
|
|
|
|
);
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
while (cursor.moveToNext()) {
|
|
|
|
Server server = populateServer(cursor);
|
|
|
|
servers.put(server.getId(), server);
|
|
|
|
}
|
|
|
|
cursor.close();
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
return servers;
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
public Server getServerById(int serverId)
|
|
|
|
{
|
|
|
|
Server server = null;
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
Cursor cursor = this.getReadableDatabase().query(
|
|
|
|
ServerConstants.TABLE_NAME,
|
|
|
|
ServerConstants.ALL,
|
|
|
|
ServerConstants._ID + "=" + serverId,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
ServerConstants.TITLE + " ASC"
|
|
|
|
);
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
if (cursor.moveToNext()) {
|
|
|
|
server = populateServer(cursor);
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
cursor.close();
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
return server;
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
|
|
|
* Check if the given server title is currently used
|
|
|
|
*
|
|
|
|
* @param title The server title
|
|
|
|
* @return true if there's a server with this title, false otherwise
|
|
|
|
*/
|
|
|
|
public boolean isTitleUsed(String title)
|
|
|
|
{
|
|
|
|
boolean isTitleUsed = false;
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
Cursor cursor = this.getReadableDatabase().query(
|
|
|
|
ServerConstants.TABLE_NAME,
|
|
|
|
ServerConstants.ALL,
|
|
|
|
ServerConstants.TITLE + " = '" + title + "'",
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null
|
|
|
|
);
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
if (cursor.moveToNext()) {
|
|
|
|
isTitleUsed = true;
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
cursor.close();
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
return isTitleUsed;
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
|
|
|
* Populate a server object from the given database cursor
|
|
|
|
* @param cursor
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
private Server populateServer(Cursor cursor)
|
|
|
|
{
|
|
|
|
Server server = new Server();
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
server.setTitle(cursor.getString(cursor.getColumnIndex((ServerConstants.TITLE))));
|
|
|
|
server.setHost(cursor.getString(cursor.getColumnIndex((ServerConstants.HOST))));
|
|
|
|
server.setPort(cursor.getInt(cursor.getColumnIndex((ServerConstants.PORT))));
|
|
|
|
server.setPassword(cursor.getString(cursor.getColumnIndex(ServerConstants.PASSWORD)));
|
|
|
|
server.setId(cursor.getInt(cursor.getColumnIndex((ServerConstants._ID))));
|
|
|
|
server.setCharset(cursor.getString(cursor.getColumnIndex(ServerConstants.CHARSET)));
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
String useSSLvalue = cursor.getString(cursor.getColumnIndex(ServerConstants.USE_SSL));
|
|
|
|
if (useSSLvalue != null && useSSLvalue.equals("1")) {
|
|
|
|
server.setUseSSL(true);
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
server.setStatus(Status.DISCONNECTED);
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
// Load identity for server
|
|
|
|
Identity identity = this.getIdentityById(cursor.getInt(cursor.getColumnIndex(ServerConstants.IDENTITY)));
|
|
|
|
server.setIdentity(identity);
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
// Load auto join channels
|
|
|
|
ArrayList<String> channels = this.getChannelsByServerId(server.getId());
|
|
|
|
server.setAutoJoinChannels(channels);
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
// Load commands to execute after connect
|
|
|
|
ArrayList<String> commands = this.getCommandsByServerId(server.getId());
|
|
|
|
server.setConnectCommands(commands);
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
return server;
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
|
|
|
* Get all servers with autoconnect enabled
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public Cursor getAutoConnectServers()
|
|
|
|
{
|
|
|
|
return this.getReadableDatabase().query(
|
|
|
|
ServerConstants.TABLE_NAME,
|
|
|
|
ServerConstants.ALL,
|
|
|
|
ServerConstants.AUTOCONNECT + " = 1",
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
ServerConstants.TITLE + " ASC"
|
|
|
|
);
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
|
|
|
* Get all channels of server
|
|
|
|
*
|
|
|
|
* @param server Unique id of server
|
|
|
|
* @return list of channel names
|
|
|
|
*/
|
|
|
|
public ArrayList<String> getChannelsByServerId(int serverId)
|
|
|
|
{
|
|
|
|
ArrayList<String> channels = new ArrayList<String>();
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
Cursor cursor = this.getReadableDatabase().query(
|
|
|
|
ChannelConstants.TABLE_NAME,
|
|
|
|
ChannelConstants.ALL,
|
|
|
|
ChannelConstants.SERVER + "=" + serverId,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
ChannelConstants.NAME + " ASC"
|
|
|
|
);
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
while (cursor.moveToNext()) {
|
|
|
|
String channel = cursor.getString(cursor.getColumnIndex(ChannelConstants.NAME));
|
|
|
|
channels.add(channel);
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
cursor.close();
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
return channels;
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
|
|
|
* Remove server from database by unique id
|
|
|
|
*
|
|
|
|
* @param title
|
|
|
|
*/
|
|
|
|
public void removeServerById(int serverId)
|
|
|
|
{
|
|
|
|
// XXX: Workaround: Remove identity assigned to this server
|
|
|
|
// until we have some kind of identity manager
|
|
|
|
int identityId = this.getIdentityIdByServerId(serverId);
|
|
|
|
if (identityId != -1) {
|
|
|
|
deleteAliases(identityId);
|
|
|
|
this.getWritableDatabase().execSQL(
|
|
|
|
"DELETE FROM " + IdentityConstants.TABLE_NAME + " WHERE " + IdentityConstants._ID + " = " + identityId + ";"
|
2011-01-25 15:02:27 -05:00
|
|
|
);
|
2010-11-18 12:52:19 -05:00
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
// Now delete the server entry
|
|
|
|
this.getWritableDatabase().execSQL(
|
|
|
|
"DELETE FROM " + ServerConstants.TABLE_NAME + " WHERE " + ServerConstants._ID + " = " + serverId + ";"
|
|
|
|
);
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
protected void setAliases(long identityId, List<String> aliases)
|
|
|
|
{
|
|
|
|
deleteAliases(identityId);
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
for (String alias : aliases) {
|
|
|
|
values.clear();
|
|
|
|
values.put(AliasConstants.ALIAS, alias);
|
|
|
|
values.put(AliasConstants.IDENTITY, identityId);
|
|
|
|
getWritableDatabase().insert(AliasConstants.TABLE_NAME, null, values);
|
|
|
|
}
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
protected void deleteAliases(long identityId)
|
|
|
|
{
|
|
|
|
getWritableDatabase().execSQL(
|
|
|
|
"DELETE FROM " + AliasConstants.TABLE_NAME + " WHERE " + AliasConstants.IDENTITY + " = " + identityId
|
|
|
|
);
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
protected List<String> getAliasesByIdentityId(long identityId)
|
|
|
|
{
|
|
|
|
List<String> aliases = new ArrayList<String>();
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
Cursor cursor = this.getReadableDatabase().query(
|
|
|
|
AliasConstants.TABLE_NAME,
|
|
|
|
AliasConstants.ALL,
|
|
|
|
AliasConstants.IDENTITY + " = " + identityId,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null
|
|
|
|
);
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
while (cursor.moveToNext()) {
|
|
|
|
aliases.add(cursor.getString(cursor.getColumnIndex(AliasConstants.ALIAS)));
|
|
|
|
}
|
|
|
|
cursor.close();
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
return aliases;
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
|
|
|
* Add a new identity
|
|
|
|
*
|
|
|
|
* @param identityId
|
|
|
|
* @param nickname
|
|
|
|
* @param ident
|
|
|
|
* @param realname
|
|
|
|
* @param aliases
|
|
|
|
*/
|
|
|
|
public long addIdentity(String nickname, String ident, String realname, List<String> aliases)
|
|
|
|
{
|
|
|
|
ContentValues values = new ContentValues();
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
values.put(IdentityConstants.NICKNAME, nickname);
|
|
|
|
values.put(IdentityConstants.IDENT, ident);
|
|
|
|
values.put(IdentityConstants.REALNAME, realname);
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
long identityId = this.getWritableDatabase().insert(IdentityConstants.TABLE_NAME, null, values);
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
setAliases(identityId, aliases);
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
return identityId;
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
|
|
|
* Update the identity with the given id
|
|
|
|
*
|
|
|
|
* @param identityId
|
|
|
|
* @param nickname
|
|
|
|
* @param ident
|
|
|
|
* @param realname
|
|
|
|
*/
|
|
|
|
public void updateIdentity(int identityId, String nickname, String ident, String realname, List<String> aliases)
|
|
|
|
{
|
|
|
|
ContentValues values = new ContentValues();
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
values.put(IdentityConstants.NICKNAME, nickname);
|
|
|
|
values.put(IdentityConstants.IDENT, ident);
|
|
|
|
values.put(IdentityConstants.REALNAME, realname);
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
this.getWritableDatabase().update(
|
|
|
|
IdentityConstants.TABLE_NAME,
|
|
|
|
values,
|
|
|
|
IdentityConstants._ID + " = " + identityId,
|
|
|
|
null
|
|
|
|
);
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
setAliases(identityId, aliases);
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
|
|
|
* Get an identity by its id
|
|
|
|
*
|
|
|
|
* @param identityId
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public Identity getIdentityById(int identityId)
|
|
|
|
{
|
|
|
|
Identity identity = null;
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
Cursor cursor = this.getReadableDatabase().query(
|
|
|
|
IdentityConstants.TABLE_NAME,
|
|
|
|
IdentityConstants.ALL,
|
|
|
|
IdentityConstants._ID + "=" + identityId,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null
|
|
|
|
);
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
if (cursor.moveToNext()) {
|
|
|
|
identity = new Identity();
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
identity.setNickname(cursor.getString(cursor.getColumnIndex(IdentityConstants.NICKNAME)));
|
|
|
|
identity.setIdent(cursor.getString(cursor.getColumnIndex(IdentityConstants.IDENT)));
|
|
|
|
identity.setRealName(cursor.getString(cursor.getColumnIndex(IdentityConstants.REALNAME)));
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
identity.setAliases(getAliasesByIdentityId(identityId));
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
cursor.close();
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
return identity;
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
|
|
|
* Get a server by its id
|
|
|
|
*
|
|
|
|
* @param serverId
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public int getIdentityIdByServerId(int serverId)
|
|
|
|
{
|
|
|
|
int identityId = -1;
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
Cursor cursor = this.getReadableDatabase().query(
|
|
|
|
ServerConstants.TABLE_NAME,
|
|
|
|
ServerConstants.ALL,
|
|
|
|
ServerConstants._ID + "=" + serverId,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null
|
|
|
|
);
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
if (cursor.moveToNext()) {
|
|
|
|
identityId = cursor.getInt(cursor.getColumnIndex(ServerConstants.IDENTITY));
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
cursor.close();
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
return identityId;
|
|
|
|
}
|
2009-05-15 11:32:49 -04:00
|
|
|
}
|