+ easyParse(argc, argv);
+
+ support::init_package(argv[0], cl_system_support, cl_user_support,
+ support::top_build_dir_is_one_level_up);
+
+ vector<FileName> files;
+
+ if (!use_gui) {
+ // FIXME: create a ConsoleApplication
+ int exit_status = loadFiles(argc, argv, files);
+ if (exit_status) {
+ prepareExit();
+ return exit_status;
+ }
+
+ if (batch_command.empty() || pimpl_->buffer_list_.empty()) {
+ prepareExit();
+ return EXIT_SUCCESS;
+ }
+
+ BufferList::iterator begin = pimpl_->buffer_list_.begin();
+ BufferList::iterator end = pimpl_->buffer_list_.end();
+
+ bool final_success = false;
+ for (BufferList::iterator I = begin; I != end; ++I) {
+ Buffer * buf = *I;
+ bool success = false;
+ buf->dispatch(batch_command, &success);
+ final_success |= success;
+ }
+ prepareExit();
+ return !final_success;
+ }
+
+ // Force adding of font path _before_ Application is initialized
+ support::addFontResources();
+
+ // Let the frontend parse and remove all arguments that it knows
+ pimpl_->application_.reset(createApplication(argc, argv));
+
+ initGuiFont();
+
+ // Parse and remove all known arguments in the LyX singleton
+ // Give an error for all remaining ones.
+ int exit_status = loadFiles(argc, argv, files);
+ if (exit_status) {
+ // Kill the application object before exiting.
+ pimpl_->application_.reset();
+ use_gui = false;
+ prepareExit();
+ return exit_status;
+ }
+
+ 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")));
+
+ exit_status = pimpl_->application_->exec();
+
+ prepareExit();
+
+ // Restore original font resources after Application is destroyed.
+ support::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
+ lyxerr[Debug::INFO] << "Deleting tmp dir " << package().temp_dir() << endl;
+
+ // Prevent the deletion of /tmp if LyX was called with invalid
+ // arguments. Does not work on windows.
+ // FIXME: Fix the real bug instead.
+ if (package().temp_dir() == "/tmp") {
+ lyxerr << "Not deleting /tmp." << endl;
+ return;
+ }
+
+ 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);
+ }
+
+ if (use_gui) {
+ if (pimpl_->session_)
+ pimpl_->session_->writeFile();
+ pimpl_->session_.reset();
+ pimpl_->lyx_server_.reset();
+ pimpl_->lyx_socket_.reset();
+ }