2
0
mirror of https://github.com/kotatogram/kotatogram-desktop synced 2025-08-31 06:35:14 +00:00

stickers emoji tab done, local cache for stickers, recent stickers and voice messages

This commit is contained in:
John Preston
2015-01-02 17:55:24 +03:00
parent 59381b8ad2
commit 091bba0fc5
41 changed files with 5178 additions and 3470 deletions

View File

@@ -1233,11 +1233,11 @@ MessageField::MessageField(HistoryWidget *history, const style::flatTextarea &st
}
void MessageField::onChange() {
int newh = ceil(document()->size().height()) + 2 * fakeMargin();
int newh = ceil(document()->size().height()) + 2 * fakeMargin(), minh = st::btnSend.height - 2 * st::sendPadding;
if (newh > st::maxFieldHeight) {
newh = st::maxFieldHeight;
} else if (newh < st::minFieldHeight) {
newh = st::minFieldHeight;
} else if (newh < minh) {
newh = minh;
}
if (height() != newh) {
@@ -1533,41 +1533,44 @@ HistoryHider::~HistoryHider() {
}
HistoryWidget::HistoryWidget(QWidget *parent) : QWidget(parent)
, histRequestsCount(0)
, histPeer(0)
, _activeHist(0)
, histPreloading(0)
, _loadingAroundId(-1)
, _loadingAroundRequest(0)
, _scroll(this, st::historyScroll, false)
, _list(0)
, hist(0)
, _histInited(false)
, _toHistoryEnd(this, st::historyToEnd)
, _send(this, lang(lng_send_button), st::btnSend)
, _attachDocument(this, st::btnAttachDocument)
, _attachPhoto(this, st::btnAttachPhoto)
, _attachEmoji(this, st::btnAttachEmoji)
, _field(this, st::taMsgField, lang(lng_message_ph))
, _attachType(this)
, _emojiPan(this)
, _attachDrag(DragStateNone)
, _attachDragDocument(this)
, _attachDragPhoto(this)
, imageLoader(this)
, _synthedTextUpdate(false)
, loadingChatId(0)
, loadingRequestId(0)
, serviceImageCacheSize(0)
, confirmImageId(0)
, confirmWithText(false)
, titlePeerTextWidth(0)
, bg(st::msgBG)
, hiderOffered(false)
, _scrollDelta(0)
, _typingRequest(0)
, _saveDraftStart(0)
, _saveDraftText(false) {
, _lastStickersUpdate(0)
, _stickersUpdateRequest(0)
, histRequestsCount(0)
, histPeer(0)
, _activeHist(0)
, histPreloading(0)
, _loadingAroundId(-1)
, _loadingAroundRequest(0)
, _scroll(this, st::historyScroll, false)
, _list(0)
, hist(0)
, _histInited(false)
, _toHistoryEnd(this, st::historyToEnd)
, _send(this, lang(lng_send_button), st::btnSend)
, _attachDocument(this, st::btnAttachDocument)
, _attachPhoto(this, st::btnAttachPhoto)
, _attachEmoji(this, st::btnAttachEmoji)
, _field(this, st::taMsgField, lang(lng_message_ph))
, _attachType(this)
, _emojiPan(this)
//, _stickerPan(this)
, _attachDrag(DragStateNone)
, _attachDragDocument(this)
, _attachDragPhoto(this)
, imageLoader(this)
, _synthedTextUpdate(false)
, loadingChatId(0)
, loadingRequestId(0)
, serviceImageCacheSize(0)
, confirmImageId(0)
, confirmWithText(false)
, titlePeerTextWidth(0)
, bg(st::msgBG)
, hiderOffered(false)
, _scrollDelta(0)
, _typingRequest(0)
, _saveDraftStart(0)
, _saveDraftText(false) {
_scroll.setFocusPolicy(Qt::NoFocus);
setAcceptDrops(true);
@@ -1588,6 +1591,9 @@ HistoryWidget::HistoryWidget(QWidget *parent) : QWidget(parent)
connect(App::wnd()->windowHandle(), SIGNAL(visibleChanged(bool)), this, SLOT(onVisibleChanged()));
connect(&_scrollTimer, SIGNAL(timeout()), this, SLOT(onScrollTimer()));
connect(&_emojiPan, SIGNAL(emojiSelected(EmojiPtr)), &_field, SLOT(onEmojiInsert(EmojiPtr)));
connect(&_emojiPan, SIGNAL(stickerSelected(DocumentData*)), this, SLOT(onStickerSend(DocumentData*)));
connect(&_emojiPan, SIGNAL(updateStickers()), this, SLOT(updateStickers()));
// connect(&_stickerPan, SIGNAL(stickerSelected(DocumentData*)), this, SLOT(onStickerSend(DocumentData*)));
connect(&_typingStopTimer, SIGNAL(timeout()), this, SLOT(cancelTyping()));
_scrollTimer.setSingleShot(false);
@@ -1618,11 +1624,15 @@ HistoryWidget::HistoryWidget(QWidget *parent) : QWidget(parent)
_attachDocument.installEventFilter(&_attachType);
_attachPhoto.installEventFilter(&_attachType);
_attachEmoji.installEventFilter(&_emojiPan);
// _attachEmoji.installEventFilter(&_stickerPan);
// _emojiPan.installEventFilter(&_stickerPan);
// _stickerPan.installEventFilter(&_emojiPan);
connect(_attachType.addButton(new IconedButton(this, st::dropdownAttachDocument, lang(lng_attach_file))), SIGNAL(clicked()), this, SLOT(onDocumentSelect()));
connect(_attachType.addButton(new IconedButton(this, st::dropdownAttachPhoto, lang(lng_attach_photo))), SIGNAL(clicked()), this, SLOT(onPhotoSelect()));
_attachType.hide();
_emojiPan.hide();
// _stickerPan.hide();
_attachDragDocument.hide();
_attachDragPhoto.hide();
@@ -1632,6 +1642,7 @@ HistoryWidget::HistoryWidget(QWidget *parent) : QWidget(parent)
void HistoryWidget::onTextChange() {
updateTyping();
// updateStickerPan();
if (!hist || _synthedTextUpdate) return;
_saveDraftText = true;
@@ -1639,6 +1650,8 @@ void HistoryWidget::onTextChange() {
}
void HistoryWidget::onDraftSaveDelayed() {
// updateStickerPan();
if (!hist || _synthedTextUpdate) return;
if (!_field.textCursor().anchor() && !_field.textCursor().position() && !_field.verticalScrollBar()->value()) {
if (!Local::hasDraftPositions(hist->peer->id)) return;
@@ -1690,6 +1703,18 @@ void HistoryWidget::updateTyping(bool typing) {
}
}
//void HistoryWidget::updateStickerPan() {
// EmojiPtr e = _field.getSingleEmoji();
// if (e) updateStickers();
// _stickerPan.setStickerPack(e, !_emojiPan.isHidden() && !_emojiPan.hiding());
//}
void HistoryWidget::updateRecentStickers() {
if (cEmojiTab() == dbietStickers) {
_emojiPan.onTabChange();
}
}
void HistoryWidget::typingDone(const MTPBool &result, mtpRequestId req) {
if (_typingRequest == req) {
_typingRequest = 0;
@@ -1734,6 +1759,98 @@ void HistoryWidget::chatLoaded(const MTPmessages_ChatFull &res) {
peerUpdated(App::chat(peerId));
}
void HistoryWidget::updateStickers() {
if (_lastStickersUpdate && getms(true) < _lastStickersUpdate + StickersUpdateTimeout) return;
if (_stickersUpdateRequest) return;
_stickersUpdateRequest = MTP::send(MTPmessages_GetAllStickers(MTP_string(cStickersHash())), rpcDone(&HistoryWidget::stickersGot), rpcFail(&HistoryWidget::stickersFailed));
}
void HistoryWidget::stickersGot(const MTPmessages_AllStickers &stickers) {
_lastStickersUpdate = getms(true);
_stickersUpdateRequest = 0;
if (stickers.type() == mtpc_messages_allStickers) {
const MTPDmessages_allStickers &d(stickers.c_messages_allStickers());
AllStickers all;
const QVector<MTPDocument> &docs(d.vdocuments.c_vector().v);
const RecentStickerPack &recent(cRecentStickers());
RecentStickerPack add;
add.reserve(docs.size());
ushort addValue = recent.isEmpty() ? 1 : qAbs(recent.front().second);
for (int32 i = 0, l = docs.size(); i < l; ++i) {
DocumentData *doc = App::feedDocument(docs.at(i));
if (!doc) continue;
int32 j = 0, s = recent.size();
for (; j < s; ++j) {
if (doc == recent.at(j).first) break;
}
if (j < s) continue;
add.push_back(qMakePair(doc, addValue));
}
if (!add.isEmpty()) {
cSetRecentStickers(add + recent);
Local::writeRecentStickers();
_emojiPan.onTabChange();
}
const QVector<MTPStickerPack> &packs(d.vpacks.c_vector().v);
for (int32 i = 0, l = packs.size(); i < l; ++i) {
if (packs.at(i).type() == mtpc_stickerPack) {
const MTPDstickerPack &p(packs.at(i).c_stickerPack());
QString emoticon(qs(p.vemoticon));
EmojiPtr e = 0;
for (const QChar *ch = emoticon.constData(), *end = emoticon.constEnd(); ch != end; ++ch) {
if (ch->isHighSurrogate()) {
if (ch + 1 < end && (ch + 1)->isLowSurrogate()) {
e = getEmoji((ch->unicode() << 16) | (ch + 1)->unicode());
if (!e) {
++ch;
}
}
} else {
if (ch + 1 < end) {
if (((ch->unicode() >= 48 && ch->unicode() < 58) || ch->unicode() == 35) && (ch + 1)->unicode() == 0x20E3) {
e = getEmoji((ch->unicode() << 16) | (ch + 1)->unicode());
} else if ((ch + 1)->unicode() == 0xFE0F) {
e = getEmoji(ch->unicode());
}
}
}
if (e) break;
}
if (e) {
const QVector<MTPlong> docs(p.vdocuments.c_vector().v);
if (!docs.isEmpty()) {
StickerPack &pack(all[e]);
pack.reserve(pack.size() + docs.size());
for (int32 j = 0, s = docs.size(); j < s; ++j) {
pack.push_back(App::document(docs.at(j).v));
}
}
} else {
LOG(("Sticker Error: Could not find emoji for string: %1").arg(emoticon));
}
}
}
cSetStickers(all);
cSetStickersHash(qba(d.vhash));
// updateStickerPan();
_emojiPan.onTabChange();
}
}
bool HistoryWidget::stickersFailed(const RPCError &error) {
_lastStickersUpdate = getms(true);
_stickersUpdateRequest = 0;
return true;
}
void HistoryWidget::clearLoadingAround() {
_loadingAroundId = -1;
if (_loadingAroundRequest) {
@@ -1928,6 +2045,7 @@ void HistoryWidget::updateControlsVisibility() {
_attachEmoji.hide();
_attachType.hide();
_emojiPan.hide();
// _stickerPan.hide();
return;
}
@@ -1957,6 +2075,7 @@ void HistoryWidget::updateControlsVisibility() {
_attachEmoji.hide();
_attachType.hide();
_emojiPan.hide();
// _stickerPan.hide();
if (!_field.isHidden()) {
_field.hide();
resizeEvent(0);
@@ -1977,6 +2096,7 @@ void HistoryWidget::updateControlsVisibility() {
_attachEmoji.hide();
_attachType.hide();
_emojiPan.hide();
// _stickerPan.hide();
_toHistoryEnd.hide();
if (!_field.isHidden()) {
_field.hide();
@@ -2301,6 +2421,7 @@ void HistoryWidget::onSend(bool ctrlShiftEnter) {
if (!_attachType.isHidden()) _attachType.hideStart();
if (!_emojiPan.isHidden()) _emojiPan.hideStart();
// if (!_stickerPan.isHidden()) _stickerPan.hideStart();
}
_field.setFocus();
@@ -2991,13 +3112,17 @@ void HistoryWidget::onDocumentUploaded(MsgId newId, const MTPInputFile &file) {
if (!MTP::authedId()) return;
HistoryMessage *item = dynamic_cast<HistoryMessage*>(App::histItemById(newId));
if (item) {
HistoryDocument *media = dynamic_cast<HistoryDocument*>(item->getMedia());
if (media) {
DocumentData *document = 0;
if (HistoryDocument *media = dynamic_cast<HistoryDocument*>(item->getMedia())) {
document = media->document();
} else if (HistorySticker *media = dynamic_cast<HistorySticker*>(item->getMedia())) {
document = media->document();
}
if (document) {
//App::main()->readServerHistory(item->history(), false);
uint64 randomId = MTP::nonce<uint64>();
App::historyRegRandom(randomId, newId);
DocumentData *document = media->document();
History *hist = item->history();
hist->sendRequestId = MTP::send(MTPmessages_SendMedia(item->history()->peer->input, MTP_inputMediaUploadedDocument(file, MTP_string(document->mime), _composeDocumentAttributes(document)), MTP_long(randomId)), App::main()->rpcDone(&MainWidget::sentFullDataReceived, randomId), RPCFailHandlerPtr(), 0, 0, hist->sendRequestId);
}
@@ -3008,13 +3133,17 @@ void HistoryWidget::onThumbDocumentUploaded(MsgId newId, const MTPInputFile &fil
if (!MTP::authedId()) return;
HistoryMessage *item = dynamic_cast<HistoryMessage*>(App::histItemById(newId));
if (item) {
HistoryDocument *media = dynamic_cast<HistoryDocument*>(item->getMedia());
if (media) {
DocumentData *document = 0;
if (HistoryDocument *media = dynamic_cast<HistoryDocument*>(item->getMedia())) {
document = media->document();
} else if (HistorySticker *media = dynamic_cast<HistorySticker*>(item->getMedia())) {
document = media->document();
}
if (document) {
//App::main()->readServerHistory(item->history(), false);
uint64 randomId = MTP::nonce<uint64>();
App::historyRegRandom(randomId, newId);
DocumentData *document = media->document();
History *hist = item->history();
hist->sendRequestId = MTP::send(MTPmessages_SendMedia(item->history()->peer->input, MTP_inputMediaUploadedThumbDocument(file, thumb, MTP_string(document->mime), _composeDocumentAttributes(document)), MTP_long(randomId)), App::main()->rpcDone(&MainWidget::sentFullDataReceived, randomId), RPCFailHandlerPtr(), 0, 0, hist->sendRequestId);
}
@@ -3070,6 +3199,7 @@ void HistoryWidget::resizeEvent(QResizeEvent *e) {
_attachType.move(0, _attachDocument.y() - _attachType.height());
_emojiPan.move(width() - _emojiPan.width(), _attachEmoji.y() - _emojiPan.height());
// _stickerPan.move(width() - _emojiPan.width() - _stickerPan.width() + st::dropdownPadding.left(), _attachEmoji.y() - _stickerPan.height());
switch (_attachDrag) {
case DragStateFiles:
@@ -3278,6 +3408,35 @@ void HistoryWidget::onFieldTabbed() {
}
}
void HistoryWidget::onStickerSend(DocumentData *sticker) {
if (!hist || !sticker) return;
App::main()->readServerHistory(hist, false);
uint64 randomId = MTP::nonce<uint64>();
MsgId newId = clientMsgId();
hist->loadAround(0);
bool out = (histPeer->input.type() != mtpc_inputPeerSelf), unread = (histPeer->input.type() != mtpc_inputPeerSelf);
hist->addToBackDocument(newId, out, unread, date(MTP_int(unixtime())), MTP::authedId(), sticker);
hist->sendRequestId = MTP::send(MTPmessages_SendMedia(histPeer->input, MTP_inputMediaDocument(MTP_inputDocument(MTP_long(sticker->id), MTP_long(sticker->access))), MTP_long(randomId)), App::main()->rpcDone(&MainWidget::sentFullDataReceived, randomId), RPCFailHandlerPtr(), 0, 0, hist->sendRequestId);
App::historyRegRandom(randomId, newId);
App::main()->historyToDown(hist);
App::main()->dialogsToUp();
peerMessagesUpdated(histPeer->id);
if (!_attachType.isHidden()) _attachType.hideStart();
if (!_emojiPan.isHidden()) _emojiPan.hideStart();
// if (!_stickerPan.isHidden()) _stickerPan.hideStart();
// _field.removeSingleEmoji();
_field.setFocus();
}
void HistoryWidget::setFieldText(const QString &text) {
_synthedTextUpdate = true;
_field.setPlainText(text);