Skip to content

Commit

Permalink
Add the ability to export notes & backup via the command line.
Browse files Browse the repository at this point in the history
  • Loading branch information
baumgarr committed Dec 14, 2015
1 parent 3f45be8 commit e589b69
Show file tree
Hide file tree
Showing 9 changed files with 299 additions and 41 deletions.
6 changes: 4 additions & 2 deletions NixNote2.pro
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ SOURCES += main.cpp\
cmdtools/deletenote.cpp \
cmdtools/emailnote.cpp \
dialog/faderdialog.cpp \
cmdtools/extractnotetext.cpp
cmdtools/extractnotetext.cpp \
cmdtools/extractnotes.cpp



Expand Down Expand Up @@ -422,7 +423,8 @@ HEADERS += nixnote.h \
cmdtools/deletenote.h \
cmdtools/emailnote.h \
dialog/faderdialog.h \
cmdtools/extractnotetext.h
cmdtools/extractnotetext.h \
cmdtools/extractnotes.h



Expand Down
18 changes: 18 additions & 0 deletions cmdtools/cmdlinetool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ int CmdLineTool::run(StartupConfig &config) {
if (config.readNote()) {
return readNote(config);
}
if (config.exports() || config.backup()) {
return exportNotes(config);
}

return 0;
}

Expand Down Expand Up @@ -470,3 +474,17 @@ int CmdLineTool::readNote(StartupConfig config) {
return 0;
}



int CmdLineTool::exportNotes(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
if (config.exportNotes->backup)
config.exportNotes->backupDB();
else
config.exportNotes->extract();
return 0;
}
1 change: 1 addition & 0 deletions cmdtools/cmdlinetool.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class CmdLineTool : public QObject
int queryNotes(StartupConfig config);
int deleteNote(StartupConfig config);
int emailNote(StartupConfig config);
int exportNotes(StartupConfig config);

signals:

Expand Down
87 changes: 87 additions & 0 deletions cmdtools/extractnotes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*********************************************************************************
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 "extractnotes.h"
#include "xml/exportdata.h"
#include "global.h"
#include "filters/filtercriteria.h"
#include "filters/filterengine.h"
#include "sql/notetable.h"

extern Global global;

ExtractNotes::ExtractNotes(QObject *parent) :
QObject(parent)
{
this->deleteAfterExtract=false;
this->verifyDelete=true;
}



void ExtractNotes::extract() {
if (outputFile.trimmed() == "") {
std::cout << QString(tr("Output file not specified.")).toStdString() << endl;
return;
}
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);
}

ExportData exports(backup, true, this);
if (!backup) {
exports.lids.append(this->lids);
}
exports.backupData(this->outputFile);

if (deleteAfterExtract) {
if (verifyDelete) {
std::string verify;
std::cout << QString(tr("Deleting notes:")).toStdString() << endl;
std::cout << QString(tr("Type DELETE to very: ")).toStdString();
std::cin >> verify;
QString qVerify = QString::fromStdString(verify);
if (qVerify.toLower() != "delete")
return;
}
NoteTable ntable(global.db);
for (int i=0; i<lids.size(); i++) {
ntable.deleteNote(lids[i],true);
}
}

}


void ExtractNotes::backupDB() {
if (outputFile.trimmed() == "") {
std::cout << QString(tr("Output file not specified.")).toStdString() << endl;
return;
}
ExportData exports(backup, true, this);
exports.backupData(this->outputFile);
}
48 changes: 48 additions & 0 deletions cmdtools/extractnotes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*********************************************************************************
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.
***********************************************************************************/



#ifndef EXTRACTNOTES_H
#define EXTRACTNOTES_H

#include <QObject>

class ExtractNotes : public QObject
{
Q_OBJECT
public:
explicit ExtractNotes(QObject *parent = 0);
QList<qint32> lids;
QString query;
QString outputFile;
bool backup;
bool deleteAfterExtract;
bool verifyDelete;

void extract();
void backupDB();

signals:

public slots:

};

#endif // EXTRACTNOTES_H
78 changes: 76 additions & 2 deletions settings/startupconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ StartupConfig::StartupConfig()
delNote = NULL;
email = NULL;
extractText = NULL;
exportNotes = NULL;
}


