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
69 public ImportData(DatabaseConnection c, boolean full) {
\r
70 logger = new ApplicationLogger("import.log");
\r
73 titleColors = new HashMap<String,Integer>();
\r
76 public void importData(String f) {
\r
82 QFile xmlFile = new QFile(fileName);
\r
83 if (!xmlFile.open(QIODevice.OpenModeFlag.ReadOnly)) {
\r
85 errorMessage = "Cannot open file.";
\r
88 reader = new QXmlStreamReader(xmlFile);
\r
89 while (!reader.atEnd()) {
\r
91 if (reader.hasError()) {
\r
92 errorMessage = reader.errorString();
\r
93 logger.log(logger.LOW, "************************* ERROR READING BACKUP " +reader.errorString());
\r
97 if (reader.name().equalsIgnoreCase("nevernote-export") && reader.isStartElement()) {
\r
98 QXmlStreamAttributes attributes = reader.attributes();
\r
99 String version = attributes.value("version");
\r
100 String type = attributes.value("exportType");
\r
101 String application = attributes.value("application");
\r
102 if (!version.equalsIgnoreCase("0.85") && !version.equalsIgnoreCase("0.86")) {
\r
104 errorMessage = "Unknown backup version = " +version;
\r
107 if (!application.equalsIgnoreCase("NeverNote")) {
\r
109 errorMessage = "This backup is from an unknown application = " +application;
\r
112 if (!type.equalsIgnoreCase("backup") && backup) {
\r
114 errorMessage = "This is an export file, not a backup file";
\r
117 if (type.equalsIgnoreCase("export") && backup) {
\r
119 errorMessage = "This is a backup file, not an export file";
\r
124 if (reader.name().equalsIgnoreCase("Synchronization") && reader.isStartElement() && backup) {
\r
125 processSynchronizationNode();
\r
126 conn.getSyncTable().setLastSequenceDate(lastSequenceDate);
\r
127 conn.getSyncTable().setUpdateSequenceNumber(highUpdateSequenceNumber);
\r
128 // Global.setSequenceDate(lastSequenceDate);
\r
129 // Global.setUpdateSequenceNumber(highUpdateSequenceNumber);
\r
131 if (reader.name().equalsIgnoreCase("note") && reader.isStartElement()) {
\r
134 conn.getNoteTable().addNote(note, noteIsDirty);
\r
136 note.setUpdateSequenceNum(0);
\r
137 if (notebookGuid != null)
\r
138 note.setNotebookGuid(notebookGuid);
\r
139 for (int i=0; i<note.getResourcesSize(); i++) {
\r
140 note.getResources().get(i).setUpdateSequenceNum(0);
\r
142 conn.getNoteTable().addNote(note, true);
\r
144 if (titleColors.containsKey(note.getGuid()))
\r
145 conn.getNoteTable().setNoteTitleColor(note.getGuid(), titleColors.get(note.getGuid()));
\r
147 if (reader.name().equalsIgnoreCase("notebook") && reader.isStartElement() && (backup || importNotebooks)) {
\r
148 processNotebookNode();
\r
149 String existingGuid = conn.getNotebookTable().findNotebookByName(notebook.getName());
\r
150 if (existingGuid == null)
\r
151 conn.getNotebookTable().addNotebook(notebook, notebookIsDirty, notebookIsLocal);
\r
154 conn.getNotebookTable().updateNotebookGuid(existingGuid, notebook.getGuid());
\r
155 conn.getNotebookTable().updateNotebook(notebook, notebookIsDirty);
\r
159 if (reader.name().equalsIgnoreCase("tag") && reader.isStartElement() && (backup || importTags)) {
\r
161 String testGuid = conn.getTagTable().findTagByName(tag.getName());
\r
162 if (testGuid == null)
\r
163 conn.getTagTable().addTag(tag, tagIsDirty);
\r
165 conn.getTagTable().updateTagGuid(testGuid, tag.getGuid());
\r
166 conn.getTagTable().updateTag(tag,tagIsDirty);
\r
169 if (reader.name().equalsIgnoreCase("savedsearch") && reader.isStartElement() && backup) {
\r
170 processSavedSearchNode();
\r
171 conn.getSavedSearchTable().addSavedSearch(search, searchIsDirty);
\r
178 private void processNoteNode() {
\r
180 note.setResources(new ArrayList<Resource>());
\r
182 boolean atEnd = false;
\r
184 if (reader.isStartElement()) {
\r
185 if (reader.name().equalsIgnoreCase("Guid"))
\r
186 note.setGuid(textValue());
\r
188 Random random1 = new Random();
\r
189 String newGuid = "IMP" +new Integer(random1.nextInt(1000)).toString();
\r
190 newGuid = newGuid+"-"+new Integer(random1.nextInt(1000)).toString();
\r
191 newGuid = newGuid+"-"+new Integer(random1.nextInt(1000)).toString();
\r
192 newGuid = newGuid+"-"+new Integer(random1.nextInt(1000)).toString();
\r
193 note.setGuid(newGuid);
\r
195 if (reader.name().equalsIgnoreCase("UpdateSequenceNumber"))
\r
196 note.setUpdateSequenceNum(intValue());
\r
197 if (reader.name().equalsIgnoreCase("Title"))
\r
198 note.setTitle(textValue());
\r
199 if (reader.name().equalsIgnoreCase("Created"))
\r
200 note.setCreated(longValue());
\r
201 if (reader.name().equalsIgnoreCase("Updated"))
\r
202 note.setUpdated(longValue());
\r
203 if (reader.name().equalsIgnoreCase("Deleted"))
\r
204 note.setDeleted(longValue());
\r
205 if (reader.name().equalsIgnoreCase("Active"))
\r
206 note.setActive(booleanValue());
\r
207 if (reader.name().equalsIgnoreCase("NotebookGuid") && (backup || importNotebooks))
\r
208 note.setNotebookGuid(textValue());
\r
209 if (reader.name().equalsIgnoreCase("Content"))
\r
210 note.setContent(textValue());
\r
211 if (reader.name().equalsIgnoreCase("NoteTags") && (backup || importTags))
\r
212 note.setTagGuids(processNoteTagList());
\r
213 if (reader.name().equalsIgnoreCase("NoteAttributes"))
\r
214 note.setAttributes(processNoteAttributes());
\r
215 if (reader.name().equalsIgnoreCase("NoteResource"))
\r
216 note.getResources().add(processResource());
\r
217 if (reader.name().equalsIgnoreCase("Dirty")) {
\r
218 if (booleanValue())
\r
221 if (reader.name().equalsIgnoreCase("TitleColor"))
\r
222 titleColors.put(note.getGuid(), intValue());
\r
225 if (reader.name().equalsIgnoreCase("note") && reader.isEndElement())
\r
232 private Resource processResource() {
\r
233 Resource resource = new Resource();
\r
234 boolean atEnd = false;
\r
235 boolean isDirty = false;
\r
237 if (reader.isStartElement()) {
\r
238 if (reader.name().equalsIgnoreCase("Guid"))
\r
239 resource.setGuid(textValue());
\r
241 Random random1 = new Random();
\r
242 String newGuid = "IMP" +new Integer(random1.nextInt(1000)).toString();
\r
243 newGuid = newGuid+"-"+new Integer(random1.nextInt(1000)).toString();
\r
244 newGuid = newGuid+"-"+new Integer(random1.nextInt(1000)).toString();
\r
245 newGuid = newGuid+"-"+new Integer(random1.nextInt(1000)).toString();
\r
246 resource.setGuid(newGuid);
\r
248 if (reader.name().equalsIgnoreCase("NoteGuid"))
\r
249 resource.setNoteGuid(textValue());
\r
250 if (reader.name().equalsIgnoreCase("UpdateSequenceNumber"))
\r
251 resource.setUpdateSequenceNum(intValue());
\r
252 if (reader.name().equalsIgnoreCase("Active"))
\r
253 resource.setActive(booleanValue());
\r
254 if (reader.name().equalsIgnoreCase("Mime"))
\r
255 resource.setMime(textValue());
\r
256 if (reader.name().equalsIgnoreCase("Duration"))
\r
257 resource.setDuration(shortValue());
\r
258 if (reader.name().equalsIgnoreCase("Height"))
\r
259 resource.setHeight(shortValue());
\r
260 if (reader.name().equalsIgnoreCase("Width"))
\r
261 resource.setWidth(shortValue());
\r
262 if (reader.name().equalsIgnoreCase("dirty"))
\r
263 isDirty = booleanValue();
\r
264 if (reader.name().equalsIgnoreCase("Data"))
\r
265 resource.setData(processData("Data"));
\r
266 if (reader.name().equalsIgnoreCase("AlternateData"))
\r
267 resource.setAlternateData(processData("AlternateData"));
\r
268 if (reader.name().equalsIgnoreCase("RecognitionData"))
\r
269 resource.setRecognition(processData("NoteResourceAttribute"));
\r
270 if (reader.name().equalsIgnoreCase("NoteResourceAttribute"))
\r
271 resource.setAttributes(processResourceAttributes());
\r
274 if (reader.name().equalsIgnoreCase("noteresource") && reader.isEndElement())
\r
278 conn.getNoteTable().noteResourceTable.saveNoteResource(resource, isDirty);
\r
284 private Data processData(String nodeName) {
\r
285 Data data = new Data();
\r
286 boolean atEnd = false;
\r
288 if (reader.isStartElement()) {
\r
289 if (reader.name().equalsIgnoreCase("Size"))
\r
290 data.setSize(intValue());
\r
291 if (reader.name().equalsIgnoreCase("Body")) {
\r
292 byte[] b = textValue().getBytes(); // data binary
\r
293 QByteArray hexData = new QByteArray(b);
\r
294 QByteArray binData = new QByteArray(QByteArray.fromHex(hexData));
\r
295 data.setBody(binData.toByteArray());
\r
297 if (reader.name().equalsIgnoreCase("BodyHash")) {
\r
298 byte[] b = textValue().getBytes(); // data binary
\r
299 QByteArray hexData = new QByteArray(b);
\r
300 QByteArray binData = new QByteArray(QByteArray.fromHex(hexData));
\r
301 data.setBodyHash(binData.toByteArray());
\r
305 if (reader.name().equalsIgnoreCase("data") && reader.isEndElement())
\r
309 if (reader.name().equalsIgnoreCase(nodeName) && reader.isEndElement())
\r
315 private ResourceAttributes processResourceAttributes() {
\r
316 ResourceAttributes attributes = new ResourceAttributes();
\r
317 boolean atEnd = false;
\r
319 if (reader.isStartElement()) {
\r
320 if (reader.name().equalsIgnoreCase("CameraMake"))
\r
321 attributes.setCameraMake(textValue());
\r
322 if (reader.name().equalsIgnoreCase("CameraModel"))
\r
323 attributes.setCameraModel(textValue());
\r
324 if (reader.name().equalsIgnoreCase("FileName"))
\r
325 attributes.setFileName(textValue());
\r
326 if (reader.name().equalsIgnoreCase("RecoType"))
\r
327 attributes.setRecoType(textValue());
\r
328 if (reader.name().equalsIgnoreCase("CameraModel"))
\r
329 attributes.setCameraMake(textValue());
\r
330 if (reader.name().equalsIgnoreCase("SourceURL"))
\r
331 attributes.setSourceURL(textValue());
\r
332 if (reader.name().equalsIgnoreCase("Altitude"))
\r
333 attributes.setAltitude(doubleValue());
\r
334 if (reader.name().equalsIgnoreCase("Longitude"))
\r
335 attributes.setLongitude(doubleValue());
\r
336 if (reader.name().equalsIgnoreCase("Latitude"))
\r
337 attributes.setLatitude(doubleValue());
\r
338 if (reader.name().equalsIgnoreCase("Timestamp"))
\r
339 attributes.setTimestamp(longValue());
\r
340 if (reader.name().equalsIgnoreCase("Attachment"))
\r
341 attributes.setAttachment(booleanValue());
\r
342 if (reader.name().equalsIgnoreCase("ClientWillIndex"))
\r
343 attributes.setClientWillIndex(booleanValue());
\r
346 if (reader.name().equalsIgnoreCase("noteresourceattribute") && reader.isEndElement())
\r
354 private List<String> processNoteTagList() {
\r
355 List<String> guidList = new ArrayList<String>();
\r
358 boolean atEnd = false;
\r
360 if (reader.isStartElement()) {
\r
361 if (reader.name().equalsIgnoreCase("guid"))
\r
362 guidList.add(textValue());
\r
365 if (reader.name().equalsIgnoreCase("notetags") && reader.isEndElement())
\r
373 private NoteAttributes processNoteAttributes() {
\r
374 NoteAttributes attributes = new NoteAttributes();
\r
376 boolean atEnd = false;
\r
378 if (reader.isStartElement()) {
\r
379 if (reader.name().equalsIgnoreCase("Author"))
\r
380 attributes.setAuthor(textValue());
\r
381 if (reader.name().equalsIgnoreCase("SourceURL"))
\r
382 attributes.setSourceURL(textValue());
\r
383 if (reader.name().equalsIgnoreCase("Source"))
\r
384 attributes.setSource(textValue());
\r
385 if (reader.name().equalsIgnoreCase("SourceApplication"))
\r
386 attributes.setSourceApplication(textValue());
\r
387 if (reader.name().equalsIgnoreCase("Altitude"))
\r
388 attributes.setAltitude(doubleValue());
\r
389 if (reader.name().equalsIgnoreCase("Longitude"))
\r
390 attributes.setLongitude(doubleValue());
\r
391 if (reader.name().equalsIgnoreCase("Latitude"))
\r
392 attributes.setLatitude(doubleValue());
\r
393 if (reader.name().equalsIgnoreCase("SubjectDate"))
\r
394 attributes.setSubjectDate(longValue());
\r
397 if (reader.name().equalsIgnoreCase("noteattributes") && reader.isEndElement())
\r
405 private void processSynchronizationNode() {
\r
406 boolean atEnd = false;
\r
408 if (reader.isStartElement()) {
\r
409 if (reader.name().equalsIgnoreCase("UpdateSequenceNumber"))
\r
410 highUpdateSequenceNumber = intValue();
\r
411 if (reader.name().equalsIgnoreCase("LastSequenceDate"))
\r
412 lastSequenceDate = longValue();
\r
415 if (reader.name().equalsIgnoreCase("synchronization") && reader.isEndElement())
\r
421 private void processSavedSearchNode() {
\r
422 search = new SavedSearch();
\r
423 searchIsDirty = false;
\r
425 boolean atEnd = false;
\r
427 if (reader.isStartElement()) {
\r
428 if (reader.name().equalsIgnoreCase("Guid"))
\r
429 search.setGuid(textValue());
\r
430 if (reader.name().equalsIgnoreCase("Name"))
\r
431 search.setName(textValue());
\r
432 if (reader.name().equalsIgnoreCase("UpdateSequenceNumber"))
\r
433 search.setUpdateSequenceNum(intValue());
\r
434 if (reader.name().equalsIgnoreCase("Query"))
\r
435 search.setQuery(textValue());
\r
436 if (reader.name().equalsIgnoreCase("Dirty")) {
\r
437 if (booleanValue())
\r
438 searchIsDirty = true;
\r
442 if (reader.name().equalsIgnoreCase("savedsearch") && reader.isEndElement())
\r
449 private void processNotebookNode() {
\r
450 notebook = new Notebook();
\r
451 notebookIsDirty = false;
\r
452 notebookIsLocal = false;
\r
453 boolean atEnd = false;
\r
455 if (reader.isStartElement()) {
\r
456 if (reader.name().equalsIgnoreCase("Guid"))
\r
457 notebook.setGuid(textValue());
\r
458 if (reader.name().equalsIgnoreCase("Name"))
\r
459 notebook.setName(textValue());
\r
460 if (reader.name().equalsIgnoreCase("UpdateSequenceNumber"))
\r
461 notebook.setUpdateSequenceNum(intValue());
\r
462 if (reader.name().equalsIgnoreCase("ServiceCreated"))
\r
463 notebook.setServiceCreated(longValue());
\r
464 if (reader.name().equalsIgnoreCase("ServiceUpdated"))
\r
465 notebook.setServiceUpdated(longValue());
\r
466 if (reader.name().equalsIgnoreCase("DefaultNotebook")) {
\r
467 notebook.setDefaultNotebook(booleanValue());
\r
469 if (reader.name().equalsIgnoreCase("Dirty")) {
\r
470 if (booleanValue())
\r
471 notebookIsDirty = true;
\r
473 if (reader.name().equalsIgnoreCase("LocalNotebook")) {
\r
474 if (booleanValue())
\r
475 notebookIsLocal = true;
\r
479 if (reader.name().equalsIgnoreCase("notebook") && reader.isEndElement())
\r
487 private void processTagNode() {
\r
489 tagIsDirty = false;
\r
490 boolean atEnd = false;
\r
492 if (reader.isStartElement()) {
\r
493 if (reader.name().equalsIgnoreCase("Guid"))
\r
494 tag.setGuid(textValue());
\r
495 if (reader.name().equalsIgnoreCase("Name"))
\r
496 tag.setName(textValue());
\r
497 if (reader.name().equalsIgnoreCase("UpdateSequenceNumber"))
\r
498 tag.setUpdateSequenceNum(intValue());
\r
499 if (reader.name().equalsIgnoreCase("ParentGuid"))
\r
500 tag.setParentGuid(textValue());
\r
501 if (reader.name().equalsIgnoreCase("Dirty")) {
\r
502 if (booleanValue())
\r
507 if (reader.name().equalsIgnoreCase("tag") && reader.isEndElement())
\r
516 private String textValue() {
\r
517 return reader.readElementText();
\r
519 private int intValue() {
\r
520 return new Integer(textValue());
\r
522 private long longValue() {
\r
523 return new Long(textValue());
\r
525 private double doubleValue() {
\r
526 return new Double(textValue());
\r
528 private boolean booleanValue() {
\r
529 String value = textValue();
\r
530 if (value.equalsIgnoreCase("true"))
\r
535 private short shortValue() {
\r
536 return new Short(textValue());
\r
539 public void setNotebookGuid(String g) {
\r
543 public String getErrorMessage() {
\r
544 return errorMessage;
\r