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