2 * This file is part of NeverNote
\r
3 * Copyright 2009 Randy Baumgarte
\r
5 * This file may be licensed under the terms of of the
\r
6 * GNU General Public License Version 2 (the ``GPL'').
\r
8 * Software distributed under the License is distributed
\r
9 * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
\r
10 * express or implied. See the GPL for the specific language
\r
11 * governing rights and limitations.
\r
13 * You should have received a copy of the GPL along with this
\r
14 * program. If not, go to http://www.gnu.org/licenses/gpl.html
\r
15 * or write to the Free Software Foundation, Inc.,
\r
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
\r
20 package cx.fbn.nevernote.xml;
\r
22 import java.util.ArrayList;
\r
23 import java.util.HashMap;
\r
24 import java.util.List;
\r
25 import java.util.Random;
\r
27 import com.evernote.edam.type.Data;
\r
28 import com.evernote.edam.type.Note;
\r
29 import com.evernote.edam.type.NoteAttributes;
\r
30 import com.evernote.edam.type.Notebook;
\r
31 import com.evernote.edam.type.Resource;
\r
32 import com.evernote.edam.type.ResourceAttributes;
\r
33 import com.evernote.edam.type.SavedSearch;
\r
34 import com.evernote.edam.type.Tag;
\r
35 import com.trolltech.qt.core.QByteArray;
\r
36 import com.trolltech.qt.core.QFile;
\r
37 import com.trolltech.qt.core.QIODevice;
\r
38 import com.trolltech.qt.xml.QXmlStreamAttributes;
\r
39 import com.trolltech.qt.xml.QXmlStreamReader;
\r
41 import cx.fbn.nevernote.sql.DatabaseConnection;
\r
42 import cx.fbn.nevernote.utilities.ApplicationLogger;
\r
44 public class ImportData {
\r
46 public int lastError;
\r
47 private String errorMessage;
\r
48 private String fileName;
\r
49 DatabaseConnection conn;
\r
50 QXmlStreamReader reader;
\r
52 private boolean noteIsDirty;
\r
53 private Notebook notebook;
\r
54 private boolean notebookIsDirty;
\r
55 private boolean notebookIsLocal;
\r
57 private boolean tagIsDirty;
\r
58 private final HashMap<String,Integer> titleColors;
\r
59 private SavedSearch search;
\r
60 private boolean searchIsDirty;
\r
61 public int highUpdateSequenceNumber;
\r
62 public long lastSequenceDate;
\r
63 private final ApplicationLogger logger;
\r
64 private final boolean backup;
\r
65 private String notebookGuid;
\r
67 public ImportData(DatabaseConnection c, boolean full) {
\r
68 logger = new ApplicationLogger("import.log");
\r
71 titleColors = new HashMap<String,Integer>();
\r
74 public void importData(String f) {
\r
80 QFile xmlFile = new QFile(fileName);
\r
81 if (!xmlFile.open(QIODevice.OpenModeFlag.ReadOnly)) {
\r
83 errorMessage = "Cannot open file.";
\r
86 reader = new QXmlStreamReader(xmlFile);
\r
87 while (!reader.atEnd()) {
\r
89 if (reader.hasError()) {
\r
90 errorMessage = reader.errorString();
\r
91 logger.log(logger.LOW, "************************* ERROR READING BACKUP " +reader.errorString());
\r
95 if (reader.name().equalsIgnoreCase("nevernote-export") && reader.isStartElement()) {
\r
96 QXmlStreamAttributes attributes = reader.attributes();
\r
97 String version = attributes.value("version");
\r
98 String type = attributes.value("exportType");
\r
99 String application = attributes.value("application");
\r
100 if (!version.equalsIgnoreCase("0.85") && !version.equalsIgnoreCase("0.86")) {
\r
102 errorMessage = "Unknown backup version = " +version;
\r
105 if (!application.equalsIgnoreCase("NeverNote")) {
\r
107 errorMessage = "This backup is from an unknown application = " +application;
\r
110 if (!type.equalsIgnoreCase("backup") && backup) {
\r
112 errorMessage = "This is an export file, not a backup file";
\r
115 if (type.equalsIgnoreCase("export") && backup) {
\r
117 errorMessage = "This is a backup file, not an export file";
\r
122 if (reader.name().equalsIgnoreCase("Synchronization") && reader.isStartElement() && backup) {
\r
123 processSynchronizationNode();
\r
124 conn.getSyncTable().setLastSequenceDate(lastSequenceDate);
\r
125 conn.getSyncTable().setUpdateSequenceNumber(highUpdateSequenceNumber);
\r
126 // Global.setSequenceDate(lastSequenceDate);
\r
127 // Global.setUpdateSequenceNumber(highUpdateSequenceNumber);
\r
129 if (reader.name().equalsIgnoreCase("note") && reader.isStartElement()) {
\r
132 conn.getNoteTable().addNote(note, noteIsDirty);
\r
134 note.setUpdateSequenceNum(0);
\r
135 if (notebookGuid != null)
\r
136 note.setNotebookGuid(notebookGuid);
\r
137 for (int i=0; i<note.getResourcesSize(); i++) {
\r
138 note.getResources().get(i).setUpdateSequenceNum(0);
\r
140 conn.getNoteTable().addNote(note, true);
\r
142 if (titleColors.containsKey(note.getGuid()))
\r
143 conn.getNoteTable().setNoteTitleColor(note.getGuid(), titleColors.get(note.getGuid()));
\r
145 if (reader.name().equalsIgnoreCase("notebook") && reader.isStartElement() && backup) {
\r
146 processNotebookNode();
\r
147 String existingGuid = conn.getNotebookTable().findNotebookByName(notebook.getName());
\r
148 if (existingGuid == null)
\r
149 conn.getNotebookTable().addNotebook(notebook, notebookIsDirty, notebookIsLocal);
\r
152 conn.getNotebookTable().updateNotebookGuid(existingGuid, notebook.getGuid());
\r
153 conn.getNotebookTable().updateNotebook(notebook, notebookIsDirty);
\r
157 if (reader.name().equalsIgnoreCase("tag") && reader.isStartElement() && backup) {
\r
159 String testGuid = conn.getTagTable().findTagByName(tag.getName());
\r
160 if (testGuid == null)
\r
161 conn.getTagTable().addTag(tag, tagIsDirty);
\r
163 conn.getTagTable().updateTagGuid(testGuid, tag.getGuid());
\r
164 conn.getTagTable().updateTag(tag,tagIsDirty);
\r
167 if (reader.name().equalsIgnoreCase("savedsearch") && reader.isStartElement() && backup) {
\r
168 processSavedSearchNode();
\r
169 conn.getSavedSearchTable().addSavedSearch(search, searchIsDirty);
\r
176 private void processNoteNode() {
\r
178 note.setResources(new ArrayList<Resource>());
\r
180 boolean atEnd = false;
\r
182 if (reader.isStartElement()) {
\r
183 if (reader.name().equalsIgnoreCase("Guid"))
\r
184 note.setGuid(textValue());
\r
186 Random random1 = new Random();
\r
187 String newGuid = "IMP" +new Integer(random1.nextInt(1000)).toString();
\r
188 newGuid = newGuid+"-"+new Integer(random1.nextInt(1000)).toString();
\r
189 newGuid = newGuid+"-"+new Integer(random1.nextInt(1000)).toString();
\r
190 newGuid = newGuid+"-"+new Integer(random1.nextInt(1000)).toString();
\r
191 note.setGuid(newGuid);
\r
193 if (reader.name().equalsIgnoreCase("UpdateSequenceNumber"))
\r
194 note.setUpdateSequenceNum(intValue());
\r
195 if (reader.name().equalsIgnoreCase("Title"))
\r
196 note.setTitle(textValue());
\r
197 if (reader.name().equalsIgnoreCase("Created"))
\r
198 note.setCreated(longValue());
\r
199 if (reader.name().equalsIgnoreCase("Updated"))
\r
200 note.setUpdated(longValue());
\r
201 if (reader.name().equalsIgnoreCase("Deleted"))
\r
202 note.setDeleted(longValue());
\r
203 if (reader.name().equalsIgnoreCase("Active"))
\r
204 note.setActive(booleanValue());
\r
205 if (reader.name().equalsIgnoreCase("NotebookGuid") && backup)
\r
206 note.setNotebookGuid(textValue());
\r
207 if (reader.name().equalsIgnoreCase("Content"))
\r
208 note.setContent(textValue());
\r
209 if (reader.name().equalsIgnoreCase("NoteTags") && backup)
\r
210 note.setTagGuids(processNoteTagList());
\r
211 if (reader.name().equalsIgnoreCase("NoteAttributes"))
\r
212 note.setAttributes(processNoteAttributes());
\r
213 if (reader.name().equalsIgnoreCase("NoteResource"))
\r
214 note.getResources().add(processResource());
\r
215 if (reader.name().equalsIgnoreCase("Dirty")) {
\r
216 if (booleanValue())
\r
219 if (reader.name().equalsIgnoreCase("TitleColor"))
\r
220 titleColors.put(note.getGuid(), intValue());
\r
223 if (reader.name().equalsIgnoreCase("note") && reader.isEndElement())
\r
230 private Resource processResource() {
\r
231 Resource resource = new Resource();
\r
232 boolean atEnd = false;
\r
233 boolean isDirty = false;
\r
235 if (reader.isStartElement()) {
\r
236 if (reader.name().equalsIgnoreCase("Guid"))
\r
237 resource.setGuid(textValue());
\r
239 Random random1 = new Random();
\r
240 String newGuid = "IMP" +new Integer(random1.nextInt(1000)).toString();
\r
241 newGuid = newGuid+"-"+new Integer(random1.nextInt(1000)).toString();
\r
242 newGuid = newGuid+"-"+new Integer(random1.nextInt(1000)).toString();
\r
243 newGuid = newGuid+"-"+new Integer(random1.nextInt(1000)).toString();
\r
244 resource.setGuid(newGuid);
\r
246 if (reader.name().equalsIgnoreCase("NoteGuid"))
\r
247 resource.setNoteGuid(textValue());
\r
248 if (reader.name().equalsIgnoreCase("UpdateSequenceNumber"))
\r
249 resource.setUpdateSequenceNum(intValue());
\r
250 if (reader.name().equalsIgnoreCase("Active"))
\r
251 resource.setActive(booleanValue());
\r
252 if (reader.name().equalsIgnoreCase("Mime"))
\r
253 resource.setMime(textValue());
\r
254 if (reader.name().equalsIgnoreCase("Duration"))
\r
255 resource.setDuration(shortValue());
\r
256 if (reader.name().equalsIgnoreCase("Height"))
\r
257 resource.setHeight(shortValue());
\r
258 if (reader.name().equalsIgnoreCase("Width"))
\r
259 resource.setWidth(shortValue());
\r
260 if (reader.name().equalsIgnoreCase("dirty"))
\r
261 isDirty = booleanValue();
\r
262 if (reader.name().equalsIgnoreCase("Data"))
\r
263 resource.setData(processData("Data"));
\r
264 if (reader.name().equalsIgnoreCase("AlternateData"))
\r
265 resource.setAlternateData(processData("AlternateData"));
\r
266 if (reader.name().equalsIgnoreCase("RecognitionData"))
\r
267 resource.setRecognition(processData("NoteResourceAttribute"));
\r
268 if (reader.name().equalsIgnoreCase("NoteResourceAttribute"))
\r
269 resource.setAttributes(processResourceAttributes());
\r
272 if (reader.name().equalsIgnoreCase("noteresource") && reader.isEndElement())
\r
276 conn.getNoteTable().noteResourceTable.saveNoteResource(resource, isDirty);
\r
282 private Data processData(String nodeName) {
\r
283 Data data = new Data();
\r
284 boolean atEnd = false;
\r
286 if (reader.isStartElement()) {
\r
287 if (reader.name().equalsIgnoreCase("Size"))
\r
288 data.setSize(intValue());
\r
289 if (reader.name().equalsIgnoreCase("Body")) {
\r
290 byte[] b = textValue().getBytes(); // data binary
\r
291 QByteArray hexData = new QByteArray(b);
\r
292 QByteArray binData = new QByteArray(QByteArray.fromHex(hexData));
\r
293 data.setBody(binData.toByteArray());
\r
295 if (reader.name().equalsIgnoreCase("BodyHash")) {
\r
296 byte[] b = textValue().getBytes(); // data binary
\r
297 QByteArray hexData = new QByteArray(b);
\r
298 QByteArray binData = new QByteArray(QByteArray.fromHex(hexData));
\r
299 data.setBodyHash(binData.toByteArray());
\r
303 if (reader.name().equalsIgnoreCase("data") && reader.isEndElement())
\r
307 if (reader.name().equalsIgnoreCase(nodeName) && reader.isEndElement())
\r
313 private ResourceAttributes processResourceAttributes() {
\r
314 ResourceAttributes attributes = new ResourceAttributes();
\r
315 boolean atEnd = false;
\r
317 if (reader.isStartElement()) {
\r
318 if (reader.name().equalsIgnoreCase("CameraMake"))
\r
319 attributes.setCameraMake(textValue());
\r
320 if (reader.name().equalsIgnoreCase("CameraModel"))
\r
321 attributes.setCameraModel(textValue());
\r
322 if (reader.name().equalsIgnoreCase("FileName"))
\r
323 attributes.setFileName(textValue());
\r
324 if (reader.name().equalsIgnoreCase("RecoType"))
\r
325 attributes.setRecoType(textValue());
\r
326 if (reader.name().equalsIgnoreCase("CameraModel"))
\r
327 attributes.setCameraMake(textValue());
\r
328 if (reader.name().equalsIgnoreCase("SourceURL"))
\r
329 attributes.setSourceURL(textValue());
\r
330 if (reader.name().equalsIgnoreCase("Altitude"))
\r
331 attributes.setAltitude(doubleValue());
\r
332 if (reader.name().equalsIgnoreCase("Longitude"))
\r
333 attributes.setLongitude(doubleValue());
\r
334 if (reader.name().equalsIgnoreCase("Latitude"))
\r
335 attributes.setLatitude(doubleValue());
\r
336 if (reader.name().equalsIgnoreCase("Timestamp"))
\r
337 attributes.setTimestamp(longValue());
\r
338 if (reader.name().equalsIgnoreCase("Attachment"))
\r
339 attributes.setAttachment(booleanValue());
\r
340 if (reader.name().equalsIgnoreCase("ClientWillIndex"))
\r
341 attributes.setClientWillIndex(booleanValue());
\r
344 if (reader.name().equalsIgnoreCase("noteresourceattribute") && reader.isEndElement())
\r
352 private List<String> processNoteTagList() {
\r
353 List<String> guidList = new ArrayList<String>();
\r
356 boolean atEnd = false;
\r
358 if (reader.isStartElement()) {
\r
359 if (reader.name().equalsIgnoreCase("guid"))
\r
360 guidList.add(textValue());
\r
363 if (reader.name().equalsIgnoreCase("notetags") && reader.isEndElement())
\r
371 private NoteAttributes processNoteAttributes() {
\r
372 NoteAttributes attributes = new NoteAttributes();
\r
374 boolean atEnd = false;
\r
376 if (reader.isStartElement()) {
\r
377 if (reader.name().equalsIgnoreCase("Author"))
\r
378 attributes.setAuthor(textValue());
\r
379 if (reader.name().equalsIgnoreCase("SourceURL"))
\r
380 attributes.setSourceURL(textValue());
\r
381 if (reader.name().equalsIgnoreCase("Source"))
\r
382 attributes.setSource(textValue());
\r
383 if (reader.name().equalsIgnoreCase("SourceApplication"))
\r
384 attributes.setSourceApplication(textValue());
\r
385 if (reader.name().equalsIgnoreCase("Altitude"))
\r
386 attributes.setAltitude(doubleValue());
\r
387 if (reader.name().equalsIgnoreCase("Longitude"))
\r
388 attributes.setLongitude(doubleValue());
\r
389 if (reader.name().equalsIgnoreCase("Latitude"))
\r
390 attributes.setLatitude(doubleValue());
\r
391 if (reader.name().equalsIgnoreCase("SubjectDate"))
\r
392 attributes.setSubjectDate(longValue());
\r
395 if (reader.name().equalsIgnoreCase("noteattributes") && reader.isEndElement())
\r
403 private void processSynchronizationNode() {
\r
404 boolean atEnd = false;
\r
406 if (reader.isStartElement()) {
\r
407 if (reader.name().equalsIgnoreCase("UpdateSequenceNumber"))
\r
408 highUpdateSequenceNumber = intValue();
\r
409 if (reader.name().equalsIgnoreCase("LastSequenceDate"))
\r
410 lastSequenceDate = longValue();
\r
413 if (reader.name().equalsIgnoreCase("synchronization") && reader.isEndElement())
\r
419 private void processSavedSearchNode() {
\r
420 search = new SavedSearch();
\r
421 searchIsDirty = false;
\r
423 boolean atEnd = false;
\r
425 if (reader.isStartElement()) {
\r
426 if (reader.name().equalsIgnoreCase("Guid"))
\r
427 search.setGuid(textValue());
\r
428 if (reader.name().equalsIgnoreCase("Name"))
\r
429 search.setName(textValue());
\r
430 if (reader.name().equalsIgnoreCase("UpdateSequenceNumber"))
\r
431 search.setUpdateSequenceNum(intValue());
\r
432 if (reader.name().equalsIgnoreCase("Query"))
\r
433 search.setQuery(textValue());
\r
434 if (reader.name().equalsIgnoreCase("Dirty")) {
\r
435 if (booleanValue())
\r
436 searchIsDirty = true;
\r
440 if (reader.name().equalsIgnoreCase("savedsearch") && reader.isEndElement())
\r
447 private void processNotebookNode() {
\r
448 notebook = new Notebook();
\r
449 notebookIsDirty = false;
\r
450 notebookIsLocal = false;
\r
451 boolean atEnd = false;
\r
453 if (reader.isStartElement()) {
\r
454 if (reader.name().equalsIgnoreCase("Guid"))
\r
455 notebook.setGuid(textValue());
\r
456 if (reader.name().equalsIgnoreCase("Name"))
\r
457 notebook.setName(textValue());
\r
458 if (reader.name().equalsIgnoreCase("UpdateSequenceNumber"))
\r
459 notebook.setUpdateSequenceNum(intValue());
\r
460 if (reader.name().equalsIgnoreCase("ServiceCreated"))
\r
461 notebook.setServiceCreated(longValue());
\r
462 if (reader.name().equalsIgnoreCase("ServiceUpdated"))
\r
463 notebook.setServiceUpdated(longValue());
\r
464 if (reader.name().equalsIgnoreCase("DefaultNotebook")) {
\r
465 notebook.setDefaultNotebook(booleanValue());
\r
467 if (reader.name().equalsIgnoreCase("Dirty")) {
\r
468 if (booleanValue())
\r
469 notebookIsDirty = true;
\r
471 if (reader.name().equalsIgnoreCase("LocalNotebook")) {
\r
472 if (booleanValue())
\r
473 notebookIsLocal = true;
\r
477 if (reader.name().equalsIgnoreCase("notebook") && reader.isEndElement())
\r
485 private void processTagNode() {
\r
487 tagIsDirty = false;
\r
488 boolean atEnd = false;
\r
490 if (reader.isStartElement()) {
\r
491 if (reader.name().equalsIgnoreCase("Guid"))
\r
492 tag.setGuid(textValue());
\r
493 if (reader.name().equalsIgnoreCase("Name"))
\r
494 tag.setName(textValue());
\r
495 if (reader.name().equalsIgnoreCase("UpdateSequenceNumber"))
\r
496 tag.setUpdateSequenceNum(intValue());
\r
497 if (reader.name().equalsIgnoreCase("ParentGuid"))
\r
498 tag.setParentGuid(textValue());
\r
499 if (reader.name().equalsIgnoreCase("Dirty")) {
\r
500 if (booleanValue())
\r
505 if (reader.name().equalsIgnoreCase("tag") && reader.isEndElement())
\r
514 private String textValue() {
\r
515 return reader.readElementText();
\r
517 private int intValue() {
\r
518 return new Integer(textValue());
\r
520 private long longValue() {
\r
521 return new Long(textValue());
\r
523 private double doubleValue() {
\r
524 return new Double(textValue());
\r
526 private boolean booleanValue() {
\r
527 String value = textValue();
\r
528 if (value.equalsIgnoreCase("true"))
\r
533 private short shortValue() {
\r
534 return new Short(textValue());
\r
537 public void setNotebookGuid(String g) {
\r
541 public String getErrorMessage() {
\r
542 return errorMessage;
\r