2018-03-09 22:59:12 +02:00
|
|
|
const { createStore, applyMiddleware } = require('redux');
|
|
|
|
const { reducer, defaultState, stateUtils } = require('lib/reducer.js');
|
|
|
|
const { JoplinDatabase } = require('lib/joplin-database.js');
|
|
|
|
const { Database } = require('lib/database.js');
|
|
|
|
const { FoldersScreenUtils } = require('lib/folders-screen-utils.js');
|
|
|
|
const { DatabaseDriverNode } = require('lib/database-driver-node.js');
|
|
|
|
const BaseModel = require('lib/BaseModel.js');
|
|
|
|
const Folder = require('lib/models/Folder.js');
|
|
|
|
const BaseItem = require('lib/models/BaseItem.js');
|
|
|
|
const Note = require('lib/models/Note.js');
|
|
|
|
const Tag = require('lib/models/Tag.js');
|
|
|
|
const Setting = require('lib/models/Setting.js');
|
|
|
|
const { Logger } = require('lib/logger.js');
|
|
|
|
const { splitCommandString } = require('lib/string-utils.js');
|
|
|
|
const { sprintf } = require('sprintf-js');
|
|
|
|
const { reg } = require('lib/registry.js');
|
|
|
|
const { time } = require('lib/time-utils.js');
|
|
|
|
const BaseSyncTarget = require('lib/BaseSyncTarget.js');
|
|
|
|
const { fileExtension } = require('lib/path-utils.js');
|
|
|
|
const { shim } = require('lib/shim.js');
|
|
|
|
const { _, setLocale, defaultLocale, closestSupportedLocale } = require('lib/locale.js');
|
2018-05-09 11:49:31 +02:00
|
|
|
const reduxSharedMiddleware = require('lib/components/shared/reduxSharedMiddleware');
|
2018-03-09 22:59:12 +02:00
|
|
|
const os = require('os');
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
const JoplinError = require('lib/JoplinError');
|
|
|
|
const EventEmitter = require('events');
|
2018-06-20 02:18:58 +02:00
|
|
|
const syswidecas = require('syswide-cas');
|
2018-03-09 22:59:12 +02:00
|
|
|
const SyncTargetRegistry = require('lib/SyncTargetRegistry.js');
|
|
|
|
const SyncTargetFilesystem = require('lib/SyncTargetFilesystem.js');
|
|
|
|
const SyncTargetOneDrive = require('lib/SyncTargetOneDrive.js');
|
|
|
|
const SyncTargetOneDriveDev = require('lib/SyncTargetOneDriveDev.js');
|
|
|
|
const SyncTargetNextcloud = require('lib/SyncTargetNextcloud.js');
|
|
|
|
const SyncTargetWebDAV = require('lib/SyncTargetWebDAV.js');
|
2018-03-26 19:33:55 +02:00
|
|
|
const SyncTargetDropbox = require('lib/SyncTargetDropbox.js');
|
2018-03-09 22:59:12 +02:00
|
|
|
const EncryptionService = require('lib/services/EncryptionService');
|
|
|
|
const DecryptionWorker = require('lib/services/DecryptionWorker');
|
2018-03-15 20:08:46 +02:00
|
|
|
const BaseService = require('lib/services/BaseService');
|
2017-11-04 14:23:46 +02:00
|
|
|
|
2017-11-24 21:21:30 +02:00
|
|
|
SyncTargetRegistry.addClass(SyncTargetFilesystem);
|
|
|
|
SyncTargetRegistry.addClass(SyncTargetOneDrive);
|
2017-11-24 21:47:24 +02:00
|
|
|
SyncTargetRegistry.addClass(SyncTargetOneDriveDev);
|
2018-01-25 21:01:14 +02:00
|
|
|
SyncTargetRegistry.addClass(SyncTargetNextcloud);
|
2018-02-02 01:40:05 +02:00
|
|
|
SyncTargetRegistry.addClass(SyncTargetWebDAV);
|
2018-03-26 19:33:55 +02:00
|
|
|
SyncTargetRegistry.addClass(SyncTargetDropbox);
|
2017-11-24 21:21:30 +02:00
|
|
|
|
2017-11-04 14:23:46 +02:00
|
|
|
class BaseApplication {
|
2018-03-09 22:59:12 +02:00
|
|
|
|
2017-11-04 14:23:46 +02:00
|
|
|
constructor() {
|
|
|
|
this.logger_ = new Logger();
|
|
|
|
this.dbLogger_ = new Logger();
|
|
|
|
this.eventEmitter_ = new EventEmitter();
|
|
|
|
|
2018-09-04 06:42:22 +02:00
|
|
|
// Note: this is basically a cache of state.selectedFolderId. It should *only*
|
2017-11-04 14:23:46 +02:00
|
|
|
// be derived from the state and not set directly since that would make the
|
|
|
|
// state and UI out of sync.
|
2018-09-04 06:42:22 +02:00
|
|
|
this.currentFolder_ = null;
|
2017-11-04 14:23:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
logger() {
|
|
|
|
return this.logger_;
|
|
|
|
}
|
|
|
|
|
|
|
|
store() {
|
|
|
|
return this.store_;
|
|
|
|
}
|
|
|
|
|
|
|
|
currentFolder() {
|
|
|
|
return this.currentFolder_;
|
|
|
|
}
|
|
|
|
|
|
|
|
async refreshCurrentFolder() {
|
|
|
|
let newFolder = null;
|
2018-09-04 06:42:22 +02:00
|
|
|
|
2017-11-04 14:23:46 +02:00
|
|
|
if (this.currentFolder_) newFolder = await Folder.load(this.currentFolder_.id);
|
|
|
|
if (!newFolder) newFolder = await Folder.defaultFolder();
|
|
|
|
|
|
|
|
this.switchCurrentFolder(newFolder);
|
|
|
|
}
|
|
|
|
|
|
|
|
switchCurrentFolder(folder) {
|
2017-12-07 20:12:46 +02:00
|
|
|
if (!this.hasGui()) {
|
|
|
|
this.currentFolder_ = Object.assign({}, folder);
|
2018-03-09 22:59:12 +02:00
|
|
|
Setting.setValue('activeFolderId', folder ? folder.id : '');
|
2017-12-07 20:12:46 +02:00
|
|
|
} else {
|
|
|
|
this.dispatch({
|
2018-03-09 22:59:12 +02:00
|
|
|
type: 'FOLDER_SELECT',
|
|
|
|
id: folder ? folder.id : '',
|
2017-12-07 20:12:46 +02:00
|
|
|
});
|
|
|
|
}
|
2017-11-04 14:23:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handles the initial flags passed to main script and
|
|
|
|
// returns the remaining args.
|
2017-11-16 20:51:11 +02:00
|
|
|
async handleStartFlags_(argv, setDefaults = true) {
|
2017-11-04 14:23:46 +02:00
|
|
|
let matched = {};
|
|
|
|
argv = argv.slice(0);
|
|
|
|
argv.splice(0, 2); // First arguments are the node executable, and the node JS file
|
|
|
|
|
|
|
|
while (argv.length) {
|
|
|
|
let arg = argv[0];
|
|
|
|
let nextArg = argv.length >= 2 ? argv[1] : null;
|
2018-09-04 06:42:22 +02:00
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (arg == '--profile') {
|
|
|
|
if (!nextArg) throw new JoplinError(_('Usage: %s', '--profile <dir-path>'), 'flagError');
|
2017-11-04 14:23:46 +02:00
|
|
|
matched.profileDir = nextArg;
|
|
|
|
argv.splice(0, 2);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (arg == '--env') {
|
|
|
|
if (!nextArg) throw new JoplinError(_('Usage: %s', '--env <dev|prod>'), 'flagError');
|
2017-11-04 14:23:46 +02:00
|
|
|
matched.env = nextArg;
|
|
|
|
argv.splice(0, 2);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (arg == '--is-demo') {
|
|
|
|
Setting.setConstant('isDemo', true);
|
2017-11-04 14:23:46 +02:00
|
|
|
argv.splice(0, 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (arg == '--open-dev-tools') {
|
|
|
|
Setting.setConstant('openDevTools', true);
|
2017-11-17 20:02:01 +02:00
|
|
|
argv.splice(0, 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (arg == '--update-geolocation-disabled') {
|
2017-11-04 14:23:46 +02:00
|
|
|
Note.updateGeolocationEnabled_ = false;
|
|
|
|
argv.splice(0, 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (arg == '--stack-trace-enabled') {
|
2017-11-04 14:23:46 +02:00
|
|
|
this.showStackTraces_ = true;
|
|
|
|
argv.splice(0, 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (arg == '--log-level') {
|
|
|
|
if (!nextArg) throw new JoplinError(_('Usage: %s', '--log-level <none|error|warn|info|debug>'), 'flagError');
|
2017-11-04 14:23:46 +02:00
|
|
|
matched.logLevel = Logger.levelStringToId(nextArg);
|
|
|
|
argv.splice(0, 2);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-05-02 16:51:33 +02:00
|
|
|
if (arg.indexOf('-psn') === 0) {
|
|
|
|
// Some weird flag passed by macOS - can be ignored.
|
|
|
|
// https://github.com/laurent22/joplin/issues/480
|
|
|
|
// https://stackoverflow.com/questions/10242115
|
|
|
|
argv.splice(0, 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (arg.length && arg[0] == '-') {
|
|
|
|
throw new JoplinError(_('Unknown flag: %s', arg), 'flagError');
|
2017-11-04 14:23:46 +02:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-16 20:51:11 +02:00
|
|
|
if (setDefaults) {
|
|
|
|
if (!matched.logLevel) matched.logLevel = Logger.LEVEL_INFO;
|
2018-03-09 22:59:12 +02:00
|
|
|
if (!matched.env) matched.env = 'prod';
|
2017-11-16 20:51:11 +02:00
|
|
|
}
|
2017-11-04 14:23:46 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
matched: matched,
|
|
|
|
argv: argv,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
on(eventName, callback) {
|
|
|
|
return this.eventEmitter_.on(eventName, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
async exit(code = 0) {
|
|
|
|
await Setting.saveAll();
|
|
|
|
process.exit(code);
|
|
|
|
}
|
|
|
|
|
2017-11-12 19:37:04 +02:00
|
|
|
async refreshNotes(state) {
|
|
|
|
let parentType = state.notesParentType;
|
|
|
|
let parentId = null;
|
2018-09-04 06:42:22 +02:00
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (parentType === 'Folder') {
|
2017-11-12 19:37:04 +02:00
|
|
|
parentId = state.selectedFolderId;
|
|
|
|
parentType = BaseModel.TYPE_FOLDER;
|
2018-03-09 22:59:12 +02:00
|
|
|
} else if (parentType === 'Tag') {
|
2017-11-12 19:37:04 +02:00
|
|
|
parentId = state.selectedTagId;
|
|
|
|
parentType = BaseModel.TYPE_TAG;
|
2018-03-09 22:59:12 +02:00
|
|
|
} else if (parentType === 'Search') {
|
2017-11-12 19:37:04 +02:00
|
|
|
parentId = state.selectedSearchId;
|
|
|
|
parentType = BaseModel.TYPE_SEARCH;
|
|
|
|
}
|
2017-11-04 14:23:46 +02:00
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
this.logger().debug('Refreshing notes:', parentType, parentId);
|
2017-11-04 14:23:46 +02:00
|
|
|
|
|
|
|
let options = {
|
2018-02-22 20:58:15 +02:00
|
|
|
order: stateUtils.notesOrder(state.settings),
|
2018-03-09 22:59:12 +02:00
|
|
|
uncompletedTodosOnTop: Setting.value('uncompletedTodosOnTop'),
|
2018-05-09 22:00:05 +02:00
|
|
|
showCompletedTodos: Setting.value('showCompletedTodos'),
|
2018-02-22 20:58:15 +02:00
|
|
|
caseInsensitive: true,
|
2017-11-04 14:23:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const source = JSON.stringify({
|
|
|
|
options: options,
|
|
|
|
parentId: parentId,
|
|
|
|
});
|
|
|
|
|
|
|
|
let notes = [];
|
|
|
|
|
|
|
|
if (parentId) {
|
|
|
|
if (parentType === Folder.modelType()) {
|
|
|
|
notes = await Note.previews(parentId, options);
|
|
|
|
} else if (parentType === Tag.modelType()) {
|
2018-09-30 21:43:46 +02:00
|
|
|
notes = await Tag.notes(parentId, options);
|
2017-11-04 14:23:46 +02:00
|
|
|
} else if (parentType === BaseModel.TYPE_SEARCH) {
|
|
|
|
let fields = Note.previewFields();
|
|
|
|
let search = BaseModel.byId(state.searches, parentId);
|
|
|
|
notes = await Note.previews(null, {
|
|
|
|
fields: fields,
|
2018-03-09 22:59:12 +02:00
|
|
|
anywherePattern: '*' + search.query_pattern + '*',
|
2017-11-04 14:23:46 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.store().dispatch({
|
2018-03-09 22:59:12 +02:00
|
|
|
type: 'NOTE_UPDATE_ALL',
|
2017-11-04 14:23:46 +02:00
|
|
|
notes: notes,
|
|
|
|
notesSource: source,
|
|
|
|
});
|
|
|
|
|
|
|
|
this.store().dispatch({
|
2018-03-09 22:59:12 +02:00
|
|
|
type: 'NOTE_SELECT',
|
2017-11-08 23:22:24 +02:00
|
|
|
id: notes.length ? notes[0].id : null,
|
2017-11-04 14:23:46 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
reducerActionToString(action) {
|
|
|
|
let o = [action.type];
|
2018-03-09 22:59:12 +02:00
|
|
|
if ('id' in action) o.push(action.id);
|
|
|
|
if ('noteId' in action) o.push(action.noteId);
|
|
|
|
if ('folderId' in action) o.push(action.folderId);
|
|
|
|
if ('tagId' in action) o.push(action.tagId);
|
|
|
|
if ('tag' in action) o.push(action.tag.id);
|
|
|
|
if ('folder' in action) o.push(action.folder.id);
|
|
|
|
if ('notesSource' in action) o.push(JSON.stringify(action.notesSource));
|
|
|
|
return o.join(', ');
|
2017-11-04 14:23:46 +02:00
|
|
|
}
|
|
|
|
|
2017-11-05 02:17:48 +02:00
|
|
|
hasGui() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-07 20:12:46 +02:00
|
|
|
uiType() {
|
2018-03-09 22:59:12 +02:00
|
|
|
return this.hasGui() ? 'gui' : 'cli';
|
2017-12-07 20:12:46 +02:00
|
|
|
}
|
|
|
|
|
2017-11-10 20:58:00 +02:00
|
|
|
generalMiddlewareFn() {
|
2018-03-09 22:59:12 +02:00
|
|
|
const middleware = store => next => (action) => {
|
2017-11-10 20:58:00 +02:00
|
|
|
return this.generalMiddleware(store, next, action);
|
2018-03-09 22:59:12 +02:00
|
|
|
}
|
2017-11-04 14:23:46 +02:00
|
|
|
|
2017-11-10 20:58:00 +02:00
|
|
|
return middleware;
|
|
|
|
}
|
2017-11-04 14:23:46 +02:00
|
|
|
|
2017-11-10 20:58:00 +02:00
|
|
|
async generalMiddleware(store, next, action) {
|
2018-03-09 22:59:12 +02:00
|
|
|
this.logger().debug('Reducer action', this.reducerActionToString(action));
|
2017-11-04 14:23:46 +02:00
|
|
|
|
2017-11-10 20:58:00 +02:00
|
|
|
const result = next(action);
|
|
|
|
const newState = store.getState();
|
2018-02-22 20:58:15 +02:00
|
|
|
let refreshNotes = false;
|
2017-11-04 14:23:46 +02:00
|
|
|
|
2018-05-09 11:49:31 +02:00
|
|
|
reduxSharedMiddleware(store, next, action);
|
|
|
|
|
2018-03-20 01:04:48 +02:00
|
|
|
if (action.type == 'FOLDER_SELECT' || action.type === 'FOLDER_DELETE' || (action.type === 'SEARCH_UPDATE' && newState.notesParentType === 'Folder')) {
|
2018-03-09 22:59:12 +02:00
|
|
|
Setting.setValue('activeFolderId', newState.selectedFolderId);
|
2017-11-10 20:58:00 +02:00
|
|
|
this.currentFolder_ = newState.selectedFolderId ? await Folder.load(newState.selectedFolderId) : null;
|
2018-02-22 20:58:15 +02:00
|
|
|
refreshNotes = true;
|
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (this.hasGui() && ((action.type == 'SETTING_UPDATE_ONE' && action.key == 'uncompletedTodosOnTop') || action.type == 'SETTING_UPDATE_ALL')) {
|
2018-02-22 20:58:15 +02:00
|
|
|
refreshNotes = true;
|
|
|
|
}
|
|
|
|
|
2018-05-09 22:00:05 +02:00
|
|
|
if (this.hasGui() && ((action.type == 'SETTING_UPDATE_ONE' && action.key == 'showCompletedTodos') || action.type == 'SETTING_UPDATE_ALL')) {
|
|
|
|
refreshNotes = true;
|
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (this.hasGui() && ((action.type == 'SETTING_UPDATE_ONE' && action.key.indexOf('notes.sortOrder') === 0) || action.type == 'SETTING_UPDATE_ALL')) {
|
2018-02-22 20:58:15 +02:00
|
|
|
refreshNotes = true;
|
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (action.type == 'TAG_SELECT' || action.type === 'TAG_DELETE') {
|
2018-02-22 20:58:15 +02:00
|
|
|
refreshNotes = true;
|
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (action.type == 'SEARCH_SELECT' || action.type === 'SEARCH_DELETE') {
|
2018-02-22 20:58:15 +02:00
|
|
|
refreshNotes = true;
|
2017-11-10 20:58:00 +02:00
|
|
|
}
|
2017-11-04 14:23:46 +02:00
|
|
|
|
2018-02-22 20:58:15 +02:00
|
|
|
if (refreshNotes) {
|
2017-11-12 19:37:04 +02:00
|
|
|
await this.refreshNotes(newState);
|
2017-11-12 02:44:26 +02:00
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if ((action.type == 'SETTING_UPDATE_ONE' && (action.key == 'dateFormat' || action.key == 'timeFormat')) || (action.type == 'SETTING_UPDATE_ALL')) {
|
|
|
|
time.setDateFormat(Setting.value('dateFormat'));
|
|
|
|
time.setTimeFormat(Setting.value('timeFormat'));
|
2017-11-28 20:02:54 +02:00
|
|
|
}
|
|
|
|
|
2018-06-20 01:28:50 +02:00
|
|
|
if ((action.type == 'SETTING_UPDATE_ONE' && action.key == 'net.ignoreTlsErrors') || (action.type == 'SETTING_UPDATE_ALL')) {
|
|
|
|
// https://stackoverflow.com/questions/20082893/unable-to-verify-leaf-signature
|
|
|
|
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = Setting.value('net.ignoreTlsErrors') ? '0' : '1';
|
|
|
|
}
|
|
|
|
|
2018-06-20 02:18:58 +02:00
|
|
|
if ((action.type == 'SETTING_UPDATE_ONE' && action.key == 'net.customCertificates') || (action.type == 'SETTING_UPDATE_ALL')) {
|
|
|
|
const caPaths = Setting.value('net.customCertificates').split(',');
|
|
|
|
for (let i = 0; i < caPaths.length; i++) {
|
|
|
|
const f = caPaths[i].trim();
|
|
|
|
if (!f) continue;
|
|
|
|
syswidecas.addCAs(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if ((action.type == 'SETTING_UPDATE_ONE' && (action.key.indexOf('encryption.') === 0)) || (action.type == 'SETTING_UPDATE_ALL')) {
|
2017-12-21 21:06:08 +02:00
|
|
|
if (this.hasGui()) {
|
|
|
|
await EncryptionService.instance().loadMasterKeysFromSettings();
|
|
|
|
DecryptionWorker.instance().scheduleStart();
|
|
|
|
const loadedMasterKeyIds = EncryptionService.instance().loadedMasterKeyIds();
|
|
|
|
|
|
|
|
this.dispatch({
|
2018-03-09 22:59:12 +02:00
|
|
|
type: 'MASTERKEY_REMOVE_NOT_LOADED',
|
2017-12-21 21:06:08 +02:00
|
|
|
ids: loadedMasterKeyIds,
|
|
|
|
});
|
2018-01-08 22:25:38 +02:00
|
|
|
|
|
|
|
// Schedule a sync operation so that items that need to be encrypted
|
|
|
|
// are sent to sync target.
|
|
|
|
reg.scheduleSync();
|
2017-12-21 21:06:08 +02:00
|
|
|
}
|
2017-12-14 21:39:13 +02:00
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (action.type === 'NOTE_UPDATE_ONE') {
|
2017-11-12 20:57:59 +02:00
|
|
|
// If there is a conflict, we refresh the folders so as to display "Conflicts" folder
|
|
|
|
if (action.note && action.note.is_conflict) {
|
|
|
|
await FoldersScreenUtils.refreshFolders();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (this.hasGui() && action.type == 'SETTING_UPDATE_ONE' && action.key == 'sync.interval' || action.type == 'SETTING_UPDATE_ALL') {
|
2017-11-10 20:58:00 +02:00
|
|
|
reg.setupRecurrentSync();
|
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (this.hasGui() && action.type === 'SYNC_GOT_ENCRYPTED_ITEM') {
|
2017-12-21 21:06:08 +02:00
|
|
|
DecryptionWorker.instance().scheduleStart();
|
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
return result;
|
2017-11-04 14:23:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
dispatch(action) {
|
|
|
|
if (this.store()) return this.store().dispatch(action);
|
|
|
|
}
|
|
|
|
|
2017-11-06 20:35:04 +02:00
|
|
|
reducer(state = defaultState, action) {
|
|
|
|
return reducer(state, action);
|
|
|
|
}
|
|
|
|
|
2017-11-04 15:23:15 +02:00
|
|
|
initRedux() {
|
2017-11-10 20:58:00 +02:00
|
|
|
this.store_ = createStore(this.reducer, applyMiddleware(this.generalMiddlewareFn()));
|
2017-11-04 15:23:15 +02:00
|
|
|
BaseModel.dispatch = this.store().dispatch;
|
|
|
|
FoldersScreenUtils.dispatch = this.store().dispatch;
|
2017-11-06 23:11:15 +02:00
|
|
|
reg.dispatch = this.store().dispatch;
|
2017-11-24 01:10:55 +02:00
|
|
|
BaseSyncTarget.dispatch = this.store().dispatch;
|
2017-12-14 20:53:08 +02:00
|
|
|
DecryptionWorker.instance().dispatch = this.store().dispatch;
|
2017-11-04 15:23:15 +02:00
|
|
|
}
|
|
|
|
|
2017-11-16 20:51:11 +02:00
|
|
|
async readFlagsFromFile(flagPath) {
|
|
|
|
if (!fs.existsSync(flagPath)) return {};
|
2018-03-09 22:59:12 +02:00
|
|
|
let flagContent = fs.readFileSync(flagPath, 'utf8');
|
2017-11-16 20:51:11 +02:00
|
|
|
if (!flagContent) return {};
|
|
|
|
|
|
|
|
flagContent = flagContent.trim();
|
|
|
|
|
|
|
|
let flags = splitCommandString(flagContent);
|
2018-03-09 22:59:12 +02:00
|
|
|
flags.splice(0, 0, 'cmd');
|
|
|
|
flags.splice(0, 0, 'node');
|
2017-11-16 20:51:11 +02:00
|
|
|
|
|
|
|
flags = await this.handleStartFlags_(flags, false);
|
2018-09-04 06:42:22 +02:00
|
|
|
|
2017-11-16 20:51:11 +02:00
|
|
|
return flags.matched;
|
|
|
|
}
|
|
|
|
|
2018-05-14 12:08:33 +02:00
|
|
|
determineProfileDir(initArgs) {
|
|
|
|
if (initArgs.profileDir) return initArgs.profileDir;
|
|
|
|
|
|
|
|
if (process && process.env && process.env.PORTABLE_EXECUTABLE_DIR) return process.env.PORTABLE_EXECUTABLE_DIR + '/JoplinProfile';
|
|
|
|
|
|
|
|
return os.homedir() + '/.config/' + Setting.value('appName');
|
|
|
|
}
|
|
|
|
|
2018-05-23 13:14:38 +02:00
|
|
|
async testing() {
|
2018-05-23 15:25:59 +02:00
|
|
|
const markdownUtils = require('lib/markdownUtils');
|
2018-05-23 13:14:38 +02:00
|
|
|
const ClipperServer = require('lib/ClipperServer');
|
|
|
|
const server = new ClipperServer();
|
|
|
|
const HtmlToMd = require('lib/HtmlToMd');
|
|
|
|
const service = new HtmlToMd();
|
|
|
|
const html = await shim.fsDriver().readFile('/mnt/d/test.html');
|
2018-05-23 15:25:59 +02:00
|
|
|
let markdown = service.parse(html, { baseUrl: 'https://duckduckgo.com/' });
|
2018-05-23 13:14:38 +02:00
|
|
|
console.info(markdown);
|
|
|
|
console.info('--------------------------------------------------');
|
|
|
|
|
2018-05-23 15:25:59 +02:00
|
|
|
const imageUrls = markdownUtils.extractImageUrls(markdown);
|
|
|
|
let result = await server.downloadImages_(imageUrls);
|
|
|
|
result = await server.createResourcesFromPaths_(result);
|
2018-05-23 13:14:38 +02:00
|
|
|
console.info(result);
|
2018-05-23 15:25:59 +02:00
|
|
|
markdown = server.replaceImageUrlsByResources_(markdown, result);
|
2018-05-23 13:14:38 +02:00
|
|
|
console.info('--------------------------------------------------');
|
|
|
|
console.info(markdown);
|
|
|
|
console.info('--------------------------------------------------');
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-04 14:23:46 +02:00
|
|
|
async start(argv) {
|
2018-03-07 21:11:55 +02:00
|
|
|
let startFlags = await this.handleStartFlags_(argv);
|
2017-11-04 15:23:15 +02:00
|
|
|
|
2017-11-04 14:23:46 +02:00
|
|
|
argv = startFlags.argv;
|
|
|
|
let initArgs = startFlags.matched;
|
|
|
|
if (argv.length) this.showPromptString_ = false;
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
let appName = initArgs.env == 'dev' ? 'joplindev' : 'joplin';
|
|
|
|
if (Setting.value('appId').indexOf('-desktop') >= 0) appName += '-desktop';
|
|
|
|
Setting.setConstant('appName', appName);
|
2017-11-04 14:23:46 +02:00
|
|
|
|
2018-05-14 12:08:33 +02:00
|
|
|
const profileDir = this.determineProfileDir(initArgs);
|
2018-03-09 22:59:12 +02:00
|
|
|
const resourceDir = profileDir + '/resources';
|
|
|
|
const tempDir = profileDir + '/tmp';
|
2017-11-04 14:23:46 +02:00
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
Setting.setConstant('env', initArgs.env);
|
|
|
|
Setting.setConstant('profileDir', profileDir);
|
|
|
|
Setting.setConstant('resourceDir', resourceDir);
|
|
|
|
Setting.setConstant('tempDir', tempDir);
|
2017-11-04 14:23:46 +02:00
|
|
|
|
2018-06-18 20:56:07 +02:00
|
|
|
await shim.fsDriver().remove(tempDir);
|
|
|
|
|
2017-11-04 14:23:46 +02:00
|
|
|
await fs.mkdirp(profileDir, 0o755);
|
|
|
|
await fs.mkdirp(resourceDir, 0o755);
|
|
|
|
await fs.mkdirp(tempDir, 0o755);
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
const extraFlags = await this.readFlagsFromFile(profileDir + '/flags.txt');
|
2017-11-16 20:51:11 +02:00
|
|
|
initArgs = Object.assign(initArgs, extraFlags);
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
this.logger_.addTarget('file', { path: profileDir + '/log.txt' });
|
2017-11-04 15:23:15 +02:00
|
|
|
//this.logger_.addTarget('console');
|
2017-11-04 14:23:46 +02:00
|
|
|
this.logger_.setLevel(initArgs.logLevel);
|
|
|
|
|
|
|
|
reg.setLogger(this.logger_);
|
2018-03-09 22:59:12 +02:00
|
|
|
reg.dispatch = (o) => {};
|
2017-11-04 14:23:46 +02:00
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
this.dbLogger_.addTarget('file', { path: profileDir + '/log-database.txt' });
|
2017-11-04 14:23:46 +02:00
|
|
|
this.dbLogger_.setLevel(initArgs.logLevel);
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (Setting.value('env') === 'dev') {
|
2017-11-28 02:22:38 +02:00
|
|
|
this.dbLogger_.setLevel(Logger.LEVEL_WARN);
|
2017-11-04 14:23:46 +02:00
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
this.logger_.info('Profile directory: ' + profileDir);
|
2017-11-04 14:23:46 +02:00
|
|
|
|
|
|
|
this.database_ = new JoplinDatabase(new DatabaseDriverNode());
|
2018-03-09 22:59:12 +02:00
|
|
|
this.database_.setLogExcludedQueryTypes(['SELECT']);
|
2017-11-04 14:23:46 +02:00
|
|
|
this.database_.setLogger(this.dbLogger_);
|
2018-03-09 22:59:12 +02:00
|
|
|
await this.database_.open({ name: profileDir + '/database.sqlite' });
|
2017-11-04 14:23:46 +02:00
|
|
|
|
|
|
|
reg.setDb(this.database_);
|
|
|
|
BaseModel.db_ = this.database_;
|
|
|
|
|
|
|
|
await Setting.load();
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (Setting.value('firstStart')) {
|
2017-11-04 14:23:46 +02:00
|
|
|
const locale = shim.detectAndSetLocale(Setting);
|
2018-03-09 22:59:12 +02:00
|
|
|
reg.logger().info('First start: detected locale as ' + locale);
|
2018-03-26 19:33:55 +02:00
|
|
|
|
|
|
|
if (Setting.value('env') === 'dev') {
|
|
|
|
Setting.setValue('showTrayIcon', 0);
|
|
|
|
Setting.setValue('autoUpdateEnabled', 0);
|
|
|
|
Setting.setValue('sync.interval', 3600);
|
|
|
|
}
|
2018-04-16 19:18:28 +02:00
|
|
|
|
2018-03-26 19:33:55 +02:00
|
|
|
Setting.setValue('firstStart', 0);
|
2017-11-04 14:23:46 +02:00
|
|
|
} else {
|
2018-03-09 22:59:12 +02:00
|
|
|
setLocale(Setting.value('locale'));
|
2017-11-04 14:23:46 +02:00
|
|
|
}
|
|
|
|
|
2018-09-28 20:24:57 +02:00
|
|
|
if (!Setting.value('api.token')) {
|
|
|
|
EncryptionService.instance().randomHexString(64).then((token) => {
|
|
|
|
Setting.setValue('api.token', token);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-05 00:03:21 +02:00
|
|
|
time.setDateFormat(Setting.value('dateFormat'));
|
|
|
|
time.setTimeFormat(Setting.value('timeFormat'));
|
|
|
|
|
2018-03-15 20:08:46 +02:00
|
|
|
BaseService.logger_ = this.logger_;
|
2017-12-14 21:39:13 +02:00
|
|
|
EncryptionService.instance().setLogger(this.logger_);
|
2017-12-14 02:23:32 +02:00
|
|
|
BaseItem.encryptionService_ = EncryptionService.instance();
|
2017-12-14 21:39:13 +02:00
|
|
|
DecryptionWorker.instance().setLogger(this.logger_);
|
|
|
|
DecryptionWorker.instance().setEncryptionService(EncryptionService.instance());
|
2017-12-14 02:23:32 +02:00
|
|
|
await EncryptionService.instance().loadMasterKeysFromSettings();
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
let currentFolderId = Setting.value('activeFolderId');
|
2017-11-04 14:23:46 +02:00
|
|
|
let currentFolder = null;
|
|
|
|
if (currentFolderId) currentFolder = await Folder.load(currentFolderId);
|
|
|
|
if (!currentFolder) currentFolder = await Folder.defaultFolder();
|
2018-03-09 22:59:12 +02:00
|
|
|
Setting.setValue('activeFolderId', currentFolder ? currentFolder.id : '');
|
2017-11-04 14:23:46 +02:00
|
|
|
|
2018-05-23 13:14:38 +02:00
|
|
|
// await this.testing();process.exit();
|
|
|
|
|
2017-11-04 14:23:46 +02:00
|
|
|
return argv;
|
|
|
|
}
|
2018-03-09 22:59:12 +02:00
|
|
|
|
2017-11-04 14:23:46 +02:00
|
|
|
}
|
|
|
|
|
2018-09-04 06:42:22 +02:00
|
|
|
module.exports = { BaseApplication };
|