summaryrefslogtreecommitdiffstats
path: root/src/main.cpp
blob: 826ddfef644c3c1fccbab671d54121a25f163a81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/***************************************************************************
 *   Copyright (C) 2006-2012 by Thomas Schweitzer                          *
 *   thomas-schweitzer(at)arcor.de                                         *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License version 2.0 as   *
 *   published by the Free Software Foundation.                            *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program in the file LICENSE.GPL; if not, write to the *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "MainWindow.h"

// -- #include "IndentHandler.h"
#include "SettingsPaths.h"
// -- #include "UiGuiIndentServer.h"
// -- #include "UiGuiIniFileParser.h"
#include "UiGuiVersion.h"

#include <tqapplication.h>
#include <tqglobal.h>
#include <tqstring.h>
#include <tqtextcodec.h>

// -- #include <algorithm>
#include <iostream>
// -- #include <string>

#include <tclap/CmdLine.h>

/*!
    /brief Entry point to UniversalIndentGUI application.

    Evaluates the following parameters:
    No parameters starts without server and full gui.
    A string without any parameter prefix will be loaded as file on start.
    -p --plugin  : Run as plugin. Server will be started with a simplified gui.
    -s --server  : Run as server only, without gui.
    If -p and -s are set, -p will be used.
    -v --verbose needs a following parameter defining the verbose level as a number from 0 to 3.
 */
int main(int argc, char *argv[])
{
 	TQString file2OpenOnStart = "";
 	int verboseLevel = 1;
 	bool startAsPlugin = false;
 	bool startAsServer = false;
 	bool tclapExitExceptionThrown = false;
 	int returnValue = 0;
 
	// Wrap everything in a try block. Do this every time,
	// because exceptions will be thrown for problems.
	try
	{
 		// Define the command line object.
 		TCLAP::CmdLine cmd("If -p and -s are set, -p will be used.\n"
				    "Giving no parameters starts full gui without server.",
						' ', PROGRAM_VERSION_STRING);
 		cmd.setExceptionHandling(false);
 
 		// Define a value argument and add it to the command line.
    TCLAP::UnlabeledValueArg<std::string> filenameArg("file",
				    "Opens the by filename defined file on start" , false, "", "filename");
 		cmd.add(filenameArg);
 
 		// Define a switch and add it to the command line.
 		TCLAP::SwitchArg pluginSwitch("p", "plugin",
				    "Run as plugin. Server will be started with a simplified gui", false);
 		cmd.add(pluginSwitch);
 
 		// Define a switch and add it to the command line.
 		TCLAP::SwitchArg serverSwitch("s", "server",
				    "Run as server only without gui", false);
 		cmd.add(serverSwitch);
 
 		// Define a value argument and add it to the command line.
 		TCLAP::ValueArg<int> verboselevelArg("v", "verbose",
				    "Sets how many info is written to the log. 0 means with debug info, "
						"3 means critical messages only" , false, 1, "int");
 		cmd.add(verboselevelArg);
 
 		// Parse the args.
 		cmd.parse(argc, argv);
 
 		// Get the value parsed by each arg.
    file2OpenOnStart = filenameArg.getValue().c_str();
 		startAsPlugin = pluginSwitch.getValue();
 		startAsServer = serverSwitch.getValue();
 		verboseLevel = verboselevelArg.getValue();
	}
 	catch (TCLAP::ArgException &e)
	{
		// catch arg exceptions
 		std::cerr << std::endl << "error: " << e.error() << ". " << e.argId() << std::endl;
 		returnValue = 1;
 	}
 	catch (TCLAP::ExitException &e)
	{
		// catch exit exceptions
 		tclapExitExceptionThrown = true;
 		returnValue = e.getExitStatus();
 	}
	catch (...)
	{
		// catch any exceptions
		std::cerr << std::endl << "There was an error! Maybe faulty "
			      "command line arguments set. See --help." << std::endl;
		returnValue = 1;
	}

	if (returnValue != 0 || tclapExitExceptionThrown)
	{
		return returnValue;
	}

  TQApplication app(argc, argv);
// --     UiGuiIndentServer server;
  MainWindow *mainWindow = NULL;
// --     IndentHandler *indentHandler = NULL;

  // Setting UTF-8 as default 8-Bit encoding
  TQTextCodec::setCodecForCStrings(TQTextCodec::codecForName("UTF-8"));
  TQTextCodec::setCodecForLocale(TQTextCodec::codecForName("UTF-8"));

  // Start normal with full gui and without server.
  if (!startAsPlugin && !startAsServer) {
      mainWindow = new MainWindow(file2OpenOnStart);
      mainWindow->show();
  }
// --     // Start as plugin with server.
// --     else if (startAsPlugin) {
// --         server.startServer();
// --         indentHandler = new IndentHandler(0);
// --         indentHandler->show();
// --     }
// --     // Start as server only without any gui.
// --     else if (startAsServer) {
// --         server.startServer();
// --     }

  try
	{
		app.connect(&app, TQ_SIGNAL(lastWindowClosed()), &app, TQ_SLOT(quit()));  // TODO: remove when no longer needed
    returnValue = app.exec();
  }
  catch (std::exception &ex)
	{
    tqDebug(TQString() + __LINE__ + " " + __FUNCTION__ + ": Something went terribly wrong: " + ex.what());
  }
// -- 
// --     if (startAsPlugin || startAsServer)
// --         server.stopServer();
// -- 
// --     delete indentHandler;
	delete mainWindow;

  return returnValue;
}