Esempio n. 1
0
int WINAPI WinMain(HINSTANCE hThisInstance,
	HINSTANCE hPrevInstance,
	LPSTR lpszArgument,
	int nFunsterStil)
{

	atexit(lastExit);
	SingleInstanceManager instanceman;

	globalResMan = new ResourceManager("JAVA", PROPID, JARID, JNISMOOTHID);

	string silentFile = FileUtils::getExecutablePath() + "setup.silent";
	_silent = FileUtils::fileExists(silentFile);

	// sets up the command line arguments
	// not sure if lpszArgument can be 0 on Windows...
	if ((lpszArgument != 0) && (strlen(lpszArgument) > 0))
	{
		// Note that this overwrites an existing KEY_ARGUMENTS
		std::vector<std::string> args = StringUtils::split(lpszArgument, " \t\n\r", "\"'", false);
		globalResMan->setUserArguments(args);
	}

	loadLanguage(globalResMan->getProperty("minversion"));

	bool dodebug = globalResMan->getBooleanProperty("skel_Debug");

	if (dodebug)
	{
		DEBUGCONSOLE = new DebugConsole("JSmooth Debug");
		globalResMan->printDebug();
	}

	bool singleinstance = globalResMan->getBooleanProperty("skel_SingleInstance");
	if (singleinstance)
	{
		if (instanceman.alreadyExists())
		{
			if (!_silent) {
				instanceman.sendMessageInstanceShow();
			}
			exit(0);
		}
		else
		{
			instanceman.startMasterInstanceServer();
		}
	}

	DEBUG(string("Main class: ") + globalResMan->getMainName());

	char curdir[_MAX_PATH];
	GetCurrentDirectory(_MAX_PATH, curdir);
	DEBUG(string("Currentdir: ") + curdir);

	string newcurdir = globalResMan->getCurrentDirectory();
	SetCurrentDirectory(newcurdir.c_str());

	std::string message = MSG_ERROR_NO_JAVA_FIRST;// globalResMan->getProperty("skel_Message");
	std::string url = globalResMan->getProperty("skel_DownloadURL");
	std::string params = globalResMan->getProperty("skel_DownloadURLParams");
	bool singleProcess = globalResMan->getBooleanProperty("skel_SingleProcess");

	int tries = 0;
	int exitCode = 0;
	while (true) {

		if (tries > 0) {
			DEBUG("try jre again after download...");
		}

		JavaMachineManager *man = new JavaMachineManager(*globalResMan);
		man->setAcceptExe(true);
		man->setAcceptDLL(true);
		man->setUseConsole(dodebug);
		man->setPreferDLL(singleProcess);

		bool ok = man->run();
		exitCode = man->getExitCode();
		delete man;

		if (ok) {
			if (tries > 0) {
				Sleep(2000); // w/o may be the cause for the message 'installer already running'
			}
			break;
		}

		if (tries > 0) {
			if (_silent) {
				DEBUG(MSG_ERROR_NO_JAVA);
			}
			else {
				MessageBox(0, MSG_ERROR_NO_JAVA.c_str(), APP_TITLE.c_str(), MB_OK | MB_APPLMODAL);
			}
			break;
		}

		tries++;

		DEBUG("Displaying message to user...");
		DEBUG("URL=" + url);
		if (url.empty())
		{
			showGenericError();
			break;
		}

		if (!_silent && MessageBox(0, message.c_str(), APP_TITLE.c_str(), MB_OKCANCEL | MB_ICONQUESTION | MB_APPLMODAL) != IDOK) {
			break;
		}

		string adParams = params;
		string file = doDownload(url, adParams);

		if (file.empty())
		{
			showGenericError();
			break;
		}

		string ext = StringUtils::toLowerCase(FileUtils::getFileExtension(file));
		if (ext == "exe")
		{
			makeExecInfoGui(MSG_INSTALLING_JAVA, MSG_INSTALLING_JAVA_WAIT, file + " " + adParams, _silent);
		}
		else // anything else, we try to open it like a document
		{
			ShellExecute(0, "open", file.c_str(), adParams.c_str(), "", 0);
		}
	}

	DEBUG("NORMAL EXIT");
	DEBUGWAITKEY();

	return exitCode;
}