forked from baumgarr/nixnote2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
266 lines (227 loc) · 10.2 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*********************************************************************************
NixNote - An open-source client for the Evernote service.
Copyright (C) 2013 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 <QtGui/QApplication>
#include "nixnote.h"
#include "global.h"
#include "settings/startupconfig.h"
#include "logger/qslog.h"
#include <QtCore/QCoreApplication>
#include <QDir>
#include <iostream>
#include <QMessageBox>
#include <QSharedMemory>
#include <execinfo.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <QNetworkProxy>
#include "application.h"
using namespace std;
//*********************************************
//* This is the main entry point for NixNote.
//*********************************************
extern Global global;
//*********************************************************************
//* Segmentation fault. This is triggered to print a stack trace.
//*********************************************************************
void fault_handler(int sig) {
void *array[30];
size_t size;
// get void*'s for all entries on the stack
size = backtrace(array, 30);
// print out all the frames to stderr
fprintf(stderr, "Error: signal %d:\n", sig);
backtrace_symbols_fd(array, size, 2);
exit(1);
}
//using namespace cv;
//*********************************************************************
//* Main entry point to the program.
//*********************************************************************
int main(int argc, char *argv[])
{
signal(SIGSEGV, fault_handler); // install our handler
// Setup the QApplication so we can begin
Application *a = new Application(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;
startupConfig.accountId = -1;
for (int i=1; i<=argc; i++) {
QString parm(argv[i]);
if (parm == "--help" || parm == "-?") {
printf("\n\n");
printf("NixNote command line options:\n");
printf(" --help or -? Show this message\n");
printf(" --accountId=<id> Start with specified user account\n");
printf(" --dontStartMinimized Override option to start minimized\n");
printf(" --disableEditing Disable note editing\n");
printf(" --disableIndexing Disable all indexing\n");
printf(" --openNote=<lid> Open a specific note on startup\n");
printf(" --forceSystemTrayAvailable Force the program to accept that\n");
printf(" the desktop supports tray icons.\n");
printf(" --startMinimized Force a startup with NixNote minimized\n");
printf(" --syncAndExit Synchronize and exit the program.\n");
printf("\n\n");
return 0;
}
if (parm.startsWith("--accountId=", Qt::CaseSensitive)) {
parm = parm.mid(12);
startupConfig.accountId = parm.toInt();
}
if (parm.startsWith("--openNote=", Qt::CaseSensitive)) {
parm = parm.mid(11);
startupConfig.startupNoteLid = parm.toInt();
}
if (parm == "--disableEditing") {
startupConfig.disableEditing = true;
}
if (parm == "--dontStartMinimized") {
startupConfig.forceNoStartMinimized = true;
}
if (parm == "--startMinimized") {
startupConfig.forceStartMinimized = true;
}
if (parm == "--newNote") {
startupConfig.startupNewNote = true;
}
if (parm == "--syncAndExit") {
startupConfig.syncAndExit = true;
}
if (parm == "--disableIndexing") {
startupConfig.disableIndexing = true;
}
if (parm == "--forceSystemTrayAvailable") {
startupConfig.forceSystemTrayAvailable = true;
}
}
startupConfig.programDirPath = global.getProgramDirPath() + QDir().separator();
startupConfig.name = "NixNote";
global.setup(startupConfig);
QString logPath = global.fileManager.getLogsDirPath("")+"messages.log";
QsLogging::DestinationPtr fileDestination(
QsLogging::DestinationFactory::MakeFileDestination(logPath) ) ;
logger.addDestination(fileDestination.get());
// Show Qt version. This is useful for debugging
QLOG_DEBUG() << "Program Home: " << global.fileManager.getProgramDirPath("");
QLOG_INFO() << "Built on " << __DATE__ << " at " << __TIME__;
QLOG_INFO() << "Built with Qt" << QT_VERSION_STR << "running on" << qVersion();
//QLOG_INFO() << "Thrift version: " << PACKAGE_VERSION;
// 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.
bool memInitNeeded = true;
if( !global.sharedMemory->create( 512*1024, QSharedMemory::ReadWrite) ) {
// Attach to it and detach. This is done in case it crashed.
global.sharedMemory->attach();
global.sharedMemory->detach();
if( !global.sharedMemory->create( 512*1024, QSharedMemory::ReadWrite) ) {
if (startupConfig.startupNewNote) {
global.sharedMemory->attach();
global.sharedMemory->lock();
void *dataptr = global.sharedMemory->data();
memcpy(dataptr, "NEW_NOTE", 8); // Tell the other guy create a new note
QLOG_INFO() << "Another NixNote was found. Requesting it to start another note";
exit(0); // Exit this one
}
if (startupConfig.startupNoteLid > 0) {
global.sharedMemory->attach();
global.sharedMemory->lock();
void *dataptr = global.sharedMemory->data();
QString msg = "OPEN_NOTE:" +QString::number(startupConfig.startupNoteLid) + " ";
memcpy(dataptr, msg.toStdString().c_str(), msg.length()); // Tell the other guy to open a note
QLOG_INFO() << "Another NixNote was found. Requesting it to open a note";
exit(0); // Exit this one
}
// If we've gotten this far, we need to either stop this instance or stop the other
global.settings->beginGroup("Debugging");
QString startup = global.settings->value("onMultipleInstances", "SHOW_OTHER").toString();
global.settings->endGroup();
global.sharedMemory->attach();
global.sharedMemory->lock();
void *dataptr = global.sharedMemory->data();
if (startup == "SHOW_OTHER") {
memcpy(dataptr, "SHOW_WINDOW", 11); // Tell the other guy to show himself
QLOG_INFO() << "Another NixNote was found. Stopping this instance";
exit(0); // Exit this one
}
if (startup == "STOP_OTHER") {
memcpy(dataptr, "IMMEDIATE_SHUTDOWN", 18); // Tell the other guy to shut down
memInitNeeded = false;
}
global.sharedMemory->unlock();
}
}
if (memInitNeeded) {
global.sharedMemory->lock();
memset(global.sharedMemory->data(), 0, global.sharedMemory->size());
global.sharedMemory->unlock();
}
// Save the clipboard
global.clipboard = QApplication::clipboard();
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)
w->show();
else
w->hide();
if (global.startMinimized)
w->showMinimized();
// Setup the proxy
QNetworkProxy proxy;
proxy.setType(QNetworkProxy::HttpProxy);
if (global.isProxyEnabled()) {
QString host = global.getProxyHost();
int port = global.getProxyPort();
QString user = global.getProxyUserid();
QString password = global.getProxyPassword();
if (host.trimmed() != "")
proxy.setHostName(host.trimmed());
if (port > 0)
proxy.setPort(port);
if (user.trimmed() != "")
proxy.setUser(user.trimmed());
if (password.trimmed() != "")
proxy.setPassword(password.trimmed());
QNetworkProxy::setApplicationProxy(proxy);
}
int rc = a->exec();
QLOG_DEBUG() << "Unlocking memory";
global.sharedMemory->unlock();
QLOG_DEBUG() << "Deleting NixNote instance";
delete w;
QLOG_DEBUG() << "Quitting application instance";
a->exit(rc);
QLOG_DEBUG() << "Deleting application instance";
delete a;
QLOG_DEBUG() << "Exiting: RC=" << rc;
exit(rc);
return rc;
}