From aa42eb544a1ebf99dc52d71c917b899d1fce628d Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Wed, 5 Feb 2014 22:33:39 +0100 Subject: basic muc support. reworked contact list stuff --- .../gultsch/chat/persistance/DatabaseBackend.java | 61 ++++++++++++++++++++-- 1 file changed, 58 insertions(+), 3 deletions(-) (limited to 'src/de/gultsch/chat/persistance') diff --git a/src/de/gultsch/chat/persistance/DatabaseBackend.java b/src/de/gultsch/chat/persistance/DatabaseBackend.java index bb209112..f28ad760 100644 --- a/src/de/gultsch/chat/persistance/DatabaseBackend.java +++ b/src/de/gultsch/chat/persistance/DatabaseBackend.java @@ -2,6 +2,7 @@ package de.gultsch.chat.persistance; import java.util.ArrayList; import java.util.List; +import java.util.UUID; import de.gultsch.chat.entities.Account; import de.gultsch.chat.entities.Contact; @@ -37,9 +38,10 @@ public class DatabaseBackend extends SQLiteOpenHelper { + " TEXT, " + Conversation.PHOTO_URI + " TEXT, " + Conversation.ACCOUNT + " TEXT, " + Conversation.CONTACT + " TEXT, " + Conversation.CREATED + " NUMBER, " - + Conversation.STATUS + " NUMBER," + "FOREIGN KEY(" - + Conversation.ACCOUNT + ") REFERENCES " + Account.TABLENAME - + "(" + Account.UUID + ") ON DELETE CASCADE);"); + + Conversation.STATUS + " NUMBER," + Conversation.MODE + + " NUMBER," + "FOREIGN KEY(" + Conversation.ACCOUNT + + ") REFERENCES " + Account.TABLENAME + "(" + Account.UUID + + ") ON DELETE CASCADE);"); db.execSQL("create table " + Message.TABLENAME + "( " + Message.UUID + " TEXT PRIMARY KEY, " + Message.CONVERSATION + " TEXT, " + Message.TIME_SENT + " NUMBER, " + Message.COUNTERPART @@ -86,6 +88,11 @@ public class DatabaseBackend extends SQLiteOpenHelper { SQLiteDatabase db = this.getWritableDatabase(); db.insert(Account.TABLENAME, null, account.getContentValues()); } + + public void createContact(Contact contact) { + SQLiteDatabase db = this.getWritableDatabase(); + db.insert(Contact.TABLENAME, null, contact.getContentValues()); + } public int getConversationCount() { SQLiteDatabase db = this.getReadableDatabase(); @@ -184,4 +191,52 @@ public class DatabaseBackend extends SQLiteOpenHelper { db.update(Message.TABLENAME, message.getContentValues(), Message.UUID + "=?", args); } + + public void updateContact(Contact contact) { + SQLiteDatabase db = this.getWritableDatabase(); + String[] args = { contact.getUuid() }; + db.update(Contact.TABLENAME, contact.getContentValues(), Contact.UUID + + "=?", args); + } + + public void mergeContacts(List contacts) { + SQLiteDatabase db = this.getWritableDatabase(); + for (int i = 0; i < contacts.size(); i++) { + Contact contact = contacts.get(i); + String[] columns = {Contact.UUID}; + String[] args = {contact.getAccount().getUuid(), contact.getJid()}; + Cursor cursor = db.query(Contact.TABLENAME, columns,Contact.ACCOUNT+"=? AND "+Contact.JID+"=?", args, null, null, null); + if (cursor.getCount()>=1) { + cursor.moveToFirst(); + contact.setUuid(cursor.getString(0)); + updateContact(contact); + } else { + contact.setUuid(UUID.randomUUID().toString()); + createContact(contact); + } + } + } + + public List getContacts() { + List list = new ArrayList(); + SQLiteDatabase db = this.getReadableDatabase(); + Cursor cursor = db.query(Contact.TABLENAME, null, null, null, null, + null, null); + while (cursor.moveToNext()) { + list.add(Contact.fromCursor(cursor)); + } + return list; + } + + public Contact findContact(Account account, String jid) { + SQLiteDatabase db = this.getReadableDatabase(); + String[] selectionArgs = { account.getUuid(), jid }; + Cursor cursor = db.query(Contact.TABLENAME, null, + Contact.ACCOUNT + "=? AND " + Contact.JID + "=?", + selectionArgs, null, null, null); + if (cursor.getCount() == 0) + return null; + cursor.moveToFirst(); + return Contact.fromCursor(cursor); + } } -- cgit v1.2.3