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