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