Skip to content

Commit

Permalink
Add the ability to import notes via the command line.
Browse files Browse the repository at this point in the history
  • Loading branch information
baumgarr committed Dec 15, 2015
1 parent ea90f98 commit 202e185
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 36 deletions.
6 changes: 4 additions & 2 deletions NixNote2.pro
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ SOURCES += main.cpp\
dialog/faderdialog.cpp \
cmdtools/extractnotetext.cpp \
cmdtools/extractnotes.cpp \
cmdtools/alternote.cpp
cmdtools/alternote.cpp \
cmdtools/importnotes.cpp



Expand Down Expand Up @@ -426,7 +427,8 @@ HEADERS += nixnote.h \
dialog/faderdialog.h \
cmdtools/extractnotetext.h \
cmdtools/extractnotes.h \
cmdtools/alternote.h
cmdtools/alternote.h \
cmdtools/importnotes.h



Expand Down
31 changes: 25 additions & 6 deletions cmdtools/cmdlinetool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ CmdLineTool::CmdLineTool(QObject *parent) :
{
}


// Run the command line request.
int CmdLineTool::run(StartupConfig &config) {
#if QT_VERSION < 0x050000
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
Expand Down Expand Up @@ -102,6 +102,9 @@ int CmdLineTool::run(StartupConfig &config) {
if (config.exports() || config.backup()) {
return exportNotes(config);
}
if (config.import()) {
return importNotes(config);
}
if (config.alterNote()) {
return alterNote(config);
}
Expand All @@ -111,7 +114,7 @@ int CmdLineTool::run(StartupConfig &config) {




// Email a note via the command line.
int CmdLineTool::emailNote(StartupConfig config) {
// Look to see if another NixNote is running. If so, then we
// expect a response if the note was delete. Otherwise, we
Expand All @@ -132,7 +135,7 @@ int CmdLineTool::emailNote(StartupConfig config) {
}



// Delete a note via the command line
int CmdLineTool::deleteNote(StartupConfig config) {
bool useCrossMemory = true;

Expand Down Expand Up @@ -164,7 +167,7 @@ int CmdLineTool::deleteNote(StartupConfig config) {
}



// Query notes via the command line
int CmdLineTool::queryNotes(StartupConfig config) {
bool expectResponse = true;

Expand Down Expand Up @@ -220,7 +223,9 @@ int CmdLineTool::queryNotes(StartupConfig config) {
}



// Add a note via the command line. if Nixnote is runnning,
// the new note is copied into the dbi directory and
// auto-imported. If it is not running it is created directly.
int CmdLineTool::addNote(StartupConfig config) {

#ifdef Q_OS_WIN32
Expand Down Expand Up @@ -427,7 +432,8 @@ int CmdLineTool::addNote(StartupConfig config) {




// Read a note via the command line and extract the text
// contents.
int CmdLineTool::readNote(StartupConfig config) {
bool useCrossMemory = true;

Expand Down Expand Up @@ -479,6 +485,7 @@ int CmdLineTool::readNote(StartupConfig config) {



// Export notes or do a backup via the command line
int CmdLineTool::exportNotes(StartupConfig config) {
if (global.sharedMemory->attach()) {
std::cout << tr("This cannot be done with NixNote running.").toStdString() << endl;
Expand All @@ -494,7 +501,19 @@ int CmdLineTool::exportNotes(StartupConfig config) {



// Import notes from a nnex file.
int CmdLineTool::importNotes(StartupConfig config) {
if (global.sharedMemory->attach()) {
std::cout << tr("This cannot be done with NixNote running.").toStdString() << endl;
return 16;
}
global.db = new DatabaseConnection("nixnote"); // Startup the database
config.importNotes->import();
return 0;
}


// Alter a note's notebook or add/remove tags for a note.
int CmdLineTool::alterNote(StartupConfig config) {
// Look to see if another NixNote is running. If so, then we
// expect a response, otherwise we do it ourself.
Expand Down
1 change: 1 addition & 0 deletions cmdtools/cmdlinetool.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class CmdLineTool : public QObject
int emailNote(StartupConfig config);
int exportNotes(StartupConfig config);
int alterNote(StartupConfig config);
int importNotes(StartupConfig config);

signals:

Expand Down
35 changes: 35 additions & 0 deletions cmdtools/importnotes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "importnotes.h"
#include <iostream>
#include <QFile>
#include "xml/importdata.h"

ImportNotes::ImportNotes(QObject *parent) :
QObject(parent)
{
inputFile = "";
}




// Perform the import via the command line
int ImportNotes::import() {

if (inputFile.trimmed() == "") {
std::cout << QString(tr("Input file not specified.")).toStdString() << std::endl;
return 16;
}
QFile f(inputFile);
if (!f.exists()) {
std::cout << QString(tr("Input file not found.")).toStdString() << std::endl;
return 16;
}

ImportData importData(false,true,0);
importData.import(inputFile);
if (importData.lastError != 0) {
std::cout << importData.errorMessage.toStdString() << std::endl;
return 16;
}
return 0;
}
20 changes: 20 additions & 0 deletions cmdtools/importnotes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef IMPORTNOTES_H
#define IMPORTNOTES_H

#include <QObject>

class ImportNotes : public QObject
{
Q_OBJECT
public:
explicit ImportNotes(QObject *parent = 0);
QString inputFile;
int import();

signals:

public slots:

};

#endif // IMPORTNOTES_H
21 changes: 21 additions & 0 deletions settings/startupconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ StartupConfig::StartupConfig()
email = NULL;
extractText = NULL;
exportNotes = NULL;
importNotes = NULL;
alter = NULL;
}

Expand Down Expand Up @@ -142,9 +143,14 @@ void StartupConfig::printHelp() {
+QString(" export options:\n")
+QString(" --id=\"<note_ids>\" Space separated list of note IDs to extract.\n")
+QString(" --search=\"search string\" Export notes matching search string.\n")
+QString(" --output=\"filename\" Output file name.\n")
+QString(" --deleteAfterExtract Delete notes after the extract completes.\n")
+QString(" --noVerifyDelete Don't verify deletions.\n")
+QString(" --accountId=<id> Account number (defaults to last used account).\n\n")
+QString(" import <options> Import notes from a NixNote extract (.nnex).\n")
+QString(" import options:\n")
+QString(" --input=\"filename\" Input file name.\n")
+QString(" --accountId=<id> Account number (defaults to last used account).\n\n")
+QString(" Examples:\n\n")
+QString(" To Start NixNote, do a sync, and then exit.\n")
+QString(" nixnote2 start --syncAndExit\n\n")
Expand Down Expand Up @@ -195,6 +201,11 @@ int StartupConfig::init(int argc, char *argv[]) {
exportNotes = new ExtractNotes();
exportNotes->backup=false;
}
if (parm.startsWith("import")) {
command->setBit(STARTUP_IMPORT,true);
if (importNotes == NULL)
importNotes = new ImportNotes();
}
if (parm.startsWith("backup")) {
command->setBit(STARTUP_BACKUP,true);
if (exportNotes == NULL)
Expand Down Expand Up @@ -344,6 +355,12 @@ int StartupConfig::init(int argc, char *argv[]) {
exportNotes->outputFile = parm;
}
}
if (command->at(STARTUP_IMPORT)) {
if (parm.startsWith("--input=", Qt::CaseSensitive)) {
parm = parm.mid(8);
importNotes->inputFile = parm;
}
}
if (command->at(STARTUP_BACKUP)) {
if (parm.startsWith("--output=", Qt::CaseSensitive)) {
parm = parm.mid(9);
Expand Down Expand Up @@ -472,6 +489,10 @@ bool StartupConfig::exports() {
return command->at(STARTUP_EXPORT);
}

bool StartupConfig::import() {
return command->at(STARTUP_IMPORT);
}

bool StartupConfig::backup() {
return command->at(STARTUP_BACKUP);
}
Expand Down
3 changes: 3 additions & 0 deletions settings/startupconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "cmdtools/extractnotetext.h"
#include "cmdtools/extractnotes.h"
#include "cmdtools/alternote.h"
#include "cmdtools/importnotes.h"

#define STARTUP_GUI 0
#define STARTUP_SYNC 1
Expand Down Expand Up @@ -78,6 +79,7 @@ class StartupConfig
CmdLineQuery *queryNotes;
ExtractNoteText *extractText;
ExtractNotes *exportNotes;
ImportNotes *importNotes;
AlterNote *alter;
bool gui();
bool sync();
Expand All @@ -91,6 +93,7 @@ class StartupConfig
bool exports();
bool backup();
bool alterNote();
bool import();
void setSyncAndExit();

int init(int argc, char *argv[]);
Expand Down
66 changes: 39 additions & 27 deletions xml/importdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ extern Global global;
//* where an import will keep the guid and will obey the
//* <dirty> tag in the note.
//***********************************************************
ImportData::ImportData(bool full, QObject *parent) : QObject(parent)
ImportData::ImportData(bool full, bool cmdline, QObject *parent) : QObject(parent)
{
importTags = false;
importNotebooks = false;
backup = full;
createTags = false;
stopNow = false;
this->cmdline = cmdline;

// get the
if (!full) {
Expand All @@ -70,8 +71,10 @@ ImportData::ImportData(bool full, QObject *parent) : QObject(parent)
t.getGuid(notebookGuid, lid);
}
}
progress = new QProgressDialog();
progress->setVisible(false);
if (!this->cmdline) {
progress = new QProgressDialog();
progress->setVisible(false);
}
}


Expand All @@ -98,37 +101,43 @@ void ImportData::import(QString file) {

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();
if (!cmdline) {
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();
if (!cmdline) {
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"));
if (!cmdline) {
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();
}
progress->setWindowModality(Qt::ApplicationModal);
connect(progress, SIGNAL(canceled()), this, SLOT(cancel()));
progress->setVisible(true);
mb.close();
progress->show();
recCnt = 0;

reader = new QXmlStreamReader(&xmlFile);
Expand Down Expand Up @@ -167,7 +176,8 @@ void ImportData::import(QString file) {
if (type.toLower() == "backup" && !backup) {
lastError = 4;
errorMessage = "This is backup file, not an export file";
progress->hide();
if (!cmdline)
progress->hide();
return;
}
if (type.toLower() == "export" && backup) {
Expand All @@ -182,7 +192,8 @@ void ImportData::import(QString file) {
if (reader->name().toString().toLower() == "note" && reader->isStartElement()) {
processNoteNode();
recCnt++;
progress->setValue(recCnt);
if (!cmdline)
progress->setValue(recCnt);
}
if (reader->name().toString().toLower() == "notebook" && reader->isStartElement() && (backup || importNotebooks)) {
processNotebookNode();
Expand Down Expand Up @@ -221,7 +232,8 @@ void ImportData::import(QString file) {
}
}
query.exec("commit");
progress->hide();
if (!this->cmdline)
progress->hide();
}


Expand Down
3 changes: 2 additions & 1 deletion xml/importdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ class ImportData : public QObject
bool importTags;
bool importNotebooks;
bool createTags;
ImportData(bool full, QObject *parent=0);
bool cmdline;
ImportData(bool full, bool cmdline=false, QObject *parent=0);
void import(QString file);
void setNotebookGuid(QString g);
QString getErrorMessage();
Expand Down

0 comments on commit 202e185

Please sign in to comment.