00001 import java.io.File;
00002 import java.io.FileInputStream;
00003 import java.io.FileOutputStream;
00004 import java.io.IOException;
00005 import java.io.InputStream;
00006 import java.io.OutputStream;
00007
00008 import org.gnu.pilotlink.MidiRecord;
00009 import org.gnu.pilotlink.PilotLink;
00010 import org.gnu.pilotlink.PilotLinkException;
00011 import org.gnu.pilotlink.SysInfo;
00012 import org.gnu.pilotlink.User;
00013 public class MidiMgr {
00014 public final static int LIST=1;
00015 public final static int INSTALL=2;
00016 public final static int NOTHING=0;
00017 public final static int DELETE=3;
00018 public final static int FETCH=4;
00019
00020
00021 public static void main(String args[]) {
00022 String port="/dev/usb/tts/1";
00023 String db="MIDI Ring Tones";
00024 int command=0;
00025 String arg="";
00026 String arg2="";
00027 try {
00028 for (int i=0; i<args.length; i++) {
00029 if (args[i].equals("-p")) {
00030 port=args[i+1];
00031 i++;
00032 continue;
00033 }
00034
00035 if (args[i].equals("-l") || args[i].equals("--list")) {
00036 command=LIST;
00037 break;
00038 }
00039 if (args[i].equals("-i") || args[i].equals("--install")) {
00040 command=INSTALL;
00041 arg=args[i+1];
00042 if (!arg.endsWith(".mid")) {
00043 System.out.println("File to install must be a .mid-File!");
00044 System.exit(1);
00045 }
00046 File f=new File(arg);
00047 if (!f.exists()) {
00048 System.out.println("File to install must exist! "+arg+" not found!");
00049 System.exit(1);
00050 }
00051 arg2=args[i+2];
00052 break;
00053 }
00054 if (args[i].equals("-d") || args[i].equals("--delete")) {
00055 command=DELETE;
00056 arg=args[i+1];
00057 break;
00058 }
00059 if (args[i].equals("-f")|| args[i].equals("--fetch")) {
00060 command=FETCH;
00061 arg=args[i+1];
00062 break;
00063 }
00064
00065 }
00066 } catch (Exception e) {
00067 command=NOTHING;
00068 }
00069 if (command==NOTHING) {
00070 System.out.println("USAGE: java MidiMgr CMD ARG\nwhere CMD:\n-p PORT: Pilot-Port (default /dev/usb/tts/1)\n-db NAME: Palm-DB to use (default 'MIDI Ring Tones')\n" +
00071 "-l|--list : list all Midi-Files\n-d|--delete NAME: Delete File by name or id\n" +
00072 "-i|--install FILE NAME: Install Midi-File FILE as NAME");
00073 System.exit(1);
00074 }
00075
00076 File p= new File(port);
00077 System.out.println("looking for file " + port);
00078 if (!p.exists()) {
00079 System.out.println("File does not exist... DevFS? Waiting for port to appear");
00080
00081 while (!p.exists()) {
00082 System.out.print(".");
00083 try {
00084 Thread.sleep(1000);
00085 } catch (Exception e) {
00086 }
00087 }
00088 }
00089 PilotLink pl= null;
00090 try {
00091 pl= new PilotLink(port);
00092 if (!pl.isConnected()) {
00093 System.out.println("Something went wrong. Check output!");
00094 System.exit(1);
00095 }
00096 } catch (Exception e) {
00097 e.printStackTrace();
00098 System.exit(1);
00099 }
00100 try {
00101 User u= pl.getUserInfo();
00102 System.out.println("User: " + u.getName());
00103 System.out.println("Last Synchronization Date: " + u.getLastSyncDate());
00104 } catch (PilotLinkException e) {
00105 e.printStackTrace();
00106 }
00107 try {
00108 SysInfo si= pl.getSysInfo();
00109 System.out.println("Product ID: '" + si.getProdID() + "'");
00110 System.out.println("Rom Version: " + si.getRomVersion());
00111 } catch (PilotLinkException e) {
00112 e.printStackTrace();
00113 }
00114 int dbh;
00115 try {
00116 int db2= pl.openDB(db);
00117 if (db2 < 0) {
00118 System.out.println("ERROR! " + db2);
00119 System.exit(1);
00120 }
00121 switch (command) {
00124 case LIST:
00125 int count=pl.getRecordCount(db2);
00126 for (int i=0; i<count;i++) {
00127 MidiRecord mr=new MidiRecord(pl.getRecordByIndex(db2,i));
00128 System.out.println(i+": "+mr.getId()+" "+mr.getName()+" size: "+mr.getSize());
00129 }
00130 break;
00131
00134 case INSTALL:
00135 File midi=new File(arg);
00136 try {
00137 InputStream in=new FileInputStream(midi);
00138 long size=midi.length();
00139 byte buf[]=new byte[(int)size];
00140 in.read(buf);
00141 MidiRecord r=new MidiRecord();
00142 r.setName(arg2);
00143 r.setMidi(buf);
00144 in.close();
00145
00146 pl.writeNewRecord(db2,r);
00147 System.out.println("File Installed as "+arg2);
00148 } catch(Exception e){
00149 e.printStackTrace();
00150 }
00151 break;
00152
00155 case FETCH:
00156
00157 MidiRecord mr=null;
00158 try {
00159 int id=Integer.parseInt(arg);
00160 mr=new MidiRecord(pl.getRecordByIndex(db2,id));
00161 } catch(NumberFormatException e) {
00162 System.out.println("No Number! Looking for file...");
00163 count=pl.getRecordCount(db2);
00164 for (int i=0; i<count;i++) {
00165 mr=new MidiRecord(pl.getRecordByIndex(db2,i));
00166
00167 if (mr.getName().equals(arg)) {
00168 break;
00169 }
00170 }
00171 }
00172 if (mr!=null) {
00173 File f=new File(mr.getName()+".mid");
00174 try {
00175 f.createNewFile();
00176 OutputStream out=new FileOutputStream(f);
00177 out.write(mr.getMidi());
00178 out.close();
00179 System.out.println("File "+f.getName()+" stored successfully!");
00180 } catch (IOException e1) {
00181
00182 e1.printStackTrace();
00183 }
00184
00185 } else {
00186 System.out.println("Not found!");
00187 }
00188 break;
00189
00190
00193 case DELETE:
00194 MidiRecord r=null;
00195 try {
00196 int id=Integer.parseInt(arg);
00197 pl.deleteRecordById(db2,id);
00198 System.out.println("Index "+id+" deleted.");
00199 } catch(NumberFormatException e) {
00200 System.out.println("No Number! Looking for file...");
00201 count=pl.getRecordCount(db2);
00202 for (int i=0; i<count;i++) {
00203 r=new MidiRecord(pl.getRecordByIndex(db2,i));
00204 if (r.getName().equals(arg)) {
00205 pl.deleteRecordById(db2,r.getId());
00206 break;
00207 }
00208 }
00209 }
00210 break;
00211 }
00212
00213 pl.closeDB(db2);
00214 } catch (PilotLinkException e) {
00215 e.printStackTrace();
00216 }
00217 try {
00218 pl.endSync();
00219 } catch (Exception e) {
00220 e.printStackTrace();
00221 }
00222 pl.close();
00223
00224 System.exit(0);
00225 }
00226
00227 public static void hexdump(byte[] arr) {
00228
00229 for (int i= 0; i < arr.length;) {
00230 String chars= "";
00231 String l= "" + Integer.toHexString(i);
00232 while (l.length() < 4) {
00233 l= "0" + l;
00234 }
00235 System.out.print(l + ": ");
00236 for (int j= 0; j < 16 && i < arr.length; j++, i++) {
00237
00238 l= Integer.toHexString(arr[i]);
00239 if (arr[i] < 0) {
00240 l= l.substring(l.length() - 2);
00241 }
00242 while (l.length() < 2) {
00243 l= "0" + l;
00244 }
00245 System.out.print(l + " ");
00246 if ((arr[i] >= '0' && arr[i] <= 'z') || (arr[i] == ' ')) {
00247 chars += (char) arr[i];
00248 } else if (arr[i] == 252) {
00249 chars += "\ufffd";
00250 } else if (arr[i] ==246) {
00251 chars += "\ufffd";
00252 } else if (arr[i] == (byte) '\ufffd') {
00253 chars += "\ufffd";
00254 } else {
00255 chars += ".";
00256 }
00257
00258 }
00259 System.out.println(" " + chars);
00260 }
00261 }
00262
00263 }