1
0
mirror of https://github.com/moparisthebest/Yaaic synced 2024-11-26 02:42:16 -05:00

Possibility to edit already existing servers

This commit is contained in:
Sebastian Kaspari 2010-03-20 15:44:05 +01:00
parent cfe90d718f
commit 7e36456b7a
5 changed files with 216 additions and 48 deletions

View File

@ -27,6 +27,7 @@
<string name="join_channel">Join Channel</string> <string name="join_channel">Join Channel</string>
<string name="connect">Connect</string> <string name="connect">Connect</string>
<string name="disconnect">Disconnect</string> <string name="disconnect">Disconnect</string>
<string name="edit">Edit</string>
<string name="delete">Delete</string> <string name="delete">Delete</string>
<string name="about_label">About</string> <string name="about_label">About</string>

View File

@ -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 * Get list of servers
* *

View File

@ -199,19 +199,7 @@ public class Database extends SQLiteOpenHelper
); );
while (cursor.moveToNext()) { while (cursor.moveToNext()) {
Server server = new Server(); Server server = populateServer(cursor);
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);
servers.put(server.getId(), server); servers.put(server.getId(), server);
} }
cursor.close(); cursor.close();
@ -219,6 +207,52 @@ public class Database extends SQLiteOpenHelper
return servers; 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 * Get all servers with autoconnect enabled
* *
@ -347,6 +381,8 @@ public class Database extends SQLiteOpenHelper
*/ */
public Identity getIdentityById(int identityId) public Identity getIdentityById(int identityId)
{ {
Identity identity = null;
Cursor cursor = this.getReadableDatabase().query( Cursor cursor = this.getReadableDatabase().query(
IdentityConstants.TABLE_NAME, IdentityConstants.TABLE_NAME,
IdentityConstants.ALL, IdentityConstants.ALL,
@ -358,18 +394,16 @@ public class Database extends SQLiteOpenHelper
); );
if (cursor.moveToNext()) { if (cursor.moveToNext()) {
Identity identity = new Identity(); identity = new Identity();
identity.setNickname(cursor.getString(cursor.getColumnIndex(IdentityConstants.NICKNAME))); identity.setNickname(cursor.getString(cursor.getColumnIndex(IdentityConstants.NICKNAME)));
identity.setIdent(cursor.getString(cursor.getColumnIndex(IdentityConstants.IDENT))); identity.setIdent(cursor.getString(cursor.getColumnIndex(IdentityConstants.IDENT)));
identity.setRealName(cursor.getString(cursor.getColumnIndex(IdentityConstants.REALNAME))); 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 * @param serverId
* @return * @return
*/ */
private int getIdentityIdByServerId(int serverId) public int getIdentityIdByServerId(int serverId)
{ {
int identityId = -1;
Cursor cursor = this.getReadableDatabase().query( Cursor cursor = this.getReadableDatabase().query(
ServerConstants.TABLE_NAME, ServerConstants.TABLE_NAME,
ServerConstants.ALL, ServerConstants.ALL,
@ -391,9 +427,11 @@ public class Database extends SQLiteOpenHelper
); );
if (cursor.moveToNext()) { if (cursor.moveToNext()) {
return cursor.getInt(cursor.getColumnIndex(ServerConstants.IDENTITY)); identityId = cursor.getInt(cursor.getColumnIndex(ServerConstants.IDENTITY));
} }
return -1; cursor.close();
return identityId;
} }
} }

View File

@ -24,11 +24,9 @@ import java.util.regex.Pattern;
import android.app.Activity; import android.app.Activity;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.View; import android.view.View;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
import android.widget.Button; import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText; import android.widget.EditText;
import android.widget.Toast; import android.widget.Toast;
@ -36,6 +34,7 @@ import org.yaaic.R;
import org.yaaic.Yaaic; import org.yaaic.Yaaic;
import org.yaaic.db.Database; import org.yaaic.db.Database;
import org.yaaic.exception.ValidationException; import org.yaaic.exception.ValidationException;
import org.yaaic.model.Broadcast;
import org.yaaic.model.Identity; import org.yaaic.model.Identity;
import org.yaaic.model.Server; import org.yaaic.model.Server;
import org.yaaic.model.Status; import org.yaaic.model.Status;
@ -48,6 +47,7 @@ import org.yaaic.model.Status;
public class AddServerActivity extends Activity implements OnClickListener public class AddServerActivity extends Activity implements OnClickListener
{ {
public static final String TAG = "Yaaic/AddServerActivity"; public static final String TAG = "Yaaic/AddServerActivity";
private Server server;
/** /**
* On create * On create
@ -61,6 +61,26 @@ public class AddServerActivity extends Activity implements OnClickListener
((Button) findViewById(R.id.add)).setOnClickListener(this); ((Button) findViewById(R.id.add)).setOnClickListener(this);
((Button) findViewById(R.id.cancel)).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 { try {
validateServer(); validateServer();
validateIdentity(); validateIdentity();
addServer(); if (server == null) {
addServer();
} else {
updateServer();
}
setResult(RESULT_OK); setResult(RESULT_OK);
finish(); finish();
} catch(ValidationException e) { } catch(ValidationException e) {
@ -92,33 +116,108 @@ public class AddServerActivity extends Activity implements OnClickListener
*/ */
private void addServer() 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 title = ((EditText) findViewById(R.id.title)).getText().toString();
String host = ((EditText) findViewById(R.id.host)).getText().toString(); String host = ((EditText) findViewById(R.id.host)).getText().toString();
int port = Integer.parseInt(((EditText) findViewById(R.id.port)).getText().toString()); int port = Integer.parseInt(((EditText) findViewById(R.id.port)).getText().toString());
String password = ((EditText) findViewById(R.id.password)).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 nickname = ((EditText) findViewById(R.id.nickname)).getText().toString();
String ident = ((EditText) findViewById(R.id.ident)).getText().toString(); String ident = ((EditText) findViewById(R.id.ident)).getText().toString();
String realname = ((EditText) findViewById(R.id.realname)).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 identity = new Identity();
identity.setNickname(nickname); identity.setNickname(nickname);
@ -126,9 +225,7 @@ public class AddServerActivity extends Activity implements OnClickListener
identity.setRealName(realname); identity.setRealName(realname);
server.setIdentity(identity); server.setIdentity(identity);
Yaaic.getInstance().addServer(server); return identity;
Log.d(TAG, "Saved server " + title);
} }
/** /**

View File

@ -36,6 +36,7 @@ import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.ListView; import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemLongClickListener; import android.widget.AdapterView.OnItemLongClickListener;
import org.yaaic.R; import org.yaaic.R;
@ -154,6 +155,7 @@ public class ServersActivity extends ListActivity implements ServiceConnection,
final CharSequence[] items = { final CharSequence[] items = {
getString(R.string.connect), getString(R.string.connect),
getString(R.string.disconnect), getString(R.string.disconnect),
getString(R.string.edit),
getString(R.string.delete) getString(R.string.delete)
}; };
@ -171,7 +173,10 @@ public class ServersActivity extends ListActivity implements ServiceConnection,
server.clearConversations(); server.clearConversations();
binder.getService().getConnection(server.getId()).quitServer(); binder.getService().getConnection(server.getId()).quitServer();
break; break;
case 2: // Delete case 2: // Edit
editServer(server.getId());
break;
case 3: // Delete
binder.getService().getConnection(server.getId()).quitServer(); binder.getService().getConnection(server.getId()).quitServer();
deleteServer(server.getId()); deleteServer(server.getId());
break; break;
@ -183,6 +188,25 @@ public class ServersActivity extends ListActivity implements ServiceConnection,
return true; 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) * Options Menu (Menu Button pressed)
*/ */