1 #include <QApplication>
9 #include "findwidget.h"
10 #include "findresultwidget.h"
12 #include "flagrowobj.h"
13 #include "headingeditor.h"
15 #include "mainwindow.h"
16 #include "noteeditor.h"
19 #include "scripteditor.h"
20 #include "shortcuts.h"
21 #include "taskeditor.h"
22 #include "taskmodel.h"
26 #include <sys/types.h> // To retrieve PID for use in DBUS
29 #if defined(Q_OS_WIN32)
30 #define WIN32_LEAN_AND_MEAN
32 #define getpid GetCurrentProcessId
38 #include <QtDBus/QDBusConnection>
39 #include <QtDBus/QDBusError>
47 QString vymInstanceName;
51 QTextStream vout(stdout); // vymout - Testing for now. Flush after writing...
53 bool bugzillaClientAvailable; // openSUSE specific currently
56 TaskEditor *taskEditor;
57 ScriptEditor *scriptEditor;
58 HeadingEditor *headingEditor;
59 NoteEditor *noteEditor; // used in Constr. of LinkableMapObj
60 // initialized in mainwindow
61 Main *mainWindow; // used in BranchObj::select()
62 FindWidget *findWidget;
63 FindResultWidget *findResultWidget;
67 uint itemLastID=0; // Unique ID for all items in all models
69 QString tmpVymDir; // All temp files go there, created in mainwindow
70 QString clipboardDir; // Clipboard used in all mapEditors
71 QString clipboardFile; // Clipboard used in all mapEditors
72 QDir vymBaseDir; // Containing all styles, scripts, images, ...
76 #if defined(Q_OS_WIN32)
79 QString iconPath; // Pointing to icons used for toolbars
80 QString flagsPath; // Pointing to flags
81 QString macroPath; // Pointing to macros
84 bool debug; // global debugging flag
85 bool testmode; // Used to disable saving of autosave setting
86 FlagRow *systemFlagsMaster;
87 FlagRow *standardFlagsMaster;
89 Switchboard switchboard;
91 Settings settings ("InSilmaril","vym"); // Organization, Application name
92 QString zipToolPath; // Platform dependant zip tool
94 QList <Command*> modelCommands;
99 int statusbarTime=10000;
105 void msgHandler (QtMsgType type, const QMessageLogContext &context, const QString &msg)
107 QByteArray localMsg = msg.toLocal8Bit();
111 fprintf(stderr, "%s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
114 fprintf(stderr, "Warning: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
118 fprintf(stderr, "Critical: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
122 fprintf(stderr, "Fatal: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
125 fprintf(stderr, "Info: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
129 int main(int argc, char* argv[])
131 QApplication app(argc,argv);
134 vymVersion=__VYM_VERSION;
135 vymBuildDate=__VYM_BUILD_DATE;
136 vymCodeName=__VYM_CODENAME;
139 // Install our own handler for messages
140 qInstallMessageHandler(msgHandler);
143 vout.setCodec("UTF-8");
145 // Reading and initializing options commandline options
146 options.add ("batch", Option::Switch, "b", "batch");
147 options.add ("commands", Option::Switch, "c", "commands");
148 options.add ("commandslatex", Option::Switch, "cl", "commandslatex");
149 options.add ("debug", Option::Switch, "d", "debug");
150 options.add ("help", Option::Switch, "h", "help");
151 options.add ("local", Option::Switch, "l", "local");
152 options.add ("locale", Option::String, "locale", "locale");
153 options.add ("name", Option::String, "n", "name");
154 options.add ("quit", Option::Switch, "q", "quit");
155 options.add ("run", Option::String, "r", "run");
156 options.add ("restore", Option::Switch, "R", "restore");
157 options.add ("shortcuts", Option::Switch, "s", "shortcuts");
158 options.add ("shortcutsLaTeX", Option::Switch, "sl", "shortcutsLaTeX");
159 options.add ("testmode", Option::Switch, "t", "testmode");
160 options.add ("version", Option::Switch, "v","version");
161 options.setHelpText (
162 "VYM - View Your Mind\n"
163 "--------------------\n\n"
164 "Information about vym can be found in vym.pdf,\n"
165 "which should be part of the vym package.\n"
166 "It is also available at the project homepage:\n\n"
167 "http://www.InSilmaril.de/vym\n\n"
168 "Usage: vym [OPTION]... [FILE]... \n"
169 "Open FILEs with vym\n\n"
170 "-b batch batch mode: hide windows\n"
171 "-c commands List all available commands\n"
172 "-d debug Show debugging output\n"
173 "-h help Show this help text\n"
174 "-l local Run with ressources in current directory\n"
175 "--locale locale Override system locale setting to select language\n"
176 "-n STRING name Set name of instance for DBus access\n"
177 "-q quit Quit immediatly after start for benchmarking\n"
178 "-r FILE run Run script\n"
179 "-R restore Restore last session\n"
180 "-s shortcuts Show Keyboard shortcuts on start\n"
181 "--sl LaTeX Show Keyboard shortcuts in LaTeX format on start\n"
182 "-t testmode Test mode, e.g. no autosave and changing of its setting\n"
183 "-v version Show vym version\n"
188 cout << endl << qPrintable( options.getHelpText())<<endl;
192 if (options.isOn ("version"))
194 cout << "VYM - View Your Mind (c) 2004-"<< QDate::currentDate().year()<<" Uwe Drechsel " << endl
195 <<" Version: "<<__VYM_VERSION <<endl
196 <<"Build date: "<<__VYM_BUILD_DATE << endl
197 <<" "<<__VYM_CODENAME<<endl;
202 // Update some configurations, which were moved in 2.4.0
203 // This code should be removed later, e.g. in 2.6.0...
204 QStringList settingsChanged;
205 settingsChanged << "readerURL"
212 << "printerFileName";
213 foreach (QString s, settingsChanged)
215 if (settings.contains("/mainwindow/" + s))
217 settings.setValue("/system/" + s, settings.value("/mainwindow/" + s));
218 settings.remove ("/mainwindow/" + s);
222 if (settings.contains( "/mainwindow/readerURL") )
223 settings.setValue( "/system/readerURL", settings.value( "/mainwindow/readerURL"));
225 taskModel = new TaskModel();
227 debug=options.isOn ("debug");
229 testmode=options.isOn ("testmode");
231 QString pidString=QString ("%1").arg(getpid());
232 if (debug) qDebug()<< "vym PID="<<pidString;
234 #if defined(VYM_DBUS)
236 QDBusConnection dbusConnection=QDBusConnection::sessionBus();
237 if (!dbusConnection.registerService ("org.insilmaril.vym-"+pidString))
239 fprintf(stderr, "%s\n",
240 qPrintable(QDBusConnection::sessionBus().lastError().message()));
245 if (options.isOn ("name"))
246 vymInstanceName=options.getArg ("name");
248 vymInstanceName=pidString;
250 bool debugBuild=false;
252 qDebug()<<"QT_DEBUG is set";
256 // Use /usr/share/vym or /usr/local/share/vym or . ?
258 if (options.isOn ("local") || debugBuild)
260 vymBaseDir.setPath (vymBaseDir.currentPath());
262 // then look for environment variable
263 if (getenv("VYMHOME")!=0)
265 vymBaseDir.setPath (getenv("VYMHOME"));
267 // ok, let's find my way on my own
269 #if defined (Q_OS_MACX)
270 // Executable is in vym.app/Contents/MacOS, so go up first:
271 vymBaseDir = QCoreApplication::applicationDirPath();
273 vymBaseDir.cd("Resources");
274 #elif defined (Q_OS_WIN32)
278 if (GetModuleFileName(NULL, wbuf, 512))
280 QString mfn(QString::fromWCharArray(wbuf));
281 mfn.replace('\\', '/');
282 int i=mfn.lastIndexOf('/');
285 QMessageBox::critical(0,
287 "Couldn't setup vymBasePath");
290 if (mfn.right(mfn.length() -i -1) != "vym.exe")
292 QMessageBox::critical(0,
294 "vym executable not known as vym.exe");
297 basePath=mfn.left(i);
299 if (basePath.isEmpty())
300 basePath = vymBaseDir.currentPath();
302 vymInstallDir.setPath(basePath);
303 vymBaseDir.setPath(basePath);
305 vymBaseDir.setPath ("/usr/share/vym");
306 if (!vymBaseDir.exists())
308 vymBaseDir.setPath ("/usr/local/share/vym");
309 if (!vymBaseDir.exists())
310 vymBaseDir.setPath(vymBaseDir.currentPath() );
315 // Platform specific settings
316 vymPlatform = QSysInfo::prettyProductName();
318 #if defined(Q_OS_MACX)
319 #elif defined(Q_OS_WIN32)
320 zipToolPath = settings.value("/system/zipToolPath", "c:\\Program Files\\7-Zip\\7z.exe").toString();
321 #elif defined(Q_OS_LINUX)
324 iconPath=vymBaseDir.path()+"/icons/";
325 flagsPath=vymBaseDir.path()+"/flags/";
326 macroPath=vymBaseDir.path() + "/macros/";
330 if (options.isOn ("local"))
331 useDir=QDir().current();
333 useDir=QDir().home();
336 lastExportDir=useDir;
338 if (options.isOn ("help"))
340 cout << qPrintable (options.getHelpText())<<endl;
344 // Initialize translations
345 if (options.isOn ("locale"))
347 localeName = options.getArg ("locale");
348 if (debug) qDebug() << "Main: using option for locale";
352 #if defined(Q_OS_LINUX)
353 if (debug) qDebug() << "Main: (OS Linux) using $LANG for locale";
354 localeName = QProcessEnvironment::systemEnvironment().value("LANG","en");
356 if (debug) qDebug() << "Main: (OS other) using QLocale::system().uiLanguages( using for locale";
357 localeName = QLocale::system().uiLanguages().first();
359 if (localeName.contains("-"))
361 if (debug) qDebug() << "Main: Replacing '-' with '_' in locale";
362 localeName.replace( "-", "_");
369 qDebug() << "Main: localName: " << localeName;
370 qDebug() << "Main: translations: " << localeName, vymBaseDir.path() + "/lang";
371 qDebug() << "Main: uiLanguages: " << QLocale::system().uiLanguages();
372 qDebug() << "Main: LANG: " << QProcessEnvironment::systemEnvironment().value("LANG","foobar");
375 QTranslator vymTranslator;
376 if (!vymTranslator.load( QString("vym.%1").arg( localeName ), vymBaseDir.path() + "/lang") )
377 QMessageBox::warning( 0, QObject::tr( "Warning" ),
378 QString("Couldn't load translations for locale \"%1\" in\n%2")
380 .arg(vymBaseDir.path() + "/lang") );
381 app.installTranslator( &vymTranslator );
383 // Initializing the master rows of flags
384 systemFlagsMaster=new FlagRow;
385 systemFlagsMaster->setName ("systemFlagsMaster");
386 standardFlagsMaster=new FlagRow;
387 standardFlagsMaster->setName ("standardFlagsMaster");
389 // Initialize editors
390 noteEditor = new NoteEditor("noteeditor");
391 noteEditor->setWindowIcon (QPixmap (":/vym-editor.png"));
392 headingEditor = new HeadingEditor("headingeditor");
394 // Check if there is a BugzillaClient
395 QFileInfo fi(vymBaseDir.path()+"/scripts/BugzillaClient.pm");
396 //bugzillaClientAvailable=fi.exists();
397 bugzillaClientAvailable=true; //FIXME-3 add real check again
399 // Initialize mainwindow
400 #if defined(Q_OS_WIN32)
401 Main m(0, Qt::Window | Qt::MSWindowsOwnDC);
406 // Check for zip tools (at least on windows...)
407 #if defined(Q_OS_WIN32)
408 QFile zipTool(zipToolPath);
409 if (!zipTool.exists() )
411 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
412 QObject::tr("Couldn't find tool to unzip data. "
413 "Please download and install 7z and set "
414 "path in Settings menu:\n ") +
415 "http://www.7-zip.org/");
420 m.setWindowIcon (QPixmap (":/vym.png"));
423 if (options.isOn ("commands"))
425 cout << "Available commands:\n";
426 cout << "==================:\n";
427 foreach (Command* c, modelCommands)
428 cout << c->getDescription().toStdString() << endl;
432 if (options.isOn ("commandslatex"))
434 foreach (Command* c, modelCommands)
435 cout << c->getDescriptionLaTeX().toStdString() << endl;
439 if (options.isOn ("batch"))
443 // Paint Mainwindow first time
444 qApp->processEvents();
448 // Show release notes, if not already done
449 m.checkReleaseNotes();
454 if (options.isOn("shortcuts")) switchboard.printASCII(); //FIXME-3 global switchboard and exit after listing
458 // For whatever reason tableView is not sorted initially
461 // Restore last session
462 if (options.isOn ("restore"))
463 m.fileRestoreSession();
466 if (options.isOn ("run"))
469 QString fn=options.getArg ("run");
473 if ( !f.open( QFile::ReadOnly|QFile::Text ) )
475 QString error (QObject::tr("Error"));
476 QString msg (QObject::tr("Couldn't open \"%1\"\n%2.").arg(fn).arg(f.errorString()));
477 if (options.isOn("batch"))
478 qWarning ()<<error+": "+msg;
479 else QMessageBox::warning(0, error,msg);
483 QTextStream in( &f );
486 m.executeEverywhere (script);
487 m.setScriptFile (fn);
491 // For benchmarking we may want to quit instead of entering event loop
492 if (options.isOn ("quit")) return 0;
494 // Enable some last minute cleanup
495 QObject::connect( &app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()) );
502 int s = warningCount + criticalCount + fatalCount;
504 qDebug() << "vym exiting with:\n" <<
505 warningCount << " warning messages\n" <<
506 criticalCount<<" critical messages\n" <<
507 fatalCount<<" fatal messages";