1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

Some code style fixes

This commit is contained in:
Zetok Zalbavar 2015-04-20 08:58:06 +01:00
parent 228398d5de
commit 33186e51dc
No known key found for this signature in database
GPG Key ID: C953D3880212068A
3 changed files with 123 additions and 44 deletions

View File

@ -172,6 +172,7 @@ void Core::make_tox(QByteArray savedata)
toxOptions.proxy_type = TOX_PROXY_TYPE_SOCKS5;
else if (proxyType == ProxyType::ptHTTP)
toxOptions.proxy_type = TOX_PROXY_TYPE_HTTP;
QByteArray proxyAddrData = proxyAddr.toUtf8();
/// TODO: We're leaking a tiny amount of memory there, go fix that later
char* proxyAddrCopy = new char[proxyAddrData.size()+1];
@ -203,7 +204,9 @@ void Core::make_tox(QByteArray savedata)
return;
}
else
{
qWarning() << "Core failed to start with IPv6, falling back to IPv4. LAN discovery may not work properly.";
}
}
else if (toxOptions.proxy_type != TOX_PROXY_TYPE_NONE)
{
@ -336,7 +339,9 @@ void Core::start()
setAvatar(data);
}
else
{
qDebug() << "Core: Error loading self avatar";
}
ready = true;
@ -367,7 +372,9 @@ void Core::process()
#endif
if (checkConnection())
{
tolerance = CORE_DISCONNECT_TOLERANCE;
}
else if (!(--tolerance))
{
bootstrapDht();
@ -383,13 +390,16 @@ bool Core::checkConnection()
//static int count = 0;
bool toxConnected = tox_self_get_connection_status(tox) != TOX_CONNECTION_NONE;
if (toxConnected && !isConnected) {
if (toxConnected && !isConnected)
{
qDebug() << "Core: Connected to DHT";
emit connected();
isConnected = true;
//if (count) qDebug() << "Core: disconnect count:" << count;
//count = 0;
} else if (!toxConnected && isConnected) {
}
else if (!toxConnected && isConnected)
{
qDebug() << "Core: Disconnected to DHT";
emit disconnected();
isConnected = false;
@ -419,10 +429,14 @@ void Core::bootstrapDht()
const Settings::DhtServer& dhtServer = dhtServerList[j % listSize];
if (tox_bootstrap(tox, dhtServer.address.toLatin1().data(),
dhtServer.port, CUserId(dhtServer.userId).data(), nullptr) == 1)
{
qDebug() << QString("Core: Bootstrapping from ")+dhtServer.name+QString(", addr ")+dhtServer.address.toLatin1().data()
+QString(", port ")+QString().setNum(dhtServer.port);
}
else
{
qDebug() << "Core: Error bootstrapping from "+dhtServer.name;
}
j++;
i++;
@ -484,7 +498,8 @@ void Core::onConnectionStatusChanged(Tox*/* tox*/, uint32_t friendId, TOX_CONNEC
{
Status friendStatus = status ? Status::Online : Status::Offline;
emit static_cast<Core*>(core)->friendStatusChanged(friendId, friendStatus);
if (friendStatus == Status::Offline) {
if (friendStatus == Status::Offline)
{
static_cast<Core*>(core)->checkLastOnline(friendId);
for (ToxFile& f : fileSendQueue)
@ -503,7 +518,9 @@ void Core::onConnectionStatusChanged(Tox*/* tox*/, uint32_t friendId, TOX_CONNEC
emit static_cast<Core*>(core)->fileTransferBrokenUnbroken(f, true);
}
}
} else {
}
else
{
for (ToxFile& f : fileRecvQueue)
{
if (f.friendId == friendId && f.status == ToxFile::BROKEN)
@ -560,6 +577,7 @@ void Core::onGroupTitleChange(Tox*, int groupnumber, int peernumber, const uint8
QString author;
if (peernumber >= 0)
author = core->getGroupPeerName(groupnumber, peernumber);
emit core->groupTitleChanged(groupnumber, author, CString::toString(title, len));
}
@ -776,9 +794,12 @@ void Core::onReadReceiptCallback(Tox*, uint32_t friendnumber, uint32_t receipt,
void Core::acceptFriendRequest(const QString& userId)
{
uint32_t friendId = tox_friend_add_norequest(tox, CUserId(userId).data(), nullptr);
if (friendId == UINT32_MAX) {
if (friendId == UINT32_MAX)
{
emit failedToAddFriend(userId);
} else {
}
else
{
saveConfiguration();
emit friendAdded(friendId, userId);
}
@ -1066,9 +1087,13 @@ void Core::removeFriend(uint32_t friendId, bool fake)
{
if (!isReady() || fake)
return;
if (tox_friend_delete(tox, friendId, nullptr) == false) {
if (tox_friend_delete(tox, friendId, nullptr) == false)
{
emit failedToRemoveFriend(friendId);
} else {
}
else
{
saveConfiguration();
emit friendRemoved(friendId);
}
@ -1078,6 +1103,7 @@ void Core::removeGroup(int groupId, bool fake)
{
if (!isReady() || fake)
return;
tox_del_groupchat(tox, groupId);
if (groupCalls[groupId].active)
@ -1099,9 +1125,12 @@ void Core::setUsername(const QString& username)
{
CString cUsername(username);
if (tox_self_set_name(tox, cUsername.data(), cUsername.size(), nullptr) == false) {
if (tox_self_set_name(tox, cUsername.data(), cUsername.size(), nullptr) == false)
{
emit failedToSetUsername(username);
} else {
}
else
{
emit usernameSet(username);
saveConfiguration();
}
@ -1168,7 +1197,9 @@ void Core::setStatusMessage(const QString& message)
if (tox_self_set_status_message(tox, cMessage.data(), cMessage.size(), nullptr) == false) {
emit failedToSetStatusMessage(message);
} else {
}
else
{
saveConfiguration();
emit statusMessageSet(message);
}
@ -1177,7 +1208,8 @@ void Core::setStatusMessage(const QString& message)
void Core::setStatus(Status status)
{
TOX_USER_STATUS userstatus;
switch (status) {
switch (status)
{
case Status::Online:
userstatus = TOX_USER_STATUS_NONE;
break;
@ -1210,11 +1242,14 @@ QString Core::sanitize(QString name)
QList<QChar> banned = {'/', '\\', ':', '<', '>', '"', '|', '?', '*'};
for (QChar c : banned)
name.replace(c, '_');
// also remove leading and trailing periods
if (name[0] == '.')
name[0] = '_';
if (name.endsWith('.'))
name[name.length()-1] = '_';
return name;
}
@ -1226,18 +1261,21 @@ QByteArray Core::loadToxSave(QString path)
QFile configurationFile(path);
qDebug() << "Core::loadConfiguration: reading from " << path;
if (!configurationFile.exists()) {
if (!configurationFile.exists())
{
qWarning() << "The Tox configuration file was not found";
return data;
}
if (!configurationFile.open(QIODevice::ReadOnly)) {
if (!configurationFile.open(QIODevice::ReadOnly))
{
qCritical() << "File " << path << " cannot be opened";
return data;
}
qint64 fileSize = configurationFile.size();
if (fileSize > 0) {
if (fileSize > 0)
{
data = configurationFile.readAll();
/* TODO: Clean this up
int error = tox_load(tox, reinterpret_cast<uint8_t *>(data.data()), data.size());
@ -1279,7 +1317,8 @@ void Core::saveConfiguration()
QString dir = Settings::getSettingsDirPath();
QDir directory(dir);
if (!directory.exists() && !directory.mkpath(directory.absolutePath())) {
if (!directory.exists() && !directory.mkpath(directory.absolutePath()))
{
qCritical() << "Error while creating directory " << dir;
return;
}
@ -1338,17 +1377,21 @@ void Core::switchConfiguration(const QString& profile)
void Core::loadFriends()
{
const uint32_t friendCount = tox_self_get_friend_list_size(tox);
if (friendCount > 0) {
if (friendCount > 0)
{
// assuming there are not that many friends to fill up the whole stack
uint32_t *ids = new uint32_t[friendCount];
tox_self_get_friend_list(tox, ids);
uint8_t clientId[TOX_PUBLIC_KEY_SIZE];
for (int32_t i = 0; i < static_cast<int32_t>(friendCount); ++i) {
if (tox_friend_get_public_key(tox, ids[i], clientId, nullptr)) {
for (int32_t i = 0; i < static_cast<int32_t>(friendCount); ++i)
{
if (tox_friend_get_public_key(tox, ids[i], clientId, nullptr))
{
emit friendAdded(ids[i], CUserId::toString(clientId));
const size_t nameSize = tox_friend_get_name_size(tox, ids[i], nullptr);
if (nameSize != SIZE_MAX) {
if (nameSize != SIZE_MAX)
{
uint8_t *name = new uint8_t[nameSize];
if (tox_friend_get_name(tox, ids[i], name, nullptr))
emit friendUsernameChanged(ids[i], CString::toString(name, nameSize));
@ -1356,9 +1399,11 @@ void Core::loadFriends()
}
const size_t statusMessageSize = tox_friend_get_status_message_size(tox, ids[i], nullptr);
if (statusMessageSize != SIZE_MAX) {
if (statusMessageSize != SIZE_MAX)
{
uint8_t *statusMessage = new uint8_t[statusMessageSize];
if (tox_friend_get_status_message(tox, ids[i], statusMessage, nullptr)) {
if (tox_friend_get_status_message(tox, ids[i], statusMessage, nullptr))
{
emit friendStatusMessageChanged(ids[i], CString::toString(statusMessage, statusMessageSize));
}
delete[] statusMessage;
@ -1374,9 +1419,8 @@ void Core::loadFriends()
void Core::checkLastOnline(uint32_t friendId) {
const uint64_t lastOnline = tox_friend_get_last_online(tox, friendId, nullptr);
if (lastOnline != UINT64_MAX) {
if (lastOnline != UINT64_MAX)
emit friendLastSeenChanged(friendId, QDateTime::fromTime_t(lastOnline));
}
}
int Core::getGroupNumberPeers(int groupId) const
@ -1433,6 +1477,7 @@ QList<QString> Core::getGroupPeerNames(int groupId) const
}
for (int i=0; i<nPeers; i++)
names.push_back(CString::toString(namesArray[i], lengths[i]));
return names;
}
@ -1620,9 +1665,7 @@ bool Core::hasFriendWithPublicKey(const QString &pubkey) const
{
// Valid length check
if (pubkey.length() != (TOX_PUBLIC_KEY_SIZE * 2))
{
return false;
}
bool found = false;
const size_t friendCount = tox_self_get_friend_list_size(tox);
@ -1653,7 +1696,8 @@ QString Core::getFriendAddress(uint32_t friendNumber) const
{
// If we don't know the full address of the client, return just the id, otherwise get the full address
uint8_t rawid[TOX_PUBLIC_KEY_SIZE];
if (!tox_friend_get_public_key(tox, friendNumber, rawid, nullptr)) {
if (!tox_friend_get_public_key(tox, friendNumber, rawid, nullptr))
{
qWarning() << "Core::getFriendAddress: Getting public key failed";
return QString();
}
@ -1670,7 +1714,8 @@ QString Core::getFriendAddress(uint32_t friendNumber) const
QString Core::getFriendUsername(uint32_t friendnumber) const
{
size_t namesize = tox_friend_get_name_size(tox, friendnumber, nullptr);
if (namesize == SIZE_MAX) {
if (namesize == SIZE_MAX)
{
qWarning() << "Core::getFriendUsername: Failed to get name size for friend "<<friendnumber;
return QString();
}

View File

@ -107,6 +107,7 @@ QByteArray Core::encryptData(const QByteArray& data, PasswordType passtype)
{
if (!pwsaltedkeys[passtype])
return QByteArray();
uint8_t encrypted[data.size() + TOX_PASS_ENCRYPTION_EXTRA_LENGTH];
if (!tox_pass_key_encrypt(reinterpret_cast<const uint8_t*>(data.data()), data.size(),
pwsaltedkeys[passtype], encrypted, nullptr))
@ -121,6 +122,7 @@ QByteArray Core::decryptData(const QByteArray& data, PasswordType passtype)
{
if (!pwsaltedkeys[passtype])
return QByteArray();
int sz = data.size() - TOX_PASS_ENCRYPTION_EXTRA_LENGTH;
uint8_t decrypted[sz];
if (!tox_pass_key_decrypt(reinterpret_cast<const uint8_t*>(data.data()), data.size(),
@ -237,10 +239,12 @@ void Core::checkEncryptedHistory()
{
if (!exists || HistoryKeeper::checkPassword())
return;
dialogtxt = tr("The chat history password failed. Please try another?", "used only when pw set before load() doesn't work");
}
else
dialogtxt = a;
dialogtxt += "\n" + c;
if (pwsaltedkeys[ptMain])
@ -287,7 +291,8 @@ void Core::saveConfiguration(const QString& path)
}
QSaveFile configurationFile(path);
if (!configurationFile.open(QIODevice::WriteOnly)) {
if (!configurationFile.open(QIODevice::WriteOnly))
{
qCritical() << "File " << path << " cannot be opened";
return;
}
@ -304,7 +309,8 @@ void Core::saveConfiguration(const QString& path)
else
fileSize = tox_get_savedata_size(tox);
if (fileSize > 0 && fileSize <= std::numeric_limits<int32_t>::max()) {
if (fileSize > 0 && fileSize <= std::numeric_limits<int32_t>::max())
{
uint8_t *data = new uint8_t[fileSize];
if (encrypt)

View File

@ -58,35 +58,45 @@ QByteArray ToxDNS::fetchLastTextRecord(const QString& record, bool silent)
qApp->processEvents();
QThread::msleep(100);
}
if (timeout >= 30) {
if (timeout >= 30)
{
dns.abort();
if (!silent)
showWarning(tr("The connection timed out","The DNS gives the Tox ID associated to toxme.se addresses"));
return result;
}
if (dns.error() == QDnsLookup::NotFoundError) {
if (dns.error() == QDnsLookup::NotFoundError)
{
if (!silent)
showWarning(tr("This address does not exist","The DNS gives the Tox ID associated to toxme.se addresses"));
return result;
}
else if (dns.error() != QDnsLookup::NoError) {
else if (dns.error() != QDnsLookup::NoError)
{
if (!silent)
showWarning(tr("Error while looking up DNS","The DNS gives the Tox ID associated to toxme.se addresses"));
return result;
}
const QList<QDnsTextRecord> textRecords = dns.textRecords();
if (textRecords.isEmpty()) {
if (textRecords.isEmpty())
{
if (!silent)
showWarning(tr("No text record found", "Error with the DNS"));
return result;
}
const QList<QByteArray> textRecordValues = textRecords.last().values();
if (textRecordValues.length() != 1) {
if (textRecordValues.length() != 1)
{
if (!silent)
showWarning(tr("Unexpected number of values in text record", "Error with the DNS"));
return result;
}
@ -104,7 +114,8 @@ QString ToxDNS::queryTox1(const QString& record, bool silent)
// Check toxdns protocol version
int verx = entry.indexOf("v=");
if (verx) {
if (verx)
{
verx += 2;
int verend = entry.indexOf(';', verx);
if (verend)
@ -114,6 +125,7 @@ QString ToxDNS::queryTox1(const QString& record, bool silent)
{
if (!silent)
showWarning(tr("The version of Tox DNS used by this server is not supported", "Error with the DNS"));
return toxId;
}
}
@ -121,23 +133,29 @@ QString ToxDNS::queryTox1(const QString& record, bool silent)
// Get the tox id
int idx = entry.indexOf("id=");
if (idx < 0) {
if (idx < 0)
{
if (!silent)
showWarning(tr("The DNS lookup does not contain any Tox ID", "Error with the DNS"));
return toxId;
}
idx += 3;
if (entry.length() < idx + static_cast<int>(TOX_HEX_ID_LENGTH)) {
if (entry.length() < idx + static_cast<int>(TOX_HEX_ID_LENGTH))
{
if (!silent)
showWarning(tr("The DNS lookup does not contain a valid Tox ID", "Error with the DNS"));
return toxId;
}
toxId = entry.mid(idx, TOX_HEX_ID_LENGTH);
if (!ToxID::isToxId(toxId)) {
if (!ToxID::isToxId(toxId))
{
if (!silent)
showWarning(tr("The DNS lookup does not contain a valid Tox ID", "Error with the DNS"));
return toxId;
}
@ -178,7 +196,8 @@ QString ToxDNS::queryTox3(const tox3_server& server, const QString &record, bool
// Check toxdns protocol version
verx = entry.indexOf("v=");
if (verx!=-1) {
if (verx!=-1)
{
verx += 2;
int verend = entry.indexOf(';', verx);
if (verend!=-1)
@ -194,7 +213,8 @@ QString ToxDNS::queryTox3(const tox3_server& server, const QString &record, bool
// Get and decrypt the tox id
idx = entry.indexOf("id=");
if (idx < 0) {
if (idx < 0)
{
qWarning() << "queryTox3: Server "<<server.name<<" returned an empty id, using tox1 as a fallback";
goto fallbackOnTox1;
}
@ -217,6 +237,7 @@ QString ToxDNS::queryTox3(const tox3_server& server, const QString &record, bool
fallbackOnTox1:
if (tox_dns3)
tox_dns3_kill(tox_dns3);
#if TOX1_SILENT_FALLBACK
toxIdStr = queryTox1(record, silent);
#elif TOX1_ASK_FALLBACK
@ -226,6 +247,7 @@ Should tox1 be used anyway?\n\
If unsure, press No, so that request to ToxDNS service will not be made using unsecure protocol."), QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
if (btn == QMessageBox::Yes)
queryTox1(record, silent);
#endif
return toxIdStr;
}
@ -234,12 +256,17 @@ ToxID ToxDNS::resolveToxAddress(const QString &address, bool silent)
{
ToxID toxId;
if (address.isEmpty()) {
if (address.isEmpty())
{
return toxId;
} else if (ToxID::isToxId(address)) {
}
else if (ToxID::isToxId(address))
{
toxId = ToxID::fromString(address);
return toxId;
} else {
}
else
{
// If we're querying one of our pinned server, do a tox3 request directly
QString servname = address.mid(address.indexOf('@')+1);
for (const ToxDNS::tox3_server& pin : ToxDNS::pinnedServers)
@ -272,6 +299,7 @@ Should tox1 be used anyway?\n\
If unsure, press No, so that request to ToxDNS service will not be made using unsecure protocol."), QMessageBox::Ok|QMessageBox::No, QMessageBox::No);
if (btn == QMessageBox::Ok)
toxId = ToxID::fromString(queryTox1(address, silent));
#else
return toxId;
#endif