2016-10-20 17:00:05 +08:00
|
|
|
/*
|
2018-04-13 04:02:28 +08:00
|
|
|
Copyright © 2015-2018 by The qTox Project Contributors
|
2016-10-20 17:00:05 +08:00
|
|
|
|
|
|
|
This file is part of qTox, a Qt-based graphical interface for Tox.
|
|
|
|
|
|
|
|
qTox is libre 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,
|
|
|
|
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.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with qTox. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2015-12-17 18:24:01 +08:00
|
|
|
#ifndef HISTORY_H
|
|
|
|
#define HISTORY_H
|
|
|
|
|
|
|
|
#include <QDateTime>
|
|
|
|
#include <QHash>
|
2018-09-29 17:28:17 +08:00
|
|
|
#include <QPointer>
|
2017-02-26 19:52:45 +08:00
|
|
|
#include <QVector>
|
2016-10-20 17:00:05 +08:00
|
|
|
|
2018-09-29 17:28:17 +08:00
|
|
|
#include <cassert>
|
2015-12-17 18:24:01 +08:00
|
|
|
#include <cstdint>
|
2016-10-20 17:00:05 +08:00
|
|
|
#include <tox/toxencryptsave.h>
|
|
|
|
|
2018-09-29 17:28:17 +08:00
|
|
|
#include "src/core/toxfile.h"
|
2017-06-25 05:45:47 +08:00
|
|
|
#include "src/core/toxpk.h"
|
2016-12-19 10:26:26 +08:00
|
|
|
#include "src/persistence/db/rawdatabase.h"
|
2018-06-25 02:11:20 +08:00
|
|
|
#include "src/widget/searchtypes.h"
|
2015-12-17 18:24:01 +08:00
|
|
|
|
|
|
|
class Profile;
|
|
|
|
class HistoryKeeper;
|
|
|
|
|
2018-09-29 17:28:17 +08:00
|
|
|
enum class HistMessageContentType
|
|
|
|
{
|
|
|
|
message,
|
|
|
|
file
|
|
|
|
};
|
|
|
|
|
|
|
|
class HistMessageContent
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
HistMessageContent(QString message)
|
|
|
|
: data(std::make_shared<QString>(std::move(message)))
|
|
|
|
, type(HistMessageContentType::message)
|
|
|
|
{}
|
|
|
|
|
|
|
|
HistMessageContent(ToxFile file)
|
|
|
|
: data(std::make_shared<ToxFile>(std::move(file)))
|
|
|
|
, type(HistMessageContentType::file)
|
|
|
|
{}
|
|
|
|
|
|
|
|
HistMessageContentType getType() const
|
|
|
|
{
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString& asMessage()
|
|
|
|
{
|
|
|
|
assert(type == HistMessageContentType::message);
|
|
|
|
return *static_cast<QString*>(data.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
ToxFile& asFile()
|
|
|
|
{
|
|
|
|
assert(type == HistMessageContentType::file);
|
|
|
|
return *static_cast<ToxFile*>(data.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString& asMessage() const
|
|
|
|
{
|
|
|
|
assert(type == HistMessageContentType::message);
|
|
|
|
return *static_cast<QString*>(data.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
const ToxFile& asFile() const
|
|
|
|
{
|
|
|
|
assert(type == HistMessageContentType::file);
|
|
|
|
return *static_cast<ToxFile*>(data.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Not really shared but shared_ptr has support for shared_ptr<void>
|
|
|
|
std::shared_ptr<void> data;
|
|
|
|
HistMessageContentType type;
|
|
|
|
};
|
|
|
|
|
2018-09-24 16:05:43 +08:00
|
|
|
struct FileDbInsertionData
|
2017-02-26 19:52:45 +08:00
|
|
|
{
|
2018-09-24 16:05:43 +08:00
|
|
|
FileDbInsertionData();
|
|
|
|
|
|
|
|
int64_t historyId;
|
|
|
|
QString friendPk;
|
|
|
|
QString fileId;
|
2018-12-12 19:31:04 +08:00
|
|
|
QString fileName;
|
2018-09-24 16:05:43 +08:00
|
|
|
QString filePath;
|
|
|
|
int64_t size;
|
|
|
|
int direction;
|
|
|
|
};
|
|
|
|
Q_DECLARE_METATYPE(FileDbInsertionData);
|
|
|
|
|
|
|
|
class History : public QObject, public std::enable_shared_from_this<History>
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
2015-12-17 18:24:01 +08:00
|
|
|
public:
|
|
|
|
struct HistMessage
|
|
|
|
{
|
2017-02-26 19:52:45 +08:00
|
|
|
HistMessage(qint64 id, bool isSent, QDateTime timestamp, QString chat, QString dispName,
|
|
|
|
QString sender, QString message)
|
|
|
|
: chat{chat}
|
|
|
|
, sender{sender}
|
|
|
|
, dispName{dispName}
|
|
|
|
, timestamp{timestamp}
|
|
|
|
, id{id}
|
|
|
|
, isSent{isSent}
|
2018-09-29 17:28:17 +08:00
|
|
|
, content(std::move(message))
|
2018-09-24 16:05:43 +08:00
|
|
|
{}
|
2015-12-17 18:24:01 +08:00
|
|
|
|
2018-09-29 17:28:17 +08:00
|
|
|
HistMessage(qint64 id, bool isSent, QDateTime timestamp, QString chat, QString dispName,
|
|
|
|
QString sender, ToxFile file)
|
|
|
|
: chat{chat}
|
|
|
|
, sender{sender}
|
|
|
|
, dispName{dispName}
|
|
|
|
, timestamp{timestamp}
|
|
|
|
, id{id}
|
|
|
|
, isSent{isSent}
|
|
|
|
, content(std::move(file))
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
2015-12-17 18:24:01 +08:00
|
|
|
QString chat;
|
|
|
|
QString sender;
|
|
|
|
QString dispName;
|
|
|
|
QDateTime timestamp;
|
|
|
|
qint64 id;
|
|
|
|
bool isSent;
|
2018-09-29 17:28:17 +08:00
|
|
|
HistMessageContent content;
|
2015-12-17 18:24:01 +08:00
|
|
|
};
|
|
|
|
|
2017-06-25 05:45:47 +08:00
|
|
|
struct DateMessages
|
|
|
|
{
|
|
|
|
uint offsetDays;
|
|
|
|
uint count;
|
|
|
|
};
|
|
|
|
|
2015-12-17 18:24:01 +08:00
|
|
|
public:
|
2017-06-12 16:33:19 +08:00
|
|
|
explicit History(std::shared_ptr<RawDatabase> db);
|
2015-12-17 18:24:01 +08:00
|
|
|
~History();
|
2016-07-27 06:20:54 +08:00
|
|
|
|
2015-12-17 18:24:01 +08:00
|
|
|
bool isValid();
|
|
|
|
|
2018-10-05 10:24:39 +08:00
|
|
|
bool isHistoryExistence(const QString& friendPk);
|
|
|
|
|
2015-12-17 18:24:01 +08:00
|
|
|
void eraseHistory();
|
|
|
|
void removeFriendHistory(const QString& friendPk);
|
2017-02-26 19:52:45 +08:00
|
|
|
void addNewMessage(const QString& friendPk, const QString& message, const QString& sender,
|
|
|
|
const QDateTime& time, bool isSent, QString dispName,
|
2017-06-12 02:48:44 +08:00
|
|
|
const std::function<void(int64_t)>& insertIdCallback = {});
|
2016-07-27 06:20:54 +08:00
|
|
|
|
2018-09-24 16:05:43 +08:00
|
|
|
void addNewFileMessage(const QString& friendPk, const QString& fileId,
|
2018-12-12 19:31:04 +08:00
|
|
|
const QString& fileName, const QString& filePath, int64_t size,
|
2018-09-24 16:05:43 +08:00
|
|
|
const QString& sender, const QDateTime& time, QString const& dispName);
|
|
|
|
|
2018-11-30 17:40:46 +08:00
|
|
|
void setFileFinished(const QString& fileId, bool success, const QString& filePath, const QByteArray& fileHash);
|
2018-09-29 17:28:17 +08:00
|
|
|
|
2018-04-24 07:51:26 +08:00
|
|
|
QList<HistMessage> getChatHistoryFromDate(const QString& friendPk, const QDateTime& from,
|
2018-11-10 09:43:37 +08:00
|
|
|
const QDateTime& to);
|
2018-04-24 07:51:26 +08:00
|
|
|
QList<HistMessage> getChatHistoryDefaultNum(const QString& friendPk);
|
2017-06-25 05:45:47 +08:00
|
|
|
QList<DateMessages> getChatHistoryCounts(const ToxPk& friendPk, const QDate& from, const QDate& to);
|
2018-11-10 09:43:37 +08:00
|
|
|
QDateTime getDateWhereFindPhrase(const QString& friendPk, const QDateTime& from, QString phrase,
|
|
|
|
const ParameterSearch& parameter);
|
2018-06-25 02:11:20 +08:00
|
|
|
QDateTime getStartDateChatHistory(const QString& friendPk);
|
2017-06-25 05:45:47 +08:00
|
|
|
|
2016-10-20 17:00:05 +08:00
|
|
|
void markAsSent(qint64 messageId);
|
2017-02-26 19:52:45 +08:00
|
|
|
|
2015-12-17 18:24:01 +08:00
|
|
|
protected:
|
2017-02-26 19:52:45 +08:00
|
|
|
QVector<RawDatabase::Query>
|
|
|
|
generateNewMessageQueries(const QString& friendPk, const QString& message,
|
|
|
|
const QString& sender, const QDateTime& time, bool isSent,
|
|
|
|
QString dispName, std::function<void(int64_t)> insertIdCallback = {});
|
2015-12-17 18:24:01 +08:00
|
|
|
|
2018-09-24 16:05:43 +08:00
|
|
|
signals:
|
|
|
|
void fileInsertionReady(FileDbInsertionData data);
|
2018-09-29 17:28:17 +08:00
|
|
|
void fileInserted(int64_t dbId, QString fileId);
|
2018-09-24 16:05:43 +08:00
|
|
|
|
|
|
|
private slots:
|
|
|
|
void onFileInsertionReady(FileDbInsertionData data);
|
2018-09-29 17:28:17 +08:00
|
|
|
void onFileInserted(int64_t dbId, QString fileId);
|
2018-09-24 16:05:43 +08:00
|
|
|
|
2015-12-17 18:24:01 +08:00
|
|
|
private:
|
2018-04-24 07:51:26 +08:00
|
|
|
QList<HistMessage> getChatHistory(const QString& friendPk, const QDateTime& from,
|
|
|
|
const QDateTime& to, int numMessages);
|
2018-09-29 17:28:17 +08:00
|
|
|
|
|
|
|
static RawDatabase::Query generateFileFinished(int64_t fileId, bool success,
|
2018-11-30 17:40:46 +08:00
|
|
|
const QString& filePath, const QByteArray& fileHash);
|
2018-09-24 16:05:43 +08:00
|
|
|
void dbSchemaUpgrade();
|
2018-09-24 16:05:43 +08:00
|
|
|
|
2016-10-20 17:00:05 +08:00
|
|
|
std::shared_ptr<RawDatabase> db;
|
2018-09-24 16:05:43 +08:00
|
|
|
|
|
|
|
|
2016-07-27 06:20:54 +08:00
|
|
|
QHash<QString, int64_t> peers;
|
2018-09-29 17:28:17 +08:00
|
|
|
struct FileInfo
|
|
|
|
{
|
|
|
|
bool finished = false;
|
|
|
|
bool success = false;
|
|
|
|
QString filePath;
|
2018-11-30 17:40:46 +08:00
|
|
|
QByteArray fileHash;
|
2018-09-29 17:28:17 +08:00
|
|
|
int64_t fileId = -1;
|
|
|
|
};
|
|
|
|
|
|
|
|
// This needs to be a shared pointer to avoid callback lifetime issues
|
|
|
|
QHash<QString, FileInfo> fileInfos;
|
2015-12-17 18:24:01 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // HISTORY_H
|