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