summaryrefslogtreecommitdiff
path: root/lockscreen
diff options
authorAndrea Azzarone <azzaronea@gmail.com>2014-10-10 13:35:40 +0200
committerAndrea Azzarone <azzaronea@gmail.com>2014-10-10 13:35:40 +0200
commit30cc2d558e960c15da9b924b8ce087c5a4806da9 (patch)
tree51070ec425ec9bd30f6e14c87252ce780a7b505f /lockscreen
parentae8df2fc2ea163ce892890ac0e1dea72d74f5d59 (diff)
Implement a shutdown notifier.
(bzr r3874.1.1)
Diffstat (limited to 'lockscreen')
-rw-r--r--lockscreen/CMakeLists.txt1
-rw-r--r--lockscreen/ShutdownNotifier.cpp128
-rw-r--r--lockscreen/ShutdownNotifier.h49
3 files changed, 178 insertions, 0 deletions
diff --git a/lockscreen/CMakeLists.txt b/lockscreen/CMakeLists.txt
index 47c1c54ed..7b4bde35a 100644
--- a/lockscreen/CMakeLists.txt
+++ b/lockscreen/CMakeLists.txt
@@ -27,6 +27,7 @@ set (LOCKSCREEN_SOURCES
LockScreenAcceleratorController.cpp
LockScreenAccelerators.cpp
ScreenSaverDBusManager.cpp
+ ShutdownNotifier.cpp
UserAuthenticatorPam.cpp
UserPromptView.cpp
)
diff --git a/lockscreen/ShutdownNotifier.cpp b/lockscreen/ShutdownNotifier.cpp
new file mode 100644
index 000000000..5a82d73d5
--- /dev/null
+++ b/lockscreen/ShutdownNotifier.cpp
@@ -0,0 +1,128 @@
+/*
+ * Copyright (C) 2014 Canonical Ltd
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authored by: Andrea Azzarone <andrea.azzarone@canonical.com>
+ */
+
+#include "ShutdownNotifier.h"
+
+#include "UnityCore/GLibDBusProxy.h"
+
+namespace unity
+{
+namespace lockscreen
+{
+
+//
+// Private Implementation
+//
+
+class ShutdownNotifier::Impl
+{
+public:
+ Impl();
+ ~Impl();
+
+ bool RegisterInterest(ShutdownCallback const& cb);
+ void UnregisterInterest();
+
+private:
+ std::shared_ptr<glib::DBusProxy> logind_proxy_;
+ ShutdownCallback cb_;
+ gint delay_inhibit_fd_;
+};
+
+ShutdownNotifier::Impl::Impl()
+ : logind_proxy_(std::make_shared<glib::DBusProxy>("org.freedesktop.login1",
+ "/org/freedesktop/login1",
+ "org.freedesktop.login1.Manager",
+ G_BUS_TYPE_SYSTEM))
+ , delay_inhibit_fd_(-1)
+{}
+
+ShutdownNotifier::Impl::~Impl()
+{
+ UnregisterInterest();
+}
+
+bool ShutdownNotifier::Impl::RegisterInterest(ShutdownCallback const& cb)
+{
+ if (!cb or cb_)
+ return false;
+
+ cb_ = cb;
+
+ std::vector<std::string> parameters;
+ parameters.push_back("shutdown"); // what
+ parameters.push_back("Unity Lockscreen"); // who
+ parameters.push_back("Screen Locked"); // why
+ parameters.push_back("delay"); // mode
+
+ logind_proxy_->CallWithUnixFdList("Inhibit",
+ glib::Variant::FromVector(parameters),
+ [this](GVariant* variant, glib::Error const& e){
+ // FIXME: we should handle the error.
+ delay_inhibit_fd_ = glib::Variant(variant).GetUInt32();
+ });
+
+ logind_proxy_->Connect("PrepareForShutdown", [this](GVariant* variant) {
+ bool active = glib::Variant(variant).GetBool();
+
+ if (active)
+ cb_();
+
+ UnregisterInterest();
+ });
+
+ return true;
+}
+
+void ShutdownNotifier::Impl::UnregisterInterest()
+{
+ if (!cb_)
+ return;
+
+ logind_proxy_->DisconnectSignal("PrepareForShutdown");
+
+ if (delay_inhibit_fd_ != -1)
+ close(delay_inhibit_fd_);
+
+ cb_ = 0;
+ delay_inhibit_fd_ = -1;
+}
+
+//
+// End Private Implementation
+//
+
+ShutdownNotifier::ShutdownNotifier()
+ : pimpl_(new(Impl))
+{}
+
+ShutdownNotifier::~ShutdownNotifier()
+{}
+
+bool ShutdownNotifier::RegisterInterest(ShutdownCallback const& cb)
+{
+ return pimpl_->RegisterInterest(cb);
+}
+
+void ShutdownNotifier::UnregisterInterest()
+{
+ pimpl_->UnregisterInterest();
+}
+
+}
+}
diff --git a/lockscreen/ShutdownNotifier.h b/lockscreen/ShutdownNotifier.h
new file mode 100644
index 000000000..7c502a013
--- /dev/null
+++ b/lockscreen/ShutdownNotifier.h
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2014 Canonical Ltd
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authored by: Andrea Azzarone <andrea.azzarone@canonical.com>
+ */
+
+#ifndef UNITY_LOCKSCREEN_SHUTDOWN_NOTIFIER
+#define UNITY_LOCKSCREEN_SHUTDOWN_NOTIFIER
+
+#include <memory>
+#include <functional>
+
+namespace unity
+{
+namespace lockscreen
+{
+
+typedef std::function<void()> ShutdownCallback;
+
+class ShutdownNotifier
+{
+public:
+ ShutdownNotifier();
+ ~ShutdownNotifier();
+
+ bool RegisterInterest(ShutdownCallback const& cb);
+ void UnregisterInterest();
+
+private:
+ class Impl;
+ std::unique_ptr<Impl> pimpl_;
+};
+
+}
+}
+
+#endif