2009-05-28 16:08:46 -04:00
|
|
|
/*
|
|
|
|
Yaaic - Yet Another Android IRC Client
|
|
|
|
|
|
|
|
Copyright 2009 Sebastian Kaspari
|
|
|
|
|
|
|
|
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.HashMap;
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2009-12-17 15:27:57 -05:00
|
|
|
public static final String TAG = "Yaaic/Database";
|
|
|
|
|
2009-05-15 11:32:49 -04:00
|
|
|
private static final String DATABASE_NAME = "servers.db";
|
|
|
|
private static final int DATABASE_VERSION = 1;
|
|
|
|
|
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.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"
|
|
|
|
+ ");"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2009-12-17 15:27:57 -05:00
|
|
|
// XXX: We delete the database currently, in future versions we want to
|
2009-05-15 11:32:49 -04:00
|
|
|
// migrate the database to the new version (add or remove rows..)
|
|
|
|
db.execSQL("DROP TABLE IF EXISTS " + ServerConstants.TABLE_NAME + ";");
|
|
|
|
db.execSQL("DROP TABLE IF EXISTS " + ChannelConstants.TABLE_NAME + ";");
|
|
|
|
|
|
|
|
onCreate(db);
|
|
|
|
}
|
|
|
|
|
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?
|
|
|
|
*/
|
|
|
|
public long addServer(String title, String host, int port, String password, boolean autoConnect, boolean useSSL)
|
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);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-12-17 15:27:57 -05:00
|
|
|
/**
|
|
|
|
* Add a channel to the database
|
|
|
|
*
|
|
|
|
* @param server Unique id of server
|
|
|
|
* @param name Name of channel
|
|
|
|
* @param password Password to join (if needed)
|
|
|
|
*/
|
2009-05-15 11:32:49 -04:00
|
|
|
public void addChannel(int server, String name, String password)
|
|
|
|
{
|
|
|
|
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, server);
|
|
|
|
|
|
|
|
this.getWritableDatabase().insert(ServerConstants.TABLE_NAME, null, values);
|
|
|
|
}
|
|
|
|
|
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 = 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.setId(cursor.getInt(cursor.getColumnIndex((ServerConstants._ID))));
|
|
|
|
server.setStatus(Status.DISCONNECTED);
|
|
|
|
|
|
|
|
servers.put(server.getId(), server);
|
|
|
|
}
|
2010-03-08 15:36:36 -05:00
|
|
|
cursor.close();
|
2009-12-17 15:27:57 -05:00
|
|
|
|
|
|
|
return servers;
|
2009-05-15 11:32:49 -04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
2009-05-15 11:32:49 -04:00
|
|
|
public Cursor getChannels()
|
|
|
|
{
|
|
|
|
return this.getReadableDatabase().query(
|
|
|
|
ChannelConstants.TABLE_NAME,
|
|
|
|
ChannelConstants.ALL,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
2009-12-17 15:27:57 -05:00
|
|
|
ChannelConstants.NAME + " 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
|
|
|
|
*/
|
|
|
|
public Cursor getChannelsById(int serverId)
|
2009-05-15 11:32:49 -04:00
|
|
|
{
|
|
|
|
return this.getReadableDatabase().query(
|
|
|
|
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"
|
|
|
|
);
|
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
|
|
|
{
|
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
|
|
|
}
|
|
|
|
}
|