Skip to content

Commit

Permalink
stop and clear audio on logout, clearing bot keyboards in supergroups
Browse files Browse the repository at this point in the history
  • Loading branch information
john-preston committed Nov 24, 2015
1 parent c80adfc commit 87b57a2
Show file tree
Hide file tree
Showing 14 changed files with 154 additions and 28 deletions.
10 changes: 10 additions & 0 deletions Telegram/SourceFiles/apiwrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,9 @@ void ApiWrap::lastParticipantsDone(ChannelData *peer, const MTPchannels_ChannelP

if (!peer->mgInfo || result.type() != mtpc_channels_channelParticipants) return;

History *h = 0;
if (bots) {
h = App::historyLoaded(peer->id);
peer->mgInfo->bots.clear();
peer->mgInfo->botStatus = -1;
} else if (fromStart) {
Expand All @@ -536,6 +538,7 @@ void ApiWrap::lastParticipantsDone(ChannelData *peer, const MTPchannels_ChannelP
App::feedUsers(d.vusers);
bool added = false, needBotsInfos = false;
int32 botStatus = peer->mgInfo->botStatus;
bool keyboardBotFound = !h || !h->lastKeyboardFrom;
for (QVector<MTPChannelParticipant>::const_iterator i = v.cbegin(), e = v.cend(); i != e; ++i) {
int32 userId = 0;
bool admin = false;
Expand All @@ -557,6 +560,9 @@ void ApiWrap::lastParticipantsDone(ChannelData *peer, const MTPchannels_ChannelP
needBotsInfos = true;
}
}
if (!keyboardBotFound && u->id == h->lastKeyboardFrom) {
keyboardBotFound = true;
}
} else {
if (peer->mgInfo->lastParticipants.indexOf(u) < 0) {
peer->mgInfo->lastParticipants.push_back(u);
Expand All @@ -574,6 +580,10 @@ void ApiWrap::lastParticipantsDone(ChannelData *peer, const MTPchannels_ChannelP
if (needBotsInfos) {
requestFullPeer(peer);
}
if (!keyboardBotFound) {
h->clearLastKeyboard();
if (App::main()) App::main()->updateBotKeyboard(h);
}
if (d.vcount.v > peer->count) {
peer->count = d.vcount.v;
} else if (v.count() > peer->count) {
Expand Down
13 changes: 7 additions & 6 deletions Telegram/SourceFiles/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ namespace App {
if (cHasPasscode()) {
cSetHasPasscode(false);
}
if (audioPlayer()) {
audioPlayer()->stopAndClear();
}
if (w) {
w->tempDirDelete(Local::ClearManagerAll);
w->notifyClearFast();
Expand Down Expand Up @@ -696,9 +699,8 @@ namespace App {
}
chat->botStatus = botStatus;
if (!found) {
h->lastKeyboardId = 0;
h->lastKeyboardFrom = 0;
if (App::main()) App::main()->updateBotKeyboard();
h->clearLastKeyboard();
if (App::main()) App::main()->updateBotKeyboard(h);
}
}
}
Expand Down Expand Up @@ -794,9 +796,8 @@ namespace App {

History *h = App::historyLoaded(chat->id);
if (h && h->lastKeyboardFrom == user->id) {
h->lastKeyboardId = 0;
h->lastKeyboardFrom = 0;
if (App::main()) App::main()->updateBotKeyboard();
h->clearLastKeyboard();
if (App::main()) App::main()->updateBotKeyboard(h);
}
}
if (chat->botStatus > 0 && user->botInfo) {
Expand Down
81 changes: 78 additions & 3 deletions Telegram/SourceFiles/audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,27 @@ void audioFinish() {
cSetHasAudioPlayer(false);
}

void AudioPlayer::Msg::clearData() {
fname = QString();
data = QByteArray();
position = duration = 0;
frequency = AudioVoiceMsgFrequency;
skipStart = skipEnd = 0;
loading = false;
started = 0;
state = AudioPlayerStopped;
if (alIsSource(source)) {
alSourceStop(source);
}
for (int32 i = 0; i < 3; ++i) {
if (samplesCount[i]) {
alSourceUnqueueBuffers(source, 1, buffers + i);
samplesCount[i] = 0;
}
}
nextBuffer = 0;
}

AudioPlayer::AudioPlayer() : _audioCurrent(0), _songCurrent(0),
_fader(new AudioPlayerFader(&_faderThread)),
_loader(new AudioPlayerLoaders(&_loaderThread)) {
Expand Down Expand Up @@ -642,10 +663,64 @@ void AudioPlayer::seek(int64 position) {
}

void AudioPlayer::stop(MediaOverviewType type) {
fadedStop(type);
switch (type) {
case OverviewAudios: if (_audioData[_audioCurrent].audio) emit updated(_audioData[_audioCurrent].audio); break;
case OverviewDocuments: if (_songData[_songCurrent].song) emit updated(_songData[_songCurrent].song); break;
case OverviewAudios: {
AudioMsgId current;
{
QMutexLocker lock(&playerMutex);
current = _audioData[_audioCurrent].audio;
fadedStop(type);
}
if (current) emit updated(current);
} break;

case OverviewDocuments: {
SongMsgId current;
{
QMutexLocker lock(&playerMutex);
current = _songData[_songCurrent].song;
fadedStop(type);
}
if (current) emit updated(current);
} break;
}
}

void AudioPlayer::stopAndClear() {
AudioMsg *current_audio = 0;
{
QMutexLocker lock(&playerMutex);
current_audio = &_audioData[_audioCurrent];
if (current_audio) {
setStoppedState(current_audio);
}
}
SongMsg *current_song = 0;
{
QMutexLocker lock(&playerMutex);
current_song = &_songData[_songCurrent];
if (current_song) {
setStoppedState(current_song);
}
}
if (current_song) {
emit updated(current_song->song);
}
if (current_audio) {
emit updated(current_audio->audio);
}
{
QMutexLocker lock(&playerMutex);
for (int32 index = 0; index < AudioVoiceMsgSimultaneously; ++index) {
if (_audioData[index].audio) {
emit loaderOnCancel(_audioData[index].audio);
}
_audioData[index].clear();
if (_songData[index].song) {
emit loaderOnCancel(_songData[index].song);
}
_songData[index].clear();
}
}
}

Expand Down
24 changes: 22 additions & 2 deletions Telegram/SourceFiles/audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class AudioPlayer : public QObject {
void seek(int64 position); // type == OverviewDocuments
void stop(MediaOverviewType type);

void stopAndClear();

void currentState(AudioMsgId *audio, AudioPlayerState *state = 0, int64 *position = 0, int64 *duration = 0, int32 *frequency = 0);
void currentState(SongMsgId *song, AudioPlayerState *state = 0, int64 *position = 0, int64 *duration = 0, int32 *frequency = 0);

Expand Down Expand Up @@ -109,12 +111,22 @@ public slots:
bool checkCurrentALError(MediaOverviewType type);

struct Msg {
Msg() : position(0), duration(0), frequency(AudioVoiceMsgFrequency), skipStart(0), skipEnd(0), loading(0), started(0),
state(AudioPlayerStopped), source(0), nextBuffer(0) {
Msg() : position(0)
, duration(0)
, frequency(AudioVoiceMsgFrequency)
, skipStart(0)
, skipEnd(0)
, loading(false)
, started(0)
, state(AudioPlayerStopped)
, source(0)
, nextBuffer(0) {
memset(buffers, 0, sizeof(buffers));
memset(samplesCount, 0, sizeof(samplesCount));
}

void clearData();

QString fname;
QByteArray data;
int64 position, duration;
Expand All @@ -132,11 +144,19 @@ public slots:
struct AudioMsg : public Msg {
AudioMsg() {
}
void clear() {
audio = AudioMsgId();
Msg::clearData();
}
AudioMsgId audio;
};
struct SongMsg : public Msg {
SongMsg() {
}
void clear() {
song = SongMsgId();
Msg::clearData();
}
SongMsgId song;
};

Expand Down
10 changes: 7 additions & 3 deletions Telegram/SourceFiles/boxes/contactsbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,10 +388,12 @@ ContactsInner::ContactData *ContactsInner::contactData(DialogRow *row) {
} else {
data->inchat = false;
}
data->onlineColor = false;
data->check = _checkedContacts.contains(peer);
data->name.setText(st::contactsNameFont, peer->name, _textNameOptions);
if (peer->isUser()) {
data->online = App::onlineText(peer->asUser(), _time);
data->onlineColor = App::onlineColorUse(peer->asUser(), _time);
} else if (peer->isChat()) {
ChatData *chat = peer->asChat();
if (!chat->amIn()) {
Expand Down Expand Up @@ -471,7 +473,7 @@ void ContactsInner::paintDialog(Painter &p, PeerData *peer, ContactData *data, b
} else {
if (inverse) {
p.setPen(st::white);
} else if ((user && (uname || App::onlineColorUse(user, _time))) || (peer->isChannel() && uname)) {
} else if ((user && (uname || data->onlineColor)) || (peer->isChannel() && uname)) {
p.setPen(st::contactsStatusFgOnline);
} else {
p.setPen(sel ? st::contactsStatusFgOver : st::contactsStatusFg);
Expand Down Expand Up @@ -1907,7 +1909,7 @@ void MembersInner::paintDialog(Painter &p, PeerData *peer, MemberData *data, boo
}

p.setFont(st::contactsStatusFont->f);
p.setPen(sel ? st::contactsStatusFgOver : st::contactsStatusFg);
p.setPen(sel ? st::contactsStatusFgOver : (data->onlineColor ? st::contactsStatusFgOnline : st::contactsStatusFg));
p.drawTextLeft(namex, st::contactsPadding.top() + st::contactsStatusTop, width(), data->online);
}

Expand Down Expand Up @@ -2028,7 +2030,9 @@ MembersInner::MemberData *MembersInner::data(int32 index) {
}
MemberData *result = _datas[index] = new MemberData();
result->name.setText(st::contactsNameFont, _rows[index]->name, _textNameOptions);
result->online = lng_mediaview_date_time(lt_date, _dates[index].date().toString(qsl("dd.MM.yy")), lt_time, _dates[index].time().toString(cTimeFormat()));
int32 t = unixtime();
result->online = App::onlineText(_rows[index], t);// lng_mediaview_date_time(lt_date, _dates[index].date().toString(qsl("dd.MM.yy")), lt_time, _dates[index].time().toString(cTimeFormat()));
result->onlineColor = App::onlineColorUse(_rows[index], t);
if (_filter == MembersFilterRecent) {
result->canKick = (_channel->amCreator() || _channel->amEditor() || _channel->amModerator()) ? (_roles[index] == MemberRoleNone) : false;
} else if (_filter == MembersFilterAdmins) {
Expand Down
2 changes: 2 additions & 0 deletions Telegram/SourceFiles/boxes/contactsbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ public slots:
struct ContactData {
Text name;
QString online;
bool onlineColor;
bool inchat;
bool check;
};
Expand Down Expand Up @@ -370,6 +371,7 @@ public slots:
struct MemberData {
Text name;
QString online;
bool onlineColor;
bool canKick;
};

Expand Down
18 changes: 11 additions & 7 deletions Telegram/SourceFiles/history.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,10 +386,14 @@ History::History(const PeerId &peerId) : width(0), height(0)
}

void History::clearLastKeyboard() {
if (lastKeyboardId) {
if (lastKeyboardId == lastKeyboardHiddenId) {
lastKeyboardHiddenId = 0;
}
lastKeyboardId = 0;
}
lastKeyboardInited = true;
lastKeyboardId = 0;
lastKeyboardFrom = 0;
lastKeyboardHiddenId = 0;
}

bool History::updateTyping(uint64 ms, uint32 dots, bool force) {
Expand Down Expand Up @@ -1545,6 +1549,7 @@ HistoryItem *History::createItem(HistoryBlock *block, const MTPMessage &msg, boo
PeerId uid = peerFromUser(d.vuser_id);
if (lastKeyboardFrom == uid) {
clearLastKeyboard();
if (App::main()) App::main()->updateBotKeyboard(this);
}
if (peer->isMegagroup()) {
if (UserData *user = App::userLoaded(uid)) {
Expand Down Expand Up @@ -1831,7 +1836,7 @@ HistoryItem *History::addNewItem(HistoryBlock *to, bool newBlock, HistoryItem *a
if (peer->isChat()) {
botNotInChat = adding->from()->isUser() && (!peer->canWrite() || !peer->asChat()->participants.isEmpty()) && !peer->asChat()->participants.contains(adding->from()->asUser());
} else if (peer->isMegagroup()) {
botNotInChat = adding->from()->isUser() && (!peer->canWrite() || !peer->asChannel()->mgInfo->bots.isEmpty()) && !peer->asChannel()->mgInfo->bots.contains(adding->from()->asUser());
botNotInChat = adding->from()->isUser() && (!peer->canWrite() || peer->asChannel()->mgInfo->botStatus != 0) && !peer->asChannel()->mgInfo->bots.contains(adding->from()->asUser());
}
if (botNotInChat) {
clearLastKeyboard();
Expand Down Expand Up @@ -2061,7 +2066,7 @@ void History::addOlderSlice(const QVector<MTPMessage> &slice, const QVector<MTPM
if (peer->isChat()) {
botNotInChat = (!peer->canWrite() || !peer->asChat()->participants.isEmpty()) && item->from()->isUser() && !peer->asChat()->participants.contains(item->from()->asUser());
} else if (peer->isMegagroup()) {
botNotInChat = (!peer->canWrite() || !peer->asChannel()->mgInfo->bots.isEmpty()) && item->from()->isUser() && !peer->asChannel()->mgInfo->bots.contains(item->from()->asUser());
botNotInChat = (!peer->canWrite() || peer->asChannel()->mgInfo->botStatus != 0) && item->from()->isUser() && !peer->asChannel()->mgInfo->bots.contains(item->from()->asUser());
}
if (wasKeyboardHide || botNotInChat) {
clearLastKeyboard();
Expand Down Expand Up @@ -3021,9 +3026,8 @@ void HistoryItem::destroy() {
history()->fixLastMessage(wasAtBottom);
}
if (history()->lastKeyboardId == id) {
history()->lastKeyboardId = 0;
history()->lastKeyboardFrom = 0;
if (App::main()) App::main()->updateBotKeyboard();
history()->clearLastKeyboard();
if (App::main()) App::main()->updateBotKeyboard(history());
}
HistoryMedia *m = getMedia(true);
MediaOverviewType t = m ? mediaToOverviewType(m->type()) : OverviewCount;
Expand Down
6 changes: 5 additions & 1 deletion Telegram/SourceFiles/historywidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5871,7 +5871,11 @@ void HistoryWidget::countHistoryShowFrom() {
_history->updateShowFrom();
}

void HistoryWidget::updateBotKeyboard() {
void HistoryWidget::updateBotKeyboard(History *h) {
if (h && h != _history && h != _migrated) {
return;
}

bool changed = false;
bool wasVisible = _kbShown || _kbReplyTo;
if ((_replyToId && !_replyTo) || !_history) {
Expand Down
2 changes: 1 addition & 1 deletion Telegram/SourceFiles/historywidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ class HistoryWidget : public TWidget, public RPCSender {
void insertBotCommand(const QString &cmd);

bool eventFilter(QObject *obj, QEvent *e);
void updateBotKeyboard();
void updateBotKeyboard(History *h = 0);

DragState getDragState(const QMimeData *d);

Expand Down
6 changes: 6 additions & 0 deletions Telegram/SourceFiles/localstorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2021,14 +2021,20 @@ namespace Local {
if (_localLoader) {
_localLoader->stop();
}

_passKeySalt.clear(); // reset passcode, local key
_draftsMap.clear();
_draftsPositionsMap.clear();
_fileLocations.clear();
_fileLocationPairs.clear();
_fileLocationAliases.clear();
_imagesMap.clear();
_draftsNotReadMap.clear();
_stickerImagesMap.clear();
_audiosMap.clear();
_storageImagesSize = _storageStickersSize = _storageAudiosSize = 0;
_locationsKey = _reportSpamStatusesKey = _recentStickersKeyOld = _stickersKey = _backgroundKey = _userSettingsKey = _recentHashtagsKey = _savedPeersKey = 0;
_oldMapVersion = _oldSettingsVersion = 0;
_mapChanged = true;
_writeMap(WriteMapNow);

Expand Down
4 changes: 2 additions & 2 deletions Telegram/SourceFiles/mainwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2090,8 +2090,8 @@ void MainWidget::updateReplyTo() {
history.updateReplyTo(true);
}

void MainWidget::updateBotKeyboard() {
history.updateBotKeyboard();
void MainWidget::updateBotKeyboard(History *h) {
history.updateBotKeyboard(h);
}

void MainWidget::pushReplyReturn(HistoryItem *item) {
Expand Down
2 changes: 1 addition & 1 deletion Telegram/SourceFiles/mainwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ class MainWidget : public TWidget, public RPCSender {

ApiWrap *api();
void updateReplyTo();
void updateBotKeyboard();
void updateBotKeyboard(History *h);

void pushReplyReturn(HistoryItem *item);

Expand Down
Loading

0 comments on commit 87b57a2

Please sign in to comment.