//*********************************************************** //* 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 ImportData::import(QString file) { fileName = file; errorMessage = ""; lastError = 0; QFile xmlFile(fileName); QFile scanFile(fileName); if (!xmlFile.open(QIODevice::ReadOnly) || !scanFile.open(QIODevice::ReadOnly)) { lastError = 16; errorMessage = "Cannot open file."; return; } QTextStream *countReader = new QTextStream(&scanFile); int recCnt = 0; QMessageBox mb; mb.setWindowTitle(tr("Scanning File")); mb.setText(QString::number(recCnt) + tr(" notes found.")); QPushButton *cancelButton = mb.addButton(QMessageBox::Cancel); connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancel())); mb.show(); QCoreApplication::processEvents(); while (!countReader->atEnd() && !stopNow) { QString line = countReader->readLine(); if (line.contains("<note>", Qt::CaseInsensitive)) { recCnt++; mb.setText(QString::number(recCnt) + tr(" notes found.")); QCoreApplication::processEvents(); } } notebookData.clear(); progress->setMaximum(recCnt); progress->setMinimum(0); if (backup) { progress->setWindowTitle(tr("Importing")); progress->setLabelText(tr("Importing Notes")); } else { progress->setWindowTitle(tr("Restore")); progress->setLabelText(tr("Restoring Notes")); } progress->setWindowModality(Qt::ApplicationModal); connect(progress, SIGNAL(canceled()), this, SLOT(cancel())); progress->setVisible(true); mb.close(); progress->show(); recCnt = 0; reader = new QXmlStreamReader(&xmlFile); NSqlQuery query(global.db); query.exec("begin"); int commitCount = 100; while (!reader->atEnd() && !stopNow) { if (commitCount <= 0) { query.exec("commit"); commitCount = 100; } commitCount--; reader->readNext(); if (reader->hasError()) { errorMessage = reader->errorString(); QLOG_ERROR() << "************************* ERROR READING BACKUP " << errorMessage; lastError = 16; return; } if (reader->name().toString().toLower() == "nevernote-export" && reader->isStartElement()) { QXmlStreamAttributes attributes = reader->attributes(); QString version = attributes.value("version").toString(); QString type = attributes.value("exportType").toString(); QString application = attributes.value("application").toString(); if (version != "0.85" && version != "0.86" && version != "0.95") { lastError = 1; errorMessage = "Unknown backup version = " +version; return; } if (application.toLower() != "nevernote") { lastError = 2; errorMessage = "This backup is from an unknown application = " +application; return; } if (type.toLower() == "backup" && !backup) { lastError = 4; errorMessage = "This is backup file, not an export file"; progress->hide(); return; } if (type.toLower() == "export" && backup) { lastError = 5; errorMessage = "This is an export file, not a backup file"; return; } } if (reader->name().toString().toLower() == "synchronization" && reader->isStartElement() && backup) { processSynchronizationNode(); } if (reader->name().toString().toLower() == "note" && reader->isStartElement()) { processNoteNode(); recCnt++; progress->setValue(recCnt); } if (reader->name().toString().toLower() == "notebook" && reader->isStartElement() && (backup || importNotebooks)) { processNotebookNode(); } if (reader->name().toString().toLower() == "tag" && reader->isStartElement() && (backup || importTags)) { processTagNode(); } if (reader->name().toString().toLower() == "savedsearch" && reader->isStartElement() && backup) { processSavedSearchNode(); } if (reader->name().toString().toLower() == "linkednotebook" && reader->isStartElement() && backup) { processLinkedNotebookNode(); } if (reader->name().toString().toLower() == "SharedNotebook" && reader->isStartElement() && backup) { processSharedNotebookNode(); } } xmlFile.close(); query.exec("commit"); // Now we do what is a "ahem" hack. We need to // go through all of the notes & rebuild the NoteTable. This // is because we may have gotten notes & tags before the notebook // & tag names existed. There is probably a more efficient way // to do this, but since we don't really do this often this works // as well as any other way. NoteTable noteTable(global.db); for (qint32 i=0; i<noteList.size(); i++) { qint32 lid = noteTable.getLid(noteList[i]); if (lid > 0) { Note note; bool dirty = noteTable.isDirty(lid); noteTable.get(note, lid, false, false); noteTable.updateNoteList(lid, note, dirty, 0); } } query.exec("commit"); progress->hide(); }
//*********************************************************** //* 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 ImportData::import(QString file) { fileName = file; errorMessage = ""; lastError = 0; QFile xmlFile(fileName); if (!xmlFile.open(QIODevice::ReadOnly)) { lastError = 16; errorMessage = "Cannot open file."; } reader = new QXmlStreamReader(&xmlFile); QSqlQuery query; query.exec("begin"); while (!reader->atEnd()) { reader->readNext(); if (reader->hasError()) { errorMessage = reader->errorString(); QLOG_ERROR() << "************************* ERROR READING BACKUP " << errorMessage; lastError = 16; return; } if (reader->name().toString().toLower() == "nevernote-export" && reader->isStartElement()) { QXmlStreamAttributes attributes = reader->attributes(); QString version = attributes.value("version").toString(); QString type = attributes.value("exportType").toString(); QString application = attributes.value("application").toString(); if (version != "0.85" && version != "0.86" && version != "0.95") { lastError = 1; errorMessage = "Unknown backup version = " +version; return; } if (application.toLower() != "nevernote") { lastError = 2; errorMessage = "This backup is from an unknown application = " +application; return; } if (type.toLower() == "backup" && !backup) { lastError = 4; errorMessage = "This is an export file, not a backup file"; return; } if (type.toLower() == "export" && backup) { lastError = 5; errorMessage = "This is a backup file, not an export file"; return; } } if (reader->name().toString().toLower() == "synchronization" && reader->isStartElement() && backup) { processSynchronizationNode(); } if (reader->name().toString().toLower() == "note" && reader->isStartElement()) { processNoteNode(); } if (reader->name().toString().toLower() == "notebook" && reader->isStartElement() && (backup || importNotebooks)) { processNotebookNode(); } if (reader->name().toString().toLower() == "tag" && reader->isStartElement() && (backup || importTags)) { processTagNode(); } if (reader->name().toString().toLower() == "savedsearch" && reader->isStartElement() && backup) { processSavedSearchNode(); } if (reader->name().toString().toLower() == "linkednotebook" && reader->isStartElement() && backup) { processLinkedNotebookNode(); } if (reader->name().toString().toLower() == "SharedNotebook" && reader->isStartElement() && backup) { processSharedNotebookNode(); } } xmlFile.close(); query.exec("commit"); // Now we do what is a "ahem" hack. We need to // go through all of the notes & rebuild the NoteTable. This // is because we may have gotten notes & tags before the notebook // & tag names existed. There is probably a more efficient way // to do this, but since we don't really do this often this works // as well as any awy. NoteTable noteTable; for (qint32 i=0; i<noteList.size(); i++) { qint32 lid = noteTable.getLid(noteList[i]); if (lid > 0) { Note note; bool dirty = noteTable.isDirty(lid); noteTable.get(note, lid, false, false); noteTable.updateNoteList(lid, note, dirty); } } }