diff --git a/res/values/strings.xml b/res/values/strings.xml index 5653d3a..efc6855 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -27,6 +27,7 @@ Join Channel Connect Disconnect + Edit Delete About diff --git a/src/org/yaaic/Yaaic.java b/src/org/yaaic/Yaaic.java index 5c94804..990d920 100644 --- a/src/org/yaaic/Yaaic.java +++ b/src/org/yaaic/Yaaic.java @@ -98,6 +98,14 @@ public class Yaaic } } + /** + * Update a server in list + */ + public void updateServer(Server server) + { + servers.put(server.getId(), server); + } + /** * Get list of servers * diff --git a/src/org/yaaic/db/Database.java b/src/org/yaaic/db/Database.java index 5fa8c00..e56d75f 100644 --- a/src/org/yaaic/db/Database.java +++ b/src/org/yaaic/db/Database.java @@ -199,19 +199,7 @@ public class Database extends SQLiteOpenHelper ); 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.setPassword(cursor.getString(cursor.getColumnIndex(ServerConstants.PASSWORD))); - server.setId(cursor.getInt(cursor.getColumnIndex((ServerConstants._ID)))); - server.setStatus(Status.DISCONNECTED); - - // Load identity for server - Identity identity = this.getIdentityById(cursor.getInt(cursor.getColumnIndex(ServerConstants.IDENTITY))); - server.setIdentity(identity); - + Server server = populateServer(cursor); servers.put(server.getId(), server); } cursor.close(); @@ -219,6 +207,52 @@ public class Database extends SQLiteOpenHelper return servers; } + 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; + } + + /** + * 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.setStatus(Status.DISCONNECTED); + + // Load identity for server + Identity identity = this.getIdentityById(cursor.getInt(cursor.getColumnIndex(ServerConstants.IDENTITY))); + server.setIdentity(identity); + + return server; + } + /** * Get all servers with autoconnect enabled * @@ -347,6 +381,8 @@ public class Database extends SQLiteOpenHelper */ public Identity getIdentityById(int identityId) { + Identity identity = null; + Cursor cursor = this.getReadableDatabase().query( IdentityConstants.TABLE_NAME, IdentityConstants.ALL, @@ -358,18 +394,16 @@ public class Database extends SQLiteOpenHelper ); if (cursor.moveToNext()) { - Identity identity = new Identity(); + 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; } - return null; + cursor.close(); + + return identity; } /** @@ -378,8 +412,10 @@ public class Database extends SQLiteOpenHelper * @param serverId * @return */ - private int getIdentityIdByServerId(int serverId) + public int getIdentityIdByServerId(int serverId) { + int identityId = -1; + Cursor cursor = this.getReadableDatabase().query( ServerConstants.TABLE_NAME, ServerConstants.ALL, @@ -391,9 +427,11 @@ public class Database extends SQLiteOpenHelper ); if (cursor.moveToNext()) { - return cursor.getInt(cursor.getColumnIndex(ServerConstants.IDENTITY)); + identityId = cursor.getInt(cursor.getColumnIndex(ServerConstants.IDENTITY)); } - return -1; + cursor.close(); + + return identityId; } } diff --git a/src/org/yaaic/view/AddServerActivity.java b/src/org/yaaic/view/AddServerActivity.java index cbb8947..ee7a3f9 100644 --- a/src/org/yaaic/view/AddServerActivity.java +++ b/src/org/yaaic/view/AddServerActivity.java @@ -24,11 +24,9 @@ import java.util.regex.Pattern; import android.app.Activity; import android.os.Bundle; -import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; -import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; @@ -36,6 +34,7 @@ import org.yaaic.R; import org.yaaic.Yaaic; import org.yaaic.db.Database; import org.yaaic.exception.ValidationException; +import org.yaaic.model.Broadcast; import org.yaaic.model.Identity; import org.yaaic.model.Server; import org.yaaic.model.Status; @@ -48,6 +47,7 @@ import org.yaaic.model.Status; public class AddServerActivity extends Activity implements OnClickListener { public static final String TAG = "Yaaic/AddServerActivity"; + private Server server; /** * On create @@ -61,6 +61,26 @@ public class AddServerActivity extends Activity implements OnClickListener ((Button) findViewById(R.id.add)).setOnClickListener(this); ((Button) findViewById(R.id.cancel)).setOnClickListener(this); + + Bundle extras = getIntent().getExtras(); + if (extras.containsKey(Broadcast.EXTRA_SERVER)) { + // Request to edit an existing server + Database db = new Database(this); + this.server = db.getServerById(extras.getInt(Broadcast.EXTRA_SERVER)); + db.close(); + + // Set server values + ((EditText) findViewById(R.id.title)).setText(server.getTitle()); + ((EditText) findViewById(R.id.host)).setText(server.getHost()); + ((EditText) findViewById(R.id.port)).setText(String.valueOf(server.getPort())); + ((EditText) findViewById(R.id.password)).setText(server.getPassword()); + + ((EditText) findViewById(R.id.nickname)).setText(server.getIdentity().getNickname()); + ((EditText) findViewById(R.id.ident)).setText(server.getIdentity().getIdent()); + ((EditText) findViewById(R.id.realname)).setText(server.getIdentity().getRealName()); + + ((Button) findViewById(R.id.add)).setText("Save"); + } } /** @@ -73,7 +93,11 @@ public class AddServerActivity extends Activity implements OnClickListener try { validateServer(); validateIdentity(); - addServer(); + if (server == null) { + addServer(); + } else { + updateServer(); + } setResult(RESULT_OK); finish(); } catch(ValidationException e) { @@ -92,33 +116,108 @@ public class AddServerActivity extends Activity implements OnClickListener */ private void addServer() { - // server + Database db = new Database(this); + + Identity identity = getIdentityFromView(); + long identityId = db.addIdentity( + identity.getNickname(), + identity.getIdent(), + identity.getRealName() + ); + + Server server = getServerFromView(); + long serverId = db.addServer( + server.getTitle(), + server.getHost(), + server.getPort(), + server.getPassword(), + false, // auto connect + false, // use ssl + identityId + ); + + db.close(); + + server.setId((int) serverId); + server.setIdentity(identity); + + Yaaic.getInstance().addServer(server); + } + + /** + * Update server + */ + private void updateServer() + { + Database db = new Database(this); + + int serverId = this.server.getId(); + int identityId = db.getIdentityIdByServerId(serverId); + + Server server = getServerFromView(); + db.updateServer( + serverId, + server.getTitle(), + server.getHost(), + server.getPort(), + server.getPassword(), + false, // auto connect + false, // use ssl + identityId + ); + + Identity identity = getIdentityFromView(); + db.updateIdentity( + identityId, + identity.getNickname(), + identity.getIdent(), + identity.getNickname() + ); + + db.close(); + + server.setId(this.server.getId()); + server.setIdentity(identity); + + Yaaic.getInstance().updateServer(server); + } + + /** + * Populate a server object from the data in the view + * + * @return The server object + */ + private Server getServerFromView() + { String title = ((EditText) findViewById(R.id.title)).getText().toString(); String host = ((EditText) findViewById(R.id.host)).getText().toString(); int port = Integer.parseInt(((EditText) findViewById(R.id.port)).getText().toString()); String password = ((EditText) findViewById(R.id.password)).getText().toString(); - boolean autoConnect = ((CheckBox) findViewById(R.id.autoconnect)).isChecked(); - boolean useSSL = ((CheckBox) findViewById(R.id.useSSL)).isChecked(); - // identity + // not in use yet + //boolean autoConnect = ((CheckBox) findViewById(R.id.autoconnect)).isChecked(); + //boolean useSSL = ((CheckBox) findViewById(R.id.useSSL)).isChecked(); + + Server server = new Server(); + server.setHost(host); + server.setPort(port); + server.setPassword(password); + server.setTitle(title); + server.setStatus(Status.DISCONNECTED); + + return server; + } + + /** + * Populate an identity object from the data in the view + * + * @return The identity object + */ + private Identity getIdentityFromView() + { String nickname = ((EditText) findViewById(R.id.nickname)).getText().toString(); String ident = ((EditText) findViewById(R.id.ident)).getText().toString(); String realname = ((EditText) findViewById(R.id.realname)).getText().toString(); - - Database db = new Database(this); - long identityId = db.addIdentity(nickname, ident, realname); - - Log.d(TAG, "New Identity with Id " + identityId + " (" + nickname + ", " + ident + ", " + realname + ")"); - - long serverId = db.addServer(title, host, port, password, autoConnect, useSSL, identityId); - db.close(); - - Server server = new Server(); - server.setId((int) serverId); - server.setHost(host); - server.setPort(port); - server.setTitle(title); - server.setStatus(Status.DISCONNECTED); Identity identity = new Identity(); identity.setNickname(nickname); @@ -126,9 +225,7 @@ public class AddServerActivity extends Activity implements OnClickListener identity.setRealName(realname); server.setIdentity(identity); - Yaaic.getInstance().addServer(server); - - Log.d(TAG, "Saved server " + title); + return identity; } /** diff --git a/src/org/yaaic/view/ServersActivity.java b/src/org/yaaic/view/ServersActivity.java index 79ff59b..1935170 100644 --- a/src/org/yaaic/view/ServersActivity.java +++ b/src/org/yaaic/view/ServersActivity.java @@ -36,6 +36,7 @@ import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; +import android.widget.Toast; import android.widget.AdapterView.OnItemLongClickListener; import org.yaaic.R; @@ -154,6 +155,7 @@ public class ServersActivity extends ListActivity implements ServiceConnection, final CharSequence[] items = { getString(R.string.connect), getString(R.string.disconnect), + getString(R.string.edit), getString(R.string.delete) }; @@ -171,7 +173,10 @@ public class ServersActivity extends ListActivity implements ServiceConnection, server.clearConversations(); binder.getService().getConnection(server.getId()).quitServer(); break; - case 2: // Delete + case 2: // Edit + editServer(server.getId()); + break; + case 3: // Delete binder.getService().getConnection(server.getId()).quitServer(); deleteServer(server.getId()); break; @@ -183,6 +188,25 @@ public class ServersActivity extends ListActivity implements ServiceConnection, return true; } + /** + * Start activity to edit server with given id + * + * @param serverId The id of the server + */ + private void editServer(int serverId) + { + Server server = Yaaic.getInstance().getServerById(serverId); + + if (server.getStatus() != Status.DISCONNECTED) { + Toast.makeText(this, "Disconnect from server before editing", Toast.LENGTH_SHORT).show(); + } + else { + Intent intent = new Intent(this, AddServerActivity.class); + intent.putExtra(Broadcast.EXTRA_SERVER, serverId); + startActivityForResult(intent, 0); + } + } + /** * Options Menu (Menu Button pressed) */