+ initGuiFont();
+
+ // Parse and remove all known arguments in the LyX singleton
+ // Give an error for all remaining ones.
+ int exit_status = init(argc, argv);
+ if (exit_status) {
+ // Kill the application object before exiting.
+ pimpl_->application_.reset();
+ use_gui = false;
+ prepareExit();
+ return exit_status;
+ }
+
+ // 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.
+ */
+ // Note: socket callback must be registered after init(argc, argv)
+ // such that package().temp_dir() is properly initialized.
+ 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")));
+
+ // Start the real execution loop.
+ exit_status = pimpl_->application_->exec();
+
+ prepareExit();
+
+ // Restore original font resources after Application is destroyed.
+ support::os::restoreFontResources();
+
+ 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
+ if (package().temp_dir() != package().system_temp_dir()) {
+ lyxerr[Debug::INFO] << "Deleting tmp dir "
+ << package().temp_dir() << endl;
+
+ if (!destroyDir(FileName(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);
+ }
+ }
+
+ if (use_gui) {
+ if (pimpl_->session_)
+ pimpl_->session_->writeFile();
+ pimpl_->session_.reset();
+ pimpl_->lyx_server_.reset();
+ pimpl_->lyx_socket_.reset();
+ }
+
+ // Kill the application object before exiting. This avoids crashes
+ // when exiting on Linux.
+ if (pimpl_->application_)
+ pimpl_->application_.reset();
+}
+
+
+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);
+}
+
+
+int LyX::init(int & argc, char * argv[])
+{