* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
-use std::ffi::{OsStr};
+
use std::mem;
use std::path::{Path, PathBuf};
use std::rc::Rc;
use ui_rs::ui::*;
use crate::AppStates;
use crate::note::NoteViewModel;
-use crate::window::NoteTypeTabView;
+use crate::window::{FileNotePreviewTabView, NoteTypeTabView};
#[derive(UiModel)]
pub struct FileNote {
pub id: NoteId,
pub nodename: String,
+ pub content_type: String,
pub local_path: Option<PathBuf>,
pub is_tmp_file: bool,
id,
nodename: String::default(),
+ content_type: String::default(),
local_path: None,
is_tmp_file: false,
is_opening: false,
let doc: UiDoc<FileNote> = UiDoc::new_from_box2(Box::new(self), |n,d| {
n.doc = d.doc_ref();
n.nodename = model.nodename.clone();
+ n.content_type = model.content_type.clone();
n.note_type.set(NoteTypeTabView::File as i64);
n.note_nodename.set(model.nodename.as_str());
doc_cast!(doc, dyn NoteViewModel)
}
+ fn set_preview(&mut self, path: &Path) {
+ const WEBVIEW_TYPES: &[&str] = &[
+ "text/html",
+ "application/pdf",
+ ];
+
+ if self.content_type.starts_with("image/") {
+ let result = self.image.load_image_file(path);
+ if result.is_ok() {
+ self.preview_tab.set(FileNotePreviewTabView::ImageViewer as i64);
+ return
+ }
+ self.preview_tab.set(crate::window::FileNotePreviewTabView::ImageViewer as i64);
+ } else if WEBVIEW_TYPES.contains(&self.content_type.as_str()) {
+ let url = Url::from_file_path(&path);
+ if let Ok(url) = url {
+ self.webview.load_url(url.as_str());
+ self.preview_tab.set(crate::window::FileNotePreviewTabView::WebView as i64);
+ return
+ }
+ }
+
+ self.preview_tab.set(crate::window::FileNotePreviewTabView::Empty as i64);
+ }
+
fn query_local_path(&mut self) {
let Some(doc) = self.doc.get_doc() else {
return;
note.is_opening = false;
match result {
OpenExternResult::Ok((path, istmp)) => {
- // TODO: implement better file type detection
- // idealy save content type in the note
- match path.extension().and_then(OsStr::to_str) {
- Some("png" | "jpeg" | "jpg" | "webp") => {
- let result = note.image.load_image_file(path.as_path());
- if result.is_ok() {
- // TODO: replace 1 with enum value
- note.preview_tab.set(1);
- }
- },
- Some("html" | "htm" | "pdf") => {
- let url = Url::from_file_path(&path);
- if let Ok(url) = url {
- note.webview.load_url(url.as_str());
- // TODO: replace 2 with enum value
- note.preview_tab.set(2);
- }
- }
- _ => {}
- }
-
+ note.set_preview(&path);
note.local_path = Some(path);
note.is_tmp_file = istmp;
}
use std::rc::Rc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::SystemTime;
-use sea_orm::{Iden, NotSet, Set};
+use sea_orm::{NotSet, Set};
use sea_orm::sea_query::prelude::Utc;
use backend::backend::{BackendHandle, BroadcastMessage, NoteContentRet, NoteId, NoteTitleUpdate, SaveNoteResult};
use backend::lockmanager::NoteLock;