📄 roster.java
字号:
/* * Copyright 2004 Grzegorz Grasza groz@gryf.info * * This file is part of mobber. Mobber 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. * Mobber 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 mobber; * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA . */import javax.microedition.lcdui.*;import java.util.*;import java.io.*;import javax.microedition.rms.*;public class Roster extends List implements CommandListener{ public boolean noRoster = true; Image images[] = new Image[9]; Vector contacts = new Vector(); Hashtable names = new Hashtable(); Hashtable groups = new Hashtable(); Hashtable shows = new Hashtable(); Hashtable statuses = new Hashtable(); //Hashtable servers = new Hashtable(); Hashtable presentGroups = new Hashtable(); public Roster() { super(Label.roster, Choice.IMPLICIT); addCommand(Action.send); addCommand(Action.read); addCommand(Action.presence); //addCommand(Action.history); //addCommand(Action.info); //addCommand(Action.extend); addCommand(Action.message); //addCommand(Action.edit); //addCommand(Action.add); //addCommand(Action.delete); //addCommand(Action.clrhistory); //addCommand(Action.syncroster); addCommand(Action.logout); setCommandListener(this); try { images[0] = Image.createImage("/online.png"); images[1] = Image.createImage("/chat.png"); images[2] = Image.createImage("/dnd.png"); images[3] = Image.createImage("/away.png"); images[4] = Image.createImage("/xa.png"); images[5] = Image.createImage("/offline.png"); images[6] = Image.createImage("/message.png"); images[7] = images[5]; images[8] = null; } catch(Exception e) { //e.printStackTrace(); } } public int setContact(Contact contact) { if(!contact.jid.equals("")) { if(contact.name == null && (contact.name = (String)names.get(contact.jid)) == null) contact.name = ""; else names.put(contact.jid, contact.name); if(contact.show != Contact.none) shows.put(contact.jid, new Integer(contact.show)); else if(shows.containsKey(contact.jid)) contact.show = ((Integer)shows.get(contact.jid)).intValue(); if(contact.status != null) statuses.put(contact.jid, contact.status); else if(statuses.containsKey(contact.jid)) contact.status = (String)statuses.get(contact.jid); if(!contact.group.equals("")) { if(!groups.containsKey(contact.jid)) groups.put(contact.jid, new Vector()); if(((Vector)groups.get(contact.jid)).indexOf(contact.group) == -1) ((Vector)groups.get(contact.jid)).addElement(contact.group); if(!presentGroups.containsKey(contact.group)) { presentGroups.put(contact.group, new Object()); setContact(new Contact(contact.group)); } } //if(contact.jid.indexOf('@')==-1) // transports.put(contact.jid, new Object()); } int b = 0; if(!contacts.isEmpty()) { int a = contacts.size(); while(a > b) { int cen = (b + a) / 2; Contact con = (Contact)contacts.elementAt(cen); int cmp = contact.compare(con); if(cmp == 0) { //if(contact.show != Contact.none) // con.show = contact.show; //if(contact.status != null) // con.status = contact.status; //if(!contact.name.equals("")) // con.name = contact.name; //contacts.setElementAt(con, cen); //set(cen, (con.name == null ? con.jid : con.name) + ((con.status == null || con.status.equals("")) ? "" : (" : " + con.status)), images[con.show]); contacts.setElementAt(contact, cen); set(cen, (contact.name.equals("") ? contact.jid : contact.name) + ((contact.status == null || contact.status.equals("")) ? "" : (" : " + contact.status)), images[contact.show]); return cen; } else if(cmp > 0) b = cen + 1; else a = cen; } } contacts.insertElementAt(contact, b); insert(b, (contact.name.equals("") ? contact.jid : contact.name) + ((contact.status == null || contact.status.equals("")) ? "" : (" : " + contact.status)), images[contact.show]); //if(contact.jid.equals("")) // MIDP 2.0 // setFont(b, Font.getFont(Font.STYLE_BOLD)); return b; } public void removeContact(String jid) { groups.remove(jid); names.remove(jid); for(int i=0; i<contacts.size(); i++) if (((Contact)contacts.elementAt(i)).isEqual(jid)) { contacts.removeElementAt(i); delete(i); } } public void commandAction(Command c, Displayable s) { int g = getSelectedIndex(); if(g == -1) mobber.display.setCurrent(new SetContact("")); else { String jid = ((Contact)contacts.elementAt(g)).jid; if(jid == null) jid = ""; if(c == List.SELECT_COMMAND && !jid.equals("")) mobber.display.setCurrent(new NewMessage (jid)); else if(c == Action.send && !jid.equals("")) mobber.display.setCurrent(new NewMessage (jid)); else if(c == Action.read) mobber.message.show(); else if(c == Action.presence) mobber.display.setCurrent(new Presence()); /*else if(c == Action.history) { ; } else if(c == Action.info) { ; } else if(c == Action.extend) { ; }*/ else if(c == Action.message) mobber.display.setCurrent(new Topic()); else if(c == Action.edit && !jid.equals("")) mobber.display.setCurrent(new SetContact(jid)); else if(c == Action.add) mobber.display.setCurrent(new SetContact("")); else if(c == Action.delete && !jid.equals("")) mobber.jabber.setContact(jid, null, null, "remove"); /*else if(c == Action.clrhistory) ; else if(c == Action.syncroster) mobber.jabber.getRoster();*/ else if(c == Action.logout) { save(); mobber.jabber.end(); mobber.kill(); } } } public void save() // clears roster { names = null; groups = null; shows = null; statuses = null; RecordStore store = null; try { store = RecordStore.openRecordStore("roster",true); if(store.getNumRecords()>0) { store.closeRecordStore(); RecordStore.deleteRecordStore("roster"); store = RecordStore.openRecordStore("roster",true); } ByteArrayOutputStream bstr = new ByteArrayOutputStream(); DataOutputStream str = new DataOutputStream(bstr); Contact contact; str.writeInt(contacts.size()); while(!contacts.isEmpty()) { contact = (Contact)contacts.firstElement(); contacts.removeElementAt(0); str.writeUTF(contact.jid == null ? "" : contact.jid); str.writeUTF(contact.name == null ? "" : contact.name); str.writeUTF(contact.group == null ? "" : contact.group); } byte data[] = bstr.toByteArray(); store.addRecord(data,0,data.length); } catch(Exception e) { //e.printStackTrace(); } if(store != null) { try { store.closeRecordStore(); } catch(Exception e) { //e.printStackTrace(); } } } public void restore() // clears roster { RecordStore store = null; try { store = RecordStore.openRecordStore("roster", true); if(store.getNumRecords() == 1) { byte[] data=store.getRecord(1); DataInputStream str=new DataInputStream(new ByteArrayInputStream(data)); int e = str.readInt(); while(e-->0) mobber.roster.setContact(new Contact(str.readUTF(), str.readUTF(), str.readUTF())); noRoster = false; } } catch(Exception e) { //e.printStackTrace(); } if(store!=null) { try { store.closeRecordStore(); } catch(Exception e) { //e.printStackTrace(); } } }};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -