Pool Video Switch v2
Software video switch for distributed remote display in a lecture environment
serverapp.cpp
Go to the documentation of this file.
1 #include "../mainwindow/mainwindow.h"
2 #include "serverapp.h"
3 #include "../../shared/util.h"
4 
5 #include <QTranslator>
6 #include <QNetworkInterface>
7 #include <QSettings>
8 #include <QLibraryInfo>
9 
10 static QSize minimalGridSize(const QMap<QString, QPoint>& clientPositions, QSize& clientSize);
11 
12 ServerApp::ServerApp(int& argc, char** argv)
13  : QApplication(argc, argv)
14 {
15  setOrganizationName("openslx");
16  setOrganizationDomain("openslx.org");
17  setApplicationName("pvsmgr");
18 
20 
21  loadRooms();
22 
23  // If started in manager-only mode, and there is no current room
24  // after reading the config, exit right away
25  if (_managerOnly && _currentRoom.isEmpty()) {
26  _doExit = true;
27  return;
28  }
29 
30  // System strings
31  auto *qtTranslator = new QTranslator(this);
32  if (!qtTranslator->load(QLocale::system(), "qt", "_", QLibraryInfo::location(QLibraryInfo::TranslationsPath))) {
33  qDebug() << "Loading system translations failed" << QLibraryInfo::location(QLibraryInfo::TranslationsPath);
34  } else {
35  installTranslator(qtTranslator);
36  }
37  // App specific
38  auto *translator = new QTranslator(this);
39  if (!translator->load(QLocale::system(), ":", "l_")) {
40  qDebug() << "Loading app translations failed";
41  } else {
42  installTranslator(translator);
43  }
44 
45  /* Set the global path of the settings */
46  QSettings::setPath(QSettings::IniFormat, QSettings::SystemScope, "/opt/");
47  QSettings* sys = getSettings();
48  qDebug() << "System settings are in:" << sys->fileName();
49 
50  new MainWindow();
51 }
52 
54 {
55  QStringList rest;
56  for (const QString& a : QApplication::arguments()) {
57  if (a == QStringLiteral("--manager-only")) {
58  _managerOnly = true;
59  break;
60  } else if (a.startsWith(QStringLiteral("--config="))) {
61  _iniPath = a.mid(9);
62  } else {
63  rest << a;
64  }
65  }
66  return rest;
67 
68 }
69 
70 QStringList ServerApp::arguments()
71 {
72  return _arguments;
73 }
74 
76 {
77  QSettings* conf = getSettings();
78 
79  if (!conf->contains(QStringLiteral("rooms"))) {
80  qDebug() << "Invalid config file (no rooms are set)!";
81  return;
82  }
83  QStringList rooms = conf->value("rooms").toStringList();
84 
85  for (const QString& roomId : rooms) {
86  conf->beginGroup(roomId);
87  QString roomName = conf->value("name").toString();
88 
89  /* fallback to the old format where the room id was actually just the name */
90  if (roomName.isEmpty()) {
91  roomName = roomId;
92  }
93  if (!conf->contains("mgrIP")) {
94  qDebug() << "Warning: Incomplete config file (room " << roomName << " needs a mgrIP)!";
95  }
96  QMap<QString, QPoint> clientPositions;
97  // First store all room configurations in _rooms.
98  int size = conf->beginReadArray("client");
99  for (int j = 0; j < size; j++) {
100  conf->setArrayIndex(j);
101  clientPositions.insert(conf->value("ip").toString(), conf->value("pos").toPoint());
102  }
103  conf->endArray();
104 
105  /* read backgroundImage */
106  QString image = conf->contains("backgroundImage") ? conf->value("backgroundImage").toString() : "";
107  QString mgrIP = conf->value("mgrIP").toString();
108  QString tutorIP = conf->value("tutorIP").toString();
109 
110  QSize gridSize;
111  QSize clientSize(1, 1);
112  /* read some other properties of the room */
113  if (conf->contains("gridSize")) {
114  gridSize = conf->value("gridSize").toSize();
115  }
116  if (conf->contains("clientSize")) {
117  clientSize = conf->value("clientSize").toSize();
118  }
119 
120  foreach (const QHostAddress & address, QNetworkInterface::allAddresses()) {
121  if (!address.isBroadcast() && !address.isLoopback() && !address.isMulticast() && mgrIP == address.toString()) {
122  qDebug() << "Found own ip in config.";
123  _currentRoom = roomName;
124  }
125  }
126  conf->endGroup();
127 
128  if (!gridSize.isValid()) {
129  /* ok, let's choose the minimum gridSize to fit all clients */
130  gridSize = minimalGridSize(clientPositions, clientSize);
131  qDebug() << "had to use minimalGridSize(): = " << gridSize;
132 
133  }
134  Room* r = new Room(clientPositions, gridSize, clientSize, image, tutorIP);
135  qDebug() << "read new room: " << roomName << ": " << gridSize << ", " << clientSize;
136  _rooms.insert(roomName, r);
137  }
138 }
139 
141 {
142  auto *room = _rooms.value(_currentRoom);
143  if (room != nullptr)
144  return room;
145  static Room* defaultRoom = nullptr;
146  if (defaultRoom == nullptr) {
147  defaultRoom = new Room(QMap<QString,
148  QPoint>(), QSize(8, 6), QSize(1, 1), "", "<none>");
149  }
150  return defaultRoom;
151 }
152 void ServerApp::setSessionName(const QString& name)
153 {
154  _sessionName = name;
155  _sessionNameArray = name.toUtf8();
156 }
157 
159 {
160  const QString name = QString::number(slxrand() % 9000 + 1000);
161  _sessionName = name;
162  _sessionNameArray = name.toUtf8();
163 }
164 
166 {
167  QSettings *set;
168  if (_iniPath.isEmpty()) {
169  /* default location (system scope) */
170  set = new QSettings(QSettings::IniFormat, QSettings::SystemScope, "openslx/pvs2", "pvs2", this);
171  } else {
172  /* use _iniPath to find ini file */
173  set = new QSettings(_iniPath, QSettings::IniFormat, this);
174  }
175  set->setIniCodec("UTF-8");
176  return set;
177 }
178 
182 static QSize minimalGridSize(const QMap<QString, QPoint>& clientPositions, QSize& clientSize)
183 {
184  /* collect the maximum coordinates */
185  int x = 0;
186  int y = 0;
187 
188  for (const auto &pos : clientPositions) {
189  if (pos.x() > x) {
190  x = pos.x();
191  }
192  if (pos.y() > y) {
193  y = pos.y();
194  }
195  }
196  /* need a little extra space */
197  return QSize(x + clientSize.width(), y + clientSize.height());
198 }
199 
QStringList parseParameters()
Definition: serverapp.cpp:53
QString _iniPath
Definition: serverapp.h:54
const Room * getCurrentRoom() const
Definition: serverapp.cpp:140
bool _doExit
Definition: serverapp.h:50
bool _managerOnly
Definition: serverapp.h:52
void loadRooms()
Definition: serverapp.cpp:75
virtual QStringList arguments()
Definition: serverapp.cpp:70
static QSize minimalGridSize(const QMap< QString, QPoint > &clientPositions, QSize &clientSize)
returns the minimal grid size such that all clients fit on the grid
Definition: serverapp.cpp:182
QString _currentRoom
Definition: serverapp.h:49
#define slxrand()
Definition: util.h:19
QStringList _arguments
Definition: serverapp.h:44
void setSessionName()
Definition: serverapp.cpp:158
ServerApp(int &argc, char **argv)
Definition: serverapp.cpp:12
QByteArray _sessionNameArray
Definition: serverapp.h:47
QMap< QString, Room * > _rooms
Definition: serverapp.h:48
QSettings * getSettings()
Definition: serverapp.cpp:165
QString _sessionName
Definition: serverapp.h:46
Definition: room.h:6
Initializing MainWindow.
Definition: mainwindow.h:27