tdesktop/Telegram/SourceFiles/structs.h

1159 lines
30 KiB
C
Raw Normal View History

2015-04-04 20:01:34 +00:00
/*
This file is part of Telegram Desktop,
the official desktop version of Telegram messaging app, see https://telegram.org
Telegram Desktop 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.
It 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.
2015-10-03 13:16:42 +00:00
In addition, as a special exception, the copyright holders give permission
to link the code of portions of this program with the OpenSSL library.
2015-04-04 20:01:34 +00:00
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
2015-10-03 13:16:42 +00:00
Copyright (c) 2014-2015 John Preston, https://desktop.telegram.org
2015-04-04 20:01:34 +00:00
*/
#pragma once
typedef int32 ChannelId;
static const ChannelId NoChannel = 0;
2015-04-04 20:01:34 +00:00
typedef uint64 PeerId;
static const uint64 PeerIdMask = 0xFFFFFFFFULL;
static const uint64 PeerIdTypeMask = 0x300000000ULL;
static const uint64 PeerIdUserShift = 0x000000000ULL;
static const uint64 PeerIdChatShift = 0x100000000ULL;
static const uint64 PeerIdChannelShift = 0x200000000ULL;
inline bool peerIsUser(const PeerId &id) {
return (id & PeerIdTypeMask) == PeerIdUserShift;
}
inline bool peerIsChat(const PeerId &id) {
return (id & PeerIdTypeMask) == PeerIdChatShift;
}
inline bool peerIsChannel(const PeerId &id) {
return (id & PeerIdTypeMask) == PeerIdChannelShift;
}
inline PeerId peerFromUser(int32 user_id) {
return PeerIdUserShift | uint64(uint32(user_id));
}
inline PeerId peerFromChat(int32 chat_id) {
return PeerIdChatShift | uint64(uint32(chat_id));
}
inline PeerId peerFromChannel(ChannelId channel_id) {
return PeerIdChannelShift | uint64(uint32(channel_id));
}
inline PeerId peerFromUser(const MTPint &user_id) {
return peerFromUser(user_id.v);
}
inline PeerId peerFromChat(const MTPint &chat_id) {
return peerFromChat(chat_id.v);
}
inline PeerId peerFromChannel(const MTPint &channel_id) {
return peerFromChannel(channel_id.v);
}
inline int32 peerToBareInt(const PeerId &id) {
return int32(uint32(id & PeerIdMask));
}
inline int32 peerToUser(const PeerId &id) {
return peerIsUser(id) ? peerToBareInt(id) : 0;
}
inline int32 peerToChat(const PeerId &id) {
return peerIsChat(id) ? peerToBareInt(id) : 0;
}
inline ChannelId peerToChannel(const PeerId &id) {
return peerIsChannel(id) ? peerToBareInt(id) : NoChannel;
}
inline MTPint peerToBareMTPInt(const PeerId &id) {
return MTP_int(peerToBareInt(id));
}
inline PeerId peerFromMTP(const MTPPeer &peer) {
switch (peer.type()) {
case mtpc_peerUser: return peerFromUser(peer.c_peerUser().vuser_id);
case mtpc_peerChat: return peerFromChat(peer.c_peerChat().vchat_id);
case mtpc_peerChannel: return peerFromChannel(peer.c_peerChannel().vchannel_id);
}
return 0;
}
inline MTPpeer peerToMTP(const PeerId &id) {
if (peerIsUser(id)) {
return MTP_peerUser(peerToBareMTPInt(id));
} else if (peerIsChat(id)) {
return MTP_peerChat(peerToBareMTPInt(id));
} else if (peerIsChannel(id)) {
return MTP_peerChannel(peerToBareMTPInt(id));
}
return MTP_peerUser(MTP_int(0));
}
inline PeerId peerFromMessage(const MTPmessage &msg) {
2015-09-13 08:41:27 +00:00
PeerId from_id = 0, to_id = 0;
switch (msg.type()) {
case mtpc_message:
from_id = msg.c_message().has_from_id() ? peerFromUser(msg.c_message().vfrom_id) : 0;
to_id = peerFromMTP(msg.c_message().vto_id);
break;
case mtpc_messageService:
from_id = msg.c_messageService().has_from_id() ? peerFromUser(msg.c_messageService().vfrom_id) : 0;
to_id = peerFromMTP(msg.c_messageService().vto_id);
break;
}
return (from_id && peerToUser(to_id) == MTP::authedId()) ? from_id : to_id;
}
inline int32 flagsFromMessage(const MTPmessage &msg) {
switch (msg.type()) {
case mtpc_message: return msg.c_message().vflags.v;
case mtpc_messageService: return msg.c_messageService().vflags.v;
}
return 0;
}
2015-09-21 20:57:42 +00:00
inline int32 idFromMessage(const MTPmessage &msg) {
switch (msg.type()) {
case mtpc_messageEmpty: return msg.c_messageEmpty().vid.v;
case mtpc_message: return msg.c_message().vid.v;
case mtpc_messageService: return msg.c_messageService().vid.v;
}
return 0;
}
2015-04-04 20:01:34 +00:00
typedef uint64 PhotoId;
typedef uint64 VideoId;
typedef uint64 AudioId;
typedef uint64 DocumentId;
typedef uint64 WebPageId;
typedef int32 MsgId;
struct FullMsgId {
FullMsgId() : channel(NoChannel), msg(0) {
}
FullMsgId(ChannelId channel, MsgId msg) : channel(channel), msg(msg) {
}
ChannelId channel;
MsgId msg;
};
inline bool operator==(const FullMsgId &a, const FullMsgId &b) {
return (a.channel == b.channel) && (a.msg == b.msg);
}
inline bool operator<(const FullMsgId &a, const FullMsgId &b) {
if (a.msg < b.msg) return true;
if (a.msg > b.msg) return false;
return a.channel < b.channel;
}
2015-04-04 20:01:34 +00:00
static const MsgId ShowAtTheEndMsgId = -0x40000000;
2015-09-19 09:13:21 +00:00
static const MsgId SwitchAtTopMsgId = -0x3FFFFFFF;
static const MsgId ShowAtUnreadMsgId = 0;
2015-04-04 20:01:34 +00:00
struct NotifySettings {
NotifySettings() : mute(0), sound("default"), previews(true), events(1) {
}
int32 mute;
string sound;
bool previews;
int32 events;
};
typedef NotifySettings *NotifySettingsPtr;
static const NotifySettingsPtr UnknownNotifySettings = NotifySettingsPtr(0);
static const NotifySettingsPtr EmptyNotifySettings = NotifySettingsPtr(1);
extern NotifySettings globalNotifyAll, globalNotifyUsers, globalNotifyChats;
extern NotifySettingsPtr globalNotifyAllPtr, globalNotifyUsersPtr, globalNotifyChatsPtr;
inline bool isNotifyMuted(NotifySettingsPtr settings, int32 *changeIn = 0) {
if (settings != UnknownNotifySettings && settings != EmptyNotifySettings) {
int32 t = unixtime();
if (settings->mute > t) {
if (changeIn) *changeIn = settings->mute - t + 1;
return true;
}
2015-04-04 20:01:34 +00:00
}
if (changeIn) *changeIn = 0;
return false;
2015-04-04 20:01:34 +00:00
}
style::color peerColor(int32 index);
ImagePtr userDefPhoto(int32 index);
ImagePtr chatDefPhoto(int32 index);
ImagePtr channelDefPhoto(int32 index);
2015-04-04 20:01:34 +00:00
static const PhotoId UnknownPeerPhotoId = 0xFFFFFFFFFFFFFFFFULL;
2015-09-06 10:17:09 +00:00
inline const QString &emptyUsername() {
static QString empty;
return empty;
}
class UserData;
class ChatData;
class ChannelData;
class PeerData {
public:
2015-04-04 20:01:34 +00:00
virtual ~PeerData() {
if (notify != UnknownNotifySettings && notify != EmptyNotifySettings) {
delete notify;
notify = UnknownNotifySettings;
}
}
bool isUser() const {
return peerIsUser(id);
}
bool isChat() const {
return peerIsChat(id);
}
bool isChannel() const {
return peerIsChannel(id);
}
bool isSelf() const {
return (input.type() == mtpc_inputPeerSelf);
}
2015-10-29 15:52:39 +00:00
bool isVerified() const;
2015-04-04 20:01:34 +00:00
UserData *asUser();
const UserData *asUser() const;
ChatData *asChat();
const ChatData *asChat() const;
ChannelData *asChannel();
const ChannelData *asChannel() const;
2015-04-04 20:01:34 +00:00
void updateName(const QString &newName, const QString &newNameOrPhone, const QString &newUsername);
void fillNames();
const Text &dialogName() const;
const QString &shortName() const;
2015-09-06 10:17:09 +00:00
const QString &userName() const;
2015-04-04 20:01:34 +00:00
const PeerId id;
int32 bareId() const {
return int32(uint32(id & 0xFFFFFFFFULL));
}
2015-04-04 20:01:34 +00:00
TextLinkPtr lnk;
2015-04-04 20:01:34 +00:00
QString name;
Text nameText;
2015-04-04 20:01:34 +00:00
typedef QSet<QString> Names;
Names names; // for filtering
typedef QSet<QChar> NameFirstChars;
NameFirstChars chars;
bool loaded;
MTPinputPeer input;
int32 colorIndex;
style::color color;
ImagePtr photo;
PhotoId photoId;
StorageImageLocation photoLoc;
2015-04-04 20:01:34 +00:00
int32 nameVersion;
NotifySettingsPtr notify;
private:
PeerData(const PeerId &id);
friend class UserData;
friend class ChatData;
friend class ChannelData;
2015-04-04 20:01:34 +00:00
};
2015-04-04 20:01:34 +00:00
static const uint64 UserNoAccess = 0xFFFFFFFFFFFFFFFFULL;
class PeerLink : public ITextLink {
TEXT_LINK_CLASS(PeerLink)
2015-04-04 20:01:34 +00:00
public:
PeerLink(PeerData *peer) : _peer(peer) {
}
void onClick(Qt::MouseButton button) const;
PeerData *peer() const {
return _peer;
}
private:
PeerData *_peer;
};
class BotCommand {
public:
BotCommand(const QString &command, const QString &description) : command(command), _description(description) {
}
QString command;
bool setDescription(const QString &description) {
if (_description != description) {
_description = description;
_descriptionText = Text();
return true;
}
return false;
}
const Text &descriptionText() const {
if (_descriptionText.isEmpty() && !_description.isEmpty()) {
_descriptionText.setText(st::mentionFont, _description, _textNameOptions);
}
return _descriptionText;
}
private:
QString _description;
mutable Text _descriptionText;
};
struct BotInfo {
BotInfo() : inited(false), readsAllHistory(false), cantJoinGroups(false), version(0), text(st::msgMinWidth) {
}
bool inited;
bool readsAllHistory, cantJoinGroups;
int32 version;
QString shareText, description;
QList<BotCommand> commands;
Text text; // description
2015-06-17 19:43:03 +00:00
QString startToken, startGroupToken;
};
enum UserBlockedStatus {
UserBlockUnknown = 0,
UserIsBlocked,
UserIsNotBlocked,
};
2015-04-04 20:01:34 +00:00
struct PhotoData;
class UserData : public PeerData {
public:
2015-10-29 15:52:39 +00:00
UserData(const PeerId &id) : PeerData(id), access(0), flags(0), onlineTill(0), contact(-1), blocked(UserBlockUnknown), photosCount(-1), botInfo(0) {
setName(QString(), QString(), QString(), QString());
2015-04-04 20:01:34 +00:00
}
void setPhoto(const MTPUserProfilePhoto &photo);
void setName(const QString &first, const QString &last, const QString &phoneName, const QString &username);
void setPhone(const QString &newPhone);
void setBotInfoVersion(int32 version);
void setBotInfo(const MTPBotInfo &info);
void setNameOrPhone(const QString &newNameOrPhone);
2015-04-04 20:01:34 +00:00
void madeAction(); // pseudo-online
uint64 access;
2015-10-29 15:52:39 +00:00
int32 flags;
bool isVerified() const {
return flags & MTPDuser::flag_verified;
}
MTPInputUser inputUser;
2015-04-04 20:01:34 +00:00
QString firstName;
QString lastName;
QString username;
QString phone;
QString nameOrPhone;
Text phoneText;
2015-04-04 20:01:34 +00:00
int32 onlineTill;
int32 contact; // -1 - not contact, cant add (self, empty, deleted, foreign), 0 - not contact, can add (request), 1 - contact
UserBlockedStatus blocked;
2015-04-04 20:01:34 +00:00
typedef QList<PhotoData*> Photos;
Photos photos;
int32 photosCount; // -1 not loaded, 0 all loaded
BotInfo *botInfo;
2015-04-04 20:01:34 +00:00
};
class ChatData : public PeerData {
public:
2015-10-29 19:10:49 +00:00
ChatData(const PeerId &id) : PeerData(id), inputChat(MTP_int(bareId())), count(0), date(0), version(0), creator(0), inviterForSpamReport(0), flags(0), isForbidden(false), botStatus(0) {
2015-04-04 20:01:34 +00:00
}
void setPhoto(const MTPChatPhoto &photo, const PhotoId &phId = UnknownPeerPhotoId);
void invalidateParticipants() {
2015-10-29 19:10:49 +00:00
participants = ChatData::Participants();
admins = ChatData::Admins();
2015-11-02 05:49:25 +00:00
flags &= ~MTPDchat::flag_admin;
2015-10-29 19:10:49 +00:00
invitedByMe = ChatData::InvitedByMe();
botStatus = 0;
}
bool noParticipantInfo() const {
return (count > 0 || amIn()) && participants.isEmpty();
2015-10-29 19:10:49 +00:00
}
2015-09-16 21:15:13 +00:00
MTPint inputChat;
2015-04-04 20:01:34 +00:00
int32 count;
int32 date;
int32 version;
2015-09-21 20:57:42 +00:00
int32 creator;
int32 inviterForSpamReport; // > 0 - user who invited me to chat in unread service msg, < 0 - have outgoing message
2015-09-21 20:57:42 +00:00
2015-10-29 19:10:49 +00:00
int32 flags;
2015-09-21 20:57:42 +00:00
bool isForbidden;
2015-10-29 19:10:49 +00:00
bool amIn() const {
return !isForbidden && !haveLeft() && !wasKicked();
}
bool canEdit() const {
return amCreator() || (adminsEnabled() ? amAdmin() : amIn());
}
bool haveLeft() const {
return flags & MTPDchat::flag_left;
}
bool wasKicked() const {
return flags & MTPDchat::flag_kicked;
}
bool adminsEnabled() const {
return flags & MTPDchat::flag_admins_enabled;
}
bool amCreator() const {
return flags & MTPDchat::flag_creator;
}
bool amAdmin() const {
return flags & MTPDchat::flag_admin;
}
2015-04-04 20:01:34 +00:00
typedef QMap<UserData*, int32> Participants;
Participants participants;
2015-10-29 19:10:49 +00:00
typedef QMap<UserData*, bool> InvitedByMe;
InvitedByMe invitedByMe;
typedef QMap<UserData*, bool> Admins;
Admins admins;
2015-04-04 20:01:34 +00:00
typedef QList<UserData*> LastAuthors;
LastAuthors lastAuthors;
typedef QMap<PeerData*, bool> MarkupSenders;
2015-06-17 19:43:03 +00:00
MarkupSenders markupSenders;
int32 botStatus; // -1 - no bots, 0 - unknown, 1 - one bot, that sees all history, 2 - other
// ImagePtr photoFull;
QString invitationUrl;
2015-04-04 20:01:34 +00:00
};
2015-09-13 08:41:27 +00:00
enum PtsSkippedQueue {
SkippedUpdate,
SkippedUpdates,
};
class PtsWaiter {
public:
PtsWaiter() :
_good(0),
_last(0),
_count(0),
_applySkippedLevel(0),
_requesting(false),
_waitingForSkipped(false),
_waitingForShortPoll(false) {
2015-09-13 08:41:27 +00:00
}
void init(int32 pts) {
_good = _last = _count = pts;
clearSkippedUpdates();
}
bool inited() const {
return _good > 0;
}
void setRequesting(bool isRequesting) {
_requesting = isRequesting;
2015-09-20 09:54:22 +00:00
if (_requesting) {
clearSkippedUpdates();
}
2015-09-13 08:41:27 +00:00
}
bool requesting() const {
return _requesting;
}
bool waitingForSkipped() const {
return _waitingForSkipped;
}
bool waitingForShortPoll() const {
return _waitingForShortPoll;
}
2015-09-20 09:54:22 +00:00
void setWaitingForSkipped(ChannelData *channel, int32 ms); // < 0 - not waiting
void setWaitingForShortPoll(ChannelData *channel, int32 ms); // < 0 - not waiting
2015-09-13 08:41:27 +00:00
int32 current() const{
return _good;
}
bool updated(ChannelData *channel, int32 pts, int32 count);
bool updated(ChannelData *channel, int32 pts, int32 count, const MTPUpdates &updates);
bool updated(ChannelData *channel, int32 pts, int32 count, const MTPUpdate &update);
void applySkippedUpdates(ChannelData *channel);
void clearSkippedUpdates();
private:
2015-09-21 20:57:42 +00:00
bool check(ChannelData *channel, int32 pts, int32 count); // return false if need to save that update and apply later
2015-09-13 08:41:27 +00:00
uint64 ptsKey(PtsSkippedQueue queue);
void checkForWaiting(ChannelData *channel);
2015-09-13 08:41:27 +00:00
QMap<uint64, PtsSkippedQueue> _queue;
QMap<uint64, MTPUpdate> _updateQueue;
QMap<uint64, MTPUpdates> _updatesQueue;
int32 _good, _last, _count;
int32 _applySkippedLevel;
bool _requesting, _waitingForSkipped, _waitingForShortPoll;
2015-09-13 08:41:27 +00:00
};
class ChannelData : public PeerData {
public:
2015-10-12 21:02:10 +00:00
ChannelData(const PeerId &id) : PeerData(id), access(0), inputChannel(MTP_inputChannel(MTP_int(bareId()), MTP_long(0))), count(1), adminsCount(1), date(0), version(0), flags(0), flagsFull(0), isForbidden(true), botStatus(-1), inviter(0), _lastFullUpdate(0) {
2015-09-06 10:17:09 +00:00
setName(QString(), QString());
}
void setPhoto(const MTPChatPhoto &photo, const PhotoId &phId = UnknownPeerPhotoId);
2015-09-06 10:17:09 +00:00
void setName(const QString &name, const QString &username);
void updateFull(bool force = false);
2015-09-21 20:57:42 +00:00
void fullUpdated();
uint64 access;
2015-09-16 21:15:13 +00:00
MTPinputChannel inputChannel;
2015-09-21 20:57:42 +00:00
QString username, about;
int32 count, adminsCount;
int32 date;
int32 version;
2015-10-12 21:02:10 +00:00
int32 flags, flagsFull;
2015-09-21 20:57:42 +00:00
bool isBroadcast() const {
2015-10-29 00:16:52 +00:00
return flags & MTPDchannel::flag_broadcast;
2015-09-21 20:57:42 +00:00
}
bool isPublic() const {
return flags & MTPDchannel::flag_username;
}
bool amCreator() const {
2015-10-29 00:16:52 +00:00
return flags & MTPDchannel::flag_creator;
2015-09-21 20:57:42 +00:00
}
bool amEditor() const {
2015-10-29 00:16:52 +00:00
return flags & MTPDchannel::flag_editor;
2015-09-21 20:57:42 +00:00
}
bool amModerator() const {
2015-10-29 00:16:52 +00:00
return flags & MTPDchannel::flag_moderator;
2015-09-21 20:57:42 +00:00
}
bool haveLeft() const {
2015-10-29 00:16:52 +00:00
return flags & MTPDchannel::flag_left;
2015-09-21 20:57:42 +00:00
}
bool wasKicked() const {
2015-10-29 00:16:52 +00:00
return flags & MTPDchannel::flag_kicked;
2015-09-21 20:57:42 +00:00
}
bool amIn() const {
return !isForbidden && !haveLeft() && !wasKicked();
}
2015-09-21 20:57:42 +00:00
bool canPublish() const {
return amCreator() || amEditor();
}
2015-10-12 21:02:10 +00:00
bool canViewParticipants() const {
2015-10-29 00:16:52 +00:00
return flagsFull & MTPDchannelFull::flag_can_view_participants;
2015-10-12 21:02:10 +00:00
}
2015-09-21 20:57:42 +00:00
bool isForbidden;
bool isVerified() const {
2015-10-29 00:16:52 +00:00
return flags & MTPDchannel::flag_verified;
}
int32 botStatus; // -1 - no bots, 0 - unknown, 1 - one bot, that sees all history, 2 - other
// ImagePtr photoFull;
QString invitationUrl;
2015-09-13 08:41:27 +00:00
2015-09-21 20:57:42 +00:00
int32 inviter; // > 0 - user who invited me to channel, < 0 - not in channel
QDateTime inviteDate;
void ptsInit(int32 pts) {
_ptsWaiter.init(pts);
}
2015-09-13 08:41:27 +00:00
void ptsReceived(int32 pts) {
2015-09-20 09:54:22 +00:00
if (_ptsWaiter.updated(this, pts, 0)) {
_ptsWaiter.applySkippedUpdates(this);
}
2015-09-13 08:41:27 +00:00
}
bool ptsUpdated(int32 pts, int32 count) {
return _ptsWaiter.updated(this, pts, count);
}
bool ptsUpdated(int32 pts, int32 count, const MTPUpdate &update) {
return _ptsWaiter.updated(this, pts, count, update);
}
int32 pts() const {
return _ptsWaiter.current();
}
bool ptsInited() const {
return _ptsWaiter.inited();
}
bool ptsRequesting() const {
return _ptsWaiter.requesting();
}
void ptsSetRequesting(bool isRequesting) {
return _ptsWaiter.setRequesting(isRequesting);
}
2015-09-20 09:54:22 +00:00
void ptsApplySkippedUpdates() {
return _ptsWaiter.applySkippedUpdates(this);
2015-09-13 08:41:27 +00:00
}
2015-09-20 09:54:22 +00:00
void ptsWaitingForShortPoll(int32 ms) { // < 0 - not waiting
return _ptsWaiter.setWaitingForShortPoll(this, ms);
}
2015-09-13 08:41:27 +00:00
private:
PtsWaiter _ptsWaiter;
2015-09-21 20:57:42 +00:00
uint64 _lastFullUpdate;
};
inline UserData *PeerData::asUser() {
return isUser() ? static_cast<UserData*>(this) : 0;
}
inline const UserData *PeerData::asUser() const {
return isUser() ? static_cast<const UserData*>(this) : 0;
}
inline ChatData *PeerData::asChat() {
return isChat() ? static_cast<ChatData*>(this) : 0;
}
inline const ChatData *PeerData::asChat() const {
return isChat() ? static_cast<const ChatData*>(this) : 0;
}
inline ChannelData *PeerData::asChannel() {
return isChannel() ? static_cast<ChannelData*>(this) : 0;
}
inline const ChannelData *PeerData::asChannel() const {
return isChannel() ? static_cast<const ChannelData*>(this) : 0;
}
2015-09-06 10:17:09 +00:00
inline const Text &PeerData::dialogName() const {
return (isUser() && !asUser()->phoneText.isEmpty()) ? asUser()->phoneText : nameText;
}
inline const QString &PeerData::shortName() const {
return isUser() ? asUser()->firstName : name;
}
inline const QString &PeerData::userName() const {
return isUser() ? asUser()->username : (isChannel() ? asChannel()->username : emptyUsername());
}
2015-10-29 15:52:39 +00:00
inline bool PeerData::isVerified() const {
return isUser() ? asUser()->isVerified() : (isChannel() ? asChannel()->isVerified() : false);
}
2015-09-06 10:17:09 +00:00
inline int32 newMessageFlags(PeerData *p) {
2015-10-29 00:16:52 +00:00
return p->isSelf() ? 0 : (((p->isChat() || (p->isUser() && !p->asUser()->botInfo)) ? MTPDmessage::flag_unread : 0) | MTPDmessage::flag_out);
}
2015-04-04 20:01:34 +00:00
typedef QMap<char, QPixmap> PreparedPhotoThumbs;
struct PhotoData {
PhotoData(const PhotoId &id, const uint64 &access = 0, int32 date = 0, const ImagePtr &thumb = ImagePtr(), const ImagePtr &medium = ImagePtr(), const ImagePtr &full = ImagePtr()) :
id(id), access(access), date(date), thumb(thumb), medium(medium), full(full), peer(0) {
2015-04-04 20:01:34 +00:00
}
void forget() {
thumb->forget();
replyPreview->forget();
medium->forget();
full->forget();
}
2015-04-06 22:15:29 +00:00
ImagePtr makeReplyPreview() {
if (replyPreview->isNull() && !thumb->isNull()) {
if (thumb->loaded()) {
int w = thumb->width(), h = thumb->height();
if (w <= 0) w = 1;
if (h <= 0) h = 1;
replyPreview = ImagePtr(w > h ? thumb->pix(w * st::msgReplyBarSize.height() / h, st::msgReplyBarSize.height()) : thumb->pix(st::msgReplyBarSize.height()), "PNG");
} else {
thumb->load();
}
}
return replyPreview;
}
2015-04-04 20:01:34 +00:00
PhotoId id;
uint64 access;
int32 date;
ImagePtr thumb, replyPreview;
ImagePtr medium;
ImagePtr full;
PeerData *peer; // for chat and channel photos connection
2015-04-04 20:01:34 +00:00
// geo, caption
int32 cachew;
QPixmap cache;
};
class PhotoLink : public ITextLink {
TEXT_LINK_CLASS(PhotoLink)
2015-04-04 20:01:34 +00:00
public:
PhotoLink(PhotoData *photo) : _photo(photo), _peer(0) {
}
PhotoLink(PhotoData *photo, PeerData *peer) : _photo(photo), _peer(peer) {
}
void onClick(Qt::MouseButton button) const;
PhotoData *photo() const {
return _photo;
}
PeerData *peer() const {
return _peer;
}
private:
PhotoData *_photo;
PeerData *_peer;
};
enum FileStatus {
FileFailed = -1,
FileUploading = 0,
FileReady = 1,
};
struct VideoData {
VideoData(const VideoId &id, const uint64 &access = 0, int32 date = 0, int32 duration = 0, int32 w = 0, int32 h = 0, const ImagePtr &thumb = ImagePtr(), int32 dc = 0, int32 size = 0);
2015-04-04 20:01:34 +00:00
void forget() {
thumb->forget();
replyPreview->forget();
}
void save(const QString &toFile);
void cancel(bool beforeDownload = false) {
mtpFileLoader *l = loader;
loader = 0;
if (l) {
l->cancel();
l->deleteLater();
l->rpcInvalidate();
}
location = FileLocation();
if (!beforeDownload) {
openOnSave = 0;
openOnSaveMsgId = FullMsgId();
2015-04-04 20:01:34 +00:00
}
}
void finish() {
if (loader->done()) {
2015-05-19 15:46:45 +00:00
location = FileLocation(mtpToStorageType(loader->fileType()), loader->fileName());
2015-04-04 20:01:34 +00:00
}
loader->deleteLater();
loader->rpcInvalidate();
loader = 0;
}
QString already(bool check = false);
VideoId id;
uint64 access;
int32 date;
int32 duration;
int32 w, h;
ImagePtr thumb, replyPreview;
int32 dc, size;
// geo, caption
FileStatus status;
int32 uploadOffset;
mtpTypeId fileType;
int32 openOnSave;
FullMsgId openOnSaveMsgId;
2015-04-04 20:01:34 +00:00
mtpFileLoader *loader;
FileLocation location;
};
class VideoLink : public ITextLink {
TEXT_LINK_CLASS(VideoLink)
2015-04-04 20:01:34 +00:00
public:
VideoLink(VideoData *video) : _video(video) {
}
VideoData *video() const {
return _video;
}
private:
VideoData *_video;
};
class VideoSaveLink : public VideoLink {
TEXT_LINK_CLASS(VideoSaveLink)
2015-04-04 20:01:34 +00:00
public:
VideoSaveLink(VideoData *video) : VideoLink(video) {
}
2015-04-19 10:29:19 +00:00
static void doSave(VideoData *video, bool forceSavingAs = false);
2015-04-04 20:01:34 +00:00
void onClick(Qt::MouseButton button) const;
};
class VideoOpenLink : public VideoLink {
TEXT_LINK_CLASS(VideoOpenLink)
2015-04-04 20:01:34 +00:00
public:
VideoOpenLink(VideoData *video) : VideoLink(video) {
}
void onClick(Qt::MouseButton button) const;
};
class VideoCancelLink : public VideoLink {
TEXT_LINK_CLASS(VideoCancelLink)
2015-04-04 20:01:34 +00:00
public:
VideoCancelLink(VideoData *video) : VideoLink(video) {
}
void onClick(Qt::MouseButton button) const;
};
struct AudioData {
AudioData(const AudioId &id, const uint64 &access = 0, int32 date = 0, const QString &mime = QString(), int32 duration = 0, int32 dc = 0, int32 size = 0);
2015-04-04 20:01:34 +00:00
void forget() {
}
void save(const QString &toFile);
void cancel(bool beforeDownload = false) {
mtpFileLoader *l = loader;
loader = 0;
if (l) {
l->cancel();
l->deleteLater();
l->rpcInvalidate();
}
location = FileLocation();
if (!beforeDownload) {
openOnSave = 0;
openOnSaveMsgId = FullMsgId();
2015-04-04 20:01:34 +00:00
}
}
void finish() {
if (loader->done()) {
2015-05-19 15:46:45 +00:00
location = FileLocation(mtpToStorageType(loader->fileType()), loader->fileName());
2015-04-04 20:01:34 +00:00
data = loader->bytes();
}
loader->deleteLater();
loader->rpcInvalidate();
loader = 0;
}
QString already(bool check = false);
AudioId id;
uint64 access;
int32 date;
QString mime;
int32 duration;
int32 dc;
int32 size;
FileStatus status;
int32 uploadOffset;
int32 openOnSave;
FullMsgId openOnSaveMsgId;
2015-04-04 20:01:34 +00:00
mtpFileLoader *loader;
FileLocation location;
QByteArray data;
int32 md5[8];
};
struct AudioMsgId {
AudioMsgId() : audio(0) {
}
AudioMsgId(AudioData *audio, const FullMsgId &msgId) : audio(audio), msgId(msgId) {
}
AudioMsgId(AudioData *audio, ChannelId channelId, MsgId msgId) : audio(audio), msgId(channelId, msgId) {
}
operator bool() const {
return audio;
}
AudioData *audio;
FullMsgId msgId;
};
inline bool operator<(const AudioMsgId &a, const AudioMsgId &b) {
return quintptr(a.audio) < quintptr(b.audio) || (quintptr(a.audio) == quintptr(b.audio) && a.msgId < b.msgId);
}
inline bool operator==(const AudioMsgId &a, const AudioMsgId &b) {
return a.audio == b.audio && a.msgId == b.msgId;
}
inline bool operator!=(const AudioMsgId &a, const AudioMsgId &b) {
return !(a == b);
}
2015-04-04 20:01:34 +00:00
class AudioLink : public ITextLink {
TEXT_LINK_CLASS(AudioLink)
2015-04-04 20:01:34 +00:00
public:
AudioLink(AudioData *audio) : _audio(audio) {
}
AudioData *audio() const {
return _audio;
}
private:
AudioData *_audio;
};
class AudioSaveLink : public AudioLink {
TEXT_LINK_CLASS(AudioSaveLink)
2015-04-04 20:01:34 +00:00
public:
AudioSaveLink(AudioData *audio) : AudioLink(audio) {
}
2015-04-19 10:29:19 +00:00
static void doSave(AudioData *audio, bool forceSavingAs = false);
2015-04-04 20:01:34 +00:00
void onClick(Qt::MouseButton button) const;
};
class AudioOpenLink : public AudioLink {
TEXT_LINK_CLASS(AudioOpenLink)
2015-04-04 20:01:34 +00:00
public:
AudioOpenLink(AudioData *audio) : AudioLink(audio) {
}
void onClick(Qt::MouseButton button) const;
};
class AudioCancelLink : public AudioLink {
TEXT_LINK_CLASS(AudioCancelLink)
2015-04-04 20:01:34 +00:00
public:
AudioCancelLink(AudioData *audio) : AudioLink(audio) {
}
void onClick(Qt::MouseButton button) const;
};
enum DocumentType {
FileDocument = 0,
VideoDocument = 1,
SongDocument = 2,
StickerDocument = 3,
AnimatedDocument = 4,
};
struct DocumentAdditionalData {
};
struct StickerData : public DocumentAdditionalData {
StickerData() : set(MTP_inputStickerSetEmpty()) {
}
ImagePtr img;
QString alt;
MTPInputStickerSet set;
bool setInstalled() const;
2015-05-19 15:46:45 +00:00
StorageImageLocation loc; // doc thumb location
};
struct SongData : public DocumentAdditionalData {
SongData() : duration(0) {
}
int32 duration;
QString title, performer;
2015-04-04 20:01:34 +00:00
};
2015-04-04 20:01:34 +00:00
struct DocumentData {
DocumentData(const DocumentId &id, const uint64 &access = 0, int32 date = 0, const QVector<MTPDocumentAttribute> &attributes = QVector<MTPDocumentAttribute>(), const QString &mime = QString(), const ImagePtr &thumb = ImagePtr(), int32 dc = 0, int32 size = 0);
void setattributes(const QVector<MTPDocumentAttribute> &attributes);
void forget() {
thumb->forget();
if (sticker()) sticker()->img->forget();
2015-04-04 20:01:34 +00:00
replyPreview->forget();
}
ImagePtr makeReplyPreview() {
if (replyPreview->isNull() && !thumb->isNull()) {
if (thumb->loaded()) {
int w = thumb->width(), h = thumb->height();
if (w <= 0) w = 1;
if (h <= 0) h = 1;
replyPreview = ImagePtr(w > h ? thumb->pix(w * st::msgReplyBarSize.height() / h, st::msgReplyBarSize.height()) : thumb->pix(st::msgReplyBarSize.height()), "PNG");
} else {
thumb->load();
}
}
return replyPreview;
}
2015-04-04 20:01:34 +00:00
void save(const QString &toFile);
void cancel(bool beforeDownload = false) {
mtpFileLoader *l = loader;
loader = 0;
if (l) {
l->cancel();
l->deleteLater();
l->rpcInvalidate();
}
location = FileLocation();
if (!beforeDownload) {
openOnSave = 0;
openOnSaveMsgId = FullMsgId();
2015-04-04 20:01:34 +00:00
}
}
void finish() {
if (loader->done()) {
2015-05-19 15:46:45 +00:00
location = FileLocation(mtpToStorageType(loader->fileType()), loader->fileName());
2015-04-04 20:01:34 +00:00
data = loader->bytes();
2015-10-01 14:05:05 +00:00
if (sticker() && !loader->imagePixmap().isNull()) {
sticker()->img = ImagePtr(data, loader->imageFormat(), loader->imagePixmap());
}
2015-04-04 20:01:34 +00:00
}
loader->deleteLater();
loader->rpcInvalidate();
loader = 0;
}
~DocumentData() {
delete _additional;
}
2015-04-04 20:01:34 +00:00
QString already(bool check = false);
StickerData *sticker() {
return (type == StickerDocument) ? static_cast<StickerData*>(_additional) : 0;
}
SongData *song() {
return (type == SongDocument) ? static_cast<SongData*>(_additional) : 0;
}
2015-04-04 20:01:34 +00:00
DocumentId id;
DocumentType type;
QSize dimensions;
uint64 access;
int32 date;
QString name, mime;
2015-04-04 20:01:34 +00:00
ImagePtr thumb, replyPreview;
int32 dc;
int32 size;
FileStatus status;
int32 uploadOffset;
int32 openOnSave;
FullMsgId openOnSaveMsgId;
2015-04-04 20:01:34 +00:00
mtpFileLoader *loader;
FileLocation location;
QByteArray data;
DocumentAdditionalData *_additional;
2015-04-04 20:01:34 +00:00
int32 md5[8];
};
struct SongMsgId {
SongMsgId() : song(0) {
}
SongMsgId(DocumentData *song, const FullMsgId &msgId) : song(song), msgId(msgId) {
}
SongMsgId(DocumentData *song, ChannelId channelId, MsgId msgId) : song(song), msgId(channelId, msgId) {
}
operator bool() const {
return song;
}
DocumentData *song;
FullMsgId msgId;
};
inline bool operator<(const SongMsgId &a, const SongMsgId &b) {
return quintptr(a.song) < quintptr(b.song) || (quintptr(a.song) == quintptr(b.song) && a.msgId < b.msgId);
}
inline bool operator==(const SongMsgId &a, const SongMsgId &b) {
return a.song == b.song && a.msgId == b.msgId;
}
inline bool operator!=(const SongMsgId &a, const SongMsgId &b) {
return !(a == b);
}
2015-04-04 20:01:34 +00:00
class DocumentLink : public ITextLink {
TEXT_LINK_CLASS(DocumentLink)
2015-04-04 20:01:34 +00:00
public:
DocumentLink(DocumentData *document) : _document(document) {
}
DocumentData *document() const {
return _document;
}
private:
DocumentData *_document;
};
class DocumentSaveLink : public DocumentLink {
TEXT_LINK_CLASS(DocumentSaveLink)
2015-04-04 20:01:34 +00:00
public:
DocumentSaveLink(DocumentData *document) : DocumentLink(document) {
}
2015-04-19 10:29:19 +00:00
static void doSave(DocumentData *document, bool forceSavingAs = false);
2015-04-04 20:01:34 +00:00
void onClick(Qt::MouseButton button) const;
};
class DocumentOpenLink : public DocumentLink {
TEXT_LINK_CLASS(DocumentOpenLink)
2015-04-04 20:01:34 +00:00
public:
DocumentOpenLink(DocumentData *document) : DocumentLink(document) {
}
2015-07-03 08:47:16 +00:00
static void doOpen(DocumentData *document);
2015-04-04 20:01:34 +00:00
void onClick(Qt::MouseButton button) const;
};
class DocumentCancelLink : public DocumentLink {
TEXT_LINK_CLASS(DocumentCancelLink)
2015-04-04 20:01:34 +00:00
public:
DocumentCancelLink(DocumentData *document) : DocumentLink(document) {
}
void onClick(Qt::MouseButton button) const;
};
enum WebPageType {
WebPagePhoto,
WebPageVideo,
WebPageProfile,
WebPageArticle
};
inline WebPageType toWebPageType(const QString &type) {
if (type == qstr("photo")) return WebPagePhoto;
if (type == qstr("video")) return WebPageVideo;
if (type == qstr("profile")) return WebPageProfile;
2015-04-04 20:01:34 +00:00
return WebPageArticle;
}
struct WebPageData {
WebPageData(const WebPageId &id, WebPageType type = WebPageArticle, const QString &url = QString(), const QString &displayUrl = QString(), const QString &siteName = QString(), const QString &title = QString(), const QString &description = QString(), PhotoData *photo = 0, DocumentData *doc = 0, int32 duration = 0, const QString &author = QString(), int32 pendingTill = -1);
2015-04-04 20:01:34 +00:00
void forget() {
if (photo) photo->forget();
}
WebPageId id;
WebPageType type;
QString url, displayUrl, siteName, title, description;
int32 duration;
QString author;
PhotoData *photo;
DocumentData *doc;
2015-04-04 20:01:34 +00:00
int32 pendingTill;
};
2015-04-19 10:29:19 +00:00
QString saveFileName(const QString &title, const QString &filter, const QString &prefix, QString name, bool savingAs, const QDir &dir = QDir());
2015-04-04 20:01:34 +00:00
MsgId clientMsgId();
struct MessageCursor {
MessageCursor() : position(0), anchor(0), scroll(QFIXED_MAX) {
}
MessageCursor(int position, int anchor, int scroll) : position(position), anchor(anchor), scroll(scroll) {
}
MessageCursor(const QTextEdit &edit) {
fillFrom(edit);
}
void fillFrom(const QTextEdit &edit) {
QTextCursor c = edit.textCursor();
position = c.position();
anchor = c.anchor();
QScrollBar *s = edit.verticalScrollBar();
scroll = s ? s->value() : QFIXED_MAX;
}
void applyTo(QTextEdit &edit, bool *lock = 0) {
if (lock) *lock = true;
QTextCursor c = edit.textCursor();
c.setPosition(anchor, QTextCursor::MoveAnchor);
c.setPosition(position, QTextCursor::KeepAnchor);
edit.setTextCursor(c);
QScrollBar *s = edit.verticalScrollBar();
if (s) s->setValue(scroll);
if (lock) *lock = false;
}
int position, anchor, scroll;
};