1 /***************************************************************************
2 * Copyright (c) 2013 Abdurrahman AVCI <abdurrahmanavci@gmail.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 ***************************************************************************/
19 
20 #include "DisplayManager.h"
21 
22 #include "DaemonApp.h"
23 #include "SeatManager.h"
24 
25 #include "displaymanageradaptor.h"
26 #include "seatadaptor.h"
27 #include "sessionadaptor.h"
28 
29 const QString DISPLAYMANAGER_SERVICE = QStringLiteral("org.freedesktop.DisplayManager");
30 const QString DISPLAYMANAGER_PATH = QStringLiteral("/org/freedesktop/DisplayManager");
31 const QString DISPLAYMANAGER_SEAT_PATH = QStringLiteral("/org/freedesktop/DisplayManager/Seat");
32 const QString DISPLAYMANAGER_SESSION_PATH = QStringLiteral("/org/freedesktop/DisplayManager/Session");
33 
34 namespace SDDM {
DisplayManager(QObject * parent)35     DisplayManager::DisplayManager(QObject *parent) : QObject(parent) {
36         // create adaptor
37         new DisplayManagerAdaptor(this);
38 
39         // register object
40         QDBusConnection connection = (daemonApp->testing()) ? QDBusConnection::sessionBus() : QDBusConnection::systemBus();
41         connection.registerService(DISPLAYMANAGER_SERVICE);
42         connection.registerObject(DISPLAYMANAGER_PATH, this);
43     }
44 
seatPath(const QString & seatName)45     QString DisplayManager::seatPath(const QString &seatName) {
46         return DISPLAYMANAGER_SEAT_PATH + seatName.mid(4);
47     }
48 
sessionPath(const QString & sessionName)49     QString DisplayManager::sessionPath(const QString &sessionName) {
50         return DISPLAYMANAGER_SESSION_PATH + sessionName.mid(7);
51     }
52 
Seats() const53     ObjectPathList DisplayManager::Seats() const {
54         ObjectPathList seats;
55 
56         for (DisplayManagerSeat *seat: m_seats)
57             seats << ObjectPath(seat->Path());
58 
59         return seats;
60     }
61 
Sessions(DisplayManagerSeat * seat) const62     ObjectPathList DisplayManager::Sessions(DisplayManagerSeat *seat) const {
63         ObjectPathList sessions;
64 
65         for (DisplayManagerSession *session: m_sessions)
66             if (seat == nullptr || seat->Name() == session->Seat())
67                 sessions << ObjectPath(session->Path());
68 
69         return sessions;
70     }
71 
AddSeat(const QString & name)72     void DisplayManager::AddSeat(const QString &name) {
73         // create seat object
74         DisplayManagerSeat *seat = new DisplayManagerSeat(name, this);
75 
76         // add to the list
77         m_seats << seat;
78 
79         // emit signal
80         emit SeatAdded(ObjectPath(seat->Path()));
81     }
82 
RemoveSeat(const QString & name)83     void DisplayManager::RemoveSeat(const QString &name) {
84         // find seat
85         for (DisplayManagerSeat *seat: m_seats) {
86             if (seat->Name() == name) {
87                 // remove from list
88                 m_seats.removeAll(seat);
89 
90                 // get object path
91                 ObjectPath path = ObjectPath(seat->Path());
92 
93                 // delete seat
94                 seat->deleteLater();
95 
96                 // emit signal
97                 emit SeatRemoved(path);
98             }
99         }
100     }
101 
AddSession(const QString & name,const QString & seat,const QString & user)102     void DisplayManager::AddSession(const QString &name, const QString &seat, const QString &user) {
103         // create session object
104         DisplayManagerSession *session = new DisplayManagerSession(name, seat, user, this);
105 
106         // add to the list
107         m_sessions << session;
108 
109         // emit signal
110         emit SessionAdded(ObjectPath(session->Path()));
111     }
112 
RemoveSession(const QString & name)113     void DisplayManager::RemoveSession(const QString &name) {
114         // find session
115         for (DisplayManagerSession *session: m_sessions) {
116             if (session->Name() == name) {
117                 // remove from list
118                 m_sessions.removeAll(session);
119 
120                 // get object path
121                 ObjectPath path = ObjectPath(session->Path());
122 
123                 // delete session
124                 session->deleteLater();
125 
126                 // emit signal
127                 emit SessionRemoved(path);
128             }
129         }
130     }
131 
DisplayManagerSeat(const QString & name,QObject * parent)132     DisplayManagerSeat::DisplayManagerSeat(const QString &name, QObject *parent)
133         : QObject(parent), m_name(name), m_path(DISPLAYMANAGER_SEAT_PATH + name.mid(4)) {
134         // create adaptor
135         new SeatAdaptor(this);
136 
137         // register object
138         QDBusConnection connection = (daemonApp->testing()) ? QDBusConnection::sessionBus() : QDBusConnection::systemBus();
139         connection.registerService(DISPLAYMANAGER_SERVICE);
140         connection.registerObject(m_path, this);
141     }
142 
Name() const143     const QString &DisplayManagerSeat::Name() const {
144         return m_name;
145     }
146 
Path() const147     const QString &DisplayManagerSeat::Path() const {
148         return m_path;
149     }
150 
SwitchToGreeter()151     void DisplayManagerSeat::SwitchToGreeter() {
152         daemonApp->seatManager()->switchToGreeter(m_name);
153     }
154 
SwitchToGuest(const QString &)155     void DisplayManagerSeat::SwitchToGuest(const QString &/*session*/) {
156         // TODO: IMPLEMENT
157     }
158 
SwitchToUser(const QString &,const QString &)159     void DisplayManagerSeat::SwitchToUser(const QString &/*user*/, const QString &/*session*/) {
160         // TODO: IMPLEMENT
161     }
162 
Lock()163     void DisplayManagerSeat::Lock() {
164         // TODO: IMPLEMENT
165     }
166 
Sessions()167     ObjectPathList DisplayManagerSeat::Sessions() {
168        return daemonApp->displayManager()->Sessions(this);
169     }
170 
DisplayManagerSession(const QString & name,const QString & seat,const QString & user,QObject * parent)171     DisplayManagerSession::DisplayManagerSession(const QString &name, const QString &seat, const QString &user, QObject *parent)
172         : QObject(parent), m_name(name), m_path(DISPLAYMANAGER_SESSION_PATH + name.mid(7)), m_seat(seat), m_user(user) {
173         // create adaptor
174         new SessionAdaptor(this);
175 
176         // register object
177         QDBusConnection connection = (daemonApp->testing()) ? QDBusConnection::sessionBus() : QDBusConnection::systemBus();
178         connection.registerService(DISPLAYMANAGER_SERVICE);
179         connection.registerObject(m_path, this);
180     }
181 
Name() const182     const QString &DisplayManagerSession::Name() const {
183         return m_name;
184     }
185 
Path() const186     const QString &DisplayManagerSession::Path() const {
187         return m_path;
188     }
189 
Seat() const190     const QString &DisplayManagerSession::Seat() const {
191         return m_seat;
192     }
193 
Lock()194     void DisplayManagerSession::Lock() {
195         // TODO: IMPLEMENT
196     }
197 
SeatPath() const198     ObjectPath DisplayManagerSession::SeatPath() const {
199         return ObjectPath(DISPLAYMANAGER_SEAT_PATH + m_seat.mid(4));
200     }
201 
User() const202     const QString &DisplayManagerSession::User() const {
203         return m_user;
204     }
205 }
206