Expand Down Expand Up @@ -121,8 +122,19 @@ void StartupConfig::printHelp() {
+QString(" --to=\"<address list>\" List of recipients for the email.\n")
+QString(" --cc=\"<address list>\" List of recipients to carbon copy.\n")
+QString(" --bcc=\"<address list>\" List of recipients to blind carbon copy.\n")
+QString(" --note=\"<note>.\" Additional comments.\n")
+QString(" --note=\"<note>\" Additional comments.\n")
+QString(" --ccSelf Send a copy to yourself.\n")
+QString(" --accountId=<id> Account number (defaults to last used account).\n")
+QString(" backup <options> Backup the NixNote database.\n")
+QString(" backup options:\n")
+QString(" --output=<filename> Output filename.\n")
+QString(" --accountId=<id> Account number (defaults to last used account).\n")
+QString(" export <options> Export notes from NixNote.\n")
+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(" --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(" Examples:\n\n")
+QString(" To Start NixNote, do a sync, and then exit.\n")
Expand All @@ -132,7 +144,9 @@ void StartupConfig::printHelp() {
+QString(" To add a note to the notebook \"My Notebook\"\n")
+QString(" nixnote2 addNote --notebook=\"My Stuff\" --title=\"My New Note\" --tag=\"Tag1\" --tag=\"Tag2\" --noteText=\"My Note Text\"\n\n")
+QString(" Query notes for the search text. Results show the ID, note title (padded to 10 characters but truncated longer) and the notebook\n")
+QString(" nixnote2 query --search=\"Famous Authors\" --delimiter=\" * \" --display=\"\%i%t10:%n\"\n")
+QString(" nixnote2 query --search=\"Famous Authors\" --delimiter=\" * \" --display=\"\%i%t10:%n\"\n\n")
+QString(" To extract all notes in the \"Notes\" notebook.\n")
+QString(" nixnote2 export --search=\"notebook:notes\" --output=/home/joe/exports.nnex\n\n")
+QString("\n\n")
);

Expand Down Expand Up @@ -160,6 +174,18 @@ int StartupConfig::init(int argc, char *argv[]) {
if (email == NULL)
email = new EmailNote();
}
if (parm.startsWith("export")) {
command->setBit(STARTUP_EXPORT,true);
if (exportNotes == NULL)
exportNotes = new ExtractNotes();
exportNotes->backup=false;
}
if (parm.startsWith("backup")) {
command->setBit(STARTUP_BACKUP,true);
if (exportNotes == NULL)
exportNotes = new ExtractNotes();
exportNotes->backup=true;
}
if (parm.startsWith("query")) {
command->setBit(STARTUP_QUERY);
if (queryNotes == NULL)
Expand Down Expand Up @@ -289,6 +315,45 @@ int StartupConfig::init(int argc, char *argv[]) {
delNote->lid = parm.toInt();
}
}
if (command->at(STARTUP_EXPORT)) {
if (parm.startsWith("--accountId=", Qt::CaseSensitive)) {
parm = parm.mid(12);
accountId = parm.toInt();
}
if (parm.startsWith("--id=", Qt::CaseSensitive)) {
parm = parm.mid(5);
QRegExp regExp("[ ,;]");
QStringList tokens = parm.split(regExp);
for (int i=0; i<tokens.size(); i++) {
if (tokens[i].trimmed() != "")
exportNotes->lids.append(tokens[i].toInt());
}
}
if (parm.startsWith("--deleteAfterExport", Qt::CaseSensitive)) {
exportNotes->deleteAfterExtract=true;
}
if (parm.startsWith("--noVerifyDelete", Qt::CaseSensitive)) {
exportNotes->verifyDelete=false;
}
if (parm.startsWith("--search=", Qt::CaseSensitive)) {
parm = parm.mid(9);
exportNotes->query = parm;
}
if (parm.startsWith("--output=", Qt::CaseSensitive)) {
parm = parm.mid(9);
exportNotes->outputFile = parm;
}
}
if (command->at(STARTUP_BACKUP)) {
if (parm.startsWith("--accountId=", Qt::CaseSensitive)) {
parm = parm.mid(12);
accountId = parm.toInt();
}
if (parm.startsWith("--output=", Qt::CaseSensitive)) {
parm = parm.mid(9);
exportNotes->outputFile = parm;
}
}
if (command->at(STARTUP_READNOTE)) {
if (parm.startsWith("--accountId=", Qt::CaseSensitive)) {
parm = parm.mid(12);
Expand Down Expand Up @@ -386,3 +451,12 @@ bool StartupConfig::readNote() {
bool StartupConfig::emailNote() {
return command->at(STARTUP_EMAILNOTE);
}


bool StartupConfig::exports() {
return command->at(STARTUP_EXPORT);
}

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

#define STARTUP_GUI 0
#define STARTUP_SYNC 1
Expand Down Expand Up @@ -77,6 +78,7 @@ class StartupConfig
EmailNote *email;
CmdLineQuery *queryNotes;
ExtractNoteText *extractText;
ExtractNotes *exportNotes;
bool gui();
bool sync();
bool addNote();
Expand All @@ -86,6 +88,8 @@ class StartupConfig
bool deleteNote();
bool emailNote();
bool readNote();
bool exports();
bool backup();
void setSyncAndExit();

int init(int argc, char *argv[]);
Expand Down
Loading

0 comments on commit e589b69

Please sign in to comment.