Fine tune translations loading for Chinese locales
[qBittorrent.git] / src / base / net / reverseresolution.cpp
blob8349229e31502a555183413e98c90f60b12bddf5
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
29 #include "reverseresolution.h"
31 #include <QHostInfo>
32 #include <QString>
34 const int CACHE_SIZE = 2048;
36 using namespace Net;
38 namespace
40 bool isUsefulHostName(const QString &hostname, const QHostAddress &ip)
42 return (!hostname.isEmpty() && (hostname != ip.toString()));
46 ReverseResolution::ReverseResolution(QObject *parent)
47 : QObject(parent)
49 m_cache.setMaxCost(CACHE_SIZE);
52 ReverseResolution::~ReverseResolution()
54 // abort on-going lookups instead of waiting them
55 for (auto iter = m_lookups.cbegin(); iter != m_lookups.cend(); ++iter)
56 QHostInfo::abortHostLookup(iter.key());
59 void ReverseResolution::resolve(const QHostAddress &ip)
61 const QString *hostname = m_cache.object(ip);
62 if (hostname)
64 emit ipResolved(ip, *hostname);
65 return;
68 // do reverse lookup: IP -> hostname
69 const int lookupId = QHostInfo::lookupHost(ip.toString(), this, &ReverseResolution::hostResolved);
70 m_lookups.insert(lookupId, ip);
73 void ReverseResolution::hostResolved(const QHostInfo &host)
75 const QHostAddress ip = m_lookups.take(host.lookupId());
77 if (host.error() != QHostInfo::NoError)
79 emit ipResolved(ip, {});
80 return;
83 const QString hostname = isUsefulHostName(host.hostName(), ip)
84 ? host.hostName()
85 : QString();
86 m_cache.insert(ip, new QString(hostname));
87 emit ipResolved(ip, hostname);