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

refactor(core): replace CString with ToxString

This commit is contained in:
sudden6 2017-03-05 21:54:08 +01:00
parent 1f0467bb1b
commit 25c279c320
No known key found for this signature in database
GPG Key ID: 279509B499E032B9
3 changed files with 38 additions and 40 deletions

View File

@ -22,6 +22,7 @@
#include "corefile.h" #include "corefile.h"
#include "src/core/coreav.h" #include "src/core/coreav.h"
#include "src/core/cstring.h" #include "src/core/cstring.h"
#include "src/core/toxstring.h"
#include "src/net/avatarbroadcaster.h" #include "src/net/avatarbroadcaster.h"
#include "src/nexus.h" #include "src/nexus.h"
#include "src/persistence/profile.h" #include "src/persistence/profile.h"
@ -421,7 +422,7 @@ void Core::onFriendRequest(Tox* /* tox*/, const uint8_t* cFriendPk, const uint8_
{ {
ToxPk friendPk(cFriendPk); ToxPk friendPk(cFriendPk);
emit static_cast<Core*>(core)->friendRequestReceived(friendPk, emit static_cast<Core*>(core)->friendRequestReceived(friendPk,
CString::toString(cMessage, cMessageSize)); ToxString(cMessage, cMessageSize).getQString());
} }
void Core::onFriendMessage(Tox* /* tox*/, uint32_t friendId, TOX_MESSAGE_TYPE type, void Core::onFriendMessage(Tox* /* tox*/, uint32_t friendId, TOX_MESSAGE_TYPE type,
@ -429,14 +430,14 @@ void Core::onFriendMessage(Tox* /* tox*/, uint32_t friendId, TOX_MESSAGE_TYPE ty
{ {
bool isAction = (type == TOX_MESSAGE_TYPE_ACTION); bool isAction = (type == TOX_MESSAGE_TYPE_ACTION);
emit static_cast<Core*>(core)->friendMessageReceived(friendId, emit static_cast<Core*>(core)->friendMessageReceived(friendId,
CString::toString(cMessage, cMessageSize), ToxString(cMessage, cMessageSize).getQString(),
isAction); isAction);
} }
void Core::onFriendNameChange(Tox* /* tox*/, uint32_t friendId, const uint8_t* cName, void Core::onFriendNameChange(Tox* /* tox*/, uint32_t friendId, const uint8_t* cName,
size_t cNameSize, void* core) size_t cNameSize, void* core)
{ {
emit static_cast<Core*>(core)->friendUsernameChanged(friendId, CString::toString(cName, cNameSize)); emit static_cast<Core*>(core)->friendUsernameChanged(friendId, ToxString(cName, cNameSize).getQString());
} }
void Core::onFriendTypingChange(Tox* /* tox*/, uint32_t friendId, bool isTyping, void* core) void Core::onFriendTypingChange(Tox* /* tox*/, uint32_t friendId, bool isTyping, void* core)
@ -448,7 +449,7 @@ void Core::onStatusMessageChanged(Tox* /* tox*/, uint32_t friendId, const uint8_
size_t cMessageSize, void* core) size_t cMessageSize, void* core)
{ {
emit static_cast<Core*>(core)->friendStatusMessageChanged(friendId, emit static_cast<Core*>(core)->friendStatusMessageChanged(friendId,
CString::toString(cMessage, cMessageSize)); ToxString(cMessage, cMessageSize).getQString());
} }
void Core::onUserStatusChanged(Tox* /* tox*/, uint32_t friendId, TOX_USER_STATUS userstatus, void* core) void Core::onUserStatusChanged(Tox* /* tox*/, uint32_t friendId, TOX_USER_STATUS userstatus, void* core)
@ -504,9 +505,8 @@ void Core::onGroupMessage(Tox*, uint32_t groupId, uint32_t peerId, TOX_MESSAGE_T
const uint8_t* cMessage, size_t length, void* vCore) const uint8_t* cMessage, size_t length, void* vCore)
{ {
Core* core = static_cast<Core*>(vCore); Core* core = static_cast<Core*>(vCore);
QString message = CString::toString(cMessage, length);
bool isAction = type == TOX_MESSAGE_TYPE_ACTION; bool isAction = type == TOX_MESSAGE_TYPE_ACTION;
emit core->groupMessageReceived(groupId, peerId, message, isAction); emit core->groupMessageReceived(groupId, peerId, ToxString(cMessage, length).getQString(), isAction);
} }
void Core::onGroupNamelistChange(Tox*, uint32_t groupId, uint32_t peerId, void Core::onGroupNamelistChange(Tox*, uint32_t groupId, uint32_t peerId,
@ -525,8 +525,7 @@ void Core::onGroupTitleChange(Tox*, uint32_t groupId, uint32_t peerId, const uin
{ {
Core* core = static_cast<Core*>(vCore); Core* core = static_cast<Core*>(vCore);
QString author = core->getGroupPeerName(groupId, peerId); QString author = core->getGroupPeerName(groupId, peerId);
QString title = CString::toString(cTitle, length); emit core->groupTitleChanged(groupId, author, ToxString(cTitle, length).getQString());
emit core->groupTitleChanged(groupId, author, title);
} }
void Core::onReadReceiptCallback(Tox*, uint32_t friendId, uint32_t receipt, void* core) void Core::onReadReceiptCallback(Tox*, uint32_t friendId, uint32_t receipt, void* core)
@ -560,7 +559,7 @@ void Core::requestFriendship(const ToxId& friendAddress, const QString& message)
} else if (hasFriendWithPublicKey(friendPk)) { } else if (hasFriendWithPublicKey(friendPk)) {
emit failedToAddFriend(friendPk, tr("Friend is already added")); emit failedToAddFriend(friendPk, tr("Friend is already added"));
} else { } else {
CString cMessage(message); ToxString cMessage(message);
uint32_t friendId = uint32_t friendId =
tox_friend_add(tox, friendAddress.getBytes(), cMessage.data(), cMessage.size(), nullptr); tox_friend_add(tox, friendAddress.getBytes(), cMessage.data(), cMessage.size(), nullptr);
@ -595,7 +594,7 @@ void Core::requestFriendship(const ToxId& friendAddress, const QString& message)
int Core::sendMessage(uint32_t friendId, const QString& message) int Core::sendMessage(uint32_t friendId, const QString& message)
{ {
QMutexLocker ml(&messageSendMutex); QMutexLocker ml(&messageSendMutex);
CString cMessage(message); ToxString cMessage(message);
int receipt = tox_friend_send_message(tox, friendId, TOX_MESSAGE_TYPE_NORMAL, cMessage.data(), int receipt = tox_friend_send_message(tox, friendId, TOX_MESSAGE_TYPE_NORMAL, cMessage.data(),
cMessage.size(), nullptr); cMessage.size(), nullptr);
emit messageSentResult(friendId, message, receipt); emit messageSentResult(friendId, message, receipt);
@ -605,7 +604,7 @@ int Core::sendMessage(uint32_t friendId, const QString& message)
int Core::sendAction(uint32_t friendId, const QString& action) int Core::sendAction(uint32_t friendId, const QString& action)
{ {
QMutexLocker ml(&messageSendMutex); QMutexLocker ml(&messageSendMutex);
CString cMessage(action); ToxString cMessage(action);
int receipt = tox_friend_send_message(tox, friendId, TOX_MESSAGE_TYPE_ACTION, cMessage.data(), int receipt = tox_friend_send_message(tox, friendId, TOX_MESSAGE_TYPE_ACTION, cMessage.data(),
cMessage.size(), nullptr); cMessage.size(), nullptr);
emit messageSentResult(friendId, action, receipt); emit messageSentResult(friendId, action, receipt);
@ -621,9 +620,10 @@ void Core::sendTyping(uint32_t friendId, bool typing)
void Core::sendGroupMessageWithType(int groupId, const QString& message, TOX_MESSAGE_TYPE type) void Core::sendGroupMessageWithType(int groupId, const QString& message, TOX_MESSAGE_TYPE type)
{ {
QList<CString> cMessages = splitMessage(message, MAX_GROUP_MESSAGE_LEN); QList<QString> cMessages = splitMessage(message, MAX_GROUP_MESSAGE_LEN);
for (auto& cMsg : cMessages) { for (auto& part : cMessages) {
ToxString cMsg(part);
TOX_ERR_CONFERENCE_SEND_MESSAGE error; TOX_ERR_CONFERENCE_SEND_MESSAGE error;
bool success = bool success =
tox_conference_send_message(tox, groupId, type, cMsg.data(), cMsg.size(), &error); tox_conference_send_message(tox, groupId, type, cMsg.data(), cMsg.size(), &error);
@ -665,7 +665,7 @@ void Core::sendGroupAction(int groupId, const QString& message)
void Core::changeGroupTitle(int groupId, const QString& title) void Core::changeGroupTitle(int groupId, const QString& title)
{ {
CString cTitle(title); ToxString cTitle(title);
TOX_ERR_CONFERENCE_TITLE error; TOX_ERR_CONFERENCE_TITLE error;
bool success = tox_conference_set_title(tox, groupId, cTitle.data(), cTitle.size(), &error); bool success = tox_conference_set_title(tox, groupId, cTitle.data(), cTitle.size(), &error);
@ -778,7 +778,7 @@ QString Core::getUsername() const
int size = tox_self_get_name_size(tox); int size = tox_self_get_name_size(tox);
uint8_t* name = new uint8_t[size]; uint8_t* name = new uint8_t[size];
tox_self_get_name(tox, name); tox_self_get_name(tox, name);
sname = CString::toString(name, size); sname = ToxString(name, size).getQString();
delete[] name; delete[] name;
return sname; return sname;
} }
@ -788,7 +788,7 @@ void Core::setUsername(const QString& username)
if (username == getUsername()) if (username == getUsername())
return; return;
CString cUsername(username); ToxString cUsername(username);
if (!tox_self_set_name(tox, cUsername.data(), cUsername.size(), nullptr)) { if (!tox_self_set_name(tox, cUsername.data(), cUsername.size(), nullptr)) {
emit failedToSetUsername(username); emit failedToSetUsername(username);
@ -856,7 +856,7 @@ QString Core::getStatusMessage() const
size_t size = tox_self_get_status_message_size(tox); size_t size = tox_self_get_status_message_size(tox);
uint8_t* name = new uint8_t[size]; uint8_t* name = new uint8_t[size];
tox_self_get_status_message(tox, name); tox_self_get_status_message(tox, name);
sname = CString::toString(name, size); sname = ToxString(name, size).getQString();
delete[] name; delete[] name;
return sname; return sname;
} }
@ -874,7 +874,7 @@ void Core::setStatusMessage(const QString& message)
if (message == getStatusMessage()) if (message == getStatusMessage())
return; return;
CString cMessage(message); ToxString cMessage(message);
if (!tox_self_set_status_message(tox, cMessage.data(), cMessage.size(), nullptr)) { if (!tox_self_set_status_message(tox, cMessage.data(), cMessage.size(), nullptr)) {
emit failedToSetStatusMessage(message); emit failedToSetStatusMessage(message);
@ -954,7 +954,7 @@ void Core::loadFriends()
if (nameSize && nameSize != SIZE_MAX) { if (nameSize && nameSize != SIZE_MAX) {
uint8_t* name = new uint8_t[nameSize]; uint8_t* name = new uint8_t[nameSize];
if (tox_friend_get_name(tox, ids[i], name, nullptr)) if (tox_friend_get_name(tox, ids[i], name, nullptr))
emit friendUsernameChanged(ids[i], CString::toString(name, nameSize)); emit friendUsernameChanged(ids[i], ToxString(name, nameSize).getQString());
delete[] name; delete[] name;
} }
@ -963,8 +963,8 @@ void Core::loadFriends()
if (statusMessageSize != SIZE_MAX) { if (statusMessageSize != SIZE_MAX) {
uint8_t* statusMessage = new uint8_t[statusMessageSize]; 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, emit friendStatusMessageChanged(ids[i], ToxString(statusMessage,
statusMessageSize)); statusMessageSize).getQString());
} }
delete[] statusMessage; delete[] statusMessage;
} }
@ -1050,7 +1050,7 @@ QString Core::getGroupPeerName(int groupId, int peerId) const
return QString{}; return QString{};
} }
return CString::toString(nameArray, length); return ToxString(nameArray, length).getQString();
} }
/** /**
@ -1106,7 +1106,7 @@ QList<QString> Core::getGroupPeerNames(int groupId) const
lengths[i] = tox_conference_peer_get_name_size(tox, groupId, i, &error); lengths[i] = tox_conference_peer_get_name_size(tox, groupId, i, &error);
bool ok = tox_conference_peer_get_name(tox, groupId, i, namesArray[i], &error); bool ok = tox_conference_peer_get_name(tox, groupId, i, namesArray[i], &error);
if (parsePeerQueryError(error) && ok) { if (parsePeerQueryError(error) && ok) {
names.push_back(CString::toString(namesArray[i], lengths[i])); names.push_back(ToxString(namesArray[i], lengths[i]).getQString());
} }
} }
@ -1292,14 +1292,14 @@ QString Core::getFriendUsername(uint32_t friendnumber) const
} }
uint8_t* name = new uint8_t[namesize]; uint8_t* name = new uint8_t[namesize];
tox_friend_get_name(tox, friendnumber, name, nullptr); tox_friend_get_name(tox, friendnumber, name, nullptr);
QString sname = CString::toString(name, namesize); ToxString sname(name, namesize);
delete[] name; delete[] name;
return sname; return sname.getQString();
} }
QList<CString> Core::splitMessage(const QString& message, int maxLen) QList<QString> Core::splitMessage(const QString& message, int maxLen)
{ {
QList<CString> splittedMsgs; QList<QString> splittedMsgs;
QByteArray ba_message(message.toUtf8()); QByteArray ba_message(message.toUtf8());
while (ba_message.size() > maxLen) { while (ba_message.size() > maxLen) {
@ -1314,11 +1314,11 @@ QList<CString> Core::splitMessage(const QString& message, int maxLen)
splitPos--; splitPos--;
} }
splittedMsgs.push_back(CString(ba_message.left(splitPos + 1))); splittedMsgs.push_back(QString(ba_message.left(splitPos + 1)));
ba_message = ba_message.mid(splitPos + 1); ba_message = ba_message.mid(splitPos + 1);
} }
splittedMsgs.push_back(CString(ba_message)); splittedMsgs.push_back(QString(ba_message));
return splittedMsgs; return splittedMsgs;
} }
@ -1343,7 +1343,7 @@ QString Core::getPeerName(const ToxPk& id) const
return name; return name;
} }
name = CString::toString(cname, nameSize); name = ToxString(cname, nameSize).getQString();
delete[] cname; delete[] cname;
return name; return name;
} }

View File

@ -36,7 +36,6 @@ template <typename T>
class QList; class QList;
class QTimer; class QTimer;
class QString; class QString;
class CString;
struct ToxAV; struct ToxAV;
class CoreAV; class CoreAV;
struct vpx_image; struct vpx_image;
@ -54,7 +53,7 @@ public:
static const QString TOX_EXT; static const QString TOX_EXT;
static const QString CONFIG_FILE_NAME; static const QString CONFIG_FILE_NAME;
static QString sanitize(QString name); static QString sanitize(QString name);
static QList<CString> splitMessage(const QString& message, int maxLen); static QList<QString> splitMessage(const QString& message, int maxLen);
static QByteArray getSaltFromFile(QString filename); static QByteArray getSaltFromFile(QString filename);

View File

@ -1010,23 +1010,22 @@ void ChatForm::SendMessageStr(QString msg)
msg.remove(0, ACTION_PREFIX.length()); msg.remove(0, ACTION_PREFIX.length());
} }
QList<CString> splittedMsg = Core::splitMessage(msg, TOX_MAX_MESSAGE_LENGTH); QList<QString> splittedMsg = Core::splitMessage(msg, TOX_MAX_MESSAGE_LENGTH);
QDateTime timestamp = QDateTime::currentDateTime(); QDateTime timestamp = QDateTime::currentDateTime();
for (CString& c_msg : splittedMsg) { for (QString& part : splittedMsg) {
QString qt_msg = CString::toString(c_msg.data(), c_msg.size()); QString historyPart = part;
QString qt_msg_hist = qt_msg;
if (isAction) { if (isAction) {
qt_msg_hist = ACTION_PREFIX + qt_msg; historyPart = ACTION_PREFIX + part;
} }
bool status = !Settings::getInstance().getFauxOfflineMessaging(); bool status = !Settings::getInstance().getFauxOfflineMessaging();
ChatMessage::Ptr ma = addSelfMessage(qt_msg, isAction, timestamp, false); ChatMessage::Ptr ma = addSelfMessage(part, isAction, timestamp, false);
Core* core = Core::getInstance(); Core* core = Core::getInstance();
uint32_t friendId = f->getFriendId(); uint32_t friendId = f->getFriendId();
int rec = isAction ? core->sendAction(friendId, qt_msg) : core->sendMessage(friendId, qt_msg); int rec = isAction ? core->sendAction(friendId, part) : core->sendMessage(friendId, part);
Profile* profile = Nexus::getProfile(); Profile* profile = Nexus::getProfile();
if (profile->isHistoryEnabled()) { if (profile->isHistoryEnabled()) {
@ -1034,7 +1033,7 @@ void ChatForm::SendMessageStr(QString msg)
QString selfPk = Core::getInstance()->getSelfId().toString(); QString selfPk = Core::getInstance()->getSelfId().toString();
QString pk = f->getPublicKey().toString(); QString pk = f->getPublicKey().toString();
QString name = Core::getInstance()->getUsername(); QString name = Core::getInstance()->getUsername();
profile->getHistory()->addNewMessage(pk, qt_msg_hist, selfPk, timestamp, status, name, profile->getHistory()->addNewMessage(pk, historyPart, selfPk, timestamp, status, name,
[offMsgEngine, rec, ma](int64_t id) { [offMsgEngine, rec, ma](int64_t id) {
offMsgEngine->registerReceipt(rec, id, ma); offMsgEngine->registerReceipt(rec, id, ma);
}); });