main.c 6.43 KB
Newer Older
1 2 3
/**
 * main.c - main for SciteProj
 *
4
 *  Copyright 2006 Roy Wood, 2009-2018 Andreas Rönnquist
5 6
 *
 * This file is part of SciteProj.
7
 *
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 * SciteProj is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * SciteProj 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 SciteProj.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <stdlib.h>

#include <gtk/gtk.h>
#include <glib.h>
#include <string.h>
28 29
#include <glib/gi18n.h>

30 31 32 33
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>

34
#include <locale.h>
35 36 37 38 39 40 41 42 43 44

#include "clicked_node.h"
#include "gui.h"
#include "tree_manipulation.h"
#include "prefs.h"
#include "graphics.h"
#include "scite_utils.h"
#include "about.h"
#include "file_utils.h"
#include "string_utils.h"
45 46
#include "load_folder.h"
#include "script.h"
47 48 49 50 51 52 53 54 55 56 57 58 59

static struct CommandLineIndata {
	const gchar *scite_filename;
} cmd;


/*
 *		Program main entry
 */
int main(int argc, char *argv[])
{
	int returnCode = EXIT_FAILURE;
	GError *err = NULL;
60
	GOptionContext *context = NULL;
61

62 63 64
	static gboolean version = FALSE;
	static gchar *scite_instance = NULL;
	static gboolean load_a_folder = FALSE;
65 66
	static gboolean start_scite = FALSE;

67 68 69 70 71 72 73 74 75 76 77 78 79
	static const GOptionEntry options[] = {
		{	"version",		'v',	0, G_OPTION_ARG_NONE,		&version,
			N_("Show program version and quit")
		},
		{	"scite",			's',	0, G_OPTION_ARG_STRING,		&scite_instance,
			N_("Set a filename for the instance of SciTE to open"),	N_("SCITE_FILENAME")
		},
		{	"load_folder",	'l',	0,	G_OPTION_ARG_NONE,		&load_a_folder,
			N_("Load a folder")
		},
		{	"start_scite",	't',	0,	G_OPTION_ARG_NONE,		&start_scite,
			N_("Start SciTE automatically with SciteProj")
		},
80 81
		{ NULL }
	};
82

83 84
	// Init gettext stuff
	setlocale(LC_ALL,"");
85

86 87
	bindtextdomain(PACKAGE, LOCALEDIR);
	bind_textdomain_codeset(PACKAGE, "");
88
	textdomain(PACKAGE);
89

90
	gchar *sciteproj_description = g_strdup_printf(_("SciTE Project Manager"));
91

92
	gchar *full_desc_string = g_strdup_printf("- %s",sciteproj_description);
93

94
	context=g_option_context_new(full_desc_string);
95
	g_option_context_add_main_entries(context, options, NULL);
96
	if (!g_option_context_parse(context, &argc, &argv, &err)) {
97
		g_print(_("option parsing failed: %s"), err->message);
98 99 100
		printf("\n");
		exit(EXIT_FAILURE);
	}
101

102 103
	g_free(sciteproj_description);
	g_free(full_desc_string);
104

105 106 107 108 109 110 111
	/*
		Interpret the options
	 */
	/*
		set instance of SciTE to run
	*/
	if (scite_instance) {
112
		cmd.scite_filename = scite_instance;
113
	}
114

115
	init_version_string();
116

117
	/*
118
		Show SciteProj version
119 120 121 122
	*/
	if (version) {
		show_version();
		printf("\n");
123
		done_version_string();
124 125 126
		exit(EXIT_SUCCESS);
	}

127 128
	// Init gtk
	gtk_init(&argc, &argv);
129 130

	// Since glib 2.36, this isn't needed
131
	/*
132
#if GLIB_MAJOR_VERSION<=2 && GLIB_MINOR_VERSION<36
133
	g_type_init();
134
#endif
135
*/
136

137 138
	init_file_utils();

139
	gchar *current_dir = g_get_current_dir();
140

141
	if (argc > 2) {
142 143 144 145 146 147
		printf(_("A folder is expected as parameter to sciteproj..."));
		printf("\n");
		return EXIT_FAILURE;
	}

	gchar *dir_to_load;
148 149
	if (argc == 1) { // only "sciteproj" on the command-line
		dir_to_load = current_dir;
150 151

	} else { // "sciteproj <folder_name>" on the command-line
152
		dir_to_load = argv[1];
153 154 155 156

		gchar *newpath;

		if (relative_path_to_abs_path(dir_to_load, &newpath, current_dir, NULL)) {
157
			dir_to_load = newpath;
158 159 160
		}
	}

161
	// Init preferences
162 163
	if (!init_prefs(dir_to_load, &err)) {
		/*
164
		g_print(_("Error initing preferences: %s"), err->message);
165
		done_version_string();
166
		return EXIT_FAILURE;
167
		*/
168
	}
169

170
	// check environment variable
171
	gchar *scite_path_env = getenv("SciTE_HOME");
172

173
	// test for scite
174 175 176 177 178
	if (scite_path_env != NULL) {
		gchar *env_filename = g_build_filename(scite_path_env, "scite", NULL);
		if (g_file_test(env_filename, G_FILE_TEST_EXISTS)) {
			if (cmd.scite_filename == NULL) {
				cmd.scite_filename = g_strdup(env_filename);
179 180
			}
		} else {
181
			g_warning(_("Environment variable exists, but doesn't point to a folder containing scite."));
182 183
		}

184 185 186 187 188
		if (env_filename != NULL) g_free(env_filename);
		env_filename = g_build_filename(scite_path_env,"SciTE",NULL);
		if (g_file_test(env_filename, G_FILE_TEST_EXISTS)) {
			if (cmd.scite_filename == NULL) {
				cmd.scite_filename = g_strdup(env_filename);
189 190
			}
		} else {
191
			g_warning(_("Environment variable exists, but doesn't point to a folder containing scite."));
192 193
		}
	}
194

195
	// do we have a custom scite executable string as command line option?
196
	if (cmd.scite_filename != NULL) {
197

198
		// Does SciTE exist at that location?
199
		if (g_file_test(cmd.scite_filename, G_FILE_TEST_IS_REGULAR)) {
200

201
			// If we have already allocated memory for scite path, free it
202
			if (prefs.scite_path != NULL) g_free(prefs.scite_path);
203

204
			// Set the new one
205 206
			prefs.scite_path=g_strdup(cmd.scite_filename);

207
		} else {
208
			g_print(_("Couldn't find a SciTE executable named '%s'!\n"), cmd.scite_filename);
209
			g_print(_("Checking for SciTE in the standard locations instead.\n"));
210
		}
211 212
	}

213
	//g_option_context_free(context);
214

215 216 217
	/*
	 * Any "used" options has been removed from the argv/argc array here.
	 */
218

219 220
	// Check for SciTE
	if (!check_if_scite_exists()) {
221
		GtkWidget *warningDialog = gtk_message_dialog_new(get_main_window(), GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK,
222 223
		                           _("Warning! Couldn't locate SciTE!\n"
		                             "Program will start, but you won't be able to open SciTE to edit files."));
224 225 226
		gtk_dialog_run(GTK_DIALOG(warningDialog));
		gtk_widget_destroy(warningDialog);
	}
227

228 229
	// Set up the GUI
	if (!setup_gui(&err)) {
230
		g_print(_("Could not setup the gui: %s"), err->message);
231
		g_print("\n");
232 233
		goto EXITPOINT;
	}
234 235 236 237 238

	if (!is_string_folder(dir_to_load)) {
		printf(_("Not a valid folder!"));
		printf("\n");
		return EXIT_FAILURE;
239
	}
240 241

	// Should we load a folder?
242
	set_project_filepath(dir_to_load, NULL);
243

244
	load_folder(dir_to_load, NULL);
245

246
	init_scite_connection();
247 248 249 250 251 252

	// open scite, if prefs says we should
	if (prefs.start_scite == TRUE || start_scite) {
		launch_scite("", NULL);
	}

253
	// Run the app
254

255
	gtk_main();
256

257
	returnCode = EXIT_SUCCESS;
258

259
EXITPOINT:
260

261
	gui_close();
262

263
	done_prefs();
264

265
	done_version_string();
266 267 268

	g_free(current_dir);

269
	if (err) g_error_free(err);
270

271 272
	return returnCode;
}