| 2559 | } |
| 2560 | |
| 2561 | void tryRunEventLoop(GUISingleApplication& mainApp) |
| 2562 | { |
| 2563 | std::stringstream out; |
| 2564 | out << App::Application::getUserCachePath() << App::Application::getExecutableName() << "_" |
| 2565 | << App::Application::uniqueInstanceId() << ".lock"; |
| 2566 | |
| 2567 | // open a lock file with the PID |
| 2568 | Base::FileInfo fi(out.str()); |
| 2569 | Base::ofstream lock(fi); |
| 2570 | |
| 2571 | #if !defined(FC_OS_WIN32) || (BOOST_VERSION < 107600) |
| 2572 | std::string filename = out.str(); |
| 2573 | #else |
| 2574 | std::wstring filename = fi.toStdWString(); |
| 2575 | #endif |
| 2576 | try { |
| 2577 | boost::interprocess::file_lock flock(filename.c_str()); |
| 2578 | if (flock.try_lock()) { |
| 2579 | Base::Console().log("Init: Executing event loop…\n"); |
| 2580 | QApplication::exec(); |
| 2581 | |
| 2582 | // Qt can't handle exceptions thrown from event handlers, so we need |
| 2583 | // to manually rethrow SystemExitExceptions. |
| 2584 | if (mainApp.caughtException) { |
| 2585 | throw Base::SystemExitException(*mainApp.caughtException.get()); |
| 2586 | } |
| 2587 | |
| 2588 | // close the lock file, in case of a crash we can see the existing lock file |
| 2589 | // on the next restart and try to repair the documents, if needed. |
| 2590 | flock.unlock(); |
| 2591 | lock.close(); |
| 2592 | fi.deleteFile(); |
| 2593 | } |
| 2594 | else { |
| 2595 | Base::Console().error( |
| 2596 | "Failed to create a file lock for the IPC.\n" |
| 2597 | "The application will be terminated.\n" |
| 2598 | "Attempted lock file: %s", |
| 2599 | fi.filePath().c_str() |
| 2600 | ); |
| 2601 | } |
| 2602 | } |
| 2603 | catch (const boost::interprocess::interprocess_exception& e) { |
| 2604 | QString msg = QString::fromLocal8Bit(e.what()); |
| 2605 | Base::Console().error( |
| 2606 | "Failed to create a file lock for the IPC: %s\n" |
| 2607 | "Attempted lock file: %s\n", |
| 2608 | msg.toUtf8().constData(), |
| 2609 | fi.filePath().c_str() |
| 2610 | ); |
| 2611 | } |
| 2612 | } |
| 2613 | |
| 2614 | void runEventLoop(GUISingleApplication& mainApp) |
| 2615 | { |
no test coverage detected