feat(modulo): wire up migration operation

This commit is contained in:
Federico Terzi 2021-06-15 23:27:17 +02:00
parent b47279db63
commit 363a5cf2ef
5 changed files with 153 additions and 93 deletions

View File

@ -91,6 +91,11 @@ typedef struct SearchMetadata {
// WIZARD // WIZARD
const int MIGRATE_RESULT_SUCCESS = 0;
const int MIGRATE_RESULT_CLEAN_FAILURE = 1;
const int MIGRATE_RESULT_DIRTY_FAILURE = 2;
const int MIGRATE_RESULT_UNKNOWN_FAILURE = 3;
typedef struct WizardMetadata { typedef struct WizardMetadata {
const char *version; const char *version;

View File

@ -107,6 +107,11 @@ pub struct SearchMetadata {
pub iconPath: *const ::std::os::raw::c_char, pub iconPath: *const ::std::os::raw::c_char,
} }
pub const WIZARD_MIGRATE_RESULT_SUCCESS: i32 = 0;
pub const WIZARD_MIGRATE_RESULT_CLEAN_FAILURE: i32 = 1;
pub const WIZARD_MIGRATE_RESULT_DIRTY_FAILURE: i32 = 2;
pub const WIZARD_MIGRATE_RESULT_UNKNOWN_FAILURE: i32 = 3;
#[repr(C)] #[repr(C)]
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
pub struct WizardMetadata { pub struct WizardMetadata {

View File

@ -20,10 +20,7 @@
use std::os::raw::{c_char, c_int}; use std::os::raw::{c_char, c_int};
use std::{ffi::CString, sync::Mutex}; use std::{ffi::CString, sync::Mutex};
use crate::{ use crate::{sys::interop::{WIZARD_MIGRATE_RESULT_CLEAN_FAILURE, WIZARD_MIGRATE_RESULT_DIRTY_FAILURE, WIZARD_MIGRATE_RESULT_SUCCESS, WIZARD_MIGRATE_RESULT_UNKNOWN_FAILURE, WizardMetadata}, wizard::{WizardHandlers, WizardOptions}};
sys::interop::WizardMetadata,
wizard::{WizardHandlers, WizardOptions},
};
lazy_static! { lazy_static! {
static ref HANDLERS: Mutex<Option<WizardHandlers>> = Mutex::new(None); static ref HANDLERS: Mutex<Option<WizardHandlers>> = Mutex::new(None);
@ -62,17 +59,16 @@ pub fn show(options: WizardOptions) {
.lock() .lock()
.expect("unable to acquire lock in backup_and_migrate method"); .expect("unable to acquire lock in backup_and_migrate method");
let handlers_ref = (*lock).as_ref().expect("unable to unwrap handlers"); let handlers_ref = (*lock).as_ref().expect("unable to unwrap handlers");
// TODO: if let Some(handler_ref) = handlers_ref.backup_and_migrate.as_ref() {
// if let Some(handler_ref) = handlers_ref.backup_and_migrate.as_ref() { match (*handler_ref)() {
// if (*handler_ref)() { crate::wizard::MigrationResult::Success => WIZARD_MIGRATE_RESULT_SUCCESS,
// 1 crate::wizard::MigrationResult::CleanFailure => WIZARD_MIGRATE_RESULT_CLEAN_FAILURE,
// } else { crate::wizard::MigrationResult::DirtyFailure => WIZARD_MIGRATE_RESULT_DIRTY_FAILURE,
// 0 crate::wizard::MigrationResult::UnknownFailure => WIZARD_MIGRATE_RESULT_UNKNOWN_FAILURE,
// } }
// } else { } else {
// -1 WIZARD_MIGRATE_RESULT_UNKNOWN_FAILURE
// } }
0
} }
extern "C" fn add_to_path() -> c_int { extern "C" fn add_to_path() -> c_int {
@ -188,7 +184,9 @@ pub fn show(options: WizardOptions) {
fn convert_to_cstring_or_null(str: Option<String>) -> (Option<CString>, *const c_char) { fn convert_to_cstring_or_null(str: Option<String>) -> (Option<CString>, *const c_char) {
let c_string = let c_string =
str.map(|str| CString::new(str).expect("unable to convert Option<String> to CString")); str.map(|str| CString::new(str).expect("unable to convert Option<String> to CString"));
let c_ptr = c_string.as_ref().map_or(std::ptr::null(), |path| path.as_ptr()); let c_ptr = c_string
.as_ref()
.map_or(std::ptr::null(), |path| path.as_ptr());
(c_string, c_ptr) (c_string, c_ptr)
} }

View File

@ -35,7 +35,7 @@ const int ADD_PATH_PAGE_INDEX = MIGRATE_PAGE_INDEX + 1;
const int ACCESSIBILITY_PAGE_INDEX = ADD_PATH_PAGE_INDEX + 1; const int ACCESSIBILITY_PAGE_INDEX = ADD_PATH_PAGE_INDEX + 1;
const int MAX_PAGE_INDEX = ACCESSIBILITY_PAGE_INDEX + 1; // Update if a new page is added at the end const int MAX_PAGE_INDEX = ACCESSIBILITY_PAGE_INDEX + 1; // Update if a new page is added at the end
WizardMetadata *metadata= nullptr; WizardMetadata *metadata = nullptr;
// App Code // App Code
@ -45,37 +45,46 @@ public:
virtual bool OnInit(); virtual bool OnInit();
}; };
int find_next_page(int current_index) { int find_next_page(int current_index)
{
int next_index = current_index + 1; int next_index = current_index + 1;
if (next_index >= MAX_PAGE_INDEX) { if (next_index >= MAX_PAGE_INDEX)
{
return -1; return -1;
} }
switch (next_index) { switch (next_index)
case WELCOME_PAGE_INDEX: {
if (metadata->is_welcome_page_enabled) { case WELCOME_PAGE_INDEX:
return WELCOME_PAGE_INDEX; if (metadata->is_welcome_page_enabled)
} {
case MOVE_BUNDLE_PAGE_INDEX: return WELCOME_PAGE_INDEX;
if (metadata->is_move_bundle_page_enabled) { }
return MOVE_BUNDLE_PAGE_INDEX; case MOVE_BUNDLE_PAGE_INDEX:
} if (metadata->is_move_bundle_page_enabled)
case LEGACY_VERSION_PAGE_INDEX: {
if (metadata->is_legacy_version_page_enabled) { return MOVE_BUNDLE_PAGE_INDEX;
return LEGACY_VERSION_PAGE_INDEX; }
} case LEGACY_VERSION_PAGE_INDEX:
case MIGRATE_PAGE_INDEX: if (metadata->is_legacy_version_page_enabled)
if (metadata->is_migrate_page_enabled) { {
return MIGRATE_PAGE_INDEX; return LEGACY_VERSION_PAGE_INDEX;
} }
case ADD_PATH_PAGE_INDEX: case MIGRATE_PAGE_INDEX:
if (metadata->is_add_path_page_enabled) { if (metadata->is_migrate_page_enabled)
return ADD_PATH_PAGE_INDEX; {
} return MIGRATE_PAGE_INDEX;
case ACCESSIBILITY_PAGE_INDEX: }
if (metadata->is_accessibility_page_enabled) { case ADD_PATH_PAGE_INDEX:
return ACCESSIBILITY_PAGE_INDEX; if (metadata->is_add_path_page_enabled)
} {
return ADD_PATH_PAGE_INDEX;
}
case ACCESSIBILITY_PAGE_INDEX:
if (metadata->is_accessibility_page_enabled)
{
return ACCESSIBILITY_PAGE_INDEX;
}
} }
return find_next_page(next_index); return find_next_page(next_index);
@ -84,13 +93,15 @@ int find_next_page(int current_index) {
class DerivedFrame : public WizardFrame class DerivedFrame : public WizardFrame
{ {
protected: protected:
void check_timer_tick( wxTimerEvent& event ); void check_timer_tick(wxTimerEvent &event);
void on_page_changed( wxBookCtrlEvent& event ); void on_page_changed(wxBookCtrlEvent &event);
void welcome_start_clicked(wxCommandEvent &event); void welcome_start_clicked(wxCommandEvent &event);
void migrate_compatibility_mode_clicked( wxCommandEvent& event ); void migrate_button_clicked(wxCommandEvent &event);
void migrate_compatibility_mode_clicked(wxCommandEvent &event);
void navigate_to_next_page_or_close(); void navigate_to_next_page_or_close();
void change_default_button(int target_page); void change_default_button(int target_page);
public: public:
DerivedFrame(wxWindow *parent); DerivedFrame(wxWindow *parent);
}; };
@ -100,7 +111,8 @@ DerivedFrame::DerivedFrame(wxWindow *parent)
{ {
// TODO: load images for accessibility page if on macOS // TODO: load images for accessibility page if on macOS
if (metadata->welcome_image_path) { if (metadata->welcome_image_path)
{
wxBitmap welcomeBitmap = wxBitmap(metadata->welcome_image_path, wxBITMAP_TYPE_PNG); wxBitmap welcomeBitmap = wxBitmap(metadata->welcome_image_path, wxBITMAP_TYPE_PNG);
this->welcome_image->SetBitmap(welcomeBitmap); this->welcome_image->SetBitmap(welcomeBitmap);
} }
@ -109,20 +121,27 @@ DerivedFrame::DerivedFrame(wxWindow *parent)
// Load the first page // Load the first page
int page = find_next_page(-1); int page = find_next_page(-1);
if (page >= 0) { if (page >= 0)
{
this->m_simplebook->SetSelection(page); this->m_simplebook->SetSelection(page);
this->change_default_button(page); this->change_default_button(page);
} else { }
else
{
Close(true); Close(true);
} }
} }
void DerivedFrame::navigate_to_next_page_or_close() { void DerivedFrame::navigate_to_next_page_or_close()
{
int current_page = this->m_simplebook->GetSelection(); int current_page = this->m_simplebook->GetSelection();
int page = find_next_page(current_page); int page = find_next_page(current_page);
if (page >= 0) { if (page >= 0)
{
this->m_simplebook->SetSelection(page); this->m_simplebook->SetSelection(page);
} else { }
else
{
Close(true); Close(true);
} }
} }
@ -132,62 +151,94 @@ void DerivedFrame::welcome_start_clicked(wxCommandEvent &event)
this->navigate_to_next_page_or_close(); this->navigate_to_next_page_or_close();
} }
void DerivedFrame::migrate_compatibility_mode_clicked( wxCommandEvent& event ) { void DerivedFrame::migrate_compatibility_mode_clicked(wxCommandEvent &event)
{
this->navigate_to_next_page_or_close(); this->navigate_to_next_page_or_close();
} }
void DerivedFrame::check_timer_tick( wxTimerEvent& event ) { void DerivedFrame::migrate_button_clicked(wxCommandEvent &event)
if (this->m_simplebook->GetSelection() == LEGACY_VERSION_PAGE_INDEX) { {
if (metadata->is_legacy_version_running) { if (metadata->backup_and_migrate)
if (metadata->is_legacy_version_running() == 0) { {
this->navigate_to_next_page_or_close(); int result = metadata->backup_and_migrate();
} if (result == MIGRATE_RESULT_SUCCESS)
} {
this->navigate_to_next_page_or_close();
}
else if (result == MIGRATE_RESULT_CLEAN_FAILURE)
{
wxMessageBox(wxT("An error occurred during the migration, but your old files were not modified.\n\nPlease run 'espanso log' in a terminal for more information."), wxT("Migration error"), wxICON_ERROR);
}
else if (result == MIGRATE_RESULT_DIRTY_FAILURE)
{
wxMessageBox(wxT("An error occurred during the migration and espanso couldn't complete the process. Some configuration files might be missing, but you'll find the backup in the Documents folder.\n\nPlease run 'espanso log' in a terminal for more information."), wxT("Migration error"), wxICON_ERROR);
}
else if (result == MIGRATE_RESULT_UNKNOWN_FAILURE)
{
wxMessageBox(wxT("An error occurred during the migration.\n\nPlease run 'espanso log' in a terminal for more information."), wxT("Migration error"), wxICON_ERROR);
}
} }
} }
void DerivedFrame::on_page_changed( wxBookCtrlEvent& event ) { void DerivedFrame::check_timer_tick(wxTimerEvent &event)
{
if (this->m_simplebook->GetSelection() == LEGACY_VERSION_PAGE_INDEX)
{
if (metadata->is_legacy_version_running)
{
if (metadata->is_legacy_version_running() == 0)
{
this->navigate_to_next_page_or_close();
}
}
}
}
void DerivedFrame::on_page_changed(wxBookCtrlEvent &event)
{
int current_page = this->m_simplebook->GetSelection(); int current_page = this->m_simplebook->GetSelection();
this->change_default_button(current_page); this->change_default_button(current_page);
} }
void DerivedFrame::change_default_button(int target_page) { void DerivedFrame::change_default_button(int target_page)
switch (target_page) { {
case WELCOME_PAGE_INDEX: switch (target_page)
{ {
this->welcome_start_button->SetDefault(); case WELCOME_PAGE_INDEX:
break; {
} this->welcome_start_button->SetDefault();
case MOVE_BUNDLE_PAGE_INDEX: break;
{ }
this->move_bundle_quit_button->SetDefault(); case MOVE_BUNDLE_PAGE_INDEX:
break; {
} this->move_bundle_quit_button->SetDefault();
case MIGRATE_PAGE_INDEX: break;
{ }
this->migrate_backup_and_migrate_button->SetDefault(); case MIGRATE_PAGE_INDEX:
break; {
} this->migrate_backup_and_migrate_button->SetDefault();
case ADD_PATH_PAGE_INDEX: break;
{ }
this->add_path_continue_button->SetDefault(); case ADD_PATH_PAGE_INDEX:
break; {
} this->add_path_continue_button->SetDefault();
case ACCESSIBILITY_PAGE_INDEX: break;
{ }
this->accessibility_enable_button->SetDefault(); case ACCESSIBILITY_PAGE_INDEX:
break; {
} this->accessibility_enable_button->SetDefault();
break;
}
} }
} }
bool WizardApp::OnInit() bool WizardApp::OnInit()
{ {
wxInitAllImageHandlers(); wxInitAllImageHandlers();
DerivedFrame *frame = new DerivedFrame(NULL); DerivedFrame *frame = new DerivedFrame(NULL);
if (metadata->window_icon_path) { if (metadata->window_icon_path)
{
setFrameIcon(metadata->window_icon_path, frame); setFrameIcon(metadata->window_icon_path, frame);
} }
@ -198,7 +249,7 @@ bool WizardApp::OnInit()
return true; return true;
} }
extern "C" void interop_show_wizard(WizardMetadata * _metadata) extern "C" void interop_show_wizard(WizardMetadata *_metadata)
{ {
// Setup high DPI support on Windows // Setup high DPI support on Windows
#ifdef __WXMSW__ #ifdef __WXMSW__

View File

@ -50,4 +50,5 @@ pub enum MigrationResult {
Success, Success,
CleanFailure, CleanFailure,
DirtyFailure, DirtyFailure,
UnknownFailure,
} }