issue #65 - Auto config feature using Zeroconf/Bonjour [no-build]

added massage box to ask user where to add client
This commit is contained in:
jerry 2014-08-26 14:49:27 +00:00
parent aa2accf5a6
commit cdc740c5d1
4 changed files with 107 additions and 16 deletions

View File

@ -291,7 +291,7 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="1" column="0"> <item row="3" column="0">
<widget class="QCheckBox" name="m_pAutoConnectCheckBox"> <widget class="QCheckBox" name="m_pAutoConnectCheckBox">
<property name="text"> <property name="text">
<string>Auto connect</string> <string>Auto connect</string>

View File

@ -65,7 +65,7 @@ MainWindow::MainWindow(QSettings& settings, AppConfig& appConfig) :
m_AppConfig(appConfig), m_AppConfig(appConfig),
m_pSynergy(NULL), m_pSynergy(NULL),
m_SynergyState(synergyDisconnected), m_SynergyState(synergyDisconnected),
m_ServerConfig(&m_Settings, 5, 3, m_AppConfig.screenName()), m_ServerConfig(&m_Settings, 5, 3, m_AppConfig.screenName(), this),
m_pTempConfigFile(NULL), m_pTempConfigFile(NULL),
m_pTrayIcon(NULL), m_pTrayIcon(NULL),
m_pTrayIconMenu(NULL), m_pTrayIconMenu(NULL),
@ -880,15 +880,17 @@ void MainWindow::autoAddScreen(const QString name)
int r = m_ServerConfig.autoAddScreen(name); int r = m_ServerConfig.autoAddScreen(name);
if (r != kAutoAddScreenOk) { if (r != kAutoAddScreenOk) {
switch (r) { switch (r) {
case kAutoAddScreenNoServer: case kAutoAddScreenManualServer:
showConfigureServer( showConfigureServer(
tr("Please add the server (%1) to the grid.") tr("Please add the server (%1) to the grid.")
.arg(appConfig().screenName())); .arg(appConfig().screenName()));
break; break;
case kAutoAddScreenNoSpace: case kAutoAddScreenManualClient:
showConfigureServer( showConfigureServer(
tr("Please add the client (%1) to the grid.").arg(name)); tr("Please drag the new client screen (%1) "
"to the desired position on the grid.")
.arg(name));
break; break;
} }
} }

View File

