+ easyParse(argc, argv);
+
+ support::init_package(to_utf8(from_local8bit(argv[0])),
+ cl_system_support, cl_user_support,
+ support::top_build_dir_is_one_level_up);
+
+ if (!use_gui) {
+ // FIXME: create a ConsoleApplication
+ int exit_status = init(argc, argv);
+ if (exit_status) {
+ prepareExit();
+ return exit_status;
+ }
+
+ loadFiles();
+
+ 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::os::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 = 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();
+ }