1 /*
2    Drawpile - a collaborative drawing program.
3 
4    Copyright (C) 2014-2019 Calle Laakkonen
5 
6    Drawpile is free software: you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation, either version 3 of the License, or
9    (at your option) any later version.
10 
11    Drawpile is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with Drawpile.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #ifndef LOGINSESSIONS_H
21 #define LOGINSESSIONS_H
22 
23 #include <QAbstractTableModel>
24 #include <QVector>
25 
26 namespace net {
27 
28 /**
29  * @brief Available session description
30  */
31 struct LoginSession {
32 	QString id;
33 	QString alias;
34 	QString title;
35 	QString founder;
36 	QString incompatibleSeries; // if not empty, this session is for a different version series
37 
38 	int userCount;
39 
40 	bool needPassword;
41 	bool persistent;
42 	bool closed;
43 
44 	bool nsfm;
45 
idOrAliasLoginSession46 	QString idOrAlias() const { return alias.isEmpty() ? id : alias; }
isIdOrAliasLoginSession47 	inline bool isIdOrAlias(const QString &idOrAlias) const {
48 		Q_ASSERT(!idOrAlias.isEmpty());
49 		return id == idOrAlias || alias == idOrAlias;
50 	}
51 };
52 
53 /**
54  * @brief List of available sessions
55  */
56 class LoginSessionModel : public QAbstractTableModel
57 {
58 	Q_OBJECT
59 public:
60 	enum LoginSessionRoles {
61 		IdRole = Qt::UserRole,     // Session ID
62 		IdAliasRole,               // ID alias
63 		AliasOrIdRole,             // Alias or session ID
64 		UserCountRole,             // Number of logged in users
65 		TitleRole,                 // Session title
66 		FounderRole,               // Name of session founder
67 		NeedPasswordRole,          // Is a password needed to join
68 		PersistentRole,            // Is this a persistent session
69 		ClosedRole,                // Is this session closed to new users
70 		IncompatibleRole,          // Is the session meant for some other client version
71 		JoinableRole,              // Is this session joinable
72 		NsfmRole                   // Is this session tagged as Not Suitable For Minors
73 	};
74 
75 	explicit LoginSessionModel(QObject *parent=nullptr);
76 
77 	void setModeratorMode(bool mod);
isModeratorMode()78 	bool isModeratorMode() const { return m_moderatorMode; }
79 
80 	int rowCount(const QModelIndex &parent=QModelIndex()) const;
81 	int columnCount(const QModelIndex &parent=QModelIndex()) const;
82 	QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const;
83 	QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const;
84 	Qt::ItemFlags flags(const QModelIndex &index) const;
85 
86 	void updateSession(const LoginSession &session);
87 	void removeSession(const QString &id);
88 
getFirstSession()89 	LoginSession getFirstSession() const { return m_sessions.isEmpty() ? LoginSession() : m_sessions.first(); }
90 
91 private:
92 	QVector<LoginSession> m_sessions;
93 	bool m_moderatorMode;
94 };
95 
96 }
97 
98 #endif
99