main.cpp
author insilmaril
Tue, 06 Apr 2010 13:30:07 +0000
changeset 843 2d36a7bb0867
parent 842 bec082472471
child 847 43268373032d
permissions -rw-r--r--
(Very) minor changes for debugging output
     1 #include <QApplication>
     2 #include <QtDBus/QDBusConnection>
     3 
     4 #include <iostream>
     5 using namespace std;
     6 
     7 #include "findwidget.h"
     8 #include "findresultwidget.h"
     9 #include "flagrow.h"
    10 #include "flagrowobj.h"
    11 #include "mainwindow.h"
    12 #include "options.h"
    13 #include "settings.h"
    14 #include "version.h"
    15 
    16 #if defined(Q_OS_WIN32)
    17 #define WIN32_LEAN_AND_MEAN
    18 #include <windows.h>
    19 #endif
    20 
    21 #include <sys/types.h>			// To retrieve PID for use in DBUS
    22 #include <unistd.h>
    23 
    24 // Global variables
    25 TextEditor *textEditor;			// used in Constr. of LinkableMapObj
    26 								// initialized in mainwindow
    27 QString vymName;
    28 QString vymVersion;
    29 QString vymBuildDate;
    30 QString vymCodeName;
    31 QString vymInstanceName;
    32 
    33 Main *mainWindow;				// used in BranchObj::select()								
    34 FindWidget *findWidget;
    35 FindResultWidget *findResultWidget;
    36 
    37 QDBusConnection dbusConnection= QDBusConnection::sessionBus();
    38 
    39 QString tmpVymDir;				// All temp files go there, created in mainwindow
    40 QString clipboardDir;			// Clipboard used in all mapEditors
    41 QString clipboardFile;			// Clipboard used in all mapEditors
    42 QDir vymBaseDir;				// Containing all styles, scripts, images, ...
    43 QDir lastImageDir;
    44 QDir lastFileDir;
    45 #if defined(Q_OS_WIN32)
    46 QDir vymInstallDir;
    47 #endif
    48 QString iconPath;				// Pointing to icons used for toolbars
    49 QString flagsPath;				// Pointing to flags
    50 bool clipboardEmpty;			
    51 bool debug;						// global debugging flag
    52 FlagRow *systemFlagsMaster;	
    53 FlagRow *standardFlagsMaster;	
    54 
    55 Settings settings ("InSilmaril","vym"); // Organization, Application name
    56 
    57 Options options;
    58 ImageIO imageIO;
    59 
    60 int statusbarTime=3500;
    61 
    62 int main(int argc, char* argv[])
    63 {
    64 	//Q_INIT_RESOURCE (application);
    65 
    66     QApplication app(argc,argv);
    67 
    68 	vymName=__VYM_NAME;
    69 	vymVersion=__VYM_VERSION;
    70 	vymBuildDate=__VYM_BUILD_DATE;
    71 	vymCodeName=__VYM_CODENAME;
    72 
    73 	// Reading and initializing options commandline options
    74 	options.add ("debug", Option::Switch, "d", "debug");
    75 	options.add ("help", Option::Switch, "h", "help");
    76 	options.add ("local", Option::Switch, "l", "local");
    77 	options.add ("name", Option::String, "n", "name");
    78 	options.add ("quit", Option::Switch, "q", "quit");
    79 	options.add ("run", Option::String, "r", "run");
    80 	options.add ("shortcuts", Option::Switch, "s", "shortcuts");
    81 	options.add ("shortcutsLaTeX", Option::Switch, "sl", "shortcutsLaTeX");
    82 	options.add ("test", Option::String, "t", "test");
    83 	options.add ("version", Option::Switch, "v","version");
    84 	options.setHelpText (
    85 		"VYM - View Your Mind\n"
    86 		"--------------------\n\n"
    87 		"Information about vym can be found in vym.pdf,\n"
    88 		"which should be part of the vym package.\n"
    89 		"It is also available at the project homepage:\n\n"
    90 		"http://www.InSilmaril.de/vym\n");
    91 	if (options.parse())
    92 	{
    93 		cout << endl << qPrintable( options.getHelpText())<<endl;
    94 		return 1;
    95 	}
    96 
    97 	debug=options.isOn ("debug");
    98 
    99 	if (options.isOn ("version"))
   100 	{
   101 		cout << "VYM - View Your Mind (c) 2004-2009 Uwe Drechsel "  << endl
   102 			<<"   Version: "<<__VYM_VERSION <<endl
   103 			<<"Build date: "<<__VYM_BUILD_DATE << endl
   104 			<<"  "<<__VYM_CODENAME<<endl;
   105 			
   106 		return 0;	
   107 	}		
   108 	
   109 	// Register for DBUS
   110 	if (debug) cout << "PID="<<getpid()<<endl;
   111 	QString pidString=QString ("%1").arg(getpid());
   112 	if (!dbusConnection.registerService ("org.insilmaril.vym-"+pidString))
   113 	{
   114 	   fprintf(stderr, "%s\n",
   115 			qPrintable(QDBusConnection::sessionBus().lastError().message()));        
   116         exit(1);
   117 	}	
   118 
   119 	if (options.isOn ("name"))
   120 		vymInstanceName=options.getArg ("name");
   121 	else
   122 		vymInstanceName=pidString;
   123 	
   124 
   125 	// Use /usr/share/vym or /usr/local/share/vym or . ?
   126 	// First try options
   127 	if (options.isOn ("local"))
   128 	{
   129 		vymBaseDir.setPath (vymBaseDir.currentDirPath());
   130 	} else
   131 	// then look for environment variable
   132 	if (getenv("VYMHOME")!=0)
   133 	{
   134 		vymBaseDir.setPath (getenv("VYMHOME"));
   135 	} else
   136 	// ok, let's find my way on my own
   137 	{
   138 		#if defined (Q_OS_MACX)
   139 			vymBaseDir.setPath(vymBaseDir.currentDirPath() +"/vym.app/Contents/Resources");
   140 
   141         #elif defined (Q_OS_WIN32)
   142             QString basePath;
   143 
   144             wchar_t wbuf[512];
   145             if (GetModuleFileName(NULL, wbuf, 512))
   146             {
   147                 QString mfn(QString::fromWCharArray(wbuf));
   148                 mfn.replace('\\', '/');
   149                 if (mfn.endsWith("/bin/vym.exe", Qt::CaseInsensitive))
   150                 {
   151                     mfn.chop(12);
   152                     basePath = mfn;
   153                 }
   154             }
   155 
   156             if (basePath.isEmpty())
   157                 basePath = vymBaseDir.currentDirPath();
   158 
   159             vymInstallDir.setPath(basePath);
   160             vymBaseDir.setPath(basePath + "/share/vym");
   161 
   162 		#else
   163 			vymBaseDir.setPath ("/usr/share/vym");
   164 			if (!vymBaseDir.exists())
   165 			{
   166 				vymBaseDir.setPath ("/usr/local/share/vym");
   167 				if (!vymBaseDir.exists())
   168 					vymBaseDir.setPath(vymBaseDir.currentDirPath() );
   169 			}		
   170 		#endif
   171 	}
   172 
   173 	iconPath=vymBaseDir.path()+"/icons/";
   174 	flagsPath=vymBaseDir.path()+"/flags/";
   175 
   176 	// Some directories
   177 	lastImageDir=QDir().current();
   178 	lastFileDir=QDir().current();
   179 
   180 	if (options.isOn ("help"))
   181 	{
   182 		cout << qPrintable (options.getHelpText())<<endl;
   183 		return 0;	
   184 	}	
   185 
   186 	// Initialize translations
   187 	QTranslator translator (0);
   188 	translator.load( QString("vym_")+QTextCodec::locale(), vymBaseDir.path() + "/lang");
   189     app.installTranslator( &translator );
   190 
   191 	// Initializing the master rows of flags
   192 	systemFlagsMaster=new FlagRow;
   193 	systemFlagsMaster->setName ("systemFlagsMaster");
   194 	standardFlagsMaster=new FlagRow;
   195 	standardFlagsMaster->setName ("standardFlagsMaster");
   196 
   197 
   198 	// Initialize window of TextEditor
   199 	textEditor = new TextEditor();
   200 	textEditor->setIcon (QPixmap (iconPath+"vym-editor.png"));
   201 
   202 	// Initialize mainwindow 
   203 #if defined(Q_OS_WIN32)
   204     Main m(0, 0, (Qt::Window | Qt::MSWindowsOwnDC));
   205 #else
   206     Main m;
   207 #endif
   208 
   209 	//m.resize(m.sizeHint());
   210 	m.setIcon (QPixmap (iconPath+"vym.png"));
   211 	m.show();
   212 	m.fileNew();
   213 	// Paint Mainwindow first time
   214 	qApp->processEvents();
   215 
   216 	m.loadCmdLine();
   217 
   218 	// Run script
   219 	if (options.isOn ("run"))
   220 	{
   221 		QString script;
   222 		QString fn=options.getArg ("run");
   223 		if ( !fn.isEmpty() )
   224 		{
   225 			QFile f( fn );
   226 			if ( !f.open( QIODevice::ReadOnly ) )
   227 			{
   228 				QMessageBox::warning(0, 
   229 					QObject::tr("Error"),
   230 					QObject::tr("Couldn't open %1.\n").arg(fn));
   231 				return 0;
   232 			}	
   233 
   234 			QTextStream ts( &f );
   235 			script= ts.read();
   236 			f.close();
   237 			m.setScript (script);
   238 			m.runScriptEverywhere (script);
   239 		}
   240 	}		
   241 	
   242 	// For benchmarking we may want to quit instead of entering event loop
   243 	if (options.isOn ("quit"))
   244 	{
   245 		return 0;
   246 	}	
   247 
   248 	// Enable some last minute cleanup
   249     QObject::connect( &app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()) );
   250 
   251     return app.exec();
   252 }