tdesktop/Telegram/SourceFiles/data/data_drafts.h

160 lines
3.6 KiB
C
Raw Normal View History

2016-05-31 09:46:31 +00:00
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
2016-05-31 09:46:31 +00:00
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
2016-05-31 09:46:31 +00:00
*/
#pragma once
namespace Ui {
2018-05-21 21:31:46 +00:00
class InputField;
} // namespace Ui
2020-06-08 08:03:45 +00:00
namespace Main {
class Session;
} // namespace Main
2016-05-31 09:46:31 +00:00
namespace Data {
2020-06-08 08:03:45 +00:00
void ApplyPeerCloudDraft(
not_null<Main::Session*> session,
PeerId peerId,
const MTPDdraftMessage &draft);
void ClearPeerCloudDraft(
not_null<Main::Session*> session,
PeerId peerId,
TimeId date);
enum class PreviewState : char {
Allowed,
Cancelled,
EmptyOnEdit,
};
struct Draft {
Draft() = default;
Draft(
const TextWithTags &textWithTags,
MsgId msgId,
const MessageCursor &cursor,
PreviewState previewState,
mtpRequestId saveRequestId = 0);
Draft(
2018-05-21 21:31:46 +00:00
not_null<const Ui::InputField*> field,
MsgId msgId,
PreviewState previewState,
mtpRequestId saveRequestId = 0);
TimeId date = 0;
TextWithTags textWithTags;
MsgId msgId = 0; // replyToId for message draft, editMsgId for edit draft
MessageCursor cursor;
PreviewState previewState = PreviewState::Allowed;
mtpRequestId saveRequestId = 0;
};
class DraftKey {
public:
[[nodiscard]] static DraftKey None() {
return 0;
}
[[nodiscard]] static DraftKey Local() {
return kLocalDraftIndex;
}
[[nodiscard]] static DraftKey LocalEdit() {
return kLocalDraftIndex + kEditDraftShift;
}
[[nodiscard]] static DraftKey Cloud() {
return kCloudDraftIndex;
}
[[nodiscard]] static DraftKey Scheduled() {
return kScheduledDraftIndex;
}
[[nodiscard]] static DraftKey ScheduledEdit() {
return kScheduledDraftIndex + kEditDraftShift;
}
[[nodiscard]] static DraftKey Replies(MsgId rootId) {
2021-09-30 06:04:55 +00:00
return rootId.bare;
}
[[nodiscard]] static DraftKey RepliesEdit(MsgId rootId) {
2021-09-30 06:04:55 +00:00
return rootId.bare + kEditDraftShift;
}
2021-09-30 06:04:55 +00:00
[[nodiscard]] static DraftKey FromSerialized(qint64 value) {
return value;
}
2021-09-30 06:04:55 +00:00
[[nodiscard]] qint64 serialize() const {
return _value;
}
2021-09-30 06:04:55 +00:00
[[nodiscard]] static DraftKey FromSerializedOld(int32 value);
inline bool operator<(const DraftKey &other) const {
return _value < other._value;
}
inline bool operator==(const DraftKey &other) const {
return _value == other._value;
}
inline bool operator>(const DraftKey &other) const {
return (other < *this);
}
inline bool operator<=(const DraftKey &other) const {
return !(other < *this);
}
inline bool operator>=(const DraftKey &other) const {
return !(*this < other);
}
inline bool operator!=(const DraftKey &other) const {
return !(*this == other);
}
inline explicit operator bool() const {
return _value != 0;
}
private:
2021-09-30 06:04:55 +00:00
DraftKey(int64 value) : _value(value) {
}
static constexpr auto kLocalDraftIndex = -1;
static constexpr auto kCloudDraftIndex = -2;
static constexpr auto kScheduledDraftIndex = -3;
2021-09-30 06:04:55 +00:00
static constexpr auto kEditDraftShift = ServerMaxMsgId.bare;
static constexpr auto kEditDraftShiftOld = 0x3FFF'FFFF;
2021-09-30 06:04:55 +00:00
int64 _value = 0;
};
using HistoryDrafts = base::flat_map<DraftKey, std::unique_ptr<Draft>>;
inline bool draftStringIsEmpty(const QString &text) {
for (const auto &ch : text) {
if (!ch.isSpace()) {
return false;
}
}
return true;
}
inline bool draftIsNull(const Draft *draft) {
return !draft
|| (draftStringIsEmpty(draft->textWithTags.text) && !draft->msgId);
}
inline bool draftsAreEqual(const Draft *a, const Draft *b) {
bool aIsNull = draftIsNull(a);
bool bIsNull = draftIsNull(b);
if (aIsNull) {
return bIsNull;
} else if (bIsNull) {
return false;
}
return (a->textWithTags == b->textWithTags)
&& (a->msgId == b->msgId)
&& (a->previewState == b->previewState);
}
2016-05-31 09:46:31 +00:00
} // namespace Data