Use Glib::MainLoop instead of QEventLoop in glib code

This commit is contained in:
Ilya Fedin 2021-06-27 01:51:32 +04:00 committed by John Preston
parent 0e25ef7524
commit 8d75078a42
3 changed files with 41 additions and 50 deletions

View File

@ -578,20 +578,23 @@ int XDPFileDialog::exec() {
// HACK we have to avoid returning until we emit // HACK we have to avoid returning until we emit
// that the dialog was accepted or rejected // that the dialog was accepted or rejected
QEventLoop loop; const auto context = Glib::MainContext::create();
const auto loop = Glib::MainLoop::create(context);
g_main_context_push_thread_default(context->gobj());
rpl::lifetime lifetime; rpl::lifetime lifetime;
accepted( accepted(
) | rpl::start_with_next([&] { ) | rpl::start_with_next([&] {
loop.quit(); loop->quit();
}, lifetime); }, lifetime);
rejected( rejected(
) | rpl::start_with_next([&] { ) | rpl::start_with_next([&] {
loop.quit(); loop->quit();
}, lifetime); }, lifetime);
loop.exec(); loop->run();
g_main_context_pop_thread_default(context->gobj());
if (guard.isNull()) { if (guard.isNull()) {
return QDialog::Rejected; return QDialog::Rejected;

View File

@ -33,19 +33,9 @@ constexpr auto kXDGDesktopPortalObjectPath = "/org/freedesktop/portal/desktop"_c
constexpr auto kXDGDesktopPortalOpenURIInterface = "org.freedesktop.portal.OpenURI"_cs; constexpr auto kXDGDesktopPortalOpenURIInterface = "org.freedesktop.portal.OpenURI"_cs;
constexpr auto kPropertiesInterface = "org.freedesktop.DBus.Properties"_cs; constexpr auto kPropertiesInterface = "org.freedesktop.DBus.Properties"_cs;
class XDPOpenWithDialog : public QWindow { } // namespace
public:
XDPOpenWithDialog(const QString &filepath)
: _filepath(filepath.toStdString()) {
}
bool exec(); bool ShowXDPOpenWithDialog(const QString &filepath) {
private:
Glib::ustring _filepath;
};
bool XDPOpenWithDialog::exec() {
try { try {
const auto connection = Gio::DBus::Connection::get_sync( const auto connection = Gio::DBus::Connection::get_sync(
Gio::DBus::BusType::BUS_TYPE_SESSION); Gio::DBus::BusType::BUS_TYPE_SESSION);
@ -69,8 +59,10 @@ bool XDPOpenWithDialog::exec() {
return false; return false;
} }
const auto filepathUtf8 = filepath.toUtf8();
const auto fd = open( const auto fd = open(
_filepath.c_str(), filepathUtf8.constData(),
O_RDONLY); O_RDONLY);
if (fd == -1) { if (fd == -1) {
@ -113,7 +105,9 @@ bool XDPOpenWithDialog::exec() {
+ '/' + '/'
+ handleToken; + handleToken;
QEventLoop loop; const auto context = Glib::MainContext::create();
const auto loop = Glib::MainLoop::create(context);
g_main_context_push_thread_default(context->gobj());
const auto signalId = connection->signal_subscribe( const auto signalId = connection->signal_subscribe(
[&]( [&](
@ -123,7 +117,7 @@ bool XDPOpenWithDialog::exec() {
const Glib::ustring &interface_name, const Glib::ustring &interface_name,
const Glib::ustring &signal_name, const Glib::ustring &signal_name,
const Glib::VariantContainerBase &parameters) { const Glib::VariantContainerBase &parameters) {
loop.quit(); loop->quit();
}, },
std::string(kXDGDesktopPortalService), std::string(kXDGDesktopPortalService),
"org.freedesktop.portal.Request", "org.freedesktop.portal.Request",
@ -166,9 +160,11 @@ bool XDPOpenWithDialog::exec() {
std::string(kXDGDesktopPortalService)); std::string(kXDGDesktopPortalService));
if (signalId != 0) { if (signalId != 0) {
QGuiApplicationPrivate::showModalWindow(this); QWindow window;
loop.exec(); QGuiApplicationPrivate::showModalWindow(&window);
QGuiApplicationPrivate::hideModalWindow(this); loop->run();
g_main_context_pop_thread_default(context->gobj());
QGuiApplicationPrivate::hideModalWindow(&window);
} }
return true; return true;
@ -178,12 +174,6 @@ bool XDPOpenWithDialog::exec() {
return false; return false;
} }
} // namespace
bool ShowXDPOpenWithDialog(const QString &filepath) {
return XDPOpenWithDialog(filepath).exec();
}
} // namespace internal } // namespace internal
} // namespace File } // namespace File
} // namespace Platform } // namespace Platform

View File

@ -85,12 +85,7 @@ constexpr auto kSnapcraftSettingsInterface = kSnapcraftSettingsService;
#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION #ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION
std::unique_ptr<internal::NotificationServiceWatcher> NSWInstance; std::unique_ptr<internal::NotificationServiceWatcher> NSWInstance;
class PortalAutostart : public QWindow { void PortalAutostart(bool start, bool silent) {
public:
PortalAutostart(bool start, bool silent = false);
};
PortalAutostart::PortalAutostart(bool start, bool silent) {
if (cExeName().isEmpty()) { if (cExeName().isEmpty()) {
return; return;
} }
@ -149,7 +144,9 @@ PortalAutostart::PortalAutostart(bool start, bool silent) {
+ '/' + '/'
+ handleToken; + handleToken;
QEventLoop loop; const auto context = Glib::MainContext::create();
const auto loop = Glib::MainLoop::create(context);
g_main_context_push_thread_default(context->gobj());
const auto signalId = connection->signal_subscribe( const auto signalId = connection->signal_subscribe(
[&]( [&](
@ -175,7 +172,7 @@ PortalAutostart::PortalAutostart(bool start, bool silent) {
} }
} }
loop.quit(); loop->quit();
}, },
std::string(kXDGDesktopPortalService), std::string(kXDGDesktopPortalService),
"org.freedesktop.portal.Request", "org.freedesktop.portal.Request",
@ -199,9 +196,11 @@ PortalAutostart::PortalAutostart(bool start, bool silent) {
std::string(kXDGDesktopPortalService)); std::string(kXDGDesktopPortalService));
if (signalId != 0) { if (signalId != 0) {
QGuiApplicationPrivate::showModalWindow(this); QWindow window;
loop.exec(); QGuiApplicationPrivate::showModalWindow(&window);
QGuiApplicationPrivate::hideModalWindow(this); loop->run();
g_main_context_pop_thread_default(context->gobj());
QGuiApplicationPrivate::hideModalWindow(&window);
} }
} catch (const Glib::Error &e) { } catch (const Glib::Error &e) {
if (!silent) { if (!silent) {
@ -211,12 +210,7 @@ PortalAutostart::PortalAutostart(bool start, bool silent) {
} }
} }
class SnapDefaultHandler : public QWindow { void SnapDefaultHandler(const QString &protocol) {
public:
SnapDefaultHandler(const QString &protocol);
};
SnapDefaultHandler::SnapDefaultHandler(const QString &protocol) {
try { try {
const auto connection = Gio::DBus::Connection::get_sync( const auto connection = Gio::DBus::Connection::get_sync(
Gio::DBus::BusType::BUS_TYPE_SESSION); Gio::DBus::BusType::BUS_TYPE_SESSION);
@ -241,7 +235,9 @@ SnapDefaultHandler::SnapDefaultHandler(const QString &protocol) {
return; return;
} }
QEventLoop loop; const auto context = Glib::MainContext::create();
const auto loop = Glib::MainLoop::create(context);
g_main_context_push_thread_default(context->gobj());
connection->call( connection->call(
std::string(kSnapcraftSettingsObjectPath), std::string(kSnapcraftSettingsObjectPath),
@ -260,13 +256,15 @@ SnapDefaultHandler::SnapDefaultHandler(const QString &protocol) {
QString::fromStdString(e.what()))); QString::fromStdString(e.what())));
} }
loop.quit(); loop->quit();
}, },
std::string(kSnapcraftSettingsService)); std::string(kSnapcraftSettingsService));
QGuiApplicationPrivate::showModalWindow(this); QWindow window;
loop.exec(); QGuiApplicationPrivate::showModalWindow(&window);
QGuiApplicationPrivate::hideModalWindow(this); loop->run();
g_main_context_pop_thread_default(context->gobj());
QGuiApplicationPrivate::hideModalWindow(&window);
} catch (const Glib::Error &e) { } catch (const Glib::Error &e) {
LOG(("Snap Default Handler Error: %1").arg( LOG(("Snap Default Handler Error: %1").arg(
QString::fromStdString(e.what()))); QString::fromStdString(e.what())));