File indexing completed on 2024-04-14 15:49:46

0001 // SPDX-License-Identifier: GPL-3.0-or-later
0002 /*
0003   Copyright 2017 Martin Koller, kollix@aon.at
0004 
0005   This file is part of liquidshell.
0006 
0007   liquidshell is free software: you can redistribute it and/or modify
0008   it under the terms of the GNU General Public License as published by
0009   the Free Software Foundation, either version 3 of the License, or
0010   (at your option) any later version.
0011 
0012   liquidshell is distributed in the hope that it will be useful,
0013   but WITHOUT ANY WARRANTY; without even the implied warranty of
0014   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0015   GNU General Public License for more details.
0016 
0017   You should have received a copy of the GNU General Public License
0018   along with liquidshell.  If not, see <http://www.gnu.org/licenses/>.
0019 */
0020 
0021 #include <LockLogout.hxx>
0022 
0023 #include <QGridLayout>
0024 #include <QIcon>
0025 #include <QDBusConnection>
0026 #include <QDBusMessage>
0027 
0028 #include <KLocalizedString>
0029 
0030 //--------------------------------------------------------------------------------
0031 
0032 LockLogout::LockLogout(DesktopPanel *parent)
0033   : QWidget(parent)
0034 {
0035   setObjectName("LockLogout");
0036 
0037   QGridLayout *grid = new QGridLayout(this);
0038   grid->setContentsMargins(QMargins());
0039   grid->setSpacing(2);
0040 
0041   lock   = new QToolButton;
0042   logout = new QToolButton;
0043 
0044   lock->setIcon(QIcon::fromTheme("system-lock-screen"));
0045   logout->setIcon(QIcon::fromTheme("system-shutdown"));
0046 
0047   lock->setIconSize(QSize(22, 22));
0048   logout->setIconSize(QSize(22, 22));
0049 
0050   lock->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
0051   logout->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
0052 
0053   connect(lock, &QToolButton::clicked,
0054           []()
0055           {
0056             QDBusConnection::sessionBus().send(
0057                 QDBusMessage::createMethodCall("org.freedesktop.ScreenSaver", "/ScreenSaver",
0058                                                "org.freedesktop.ScreenSaver", "Lock"));
0059           });
0060 
0061   connect(logout, &QToolButton::clicked,
0062           []()
0063           {
0064             QDBusMessage msg = QDBusMessage::createMethodCall("org.kde.ksmserver", "/KSMServer",
0065                                                               "org.kde.KSMServerInterface", "logout");
0066             msg << -1 << 0 << 0;  // plasma-workspace/libkworkspace/kworkspace.h
0067 
0068             QDBusConnection::sessionBus().send(msg);
0069           });
0070 
0071   fill(parent->getRows());
0072   connect(parent, &DesktopPanel::rowsChanged, this, &LockLogout::fill);
0073 }
0074 
0075 //--------------------------------------------------------------------------------
0076 
0077 void LockLogout::fill(int rows)
0078 {
0079   QGridLayout *grid = static_cast<QGridLayout *>(layout());
0080   delete grid->takeAt(0);
0081   delete grid->takeAt(1);
0082 
0083   if ( rows == 1 )
0084   {
0085     grid->addWidget(lock, 0, 0);
0086     grid->addWidget(logout, 0, 1);
0087   }
0088   else
0089   {
0090     grid->addWidget(lock, 0, 0);
0091     grid->addWidget(logout, 1, 0);
0092   }
0093 }
0094 
0095 //--------------------------------------------------------------------------------