import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Timer; import java.util.TimerTask; import java.util.Vector; import javax.microedition.io.Connector; import javax.microedition.io.ContentConnection; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Form; import javax.microedition.lcdui.Gauge; import javax.microedition.lcdui.List; import javax.microedition.lcdui.Screen; import javax.microedition.lcdui.StringItem; import javax.microedition.lcdui.TextBox; import javax.microedition.lcdui.TextField; import javax.microedition.midlet.MIDlet; import javax.microedition.rms.InvalidRecordIDException; import javax.microedition.rms.RecordComparator; import javax.microedition.rms.RecordEnumeration; import javax.microedition.rms.RecordStore; import javax.microedition.rms.RecordStoreException; import javax.microedition.rms.RecordStoreFullException; import javax.microedition.rms.RecordStoreNotFoundException; import javax.microedition.rms.RecordStoreNotOpenException; /* * Created on 2005-05-29 */ //TODO priorytet zasobu //TODO edycja kontaktów //TODO bugfix nowego kontaktu //TODO roster //TODO historia /** * @author skolima */ public class skolima extends MIDlet implements CommandListener { /** * */ class receiver { String display; String JID; int recordNum; } static class configID { static int jid = 1; static int passwd = 2; static int serverURL = 3; static int listeningTime = 4; } //-------------------------- private RecordStore config = null; private RecordStore users = null; //-------------------------- private List mainScreen; private Command exitCommand; private Command newCommand; private Command readCommand; private Command configCommand; private Command addCommand; private Command deleteCommand; //--------------------------- private Form configScreen; private TextField jid; private TextField passwd; private TextField serverURL; private TextField listeningTime; private Command cancelCommand; private Command saveCommand; //--------------------------- private Form addScreen; private TextField newUserJid; private TextField newUserApp; private TextField newUserDisplay; private Command saveUserCommand; private Vector userData; //--------------------------- private Form deleteScreen; private StringItem userToDelete; private Command deleteUserCommand; //---------------------------- private Form waitScreen; private Gauge progressBar; private Timer waitTimer; //---------------------------- private Form readScreen; private StringItem readResult; private Reader getIt; //---------------------------- private TextBox writeScreen; private Command sendCommand; public skolima() { mainScreen = new List("http-jabber", List.IMPLICIT); exitCommand = new Command("Wyjście", Command.EXIT, 1); newCommand = new Command("Nowa", Command.SCREEN, 1); readCommand = new Command("Sprawdź", Command.SCREEN, 1); addCommand = new Command("Dodaj kontakt", Command.SCREEN, 1); deleteCommand = new Command("Usuń kontakt", Command.SCREEN, 1); configCommand = new Command("Konfiguracja", Command.SCREEN, 1); mainScreen.addCommand(exitCommand); mainScreen.addCommand(newCommand); mainScreen.addCommand(readCommand); mainScreen.addCommand(addCommand); mainScreen.addCommand(deleteCommand); mainScreen.addCommand(configCommand); mainScreen.setCommandListener(this); //---------------------------------- configScreen = new Form("Konfiguracja konta"); cancelCommand = new Command("Wróć", Command.CANCEL, 1); saveCommand = new Command("Zatwierdź", Command.OK, 1); jid = new TextField("ID użytkownika", null, 250, TextField.ANY); passwd = new TextField("hasło do konta", null, 250, TextField.PASSWORD); serverURL = new TextField("adres skryptu", "http://", 250, TextField.URL); listeningTime = new TextField("nasłuchuj odpowiedzi przez [s]", "10", 2, TextField.NUMERIC); configScreen.addCommand(cancelCommand); configScreen.addCommand(saveCommand); configScreen.append(jid); configScreen.append(passwd); configScreen.append(serverURL); configScreen.append(listeningTime); configScreen.setCommandListener(this); //--------------------------------- addScreen = new Form("Dodaj kontakt"); saveUserCommand = new Command("Zatwierdź", Command.OK, 1); newUserJid = new TextField("JID", null, 250, TextField.EMAILADDR); newUserApp = new TextField("zasób (opcjonalny)", null, 250, TextField.ANY); newUserDisplay = new TextField("wyświetl jako", null, 250, TextField.ANY); addScreen.addCommand(cancelCommand); addScreen.addCommand(saveUserCommand); addScreen.append(newUserJid); addScreen.append(newUserApp); addScreen.append(newUserDisplay); addScreen.setCommandListener(this); //--------------------------------- deleteScreen = new Form("Usuń kontakt"); deleteUserCommand = new Command("Usuń", Command.OK, 1); userToDelete = new StringItem("Czy na pewno usunąć : ", "[nikt nie został wybrany]"); deleteScreen.addCommand(cancelCommand); deleteScreen.addCommand(deleteUserCommand); deleteScreen.append(userToDelete); deleteScreen.setCommandListener(this); //--------------------------------- waitScreen = new Form("Odczyt wiadomości"); progressBar = new Gauge("Czekam...", false, 5+Integer.parseInt(listeningTime.getString()), 0); waitScreen.append(progressBar); waitTimer = new Timer(); waitScreen.setCommandListener(this); //--------------------------------- readScreen = new Form("Odczyt wiadomości"); readResult = new StringItem("Odebrałem : ", "[tutaj zaraz wyświetli się wiadomość]"); readScreen.addCommand(cancelCommand); readScreen.append(readResult); readScreen.setCommandListener(this); //--------------------------------- writeScreen = new TextBox("Wysyłka wiadomości", null, 1000, TextField.ANY); sendCommand = new Command("Wyślij", Command.SCREEN, 1); writeScreen.addCommand(cancelCommand); writeScreen.addCommand(sendCommand); writeScreen.setCommandListener(this); //--------------------------------- try { config = RecordStore.openRecordStore("mainConfig",true); users = RecordStore.openRecordStore("users",true); } catch (RecordStoreFullException e) { e.printStackTrace(); } catch (RecordStoreNotFoundException e) { e.printStackTrace(); } catch (RecordStoreException e) { e.printStackTrace(); } //---------------------------------- userData = new Vector(); } protected void startApp() { this.commandAction(cancelCommand, configScreen); try { if (users.getNumRecords() > 0) { RecordEnumeration dtb = users.enumerateRecords(null,new RecordComparator() { //anonimowy komparator - sortowanie leksykograficzne public int compare(byte[] arg0, byte[] arg1) { try { DataInputStream istream = new DataInputStream(new ByteArrayInputStream(arg0, 0, arg0.length)); String record1 = istream.readUTF(); istream = new DataInputStream(new ByteArrayInputStream(arg1, 0, arg1.length)); String record2 = istream.readUTF(); int result = record1.compareTo(record2); if(result == 0)return RecordComparator.EQUIVALENT; else if (result < 0)return RecordComparator.PRECEDES; else return RecordComparator.FOLLOWS; } catch (IOException e) { e.printStackTrace(); } return RecordComparator.EQUIVALENT; } },true); while(dtb.hasNextElement()) { byte[] record; receiver toAdd = new receiver(); toAdd.recordNum = dtb.nextRecordId();//przesuwa wskaźnik record = users.getRecord(toAdd.recordNum); DataInputStream istream = new DataInputStream(new ByteArrayInputStream(record, 0, record.length)); toAdd.display = istream.readUTF(); toAdd.JID = istream.readUTF(); //ten sam index na liście i w wektorze userData.insertElementAt(toAdd,mainScreen.append(toAdd.display,null)); } } } catch (RecordStoreNotOpenException e) { e.printStackTrace(); } catch (InvalidRecordIDException e) { e.printStackTrace(); } catch (RecordStoreException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } protected void pauseApp() {} protected void destroyApp(boolean bool) {} public void commandAction(Command cmd, Displayable disp) { if (cmd == exitCommand) { if(config != null) try { config.closeRecordStore(); users.closeRecordStore(); } catch (RecordStoreNotOpenException e) { e.printStackTrace(); } catch (RecordStoreException e) { e.printStackTrace(); } destroyApp(false); notifyDestroyed(); } else if (cmd == configCommand) { Display.getDisplay(this).setCurrent(configScreen); } else if (cmd == cancelCommand) { try { if (config.getNumRecords() > 0){//odczyt //ID byte[] record = config.getRecord(configID.jid); DataInputStream istream = new DataInputStream(new ByteArrayInputStream(record, 0, record.length)); jid.setString(istream.readUTF()); //passwd record = config.getRecord(configID.passwd); istream = new DataInputStream(new ByteArrayInputStream(record, 0, record.length)); passwd.setString(istream.readUTF()); //skrypt record = config.getRecord(configID.serverURL); istream = new DataInputStream(new ByteArrayInputStream(record, 0, record.length)); serverURL.setString(istream.readUTF()); //czas nasłuchiwania record = config.getRecord(configID.listeningTime); istream = new DataInputStream(new ByteArrayInputStream(record, 0, record.length)); listeningTime.setString(new Integer(istream.readInt()).toString()); } else {//brak danych zapisanych w pamięci ByteArrayOutputStream bstream = new ByteArrayOutputStream(250); DataOutputStream ostream = new DataOutputStream(bstream); //ID ostream.writeUTF(jid.getString()); ostream.flush(); ostream.close(); byte[] record = bstream.toByteArray(); config.addRecord(record, 0, record.length); //passwd bstream = new ByteArrayOutputStream(250); ostream = new DataOutputStream(bstream); ostream.writeUTF(passwd.getString()); ostream.flush(); ostream.close(); record = bstream.toByteArray(); config.addRecord(record, 0, record.length); //skrypt bstream = new ByteArrayOutputStream(250); ostream = new DataOutputStream(bstream); ostream.writeUTF(serverURL.getString()); ostream.flush(); ostream.close(); record = bstream.toByteArray(); config.addRecord(record, 0, record.length); //czas nasłuchu bstream = new ByteArrayOutputStream(4); ostream = new DataOutputStream(bstream); ostream.writeInt(Integer.parseInt(listeningTime.getString())); ostream.flush(); ostream.close(); record = bstream.toByteArray(); config.addRecord(record, 0, record.length); } Display.getDisplay(this).setCurrent(mainScreen); } catch (RecordStoreNotOpenException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (RecordStoreFullException e) { e.printStackTrace(); } catch (RecordStoreException e) { e.printStackTrace(); } } else if (cmd == saveCommand) { try { ByteArrayOutputStream bstream = new ByteArrayOutputStream(250); DataOutputStream ostream = new DataOutputStream(bstream); //ID ostream.writeUTF(jid.getString()); ostream.flush(); ostream.close(); byte[] record = bstream.toByteArray(); config.setRecord(configID.jid, record, 0, record.length); //passwd bstream = new ByteArrayOutputStream(250); ostream = new DataOutputStream(bstream); ostream.writeUTF(passwd.getString()); ostream.flush(); ostream.close(); record = bstream.toByteArray(); config.setRecord(configID.passwd, record, 0, record.length); //skrypt bstream = new ByteArrayOutputStream(250); ostream = new DataOutputStream(bstream); ostream.writeUTF(serverURL.getString()); ostream.flush(); ostream.close(); record = bstream.toByteArray(); config.setRecord(configID.serverURL, record, 0, record.length); //czas nasłuchu bstream = new ByteArrayOutputStream(4); ostream = new DataOutputStream(bstream); ostream.writeInt(Integer.parseInt(listeningTime.getString())); ostream.flush(); ostream.close(); record = bstream.toByteArray(); config.setRecord(configID.listeningTime,record, 0, record.length); } catch (IOException e) { e.printStackTrace(); } catch (RecordStoreNotOpenException e) { e.printStackTrace(); } catch (RecordStoreFullException e) { e.printStackTrace(); } catch (RecordStoreException e) { e.printStackTrace(); } Display.getDisplay(this).setCurrent(mainScreen); } else if (cmd == addCommand) { Display.getDisplay(this).setCurrent(addScreen); } else if (cmd == saveUserCommand) { if(newUserDisplay.getString()==""||newUserDisplay.getString()==null ||newUserJid.getString()==""||newUserJid.getString()==null) { Display.getDisplay(this).setCurrent(mainScreen); return; } receiver toAdd = new receiver(); toAdd.display = newUserDisplay.getString(); toAdd.JID = newUserJid.getString(); if(!(newUserApp.getString().equals("")||newUserApp.getString()==null)) toAdd.JID = toAdd.JID.concat("/"+newUserApp.getString()); int position = mainScreen.append(newUserDisplay.getString(),null); //a teraz zapis do dtb ByteArrayOutputStream bstream = new ByteArrayOutputStream(751); DataOutputStream ostream = new DataOutputStream(bstream); try { ostream.writeUTF(toAdd.display); ostream.writeUTF(toAdd.JID); ostream.flush(); ostream.close(); byte[] record = bstream.toByteArray(); toAdd.recordNum = users.addRecord(record, 0, record.length); userData.insertElementAt(toAdd.display,position); } catch (IOException e) { e.printStackTrace(); } catch (RecordStoreNotOpenException e) { e.printStackTrace(); } catch (RecordStoreFullException e) { e.printStackTrace(); } catch (RecordStoreException e) { e.printStackTrace(); } Display.getDisplay(this).setCurrent(mainScreen); } else if (cmd == deleteCommand) { if(mainScreen.getSelectedIndex()<0)return; userToDelete.setText(mainScreen.getString(mainScreen.getSelectedIndex())); Display.getDisplay(this).setCurrent(deleteScreen); } else if (cmd == deleteUserCommand) { try { //kasowanie danych receiver toDel = (receiver) userData.elementAt(mainScreen.getSelectedIndex()); users.deleteRecord(toDel.recordNum); } catch (RecordStoreNotOpenException e) { e.printStackTrace(); } catch (InvalidRecordIDException e) { e.printStackTrace(); } catch (RecordStoreException e) { e.printStackTrace(); } userData.removeElementAt(mainScreen.getSelectedIndex()); mainScreen.delete(mainScreen.getSelectedIndex()); Display.getDisplay(this).setCurrent(mainScreen); } else if (cmd == readCommand) { Display.getDisplay(this).setCurrent(waitScreen); progressBar.setValue(0); progressBar.setMaxValue(2*Integer.parseInt(listeningTime.getString())); waitTimer.scheduleAtFixedRate(new _Ticker(),0,1000); getIt = new Reader(serverURL.getString()+"?u="+jid.getString()+"&p="+ passwd.getString()+"&c=r&w="+listeningTime.getString()); getIt.start(); } else if (cmd == newCommand) { if(mainScreen.getSelectedIndex()<0)return; Display.getDisplay(this).setCurrent(writeScreen); writeScreen.setTitle(mainScreen.getString(mainScreen.getSelectedIndex())); } else if (cmd == sendCommand) { Display.getDisplay(this).setCurrent(waitScreen); progressBar.setValue(0); progressBar.setMaxValue(2*Integer.parseInt(listeningTime.getString())); waitTimer.scheduleAtFixedRate(new _Ticker(),0,1000); getIt = new Reader(serverURL.getString()+"?u="+ParanoidEncode(jid.getString())+"&p="+ ParanoidEncode(passwd.getString())+"&c=s&w="+listeningTime.getString()+ "&r="+ParanoidEncode(((receiver)userData.elementAt(mainScreen.getSelectedIndex())).JID)+ "&m="+ParanoidEncode(writeScreen.getString())); getIt.start(); } } private void _setDisplay(Screen disp) { Display.getDisplay(this).setCurrent(disp); } private String ParanoidEncode(String URL) { String result = new String(); for(int i =0;i=48&&toWrite<=57)||//0-9 (toWrite>=65&&toWrite<=90)||//A-Z (toWrite>=97&&toWrite<=122)||//a-z toWrite=='$'||toWrite=='-'|| toWrite=='_'||toWrite=='.'|| toWrite=='!'||toWrite==','|| toWrite=='*'||toWrite=='\''|| toWrite=='('||toWrite==')') result = result.concat(new String(URL.substring(i,i+1))); else if(toWrite<32);//znaki kontrolne else if(toWrite=='&') result = result.concat("%26amp%3B"); else if(toWrite=='<') result = result.concat("%26lt%3B"); else if(toWrite=='+') result = result.concat("%26plus%3B"); else switch(toWrite) {//podmieniamy pliterki case 'ą' : result = result.concat("%C4%85");break; case 'Ą' : result = result.concat("%C4%84");break; case 'ć' : result = result.concat("%C4%87");break; case 'Ć' : result = result.concat("%C4%86");break; case 'ę' : result = result.concat("%C4%99");break; case 'Ę' : result = result.concat("%C4%98");break; case 'ł' : result = result.concat("%C5%82");break; case 'Ł' : result = result.concat("%C5%81");break; case 'ń' : result = result.concat("%C5%84");break; case 'Ń' : result = result.concat("%C5%83");break; case 'ó' : result = result.concat("%C3%B3");break; case 'Ó' : result = result.concat("%C3%93");break; case 'ś' : result = result.concat("%C5%9B");break; case 'Ś' : result = result.concat("%C5%9A");break; case 'ź' : result = result.concat("%C5%BA");break; case 'ż' : result = result.concat("%C5%BC");break; case 'Ź' : result = result.concat("%C5%B9");break; case 'Ż' : result = result.concat("%C5%BB");break; default : { if(toWrite>126)result = result.concat("_"); else result = result.concat("%"+Integer.toHexString(toWrite).toUpperCase());} } } return result; } private class _Ticker extends TimerTask { public void run() { progressBar.setValue(progressBar.getValue()+1); if(progressBar.getValue()>=progressBar.getMaxValue()) { cancel(); } } } private class Reader extends Thread { String Address; Reader(String URL) { Address = URL; } public void run() { try { readResult.setText("[poczekaj aż wyświetli się wiadomość]"); //start reading ContentConnection conn = (ContentConnection) Connector.open(Address); InputStream in = conn.openInputStream(); int ch; ByteArrayOutputStream bstream = new ByteArrayOutputStream(); DataOutputStream ostream = new DataOutputStream(bstream); while( ( ch = in.read() ) != -1 ){ ostream.write((char) ch); } ostream.flush(); ostream.close(); byte[] record = bstream.toByteArray(); String result = new String(record,"UTF-8"); if(result == ""|| result == null||result.length()<=1)readResult.setText("[brak nowych wiadomości]"); else readResult.setText("\n"+result); in.close(); conn.close(); _setDisplay(readScreen); } catch (IOException e) { // problem z dostępem do sieci readResult.setText("[problem z dostępem do sieci, sprawdź połączenie]"); _setDisplay(readScreen); } catch (SecurityException e) { // brak zezwolenia na dostęp do sieci readResult.setText("[brak uprawnień do korzystania z transmisji danych]"); _setDisplay(readScreen); } } } }