Skip to content

Commit

Permalink
Suggest start export when time comes.
Browse files Browse the repository at this point in the history
  • Loading branch information
john-preston committed Jun 22, 2018
1 parent 844d030 commit 0143fd2
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 11 deletions.
3 changes: 3 additions & 0 deletions Telegram/Resources/langs/lang.strings
Original file line number Diff line number Diff line change
Expand Up @@ -1703,6 +1703,9 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
"lng_export_folder" = "Choose export folder";
"lng_export_invalid" = "Sorry, you have started a new data export, so this data export is now cancelled.";
"lng_export_delay" = "Sorry, for security reasons, you will be able to begin downloading your data in 24 hours. We have notified all your devices about the export request to make sure it's authorized and give you time to react if it's not.\n\nPlease come back on {date} and repeat the request using the same device.";
"lng_export_suggest_title" = "Data export ready";
"lng_export_suggest_text" = "You can now download the data you requested. Start exporting data?";
"lng_export_suggest_cancel" = "Not now";

// Wnd specific

Expand Down
1 change: 1 addition & 0 deletions Telegram/SourceFiles/core/basic_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ using float32 = float;
using float64 = double;

using TimeMs = int64;
using TimeId = int32;

#define qsl(s) QStringLiteral(s)
#define qstr(s) QLatin1String((s), sizeof(s) - 1)
1 change: 0 additions & 1 deletion Telegram/SourceFiles/core/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ inline void accumulate_max(T &a, const T &b) { if (a < b) a = b; }
template <typename T>
inline void accumulate_min(T &a, const T &b) { if (a > b) a = b; }

using TimeId = int32;
void unixtimeInit();
void unixtimeSet(TimeId serverTime, bool force = false);
TimeId unixtime();
Expand Down
23 changes: 23 additions & 0 deletions Telegram/SourceFiles/data/data_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,29 @@ void Session::startExport() {
}, _export->lifetime());
}

void Session::suggestStartExport(TimeId availableAt) {
_exportAvailableAt = availableAt;
suggestStartExport();
}

void Session::suggestStartExport() {
if (_exportAvailableAt <= 0) {
return;
}
const auto now = unixtime();
const auto left = (_exportAvailableAt <= now)
? 0
: (_exportAvailableAt - now);
if (!left) {
Export::View::SuggestStart();
} else {
App::CallDelayed(
std::min(left + 5, 3600) * TimeMs(1000),
_session,
[=] { suggestStartExport(); });
}
}

