mirror of
https://bitbucket.org/smil3y/kde-workspace.git
synced 2025-02-23 10:22:49 +00:00
kwin: make use of QHostInfo::localHostName()
Signed-off-by: Ivailo Monev <xakepa10@laimg.moc>
This commit is contained in:
parent
4d11ad9d72
commit
fa976538d8
3 changed files with 2 additions and 198 deletions
|
@ -24,147 +24,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
// KDE
|
// KDE
|
||||||
#include <KDebug>
|
#include <KDebug>
|
||||||
// Qt
|
// Qt
|
||||||
#include <QtConcurrentRun>
|
#include <QHostInfo>
|
||||||
#include <QFutureWatcher>
|
|
||||||
// system
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <netdb.h>
|
|
||||||
|
|
||||||
namespace KWin {
|
namespace KWin {
|
||||||
|
|
||||||
static QByteArray getHostName()
|
|
||||||
{
|
|
||||||
#ifdef HOST_NAME_MAX
|
|
||||||
char hostnamebuf[HOST_NAME_MAX];
|
|
||||||
#else
|
|
||||||
char hostnamebuf[256];
|
|
||||||
#endif
|
|
||||||
if (gethostname(hostnamebuf, sizeof hostnamebuf) >= 0) {
|
|
||||||
hostnamebuf[sizeof(hostnamebuf)-1] = 0;
|
|
||||||
return QByteArray(hostnamebuf);
|
|
||||||
}
|
|
||||||
return QByteArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
GetAddrInfo::GetAddrInfo(const QByteArray &hostName, QObject *parent)
|
|
||||||
: QObject(parent)
|
|
||||||
, m_resolving(false)
|
|
||||||
, m_resolved(false)
|
|
||||||
, m_ownResolved(false)
|
|
||||||
, m_hostName(hostName)
|
|
||||||
, m_addressHints(new addrinfo)
|
|
||||||
, m_address(NULL)
|
|
||||||
, m_ownAddress(NULL)
|
|
||||||
, m_watcher(new QFutureWatcher<int>(this))
|
|
||||||
, m_ownAddressWatcher(new QFutureWatcher<int>(this))
|
|
||||||
{
|
|
||||||
// watcher will be deleted together with the GetAddrInfo once the future
|
|
||||||
// got canceled or finished
|
|
||||||
connect(m_watcher, SIGNAL(canceled()), SLOT(deleteLater()));
|
|
||||||
connect(m_watcher, SIGNAL(finished()), SLOT(slotResolved()));
|
|
||||||
connect(m_ownAddressWatcher, SIGNAL(canceled()), SLOT(deleteLater()));
|
|
||||||
connect(m_ownAddressWatcher, SIGNAL(finished()), SLOT(slotOwnAddressResolved()));
|
|
||||||
}
|
|
||||||
|
|
||||||
GetAddrInfo::~GetAddrInfo()
|
|
||||||
{
|
|
||||||
if (m_watcher && m_watcher->isRunning()) {
|
|
||||||
m_watcher->cancel();
|
|
||||||
}
|
|
||||||
if (m_ownAddressWatcher && m_ownAddressWatcher->isRunning()) {
|
|
||||||
m_ownAddressWatcher->cancel();
|
|
||||||
}
|
|
||||||
if (m_address) {
|
|
||||||
freeaddrinfo(m_address);
|
|
||||||
}
|
|
||||||
if (m_ownAddress) {
|
|
||||||
freeaddrinfo(m_ownAddress);
|
|
||||||
}
|
|
||||||
delete m_addressHints;
|
|
||||||
}
|
|
||||||
|
|
||||||
void GetAddrInfo::resolve()
|
|
||||||
{
|
|
||||||
if (m_resolving) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
m_resolving = true;
|
|
||||||
memset(m_addressHints, 0, sizeof(*m_addressHints));
|
|
||||||
m_addressHints->ai_family = PF_UNSPEC;
|
|
||||||
m_addressHints->ai_socktype = SOCK_STREAM;
|
|
||||||
m_addressHints->ai_flags |= AI_CANONNAME;
|
|
||||||
|
|
||||||
// TODO: C++11 nullptr
|
|
||||||
const char* nullPtr = NULL;
|
|
||||||
m_watcher->setFuture(QtConcurrent::run(getaddrinfo, m_hostName, nullPtr, m_addressHints, &m_address));
|
|
||||||
m_ownAddressWatcher->setFuture(QtConcurrent::run(getaddrinfo, getHostName(), nullPtr, m_addressHints, &m_ownAddress));
|
|
||||||
}
|
|
||||||
|
|
||||||
void GetAddrInfo::slotResolved()
|
|
||||||
{
|
|
||||||
if (resolved(m_watcher)) {
|
|
||||||
m_resolved = true;
|
|
||||||
compare();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void GetAddrInfo::slotOwnAddressResolved()
|
|
||||||
{
|
|
||||||
if (resolved(m_ownAddressWatcher)) {
|
|
||||||
m_ownResolved = true;
|
|
||||||
compare();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool GetAddrInfo::resolved(QFutureWatcher< int >* watcher)
|
|
||||||
{
|
|
||||||
if (!watcher->isFinished()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (watcher->result() != 0) {
|
|
||||||
kDebug(1212) << "getaddrinfo failed with error:" << gai_strerror(watcher->result());
|
|
||||||
// call failed;
|
|
||||||
deleteLater();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void GetAddrInfo::compare()
|
|
||||||
{
|
|
||||||
if (!m_resolved || !m_ownResolved) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
addrinfo *address = m_address;
|
|
||||||
while (address) {
|
|
||||||
if (address->ai_canonname && m_hostName == QByteArray(address->ai_canonname).toLower()) {
|
|
||||||
addrinfo *ownAddress = m_ownAddress;
|
|
||||||
bool localFound = false;
|
|
||||||
while (ownAddress) {
|
|
||||||
if (ownAddress->ai_canonname && QByteArray(ownAddress->ai_canonname).toLower() == m_hostName) {
|
|
||||||
localFound = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
ownAddress = ownAddress->ai_next;
|
|
||||||
}
|
|
||||||
if (localFound) {
|
|
||||||
emit local();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
address = address->ai_next;
|
|
||||||
}
|
|
||||||
deleteLater();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
ClientMachine::ClientMachine(QObject *parent)
|
ClientMachine::ClientMachine(QObject *parent)
|
||||||
: QObject(parent)
|
: QObject(parent)
|
||||||
, m_localhost(false)
|
, m_localhost(false)
|
||||||
, m_resolved(false)
|
, m_resolved(false)
|
||||||
, m_resolving(false)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -198,7 +65,7 @@ void ClientMachine::checkForLocalhost()
|
||||||
// nothing to do
|
// nothing to do
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
QByteArray host = getHostName();
|
QByteArray host = QHostInfo::localHostName().toAscii();
|
||||||
|
|
||||||
if (!host.isEmpty()) {
|
if (!host.isEmpty()) {
|
||||||
host = host.toLower();
|
host = host.toLower();
|
||||||
|
@ -211,16 +78,7 @@ void ClientMachine::checkForLocalhost()
|
||||||
*dot = '\0';
|
*dot = '\0';
|
||||||
if (host == lowerHostName) {
|
if (host == lowerHostName) {
|
||||||
setLocal();
|
setLocal();
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
m_resolving = true;
|
|
||||||
// check using information from get addr info
|
|
||||||
// GetAddrInfo gets automatically destroyed once it finished or not
|
|
||||||
GetAddrInfo *info = new GetAddrInfo(lowerHostName, this);
|
|
||||||
connect(info, SIGNAL(local()), SLOT(setLocal()));
|
|
||||||
connect(info, SIGNAL(destroyed(QObject*)), SLOT(resolveFinished()));
|
|
||||||
info->resolve();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -231,9 +89,4 @@ void ClientMachine::setLocal()
|
||||||
emit localhostChanged();
|
emit localhostChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClientMachine::resolveFinished()
|
|
||||||
{
|
|
||||||
m_resolving = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
|
@ -23,42 +23,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <xcb/xcb.h>
|
#include <xcb/xcb.h>
|
||||||
|
|
||||||
// forward declaration
|
|
||||||
struct addrinfo;
|
|
||||||
#include <QFutureWatcher>
|
|
||||||
|
|
||||||
namespace KWin {
|
namespace KWin {
|
||||||
|
|
||||||
class GetAddrInfo : public QObject
|
|
||||||
{
|
|
||||||
Q_OBJECT
|
|
||||||
public:
|
|
||||||
explicit GetAddrInfo(const QByteArray &hostName, QObject *parent = NULL);
|
|
||||||
virtual ~GetAddrInfo();
|
|
||||||
|
|
||||||
void resolve();
|
|
||||||
|
|
||||||
Q_SIGNALS:
|
|
||||||
void local();
|
|
||||||
|
|
||||||
private Q_SLOTS:
|
|
||||||
void slotResolved();
|
|
||||||
void slotOwnAddressResolved();
|
|
||||||
|
|
||||||
private:
|
|
||||||
void compare();
|
|
||||||
bool resolved(QFutureWatcher<int> *watcher);
|
|
||||||
bool m_resolving;
|
|
||||||
bool m_resolved;
|
|
||||||
bool m_ownResolved;
|
|
||||||
QByteArray m_hostName;
|
|
||||||
addrinfo *m_addressHints;
|
|
||||||
addrinfo *m_address;
|
|
||||||
addrinfo *m_ownAddress;
|
|
||||||
QFutureWatcher<int> *m_watcher;
|
|
||||||
QFutureWatcher<int> *m_ownAddressWatcher;
|
|
||||||
};
|
|
||||||
|
|
||||||
class ClientMachine : public QObject
|
class ClientMachine : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
@ -70,21 +36,18 @@ public:
|
||||||
const QByteArray &hostName() const;
|
const QByteArray &hostName() const;
|
||||||
bool isLocal() const;
|
bool isLocal() const;
|
||||||
static QByteArray localhost();
|
static QByteArray localhost();
|
||||||
bool isResolving() const;
|
|
||||||
|
|
||||||
Q_SIGNALS:
|
Q_SIGNALS:
|
||||||
void localhostChanged();
|
void localhostChanged();
|
||||||
|
|
||||||
private Q_SLOTS:
|
private Q_SLOTS:
|
||||||
void setLocal();
|
void setLocal();
|
||||||
void resolveFinished();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void checkForLocalhost();
|
void checkForLocalhost();
|
||||||
QByteArray m_hostName;
|
QByteArray m_hostName;
|
||||||
bool m_localhost;
|
bool m_localhost;
|
||||||
bool m_resolved;
|
bool m_resolved;
|
||||||
bool m_resolving;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
inline
|
inline
|
||||||
|
@ -105,12 +68,6 @@ QByteArray ClientMachine::localhost()
|
||||||
return "localhost";
|
return "localhost";
|
||||||
}
|
}
|
||||||
|
|
||||||
inline
|
|
||||||
bool ClientMachine::isResolving() const
|
|
||||||
{
|
|
||||||
return m_resolving;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -129,12 +129,6 @@ void TestClientMachine::hostName()
|
||||||
clientMachine.resolve(window, XCB_WINDOW_NONE);
|
clientMachine.resolve(window, XCB_WINDOW_NONE);
|
||||||
QTEST(clientMachine.hostName(), "expectedHost");
|
QTEST(clientMachine.hostName(), "expectedHost");
|
||||||
|
|
||||||
int i=0;
|
|
||||||
while (clientMachine.isResolving() && i++ < 50) {
|
|
||||||
// name is being resolved in an external thread, so let's wait a little bit
|
|
||||||
QTest::qWait(250);
|
|
||||||
}
|
|
||||||
|
|
||||||
QCOMPARE(clientMachine.isLocal(), local);
|
QCOMPARE(clientMachine.isLocal(), local);
|
||||||
QCOMPARE(spy.isEmpty(), !local);
|
QCOMPARE(spy.isEmpty(), !local);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue