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