Skip to content

Commit c9f02ea

Browse files
committed
Move webview path logic into Rust, remove unneeded code.
1 parent 91aed8c commit c9f02ea

File tree

5 files changed

+40
-32
lines changed

5 files changed

+40
-32
lines changed

app/config_manager.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,6 @@ void config_manager::remove_document_history(const wxString& path) {
8989
if (is_initialized()) config_manager_remove_document_history(backend_mut(), to_utf8(path));
9090
}
9191

92-
void config_manager::remove_navigation_history(const wxString& path) {
93-
if (is_initialized()) config_manager_remove_navigation_history(backend_mut(), to_utf8(path));
94-
}
95-
9692
bool config_manager::get_document_opened(const wxString& path) const {
9793
return is_initialized() ? config_manager_get_document_opened(backend_ref(), to_utf8(path)) : false;
9894
}

app/config_manager.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ class config_manager {
7676
long get_document_position(const wxString& path) const;
7777
void set_document_opened(const wxString& path, bool opened);
7878
void remove_document_history(const wxString& path);
79-
void remove_navigation_history(const wxString& path);
8079
bool get_document_opened(const wxString& path) const;
8180
wxArrayString get_all_opened_documents() const;
8281
wxArrayString get_all_documents() const;

app/main_window.cpp

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -774,23 +774,12 @@ void main_window::on_open_in_webview(wxCommandEvent&) {
774774
if (text_ctrl == nullptr) return;
775775
const long pos = text_ctrl->GetInsertionPoint();
776776
try {
777-
rust::String section_path = session_get_current_section_path(*tab->session_doc->session, pos);
777+
const std::string temp_dir_utf8 = std::string(wxStandardPaths::Get().GetTempDir().ToUTF8());
778+
auto target = session_webview_target(*tab->session_doc->session, pos, temp_dir_utf8);
778779
wxString url_to_load;
779-
wxString section_path_wx = wxString::FromUTF8(section_path.c_str());
780-
wxString temp_base = wxStandardPaths::Get().GetTempDir();
781-
size_t path_hash = std::hash<std::string>{}(std::string(tab->file_path.ToUTF8().data()));
782-
wxString doc_temp_dir = temp_base + wxFileName::GetPathSeparator() + "paperback_" + std::to_string(path_hash);
783-
if (!wxDirExists(doc_temp_dir)) wxFileName::Mkdir(doc_temp_dir, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL);
784-
if (!section_path.empty()) {
785-
wxFileName temp_file(doc_temp_dir, wxFileName(section_path_wx).GetFullName());
786-
bool success = session_extract_resource(*tab->session_doc->session, section_path, temp_file.GetFullPath().ToStdString());
787-
if (success) url_to_load = wxFileSystem::FileNameToURL(temp_file);
788-
}
789-
if (url_to_load.IsEmpty()) {
790-
wxFileName fn(tab->file_path);
791-
wxString ext = fn.GetExt().Lower();
792-
if (ext == "html" || ext == "htm" || ext == "xhtml" || ext == "md" || ext == "markdown")
793-
url_to_load = wxFileSystem::FileNameToURL(fn);
780+
if (target.found) {
781+
const wxString path_wx = wxString::FromUTF8(target.path.c_str());
782+
url_to_load = wxFileSystem::FileNameToURL(path_wx);
794783
}
795784
auto navigation_handler = [](const wxString& url) -> bool {
796785
if (url.Lower().StartsWith("http://") || url.Lower().StartsWith("https://") || url.Lower().StartsWith("mailto:")) {

lib/src/bridge.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,11 @@ pub mod ffi {
264264
pub total_chars: i64,
265265
}
266266

267+
pub struct FfiWebViewTarget {
268+
pub found: bool,
269+
pub path: String,
270+
}
271+
267272
extern "Rust" {
268273
type ConfigManager;
269274
type DocumentSession;
@@ -297,7 +302,6 @@ pub mod ffi {
297302
fn config_manager_set_document_opened(manager: &mut ConfigManager, path: &str, opened: bool);
298303
fn config_manager_get_document_opened(manager: &ConfigManager, path: &str) -> bool;
299304
fn config_manager_remove_document_history(manager: &mut ConfigManager, path: &str);
300-
fn config_manager_remove_navigation_history(manager: &mut ConfigManager, path: &str);
301305
fn config_manager_get_all_opened_documents(manager: &ConfigManager) -> Vec<String>;
302306
fn config_manager_get_all_documents(manager: &ConfigManager) -> Vec<String>;
303307
fn config_manager_add_bookmark(manager: &mut ConfigManager, path: &str, start: i64, end: i64, note: &str);
@@ -486,8 +490,7 @@ pub mod ffi {
486490
fn session_history_go_forward(session: &mut DocumentSession, current_pos: i64) -> FfiSessionNavResult;
487491
fn session_activate_link(session: &mut DocumentSession, position: i64) -> FfiLinkActivationResult;
488492
fn session_get_table_at_position(session: &DocumentSession, position: i64) -> String;
489-
fn session_get_current_section_path(session: &DocumentSession, position: i64) -> String;
490-
fn session_extract_resource(session: &DocumentSession, resource_path: &str, output_path: &str) -> Result<bool>;
493+
fn session_webview_target(session: &DocumentSession, position: i64, temp_dir: &str) -> FfiWebViewTarget;
491494
fn session_get_status_info(session: &DocumentSession, position: i64) -> FfiStatusInfo;
492495
fn session_stats(session: &DocumentSession) -> FfiDocumentStats;
493496
fn session_page_count(session: &DocumentSession) -> usize;
@@ -593,7 +596,6 @@ ffi_wrapper!(config_manager_get_document_position, get_document_position(path: &
593596
ffi_wrapper!(mut config_manager_set_document_opened, set_document_opened(path: &str, opened: bool));
594597
ffi_wrapper!(config_manager_get_document_opened, get_document_opened(path: &str) -> bool);
595598
ffi_wrapper!(mut config_manager_remove_document_history, remove_document_history(path: &str));
596-
ffi_wrapper!(mut config_manager_remove_navigation_history, remove_navigation_history(path: &str));
597599
ffi_wrapper!(config_manager_get_all_opened_documents, get_all_opened_documents -> Vec<String>);
598600
ffi_wrapper!(config_manager_get_all_documents, get_all_documents -> Vec<String>);
599601

@@ -1091,12 +1093,9 @@ fn session_get_table_at_position(session: &DocumentSession, position: i64) -> St
10911093
session.get_table_at_position(position).unwrap_or_default()
10921094
}
10931095

1094-
fn session_get_current_section_path(session: &DocumentSession, position: i64) -> String {
1095-
session.get_current_section_path(position).unwrap_or_default()
1096-
}
1097-
1098-
fn session_extract_resource(session: &DocumentSession, resource_path: &str, output_path: &str) -> Result<bool, String> {
1099-
session.extract_resource(resource_path, output_path).map_err(|e| e.to_string())
1096+
fn session_webview_target(session: &DocumentSession, position: i64, temp_dir: &str) -> ffi::FfiWebViewTarget {
1097+
let path = session.webview_target_path(position, temp_dir);
1098+
ffi::FfiWebViewTarget { found: path.is_some(), path: path.unwrap_or_default() }
11001099
}
11011100

11021101
fn session_get_status_info(session: &DocumentSession, position: i64) -> ffi::FfiStatusInfo {

lib/src/session.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use std::{
2-
fs::File,
2+
fs::{self, File},
33
io::{BufReader, Write},
44
path::Path,
55
};
66

7+
use sha1::{Digest, Sha1};
78
use zip::ZipArchive;
89

910
use crate::{
@@ -561,6 +562,30 @@ impl DocumentSession {
561562
Some(marker.reference.clone())
562563
}
563564

565+
#[must_use]
566+
pub fn webview_target_path(&self, position: i64, temp_dir: &str) -> Option<String> {
567+
let section_path = self.get_current_section_path(position).filter(|path| !path.is_empty());
568+
if let Some(section_path) = section_path {
569+
let mut hasher = Sha1::new();
570+
hasher.update(self.file_path.as_bytes());
571+
let hash = format!("{:x}", hasher.finalize());
572+
let doc_temp_dir = Path::new(temp_dir).join(format!("paperback_{hash}"));
573+
if fs::create_dir_all(&doc_temp_dir).is_ok() {
574+
let file_name = Path::new(&section_path).file_name()?.to_string_lossy().to_string();
575+
let output_path = doc_temp_dir.join(file_name);
576+
let output_str = output_path.to_string_lossy().to_string();
577+
if self.extract_resource(&section_path, &output_str).ok() == Some(true) {
578+
return Some(output_str);
579+
}
580+
}
581+
}
582+
let ext = Path::new(&self.file_path).extension().map(|ext| ext.to_string_lossy().to_ascii_lowercase());
583+
match ext.as_deref() {
584+
Some("html") | Some("htm") | Some("xhtml") | Some("md") | Some("markdown") => Some(self.file_path.clone()),
585+
_ => None,
586+
}
587+
}
588+
564589
/// Extracts a resource to the given output path.
565590
///
566591
/// # Errors

0 commit comments

Comments
 (0)