File: test.java

package info (click to toggle)
pilot-link 0.12.5-dfsg-2
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 6,868 kB
  • ctags: 5,811
  • sloc: ansic: 53,153; sh: 10,459; java: 2,584; perl: 2,247; python: 1,044; makefile: 991; yacc: 662; cpp: 551; xml: 39
file content (256 lines) | stat: -rw-r--r-- 7,445 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import org.gnu.pilotlink.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
/**
 *  Description of the Class
 *
 *@author     stephan
 *@created    5. Dezember 2004
 */
public class test {
	/**
	 *  Description of the Method
	 *
	 *@param  args  Description of the Parameter
	 */
	public static void main(String args[]) {
		JFrame frame = new JFrame("Progress");
		frame.getContentPane().setLayout(new BorderLayout());
		JLabel txt = new JLabel("Reading in");
		frame.getContentPane().add(txt, BorderLayout.NORTH);
		JProgressBar progress = new JProgressBar(0, 100);
		frame.getContentPane().add(progress, BorderLayout.CENTER);
		frame.setSize(300, 50);
		frame.setVisible(true);

		Date yesterday = new Date(System.currentTimeMillis() - 3600000 * 24);
		System.out.println("Date 2 check: " + yesterday);
		String port;
		if (args.length == 0) {
			port = "/dev/usb/tts/1";
		} else {
			port = args[0];
		}
		File p = new File(port);
		System.out.println("looking for file " + port);
		if (!p.exists()) {
			System.out.println("File does not exist... USB? Waiting for port to appear");

			while (!p.exists()) {
				System.out.print(".");
				try {
					Thread.sleep(1000);
				} catch (Exception e) {}
			}
		}
		//System.out.println("Systemzeit: "+Long.toHexString(System.currentTimeMillis()/1000));
		PilotLink pl = null;
		try {
			pl = new PilotLink(port);
			if (!pl.isConnected()) {
				System.out.println("Something went wrong. Check output!");
				System.exit(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
		try {
			User u = pl.getUserInfo();
			System.out.println("User: " + u.getName());
			System.out.println("Last Synchronization Date: " + u.getLastSyncDate());
		} catch (PilotLinkException e) {
			e.printStackTrace();
		}

		System.out.println("GEtting App...");
		pl.getAppInfoBlock("AddressDB");
		if (true) {
			try {
				pl.endSync();
				pl.close();
				System.exit(1);
			} catch (Exception e) {}
		}
		try {
			SysInfo si = pl.getSysInfo();
			System.out.println("Product ID: '" + si.getProdID() + "'");
			System.out.println("Rom Version: " + si.getRomVersion());
		} catch (PilotLinkException e) {
			e.printStackTrace();
		}
		try {
			System.out.println("Opening catdb!");
			int dbh = pl.openDB("DatebookDB");
			System.out.println("db opened!");
			System.out.println("Count: " + pl.getRecordCount(dbh));
			progress.setMaximum(pl.getRecordCount(dbh));
			Vector ids = new Vector();

			for (int i = 0; i < pl.getRecordCount(dbh); i++) {
				Record r = pl.getRecordByIndex(dbh, i);
				progress.setValue(i);
				txt.setText("Reading in " + i);
				if (r == null) {
					break;
				}
				if (r.getBuffer() == null || r.getBuffer().length == 0) {
					System.out.println("0-sized record? Deleting...");
					pl.deleteRecordById(dbh, r.getId());
					continue;
				}
				if (!r.isDirty()) {
					//only new entries
					continue;
				}
				DatebookRecord dbr = new DatebookRecord(r);
				//System.out.println("comparing "+dbr.getStartDate()+ "to yesterday "+yesterday);
				//if (dbr.getStartDate().before(yesterday) || dbr.getStartDate().after(new Date())) {
				//    continue;
				//}
				ids.add(new Integer(i));
				System.out.println("Read index " + i);
				System.out.println("  id: " + dbr.getId());
				System.out.println("attr: " + Integer.toHexString(dbr.getAttribs()));
				System.out.println(" dirty:" + dbr.isDirty());
				System.out.println(" arch :" + dbr.isArchived());
				System.out.println(" del  :" + dbr.isDeleted());
				System.out.println("size: " + dbr.getSize());
				System.out.println("cat : " + dbr.getCategory());
				hexdump(dbr.getBuffer());
				hexdump(r.getBuffer());
				System.out.println("Has alarm  : " + dbr.hasAlarm());
				System.out.println("Description: " + dbr.hasDescription());
				System.out.println("Note       : " + dbr.hasNote());
				System.out.println("Reapeated  : " + dbr.isRepeated());
				System.out.println("hasTime    : " + dbr.hasTime());
				if (dbr.hasDescription()) {
					System.out.println("Descr: " + dbr.getDescription());
				}
				if (dbr.hasNote()) {
					System.out.println("Note: " + dbr.getNote());
				}
				System.out.println("Start: " + dbr.getStartDate());
				System.out.println("End: " + dbr.getEndDate());

			}
			//Doing this not to interfere with the reading of records
			for (int i = 0; i < ids.size(); i++) {
				System.out.println("updating..." + ids.get(i));
				Record r = pl.getRecordByIndex(dbh, ((Integer) ids.get(i)).intValue());
				r.setDirty(false);
				hexdump(r.getBuffer());
				if (r.isDirty()) {
					System.out.println("H\ufffd\ufffd\ufffd\ufffd?!");
				}
				//pl.writeRecord(dbh,r);
			}

			System.out.println("\n\n\nNew Datebookentry:\n");
			GregorianCalendar start = new GregorianCalendar();
			start.setTime(new Date());
			start.set(GregorianCalendar.SECOND, 0);
			GregorianCalendar end = new GregorianCalendar();
			end.setTime(new Date(System.currentTimeMillis() + 15 * 60000));
			end.set(GregorianCalendar.SECOND, 0);
			DatebookRecord dbr = new DatebookRecord(start.getTime(), end.getTime(), "Test of pilot-links new java bindings", "");
			//hexdump(dbr.getBuffer());
			//System.out.println("Size: "+dbr.getSize());

			//pl.writeRecord(dbh,dbr);
			pl.closeDB(dbh);

		} catch (PilotLinkException e) {
			e.printStackTrace();
		}

		try {
			System.out.println("\n\n------------------------------------\nNOW reading in Memos...");
			//Now reading MEMOS
			int db2 = pl.openDB("MemoDB");
			if (db2 < 0) {
				System.out.println("ERROR! " + db2);
				System.exit(1);
			}
			for (int i = 0; i < pl.getRecordCount(db2); i++) {
				Record r = pl.getRecordByIndex(db2, i);
				MemoRecord mr = new MemoRecord(r);
				System.out.println("Memo " + i + ": " + mr.getText());
			}

			MemoRecord newMemo = new MemoRecord("The new Java-Bindings work fine");
			pl.writeRecord(db2, newMemo);

			pl.closeDB(db2);
		} catch (PilotLinkException e) {
			e.printStackTrace();
		}
		try {
			System.out.println("Creating new database 'testDB2'...");
			int db3 = pl.createDB("testDB2", "RMSd", "Me");
			MemoRecord memo = new MemoRecord("new database");
			pl.writeRecord(db3, memo);
			pl.closeDB(db3);
		} catch (DatabaseExistsException e) {
			try {
				System.out.println("Database exists, deleting database 'testDB2'...");
				pl.deleteDB("testDB2");
			} catch (Exception f) {
				f.printStackTrace();
			}
		} catch (PilotLinkException e) {
			e.printStackTrace();
		}
		try {
			pl.endSync();
		} catch (PilotLinkException e) {
			e.printStackTrace();
		}
		pl.close();
		//done
		frame.dispose();
		System.exit(0);
	}
 

	/**
	 *  Description of the Method
	 *
	 *@param  arr  Description of the Parameter
	 */
	public static void hexdump(byte[] arr) {

		for (int i = 0; i < arr.length; ) {
			String chars = "";
			String l = "" + Integer.toHexString(i);
			while (l.length() < 4) {
				l = "0" + l;
			}
			System.out.print(l + ":  ");
			for (int j = 0; j < 16 && i < arr.length; j++, i++) {

				l = Integer.toHexString(arr[i]);
				if (arr[i] < 0) {
					l = l.substring(l.length() - 2);
				}
				while (l.length() < 2) {
					l = "0" + l;
				}
				System.out.print(l + " ");
				if ((arr[i] >= '0' && arr[i] <= 'z') || (arr[i] == ' ')) {
					chars += (char) arr[i];
				} else if (arr[i] == (byte) '\ufffd') {
					chars += "\ufffd";
				} else {
					chars += ".";
				}

			}
			System.out.println("   " + chars);
		}
	}

}