Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Rework command line parameters so GUI is not needed for non-GUI opera…
…tions.
  • Loading branch information
baumgarr committed Aug 28, 2016
1 parent 76e5e29 commit 59893b1
Show file tree
Hide file tree
Showing 11 changed files with 188 additions and 104 deletions.
36 changes: 34 additions & 2 deletions cmdtools/cmdlinetool.cpp
Expand Up @@ -33,6 +33,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "utilities/nuuid.h"
#include "email/smtpclient.h"
#include "utilities/mimereference.h"
#include "threads/syncrunner.h"


extern Global global;
Expand All @@ -56,8 +57,7 @@ int CmdLineTool::run(StartupConfig &config) {
if (config.sync()) {
// If the shared memory segment doesn't exist, we just do a sync & exit
if (!global.sharedMemory->attach()) {
config.setSyncAndExit();
return 16;
return this->sync(config);
}
global.sharedMemory->write(QString("SNCHRONIZE"));
global.sharedMemory->detach();
Expand Down Expand Up @@ -733,3 +733,35 @@ int CmdLineTool::closeNotebook(StartupConfig config) {
}
return 0;
}


#include "models/notemodel.h"
#include "sql/nsqlquery.h"

// Do a sync
int CmdLineTool::sync(StartupConfig config) {
if (!global.accountsManager->oauthTokenFound()) {
std::cout << "OAuth token not found." << endl;
return 16;
}

global.db = new DatabaseConnection("nixnote"); // Startup the database

// Check if the table exists. If not, create it.
NSqlQuery sql(global.db);
sql.exec("Select * from sqlite_master where type='table' and name='NoteTable';");
if (!sql.next()) {
NoteModel model(this);
model.createTable();
}
sql.finish();

SyncRunner runner;
runner.synchronize();
if (runner.error) {
std::cout << "Erorr synchronizing with Evernote." << endl;
return 16;
}
std::cout << "Sync completed." << std::endl;
return 0;
}
1 change: 1 addition & 0 deletions cmdtools/cmdlinetool.h
Expand Up @@ -44,6 +44,7 @@ class CmdLineTool : public QObject
int importNotes(StartupConfig config);
int openNotebook(StartupConfig config);
int closeNotebook(StartupConfig config);
int sync(StartupConfig config);

signals:

Expand Down
9 changes: 5 additions & 4 deletions global.cpp
Expand Up @@ -75,7 +75,8 @@ Global::~Global() {


//Initial global settings setup
void Global::setup(StartupConfig startupConfig) {
void Global::setup(StartupConfig startupConfig, bool guiAvailable) {
this->guiAvailable = guiAvailable;
fileManager.setup(startupConfig.homeDirPath, startupConfig.programDirPath, startupConfig.accountId);
shortcutKeys = new ShortcutKeys();
QString settingsFile = fileManager.getHomeDirPath("") + "nixnote.conf";
Expand All @@ -102,7 +103,7 @@ void Global::setup(StartupConfig startupConfig) {
this->forceNoStartMimized = startupConfig.forceNoStartMinimized;
this->forceSystemTrayAvailable = startupConfig.forceSystemTrayAvailable;
this->startupNewNote = startupConfig.startupNewNote;
this->syncAndExit = startupConfig.syncAndExit;
//this->syncAndExit = startupConfig.syncAndExit;
this->forceStartMinimized = startupConfig.forceStartMinimized;
this->startupNote = startupConfig.startupNoteLid;
startupConfig.accountId = accountId;
Expand Down Expand Up @@ -153,7 +154,7 @@ void Global::setup(StartupConfig startupConfig) {
disableEditing = true;
settings->endGroup();

if (defaultFont != "" && defaultFontSize > 0) {
if (defaultFont != "" && defaultFontSize > 0 && this->guiAvailable) {
QWebSettings *settings = QWebSettings::globalSettings();
settings->setFontFamily(QWebSettings::StandardFont, defaultFont);
// QWebkit DPI is hard coded to 96. Hence, we calculate the correct
Expand All @@ -162,7 +163,7 @@ void Global::setup(StartupConfig startupConfig) {
defaultFontSize * (QApplication::desktop()->logicalDpiX() / 96.0)
);
}
if (defaultFont != "" && defaultFontSize <= 0) {
if (defaultFont != "" && defaultFontSize <= 0 && this->guiAvailable) {
QWebSettings *settings = QWebSettings::globalSettings();
settings->setFontFamily(QWebSettings::StandardFont, defaultFont);
}
Expand Down
6 changes: 3 additions & 3 deletions global.h
Expand Up @@ -117,7 +117,7 @@ class Global
char** argv; // List of arguments from the program start
FileManager fileManager; // Manage file paths
AccountsManager *accountsManager; // Manage user account
Application *application; // pointer to this current application
QCoreApplication *application; // pointer to this current application
unsigned int cryptCounter; // Count of crytpographic entries. This is incremented each time we encrypt some text.
QString attachmentNameDelimeter; // Delimeter between attachment ID & name
string username; // This is probably obsolete
Expand Down Expand Up @@ -170,7 +170,6 @@ class Global
bool forceSystemTrayAvailable; // Override QSystemTrayIcon::isSystemTrayAvailable()
bool forceStartMinimized; // Force it to start minimized, despiet the user's settings
bool startMinimized; // Do user prefernces say to start minimized?
bool syncAndExit; // Should we just start, do a sync, and then quit?
bool forceWebFonts;
qint32 startupNote; // Initial note to startup with.

Expand All @@ -187,7 +186,8 @@ class Global

QHash<qint32, NoteCache*> cache; // Note cache used to keep from needing to re-format the same note for a display

void setup(StartupConfig config); // Setup the global variables
void setup(StartupConfig config, bool guiAvailable); // Setup the global variables
bool guiAvailable; // Is there a GUI available?
QString full_username; // current username
bool autosetUsername(); // Should the username be set automatically?
void setAutosetUsername(bool value);
Expand Down
66 changes: 34 additions & 32 deletions html/enmlformatter.cpp
Expand Up @@ -274,40 +274,42 @@ QByteArray EnmlFormatter::rebuildNoteEnml() {
content.append("</html>");
content = fixEncryptionTags(content);

QWebPage page;
QEventLoop loop;
page.mainFrame()->setContent(content);
QObject::connect(&page, SIGNAL(loadFinished(bool)), &loop, SLOT(quit()));
loop.exit();

QWebElement element = page.mainFrame()->documentElement();
QStringList tags = findAllTags(element);

for (int i=0; i<tags.size(); i++) {
QString tag = tags[i];
QWebElementCollection anchors = page.mainFrame()->findAllElements(tag);
foreach (QWebElement element, anchors) {
//QLOG_DEBUG() << "Processing tag name: " << element.tagName();
if (element.tagName().toLower() == "input") {
processTodo(element);
} else if (element.tagName().toLower() == "a") {
fixLinkNode(element);
} else if (element.tagName().toLower() == "object") {
fixObjectNode(element);
} else if (element.tagName().toLower() == "img") {
fixImgNode(element);
} else if (element.tagName().toLower() == "span"){
fixSpanNode(element);
} else if (element.tagName().toLower() == "div") {
fixDivNode(element);
} else if (element.tagName().toLower() == "pre") {
fixPreNode(element);
} else if (!isElementValid(element))
element.removeFromDocument();
if (global.guiAvailable) {
QWebPage page;
QEventLoop loop;
page.mainFrame()->setContent(content);
QObject::connect(&page, SIGNAL(loadFinished(bool)), &loop, SLOT(quit()));
loop.exit();

QWebElement element = page.mainFrame()->documentElement();
QStringList tags = findAllTags(element);

for (int i=0; i<tags.size(); i++) {
QString tag = tags[i];
QWebElementCollection anchors = page.mainFrame()->findAllElements(tag);
foreach (QWebElement element, anchors) {
//QLOG_DEBUG() << "Processing tag name: " << element.tagName();
if (element.tagName().toLower() == "input") {
processTodo(element);
} else if (element.tagName().toLower() == "a") {
fixLinkNode(element);
} else if (element.tagName().toLower() == "object") {
fixObjectNode(element);
} else if (element.tagName().toLower() == "img") {
fixImgNode(element);
} else if (element.tagName().toLower() == "span"){
fixSpanNode(element);
} else if (element.tagName().toLower() == "div") {
fixDivNode(element);
} else if (element.tagName().toLower() == "pre") {
fixPreNode(element);
} else if (!isElementValid(element))
element.removeFromDocument();
}
}
content.clear();
content.append(element.toOuterXml());
}
content.clear();
content.append(element.toOuterXml());

// Strip off HTML header
index = content.indexOf("<body");
Expand Down
53 changes: 31 additions & 22 deletions main.cpp
Expand Up @@ -23,9 +23,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "global.h"
#include "settings/startupconfig.h"
#include "cmdtools/cmdlinetool.h"
//#include "cmdtools/cmdlineapp.h"

#include "logger/qslog.h"
#include <QCoreApplication>
#include <QDir>
#include <iostream>
#include <QMessageBox>
Expand Down Expand Up @@ -72,37 +72,49 @@ void fault_handler(int sig) {
//*********************************************************************
int main(int argc, char *argv[])
{

bool guiAvailable = true;
signal(SIGSEGV, fault_handler); // install our handler

// Setup the QApplication so we can begin
Application *a = new Application(argc, argv);
// Begin setting up the environment
StartupConfig startupConfig;
global.argc = argc;
global.argv = argv;

int retval = startupConfig.init(argc,argv, guiAvailable);
if (retval != 0)
return retval;

// Setup the application. If we have a GUI, then we use Application.
// If we don't, then we just use a derivitive of QCoreApplication
QCoreApplication *a = NULL;
if (guiAvailable) {
Application *app = new Application(argc, argv);
a = app;
} else {
a = new QCoreApplication(argc, argv);
}
global.application = a;

// Setup the QLOG functions for debugging & messages
QsLogging::Logger& logger = QsLogging::Logger::instance();
logger.setLoggingLevel(QsLogging::TraceLevel);
// const QString sLogPath(a->applicationDirPath());

QsLogging::DestinationPtr debugDestination(
QsLogging::DestinationFactory::MakeDebugOutputDestination() );
logger.addDestination(debugDestination.get());

// Begin setting up the environment
StartupConfig startupConfig;
global.argc = argc;
global.argv = argv;

int retval = startupConfig.init(argc,argv);
if (retval != 0)
return retval;

startupConfig.programDirPath = global.getProgramDirPath() + QDir().separator();
startupConfig.name = "NixNote";
global.setup(startupConfig);
global.setup(startupConfig, guiAvailable);
// global.syncAndExit=startupConfig.syncAndExit;

// We were passed a SQL command
if (startupConfig.sqlExec) {
DatabaseConnection *db = new DatabaseConnection("nixnote"); // Startup the database
QLOG_DEBUG() << "Starting DB";
QSqlQuery query(db->conn);
QLOG_DEBUG() << "After DB Start";
if (!query.exec(startupConfig.sqlString)) {
QLOG_FATAL() << query.lastError();
delete db;
Expand All @@ -121,7 +133,6 @@ int main(int argc, char *argv[])
}



// If we want something other than the GUI, try let the CmdLineTool deal with it.
if (!startupConfig.gui()) {
global.purgeTemporaryFilesOnShutdown=startupConfig.purgeTemporaryFiles;
Expand All @@ -130,9 +141,9 @@ int main(int argc, char *argv[])
int retval = cmdline.run(startupConfig);
if (global.sharedMemory->isAttached())
global.sharedMemory->detach();
if (!startupConfig.gui())
return retval;
global.syncAndExit=startupConfig.syncAndExit;
QLOG_DEBUG() << "Exiting: RC=" << retval;
delete a;
exit(retval);
}

QString logPath = global.fileManager.getLogsDirPath("")+"messages.log";
Expand All @@ -151,13 +162,11 @@ int main(int argc, char *argv[])
// Create a shared memory region. We use this to communicate
// with any other instance that may be running. If another instance
// is found we need to either show that one or kill this one.
QLOG_DEBUG() << "Creating shared segment";
bool memInitNeeded = true;
if( !global.sharedMemory->allocate(512*1024) ) {
// Attach to it and detach. This is done in case it crashed.
global.sharedMemory->attach();
global.sharedMemory->detach();
QLOG_DEBUG() << "Creating segment";
if( !global.sharedMemory->allocate(512*1024) ) {
QLOG_DEBUG() << "segment created";
if (startupConfig.startupNewNote) {
Expand Down Expand Up @@ -198,11 +207,10 @@ int main(int argc, char *argv[])
// Save the clipboard
global.clipboard = QApplication::clipboard();

QLOG_DEBUG() << "Setting up NN";
NixNote *w = new NixNote();
w->setAttribute(Qt::WA_QuitOnClose);
bool show = true;
if (global.syncAndExit)
show = false;
if (global.minimizeToTray() && global.startMinimized)
show = false;
if (show)
Expand Down Expand Up @@ -233,6 +241,7 @@ int main(int argc, char *argv[])
QNetworkProxy::setApplicationProxy(proxy);
}

QLOG_DEBUG() << "Launching";
int rc = a->exec();
if (global.sharedMemory->isAttached())
global.sharedMemory->detach();
Expand Down
42 changes: 21 additions & 21 deletions models/notemodel.cpp
Expand Up @@ -94,27 +94,27 @@ void NoteModel::createTable() {
QString("thumbnail default null") +
QString(")"));
if (!sql.exec(command) ||
!sql.exec("CREATE INDEX NoteTable_Title_Index on NoteTable (title)") ||
!sql.exec("CREATE INDEX NoteTable_Author_Index on NoteTable (author)") ||
!sql.exec("CREATE INDEX NoteTable_Notebook_Index on NoteTable (notebook)") ||
!sql.exec("CREATE INDEX NoteTable_Notebook_Lid_Index on NoteTable (notebookLid)") ||
!sql.exec("CREATE INDEX NoteTable_DateCreated_Index on NoteTable (dateCreated)") ||
!sql.exec("CREATE INDEX NoteTable_DateUpdated_Index on NoteTable (dateUpdated)") ||
!sql.exec("CREATE INDEX NoteTable_Date_Subject_Index on NoteTable (dateSubject)") ||
!sql.exec("CREATE INDEX NoteTable_Date_Deleted_Index on NoteTable (dateDeleted)") ||
!sql.exec("CREATE INDEX NoteTable_Source_Index on NoteTable (source)") ||
!sql.exec("CREATE INDEX NoteTable_Source_Url_Index on NoteTable (sourceUrl)") ||
!sql.exec("CREATE INDEX NoteTable_Source_Application_Index on NoteTable (sourceApplication)") ||
!sql.exec("CREATE INDEX NoteTable_Latitude_Index on NoteTable (latitude)") ||
!sql.exec("CREATE INDEX NoteTable_Longitude_Index on NoteTable (longitude)") ||
!sql.exec("CREATE INDEX NoteTable_Altitude_Index on NoteTable (altitude)") ||
!sql.exec("CREATE INDEX NoteTable_Has_Encryption_Index on NoteTable (hasEncryption)") ||
!sql.exec("CREATE INDEX NoteTable_Has_Todo_Index on NoteTable (hasTodo)") ||
!sql.exec("CREATE INDEX NoteTable_Is_Dirty_Index on NoteTable (isDirty)") ||
!sql.exec("CREATE INDEX NoteTable_Reminder_Order_Index on NoteTable (reminderOrder)") ||
!sql.exec("CREATE INDEX NoteTable_Reminder_Time_Index on NoteTable (reminderTime)") ||
!sql.exec("CREATE INDEX NoteTable_isPinned_Index on NoteTable (isPinned)") ||
!sql.exec("CREATE INDEX NoteTable_Reminder_Done_Time_Index on NoteTable (reminderDoneTime)")
!sql.exec("CREATE INDEX if not exists NoteTable_Title_Index on NoteTable (title)") ||
!sql.exec("CREATE INDEX if not exists NoteTable_Author_Index on NoteTable (author)") ||
!sql.exec("CREATE INDEX if not exists NoteTable_Notebook_Index on NoteTable (notebook)") ||
!sql.exec("CREATE INDEX if not exists NoteTable_Notebook_Lid_Index on NoteTable (notebookLid)") ||
!sql.exec("CREATE INDEX if not exists NoteTable_DateCreated_Index on NoteTable (dateCreated)") ||
!sql.exec("CREATE INDEX if not exists NoteTable_DateUpdated_Index on NoteTable (dateUpdated)") ||
!sql.exec("CREATE INDEX if not exists NoteTable_Date_Subject_Index on NoteTable (dateSubject)") ||
!sql.exec("CREATE INDEX if not exists NoteTable_Date_Deleted_Index on NoteTable (dateDeleted)") ||
!sql.exec("CREATE INDEX if not exists NoteTable_Source_Index on NoteTable (source)") ||
!sql.exec("CREATE INDEX if not exists NoteTable_Source_Url_Index on NoteTable (sourceUrl)") ||
!sql.exec("CREATE INDEX if not exists NoteTable_Source_Application_Index on NoteTable (sourceApplication)") ||
!sql.exec("CREATE INDEX if not exists NoteTable_Latitude_Index on NoteTable (latitude)") ||
!sql.exec("CREATE INDEX if not exists NoteTable_Longitude_Index on NoteTable (longitude)") ||
!sql.exec("CREATE INDEX if not exists NoteTable_Altitude_Index on NoteTable (altitude)") ||
!sql.exec("CREATE INDEX if not exists NoteTable_Has_Encryption_Index on NoteTable (hasEncryption)") ||
!sql.exec("CREATE INDEX if not exists NoteTable_Has_Todo_Index on NoteTable (hasTodo)") ||
!sql.exec("CREATE INDEX if not exists NoteTable_Is_Dirty_Index on NoteTable (isDirty)") ||
!sql.exec("CREATE INDEX if not exists NoteTable_Reminder_Order_Index on NoteTable (reminderOrder)") ||
!sql.exec("CREATE INDEX if not exists NoteTable_Reminder_Time_Index on NoteTable (reminderTime)") ||
!sql.exec("CREATE INDEX if not exists NoteTable_isPinned_Index on NoteTable (isPinned)") ||
!sql.exec("CREATE INDEX if not exists NoteTable_Reminder_Done_Time_Index on NoteTable (reminderDoneTime)")
) {
QLOG_ERROR() << "Creation of NoteTable table failed: " << sql.lastError();
}
Expand Down

0 comments on commit 59893b1

Please sign in to comment.