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
66 public final boolean importTags = false;
\r
67 public final boolean importNotebooks = false;
\r
68 private final HashMap<String,String> tagMap;
\r
69 private final HashMap<String,String> noteMap;
\r
70 private final HashMap<String,String> notebookMap;
\r
72 public ImportData(DatabaseConnection c, boolean full) {
\r
73 logger = new ApplicationLogger("import.log");
\r
76 titleColors = new HashMap<String,Integer>();
\r
77 notebookMap = new HashMap<String,String>();
\r
78 tagMap = new HashMap<String,String>();
\r
79 noteMap = new HashMap<String,String>();
\r
82 public void importData(String f) {
\r
88 QFile xmlFile = new QFile(fileName);
\r
89 if (!xmlFile.open(QIODevice.OpenModeFlag.ReadOnly)) {
\r
91 errorMessage = "Cannot open file.";
\r
94 reader = new QXmlStreamReader(xmlFile);
\r
95 while (!reader.atEnd()) {
\r
97 if (reader.hasError()) {
\r
98 errorMessage = reader.errorString();
\r
99 logger.log(logger.LOW, "************************* ERROR READING BACKUP " +reader.errorString());
\r
103 if (reader.name().equalsIgnoreCase("nevernote-export") && reader.isStartElement()) {
\r
104 QXmlStreamAttributes attributes = reader.attributes();
\r
105 String version = attributes.value("version");
\r
106 String type = attributes.value("exportType");
\r
107 String application = attributes.value("application");
\r
108 if (!version.equalsIgnoreCase("0.85") && !version.equalsIgnoreCase("0.86")) {
\r
110 errorMessage = "Unknown backup version = " +version;
\r
113 if (!application.equalsIgnoreCase("NeverNote")) {
\r
115 errorMessage = "This backup is from an unknown application = " +application;
\r
118 if (!type.equalsIgnoreCase("backup") && backup) {
\r
120 errorMessage = "This is an export file, not a backup file";
\r
123 if (type.equalsIgnoreCase("export") && backup) {
\r
125 errorMessage = "This is a backup file, not an export file";
\r
130 if (reader.name().equalsIgnoreCase("Synchronization") && reader.isStartElement() && backup) {
\r
131 processSynchronizationNode();
\r
132 conn.getSyncTable().setLastSequenceDate(lastSequenceDate);
\r
133 conn.getSyncTable().setUpdateSequenceNumber(highUpdateSequenceNumber);
\r
134 // Global.setSequenceDate(lastSequenceDate);
\r
135 // Global.setUpdateSequenceNumber(highUpdateSequenceNumber);
\r
137 if (reader.name().equalsIgnoreCase("note") && reader.isStartElement()) {
\r
140 conn.getNoteTable().addNote(note, noteIsDirty);
\r
142 note.setUpdateSequenceNum(0);
\r
143 if (notebookGuid != null)
\r
144 note.setNotebookGuid(notebookGuid);
\r
145 for (int i=0; i<note.getResourcesSize(); i++) {
\r
146 note.getResources().get(i).setUpdateSequenceNum(0);
\r
148 conn.getNoteTable().addNote(note, true);
\r
150 if (titleColors.containsKey(note.getGuid()))
\r
151 conn.getNoteTable().setNoteTitleColor(note.getGuid(), titleColors.get(note.getGuid()));
\r
153 if (reader.name().equalsIgnoreCase("notebook") && reader.isStartElement() && (backup || importNotebooks)) {
\r
154 processNotebookNode();
\r
155 String existingGuid = conn.getNotebookTable().findNotebookByName(notebook.getName());
\r
156 if (existingGuid == null)
\r
157 conn.getNotebookTable().addNotebook(notebook, notebookIsDirty, notebookIsLocal);
\r
160 conn.getNotebookTable().updateNotebookGuid(existingGuid, notebook.getGuid());
\r
161 conn.getNotebookTable().updateNotebook(notebook, notebookIsDirty);
\r
165 if (reader.name().equalsIgnoreCase("tag") && reader.isStartElement() && (backup || importTags)) {
\r
167 String testGuid = conn.getTagTable().findTagByName(tag.getName());
\r
168 if (testGuid == null)
\r
169 conn.getTagTable().addTag(tag, tagIsDirty);
\r
171 conn.getTagTable().updateTagGuid(testGuid, tag.getGuid());
\r
172 conn.getTagTable().updateTag(tag,tagIsDirty);
\r
175 if (reader.name().equalsIgnoreCase("savedsearch") && reader.isStartElement() && backup) {
\r
176 processSavedSearchNode();
\r
177 conn.getSavedSearchTable().addSavedSearch(search, searchIsDirty);
\r
184 private void processNoteNode() {
\r
186 note.setResources(new ArrayList<Resource>());
\r
188 boolean atEnd = false;
\r
190 if (reader.isStartElement()) {
\r
191 if (reader.name().equalsIgnoreCase("Guid")) {
\r
192 note.setGuid(textValue());
\r
194 Random random1 = new Random();
\r
195 String newGuid = "IMP" +new Integer(random1.nextInt(1000)).toString();
\r
196 newGuid = newGuid+"-"+new Integer(random1.nextInt(1000)).toString();
\r
197 newGuid = newGuid+"-"+new Integer(random1.nextInt(1000)).toString();
\r
198 newGuid = newGuid+"-"+new Integer(random1.nextInt(1000)).toString();
\r
199 noteMap.put(note.getGuid(), newGuid);
\r
200 note.setGuid(newGuid);
\r
202 noteMap.put(note.getGuid(), note.getGuid());
\r
204 if (reader.name().equalsIgnoreCase("UpdateSequenceNumber"))
\r
205 note.setUpdateSequenceNum(intValue());
\r
206 if (reader.name().equalsIgnoreCase("Title"))
\r
207 note.setTitle(textValue());
\r
208 if (reader.name().equalsIgnoreCase("Created"))
\r
209 note.setCreated(longValue());
\r
210 if (reader.name().equalsIgnoreCase("Updated"))
\r
211 note.setUpdated(longValue());
\r
212 if (reader.name().equalsIgnoreCase("Deleted"))
\r
213 note.setDeleted(longValue());
\r
214 if (reader.name().equalsIgnoreCase("Active"))
\r
215 note.setActive(booleanValue());
\r
216 if (reader.name().equalsIgnoreCase("NotebookGuid") && (backup || importNotebooks))
\r
217 note.setNotebookGuid(textValue());
\r
218 if (reader.name().equalsIgnoreCase("Content"))
\r
219 note.setContent(textValue());
\r
220 if (reader.name().equalsIgnoreCase("NoteTags") && (backup || importTags))
\r
221 note.setTagGuids(processNoteTagList());
\r
222 if (reader.name().equalsIgnoreCase("NoteAttributes"))
\r
223 note.setAttributes(processNoteAttributes());
\r
224 if (reader.name().equalsIgnoreCase("NoteResource"))
\r
225 note.getResources().add(processResource());
\r
226 if (reader.name().equalsIgnoreCase("Dirty")) {
\r
227 if (booleanValue())
\r
230 if (reader.name().equalsIgnoreCase("TitleColor"))
\r
231 titleColors.put(note.getGuid(), intValue());
\r
234 if (reader.name().equalsIgnoreCase("note") && reader.isEndElement())
\r
241 private Resource processResource() {
\r
242 Resource resource = new Resource();
\r
243 boolean atEnd = false;
\r
244 boolean isDirty = false;
\r
246 if (reader.isStartElement()) {
\r
247 if (reader.name().equalsIgnoreCase("Guid"))
\r
248 resource.setGuid(textValue());
\r
250 Random random1 = new Random();
\r
251 String newGuid = "IMP" +new Integer(random1.nextInt(1000)).toString();
\r
252 newGuid = newGuid+"-"+new Integer(random1.nextInt(1000)).toString();
\r
253 newGuid = newGuid+"-"+new Integer(random1.nextInt(1000)).toString();
\r
254 newGuid = newGuid+"-"+new Integer(random1.nextInt(1000)).toString();
\r
255 resource.setGuid(newGuid);
\r
257 if (reader.name().equalsIgnoreCase("NoteGuid"))
\r
258 resource.setNoteGuid(noteMap.get(textValue()));
\r
259 if (reader.name().equalsIgnoreCase("UpdateSequenceNumber"))
\r
260 resource.setUpdateSequenceNum(intValue());
\r
261 if (reader.name().equalsIgnoreCase("Active"))
\r
262 resource.setActive(booleanValue());
\r
263 if (reader.name().equalsIgnoreCase("Mime"))
\r
264 resource.setMime(textValue());
\r
265 if (reader.name().equalsIgnoreCase("Duration"))
\r
266 resource.setDuration(shortValue());
\r
267 if (reader.name().equalsIgnoreCase("Height"))
\r
268 resource.setHeight(shortValue());
\r
269 if (reader.name().equalsIgnoreCase("Width"))
\r
270 resource.setWidth(shortValue());
\r
271 if (reader.name().equalsIgnoreCase("dirty"))
\r
272 isDirty = booleanValue();
\r
273 if (reader.name().equalsIgnoreCase("Data"))
\r
274 resource.setData(processData("Data"));
\r
275 if (reader.name().equalsIgnoreCase("AlternateData"))
\r
276 resource.setAlternateData(processData("AlternateData"));
\r
277 if (reader.name().equalsIgnoreCase("RecognitionData"))
\r
278 resource.setRecognition(processData("NoteResourceAttribute"));
\r
279 if (reader.name().equalsIgnoreCase("NoteResourceAttribute"))
\r
280 resource.setAttributes(processResourceAttributes());
\r
283 if (reader.name().equalsIgnoreCase("noteresource") && reader.isEndElement())
\r
287 conn.getNoteTable().noteResourceTable.saveNoteResource(resource, isDirty);
\r
293 private Data processData(String nodeName) {
\r
294 Data data = new Data();
\r
295 boolean atEnd = false;
\r
297 if (reader.isStartElement()) {
\r
298 if (reader.name().equalsIgnoreCase("Size"))
\r
299 data.setSize(intValue());
\r
300 if (reader.name().equalsIgnoreCase("Body")) {
\r
301 byte[] b = textValue().getBytes(); // data binary
\r
302 QByteArray hexData = new QByteArray(b);
\r
303 QByteArray binData = new QByteArray(QByteArray.fromHex(hexData));
\r
304 data.setBody(binData.toByteArray());
\r
306 if (reader.name().equalsIgnoreCase("BodyHash")) {
\r
307 byte[] b = textValue().getBytes(); // data binary
\r
308 QByteArray hexData = new QByteArray(b);
\r
309 QByteArray binData = new QByteArray(QByteArray.fromHex(hexData));
\r
310 data.setBodyHash(binData.toByteArray());
\r
314 if (reader.name().equalsIgnoreCase("data") && reader.isEndElement())
\r
318 if (reader.name().equalsIgnoreCase(nodeName) && reader.isEndElement())
\r
324 private ResourceAttributes processResourceAttributes() {
\r
325 ResourceAttributes attributes = new ResourceAttributes();
\r
326 boolean atEnd = false;
\r
328 if (reader.isStartElement()) {
\r
329 if (reader.name().equalsIgnoreCase("CameraMake"))
\r
330 attributes.setCameraMake(textValue());
\r
331 if (reader.name().equalsIgnoreCase("CameraModel"))
\r
332 attributes.setCameraModel(textValue());
\r
333 if (reader.name().equalsIgnoreCase("FileName"))
\r
334 attributes.setFileName(textValue());
\r
335 if (reader.name().equalsIgnoreCase("RecoType"))
\r
336 attributes.setRecoType(textValue());
\r
337 if (reader.name().equalsIgnoreCase("CameraModel"))
\r
338 attributes.setCameraMake(textValue());
\r
339 if (reader.name().equalsIgnoreCase("SourceURL"))
\r
340 attributes.setSourceURL(textValue());
\r
341 if (reader.name().equalsIgnoreCase("Altitude"))
\r
342 attributes.setAltitude(doubleValue());
\r
343 if (reader.name().equalsIgnoreCase("Longitude"))
\r
344 attributes.setLongitude(doubleValue());
\r
345 if (reader.name().equalsIgnoreCase("Latitude"))
\r
346 attributes.setLatitude(doubleValue());
\r
347 if (reader.name().equalsIgnoreCase("Timestamp"))
\r
348 attributes.setTimestamp(longValue());
\r
349 if (reader.name().equalsIgnoreCase("Attachment"))
\r
350 attributes.setAttachment(booleanValue());
\r
351 if (reader.name().equalsIgnoreCase("ClientWillIndex"))
\r
352 attributes.setClientWillIndex(booleanValue());
\r
355 if (reader.name().equalsIgnoreCase("noteresourceattribute") && reader.isEndElement())
\r
363 private List<String> processNoteTagList() {
\r
364 List<String> guidList = new ArrayList<String>();
\r
367 boolean atEnd = false;
\r
369 if (reader.isStartElement()) {
\r
370 if (reader.name().equalsIgnoreCase("guid"))
\r
371 guidList.add(textValue());
\r
374 if (reader.name().equalsIgnoreCase("notetags") && reader.isEndElement())
\r
382 private NoteAttributes processNoteAttributes() {
\r
383 NoteAttributes attributes = new NoteAttributes();
\r
385 boolean atEnd = false;
\r
387 if (reader.isStartElement()) {
\r
388 if (reader.name().equalsIgnoreCase("Author"))
\r
389 attributes.setAuthor(textValue());
\r
390 if (reader.name().equalsIgnoreCase("SourceURL"))
\r
391 attributes.setSourceURL(textValue());
\r
392 if (reader.name().equalsIgnoreCase("Source"))
\r
393 attributes.setSource(textValue());
\r
394 if (reader.name().equalsIgnoreCase("SourceApplication"))
\r
395 attributes.setSourceApplication(textValue());
\r
396 if (reader.name().equalsIgnoreCase("Altitude"))
\r
397 attributes.setAltitude(doubleValue());
\r
398 if (reader.name().equalsIgnoreCase("Longitude"))
\r
399 attributes.setLongitude(doubleValue());
\r
400 if (reader.name().equalsIgnoreCase("Latitude"))
\r
401 attributes.setLatitude(doubleValue());
\r
402 if (reader.name().equalsIgnoreCase("SubjectDate"))
\r
403 attributes.setSubjectDate(longValue());
\r
406 if (reader.name().equalsIgnoreCase("noteattributes") && reader.isEndElement())
\r
414 private void processSynchronizationNode() {
\r
415 boolean atEnd = false;
\r
417 if (reader.isStartElement()) {
\r
418 if (reader.name().equalsIgnoreCase("UpdateSequenceNumber"))
\r
419 highUpdateSequenceNumber = intValue();
\r
420 if (reader.name().equalsIgnoreCase("LastSequenceDate"))
\r
421 lastSequenceDate = longValue();
\r
424 if (reader.name().equalsIgnoreCase("synchronization") && reader.isEndElement())
\r
430 private void processSavedSearchNode() {
\r
431 search = new SavedSearch();
\r
432 searchIsDirty = false;
\r
434 boolean atEnd = false;
\r
436 if (reader.isStartElement()) {
\r
437 if (reader.name().equalsIgnoreCase("Guid"))
\r
438 search.setGuid(textValue());
\r
439 if (reader.name().equalsIgnoreCase("Name"))
\r
440 search.setName(textValue());
\r
441 if (reader.name().equalsIgnoreCase("UpdateSequenceNumber"))
\r
442 search.setUpdateSequenceNum(intValue());
\r
443 if (reader.name().equalsIgnoreCase("Query"))
\r
444 search.setQuery(textValue());
\r
445 if (reader.name().equalsIgnoreCase("Dirty")) {
\r
446 if (booleanValue())
\r
447 searchIsDirty = true;
\r
451 if (reader.name().equalsIgnoreCase("savedsearch") && reader.isEndElement())
\r
458 private void processNotebookNode() {
\r
459 notebook = new Notebook();
\r
460 notebookIsDirty = false;
\r
461 notebookIsLocal = false;
\r
462 boolean atEnd = false;
\r
464 if (reader.isStartElement()) {
\r
465 if (reader.name().equalsIgnoreCase("Guid"))
\r
466 notebook.setGuid(textValue());
\r
467 if (reader.name().equalsIgnoreCase("Name"))
\r
468 notebook.setName(textValue());
\r
469 if (reader.name().equalsIgnoreCase("UpdateSequenceNumber"))
\r
470 notebook.setUpdateSequenceNum(intValue());
\r
471 if (reader.name().equalsIgnoreCase("ServiceCreated"))
\r
472 notebook.setServiceCreated(longValue());
\r
473 if (reader.name().equalsIgnoreCase("ServiceUpdated"))
\r
474 notebook.setServiceUpdated(longValue());
\r
475 if (reader.name().equalsIgnoreCase("DefaultNotebook")) {
\r
476 notebook.setDefaultNotebook(booleanValue());
\r
478 if (reader.name().equalsIgnoreCase("Dirty")) {
\r
479 if (booleanValue())
\r
480 notebookIsDirty = true;
\r
482 if (reader.name().equalsIgnoreCase("LocalNotebook")) {
\r
483 if (booleanValue())
\r
484 notebookIsLocal = true;
\r
488 if (reader.name().equalsIgnoreCase("notebook") && reader.isEndElement())
\r
496 private void processTagNode() {
\r
498 tagIsDirty = false;
\r
499 boolean atEnd = false;
\r
501 if (reader.isStartElement()) {
\r
502 if (reader.name().equalsIgnoreCase("Guid"))
\r
503 tag.setGuid(textValue());
\r
504 if (reader.name().equalsIgnoreCase("Name"))
\r
505 tag.setName(textValue());
\r
506 if (reader.name().equalsIgnoreCase("UpdateSequenceNumber"))
\r
507 tag.setUpdateSequenceNum(intValue());
\r
508 if (reader.name().equalsIgnoreCase("ParentGuid"))
\r
509 tag.setParentGuid(textValue());
\r
510 if (reader.name().equalsIgnoreCase("Dirty")) {
\r
511 if (booleanValue())
\r
516 if (reader.name().equalsIgnoreCase("tag") && reader.isEndElement())
\r
525 private String textValue() {
\r
526 return reader.readElementText();
\r
528 private int intValue() {
\r
529 return new Integer(textValue());
\r
531 private long longValue() {
\r
532 return new Long(textValue());
\r
534 private double doubleValue() {
\r
535 return new Double(textValue());
\r
537 private boolean booleanValue() {
\r
538 String value = textValue();
\r
539 if (value.equalsIgnoreCase("true"))
\r
544 private short shortValue() {
\r
545 return new Short(textValue());
\r
548 public void setNotebookGuid(String g) {
\r
552 public String getErrorMessage() {
\r
553 return errorMessage;
\r