rpl::producer<Export::View::PanelController*> Session::currentExportView(
) const {
return _exportViewChanges.events_starting_with(_exportPanel.get());
Expand Down
4 changes: 4 additions & 0 deletions Telegram/SourceFiles/data/data_session.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class Session final {
}

void startExport();
void suggestStartExport(TimeId availableAt);
rpl::producer<Export::View::PanelController*> currentExportView() const;
bool exportInProgress() const;
void stopExportWithConfirmation(FnMut<void()> callback);
Expand Down Expand Up @@ -408,6 +409,8 @@ class Session final {
}

private:
void suggestStartExport();

void setupContactViewsViewer();
void setupChannelLeavingViewer();
void photoApplyFields(
Expand Down Expand Up @@ -505,6 +508,7 @@ class Session final {
std::unique_ptr<Export::ControllerWrap> _export;
std::unique_ptr<Export::View::PanelController> _exportPanel;
rpl::event_stream<Export::View::PanelController*> _exportViewChanges;
TimeId _exportAvailableAt = 0;

base::Variable<bool> _contactsLoaded = { false };
base::Variable<bool> _allChatsLoaded = { false };
Expand Down
1 change: 0 additions & 1 deletion Telegram/SourceFiles/export/data/export_data_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ namespace Export {
struct Settings;
namespace Data {

using TimeId = int32;
using Utf8String = QByteArray;
using PeerId = uint64;

Expand Down
2 changes: 1 addition & 1 deletion Telegram/SourceFiles/export/export_api_wrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ struct ApiWrap::LeftChannelsProcess : ChatsProcess {

struct ApiWrap::DialogsProcess : ChatsProcess {
int splitIndexPlusOne = 0;
Data::TimeId offsetDate = 0;
TimeId offsetDate = 0;
int32 offsetId = 0;
MTPInputPeer offsetPeer = MTP_inputPeerEmpty();
};
Expand Down
2 changes: 2 additions & 0 deletions Telegram/SourceFiles/export/export_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ struct Settings {
Types fullChats = DefaultFullChats();
MediaSettings media;

TimeId availableAt = 0;

static inline Types DefaultTypes() {
return Type::PersonalInfo
| Type::Userpics
Expand Down
72 changes: 70 additions & 2 deletions Telegram/SourceFiles/export/view/export_view_panel_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,81 @@ For license and copyright information please follow this link:
#include "storage/localstorage.h"
#include "core/file_utilities.h"
#include "platform/platform_specific.h"
#include "auth_session.h"
#include "data/data_session.h"
#include "styles/style_export.h"
#include "styles/style_boxes.h"

namespace Export {
namespace View {
namespace {

constexpr auto kAddDelay = TimeId(60);
constexpr auto kSaveSettingsTimeout = TimeMs(1000);

class SuggestBox : public BoxContent {
public:
SuggestBox(QWidget*);

protected:
void prepare() override;

private:
bool _cleared = false;

};

SuggestBox::SuggestBox(QWidget*) {
}

void SuggestBox::prepare() {
setTitle(langFactory(lng_export_suggest_title));

const auto clear = [=] {
if (_cleared) {
return;
}
_cleared = true;

auto settings = Local::ReadExportSettings();
settings.availableAt = 0;
Local::WriteExportSettings(settings);
};

addButton(langFactory(lng_box_ok), [=] {
clear();
closeBox();
Auth().data().startExport();
});
addButton(langFactory(lng_export_suggest_cancel), [=] { closeBox(); });
setCloseByOutsideClick(false);

const auto content = Ui::CreateChild<Ui::FlatLabel>(
this,
lang(lng_export_suggest_text),
Ui::FlatLabel::InitType::Simple,
st::boxLabel);
widthValue(
) | rpl::start_with_next([=](int width) {
const auto contentWidth = width
- st::boxPadding.left()
- st::boxPadding.right();
content->resizeToWidth(contentWidth);
content->moveToLeft(st::boxPadding.left(), 0);
}, content->lifetime());
content->heightValue(
) | rpl::start_with_next([=](int height) {
setDimensions(st::boxWidth, height + st::boxPadding.bottom());
}, content->lifetime());

boxClosing() | rpl::start_with_next(clear, lifetime());
}

} // namespace

void SuggestStart() {
Ui::show(Box<SuggestBox>(), LayerOption::KeepOther);
}

PanelController::PanelController(not_null<ControllerWrap*> process)
: _process(process)
, _settings(std::make_unique<Settings>(Local::ReadExportSettings()))
Expand Down Expand Up @@ -91,10 +154,15 @@ void PanelController::showError(const ApiErrorState &error) {
showError(lang(lng_export_invalid));
} else if (error.data.type().startsWith(qstr("TAKEOUT_INIT_DELAY_"))) {
const auto seconds = std::max(error.data.type().mid(
qstr("TAKEOUT_INIT_DELAY_").size()).toInt(), 0) + kAddDelay;
qstr("TAKEOUT_INIT_DELAY_").size()).toInt(), 1);
const auto now = QDateTime::currentDateTime();
const auto when = now.addSecs(seconds);
showError(lng_export_delay(lt_date, langDateTimeFull(when)));

_settings->availableAt = unixtime() + seconds;
_saveSettingsTimer.callOnce(kSaveSettingsTimeout);

Auth().data().suggestStartExport(_settings->availableAt);
} else {
showCriticalError("API Error happened :(\n"
+ QString::number(error.data.code()) + ": " + error.data.type()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class SeparatePanel;
namespace Export {
namespace View {

void SuggestStart();

class Panel;

class PanelController {
Expand Down
5 changes: 5 additions & 0 deletions Telegram/SourceFiles/mainwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ For license and copyright information please follow this link:
#include "core/update_checker.h"
#include "calls/calls_instance.h"
#include "calls/calls_top_bar.h"
#include "export/export_settings.h"
#include "export/view/export_view_top_bar.h"
#include "export/view/export_view_panel_controller.h"
#include "auth_session.h"
Expand Down Expand Up @@ -3821,6 +3822,10 @@ void MainWidget::start(const MTPUser *self) {
Local::readRecentStickers();
Local::readFavedStickers();
Local::readSavedGifs();
if (const auto availableAt = Local::ReadExportSettings().availableAt) {
Auth().data().suggestStartExport(availableAt);
}

_history->start();

Messenger::Instance().checkStartUrl();
Expand Down
18 changes: 12 additions & 6 deletions Telegram/SourceFiles/storage/localstorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4623,7 +4623,8 @@ void WriteExportSettings(const Export::Settings &settings) {
&& settings.media.types == check.media.types
&& settings.media.sizeLimit == check.media.sizeLimit
&& settings.path == check.path
&& settings.format == check.format) {
&& settings.format == check.format
&& settings.availableAt == check.availableAt) {
if (_exportSettingsKey) {
clearKey(_exportSettingsKey);
_exportSettingsKey = 0;
Expand All @@ -4636,7 +4637,7 @@ void WriteExportSettings(const Export::Settings &settings) {
_mapChanged = true;
_writeMap(WriteMapWhen::Fast);
}
quint32 size = sizeof(quint32) * 5
quint32 size = sizeof(quint32) * 6
+ Serialize::stringSize(settings.path);
EncryptedDescriptor data(size);
data.stream
Expand All @@ -4645,7 +4646,8 @@ void WriteExportSettings(const Export::Settings &settings) {
<< quint32(settings.media.types)
<< quint32(settings.media.sizeLimit)
<< quint32(settings.format)
<< settings.path;
<< settings.path
<< quint32(settings.availableAt);

FileWriteDescriptor file(_exportSettingsKey);
file.writeEncrypted(data);
Expand All @@ -4663,23 +4665,27 @@ Export::Settings ReadExportSettings() {

quint32 types = 0, fullChats = 0;
quint32 mediaTypes = 0, mediaSizeLimit = 0;
quint32 format = 0;
quint32 format = 0, availableAt = 0;
QString path;
file.stream
>> types
>> fullChats
>> mediaTypes
>> mediaSizeLimit
>> format
>> path;
>> path
>> availableAt;
auto result = Export::Settings();
result.types = Export::Settings::Types::from_raw(types);
result.fullChats = Export::Settings::Types::from_raw(fullChats);
result.media.types = Export::MediaSettings::Types::from_raw(mediaTypes);
result.media.sizeLimit = mediaSizeLimit;
result.format = Export::Output::Format(format);
result.path = path;
return result.validate() ? result : Export::Settings();
result.availableAt = availableAt;
return (file.stream.status() == QDataStream::Ok && result.validate())
? result
: Export::Settings();
}

void writeSavedPeers() {
Expand Down

0 comments on commit 0143fd2

Please sign in to comment.