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

317 lines
15 KiB
C
Raw Normal View History

2014-06-25 04:11:11 +08:00
/*
Copyright (C) 2013 by Maxim Biro <nurupo.contributions@gmail.com>
Copyright © 2014-2015 by The qTox Project
2014-06-25 04:11:11 +08:00
This file is part of qTox, a Qt-based graphical interface for Tox.
2014-06-25 04:11:11 +08:00
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
qTox is distributed in the hope that it will be useful,
2014-06-25 04:11:11 +08:00
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
2014-06-25 04:11:11 +08:00
You should have received a copy of the GNU General Public License
along with qTox. If not, see <http://www.gnu.org/licenses/>.
2014-06-25 04:11:11 +08:00
*/
#ifndef CORE_HPP
#define CORE_HPP
2014-06-26 04:43:28 +08:00
#include <cstdint>
2014-06-25 04:11:11 +08:00
#include <QObject>
#include <QMutex>
2014-06-25 04:11:11 +08:00
#include <tox/tox.h>
#include <tox/toxencryptsave.h>
2014-09-11 21:44:34 +08:00
#include "corestructs.h"
#include "coreav.h"
#include "coredefines.h"
#include "toxid.h"
2014-06-26 04:43:28 +08:00
class Profile;
2014-09-11 21:44:34 +08:00
template <typename T> class QList;
class QTimer;
class QString;
2014-09-21 18:30:10 +08:00
class CString;
2014-10-15 20:46:01 +08:00
class VideoSource;
2015-05-14 10:46:28 +08:00
class VideoFrame;
#ifdef QTOX_FILTER_AUDIO
class AudioFilterer;
#endif
2014-06-25 04:11:11 +08:00
class Core : public QObject
{
Q_OBJECT
public:
explicit Core(QThread* coreThread, Profile& profile);
2014-09-11 21:44:34 +08:00
static Core* getInstance(); ///< Returns the global widget's Core instance
2014-06-25 04:11:11 +08:00
~Core();
static const QString TOX_EXT;
2014-10-09 13:30:33 +08:00
static const QString CONFIG_FILE_NAME;
static QString sanitize(QString name);
2014-12-29 01:26:35 +08:00
static QList<CString> splitMessage(const QString &message, int maxLen);
2014-06-25 04:11:11 +08:00
2014-11-04 00:44:01 +08:00
static QByteArray getSaltFromFile(QString filename);
QString getPeerName(const ToxId& id) const;
QVector<uint32_t> getFriendList() const; ///< Returns the list of friendIds in our friendlist, an empty list on error
int getGroupNumberPeers(int groupId) const; ///< Return the number of peers in the group chat on success, or -1 on failure
QString getGroupPeerName(int groupId, int peerId) const; ///< Get the name of a peer of a group
ToxId getGroupPeerToxId(int groupId, int peerId) const; ///< Get the public key of a peer of a group
QList<QString> getGroupPeerNames(int groupId) const; ///< Get the names of the peers of a group
QString getFriendAddress(uint32_t friendId) const; ///< Get the full address if known, or public key of a friend
QString getFriendUsername(uint32_t friendId) const; ///< Get the username of a friend
bool isFriendOnline(uint32_t friendId) const; ///< Check if a friend is online. Unknown friends are considered offline.
bool hasFriendWithAddress(const QString &addr) const; ///< Check if we have a friend by address
bool hasFriendWithPublicKey(const QString &pubkey) const; ///< Check if we have a friend by public key
int joinGroupchat(int32_t friendId, uint8_t type, const uint8_t* pubkey,uint16_t length) const; ///< Accept a groupchat invite
void quitGroupChat(int groupId) const; ///< Quit a groupchat
2014-06-25 04:11:11 +08:00
QString getIDString() const; ///< Get the 12 first characters of our Tox ID
QString getUsername() const; ///< Returns our username, or an empty string on failure
QString getStatusMessage() const; ///< Returns our status message, or an empty string on failure
ToxId getSelfId() const; ///< Returns our Tox ID
2015-02-20 07:47:13 +08:00
QPair<QByteArray, QByteArray> getKeypair() const; ///< Returns our public and private keys
2014-07-04 01:38:30 +08:00
static std::unique_ptr<TOX_PASS_KEY> createPasskey(const QString &password, uint8_t* salt = nullptr);
static QByteArray encryptData(const QByteArray& data, const TOX_PASS_KEY& encryptionKey);
static QByteArray encryptData(const QByteArray& data); ///< Uses the default profile's key
static QByteArray decryptData(const QByteArray& data, const TOX_PASS_KEY &encryptionKey);
static QByteArray decryptData(const QByteArray& data); ///< Uses the default profile's key
VideoSource* getVideoSourceFromCall(int callNumber); ///< Get a call's video source
static bool anyActiveCalls(); ///< true is any calls are currently active (note: a call about to start is not yet active)
bool isReady(); ///< Most of the API shouldn't be used until Core is ready, call start() first
2014-10-15 09:04:53 +08:00
void resetCallSources(); ///< Forces to regenerate each call's audio sources
2014-06-25 04:11:11 +08:00
public slots:
void start(); ///< Initializes the core, must be called before anything else
2015-06-04 22:36:08 +08:00
void reset(); ///< Reinitialized the core. Must be called from the Core thread, with the GUI thread ready to process events.
void process(); ///< Processes toxcore events and ensure we stay connected, called by its own timer
void bootstrapDht(); ///< Connects us to the Tox network
2014-06-25 04:11:11 +08:00
QByteArray getToxSaveData(); ///< Returns the unencrypted tox save data
2015-01-24 22:31:11 +08:00
2014-06-25 04:11:11 +08:00
void acceptFriendRequest(const QString& userId);
void requestFriendship(const QString& friendAddress, const QString& message);
void groupInviteFriend(uint32_t friendId, int groupId);
2015-08-21 22:32:00 +08:00
int createGroup(uint8_t type = TOX_GROUPCHAT_TYPE_AV);
2014-06-25 04:11:11 +08:00
void removeFriend(uint32_t friendId, bool fake = false);
void removeGroup(int groupId, bool fake = false);
2014-06-25 04:11:11 +08:00
2014-07-01 06:36:48 +08:00
void setStatus(Status status);
void setUsername(const QString& username);
void setStatusMessage(const QString& message);
void setAvatar(const QByteArray& data);
int sendMessage(uint32_t friendId, const QString& message);
2014-06-26 04:43:28 +08:00
void sendGroupMessage(int groupId, const QString& message);
2014-11-01 21:00:43 +08:00
void sendGroupAction(int groupId, const QString& message);
2014-11-06 08:22:50 +08:00
void changeGroupTitle(int groupId, const QString& title);
int sendAction(uint32_t friendId, const QString& action);
void sendTyping(uint32_t friendId, bool typing);
2014-06-26 04:43:28 +08:00
void sendFile(uint32_t friendId, QString Filename, QString FilePath, long long filesize);
void sendAvatarFile(uint32_t friendId, const QByteArray& data);
void cancelFileSend(uint32_t friendId, uint32_t fileNum);
void cancelFileRecv(uint32_t friendId, uint32_t fileNum);
void rejectFileRecvRequest(uint32_t friendId, uint32_t fileNum);
void acceptFileRecvRequest(uint32_t friendId, uint32_t fileNum, QString path);
void pauseResumeFileSend(uint32_t friendId, uint32_t fileNum);
void pauseResumeFileRecv(uint32_t friendId, uint32_t fileNum);
2014-06-25 04:11:11 +08:00
void answerCall(int callId);
void rejectCall(int callId);
void hangupCall(int callId);
void startCall(uint32_t friendId, bool video=false);
void cancelCall(int callId, uint32_t friendId);
2014-06-25 04:11:11 +08:00
2014-08-19 23:20:20 +08:00
void micMuteToggle(int callId);
2014-10-28 20:50:30 +08:00
void volMuteToggle(int callId);
2014-08-19 23:20:20 +08:00
2014-11-16 00:10:04 +08:00
void setNospam(uint32_t nospam);
bool isGroupAvEnabled(int groupId); ///< True for AV groups, false for text-only groups
static void joinGroupCall(int groupId); ///< Starts a call in an existing AV groupchat. Call from the GUI thread.
static void leaveGroupCall(int groupId); ///< Will not leave the group, just stop the call. Call from the GUI thread.
2014-11-13 20:11:23 +08:00
static void disableGroupCallMic(int groupId);
static void disableGroupCallVol(int groupId);
static void enableGroupCallMic(int groupId);
static void enableGroupCallVol(int groupId);
static bool isGroupCallMicEnabled(int groupId);
static bool isGroupCallVolEnabled(int groupId);
2014-11-13 20:11:23 +08:00
2014-06-25 04:11:11 +08:00
signals:
void connected();
void disconnected();
void friendRequestReceived(const QString& userId, const QString& message);
void friendMessageReceived(uint32_t friendId, const QString& message, bool isAction);
2014-06-25 04:11:11 +08:00
void friendAdded(uint32_t friendId, const QString& userId);
void friendshipChanged(uint32_t friendId);
2014-06-25 04:11:11 +08:00
void friendStatusChanged(uint32_t friendId, Status status);
void friendStatusMessageChanged(uint32_t friendId, const QString& message);
void friendUsernameChanged(uint32_t friendId, const QString& username);
void friendTypingChanged(uint32_t friendId, bool isTyping);
void friendAvatarChanged(uint32_t friendId, const QPixmap& pic);
void friendAvatarRemoved(uint32_t friendId);
2014-06-25 04:11:11 +08:00
void friendRemoved(uint32_t friendId);
2014-06-25 04:11:11 +08:00
void friendLastSeenChanged(uint32_t friendId, const QDateTime& dateTime);
2014-06-25 04:11:11 +08:00
2014-06-30 05:38:48 +08:00
void emptyGroupCreated(int groupnumber);
void groupInviteReceived(uint32_t friendId, uint8_t type, QByteArray publicKey);
2014-11-23 23:55:49 +08:00
void groupMessageReceived(int groupnumber, int peernumber, const QString& message, bool isAction);
2014-06-25 04:11:11 +08:00
void groupNamelistChanged(int groupnumber, int peernumber, uint8_t change);
2014-11-06 08:22:50 +08:00
void groupTitleChanged(int groupnumber, const QString& author, const QString& title);
void groupPeerAudioPlaying(int groupnumber, int peernumber);
2014-06-25 04:11:11 +08:00
void usernameSet(const QString& username);
void statusMessageSet(const QString& message);
void statusSet(Status status);
2014-10-18 09:15:26 +08:00
void idSet(const QString& id);
2014-09-24 22:51:16 +08:00
void selfAvatarChanged(const QPixmap& pic);
2014-06-25 04:11:11 +08:00
void messageSentResult(uint32_t friendId, const QString& message, int messageId);
void groupSentResult(int groupId, const QString& message, int result);
void actionSentResult(uint32_t friendId, const QString& action, int success);
2014-06-25 04:11:11 +08:00
2014-11-01 22:19:11 +08:00
void receiptRecieved(int friedId, int receipt);
void failedToAddFriend(const QString& userId, const QString& errorInfo = QString());
void failedToRemoveFriend(uint32_t friendId);
2014-06-25 04:11:11 +08:00
void failedToSetUsername(const QString& username);
void failedToSetStatusMessage(const QString& message);
void failedToSetStatus(Status status);
void failedToSetTyping(bool typing);
void failedToStart();
2014-10-06 04:49:44 +08:00
void badProxy();
2014-06-25 04:11:11 +08:00
void fileSendStarted(ToxFile file);
void fileReceiveRequested(ToxFile file);
void fileTransferAccepted(ToxFile file);
2014-11-17 23:05:14 +08:00
void fileTransferCancelled(ToxFile file);
void fileTransferFinished(ToxFile file);
void fileUploadFinished(const QString& path);
void fileDownloadFinished(const QString& path);
2014-11-17 23:05:14 +08:00
void fileTransferPaused(ToxFile file);
void fileTransferInfo(ToxFile file);
void fileTransferRemotePausedUnpaused(ToxFile file, bool paused);
void fileTransferBrokenUnbroken(ToxFile file, bool broken);
void fileSendFailed(uint32_t friendId, const QString& fname);
void avInvite(uint32_t friendId, int callIndex, bool video);
void avStart(uint32_t friendId, int callIndex, bool video);
void avCancel(uint32_t friendId, int callIndex);
void avEnd(uint32_t friendId, int callIndex);
void avRinging(uint32_t friendId, int callIndex, bool video);
void avStarting(uint32_t friendId, int callIndex, bool video);
void avEnding(uint32_t friendId, int callIndex);
void avRequestTimeout(uint32_t friendId, int callIndex);
void avPeerTimeout(uint32_t friendId, int callIndex);
void avMediaChange(uint32_t friendId, int callIndex, bool videoEnabled);
void avCallFailed(uint32_t friendId);
void avRejected(uint32_t friendId, int callIndex);
void videoFrameReceived(vpx_image* frame);
private:
static void onFriendRequest(Tox* tox, const uint8_t* cUserId, const uint8_t* cMessage,
size_t cMessageSize, void* core);
static void onFriendMessage(Tox* tox, uint32_t friendId, TOX_MESSAGE_TYPE type,
const uint8_t* cMessage, size_t cMessageSize, void* core);
static void onFriendNameChange(Tox* tox, uint32_t friendId, const uint8_t* cName,
size_t cNameSize, void* core);
static void onFriendTypingChange(Tox* tox, uint32_t friendId, bool isTyping, void* core);
static void onStatusMessageChanged(Tox* tox, uint32_t friendId, const uint8_t* cMessage,
size_t cMessageSize, void* core);
static void onUserStatusChanged(Tox* tox, uint32_t friendId, TOX_USER_STATUS userstatus, void* core);
static void onConnectionStatusChanged(Tox* tox, uint32_t friendId, TOX_CONNECTION status, void* core);
static void onGroupAction(Tox* tox, int groupnumber, int peernumber, const uint8_t * action,
uint16_t length, void* core);
static void onGroupInvite(Tox *tox, int32_t friendId, uint8_t type, const uint8_t *data,
uint16_t length, void *userdata);
static void onGroupMessage(Tox *tox, int groupnumber, int friendgroupnumber,
const uint8_t * message, uint16_t length, void *userdata);
static void onGroupNamelistChange(Tox *tox, int groupId, int peerId, uint8_t change, void *core);
static void onGroupTitleChange(Tox*, int groupnumber, int peernumber,
const uint8_t* title, uint8_t len, void* _core);
static void onReadReceiptCallback(Tox *tox, uint32_t friendId, uint32_t receipt, void *core);
static void onAvInvite(void* toxav, int32_t call_index, void* core);
static void onAvStart(void* toxav, int32_t call_index, void* core);
static void onAvCancel(void* toxav, int32_t call_index, void* core);
static void onAvReject(void* toxav, int32_t call_index, void* core);
static void onAvEnd(void* toxav, int32_t call_index, void* core);
static void onAvRinging(void* toxav, int32_t call_index, void* core);
static void onAvRequestTimeout(void* toxav, int32_t call_index, void* core);
static void onAvPeerTimeout(void* toxav, int32_t call_index, void* core);
static void onAvMediaChange(void *toxav, int32_t call_index, void* core);
2014-06-27 07:17:10 +08:00
2014-11-13 19:18:04 +08:00
static void sendGroupCallAudio(int groupId, ToxAv* toxav);
static void prepareCall(uint32_t friendId, int callId, ToxAv *toxav, bool videoEnabled);
static void cleanupCall(int callId);
static void playCallAudio(void *toxav, int32_t callId, const int16_t *data,
uint16_t samples, void *user_data); // Callback
2014-07-09 15:55:25 +08:00
static void sendCallAudio(int callId, ToxAv* toxav);
static void playAudioBuffer(ALuint alSource, const int16_t *data, int samples,
unsigned channels, int sampleRate);
2014-11-18 06:17:54 +08:00
static void playCallVideo(void *toxav, int32_t callId, const vpx_image_t* img, void *user_data);
2015-05-14 10:46:28 +08:00
static void sendCallVideo(int callId, ToxAv* toxav, std::shared_ptr<VideoFrame> frame);
bool checkConnection();
void checkEncryptedHistory();
2015-06-04 08:43:07 +08:00
void makeTox(QByteArray savedata);
void loadFriends();
void checkLastOnline(uint32_t friendId);
2014-12-06 10:58:44 +08:00
void deadifyTox();
2015-06-04 18:14:17 +08:00
private slots:
2015-06-04 22:36:08 +08:00
void killTimers(bool onlyStop); ///< Must only be called from the Core thread
2015-06-04 18:14:17 +08:00
private:
Tox* tox;
ToxAv* toxav;
2015-06-04 18:14:17 +08:00
QTimer *toxTimer;
Profile& profile;
static ToxCall calls[TOXAV_MAX_CALLS];
#ifdef QTOX_FILTER_AUDIO
static AudioFilterer * filterer[TOXAV_MAX_CALLS];
#endif
2014-11-13 19:18:04 +08:00
static QHash<int, ToxGroupCall> groupCalls; // Maps group IDs to ToxGroupCalls
QMutex messageSendMutex;
bool ready;
2014-07-12 18:50:40 +08:00
static const int videobufsize;
static uint8_t* videobuf;
2014-08-30 20:40:41 +08:00
static QThread *coreThread;
friend class Audio; ///< Audio can access our calls directly to reduce latency
friend class CoreFile; ///< CoreFile can access tox* and emit our signals
2014-06-25 04:11:11 +08:00
};
#endif // CORE_HPP