use std::io::Error;
use std::path::{Path, PathBuf};
use std::pin::Pin;
-use sea_orm::{ActiveModelTrait, Database, DatabaseConnection, EntityTrait, QueryFilter, ColumnTrait, Set, QueryOrder, DbErr, ExprTrait};
+use sea_orm::{ActiveModelTrait, Database, DatabaseConnection, EntityTrait, QueryFilter, ColumnTrait, Set, QueryOrder, DbErr, ExprTrait, UpdateResult};
use tokio::runtime::Runtime;
use std::sync::{Arc};
use std::thread::JoinHandle;
let _ = self.tx.send(cmd);
}
+ pub fn rename_note<F>(&self, note_id: i32, nodename: String, callback: F)
+ where F: FnOnce(Result<UpdateResult, DbErr>) + Send + 'static {
+ let bhandle = self.clone();
+ let cmd = Box::pin(async move {
+ let local_path_res = get_note_storage_path(&bhandle.backend.db, note_id).await;
+ let local_path = match local_path_res {
+ Ok(path) => path,
+ Err(e) => {
+ callback(Err(e));
+ return;
+ }
+ };
+
+ let result = Note::update_many()
+ .col_expr(Column::Nodename, Expr::value(nodename.clone()))
+ .filter(Column::NoteId.eq(note_id))
+ .exec(&bhandle.backend.db).await;
+
+ if let Some(path_str) = local_path {
+ let path = Path::new(&path_str);
+ let parent = path.parent();
+ if let Some(parent) = parent {
+ let new_path = parent.join(nodename);
+ let result = fs::rename(path, &new_path).await;
+ if let Err(e) = result {
+ callback(Err(DbErr::Custom(format!("rename failed: {}", e))));
+ // TODO: rollback nodename update
+ return;
+ }
+ } // else: should not happen
+ }
+
+ callback(result);
+ });
+ let _ = self.tx.send(cmd);
+ }
+
pub fn get_file_metadata<F>(&self, path: &Path, callback: F)
where F: FnOnce(std::io::Result<Metadata>) + Send + 'static {
let p = PathBuf::from(path);
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
+use std::mem;
use std::path::PathBuf;
use std::rc::Rc;
use backend::backend::{BackendHandle, NoteId, OpenExternResult};
self.save_note();
}
- pub fn save_note(&mut self) {
- // TODO
- }
-
#[action]
pub fn notebook_view_changed(&mut self, event: &mut ActionEvent) {
// TODO: duplicated code in note.rs
impl NoteViewModel for FileNote {
fn save_note(&mut self) {
+ let Some(doc) = self.doc.get_doc() else {
+ return; // should not happen
+ };
+ let NoteId::Id(note_id) = self.id else {
+ return; // should not happen
+ };
+
+ let mut nodename = self.note_nodename.get();
+ if self.nodename == nodename {
+ // nothing has changed
+ return;
+ }
+ mem::swap(&mut self.nodename, &mut nodename);
+
+ let proxy = doc.doc_proxy();
+ self.backend.rename_note(note_id, self.nodename.clone(), |result|{
+ proxy.call_mainthread(move |_doc, note|{
+ match result {
+ Ok(updated) => {
+ if updated.rows_affected != 1 {
+ println!("rename_note: unexpected number of rows affected");
+ }
+ },
+ Err(e) => {
+ println!("note update failed: {:?}", e);
+ note.nodename = nodename; // restore previous nodename
+ }
+ }
+ });
+ });
}
}
\ No newline at end of file