2015-04-02 10:33:19 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2015-04-02 10:33:19 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2015-04-02 10:33:19 +00:00
|
|
|
*/
|
2017-04-06 14:38:10 +00:00
|
|
|
#include "boxes/sessions_box.h"
|
2015-04-02 10:33:19 +00:00
|
|
|
|
2020-09-08 15:01:12 +00:00
|
|
|
#include "apiwrap.h"
|
|
|
|
#include "api/api_authorizations.h"
|
2020-09-05 14:33:53 +00:00
|
|
|
#include "base/timer.h"
|
2019-07-10 17:28:33 +00:00
|
|
|
#include "base/unixtime.h"
|
2021-10-18 21:36:55 +00:00
|
|
|
#include "ui/boxes/confirm_box.h"
|
2020-09-05 14:48:15 +00:00
|
|
|
#include "lang/lang_keys.h"
|
|
|
|
#include "main/main_session.h"
|
|
|
|
#include "styles/style_boxes.h"
|
|
|
|
#include "styles/style_info.h"
|
|
|
|
#include "styles/style_layers.h"
|
|
|
|
#include "styles/style_settings.h"
|
2016-11-11 13:46:04 +00:00
|
|
|
#include "ui/widgets/buttons.h"
|
2018-11-20 10:03:44 +00:00
|
|
|
#include "ui/widgets/labels.h"
|
2020-09-05 14:48:15 +00:00
|
|
|
#include "ui/widgets/scroll_area.h"
|
2018-11-20 10:03:44 +00:00
|
|
|
#include "ui/wrap/slide_wrap.h"
|
|
|
|
#include "ui/wrap/vertical_layout.h"
|
2020-09-05 14:48:15 +00:00
|
|
|
#include "window/window_session_controller.h"
|
2018-11-20 10:03:44 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
constexpr auto kSessionsShortPollTimeout = 60 * crl::time(1000);
|
2018-11-20 10:03:44 +00:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2020-09-05 14:33:53 +00:00
|
|
|
class SessionsContent : public Ui::RpWidget {
|
|
|
|
public:
|
|
|
|
SessionsContent(QWidget*, not_null<Main::Session*> session);
|
|
|
|
|
|
|
|
void setupContent();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void resizeEvent(QResizeEvent *e) override;
|
|
|
|
void paintEvent(QPaintEvent *e) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct Entry {
|
2020-09-08 15:01:12 +00:00
|
|
|
Entry() = default;
|
|
|
|
Entry(const Api::Authorizations::Entry &entry)
|
|
|
|
: hash(entry.hash)
|
|
|
|
, incomplete(entry.incomplete)
|
|
|
|
, activeTime(entry.activeTime)
|
2020-09-11 12:28:49 +00:00
|
|
|
, name(st::sessionNameStyle, entry.name)
|
|
|
|
, active(st::sessionWhenStyle, entry.active)
|
|
|
|
, info(st::sessionInfoStyle, entry.info)
|
|
|
|
, ip(st::sessionInfoStyle, entry.ip) {
|
2020-09-08 15:01:12 +00:00
|
|
|
};
|
|
|
|
|
2020-09-05 14:33:53 +00:00
|
|
|
uint64 hash = 0;
|
|
|
|
|
|
|
|
bool incomplete = false;
|
|
|
|
TimeId activeTime = 0;
|
2020-09-11 12:28:49 +00:00
|
|
|
Ui::Text::String name, active, info, ip;
|
2020-09-05 14:33:53 +00:00
|
|
|
};
|
|
|
|
struct Full {
|
|
|
|
Entry current;
|
|
|
|
std::vector<Entry> incomplete;
|
|
|
|
std::vector<Entry> list;
|
|
|
|
};
|
|
|
|
class Inner;
|
|
|
|
class List;
|
|
|
|
|
|
|
|
void shortPollSessions();
|
2020-09-18 19:04:18 +00:00
|
|
|
void parse(const Api::Authorizations::List &list);
|
2020-09-05 14:33:53 +00:00
|
|
|
|
|
|
|
void terminate(Fn<void()> terminateRequest, QString message);
|
|
|
|
void terminateOne(uint64 hash);
|
|
|
|
void terminateAll();
|
|
|
|
|
2020-09-08 15:01:12 +00:00
|
|
|
const not_null<Api::Authorizations*> _authorizations;
|
2020-09-05 14:33:53 +00:00
|
|
|
|
|
|
|
rpl::variable<bool> _loading = false;
|
|
|
|
Full _data;
|
|
|
|
|
|
|
|
object_ptr<Inner> _inner;
|
2021-10-18 22:28:08 +00:00
|
|
|
QPointer<Ui::ConfirmBox> _terminateBox;
|
2020-09-05 14:33:53 +00:00
|
|
|
|
|
|
|
base::Timer _shortPollTimer;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class SessionsContent::List : public Ui::RpWidget {
|
2018-11-20 10:03:44 +00:00
|
|
|
public:
|
|
|
|
List(QWidget *parent);
|
|
|
|
|
|
|
|
void showData(gsl::span<const Entry> items);
|
|
|
|
rpl::producer<int> itemsCount() const;
|
|
|
|
rpl::producer<uint64> terminate() const;
|
|
|
|
|
|
|
|
void terminating(uint64 hash, bool terminating);
|
|
|
|
|
|
|
|
protected:
|
2020-09-11 12:28:49 +00:00
|
|
|
void resizeEvent(QResizeEvent *e) override;
|
2018-11-20 10:03:44 +00:00
|
|
|
void paintEvent(QPaintEvent *e) override;
|
|
|
|
|
|
|
|
int resizeGetHeight(int newWidth) override;
|
|
|
|
|
|
|
|
private:
|
2020-09-11 12:28:49 +00:00
|
|
|
struct RowWidth {
|
|
|
|
int available = 0;
|
|
|
|
int info = 0;
|
|
|
|
};
|
|
|
|
RowWidth _rowWidth;
|
|
|
|
|
|
|
|
void computeRowWidth();
|
|
|
|
|
2018-11-20 10:03:44 +00:00
|
|
|
std::vector<Entry> _items;
|
|
|
|
std::map<uint64, std::unique_ptr<Ui::IconButton>> _terminateButtons;
|
|
|
|
rpl::event_stream<uint64> _terminate;
|
|
|
|
rpl::event_stream<int> _itemsCount;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2020-09-05 14:33:53 +00:00
|
|
|
class SessionsContent::Inner : public Ui::RpWidget {
|
2018-11-20 10:03:44 +00:00
|
|
|
public:
|
|
|
|
Inner(QWidget *parent);
|
|
|
|
|
|
|
|
void showData(const Full &data);
|
|
|
|
rpl::producer<uint64> terminateOne() const;
|
|
|
|
rpl::producer<> terminateAll() const;
|
|
|
|
|
|
|
|
void terminatingOne(uint64 hash, bool terminating);
|
|
|
|
|
|
|
|
private:
|
|
|
|
void setupContent();
|
|
|
|
|
|
|
|
QPointer<List> _current;
|
2019-11-02 17:06:47 +00:00
|
|
|
QPointer<Ui::SettingsButton> _terminateAll;
|
2018-11-20 10:03:44 +00:00
|
|
|
QPointer<List> _incomplete;
|
|
|
|
QPointer<List> _list;
|
|
|
|
|
|
|
|
};
|
2015-04-02 10:33:19 +00:00
|
|
|
|
2020-09-05 14:33:53 +00:00
|
|
|
SessionsContent::SessionsContent(QWidget*, not_null<Main::Session*> session)
|
2020-09-08 15:12:16 +00:00
|
|
|
: _authorizations(&session->api().authorizations())
|
2020-09-05 14:33:53 +00:00
|
|
|
, _inner(this)
|
2019-07-25 18:55:11 +00:00
|
|
|
, _shortPollTimer([=] { shortPollSessions(); }) {
|
2016-12-13 17:07:56 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 14:33:53 +00:00
|
|
|
void SessionsContent::setupContent() {
|
2018-06-03 19:52:23 +00:00
|
|
|
_inner->resize(width(), st::noContactsHeight);
|
|
|
|
|
2020-09-05 14:33:53 +00:00
|
|
|
_inner->heightValue(
|
|
|
|
) | rpl::distinct_until_changed(
|
|
|
|
) | rpl::start_with_next([=](int height) {
|
|
|
|
resize(width(), height);
|
|
|
|
}, _inner->lifetime());
|
|
|
|
|
2018-11-20 10:03:44 +00:00
|
|
|
_inner->terminateOne(
|
|
|
|
) | rpl::start_with_next([=](uint64 hash) {
|
|
|
|
terminateOne(hash);
|
|
|
|
}, lifetime());
|
2015-04-02 10:33:19 +00:00
|
|
|
|
2018-11-20 10:03:44 +00:00
|
|
|
_inner->terminateAll(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
terminateAll();
|
|
|
|
}, lifetime());
|
|
|
|
|
2020-09-05 09:54:23 +00:00
|
|
|
_loading.changes(
|
|
|
|
) | rpl::start_with_next([=](bool value) {
|
2020-09-05 14:33:53 +00:00
|
|
|
_inner->setVisible(!value);
|
2020-09-05 09:54:23 +00:00
|
|
|
}, lifetime());
|
2015-04-02 10:33:19 +00:00
|
|
|
|
2020-09-08 15:01:12 +00:00
|
|
|
_authorizations->listChanges(
|
|
|
|
) | rpl::start_with_next([=](const Api::Authorizations::List &list) {
|
2020-09-18 19:04:18 +00:00
|
|
|
parse(list);
|
|
|
|
}, lifetime());
|
|
|
|
|
|
|
|
_loading = true;
|
|
|
|
shortPollSessions();
|
|
|
|
}
|
2020-09-08 15:01:12 +00:00
|
|
|
|
2020-09-18 19:04:18 +00:00
|
|
|
void SessionsContent::parse(const Api::Authorizations::List &list) {
|
2020-10-01 15:09:36 +00:00
|
|
|
if (list.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
2020-09-18 19:04:18 +00:00
|
|
|
_data = Full();
|
|
|
|
for (const auto &auth : list) {
|
|
|
|
auto entry = Entry(auth);
|
|
|
|
if (!entry.hash) {
|
|
|
|
_data.current = std::move(entry);
|
|
|
|
} else if (entry.incomplete) {
|
|
|
|
_data.incomplete.push_back(std::move(entry));
|
|
|
|
} else {
|
|
|
|
_data.list.push_back(std::move(entry));
|
|
|
|
}
|
|
|
|
}
|
2020-09-08 15:01:12 +00:00
|
|
|
|
2020-09-18 19:04:18 +00:00
|
|
|
_loading = false;
|
2020-09-08 15:01:12 +00:00
|
|
|
|
2020-09-18 19:04:18 +00:00
|
|
|
ranges::sort(_data.list, std::greater<>(), &Entry::activeTime);
|
|
|
|
ranges::sort(_data.incomplete, std::greater<>(), &Entry::activeTime);
|
2020-09-08 15:01:12 +00:00
|
|
|
|
2020-09-18 19:04:18 +00:00
|
|
|
_inner->showData(_data);
|
2020-09-08 15:01:12 +00:00
|
|
|
|
2020-09-18 19:04:18 +00:00
|
|
|
_shortPollTimer.callOnce(kSessionsShortPollTimeout);
|
2016-11-18 13:34:58 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 14:33:53 +00:00
|
|
|
void SessionsContent::resizeEvent(QResizeEvent *e) {
|
|
|
|
RpWidget::resizeEvent(e);
|
2016-12-13 17:07:56 +00:00
|
|
|
|
|
|
|
_inner->resize(width(), _inner->height());
|
2015-04-02 10:33:19 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 14:33:53 +00:00
|
|
|
void SessionsContent::paintEvent(QPaintEvent *e) {
|
|
|
|
RpWidget::paintEvent(e);
|
2016-11-19 14:47:28 +00:00
|
|
|
|
2015-04-02 10:33:19 +00:00
|
|
|
Painter p(this);
|
|
|
|
|
2020-09-05 09:54:23 +00:00
|
|
|
if (_loading.current()) {
|
2016-11-18 13:34:58 +00:00
|
|
|
p.setFont(st::noContactsFont);
|
|
|
|
p.setPen(st::noContactsColor);
|
2018-11-20 10:03:44 +00:00
|
|
|
p.drawText(
|
|
|
|
QRect(0, 0, width(), st::noContactsHeight),
|
2019-06-19 15:09:03 +00:00
|
|
|
tr::lng_contacts_loading(tr::now),
|
2018-11-20 10:03:44 +00:00
|
|
|
style::al_center);
|
2015-04-02 10:33:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-05 14:33:53 +00:00
|
|
|
void SessionsContent::shortPollSessions() {
|
2020-09-18 19:04:18 +00:00
|
|
|
const auto left = kSessionsShortPollTimeout
|
|
|
|
- (crl::now() - _authorizations->lastReceivedTime());
|
|
|
|
if (left > 0) {
|
|
|
|
parse(_authorizations->list());
|
|
|
|
_shortPollTimer.cancel();
|
|
|
|
_shortPollTimer.callOnce(left);
|
|
|
|
} else {
|
|
|
|
_authorizations->reload();
|
|
|
|
}
|
2018-11-20 10:03:44 +00:00
|
|
|
update();
|
2015-04-02 10:33:19 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 14:33:53 +00:00
|
|
|
void SessionsContent::terminate(Fn<void()> terminateRequest, QString message) {
|
2020-09-05 10:08:00 +00:00
|
|
|
if (_terminateBox) {
|
|
|
|
_terminateBox->deleteLater();
|
|
|
|
}
|
2018-11-20 10:03:44 +00:00
|
|
|
const auto callback = crl::guard(this, [=] {
|
|
|
|
if (_terminateBox) {
|
|
|
|
_terminateBox->closeBox();
|
|
|
|
_terminateBox = nullptr;
|
|
|
|
}
|
2020-09-05 10:08:00 +00:00
|
|
|
terminateRequest();
|
|
|
|
});
|
|
|
|
_terminateBox = Ui::show(
|
2021-10-18 22:28:08 +00:00
|
|
|
Box<Ui::ConfirmBox>(
|
2020-09-05 10:08:00 +00:00
|
|
|
message,
|
|
|
|
tr::lng_settings_reset_button(tr::now),
|
|
|
|
st::attentionBoxButton,
|
|
|
|
callback),
|
|
|
|
Ui::LayerOption::KeepOther);
|
|
|
|
}
|
|
|
|
|
2020-09-05 14:33:53 +00:00
|
|
|
void SessionsContent::terminateOne(uint64 hash) {
|
2020-09-08 15:01:12 +00:00
|
|
|
const auto weak = Ui::MakeWeak(this);
|
2020-09-05 10:08:00 +00:00
|
|
|
auto callback = [=] {
|
2020-09-08 15:01:12 +00:00
|
|
|
auto done = crl::guard(weak, [=](const MTPBool &result) {
|
2021-02-16 22:25:57 +00:00
|
|
|
if (mtpIsFalse(result)) {
|
|
|
|
return;
|
|
|
|
}
|
2018-11-20 10:03:44 +00:00
|
|
|
_inner->terminatingOne(hash, false);
|
|
|
|
const auto removeByHash = [&](std::vector<Entry> &list) {
|
|
|
|
list.erase(
|
2021-02-16 22:25:57 +00:00
|
|
|
ranges::remove(list, hash, &Entry::hash),
|
2018-11-20 10:03:44 +00:00
|
|
|
end(list));
|
|
|
|
};
|
|
|
|
removeByHash(_data.incomplete);
|
|
|
|
removeByHash(_data.list);
|
|
|
|
_inner->showData(_data);
|
2020-09-08 15:01:12 +00:00
|
|
|
});
|
2021-03-12 12:48:00 +00:00
|
|
|
auto fail = crl::guard(weak, [=](const MTP::Error &error) {
|
2018-11-20 10:03:44 +00:00
|
|
|
_inner->terminatingOne(hash, false);
|
2020-09-08 15:01:12 +00:00
|
|
|
});
|
|
|
|
_authorizations->requestTerminate(
|
|
|
|
std::move(done),
|
|
|
|
std::move(fail),
|
|
|
|
hash);
|
2018-11-20 10:03:44 +00:00
|
|
|
_inner->terminatingOne(hash, true);
|
2020-09-05 10:08:00 +00:00
|
|
|
};
|
|
|
|
terminate(std::move(callback), tr::lng_settings_reset_one_sure(tr::now));
|
2015-04-02 10:33:19 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 14:33:53 +00:00
|
|
|
void SessionsContent::terminateAll() {
|
2020-09-08 15:01:12 +00:00
|
|
|
const auto weak = Ui::MakeWeak(this);
|
2020-09-05 10:08:00 +00:00
|
|
|
auto callback = [=] {
|
2020-09-08 15:01:12 +00:00
|
|
|
const auto reset = crl::guard(weak, [=] {
|
|
|
|
_authorizations->cancelCurrentRequest();
|
2020-12-15 14:23:41 +00:00
|
|
|
_authorizations->reload();
|
2020-09-08 15:01:12 +00:00
|
|
|
});
|
|
|
|
_authorizations->requestTerminate(
|
|
|
|
[=](const MTPBool &result) { reset(); },
|
2021-03-12 12:48:00 +00:00
|
|
|
[=](const MTP::Error &result) { reset(); });
|
2020-09-05 09:54:23 +00:00
|
|
|
_loading = true;
|
2020-09-05 10:08:00 +00:00
|
|
|
};
|
|
|
|
terminate(std::move(callback), tr::lng_settings_reset_sure(tr::now));
|
2015-04-02 10:33:19 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 14:33:53 +00:00
|
|
|
SessionsContent::Inner::Inner(QWidget *parent)
|
2018-11-20 10:03:44 +00:00
|
|
|
: RpWidget(parent) {
|
|
|
|
setupContent();
|
2016-10-20 16:32:15 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 14:33:53 +00:00
|
|
|
void SessionsContent::Inner::setupContent() {
|
2018-11-20 10:03:44 +00:00
|
|
|
using namespace Settings;
|
|
|
|
using namespace rpl::mappers;
|
|
|
|
|
|
|
|
const auto content = Ui::CreateChild<Ui::VerticalLayout>(this);
|
|
|
|
|
2019-06-18 12:16:43 +00:00
|
|
|
AddSubsectionTitle(content, tr::lng_sessions_header());
|
2018-11-20 10:03:44 +00:00
|
|
|
_current = content->add(object_ptr<List>(content));
|
|
|
|
const auto terminateWrap = content->add(
|
|
|
|
object_ptr<Ui::SlideWrap<Ui::VerticalLayout>>(
|
|
|
|
content,
|
|
|
|
object_ptr<Ui::VerticalLayout>(content)))->setDuration(0);
|
|
|
|
const auto terminateInner = terminateWrap->entity();
|
|
|
|
_terminateAll = terminateInner->add(
|
2019-11-02 17:06:47 +00:00
|
|
|
object_ptr<Ui::SettingsButton>(
|
2018-11-20 10:03:44 +00:00
|
|
|
terminateInner,
|
2019-06-18 12:16:43 +00:00
|
|
|
tr::lng_sessions_terminate_all(),
|
2018-11-20 10:03:44 +00:00
|
|
|
st::terminateSessionsButton));
|
|
|
|
AddSkip(terminateInner);
|
2019-06-18 12:16:43 +00:00
|
|
|
AddDividerText(terminateInner, tr::lng_sessions_terminate_all_about());
|
2018-11-20 10:03:44 +00:00
|
|
|
|
|
|
|
const auto incompleteWrap = content->add(
|
|
|
|
object_ptr<Ui::SlideWrap<Ui::VerticalLayout>>(
|
|
|
|
content,
|
|
|
|
object_ptr<Ui::VerticalLayout>(content)))->setDuration(0);
|
|
|
|
const auto incompleteInner = incompleteWrap->entity();
|
|
|
|
AddSkip(incompleteInner);
|
2019-06-18 12:16:43 +00:00
|
|
|
AddSubsectionTitle(incompleteInner, tr::lng_sessions_incomplete());
|
2018-11-20 10:03:44 +00:00
|
|
|
_incomplete = incompleteInner->add(object_ptr<List>(incompleteInner));
|
|
|
|
AddSkip(incompleteInner);
|
2019-06-18 12:16:43 +00:00
|
|
|
AddDividerText(incompleteInner, tr::lng_sessions_incomplete_about());
|
2018-11-20 10:03:44 +00:00
|
|
|
|
|
|
|
const auto listWrap = content->add(
|
|
|
|
object_ptr<Ui::SlideWrap<Ui::VerticalLayout>>(
|
|
|
|
content,
|
|
|
|
object_ptr<Ui::VerticalLayout>(content)))->setDuration(0);
|
|
|
|
const auto listInner = listWrap->entity();
|
|
|
|
AddSkip(listInner);
|
2019-06-18 12:16:43 +00:00
|
|
|
AddSubsectionTitle(listInner, tr::lng_sessions_other_header());
|
2018-11-20 10:03:44 +00:00
|
|
|
_list = listInner->add(object_ptr<List>(listInner));
|
|
|
|
AddSkip(listInner);
|
|
|
|
|
|
|
|
const auto placeholder = content->add(
|
|
|
|
object_ptr<Ui::SlideWrap<Ui::FlatLabel>>(
|
|
|
|
content,
|
|
|
|
object_ptr<Ui::FlatLabel>(
|
|
|
|
content,
|
2019-06-18 12:16:43 +00:00
|
|
|
tr::lng_sessions_other_desc(),
|
2018-11-20 10:03:44 +00:00
|
|
|
st::boxDividerLabel),
|
|
|
|
st::settingsDividerLabelPadding))->setDuration(0);
|
|
|
|
|
|
|
|
terminateWrap->toggleOn(
|
|
|
|
rpl::combine(
|
|
|
|
_incomplete->itemsCount(),
|
|
|
|
_list->itemsCount(),
|
|
|
|
(_1 + _2) > 0));
|
|
|
|
incompleteWrap->toggleOn(_incomplete->itemsCount() | rpl::map(_1 > 0));
|
|
|
|
listWrap->toggleOn(_list->itemsCount() | rpl::map(_1 > 0));
|
|
|
|
placeholder->toggleOn(_list->itemsCount() | rpl::map(_1 == 0));
|
|
|
|
|
|
|
|
Ui::ResizeFitChild(this, content);
|
2016-10-20 16:32:15 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 14:33:53 +00:00
|
|
|
void SessionsContent::Inner::showData(const Full &data) {
|
2018-11-20 10:03:44 +00:00
|
|
|
_current->showData({ &data.current, &data.current + 1 });
|
|
|
|
_list->showData(data.list);
|
|
|
|
_incomplete->showData(data.incomplete);
|
|
|
|
}
|
2016-10-20 16:32:15 +00:00
|
|
|
|
2020-09-05 14:33:53 +00:00
|
|
|
rpl::producer<> SessionsContent::Inner::terminateAll() const {
|
2020-06-21 16:25:29 +00:00
|
|
|
return _terminateAll->clicks() | rpl::to_empty;
|
2018-11-20 10:03:44 +00:00
|
|
|
}
|
2016-10-20 16:32:15 +00:00
|
|
|
|
2020-09-05 14:33:53 +00:00
|
|
|
rpl::producer<uint64> SessionsContent::Inner::terminateOne() const {
|
2018-11-20 10:03:44 +00:00
|
|
|
return rpl::merge(
|
|
|
|
_incomplete->terminate(),
|
|
|
|
_list->terminate());
|
|
|
|
}
|
2016-10-20 16:32:15 +00:00
|
|
|
|
2020-09-05 14:33:53 +00:00
|
|
|
void SessionsContent::Inner::terminatingOne(uint64 hash, bool terminating) {
|
2018-11-20 10:03:44 +00:00
|
|
|
_incomplete->terminating(hash, terminating);
|
|
|
|
_list->terminating(hash, terminating);
|
|
|
|
}
|
2016-10-20 16:32:15 +00:00
|
|
|
|
2020-09-05 14:33:53 +00:00
|
|
|
SessionsContent::List::List(QWidget *parent) : RpWidget(parent) {
|
2018-11-20 10:03:44 +00:00
|
|
|
setAttribute(Qt::WA_OpaquePaintEvent);
|
|
|
|
}
|
2016-10-20 16:32:15 +00:00
|
|
|
|
2020-09-11 12:28:49 +00:00
|
|
|
void SessionsContent::List::resizeEvent(QResizeEvent *e) {
|
|
|
|
RpWidget::resizeEvent(e);
|
|
|
|
|
|
|
|
computeRowWidth();
|
|
|
|
}
|
|
|
|
|
2020-09-05 14:33:53 +00:00
|
|
|
void SessionsContent::List::showData(gsl::span<const Entry> items) {
|
2020-09-11 12:28:49 +00:00
|
|
|
computeRowWidth();
|
|
|
|
|
2018-11-20 10:03:44 +00:00
|
|
|
auto buttons = base::take(_terminateButtons);
|
|
|
|
_items.clear();
|
|
|
|
_items.insert(begin(_items), items.begin(), items.end());
|
|
|
|
for (const auto &entry : _items) {
|
|
|
|
const auto hash = entry.hash;
|
|
|
|
if (!hash) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const auto button = [&] {
|
|
|
|
const auto i = buttons.find(hash);
|
|
|
|
return _terminateButtons.emplace(
|
|
|
|
hash,
|
|
|
|
(i != end(buttons)
|
|
|
|
? std::move(i->second)
|
|
|
|
: std::make_unique<Ui::IconButton>(
|
|
|
|
this,
|
|
|
|
st::sessionTerminate))).first->second.get();
|
|
|
|
}();
|
|
|
|
button->setClickedCallback([=] {
|
|
|
|
_terminate.fire_copy(hash);
|
|
|
|
});
|
|
|
|
button->show();
|
2020-09-05 14:33:53 +00:00
|
|
|
const auto number = _terminateButtons.size() - 1;
|
|
|
|
widthValue(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
button->moveToRight(
|
|
|
|
st::sessionTerminateSkip,
|
|
|
|
(number * st::sessionHeight + st::sessionTerminateTop));
|
|
|
|
}, lifetime());
|
2016-10-20 16:32:15 +00:00
|
|
|
}
|
2018-11-20 10:03:44 +00:00
|
|
|
resizeToWidth(width());
|
|
|
|
_itemsCount.fire(_items.size());
|
|
|
|
}
|
2016-10-20 16:32:15 +00:00
|
|
|
|
2020-09-05 14:33:53 +00:00
|
|
|
rpl::producer<int> SessionsContent::List::itemsCount() const {
|
2018-11-20 10:03:44 +00:00
|
|
|
return _itemsCount.events_starting_with(_items.size());
|
|
|
|
}
|
2016-10-20 16:32:15 +00:00
|
|
|
|
2020-09-05 14:33:53 +00:00
|
|
|
rpl::producer<uint64> SessionsContent::List::terminate() const {
|
2018-11-20 10:03:44 +00:00
|
|
|
return _terminate.events();
|
2016-10-20 16:32:15 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 14:33:53 +00:00
|
|
|
void SessionsContent::List::terminating(uint64 hash, bool terminating) {
|
2018-11-20 10:03:44 +00:00
|
|
|
const auto i = _terminateButtons.find(hash);
|
|
|
|
if (i != _terminateButtons.cend()) {
|
|
|
|
if (terminating) {
|
|
|
|
i->second->clearState();
|
|
|
|
i->second->hide();
|
|
|
|
} else {
|
|
|
|
i->second->show();
|
2016-10-20 16:32:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-05 14:33:53 +00:00
|
|
|
int SessionsContent::List::resizeGetHeight(int newWidth) {
|
2018-11-20 10:03:44 +00:00
|
|
|
return _items.size() * st::sessionHeight;
|
2016-10-20 16:32:15 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 12:28:49 +00:00
|
|
|
void SessionsContent::List::computeRowWidth() {
|
|
|
|
const auto available = width()
|
|
|
|
- st::sessionPadding.left()
|
|
|
|
- st::sessionTerminateSkip;
|
|
|
|
_rowWidth = {
|
|
|
|
.available = available,
|
|
|
|
.info = available - st::sessionTerminate.width,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-09-05 14:33:53 +00:00
|
|
|
void SessionsContent::List::paintEvent(QPaintEvent *e) {
|
2018-11-20 10:03:44 +00:00
|
|
|
QRect r(e->rect());
|
|
|
|
Painter p(this);
|
2016-10-20 16:32:15 +00:00
|
|
|
|
2018-11-20 10:03:44 +00:00
|
|
|
p.fillRect(r, st::boxBg);
|
|
|
|
p.setFont(st::linkFont);
|
|
|
|
const auto count = int(_items.size());
|
|
|
|
const auto from = floorclamp(r.y(), st::sessionHeight, 0, count);
|
|
|
|
const auto till = ceilclamp(
|
|
|
|
r.y() + r.height(),
|
|
|
|
st::sessionHeight,
|
|
|
|
0,
|
|
|
|
count);
|
|
|
|
|
2020-09-11 12:28:49 +00:00
|
|
|
const auto available = _rowWidth.available;
|
2018-11-20 10:03:44 +00:00
|
|
|
const auto x = st::sessionPadding.left();
|
|
|
|
const auto y = st::sessionPadding.top();
|
|
|
|
const auto w = width();
|
|
|
|
const auto xact = st::sessionTerminateSkip
|
|
|
|
+ st::sessionTerminate.iconPosition.x();
|
|
|
|
p.translate(0, from * st::sessionHeight);
|
|
|
|
for (auto i = from; i != till; ++i) {
|
|
|
|
const auto &entry = _items[i];
|
2016-10-20 16:32:15 +00:00
|
|
|
|
2020-09-11 12:28:49 +00:00
|
|
|
const auto activeW = entry.active.maxWidth();
|
|
|
|
const auto nameW = available
|
|
|
|
- activeW
|
|
|
|
- st::sessionNamePadding.right();
|
|
|
|
const auto nameH = entry.name.style()->font->height;
|
|
|
|
const auto infoW = entry.hash ? _rowWidth.info : available;
|
|
|
|
const auto infoH = entry.info.style()->font->height;
|
|
|
|
|
|
|
|
p.setPen(entry.hash ? st::sessionWhenFg : st::contactsStatusFgOnline);
|
|
|
|
entry.active.drawRight(p, xact, y, activeW, w);
|
2016-10-20 16:32:15 +00:00
|
|
|
|
2020-09-11 12:28:49 +00:00
|
|
|
p.setPen(st::sessionNameFg);
|
|
|
|
entry.name.drawLeftElided(p, x, y, nameW, w);
|
2016-10-20 16:32:15 +00:00
|
|
|
|
2018-11-20 10:03:44 +00:00
|
|
|
p.setPen(st::boxTextFg);
|
2020-09-11 12:28:49 +00:00
|
|
|
entry.info.drawLeftElided(p, x, y + nameH, infoW, w);
|
2016-10-20 16:32:15 +00:00
|
|
|
|
2018-11-20 10:03:44 +00:00
|
|
|
p.setPen(st::sessionInfoFg);
|
2020-09-11 12:28:49 +00:00
|
|
|
entry.ip.drawLeftElided(p, x, y + nameH + infoH, available, w);
|
2016-10-20 16:32:15 +00:00
|
|
|
|
2018-11-20 10:03:44 +00:00
|
|
|
p.translate(0, st::sessionHeight);
|
2016-10-20 16:32:15 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-05 14:33:53 +00:00
|
|
|
|
|
|
|
SessionsBox::SessionsBox(QWidget*, not_null<Main::Session*> session)
|
|
|
|
: _session(session) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void SessionsBox::prepare() {
|
|
|
|
setTitle(tr::lng_sessions_other_header());
|
|
|
|
|
|
|
|
addButton(tr::lng_close(), [=] { closeBox(); });
|
|
|
|
|
|
|
|
const auto w = st::boxWideWidth;
|
|
|
|
|
|
|
|
const auto content = setInnerWidget(
|
|
|
|
object_ptr<SessionsContent>(this, _session),
|
|
|
|
st::sessionsScroll);
|
|
|
|
content->resize(w, st::noContactsHeight);
|
|
|
|
content->setupContent();
|
|
|
|
|
|
|
|
setDimensions(w, st::sessionsHeight);
|
|
|
|
}
|
2020-09-05 14:48:15 +00:00
|
|
|
|
|
|
|
namespace Settings {
|
|
|
|
|
|
|
|
Sessions::Sessions(
|
|
|
|
QWidget *parent,
|
|
|
|
not_null<Window::SessionController*> controller)
|
|
|
|
: Section(parent) {
|
|
|
|
setupContent(controller);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sessions::setupContent(not_null<Window::SessionController*> controller) {
|
|
|
|
const auto container = Ui::CreateChild<Ui::VerticalLayout>(this);
|
|
|
|
const auto content = container->add(
|
|
|
|
object_ptr<SessionsContent>(container, &controller->session()));
|
|
|
|
content->setupContent();
|
|
|
|
|
|
|
|
Ui::ResizeFitChild(this, container);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Settings
|