// Synchronize remote notes with the current database void SyncRunner::syncRemoteNotes(QList<Note> notes, qint32 account) { QLOG_TRACE() << "Entering SyncRunner::syncRemoteNotes"; NoteTable noteTable(db); NotebookTable bookTable(db); for (int i=0; i<notes.size() && keepRunning; i++) { Note t = notes[i]; qint32 lid = noteTable.getLid(t.guid); if (lid > 0) { // Find out if it is a conflicting change if (noteTable.isDirty(lid)) { qint32 newLid = noteTable.duplicateNote(lid); qint32 conflictNotebook = bookTable.getConflictNotebook(); noteTable.updateNotebook(newLid, conflictNotebook, true); if (!finalSync) emit noteUpdated(newLid); } noteTable.sync(lid, notes.at(i), account); } else { noteTable.sync(t, account); lid = noteTable.getLid(t.guid); } // Remove it from the cache (if it exists) if (global.cache.contains(lid)) { delete global.cache[lid]; global.cache.remove(lid); } if (!finalSync) emit noteUpdated(lid); } QLOG_TRACE() << "Leaving SyncRunner::syncRemoteNotes"; }
// Handle what happens when something is dropped onto a tag item bool NNotebookView::dropMimeData(QTreeWidgetItem *parent, int index, const QMimeData *data, Qt::DropAction action) { Q_UNUSED(index); // suppress unused variable Q_UNUSED(action); // suppress unused variable // If this is a note-to-tag drop we are assigning tags to a note if (data->hasFormat("application/x-nixnote-note")) { QByteArray d = data->data("application/x-nixnote-note"); QString data(d); // Find the tag lid we dropped onto qint32 bookLid = parent->data(NAME_POSITION, Qt::UserRole).toInt(); if (bookLid <=0) return false; NotebookTable bookTable(global.db); Notebook notebook; bookTable.get(notebook, bookLid); // The string has a long list of note lids. We parse them out & update the note QStringList stringLids = data.split(" "); for (int i=0; i<stringLids.size(); i++) { if (stringLids[i].trimmed() != "") { qint32 noteLid = stringLids.at(i).toInt(); if (noteLid > 0) { NoteTable noteTable(global.db); qint32 currentNotebook = noteTable.getNotebookLid(noteLid); if (currentNotebook != bookLid) { noteTable.updateNotebook(noteLid, bookLid, true); emit(updateNoteList(noteLid, NOTE_TABLE_NOTEBOOK_POSITION, notebook.name.value())); qint64 dt = QDateTime::currentMSecsSinceEpoch(); noteTable.updateDate(noteLid, dt, NOTE_UPDATED_DATE, true); emit(updateNoteList(noteLid, NOTE_TABLE_DATE_UPDATED_POSITION, dt)); } } } } if (stringLids.size() > 0) { emit(updateCounts()); } return true; } return false; }
void NNotebookViewItem::setIconType() { LinkedNotebookTable linkedTable(global.db); SharedNotebookTable sharedTable(global.db); NotebookTable bookTable(global.db); if (lid == 0) { this->setType(Stack); return; } if (linkedTable.exists(lid)) { this->setType(Linked); return; } if (sharedTable.exists(lid)) { this->setType(Shared); return; } if (!bookTable.isLocal(lid)) { this->setType(Synchronized); return; } Notebook notebook; bookTable.get(notebook, lid); QString notebookname = ""; if (notebook.name.isSet()) notebookname = notebook.name; if (notebookname.startsWith("Conflict", Qt::CaseInsensitive)) { this->setType(Conflict); return; } this->setType(Local); return; }
//*********************************************************** //* Process a <note> tag //*********************************************************** void BatchImport::addNoteNode() { Note note; note.title = QString(tr("Untitled Note")); QUuid uuid; QString newGuid = uuid.createUuid().toString().replace("{", "").replace("}", ""); note.guid = newGuid; QStringList tagNames; QStringList tagGuids; QString newNoteBody = QString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")+ QString("<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\">")+ QString("<en-note style=\"word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;\"><br/></en-note>"); note.active = true; note.content = newNoteBody; note.created = QDateTime::currentMSecsSinceEpoch(); note.updated = QDateTime::currentMSecsSinceEpoch(); bool atEnd = false; while(!atEnd) { QString name = reader->name().toString().toLower(); if (name == "title" && !reader->isEndElement()) { note.title = textValue(); } if (name == "created" && !reader->isEndElement()) { QString dateString = textValue(); //QDateTime date = QDateTime::fromString("2010-10-25T10:28:58.570Z", "yyyy-MM-ddTHH:mm:ss.zzzZ"); QDateTime date = QDateTime::fromString(dateString, "yyyy-MM-ddTHH:mm:ss.zzzZ"); note.created = date.toMSecsSinceEpoch(); } if (name == "updated" && !reader->isEndElement()) { QString dateString = textValue(); QDateTime date = QDateTime::fromString(dateString, "yyyy-MM-ddTHH:mm:ss.zzzZ"); note.updated = date.toMSecsSinceEpoch(); } if (name == "notebook" && !reader->isEndElement()) { QString notebookName = textValue(); NotebookTable notebookTable(global.db); qint32 lid = notebookTable.findByName(notebookName); QString notebookGuid; // Do we need to add the notebook? if (lid == 0) { Notebook book; book.name = notebookName; QUuid uuid; QString newGuid = uuid.createUuid().toString().replace("{", "").replace("}", ""); book.guid = newGuid; notebookGuid = newGuid; lid = notebookTable.add(0, book, true, false); } else { notebookTable.getGuid(notebookGuid, lid); } note.notebookGuid = notebookGuid; } if (name == "content" && !reader->isEndElement()) { note.content = textValue(); } if (name == "tag" && !reader->isEndElement()) { QString tagName = textValue(); TagTable tagTable(global.db); qint32 tagLid = tagTable.findByName(tagName, 0); QString tagGuid; // Do we need to add the tag? if (tagLid == 0) { Tag tag; tag.name = tagName; QUuid uuid; tagGuid = uuid.createUuid().toString().replace("{", "").replace("}", ""); tag.guid = tagGuid; tagTable.add(0, tag, true, 0); } else { tagTable.getGuid(tagGuid, tagLid); } tagNames.append(tagName); tagGuids.append(tagGuid); } reader->readNext(); QString endName = reader->name().toString().toLower(); if (endName == "noteadd" && reader->isEndElement()) { atEnd = true; note.tagGuids = tagGuids; note.tagNames = tagNames; NoteTable ntable(global.db); if (!note.notebookGuid.isSet()) { NotebookTable bookTable(global.db); QString book = bookTable.getDefaultNotebookGuid(); note.notebookGuid = book; } ntable.add(0, note, true); } } return; }
// Do the alter. If the notebook is not found we create it. // If a tag is not found for an add, we add it. If a tag is // not found for a deleteTag, we just ignore it. int AlterNote::alterNote() { // If a query is specified, we find the matching notes. if (query != "") { FilterCriteria *filter = new FilterCriteria(); global.filterCriteria.append(filter); global.filterPosition = 0; FilterEngine engine; filter->setSearchString(query); QList<qint32> lids; engine.filter(filter, &lids); this->lids.append(lids); } NotebookTable bookTable(global.db); TagTable tagTable(global.db); NoteTable noteTable(global.db); // Loop through each note requested. for (int i=0; i<lids.size(); i++) { qint32 lid = lids[i]; // Do the notebook request if (notebook != "") { qint32 notebookLid = bookTable.findByName(notebook); if (notebookLid<0) { Notebook book; book.name = notebook; NUuid uuid; book.guid = uuid.create(); notebookLid = bookTable.add(0,book,true,true); } if (noteTable.getNotebookLid(lid) != notebookLid) noteTable.updateNotebook(lid, notebookLid, true); } // Add the tags for (int j=0; j<addTagNames.size(); j++) { qint32 tagLid = tagTable.findByName(addTagNames[j],0); if (tagLid <= 0) { Tag t; t.name = addTagNames[j]; NUuid uuid; t.guid = uuid.create(); tagLid = tagTable.add(0,t,true,0); } if (!noteTable.hasTag(lid,tagLid)) noteTable.addTag(lid, tagLid, true); } // Remove any tags specified for (int j=0; j<delTagNames.size(); j++) { qint32 tagLid = tagTable.findByName(delTagNames[j],0); if (tagLid > 0 && noteTable.hasTag(lid,tagLid)) { noteTable.removeTag(lid, tagLid, true); } } if (reminderCompleted) { noteTable.setReminderCompleted(lid, true); } if (clearReminder) { noteTable.removeReminder(lid); } } return 0; }
// Upload notes that belong to me qint32 SyncRunner::uploadLinkedNotes(qint32 notebookLid) { qint32 usn; qint32 maxUsn = 0; NoteTable noteTable(db); QList<qint32> lids, validLids, deletedLids; noteTable.getAllDirty(lids, notebookLid); // Split the list into deleted and updated notes for (int i=0; i<lids.size(); i++) { if (noteTable.isDeleted(lids[i])) deletedLids.append(lids[i]); else validLids.append(lids[i]); } // Start deleting notes for (int i=0; i<deletedLids.size(); i++) { QString guid = noteTable.getGuid(deletedLids[i]); noteTable.setDirty(lids[i], false); usn = comm->deleteLinkedNote(guid); if (usn > maxUsn) { maxUsn = usn; noteTable.setUpdateSequenceNumber(deletedLids[i], usn); noteTable.setDirty(deletedLids[i], false); if (!finalSync) emit(noteSynchronized(deletedLids[i], false)); } } // Start deleting notes that were in the trash, but the trash was // emptied. NotebookTable bookTable(db); QString notebookGuid = ""; bookTable.getGuid(notebookGuid, notebookLid); // Get all of the notes QStringList deleteQueueGuids; noteTable.getAllDeleteQueue(deleteQueueGuids, notebookGuid); // Do the actual deletes for (int i=0; i<deleteQueueGuids.size(); i++) { QString guid = deleteQueueGuids[i]; usn = comm->deleteLinkedNote(guid); if (usn > maxUsn) { maxUsn = usn; } noteTable.expungeFromDeleteQueue(guid); } // Start uploading notes for (int i=0; i<validLids.size(); i++) { Note note; noteTable.get(note, validLids[i],true, true); qint32 oldUsn = note.updateSequenceNum; usn = comm->uploadLinkedNote(note); if (usn == 0) { this->communicationErrorHandler(); error = true; return maxUsn; } if (usn > maxUsn) { maxUsn = usn; if (oldUsn == 0) noteTable.updateGuid(validLids[i], note.guid); noteTable.setUpdateSequenceNumber(validLids[i], usn); noteTable.setDirty(validLids[i], false); if (!finalSync) emit(noteSynchronized(validLids[i], false)); } else { error = true; } } return maxUsn; }
void FileWatcher::saveFile(QString file) { QFileInfo fileInfo(file); // If we have a dbi import file QLOG_DEBUG() << fileInfo.dir().absolutePath() + QDir::separator(); QLOG_DEBUG() << global.fileManager.getDbiDirPath(); if ((fileInfo.dir().absolutePath() + QDir::separator()) == global.fileManager.getDbiDirPath()) { BatchImport importer; importer.import(file); emit(nnexImported()); QFile f(file); f.remove(); return; } // If we have a user-import file QFile f(file); f.open(QIODevice::ReadOnly); QByteArray data = f.readAll(); f.close(); if (f.size() == 0) return; Note newNote; NoteTable ntable(global.db); ConfigStore cs(global.db); qint32 lid = cs.incrementLidCounter(); QCryptographicHash md5hash(QCryptographicHash::Md5); QByteArray hash = md5hash.hash(data, QCryptographicHash::Md5); // * Start setting up the new note newNote.guid = QString::number(lid); newNote.title = file; NotebookTable bookTable(global.db); QString notebook; bookTable.getGuid(notebook, notebookLid); newNote.notebookGuid = notebook; QString newNoteBody = QString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")+ QString("<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\">")+ QString("<en-note style=\"word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;\">"); MimeReference mimeRef; QString mime = mimeRef.getMimeFromFileName(file); QString enMedia =QString("<en-media hash=\"") +hash.toHex() +QString("\" border=\"0\"") +QString(" type=\"" +mime +"\" ") +QString("/>"); newNoteBody.append(enMedia + QString("</en-note>")); newNote.content = newNoteBody; newNote.active = true; newNote.created = QDateTime::currentMSecsSinceEpoch();; newNote.updated = newNote.created; newNote.updateSequenceNum = 0; NoteAttributes na; na.sourceURL = "file://" + file; newNote.attributes = na; qint32 noteLid = lid; ntable.add(lid, newNote, true); QString noteGuid = ntable.getGuid(lid); lid = cs.incrementLidCounter(); // Start creating the new resource Resource newRes; Data d; d.body = data; d.bodyHash = hash; d.size = data.size(); newRes.data = d; newRes.mime = mime; ResourceAttributes ra; ra.fileName = file; if (mime.startsWith("image", Qt::CaseInsensitive) || mime.endsWith("pdf", Qt::CaseInsensitive)) ra.attachment = false; else ra.attachment = true; newRes.active = true; newRes.guid = QString::number(lid); newRes.noteGuid = noteGuid; newRes.updateSequenceNum = 0; ResourceTable restable(global.db); restable.add(lid, newRes, true, noteLid); emit(fileImported(noteLid, lid)); if (scanType == FileWatcher::ImportDelete) { QLOG_DEBUG() << f.remove(); } }