Yaaic/src/org/yaaic/db/Database.java

590 lines
15 KiB
Java
Raw Normal View History

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/>.
*/
2009-12-17 15:27:57 -05:00
package org.yaaic.db;
import java.util.ArrayList;
2009-12-17 15:27:57 -05:00
import java.util.HashMap;
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
/**
2009-12-17 15:27:57 -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
{
private static final String DATABASE_NAME = "servers.db";
private static final int DATABASE_VERSION = 3;
2009-05-15 11:32:49 -04:00
2009-12-17 15:27:57 -05:00
/**
* Create a new helper for database access
*
* @param context
*/
public Database(Context context)
2009-05-15 11:32:49 -04:00
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
2009-12-17 15:27:57 -05:00
/**
* Create all needed tables on first start
*/
2009-05-15 11:32:49 -04:00
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL("CREATE TABLE " + ServerConstants.TABLE_NAME + " ( "
+ 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, "
2009-05-15 11:32:49 -04:00
+ ServerConstants.IDENTITY + " INTEGER"
+ ");"
);
db.execSQL("CREATE TABLE " + ChannelConstants.TABLE_NAME + " ("
+ ChannelConstants._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ ChannelConstants.NAME + " TEXT NOT NULL, "
+ ChannelConstants.PASSWORD + " TEXT, "
+ ChannelConstants.SERVER + " INTEGER"
+ ");"
);
db.execSQL("CREATE TABLE " + IdentityConstants.TABLE_NAME +" ("
+ IdentityConstants._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ IdentityConstants.NICKNAME + " TEXT NOT NULL,"
+ IdentityConstants.IDENT + " TEXT NOT NULL,"
+ IdentityConstants.REALNAME + " TEXT NOT NULL"
+ ");"
);
db.execSQL("CREATE TABLE " + CommandConstants.TABLE_NAME + " ("
+ CommandConstants._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ CommandConstants.COMMAND + " TEXT NOT NULL, "
+ ChannelConstants.SERVER + " INTEGER"
+ ");"
);
2009-05-15 11:32:49 -04:00
}
2009-12-17 15:27:57 -05:00
/**
* Migrate existing databases to
*/
2009-05-15 11:32:49 -04:00
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// XXX: Do not delete databases (release version)
// 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 + ";");
// onCreate(db);
if (oldVersion == 1) {
// Add charset field to server table
db.execSQL("ALTER TABLE " + ServerConstants.TABLE_NAME + " ADD " + ServerConstants.CHARSET + " TEXT AFTER " + ServerConstants.USE_SSL + ";");
oldVersion = 2; // now do the updates for version 2
}
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"
+ ");"
);
}
2009-05-15 11:32:49 -04:00
}
2009-12-17 15:27:57 -05:00
/**
* Add a new server to the database
*
* @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
2009-12-17 15:27:57 -05:00
*/
public long addServer(String title, String host, int port, String password, boolean autoConnect, boolean useSSL, long identityId, String charset)
2009-05-15 11:32:49 -04:00
{
ContentValues values = new ContentValues();
2009-12-17 15:27:57 -05:00
2009-05-15 11:32:49 -04: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);
2009-05-15 11:32:49 -04:00
2009-12-17 15:27:57 -05:00
return this.getWritableDatabase().insert(ServerConstants.TABLE_NAME, null, values);
2009-05-15 11:32:49 -04: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();
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);
this.getWritableDatabase().update(
ServerConstants.TABLE_NAME,
2010-04-13 18:34:11 -04:00
values,
ServerConstants._ID + " = " + serverId,
null
);
}
2009-12-17 15:27:57 -05:00
/**
* Add a channel to the database
*
* @param serverId Unique id of server
2009-12-17 15:27:57 -05:00
* @param name Name of channel
* @param password Password to join (if needed)
*/
public void addChannel(int serverId, String name, String password)
2009-05-15 11:32:49 -04:00
{
ContentValues values = new ContentValues();
2009-12-17 15:27:57 -05:00
2009-05-15 11:32:49 -04:00
values.put(ChannelConstants.NAME, name);
values.put(ChannelConstants.PASSWORD, password);
values.put(ChannelConstants.SERVER, serverId);
2009-05-15 11:32:49 -04:00
this.getWritableDatabase().insert(ChannelConstants.TABLE_NAME, null, values);
2009-05-15 11:32:49 -04: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
);
// Add new channels
for (String channel : channels) {
addChannel(serverId, channel, "");
}
}
/**
* 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>();
Cursor cursor = this.getReadableDatabase().query(
CommandConstants.TABLE_NAME,
CommandConstants.ALL,
CommandConstants.SERVER + "=" + serverId,
null,
null,
null,
null
);
while (cursor.moveToNext()) {
String command = cursor.getString(cursor.getColumnIndex(CommandConstants.COMMAND));
commands.add(command);
}
cursor.close();
return commands;
}
/**
* 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();
values.put(CommandConstants.COMMAND, command);
values.put(CommandConstants.SERVER, serverId);
this.getWritableDatabase().insert(CommandConstants.TABLE_NAME, null, values);
}
/**
* 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
);
// Add new commands
for (String command : commands) {
addCommand(serverId, command);
}
}
2009-12-17 15:27:57 -05:00
/**
* Get all servers from database
*
* @return
*/
public HashMap<Integer, Server> getServers()
2009-05-15 11:32:49 -04:00
{
2009-12-17 15:27:57 -05:00
HashMap<Integer, Server> servers = new HashMap<Integer, Server>();
Cursor cursor = this.getReadableDatabase().query(
2009-05-15 11:32:49 -04:00
ServerConstants.TABLE_NAME,
ServerConstants.ALL,
null,
null,
null,
null,
ServerConstants.TITLE + " ASC"
);
2009-12-17 15:27:57 -05:00
while (cursor.moveToNext()) {
Server server = populateServer(cursor);
2009-12-17 15:27:57 -05:00
servers.put(server.getId(), server);
}
cursor.close();
2009-12-17 15:27:57 -05:00
return servers;
2009-05-15 11:32:49 -04:00
}
public Server getServerById(int serverId)
{
Server server = null;
Cursor cursor = this.getReadableDatabase().query(
ServerConstants.TABLE_NAME,
ServerConstants.ALL,
ServerConstants._ID + "=" + serverId,
null,
null,
null,
ServerConstants.TITLE + " ASC"
);
if (cursor.moveToNext()) {
server = populateServer(cursor);
}
cursor.close();
return server;
}
/**
* 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;
Cursor cursor = this.getReadableDatabase().query(
ServerConstants.TABLE_NAME,
ServerConstants.ALL,
ServerConstants.TITLE + " = '" + title + "'",
null,
null,
null,
null
);
if (cursor.moveToNext()) {
isTitleUsed = true;
}
cursor.close();
return isTitleUsed;
}
/**
* Populate a server object from the given database cursor
* @param cursor
* @return
*/
private Server populateServer(Cursor cursor)
{
Server server = new Server();
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)));
2010-04-13 18:34:11 -04:00
String useSSLvalue = cursor.getString(cursor.getColumnIndex(ServerConstants.USE_SSL));
if (useSSLvalue != null && useSSLvalue.equals("1")) {
server.setUseSSL(true);
}
server.setStatus(Status.DISCONNECTED);
// Load identity for server
Identity identity = this.getIdentityById(cursor.getInt(cursor.getColumnIndex(ServerConstants.IDENTITY)));
server.setIdentity(identity);
// Load auto join channels
ArrayList<String> channels = this.getChannelsByServerId(server.getId());
server.setAutoJoinChannels(channels);
// Load commands to execute after connect
ArrayList<String> commands = this.getCommandsByServerId(server.getId());
server.setConnectCommands(commands);
return server;
}
2009-12-17 15:27:57 -05:00
/**
* Get all servers with autoconnect enabled
*
* @return
*/
2009-05-15 11:32:49 -04:00
public Cursor getAutoConnectServers()
{
return this.getReadableDatabase().query(
2009-12-17 15:27:57 -05:00
ServerConstants.TABLE_NAME,
ServerConstants.ALL,
ServerConstants.AUTOCONNECT + " = 1",
null,
null,
null,
ServerConstants.TITLE + " ASC"
);
2009-05-15 11:32:49 -04:00
}
2009-12-17 15:27:57 -05:00
/**
* Get all channels of server
*
* @param server Unique id of server
* @return list of channel names
2009-12-17 15:27:57 -05:00
*/
public ArrayList<String> getChannelsByServerId(int serverId)
2009-05-15 11:32:49 -04:00
{
ArrayList<String> channels = new ArrayList<String>();
Cursor cursor = this.getReadableDatabase().query(
2009-05-15 11:32:49 -04:00
ChannelConstants.TABLE_NAME,
ChannelConstants.ALL,
2009-12-17 15:27:57 -05:00
ChannelConstants.SERVER + "=" + serverId,
2009-05-15 11:32:49 -04:00
null,
null,
null,
2009-12-17 15:27:57 -05:00
ChannelConstants.NAME + " ASC"
);
while (cursor.moveToNext()) {
String channel = cursor.getString(cursor.getColumnIndex(ChannelConstants.NAME));
channels.add(channel);
}
cursor.close();
return channels;
2009-05-15 11:32:49 -04:00
}
2009-12-17 15:27:57 -05:00
/**
* Remove server from database by unique id
*
* @param title
*/
public void removeServerById(int serverId)
2009-05-15 11:32:49 -04:00
{
// XXX: Workaround: Remove identity assigned to this server
// until we have some kind of identity manager
int identityId = this.getIdentityIdByServerId(serverId);
if (identityId != -1) {
this.getWritableDatabase().execSQL(
"DELETE FROM " + IdentityConstants.TABLE_NAME + " WHERE " + IdentityConstants._ID + " = " + identityId + ";"
);
}
// Now delete the server entry
2009-12-17 15:27:57 -05:00
this.getWritableDatabase().execSQL(
"DELETE FROM " + ServerConstants.TABLE_NAME + " WHERE " + ServerConstants._ID + " = " + serverId + ";"
);
2009-05-15 11:32:49 -04:00
}
/**
* Add a new identity
*
* @param identityId
* @param nickname
* @param ident
* @param realname
*/
public long addIdentity(String nickname, String ident, String realname)
{
ContentValues values = new ContentValues();
values.put(IdentityConstants.NICKNAME, nickname);
values.put(IdentityConstants.IDENT, ident);
values.put(IdentityConstants.REALNAME, realname);
return this.getWritableDatabase().insert(IdentityConstants.TABLE_NAME, null, values);
}
/**
* 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)
{
ContentValues values = new ContentValues();
values.put(IdentityConstants.NICKNAME, nickname);
values.put(IdentityConstants.IDENT, ident);
values.put(IdentityConstants.REALNAME, realname);
this.getWritableDatabase().update(
IdentityConstants.TABLE_NAME,
values,
IdentityConstants._ID + " = " + identityId,
null
);
}
/**
* Get an identity by its id
*
* @param identityId
* @return
*/
public Identity getIdentityById(int identityId)
{
Identity identity = null;
Cursor cursor = this.getReadableDatabase().query(
IdentityConstants.TABLE_NAME,
IdentityConstants.ALL,
IdentityConstants._ID + "=" + identityId,
null,
null,
null,
null
);
if (cursor.moveToNext()) {
identity = new Identity();
identity.setNickname(cursor.getString(cursor.getColumnIndex(IdentityConstants.NICKNAME)));
identity.setIdent(cursor.getString(cursor.getColumnIndex(IdentityConstants.IDENT)));
identity.setRealName(cursor.getString(cursor.getColumnIndex(IdentityConstants.REALNAME)));
}
cursor.close();
return identity;
}
/**
* Get a server by its id
*
* @param serverId
* @return
*/
public int getIdentityIdByServerId(int serverId)
{
int identityId = -1;
Cursor cursor = this.getReadableDatabase().query(
ServerConstants.TABLE_NAME,
ServerConstants.ALL,
ServerConstants._ID + "=" + serverId,
null,
null,
null,
null
);
if (cursor.moveToNext()) {
identityId = cursor.getInt(cursor.getColumnIndex(ServerConstants.IDENTITY));
}
cursor.close();
return identityId;
}
2009-05-15 11:32:49 -04:00
}