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/>.
|
|
|
|
*/
|
2010-03-24 13:48:38 -04:00
|
|
|
package org.yaaic.activity;
|
2009-05-15 11:32:49 -04:00
|
|
|
|
2010-04-05 16:36:04 -04:00
|
|
|
import java.io.UnsupportedEncodingException;
|
2010-04-25 04:53:29 -04:00
|
|
|
import java.util.ArrayList;
|
2010-03-14 08:34:25 -04:00
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
2009-05-15 11:32:49 -04:00
|
|
|
import android.app.Activity;
|
2010-04-25 04:19:59 -04:00
|
|
|
import android.content.Intent;
|
2010-03-20 11:32:42 -04:00
|
|
|
import android.net.Uri;
|
2009-05-15 11:32:49 -04:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.View.OnClickListener;
|
2010-04-05 16:36:04 -04:00
|
|
|
import android.widget.ArrayAdapter;
|
2009-05-15 11:32:49 -04:00
|
|
|
import android.widget.Button;
|
2010-04-13 13:25:09 -04:00
|
|
|
import android.widget.CheckBox;
|
2009-05-15 11:32:49 -04:00
|
|
|
import android.widget.EditText;
|
2010-04-05 16:36:04 -04:00
|
|
|
import android.widget.Spinner;
|
2010-03-14 08:34:25 -04:00
|
|
|
import android.widget.Toast;
|
2009-05-15 11:32:49 -04:00
|
|
|
|
2009-12-17 15:27:57 -05:00
|
|
|
import org.yaaic.R;
|
|
|
|
import org.yaaic.Yaaic;
|
|
|
|
import org.yaaic.db.Database;
|
2010-03-14 08:34:25 -04:00
|
|
|
import org.yaaic.exception.ValidationException;
|
2010-03-24 14:35:46 -04:00
|
|
|
import org.yaaic.model.Extra;
|
2010-03-13 14:53:37 -05:00
|
|
|
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-28 15:10:18 -04:00
|
|
|
/**
|
2009-12-17 15:27:57 -05:00
|
|
|
* Add a new server to the list
|
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 AddServerActivity extends Activity implements OnClickListener
|
2009-05-15 11:32:49 -04:00
|
|
|
{
|
2010-03-20 10:44:05 -04:00
|
|
|
private Server server;
|
2010-04-25 04:53:29 -04:00
|
|
|
private ArrayList<String> channels;
|
2009-12-17 15:27:57 -05:00
|
|
|
|
2010-03-13 13:56:32 -05:00
|
|
|
/**
|
|
|
|
* On create
|
|
|
|
*/
|
2009-12-17 15:27:57 -05:00
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState)
|
|
|
|
{
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
|
|
setContentView(R.layout.serveradd);
|
2010-04-25 04:53:29 -04:00
|
|
|
channels = new ArrayList<String>();
|
2009-12-17 15:27:57 -05:00
|
|
|
|
|
|
|
((Button) findViewById(R.id.add)).setOnClickListener(this);
|
|
|
|
((Button) findViewById(R.id.cancel)).setOnClickListener(this);
|
2010-04-05 16:36:04 -04:00
|
|
|
|
|
|
|
Spinner spinner = (Spinner) findViewById(R.id.charset);
|
|
|
|
String[] charsets = getResources().getStringArray(R.array.charsets);
|
|
|
|
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, charsets);
|
|
|
|
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
|
|
|
spinner.setAdapter(adapter);
|
2010-03-20 10:44:05 -04:00
|
|
|
|
2010-04-25 04:19:59 -04:00
|
|
|
((Button) findViewById(R.id.channels)).setOnClickListener(this);
|
|
|
|
|
2010-03-20 10:44:05 -04:00
|
|
|
Bundle extras = getIntent().getExtras();
|
2010-03-24 14:35:46 -04:00
|
|
|
if (extras != null && extras.containsKey(Extra.SERVER)) {
|
2010-03-20 10:44:05 -04:00
|
|
|
// Request to edit an existing server
|
|
|
|
Database db = new Database(this);
|
2010-03-24 14:35:46 -04:00
|
|
|
this.server = db.getServerById(extras.getInt(Extra.SERVER));
|
2010-04-25 05:05:12 -04:00
|
|
|
this.channels = db.getChannelsByServerId(server.getId());
|
2010-03-20 10:44:05 -04:00
|
|
|
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());
|
2010-04-13 13:25:09 -04:00
|
|
|
((CheckBox) findViewById(R.id.useSSL)).setChecked(server.useSSL());
|
2010-03-20 10:44:05 -04:00
|
|
|
|
|
|
|
((Button) findViewById(R.id.add)).setText("Save");
|
2010-04-05 16:36:04 -04:00
|
|
|
|
|
|
|
// Select charset
|
|
|
|
if (server.getCharset() != null) {
|
|
|
|
for (int i = 0; i < charsets.length; i++) {
|
|
|
|
if (server.getCharset().equals(charsets[i])) {
|
|
|
|
spinner.setSelection(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-03-20 10:44:05 -04:00
|
|
|
}
|
2010-03-20 11:32:42 -04:00
|
|
|
|
|
|
|
Uri uri = getIntent().getData();
|
|
|
|
if (uri != null && uri.getScheme().equals("irc")) {
|
|
|
|
// handling an irc:// uri
|
|
|
|
|
|
|
|
((EditText) findViewById(R.id.host)).setText(uri.getHost());
|
|
|
|
if (uri.getPort() != -1) {
|
|
|
|
((EditText) findViewById(R.id.port)).setText(String.valueOf(uri.getPort()));
|
|
|
|
}
|
|
|
|
}
|
2009-12-17 15:27:57 -05:00
|
|
|
}
|
2010-04-25 04:53:29 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* On activity result
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
protected void onActivityResult(int requestCode, int resultCode, Intent data)
|
|
|
|
{
|
|
|
|
if (resultCode == RESULT_OK) {
|
|
|
|
channels = data.getExtras().getStringArrayList(Extra.CHANNELS);
|
|
|
|
}
|
|
|
|
}
|
2009-12-17 15:27:57 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* On click add server or cancel activity
|
|
|
|
*/
|
|
|
|
public void onClick(View v)
|
|
|
|
{
|
|
|
|
switch (v.getId()) {
|
2010-04-25 04:19:59 -04:00
|
|
|
case R.id.channels:
|
2010-04-25 04:53:29 -04:00
|
|
|
Intent intent = new Intent(this, AddChannelActivity.class);
|
|
|
|
intent.putExtra(Extra.CHANNELS, channels);
|
|
|
|
startActivityForResult(intent, 0);
|
2010-04-25 04:19:59 -04:00
|
|
|
break;
|
2009-12-17 15:27:57 -05:00
|
|
|
case R.id.add:
|
2010-03-14 08:34:25 -04:00
|
|
|
try {
|
|
|
|
validateServer();
|
|
|
|
validateIdentity();
|
2010-03-20 10:44:05 -04:00
|
|
|
if (server == null) {
|
|
|
|
addServer();
|
|
|
|
} else {
|
|
|
|
updateServer();
|
|
|
|
}
|
2010-03-14 08:34:25 -04:00
|
|
|
setResult(RESULT_OK);
|
|
|
|
finish();
|
|
|
|
} catch(ValidationException e) {
|
|
|
|
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
2009-12-17 15:27:57 -05:00
|
|
|
break;
|
|
|
|
case R.id.cancel:
|
|
|
|
setResult(RESULT_CANCELED);
|
|
|
|
finish();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-03-14 08:34:25 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add server to database
|
|
|
|
*/
|
|
|
|
private void addServer()
|
|
|
|
{
|
2010-03-20 10:44:05 -04:00
|
|
|
Database db = new Database(this);
|
2010-03-14 08:34:25 -04:00
|
|
|
|
2010-03-20 10:44:05 -04:00
|
|
|
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
|
2010-04-13 18:26:06 -04:00
|
|
|
server.useSSL(),
|
2010-04-05 12:53:49 -04:00
|
|
|
identityId,
|
2010-04-05 16:36:04 -04:00
|
|
|
server.getCharset()
|
2010-03-20 10:44:05 -04:00
|
|
|
);
|
|
|
|
|
2010-04-25 05:17:44 -04:00
|
|
|
db.setChannels((int) serverId, channels);
|
2010-04-25 05:05:12 -04:00
|
|
|
|
2010-03-20 10:44:05 -04:00
|
|
|
db.close();
|
|
|
|
|
|
|
|
server.setId((int) serverId);
|
|
|
|
server.setIdentity(identity);
|
|
|
|
|
|
|
|
Yaaic.getInstance().addServer(server);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update server
|
|
|
|
*/
|
|
|
|
private void updateServer()
|
|
|
|
{
|
2010-03-14 08:34:25 -04:00
|
|
|
Database db = new Database(this);
|
|
|
|
|
2010-03-20 10:44:05 -04:00
|
|
|
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
|
2010-04-13 18:26:06 -04:00
|
|
|
server.useSSL(),
|
2010-04-05 12:53:49 -04:00
|
|
|
identityId,
|
2010-04-05 16:36:04 -04:00
|
|
|
server.getCharset()
|
2010-03-20 10:44:05 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
Identity identity = getIdentityFromView();
|
|
|
|
db.updateIdentity(
|
|
|
|
identityId,
|
|
|
|
identity.getNickname(),
|
|
|
|
identity.getIdent(),
|
|
|
|
identity.getNickname()
|
|
|
|
);
|
2010-03-14 08:34:25 -04:00
|
|
|
|
2010-04-25 05:17:44 -04:00
|
|
|
db.setChannels(serverId, channels);
|
2010-04-25 05:05:12 -04:00
|
|
|
|
2010-03-14 08:34:25 -04:00
|
|
|
db.close();
|
|
|
|
|
2010-03-20 10:44:05 -04:00
|
|
|
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()
|
|
|
|
{
|
2010-04-25 05:13:26 -04:00
|
|
|
String title = ((EditText) findViewById(R.id.title)).getText().toString().trim();
|
|
|
|
String host = ((EditText) findViewById(R.id.host)).getText().toString().trim();
|
|
|
|
int port = Integer.parseInt(((EditText) findViewById(R.id.port)).getText().toString().trim());
|
|
|
|
String password = ((EditText) findViewById(R.id.password)).getText().toString().trim();
|
2010-04-05 16:36:04 -04:00
|
|
|
String charset = ((Spinner) findViewById(R.id.charset)).getSelectedItem().toString();
|
2010-04-13 18:26:06 -04:00
|
|
|
Boolean useSSL = ((CheckBox) findViewById(R.id.useSSL)).isChecked();
|
2010-03-20 10:44:05 -04:00
|
|
|
|
|
|
|
// not in use yet
|
|
|
|
//boolean autoConnect = ((CheckBox) findViewById(R.id.autoconnect)).isChecked();
|
|
|
|
|
2010-03-14 08:34:25 -04:00
|
|
|
Server server = new Server();
|
|
|
|
server.setHost(host);
|
|
|
|
server.setPort(port);
|
2010-03-20 10:44:05 -04:00
|
|
|
server.setPassword(password);
|
2010-03-14 08:34:25 -04:00
|
|
|
server.setTitle(title);
|
2010-04-05 16:36:04 -04:00
|
|
|
server.setCharset(charset);
|
2010-04-13 13:25:09 -04:00
|
|
|
server.setUseSSL(useSSL);
|
2010-03-14 08:34:25 -04:00
|
|
|
server.setStatus(Status.DISCONNECTED);
|
2010-03-20 10:44:05 -04:00
|
|
|
|
|
|
|
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();
|
2010-03-14 08:34:25 -04:00
|
|
|
|
|
|
|
Identity identity = new Identity();
|
|
|
|
identity.setNickname(nickname);
|
|
|
|
identity.setIdent(ident);
|
|
|
|
identity.setRealName(realname);
|
|
|
|
|
2010-03-20 10:44:05 -04:00
|
|
|
return identity;
|
2010-03-14 08:34:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate the input for a server
|
|
|
|
*
|
|
|
|
* @throws ValidationException
|
|
|
|
*/
|
|
|
|
private void validateServer() throws ValidationException
|
|
|
|
{
|
|
|
|
String title = ((EditText) findViewById(R.id.title)).getText().toString();
|
|
|
|
String host = ((EditText) findViewById(R.id.host)).getText().toString();
|
|
|
|
String port = ((EditText) findViewById(R.id.port)).getText().toString();
|
2010-04-05 16:36:04 -04:00
|
|
|
String charset = ((Spinner) findViewById(R.id.charset)).getSelectedItem().toString();
|
2010-03-14 08:34:25 -04:00
|
|
|
|
|
|
|
if (title.trim().equals("")) {
|
|
|
|
throw new ValidationException("Title cannot be blank");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (host.trim().equals("")) {
|
|
|
|
// XXX: We should use some better host validation
|
|
|
|
throw new ValidationException("Host cannot be blank");
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
Integer.parseInt(port);
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
throw new ValidationException("Enter a numeric port");
|
|
|
|
}
|
2010-03-20 12:52:42 -04:00
|
|
|
|
2010-04-05 16:36:04 -04:00
|
|
|
try {
|
|
|
|
"".getBytes(charset);
|
|
|
|
}
|
|
|
|
catch (UnsupportedEncodingException e) {
|
|
|
|
throw new ValidationException("Charset is not supported by your device");
|
|
|
|
}
|
|
|
|
|
2010-03-20 12:52:42 -04:00
|
|
|
Database db = new Database(this);
|
|
|
|
if (db.isTitleUsed(title) && (server == null || !server.getTitle().equals(title))) {
|
|
|
|
db.close();
|
|
|
|
throw new ValidationException("There is already a server with this title");
|
|
|
|
}
|
|
|
|
db.close();
|
2010-03-14 08:34:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate the input for a identity
|
|
|
|
*
|
|
|
|
* @throws ValidationException
|
|
|
|
*/
|
|
|
|
private void validateIdentity() throws ValidationException
|
|
|
|
{
|
|
|
|
String nickname = ((EditText) findViewById(R.id.nickname)).getText().toString();
|
|
|
|
String ident = ((EditText) findViewById(R.id.ident)).getText().toString();
|
2010-03-14 10:43:26 -04:00
|
|
|
String realname = ((EditText) findViewById(R.id.realname)).getText().toString();
|
2010-03-14 08:34:25 -04:00
|
|
|
|
|
|
|
if (nickname.trim().equals("")) {
|
|
|
|
throw new ValidationException("Nickname cannot be blank");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ident.trim().equals("")) {
|
|
|
|
throw new ValidationException("Ident cannot be blank");
|
|
|
|
}
|
|
|
|
|
2010-03-14 10:43:26 -04:00
|
|
|
if (realname.trim().equals("")) {
|
|
|
|
throw new ValidationException("Realname cannot be blank");
|
|
|
|
}
|
|
|
|
|
2010-03-14 08:34:25 -04:00
|
|
|
// RFC 1459: <nick> ::= <letter> { <letter> | <number> | <special> }
|
|
|
|
// <special> ::= '-' | '[' | ']' | '\' | '`' | '^' | '{' | '}'
|
|
|
|
// Chars that are not in RFC 1459 but are supported too:
|
|
|
|
// | and _
|
2010-04-18 16:45:53 -04:00
|
|
|
Pattern nickPattern = Pattern.compile("^[a-zA-Z][a-zA-Z0-9^\\-`\\[\\]{}|_\\\\]*$");
|
2010-03-14 08:34:25 -04:00
|
|
|
if (!nickPattern.matcher(nickname).matches()) {
|
|
|
|
throw new ValidationException("Invalid nickname");
|
|
|
|
}
|
|
|
|
|
|
|
|
// We currently only allow chars as ident
|
|
|
|
Pattern identPattern = Pattern.compile("^[a-zA-Z]+$");
|
|
|
|
if (!identPattern.matcher(ident).matches()) {
|
|
|
|
throw new ValidationException("Invalid ident");
|
|
|
|
}
|
|
|
|
}
|
2009-12-17 15:27:57 -05:00
|
|
|
}
|