main.cpp
author insilmaril
Wed, 10 Mar 2010 15:36:19 +0000
changeset 834 0fad394bc330
parent 829 832e96c9abb6
child 842 bec082472471
permissions -rw-r--r--
Minor fixes
     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 ("version", Option::Switch, "v","version");
    76 	options.add ("local", Option::Switch, "l", "local");
    77 	options.add ("name", Option::String, "n", "name");
    78 	options.add ("help", Option::Switch, "h", "help");
    79 	options.add ("quit", Option::Switch, "q", "quit");
    80 	options.add ("run", Option::String, "r", "run");
    81 	options.add ("test", Option::String, "t", "test");
    82 	options.setHelpText (
    83 		"VYM - View Your Mind\n"
    84 		"--------------------\n\n"
    85 		"Information about vym can be found in vym.pdf,\n"
    86 		"which should be part of the vym package.\n"
    87 		"It is also available at the project homepage:\n\n"
    88 		"http://www.InSilmaril.de/vym\n");
    89 	if (options.parse())
    90 	{
    91 		cout << endl << qPrintable( options.getHelpText())<<endl;
    92 		return 1;
    93 	}
    94 
    95 	debug=options.isOn ("debug");
    96 
    97 	if (options.isOn ("version"))
    98 	{
    99 		cout << "VYM - View Your Mind (c) 2004-2009 Uwe Drechsel "  << endl
   100 			<<"   Version: "<<__VYM_VERSION <<endl
   101 			<<"Build date: "<<__VYM_BUILD_DATE << endl
   102 			<<"  "<<__VYM_CODENAME<<endl;
   103 			
   104 		return 0;	
   105 	}		
   106 	
   107 	// Register for DBUS
   108 	if (debug) cout << "PID="<<getpid()<<endl;
   109 	QString pidString=QString ("%1").arg(getpid());
   110 	if (!dbusConnection.registerService ("org.insilmaril.vym-"+pidString))
   111 	{
   112 	   fprintf(stderr, "%s\n",
   113 			qPrintable(QDBusConnection::sessionBus().lastError().message()));        
   114         exit(1);
   115 	}	
   116 
   117 	if (options.isOn ("name"))
   118 		vymInstanceName=options.getArg ("name");
   119 	else
   120 		vymInstanceName=pidString;
   121 	
   122 
   123 	// Use /usr/share/vym or /usr/local/share/vym or . ?
   124 	// First try options
   125 	if (options.isOn ("local"))
   126 	{
   127 		vymBaseDir.setPath (vymBaseDir.currentDirPath());
   128 	} else
   129 	// then look for environment variable
   130 	if (getenv("VYMHOME")!=0)
   131 	{
   132 		vymBaseDir.setPath (getenv("VYMHOME"));
   133 	} else
   134 	// ok, let's find my way on my own
   135 	{
   136 		#if defined (Q_OS_MACX)
   137 			vymBaseDir.setPath(vymBaseDir.currentDirPath() +"/vym.app/Contents/Resources");
   138 
   139         #elif defined (Q_OS_WIN32)
   140             QString basePath;
   141 
   142             wchar_t wbuf[512];
   143             if (GetModuleFileName(NULL, wbuf, 512))
   144             {
   145                 QString mfn(QString::fromWCharArray(wbuf));
   146                 mfn.replace('\\', '/');
   147                 if (mfn.endsWith("/bin/vym.exe", Qt::CaseInsensitive))
   148                 {
   149                     mfn.chop(12);
   150                     basePath = mfn;
   151                 }
   152             }
   153 
   154             if (basePath.isEmpty())
   155                 basePath = vymBaseDir.currentDirPath();
   156 
   157             vymInstallDir.setPath(basePath);
   158             vymBaseDir.setPath(basePath + "/share/vym");
   159 
   160 		#else
   161 			vymBaseDir.setPath ("/usr/share/vym");
   162 			if (!vymBaseDir.exists())
   163 			{
   164 				vymBaseDir.setPath ("/usr/local/share/vym");
   165 				if (!vymBaseDir.exists())
   166 					vymBaseDir.setPath(vymBaseDir.currentDirPath() );
   167 			}		
   168 		#endif
   169 	}
   170 
   171 	iconPath=vymBaseDir.path()+"/icons/";
   172 	flagsPath=vymBaseDir.path()+"/flags/";
   173 
   174 	// Some directories
   175 	lastImageDir=QDir().current();
   176 	lastFileDir=QDir().current();
   177 
   178 	if (options.isOn ("help"))
   179 	{
   180 		cout << qPrintable (options.getHelpText())<<endl;
   181 		return 0;	
   182 	}	
   183 
   184 	// Initialize translations
   185 	QTranslator translator (0);
   186 	translator.load( QString("vym_")+QTextCodec::locale(), vymBaseDir.path() + "/lang");
   187     app.installTranslator( &translator );
   188 
   189 	// Initializing the master rows of flags
   190 	systemFlagsMaster=new FlagRow;
   191 	systemFlagsMaster->setName ("systemFlagsMaster");
   192 	standardFlagsMaster=new FlagRow;
   193 	standardFlagsMaster->setName ("standardFlagsMaster");
   194 
   195 
   196 	// Initialize window of TextEditor
   197 	textEditor = new TextEditor();
   198 	textEditor->setIcon (QPixmap (iconPath+"vym-editor.png"));
   199 
   200 	// Initialize mainwindow 
   201 #if defined(Q_OS_WIN32)
   202     Main m(0, 0, (Qt::Window | Qt::MSWindowsOwnDC));
   203 #else
   204     Main m;
   205 #endif
   206 
   207 	//m.resize(m.sizeHint());
   208 	m.setIcon (QPixmap (iconPath+"vym.png"));
   209 	m.show();
   210 	m.fileNew();
   211 	// Paint Mainwindow first time
   212 	qApp->processEvents();
   213 
   214 	m.loadCmdLine();
   215 
   216 	// Run script
   217 	if (options.isOn ("run"))
   218 	{
   219 		QString script;
   220 		QString fn=options.getArg ("run");
   221 		if ( !fn.isEmpty() )
   222 		{
   223 			QFile f( fn );
   224 			if ( !f.open( QIODevice::ReadOnly ) )
   225 			{
   226 				QMessageBox::warning(0, 
   227 					QObject::tr("Error"),
   228 					QObject::tr("Couldn't open %1.\n").arg(fn));
   229 				return 0;
   230 			}	
   231 
   232 			QTextStream ts( &f );
   233 			script= ts.read();
   234 			f.close();
   235 			m.setScript (script);
   236 			m.runScriptEverywhere (script);
   237 		}
   238 	}		
   239 	
   240 	// For benchmarking we may want to quit instead of entering event loop
   241 	if (options.isOn ("quit"))
   242 	{
   243 		return 0;
   244 	}	
   245 
   246 	// Enable some last minute cleanup
   247     QObject::connect( &app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()) );
   248 
   249     return app.exec();
   250 }