Menu

[c54480]: / main.cpp  Maximize  Restore  History

Download this file

579 lines (497 with data), 19.6 kB

  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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
#include <QApplication>
#include <QMessageBox>
#include <cstdlib>
#include <iostream>
using namespace std;
#include "command.h"
#include "findwidget.h"
#include "findresultwidget.h"
#include "flagrow.h"
#include "flagrowobj.h"
#include "headingeditor.h"
#include "macros.h"
#include "mainwindow.h"
#include "noteeditor.h"
#include "options.h"
#include "settings.h"
#include "scripteditor.h"
#include "scriptoutput.h"
#include "shortcuts.h"
#include "taskeditor.h"
#include "taskmodel.h"
#include "version.h"
#include "warningdialog.h"
#if defined(VYM_DBUS)
#include <sys/types.h> // To retrieve PID for use in DBUS
#endif
#if defined(Q_OS_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#define getpid GetCurrentProcessId
#else
#include <unistd.h>
#endif
#if defined(VYM_DBUS)
#include <QtDBus/QDBusConnection>
#include <QtDBus/QDBusError>
#endif
QString vymName;
QString vymVersion;
QString vymCodeQuality;
QString vymHome;
QString vymBuildDate;
QString vymCodeName;
QString vymInstanceName;
QString vymPlatform;
QString localeName;
QTextStream vout(stdout); // vymout - Testing for now. Flush after writing...
QStringList jiraPrefixList; // List containing URLs of Jira systems
bool jiraClientAvailable; // collabzone specific currently
bool bugzillaClientAvailable; // openSUSE specific currently
TaskModel *taskModel;
TaskEditor *taskEditor;
ScriptEditor *scriptEditor;
ScriptOutput *scriptOutput;
HeadingEditor *headingEditor;
NoteEditor *noteEditor; // used in Constr. of LinkableMapObj
// initialized in mainwindow
Main *mainWindow; // used in BranchObj::select()
FindWidget *findWidget;
FindResultWidget *findResultWidget;
Macros macros;
uint itemLastID=0; // Unique ID for all items in all models
QString tmpVymDir; // All temp files go there, created in mainwindow
QString clipboardDir; // Clipboard used in all mapEditors
QString clipboardFile; // Clipboard used in all mapEditors
uint clipboardItemCount; // Number of items in clipboard
QDir vymBaseDir; // Containing all styles, scripts, images, ...
QDir lastImageDir;
QDir lastMapDir;
QDir lastExportDir;
#if defined(Q_OS_WIN32)
QDir vymInstallDir;
#endif
QString iconPath; // Pointing to icons used for toolbars
QString flagsPath; // Pointing to flags
bool debug; // global debugging flag
bool testmode; // Used to disable saving of autosave setting
bool recoveryMode = false; // Activated via command line switch and deactivated after initial loading of files
QStringList ignoredLockedFiles;
FlagRow *systemFlagsMaster;
FlagRow *standardFlagsMaster;
Switchboard switchboard;
Settings settings ("InSilmaril","vym"); // Organization, Application name
bool zipToolAvailable = false;
bool unzipToolAvailable = false;
QString zipToolPath; // Platform dependant zip tool
QString unzipToolPath; // For windows same as zipToolPath
QList <Command*> modelCommands;
QList <Command*> vymCommands;
Options options;
ImageIO imageIO;
int statusbarTime = 10000;
int warningCount = 0;
int criticalCount = 0;
int fatalCount = 0;
QString editorFocusStyle = QString(" border-color: #3daee9; border-style:outset; border-width:3px; color:black;");
#include <QScriptEngine>
QScriptValue scriptPrint( QScriptContext * ctx, QScriptEngine * eng );
void msgHandler (QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
QByteArray localMsg = msg.toLocal8Bit();
switch (type)
{
case QtDebugMsg:
fprintf(stderr, "%s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
break;
case QtWarningMsg:
fprintf(stderr, "Warning: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
warningCount++;
break;
case QtCriticalMsg:
fprintf(stderr, "Critical: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
criticalCount++;
break;
case QtFatalMsg:
fprintf(stderr, "Fatal: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
fatalCount++;
break;
default:
fprintf(stderr, "Info: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
}
}
int main(int argc, char* argv[])
{
QApplication app(argc,argv);
// Define some constants shared in various places
vymName = __VYM_NAME;
vymVersion = __VYM_VERSION;
vymBuildDate = __VYM_BUILD_DATE;
vymCodeName = __VYM_CODENAME;
vymCodeQuality = __VYM_CODE_QUALITY;
vymHome = __VYM_HOME;
// Install our own handler for messages
qInstallMessageHandler(msgHandler);
// Testing for now
vout.setCodec("UTF-8");
// Reading and initializing options commandline options
options.add ("batch", Option::Switch, "b", "batch");
options.add ("commands", Option::Switch, "c", "commands");
options.add ("commandslatex", Option::Switch, "cl", "commandslatex");
options.add ("debug", Option::Switch, "d", "debug");
options.add ("help", Option::Switch, "h", "help");
options.add ("load", Option::String, "L", "load");
options.add ("local", Option::Switch, "l", "local");
options.add ("locale", Option::String, "locale", "locale");
options.add ("name", Option::String, "n", "name");
options.add ("quit", Option::Switch, "q", "quit");
options.add ("run", Option::String, "R", "run");
options.add ("recover", Option::Switch, "recover", "recover");
options.add ("restore", Option::Switch, "r", "restore");
options.add ("shortcuts", Option::Switch, "s", "shortcuts");
options.add ("shortcutsLaTeX", Option::Switch, "sl", "shortcutsLaTeX");
options.add ("testmode", Option::Switch, "t", "testmode");
options.add ("version", Option::Switch, "v","version");
options.setHelpText (
"VYM - View Your Mind\n"
"--------------------\n\n"
"Information about vym can be found in vym.pdf,\n"
"which should be part of the vym package.\n"
"It is also available at the project homepage:\n\n"
"http://www.InSilmaril.de/vym\n\n"
"Usage: vym [OPTION]... [FILE]... \n"
"Open FILEs with vym\n\n"
"-b batch batch mode: hide windows\n"
"-c commands List all available commands\n"
"-d debug Show debugging output\n"
"-h help Show this help text\n"
"-L load Load script\n"
"-l local Run with ressources in current directory\n"
"--locale locale Override system locale setting to select language\n"
"-n STRING name Set name of instance for DBus access\n"
"-q quit Quit immediatly after start for benchmarking\n"
"-R FILE run Run script\n"
"-r restore Restore last session\n"
"--recover recover Delete lockfiles during initial loading of files\n"
"-s shortcuts Show Keyboard shortcuts on start\n"
"--sl LaTeX Show Keyboard shortcuts in LaTeX format on start\n"
"-t testmode Test mode, e.g. no autosave and changing of its setting\n"
"-v version Show vym version\n"
);
if (options.parse())
{
cout << endl << qPrintable( options.getHelpText())<<endl;
return 1;
}
if (options.isOn ("version"))
{
cout << "VYM - View Your Mind (c) 2004-"<< QDate::currentDate().year()<<" Uwe Drechsel " << endl
<<" Version: "<<__VYM_VERSION <<endl
<<"Build date: "<<__VYM_BUILD_DATE << endl
<<" "<<__VYM_CODENAME<<endl;
return 0;
}
// Update some configurations, which were moved in 2.4.0
// This code should be removed later, e.g. in 2.6.0... // FIXME-2
QStringList settingsChanged;
settingsChanged << "readerURL"
<< "readerPDF"
<< "autosave/use"
<< "autosave/ms"
<< "writeBackupFile"
<< "printerName"
<< "printerFormat"
<< "printerFileName";
foreach (QString s, settingsChanged)
{
if (settings.contains("/mainwindow/" + s))
{
settings.setValue("/system/" + s, settings.value("/mainwindow/" + s));
settings.remove ("/mainwindow/" + s);
}
}
if (settings.contains( "/mainwindow/readerURL") )
settings.setValue( "/system/readerURL", settings.value( "/mainwindow/readerURL"));
taskModel = new TaskModel();
debug=options.isOn ("debug");
//debug=true;
testmode=options.isOn ("testmode");
QString pidString=QString ("%1").arg(getpid());
if (debug) qDebug()<< "vym PID="<<pidString;
#if defined(VYM_DBUS)
// Register for DBUS
QDBusConnection dbusConnection=QDBusConnection::sessionBus();
if (!dbusConnection.registerService ("org.insilmaril.vym-"+pidString))
{
fprintf(stderr, "%s\n",
qPrintable(QDBusConnection::sessionBus().lastError().message()));
exit(1);
}
#endif
if (options.isOn ("name"))
vymInstanceName=options.getArg ("name");
else
vymInstanceName=pidString;
bool debugBuild=false;
#ifdef QT_DEBUG
qDebug()<<"QT_DEBUG is set";
debugBuild=true;
#endif
// Use /usr/share/vym or /usr/local/share/vym or . ?
// First try options
if (options.isOn ("local") || debugBuild)
{
vymBaseDir.setPath (vymBaseDir.currentPath());
} else
// then look for environment variable
if (getenv("VYMHOME")!=0)
{
vymBaseDir.setPath (getenv("VYMHOME"));
} else
// ok, let's find my way on my own
{
#if defined (Q_OS_MACX)
// Executable is in vym.app/Contents/MacOS, so go up first:
vymBaseDir = QCoreApplication::applicationDirPath();
vymBaseDir.cdUp();
vymBaseDir.cd("Resources");
#elif defined (Q_OS_WIN32)
QString basePath;
wchar_t wbuf[512];
if (GetModuleFileName(NULL, wbuf, 512))
{
QString mfn(QString::fromWCharArray(wbuf));
mfn.replace('\\', '/');
int i=mfn.lastIndexOf('/');
if (i<0)
{
QMessageBox::critical(0,
"Error",
"Couldn't setup vymBasePath");
return 0;
}
if (mfn.right(mfn.length() -i -1) != "vym.exe")
{
QMessageBox::critical(0,
"Error",
"vym executable not known as vym.exe");
return 0;
}
basePath=mfn.left(i);
}
if (basePath.isEmpty())
basePath = vymBaseDir.currentPath();
vymInstallDir.setPath(basePath);
vymBaseDir.setPath(basePath);
#else
vymBaseDir.setPath ("/usr/share/vym");
if (!vymBaseDir.exists())
{
vymBaseDir.setPath ("/usr/local/share/vym");
if (!vymBaseDir.exists())
vymBaseDir.setPath(vymBaseDir.currentPath() );
}
#endif
}
// Platform specific settings
vymPlatform = QSysInfo::prettyProductName();
#if defined(Q_OS_WIN32)
zipToolPath = settings.value("/system/zipToolPath", "c:\\Program Files\\7-Zip\\7z.exe").toString();
#else
zipToolPath = "/usr/bin/zip";
unzipToolPath = "/usr/bin/unzip";
#endif
iconPath = vymBaseDir.path()+"/icons/";
flagsPath = vymBaseDir.path()+"/flags/";
// Some directories
QDir useDir;
if (options.isOn ("local"))
useDir = QDir().current();
else
useDir = QDir().home();
lastImageDir = useDir;
lastMapDir = useDir;
lastExportDir = useDir;
if (options.isOn ("help"))
{
cout << qPrintable (options.getHelpText())<<endl;
return 0;
}
// Initialize translations
if (options.isOn ("locale"))
{
localeName = options.getArg ("locale");
if (debug) qDebug() << "Main: using option for locale";
}
else
{
#if defined(Q_OS_LINUX)
if (debug) qDebug() << "Main: (OS Linux) using $LANG for locale";
localeName = QProcessEnvironment::systemEnvironment().value("LANG","en");
#else
if (debug) qDebug() << "Main: (OS other) using QLocale::system().uiLanguages( using for locale";
localeName = QLocale::system().uiLanguages().first();
if (localeName.contains("-"))
{
if (debug) qDebug() << "Main: Replacing '-' with '_' in locale";
localeName.replace( "-", "_");
}
#endif
}
if (debug)
{
qDebug() << "Main: localName: " << localeName;
qDebug() << "Main: translations: " << localeName, vymBaseDir.path() + "/lang";
qDebug() << "Main: uiLanguages: " << QLocale::system().uiLanguages();
qDebug() << "Main: LANG: " << QProcessEnvironment::systemEnvironment().value("LANG","foobar");
}
QTranslator vymTranslator;
if (!vymTranslator.load( QString("vym.%1").arg( localeName ), vymBaseDir.path() + "/lang") )
{
WarningDialog warn;
warn.showCancelButton (false);
warn.setText(QString( "Couldn't load translations for locale \"%1\" in\n%2")
.arg(localeName)
.arg(vymBaseDir.path() + "/lang") );
warn.setShowAgainName("mainwindow/loadTranslations");
}
app.installTranslator( &vymTranslator );
// Initializing the master rows of flags
systemFlagsMaster=new FlagRow;
systemFlagsMaster->setName ("systemFlagsMaster");
standardFlagsMaster=new FlagRow;
standardFlagsMaster->setName ("standardFlagsMaster");
// Initialize editors
noteEditor = new NoteEditor("noteeditor");
noteEditor->setWindowIcon (QPixmap (":/vym-editor.png"));
headingEditor = new HeadingEditor("headingeditor");
// Check if there is a JiraClient // FIXME-4 check for ruby
QFileInfo fi(vymBaseDir.path() + "/scripts/jigger");
jiraClientAvailable = fi.exists();
jiraPrefixList = settings.value("/system/jiraPrefixList").toStringList(); // FIXME-2 currently not used
// Check if there is a BugzillaClient // FIXME-4 check for ruby
fi.setFile( vymBaseDir.path() + "/scripts/bugger");
bugzillaClientAvailable = fi.exists();
// Initialize mainwindow
// Note: mainWindow pointer is set in constructor // FIXME-3 check this...
#if defined(Q_OS_WIN32)
Main m(0, Qt::Window | Qt::MSWindowsOwnDC);
#else
Main m;
#endif
// Check for zip tools
checkZipTool();
checkUnzipTool();
#if defined(Q_OS_WIN32)
if (!zipToolAvailable)
{
QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
QObject::tr("Couldn't find tool to unzip data. "
"Please download and install 7z and set "
"path in Settings menu:\n ","zip tool missing on Win platform") +
"http://www.7-zip.org/");
m.settingsZipTool();
}
#else
if (!zipToolAvailable || !unzipToolAvailable)
{
QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
QObject::tr("Couldn't find tool to zip/unzip data. "
"Please install on your platform and set"
"path in Settings menu:\n ","zip tool missing on Linux/Mac platform"));
m.settingsZipTool();
}
#endif
m.setWindowIcon (QPixmap (":/vym.png"));
m.fileNew();
if (options.isOn ("commands"))
{
cout << "Available commands:\n";
cout << "==================:\n";
foreach (Command* c, modelCommands)
cout << c->getDescription().toStdString() << endl;
foreach (Command* c, vymCommands)
cout << c->getDescription().toStdString() << endl;
return 0;
}
if (options.isOn ("commandslatex"))
{
foreach (Command* c, modelCommands)
cout << c->getDescriptionLaTeX().toStdString() << endl;
foreach (Command* c, vymCommands)
cout << c->getDescriptionLaTeX().toStdString() << endl;
return 0;
}
if (options.isOn ("batch"))
m.hide();
else
{
// Paint Mainwindow first time
qApp->processEvents();
m.show();
}
// Show release notes, if not already done
m.checkReleaseNotes();
// Check for updates
m.checkUpdates();
if (options.isOn("shortcuts")) switchboard.printASCII(); //FIXME-3 global switchboard and exit after listing
if (options.isOn ("recover"))
recoveryMode = true;
m.loadCmdLine();
// Restore last session
if (options.isOn ("restore"))
m.fileRestoreSession();
// By now all files should have been loaded
// Reset the restore flag and display message if needed
if (ignoredLockedFiles.count() > 0 )
{
QString msg (QObject::tr("Existing lockfiles have been ignored for the maps listed below. Please check, if the maps might be openend in another instance of vym.\n\n"));
QMessageBox::warning(0, QObject::tr("Warning"), msg + ignoredLockedFiles.join("\n"));
}
recoveryMode = false;
ignoredLockedFiles.clear();
// Load script
if (options.isOn ("load"))
{
QString fn = options.getArg ("load");
if (!scriptEditor->loadScript ( fn ) )
{
QString error (QObject::tr("Error"));
QString msg (QObject::tr("Couldn't open \"%1\"\n.").arg(fn));
if (options.isOn("batch"))
qWarning () << error + ": " + msg;
else QMessageBox::warning(0, error, msg);
return 0;
}
}
// Run script
if (options.isOn ("run"))
{
QString script;
QString fn = options.getArg ("run");
if (!scriptEditor->loadScript ( fn ) )
{
QString error (QObject::tr("Error"));
QString msg (QObject::tr("Couldn't open \"%1\"\n.").arg(fn));
if (options.isOn("batch"))
qWarning () << error + ": " + msg;
else QMessageBox::warning(0, error, msg);
return 0;
}
m.runScript (scriptEditor->getScriptFile() );
}
// For benchmarking we may want to quit instead of entering event loop
if (options.isOn ("quit")) return 0;
// Enable some last minute cleanup
QObject::connect( &app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()) );
app.exec();
// Cleanup
delete noteEditor;
int s = warningCount + criticalCount + fatalCount;
if ( s > 0 )
qDebug() << "vym exiting with:\n" <<
warningCount << " warning messages\n" <<
criticalCount<<" critical messages\n" <<
fatalCount<<" fatal messages";
return s;
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.