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 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
|
/*********************************************************************************
NixNote - An open-source client for the Evernote service.
Copyright (C) 2015 Randy Baumgarte
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
***********************************************************************************/
#include "batchimport.h"
#include <QStringList>
#include <QString>
#include "sql/resourcetable.h"
#include "sql/notebooktable.h"
#include "sql/notetable.h"
#include "sql/tagtable.h"
#include "sql/searchtable.h"
#include "sql/usertable.h"
#include "utilities/crossmemorymapper.h"
#include "utilities/mimereference.h"
#include "global.h"
#include <QProgressDialog>
#include <QMessageBox>
#include <QPushButton>
extern Global global;
//***********************************************************
//* Constructor. If full == true then this is a restore.
//* if full=false then this is considered an import. The
//* only real difference is that an import automatically
//* gets a new guid and is always considered "dirty"
//* where an import will keep the guid and will obey the
//* <dirty> tag in the note.
//***********************************************************
BatchImport::BatchImport(QObject *parent) : QObject(parent)
{
}
//***********************************************************
//* This is the main entry point for an import. It is passed
//* the file which contains the export data. It then
//* opens up the file, checks the validity of the data, and
//* then begins to parse through all of the crap.
//***********************************************************
void BatchImport::import(QString file) {
fileName = file;
qint32 newLid = -1;
errorMessage = "";
lastError = 0;
QFile xmlFile(fileName);
if (!xmlFile.open(QIODevice::ReadOnly)) {
lastError = 16;
errorMessage = "Cannot open file.";
return;
}
reader = new QXmlStreamReader(&xmlFile);
while (!reader->atEnd()) {
reader->readNext();
if (reader->hasError()) {
errorMessage = reader->errorString();
QLOG_ERROR() << "************************* ERROR READING IMPORT " << errorMessage;
lastError = 16;
return;
}
if (reader->name().toString().toLower() == "noteadd" && reader->isStartElement()) {
newLid = addNoteNode();
}
}
xmlFile.close();
QString id = file;
int pos = id.lastIndexOf(".");
if (pos>0)
id = id.mid(0,pos);
pos = id.lastIndexOf(QDir::separator());
if (pos>0)
id = id.mid(pos+1);
CrossMemoryMapper sharedMemory(id);
if (!sharedMemory.attach())
return;
QString response = QString::number(newLid);
sharedMemory.write(response.toAscii());
sharedMemory.detach();
}
//***********************************************************
//* Process a <note> tag
//***********************************************************
qint32 BatchImport::addNoteNode() {
qint32 newLid = -1;
Note note;
note.title = QString(tr("Untitled Note"));
QUuid uuid;
qint32 tempLid = 0;
QString newGuid = uuid.createUuid().toString().replace("{", "").replace("}", "");
note.guid = newGuid;
QStringList tagNames;
QStringList tagGuids;
QStringList resourceList;
QString resourceDelimiter;
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 == "lid" && !reader->isEndElement()) {
tempLid = intValue();
}
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 == "reminder" && !reader->isEndElement()) {
QString dateString = textValue();
QDateTime date = QDateTime::fromString(dateString, "yyyy-MM-ddTHH:mm:ss.zzzZ");
if (date >= QDateTime::currentDateTime()) {
if (!note.attributes.isSet()) {
NoteAttributes na;
note.attributes = na;
}
note.attributes->reminderTime = 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 == "attachmentdelimiter" && !reader->isEndElement()) {
resourceDelimiter = textValue();
}
if (name == "attachment" && !reader->isEndElement()) {
resourceList.append(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() && tempLid > 0) {
// global.db = new DatabaseConnection("nixnote"); // Startup the database
Note newNote;
// Fetch the existing note
NoteTable noteTable(global.db);
if (!noteTable.get(newNote, tempLid, true, true)) {
return -1;
}
// Append the text to the existing note
newNote.content->replace("</en-note>", "<br/>");
// Chop off the beginning of the new text to remove the <en-note stuff
int startOfNote = note.content->indexOf("<en-note");
note.content = note.content->mid(startOfNote+9);
// Append the two notes
newNote.content = newNote.content + note.content;
// Start adding the resources
qint32 noteLid = tempLid;
if (resourceList.size() > 0) {
note.resources = QList<Resource>();
for (int i=0; i<resourceList.size(); i++) {
QString filename = resourceList[i];
QFile file(filename);
if (file.exists()) {
file.open(QIODevice::ReadOnly);
QByteArray ba = file.readAll();
file.close();
MimeReference mimeRef;
QString extension = filename;
int endPos = filename.lastIndexOf(".");
if (endPos != -1)
extension = extension.mid(endPos);
QString mime = mimeRef.getMimeFromExtension(extension);
Resource newRes;
bool attachment = true;
if (mime == "application/pdf" || mime.startsWith("image/"))
attachment = false;
AddNote newNote;
newNote.createResource(newRes, 0, ba, mime, attachment, QFileInfo(filename).fileName(), noteLid);
QByteArray hash;
if (newRes.data.isSet()) {
Data d = newRes.data;
if (d.bodyHash.isSet())
hash = d.bodyHash;
}
QString mediaString = "<en-media hash=\""+hash.toHex()+"\"; type=\""+mime+"\"/>";
if (note.content->contains(resourceDelimiter)) {
note.content = note.content->replace(note.content->indexOf(resourceDelimiter),
resourceDelimiter.size(), mediaString);
} else {
note.content = note.content->replace("</en-note>","<br>"+mediaString+"</en-note>");
}
note.resources->append(newRes);
}
}
}
noteTable.updateNoteContent(tempLid, newNote.content, true);
noteTable.add(tempLid,newNote,true);
return tempLid;
}
if (endName == "noteadd" && reader->isEndElement() && tempLid == 0) {
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;
}
// Start adding the resources
qint32 noteLid = ntable.addStub(note.guid);
if (resourceList.size() > 0) {
note.resources = QList<Resource>();
for (int i=0; i<resourceList.size(); i++) {
QString filename = resourceList[i];
QFile file(filename);
if (file.exists()) {
file.open(QIODevice::ReadOnly);
QByteArray ba = file.readAll();
file.close();
MimeReference mimeRef;
QString extension = filename;
int endPos = filename.lastIndexOf(".");
if (endPos != -1)
extension = extension.mid(endPos);
QString mime = mimeRef.getMimeFromExtension(extension);
Resource newRes;
bool attachment = true;
if (mime == "application/pdf" || mime.startsWith("image/"))
attachment = false;
AddNote newNote;
newNote.createResource(newRes, 0, ba, mime, attachment, QFileInfo(filename).fileName(), noteLid);
QByteArray hash;
if (newRes.data.isSet()) {
Data d = newRes.data;
if (d.bodyHash.isSet())
hash = d.bodyHash;
}
QString mediaString = "<en-media hash=\""+hash.toHex()+"\"; type=\""+mime+"\"/>";
if (note.content->contains(resourceDelimiter)) {
note.content = note.content->replace(note.content->indexOf(resourceDelimiter),
resourceDelimiter.size(), mediaString);
} else {
note.content = note.content->replace("</en-note>","<br>"+mediaString+"</en-note>");
}
note.resources->append(newRes);
}
}
}
ntable.expunge(noteLid); // delete the stub.
newLid = ntable.add(0, note, true);
}
}
return newLid;
}
QString BatchImport::textValue() {
reader->readNext();
return reader->text().toString();
}
qint32 BatchImport::intValue() {
return textValue().toLong();
}
long BatchImport::longValue() {
return textValue().toLong();
}
qlonglong BatchImport::longlongValue() {
return textValue().toLongLong();
}
double BatchImport::doubleValue() {
return textValue().toDouble();
}
bool BatchImport::booleanValue() {
QString value = textValue();
value.toLower();
if (value == "1" || value == "true")
return true;
else
return false;
}
short BatchImport::shortValue() {
return textValue().toShort();
}
|