@ -18,8 +18,12 @@
#include "ServerConfig.h" #include "ServerConfig.h"
#include "Hotkey.h" #include "Hotkey.h"
#include "MainWindow.h"
#include <QtCore> #include <QtCore>
#include <QMessageBox>
#include <QAbstractButton>
#include <QPushButton>
static const struct static const struct
{ {
@ -35,14 +39,23 @@ static const struct
}; };
enum {
kAddClientRight,
kAddClientLeft,
kAddClientOther,
kAddClientIgnore
};
const int serverDefaultIndex = 7; const int serverDefaultIndex = 7;
ServerConfig::ServerConfig(QSettings* settings, int numColumns, int numRows , QString serverName) : ServerConfig::ServerConfig(QSettings* settings, int numColumns, int numRows ,
QString serverName, MainWindow* mainWindow) :
m_pSettings(settings), m_pSettings(settings),
m_Screens(), m_Screens(),
m_NumColumns(numColumns), m_NumColumns(numColumns),
m_NumRows(numRows), m_NumRows(numRows),
m_ServerName(serverName) m_ServerName(serverName),
m_pMainWindow(mainWindow)
{ {
Q_ASSERT(m_pSettings); Q_ASSERT(m_pSettings);
@ -273,7 +286,7 @@ int ServerConfig::autoAddScreen(const QString name)
int targetIndex = -1; int targetIndex = -1;
if (!findScreenName(m_ServerName, serverIndex)) { if (!findScreenName(m_ServerName, serverIndex)) {
if (!fixNoServer(m_ServerName, serverIndex)) { if (!fixNoServer(m_ServerName, serverIndex)) {
return kAutoAddScreenNoServer; return kAutoAddScreenManualServer;
} }
} }
if (findScreenName(name, targetIndex)) { if (findScreenName(name, targetIndex)) {
@ -281,18 +294,44 @@ int ServerConfig::autoAddScreen(const QString name)
return kAutoAddScreenOk; return kAutoAddScreenOk;
} }
int result = showAddClientMsgBox(name);
if (result == kAddClientIgnore) {
return kAutoAddScreenIgnore;
}
if (result == kAddClientOther) {
addToFirstEmptyGrid(name);
return kAutoAddScreenManualClient;
}
bool success = false; bool success = false;
for (unsigned int i = 0; i < sizeof(neighbourDirs) / sizeof(neighbourDirs[0]); i++) { int startIndex = serverIndex;
int idx = adjacentScreenIndex(serverIndex, neighbourDirs[i].x, neighbourDirs[i].y); int offset = 1;
if (idx != -1 && screens()[idx].isNull()) { int dirIndex = 0;
if (result == kAddClientLeft) {
offset = -1;
dirIndex = 1;
}
int idx = adjacentScreenIndex(startIndex, neighbourDirs[dirIndex].x,
neighbourDirs[dirIndex].y);
while (idx != -1) {
if (screens()[idx].isNull()) {
m_Screens[idx].setName(name); m_Screens[idx].setName(name);
success = true; success = true;
break; break;
} }
startIndex += offset;
idx = adjacentScreenIndex(startIndex, neighbourDirs[dirIndex].x,
neighbourDirs[dirIndex].y);
} }
if (!success) { if (!success) {
return kAutoAddScreenNoSpace; addToFirstEmptyGrid(name);
return kAutoAddScreenManualClient;
} }
saveSettings(); saveSettings();
@ -303,7 +342,6 @@ bool ServerConfig::findScreenName(const QString& name, int& index)
{ {
bool found = false; bool found = false;
for (int i = 0; i < screens().size(); i++) { for (int i = 0; i < screens().size(); i++) {
QString test = screens()[i].name();
if (!screens()[i].isNull() && if (!screens()[i].isNull() &&
screens()[i].name().compare(name) == 0) { screens()[i].name().compare(name) == 0) {
index = i; index = i;
@ -325,3 +363,48 @@ bool ServerConfig::fixNoServer(const QString& name, int& index)
return fixed; return fixed;
} }
int ServerConfig::showAddClientMsgBox(const QString& clientName)
{
int result = kAddClientIgnore;
QWidget* w = dynamic_cast<QWidget*>(m_pMainWindow);
QMessageBox msgBox(w);
msgBox.setIcon(QMessageBox::Question);
msgBox.setWindowTitle(QObject::tr("Incoming client"));
msgBox.setText(QObject::tr(
"A new client wants to connect. Which side\n"
"of this screen is the client (%1) located?")
.arg(clientName));
QPushButton* left = msgBox.addButton(QObject::tr("Left"), QMessageBox::AcceptRole);
QPushButton* right = msgBox.addButton(QObject::tr("Right"), QMessageBox::AcceptRole);
QPushButton* other = msgBox.addButton(QObject::tr("Other"), QMessageBox::AcceptRole);
QPushButton* ignore = msgBox.addButton(QObject::tr("Ignore"), QMessageBox::RejectRole);
msgBox.setDefaultButton(ignore);
msgBox.exec();
QAbstractButton* button = msgBox.clickedButton();
QPushButton* clickedButton = dynamic_cast<QPushButton*>(button);
if (clickedButton == right) {
result = kAddClientRight;
}
else if (clickedButton == left) {
result = kAddClientLeft;
}
else if (clickedButton == other) {
result = kAddClientOther;
}
return result;
}
void::ServerConfig::addToFirstEmptyGrid(const QString &clientName)
{
for (int i = 0; i < screens().size(); i++) {
if (screens()[i].isNull()) {
m_Screens[i].setName(clientName);
break;
}
}
}

View File

@ -31,6 +31,7 @@ class QSettings;
class QString; class QString;
class QFile; class QFile;
class ServerConfigDialog; class ServerConfigDialog;
class MainWindow;
class ServerConfig : public BaseConfig class ServerConfig : public BaseConfig
{ {
@ -38,7 +39,8 @@ class ServerConfig : public BaseConfig
friend QTextStream& operator<<(QTextStream& outStream, const ServerConfig& config); friend QTextStream& operator<<(QTextStream& outStream, const ServerConfig& config);
public: public:
ServerConfig(QSettings* settings, int numColumns, int numRows, QString serverName); ServerConfig(QSettings* settings, int numColumns, int numRows,
QString serverName, MainWindow* mainWindow);
~ServerConfig(); ~ServerConfig();
public: public:
@ -93,6 +95,8 @@ class ServerConfig : public BaseConfig
private: private:
bool findScreenName(const QString& name, int& index); bool findScreenName(const QString& name, int& index);
bool fixNoServer(const QString& name, int& index); bool fixNoServer(const QString& name, int& index);
int showAddClientMsgBox(const QString& clientName);
void addToFirstEmptyGrid(const QString& clientName);
private: private:
QSettings* m_pSettings; QSettings* m_pSettings;
@ -112,14 +116,16 @@ class ServerConfig : public BaseConfig
QList<bool> m_SwitchCorners; QList<bool> m_SwitchCorners;
HotkeyList m_Hotkeys; HotkeyList m_Hotkeys;
QString m_ServerName; QString m_ServerName;
MainWindow* m_pMainWindow;
}; };
QTextStream& operator<<(QTextStream& outStream, const ServerConfig& config); QTextStream& operator<<(QTextStream& outStream, const ServerConfig& config);
enum { enum {
kAutoAddScreenOk, kAutoAddScreenOk,
kAutoAddScreenNoServer, kAutoAddScreenManualServer,
kAutoAddScreenNoSpace kAutoAddScreenManualClient,
kAutoAddScreenIgnore
}; };
#endif #endif