/*
Copyright (C) 2004 Juho Vähä-Herttua
This program 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 2
of the License, or (at your option) any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package jmirc;
import java.io.*;
import java.util.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
public class Window extends Canvas implements CommandListener {
public static final int TYPE_CONSOLE = 0;
public static final int TYPE_CHANNEL = 1;
public static final int TYPE_PRIVATE = 2;
public static final int STATE_NONE = 0;
public static final int STATE_INFO = 1;
public static final int STATE_MSG = 2;
public static final int STATE_HILIGHT = 3;
public static final int STATE_SELECTED = 4;
private static final String CHANGE_NICK = "Change nick";
private static final String JOIN_CHANNEL = "Join channel";
private int type, state;
private UIHandler uihandler;
public Vector names;
private TextBox textbox;
private Command cmd_msg, cmd_join, cmd_query, cmd_nick, cmd_traffic, cmd_timestamp, cmd_disconnect;
private Command cmd_closeconsole;
private Command cmd_action, cmd_close;
private Command cmd_whois;
private Command cmd_names;
private Command cmd_send, cmd_send_action, cmd_cancel;
private Command cmd_ok;
private String name, header, hilight;
private long keylocktime;
private int buflines;
private boolean timestamp;
private final static int MAX_LIST_PERSONS = 20;
private int person_position = 0;
private List nameslist;
private List namecmdlist;
public Window(UIHandler uihandler, String name, int type, String hilight, boolean showheader, boolean timestamp, int buflines) {
this.uihandler = uihandler;
this.name = name;
this.header = name;
this.type = type;
this.hilight = hilight;
this.showheader = showheader;
this.timestamp = timestamp;
this.buflines = buflines;
state = STATE_NONE;
textarea = new TextArea(0, 0, getWidth(), getHeight(), buflines, true);;
names = new Vector();
setHeaderVisible(showheader);
cmd_ok = new Command("Ok", Command.OK, 1);
cmd_send = new Command("Send", Command.OK, 1);
cmd_send_action = new Command("Send", Command.OK, 1);
cmd_cancel = new Command("Cancel", Command.CANCEL, 2);
cmd_msg = new Command("Msg", Command.OK, 10);
cmd_join = new Command("Join", Command.SCREEN, 20);
cmd_query = new Command("Query", Command.SCREEN, 30);
cmd_nick = new Command("Change nick", Command.SCREEN, 40);
cmd_traffic = new Command("Bytecounter", Command.SCREEN, 50);
cmd_timestamp = new Command("Timestamp on", Command.SCREEN, 60);
cmd_disconnect = new Command("Disconnect", Command.SCREEN, 70);
cmd_action = new Command("Action", Command.OK, 80);
cmd_close = new Command("Close", Command.SCREEN, 11);
cmd_whois = new Command("Whois", Command.SCREEN, 12);
cmd_names = new Command("Names", Command.SCREEN, 12);
cmd_closeconsole = new Command("Close", Command.CANCEL, 1);
addMenu();
this.setCommandListener(this);
}
public void write(String nick, String text) {
if (!hilight.equals("") && text.indexOf(hilight) >= 0) {
write(new String[] {"<", nick, "> ", text}, new int[] {0,4,0,4});
if (state<STATE_HILIGHT) state = STATE_HILIGHT;
if (uihandler.keylock) uihandler.playAlarm(false);
}
else {
write(new String[] {"<", nick, "> " + text}, new int[] {0,4,0});
if (state<STATE_MSG) state = STATE_MSG;
}
}
public void writeAction(String str) {
write(new String[] {str}, new int[] {4});
if (state<STATE_MSG) state = STATE_MSG;
}
public void writeInfo(String str) {
write(new String[] {"*** " + str}, new int[] {2});
if (state<STATE_INFO) state = STATE_INFO;
}
public synchronized void write(String[] strings, int[] colours) {
boolean end = textarea.isAtEndpos();
if (timestamp) {
Calendar cal = Calendar.getInstance();
String time = "[" + cal.get(Calendar.HOUR_OF_DAY) + ":" +
(cal.get(Calendar.MINUTE)<10?"0":"") + cal.get(Calendar.MINUTE) + "]";
strings[0] = time + " " + strings[0];
}
textarea.addText(strings, colours);
if (end) textarea.setPosition(-1);
uihandler.repaint();
}
public String getName() {
return name;
}
private void show() {
uihandler.setDisplay(this);
state = STATE_NONE;
}
public void close() {
uihandler.deleteWindow(this);
}
private void sendData() {
String str = textbox.getString();
if (type == Window.TYPE_CONSOLE) {
jmIrc.writeLine(str);
write("rawcmd", str);
}
else if (str != null && str.length()>0) {
if (str.charAt(0) =='/') {
if (str.toUpperCase().startsWith("/MSG")) {
String[] s = Utils.splitString(str," ");
if (s.length >1) {
jmIrc.writeLine("PRIVMSG " + s[1] + " :" + str.substring(6 + s[1].length()));
}
}
else
jmIrc.writeLine(str.substring(1));
}
else {
jmIrc.writeLine("PRIVMSG " + name + " :" + str);
write(uihandler.nick, str);
}
}
show();
textbox = null;
}
private String trimName(String name) {
if (name.charAt(0) == '@' || name.charAt(0) == '+')
return name.substring(1).trim();
else
return name.trim();
}
private void addMenu() {
this.addCommand(cmd_msg);
this.addCommand(cmd_join);
this.addCommand(cmd_query);
this.addCommand(cmd_nick);
this.addCommand(cmd_traffic);
this.addCommand(cmd_timestamp);
this.addCommand(cmd_disconnect);
if (type == Window.TYPE_PRIVATE || type == Window.TYPE_CHANNEL) {
this.addCommand(cmd_action);
this.addCommand(cmd_close);
if (type == Window.TYPE_PRIVATE)
this.addCommand(cmd_whois);
else
this.addCommand(cmd_names);
}
}
private void deleteMenu() {
this.removeCommand(cmd_msg);
this.removeCommand(cmd_join);
this.removeCommand(cmd_query);
this.removeCommand(cmd_nick);
this.removeCommand(cmd_traffic);
this.removeCommand(cmd_timestamp);
this.removeCommand(cmd_disconnect);
if (type == Window.TYPE_PRIVATE || type == Window.TYPE_CHANNEL) {
this.removeCommand(cmd_action);
this.removeCommand(cmd_close);
if (type == Window.TYPE_PRIVATE)
this.removeCommand(cmd_whois);
else
this.removeCommand(cmd_names);
}
}
/* Channel.java starts here */
/* these are accessed from Listener.java, think about it... */
public boolean readinglist = false;
public String topic = "";
public void addName(String name) {
int i, nsize;
nsize = names.size();
String tuname = name.toUpperCase();
for (i=0; i<nsize; i++) {
String str = (String)names.elementAt(i);
if (tuname.compareTo(str.toUpperCase()) < 1) {
names.insertElementAt(name,i);
break;
}
}
if (i == nsize) names.addElement(name);
updateHeader();
}
public boolean hasName(String name) {
int s = names.size();
name = trimName(name);
for (int i=0;i<s;i++) {
String n = (String)names.elementAt(i);
if (trimName(n).equals(name)) {
return true;
}
}
return false;
}
public void deleteName(String id) {
int s = names.size();
for (int i=0;i<s;i++) {
String n = (String)names.elementAt(i);
if (trimName(n).equals(id)) {
names.removeElementAt(i);
break;
}
}
updateHeader();
}
private void listnames(Command c, Displayable s) {
nameslist = new List("Names", List.IMPLICIT);
nameslist.append("[Back]", null);
int mp = (person_position+1)*MAX_LIST_PERSONS;
if (person_position>0) {
nameslist.append("[Previous]",null);
}
if (names.size()>mp) {
nameslist.append("