2017-10-31 18:25:22 +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.
|
|
|
|
|
|
|
|
In addition, as a special exception, the copyright holders give permission
|
|
|
|
to link the code of portions of this program with the OpenSSL library.
|
|
|
|
|
|
|
|
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
|
|
|
Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
|
|
|
|
*/
|
|
|
|
#include "info/info_controller.h"
|
|
|
|
|
2017-11-14 17:22:44 +00:00
|
|
|
#include <rpl/range.h>
|
|
|
|
#include <rpl/then.h>
|
2017-10-31 18:25:22 +00:00
|
|
|
#include "ui/search_field_controller.h"
|
2017-11-06 07:31:18 +00:00
|
|
|
#include "data/data_shared_media.h"
|
2017-10-31 18:25:22 +00:00
|
|
|
#include "info/info_content_widget.h"
|
|
|
|
#include "info/info_memento.h"
|
2017-11-03 15:47:08 +00:00
|
|
|
#include "info/media/info_media_widget.h"
|
2017-11-06 12:14:44 +00:00
|
|
|
#include "observer_peer.h"
|
|
|
|
#include "window/window_controller.h"
|
2017-10-31 18:25:22 +00:00
|
|
|
|
|
|
|
namespace Info {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
not_null<PeerData*> CorrectPeer(PeerId peerId) {
|
|
|
|
Expects(peerId != 0);
|
|
|
|
auto result = App::peer(peerId);
|
|
|
|
if (auto to = result->migrateTo()) {
|
|
|
|
return to;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2017-12-09 15:13:06 +00:00
|
|
|
rpl::producer<SparseIdsMergedSlice> AbstractController::mediaSource(
|
|
|
|
SparseIdsMergedSlice::UniversalMsgId aroundId,
|
|
|
|
int limitBefore,
|
|
|
|
int limitAfter) const {
|
|
|
|
return SharedMediaMergedViewer(
|
|
|
|
SharedMediaMergedKey(
|
|
|
|
SparseIdsMergedSlice::Key(
|
|
|
|
peerId(),
|
|
|
|
migratedPeerId(),
|
|
|
|
aroundId),
|
|
|
|
section().mediaType()),
|
|
|
|
limitBefore,
|
|
|
|
limitAfter);
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<QString> AbstractController::mediaSourceQueryValue() const {
|
|
|
|
return rpl::single(QString());
|
|
|
|
}
|
|
|
|
|
|
|
|
void AbstractController::showSection(
|
|
|
|
Window::SectionMemento &&memento,
|
|
|
|
const Window::SectionShow ¶ms) {
|
|
|
|
return parentController()->showSection(std::move(memento), params);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AbstractController::showBackFromStack(
|
|
|
|
const Window::SectionShow ¶ms) {
|
|
|
|
return parentController()->showBackFromStack(params);
|
|
|
|
}
|
|
|
|
|
2017-10-31 18:25:22 +00:00
|
|
|
Controller::Controller(
|
|
|
|
not_null<WrapWidget*> widget,
|
|
|
|
not_null<Window::Controller*> window,
|
|
|
|
not_null<ContentMemento*> memento)
|
2017-12-09 15:13:06 +00:00
|
|
|
: AbstractController(window)
|
|
|
|
, _widget(widget)
|
2017-10-31 18:25:22 +00:00
|
|
|
, _peer(App::peer(memento->peerId()))
|
|
|
|
, _migrated(memento->migratedPeerId()
|
|
|
|
? App::peer(memento->migratedPeerId())
|
|
|
|
: nullptr)
|
|
|
|
, _section(memento->section()) {
|
2017-11-03 15:47:08 +00:00
|
|
|
updateSearchControllers(memento);
|
2017-11-06 12:14:44 +00:00
|
|
|
setupMigrationViewer();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Controller::setupMigrationViewer() {
|
|
|
|
if (!_peer->isChat() && (!_peer->isChannel() || _migrated != nullptr)) {
|
|
|
|
return;
|
|
|
|
}
|
2017-12-22 07:05:20 +00:00
|
|
|
Notify::PeerUpdateValue(
|
|
|
|
_peer,
|
|
|
|
Notify::PeerUpdate::Flag::MigrationChanged
|
|
|
|
) | rpl::start_with_next([this] {
|
|
|
|
if (_peer->migrateTo() || (_peer->migrateFrom() != _migrated)) {
|
|
|
|
auto window = parentController();
|
|
|
|
auto peerId = _peer->id;
|
|
|
|
auto section = _section;
|
|
|
|
InvokeQueued(_widget, [=] {
|
|
|
|
window->showSection(
|
|
|
|
Memento(peerId, section),
|
|
|
|
Window::SectionShow(
|
|
|
|
Window::SectionShow::Way::Backward,
|
|
|
|
anim::type::instant,
|
|
|
|
anim::activation::background));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, lifetime());
|
2017-10-31 18:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Wrap Controller::wrap() const {
|
|
|
|
return _widget->wrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<Wrap> Controller::wrapValue() const {
|
|
|
|
return _widget->wrapValue();
|
2017-11-03 12:03:00 +00:00
|
|
|
}
|
|
|
|
|
2017-10-31 18:25:22 +00:00
|
|
|
bool Controller::validateMementoPeer(
|
|
|
|
not_null<ContentMemento*> memento) const {
|
|
|
|
return memento->peerId() == peerId()
|
|
|
|
&& memento->migratedPeerId() == migratedPeerId();
|
|
|
|
}
|
|
|
|
|
2017-11-03 15:47:08 +00:00
|
|
|
void Controller::setSection(not_null<ContentMemento*> memento) {
|
|
|
|
_section = memento->section();
|
|
|
|
updateSearchControllers(memento);
|
2017-10-31 18:25:22 +00:00
|
|
|
}
|
|
|
|
|
2017-11-03 15:47:08 +00:00
|
|
|
void Controller::updateSearchControllers(
|
|
|
|
not_null<ContentMemento*> memento) {
|
2017-11-17 13:23:36 +00:00
|
|
|
using Type = Section::Type;
|
|
|
|
auto type = _section.type();
|
|
|
|
auto isMedia = (type == Type::Media);
|
2017-10-31 18:25:22 +00:00
|
|
|
auto mediaType = isMedia
|
|
|
|
? _section.mediaType()
|
|
|
|
: Section::MediaType::kCount;
|
|
|
|
auto hasMediaSearch = isMedia
|
|
|
|
&& SharedMediaAllowSearch(mediaType);
|
2017-11-03 18:26:14 +00:00
|
|
|
auto hasCommonGroupsSearch
|
2017-11-17 13:23:36 +00:00
|
|
|
= (type == Type::CommonGroups);
|
|
|
|
auto hasMembersSearch
|
|
|
|
= (type == Type::Members || type == Type::Profile);
|
2017-11-03 15:47:08 +00:00
|
|
|
auto searchQuery = memento->searchFieldQuery();
|
2017-10-31 18:25:22 +00:00
|
|
|
if (isMedia) {
|
|
|
|
_searchController
|
|
|
|
= std::make_unique<Api::DelayedSearchController>();
|
2017-11-03 15:47:08 +00:00
|
|
|
auto mediaMemento = dynamic_cast<Media::Memento*>(memento.get());
|
|
|
|
Assert(mediaMemento != nullptr);
|
|
|
|
_searchController->restoreState(
|
|
|
|
mediaMemento->searchState());
|
2017-10-31 18:25:22 +00:00
|
|
|
} else {
|
|
|
|
_searchController = nullptr;
|
|
|
|
}
|
2017-11-17 13:23:36 +00:00
|
|
|
if (hasMediaSearch || hasCommonGroupsSearch || hasMembersSearch) {
|
2017-10-31 18:25:22 +00:00
|
|
|
_searchFieldController
|
2017-11-03 15:47:08 +00:00
|
|
|
= std::make_unique<Ui::SearchFieldController>(
|
|
|
|
searchQuery);
|
2017-11-03 18:26:14 +00:00
|
|
|
if (_searchController) {
|
2017-12-22 07:05:20 +00:00
|
|
|
_searchFieldController->queryValue(
|
|
|
|
) | rpl::start_with_next([=](QString &&query) {
|
|
|
|
_searchController->setQuery(
|
|
|
|
produceSearchQuery(std::move(query)));
|
|
|
|
}, _searchFieldController->lifetime());
|
2017-11-03 18:26:14 +00:00
|
|
|
}
|
|
|
|
_seachEnabledByContent = memento->searchEnabledByContent();
|
2017-12-08 08:14:30 +00:00
|
|
|
_searchStartsFocused = memento->searchStartsFocused();
|
2017-10-31 18:25:22 +00:00
|
|
|
} else {
|
|
|
|
_searchFieldController = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-03 15:47:08 +00:00
|
|
|
void Controller::saveSearchState(not_null<ContentMemento*> memento) {
|
|
|
|
if (_searchFieldController) {
|
|
|
|
memento->setSearchFieldQuery(
|
|
|
|
_searchFieldController->query());
|
2017-11-03 18:26:14 +00:00
|
|
|
memento->setSearchEnabledByContent(
|
|
|
|
_seachEnabledByContent.current());
|
2017-11-03 15:47:08 +00:00
|
|
|
}
|
|
|
|
if (_searchController) {
|
|
|
|
auto mediaMemento = dynamic_cast<Media::Memento*>(
|
|
|
|
memento.get());
|
|
|
|
Assert(mediaMemento != nullptr);
|
|
|
|
mediaMemento->setSearchState(_searchController->saveState());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-04 07:33:33 +00:00
|
|
|
void Controller::showSection(
|
|
|
|
Window::SectionMemento &&memento,
|
|
|
|
const Window::SectionShow ¶ms) {
|
|
|
|
if (!_widget->showInternal(&memento, params)) {
|
2017-12-09 15:13:06 +00:00
|
|
|
AbstractController::showSection(std::move(memento), params);
|
2017-12-04 07:33:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Controller::showBackFromStack(const Window::SectionShow ¶ms) {
|
|
|
|
if (!_widget->showBackFromStackInternal(params)) {
|
2017-12-09 15:13:06 +00:00
|
|
|
AbstractController::showBackFromStack(params);
|
2017-12-04 07:33:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-31 18:25:22 +00:00
|
|
|
auto Controller::produceSearchQuery(
|
2017-11-03 15:47:08 +00:00
|
|
|
const QString &query) const -> SearchQuery {
|
2017-10-31 18:25:22 +00:00
|
|
|
auto result = SearchQuery();
|
|
|
|
result.type = _section.mediaType();
|
|
|
|
result.peerId = _peer->id;
|
2017-11-03 15:47:08 +00:00
|
|
|
result.query = query;
|
2017-10-31 18:25:22 +00:00
|
|
|
result.migratedPeerId = _migrated ? _migrated->id : PeerId(0);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-11-21 09:20:56 +00:00
|
|
|
rpl::producer<bool> Controller::searchEnabledByContent() const {
|
|
|
|
return _seachEnabledByContent.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<QString> Controller::mediaSourceQueryValue() const {
|
|
|
|
return _searchController->currentQueryValue();
|
|
|
|
}
|
|
|
|
|
2017-10-31 18:25:22 +00:00
|
|
|
rpl::producer<SparseIdsMergedSlice> Controller::mediaSource(
|
|
|
|
SparseIdsMergedSlice::UniversalMsgId aroundId,
|
|
|
|
int limitBefore,
|
|
|
|
int limitAfter) const {
|
|
|
|
auto query = _searchController->currentQuery();
|
|
|
|
if (!query.query.isEmpty()) {
|
|
|
|
return _searchController->idsSlice(
|
|
|
|
aroundId,
|
|
|
|
limitBefore,
|
|
|
|
limitAfter);
|
|
|
|
}
|
|
|
|
|
|
|
|
return SharedMediaMergedViewer(
|
|
|
|
SharedMediaMergedKey(
|
|
|
|
SparseIdsMergedSlice::Key(
|
|
|
|
query.peerId,
|
|
|
|
query.migratedPeerId,
|
|
|
|
aroundId),
|
|
|
|
query.type),
|
|
|
|
limitBefore,
|
|
|
|
limitAfter);
|
|
|
|
}
|
|
|
|
|
|
|
|
Controller::~Controller() = default;
|
|
|
|
|
|
|
|
} // namespace Info
|