Added hotkey backup.

This commit is contained in:
casey langen 2018-06-03 20:48:31 -07:00
parent 4a22c48293
commit a04a626178
6 changed files with 96 additions and 4 deletions

View File

@ -318,4 +318,18 @@ namespace musik { namespace core {
#endif
}
bool CopyFile(const std::string& from, const std::string& to) {
if (from.size() && to.size() && from != to) {
std::ifstream in(from);
if (in.is_open()) {
std::ofstream out(to);
if (out.is_open()) {
out << in.rdbuf();
return true;
}
}
}
return false;
}
} }

View File

@ -46,6 +46,7 @@ namespace musik { namespace core {
std::string GetPluginDirectory();
std::string NormalizeDir(std::string path);
void OpenFile(const std::string& path);
bool CopyFile(const std::string& from, const std::string& to);
int64_t Checksum(char *data,unsigned int bytes);
size_t CopyString(const std::string& src, char* dst, size_t size);
void ReplaceAll(std::string& input, const std::string& find, const std::string& replace);

View File

@ -42,6 +42,8 @@
#include <app/model/HotkeysAdapter.h>
#include <app/overlay/ReassignHotkeyOverlay.h>
#include <app/util/Messages.h>
#include <ctime>
#include <time.h>
using namespace cursespp;
using namespace musik::cube;
@ -49,6 +51,16 @@ using namespace musik::core;
using Entry = IScrollAdapter::EntryPtr;
static std::string formattedTime() {
char buffer[128];
time_t rawtime;
struct tm* timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
strftime(buffer, sizeof(buffer), "%F-%H-%M-%S", timeinfo);
return std::string(buffer);
}
static void confirmResetHotkeys() {
std::shared_ptr<DialogOverlay> dialog(new DialogOverlay());
@ -106,6 +118,39 @@ static void checkConflictAndSave(Hotkeys::Id id, const std::string& key, std::fu
}
}
static void backupAndShowDialog() {
std::string dir = NormalizeDir(GetDataDirectory());
std::string in = dir + "hotkeys.json";
std::string out = dir + "hotkeys-" + formattedTime() + ".json";
if (CopyFile(in, out)) {
std::shared_ptr<DialogOverlay> dialog(new DialogOverlay());
std::string message = _TSTR("hotkeys_backup_success_message");
ReplaceAll(message, "{{path}}", out);
(*dialog)
.SetTitle(_TSTR("hotkeys_backup_success_title"))
.SetMessage(message)
.AddButton("KEY_ENTER", "ENTER", _TSTR("button_ok"));
App::Overlays().Push(dialog);
}
else {
std::shared_ptr<DialogOverlay> dialog(new DialogOverlay());
std::string message = _TSTR("hotkeys_backup_failure_message");
ReplaceAll(message, "{{path}}", dir);
(*dialog)
.SetTitle(_TSTR("hotkeys_backup_failure_title"))
.SetMessage(message)
.AddButton("KEY_ENTER", "ENTER", _TSTR("button_ok"));
App::Overlays().Push(dialog);
}
}
HotkeysLayout::HotkeysLayout() {
auto adapter = std::make_shared<HotkeysAdapter>();
@ -146,9 +191,22 @@ void HotkeysLayout::SetShortcutsWindow(ShortcutsWindow* shortcuts) {
if (shortcuts) {
shortcuts->RemoveAll();
shortcuts->AddShortcut("M-r", _TSTR("hotkeys_reset_defaults"));
shortcuts->AddShortcut(Hotkeys::Get(Hotkeys::NavigateLibrary), _TSTR("shortcuts_library"));
shortcuts->AddShortcut(Hotkeys::Get(Hotkeys::NavigateSettings), _TSTR("shortcuts_settings"));
shortcuts->AddShortcut(
Hotkeys::Get(Hotkeys::HotkeysResetToDefault),
_TSTR("hotkeys_reset_defaults"));
shortcuts->AddShortcut(
Hotkeys::Get(Hotkeys::HotkeysBackup),
_TSTR("hotkeys_backup"));
shortcuts->AddShortcut(
Hotkeys::Get(Hotkeys::NavigateLibrary),
_TSTR("shortcuts_library"));
shortcuts->AddShortcut(
Hotkeys::Get(Hotkeys::NavigateSettings),
_TSTR("shortcuts_settings"));
shortcuts->AddShortcut("^D", _TSTR("shortcuts_quit"));
shortcuts->SetChangedCallback([this](std::string key) {
@ -158,11 +216,15 @@ void HotkeysLayout::SetShortcutsWindow(ShortcutsWindow* shortcuts) {
}
bool HotkeysLayout::KeyPress(const std::string& kn) {
if (kn == "M-r") {
if (Hotkeys::Is(Hotkeys::HotkeysResetToDefault, kn)) {
confirmResetHotkeys();
this->listWindow->OnAdapterChanged();
return true;
}
else if (Hotkeys::Is(Hotkeys::HotkeysBackup, kn)) {
backupAndShowDialog();
return true;
}
else if (Hotkeys::Is(Hotkeys::NavigateSettings, kn)) {
this->BroadcastMessage(message::JumpToSettings);
return true;

View File

@ -116,6 +116,9 @@ static std::unordered_map<std::string, Id> NAME_TO_ID = {
{ "metadata_rescan", Id::RescanMetadata },
{ "hotkeys_reset_to_default", Id::HotkeysResetToDefault },
{ "hotkeys_backup", Id::HotkeysBackup },
{ "context_menu", Id::ContextMenu }
};
@ -194,6 +197,9 @@ static std::unordered_map<Id, std::string, EnumHasher> ID_TO_DEFAULT = {
{ Id::RescanMetadata, "^R"},
{ Id::HotkeysResetToDefault, "M-r" },
{ Id::HotkeysBackup, "M-b" },
{ Id::ContextMenu, "M-enter" }
};

View File

@ -109,6 +109,10 @@ namespace musik {
/* indexer */
RescanMetadata,
/* hotkeys */
HotkeysResetToDefault,
HotkeysBackup,
/* general */
ContextMenu,

View File

@ -39,11 +39,16 @@
"hotkeys_title": "hotkey configuration",
"hotkeys_reset_defaults": "reset all",
"hotkeys_backup": "backup",
"hotkeys_reassign_overlay_title": "reassign hotkey",
"hotkeys_reset_all_title": "reset hotkeys",
"hotkeys_reset_all_message": "are you sure you want to reset all hotkeys to their default values?\n\nexisting custom hotkeys will be forgotten!",
"hotkeys_conflict_title": "hotkey conflict",
"hotkeys_conflict_message": "the hotkey '{{hotkey}}' *may* conflict with '{{existing}}', depending on context.\n\nare you sure you want to assign this binding?",
"hotkeys_backup_success_title": "backup succeeded",
"hotkeys_backup_success_message": "hotkeys were backed up to the following location:\n\n{{path}}",
"hotkeys_backup_failure_title": "backup failed",
"hotkeys_backup_failure_message": "hotkey backup was *NOT* successful! please make sure you have write permission to the following directory:\n\n{{path}}",
"settings_space_to_add": "browse (SPACE to add)",
"settings_backspace_to_remove": "indexed paths (BACKSPACE to remove)",