+ easyParse(argc, argv);
+
+ support::init_package(argv[0], cl_system_support, cl_user_support,
+ support::top_build_dir_is_one_level_up);
+
+ vector<string> files;
+ int exit_status = execBatchCommands(argc, argv, files);
+
+ if (exit_status)
+ return exit_status;
+
+ if (use_gui) {
+ // Force adding of font path _before_ Application is initialized
+ support::addFontResources();
+ pimpl_->application_.reset(createApplication(argc, argv));
+ initGuiFont();
+ // FIXME: this global pointer should probably go.
+ theApp = pimpl_->application_.get();
+ restoreGuiSession(files);
+ // Start the real execution loop.
+
+ // FIXME
+ /* Create a CoreApplication class that will provide the main event loop
+ * and the socket callback registering. With Qt4, only QtCore
+ * library would be needed.
+ * When this is done, a server_mode could be created and the following two
+ * line would be moved out from here.
+ */
+ pimpl_->lyx_server_.reset(new LyXServer(&pimpl_->lyxfunc_, lyxrc.lyxpipes));
+ pimpl_->lyx_socket_.reset(new LyXServerSocket(&pimpl_->lyxfunc_,
+ support::os::internal_path(package().temp_dir() + "/lyxsocket")));
+
+ // handle the batch commands the user asked for
+ if (!batch_command.empty()) {
+ pimpl_->lyxfunc_.dispatch(lyxaction.lookupFunc(batch_command));
+ }
+
+ exit_status = pimpl_->application_->start(batch_command);
+ // Kill the application object before exiting. This avoid crash
+ // on exit on Linux.
+ pimpl_->application_.reset();
+ // Restore original font resources after Application is destroyed.
+ support::restoreFontResources();
+ }
+ else {
+ // FIXME: create a ConsoleApplication
+ theApp = 0;
+ }
+
+ return exit_status;
+}
+
+
+void LyX::prepareExit()
+{
+ // Set a flag that we do quitting from the program,
+ // so no refreshes are necessary.
+ quitting = true;
+
+ // close buffers first
+ pimpl_->buffer_list_.closeAll();
+
+ // do any other cleanup procedures now
+ lyxerr[Debug::INFO] << "Deleting tmp dir " << package().temp_dir() << endl;
+
+ if (!destroyDir(package().temp_dir())) {
+ docstring const msg =
+ bformat(_("Unable to remove the temporary directory %1$s"),
+ from_utf8(package().temp_dir()));
+ Alert::warning(_("Unable to remove temporary directory"), msg);
+ }
+}
+
+
+void LyX::earlyExit(int status)
+{
+ BOOST_ASSERT(pimpl_->application_.get());
+ // LyX::pimpl_::application_ is not initialised at this
+ // point so it's safe to just exit after some cleanup.
+ prepareExit();
+ exit(status);
+}
+
+
+void LyX::quit()
+{
+ lyxerr[Debug::INFO] << "Running QuitLyX." << endl;