Skip to content

Commit

Permalink
Save export settings to local storage.
Browse files Browse the repository at this point in the history
  • Loading branch information
john-preston committed Jun 22, 2018
1 parent ae18ece commit 844d030
Show file tree
Hide file tree
Showing 10 changed files with 312 additions and 79 deletions.
2 changes: 1 addition & 1 deletion Telegram/SourceFiles/core/click_handler_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class HiddenUrlClickHandler : public UrlClickHandler {
HiddenUrlClickHandler(QString url) : UrlClickHandler(url, false) {
}
QString copyToClipboardContextItemText() const override {
return url().isEmpty()
return (url().isEmpty() || url().startsWith(qstr("internal:")))
? QString()
: UrlClickHandler::copyToClipboardContextItemText();
}
Expand Down
48 changes: 48 additions & 0 deletions Telegram/SourceFiles/export/export_settings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "export/export_settings.h"

#include "export/output/export_output_abstract.h"

namespace Export {
namespace {

constexpr auto kMaxFileSize = 1500 * 1024 * 1024;

} // namespace

bool MediaSettings::validate() const {
if ((types | Type::AllMask) != Type::AllMask) {
return false;
} else if (sizeLimit < 0 || sizeLimit > kMaxFileSize) {
return false;
}
return true;
}

bool Settings::validate() const {
using Format = Output::Format;
const auto MustBeFull = Type::PersonalChats | Type::BotChats;
const auto MustNotBeFull = Type::PublicGroups | Type::PublicChannels;
if ((types | Type::AllMask) != Type::AllMask) {
return false;
} else if ((fullChats | Type::AllMask) != Type::AllMask) {
return false;
} else if ((fullChats & MustBeFull) != MustBeFull) {
return false;
} else if ((fullChats & MustNotBeFull) != 0) {
return false;
} else if (format != Format::Text && format != Format::Json) {
return false;
} else if (!media.validate()) {
return false;
}
return true;
};

} // namespace Export
9 changes: 9 additions & 0 deletions Telegram/SourceFiles/export/export_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ enum class Format;
} // namespace Output

struct MediaSettings {
bool validate() const;

enum class Type {
Photo = 0x01,
Video = 0x02,
Expand All @@ -24,6 +26,9 @@ struct MediaSettings {
Sticker = 0x10,
GIF = 0x20,
File = 0x40,

MediaMask = Photo | Video | VoiceMessage | VideoMessage,
AllMask = MediaMask | Sticker | GIF | File,
};
using Types = base::flags<Type>;
friend inline constexpr auto is_flag_type(Type) { return true; };
Expand All @@ -38,6 +43,8 @@ struct MediaSettings {
};

struct Settings {
bool validate() const;

enum class Type {
PersonalInfo = 0x001,
Userpics = 0x002,
Expand All @@ -55,6 +62,8 @@ struct Settings {
GroupsChannelsMask = GroupsMask | ChannelsMask,
NonChannelChatsMask = PersonalChats | BotChats | PrivateGroups,
AnyChatsMask = PersonalChats | BotChats | GroupsChannelsMask,
NonChatsMask = PersonalInfo | Userpics | Contacts | Sessions,
AllMask = NonChatsMask | AnyChatsMask,
};
using Types = base::flags<Type>;
friend inline constexpr auto is_flag_type(Type) { return true; };
Expand Down
46 changes: 41 additions & 5 deletions Telegram/SourceFiles/export/view/export_view_panel_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ For license and copyright information please follow this link:
#include "ui/wrap/padding_wrap.h"
#include "boxes/confirm_box.h"
#include "lang/lang_keys.h"
#include "storage/localstorage.h"
#include "core/file_utilities.h"
#include "platform/platform_specific.h"
#include "styles/style_export.h"
#include "styles/style_boxes.h"

Expand All @@ -23,11 +25,19 @@ namespace View {
namespace {

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

} // namespace

PanelController::PanelController(not_null<ControllerWrap*> process)
: _process(process) {
: _process(process)
, _settings(std::make_unique<Settings>(Local::ReadExportSettings()))
, _saveSettingsTimer([=] { saveSettings(); }) {
if (_settings->path.isEmpty()) {
_settings->path = psDownloadPath();
}
_settings->internalLinksDomain = Global::InternalLinksDomain();

_process->state(
) | rpl::start_with_next([=](State &&state) {
updateState(std::move(state));
Expand All @@ -52,19 +62,27 @@ void PanelController::createPanel() {
}

void PanelController::showSettings() {
auto settings = base::make_unique_q<SettingsWidget>(_panel);
auto settings = base::make_unique_q<SettingsWidget>(
_panel,
*_settings);

settings->startClicks(
) | rpl::start_with_next([=](const Settings &settings) {
) | rpl::start_with_next([=]() {
showProgress();
_process->startExport(settings);
_process->startExport(*_settings);
}, settings->lifetime());

settings->cancelClicks(
) | rpl::start_with_next([=] {
_panel->hideGetDuration();
}, settings->lifetime());

settings->changes(
) | rpl::start_with_next([=](Settings &&settings) {
*_settings = std::move(settings);
_saveSettingsTimer.callOnce(kSaveSettingsTimeout);
}, settings->lifetime());

_panel->showInner(std::move(settings));
}

Expand Down Expand Up @@ -218,7 +236,25 @@ void PanelController::updateState(State &&state) {
}
}

PanelController::~PanelController() = default;
void PanelController::saveSettings() const {
const auto check = [](const QString &value) {
const auto result = value.endsWith('/')
? value.mid(0, value.size() - 1)
: value;
return (cPlatform() == dbipWindows) ? result.toLower() : result;
};
auto settings = *_settings;
if (check(settings.path) == check(psDownloadPath())) {
settings.path = QString();
}
Local::WriteExportSettings(settings);
}

PanelController::~PanelController() {
if (_saveSettingsTimer.isActive()) {
saveSettings();
}
}

} // namespace View
} // namespace Export
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ For license and copyright information please follow this link:
#include "export/export_controller.h"
#include "export/view/export_view_content.h"
#include "base/unique_qptr.h"
#include "base/timer.h"

class BoxContent;

Expand Down Expand Up @@ -52,7 +53,11 @@ class PanelController {
void showError(const QString &text);
void showCriticalError(const QString &text);

void saveSettings() const;

not_null<ControllerWrap*> _process;
std::unique_ptr<Settings> _settings;
base::Timer _saveSettingsTimer;

base::unique_qptr<Ui::SeparatePanel> _panel;

Expand Down
Loading

0 comments on commit 844d030

Please sign in to comment.