[niepce/lr-import: 17/20] npc-fwk: Remove gio from files




commit f917f00aa437137c71c4b45e58577ceb1f0fc2c4
Author: Hubert Figuière <hub figuiere net>
Date:   Mon Dec 13 22:38:39 2021 -0500

    npc-fwk: Remove gio from files

 crates/npc-fwk/src/toolkit/mimetype.rs    |  2 +-
 crates/npc-fwk/src/utils/files.rs         | 72 ++++++++++++++-----------------
 src/engine/importer/directoryimporter.cpp |  9 +---
 src/fwk/utils/files.cpp                   | 19 ++++----
 src/fwk/utils/files.hpp                   |  8 ++--
 src/fwk/utils/modulemanager.cpp           |  8 ++--
 6 files changed, 51 insertions(+), 67 deletions(-)
---
diff --git a/crates/npc-fwk/src/toolkit/mimetype.rs b/crates/npc-fwk/src/toolkit/mimetype.rs
index b08531b..344ecb6 100644
--- a/crates/npc-fwk/src/toolkit/mimetype.rs
+++ b/crates/npc-fwk/src/toolkit/mimetype.rs
@@ -53,7 +53,7 @@ pub fn guess_type(gmtype: &str) -> MType {
 }
 
 /// Guess the type from a file
-fn guess_type_for_file<P: AsRef<Path>>(p: P) -> MType {
+pub fn guess_type_for_file<P: AsRef<Path>>(p: P) -> MType {
     let path = p.as_ref();
     let guess = mime_guess::from_path(path);
     if !guess.is_empty() {
diff --git a/crates/npc-fwk/src/utils/files.rs b/crates/npc-fwk/src/utils/files.rs
index a1ba27f..12f92fb 100644
--- a/crates/npc-fwk/src/utils/files.rs
+++ b/crates/npc-fwk/src/utils/files.rs
@@ -19,50 +19,41 @@
 
 use libc::c_char;
 use std::ffi::{CStr, CString};
+use std::os::unix::ffi::OsStrExt;
 use std::path::{Path, PathBuf};
 
-use gio::prelude::*;
-use glib::translate::*;
-
-use crate::toolkit::mimetype::{guess_type, MType};
+use crate::toolkit::mimetype::{guess_type_for_file, MType};
 
 #[derive(Clone, Default)]
 pub struct FileList(pub Vec<PathBuf>);
 
 impl FileList {
+    /// Get files from directory P, possibly filtered by F.
     pub fn get_files_from_directory<P, F>(dir: P, filter: F) -> Self
     where
         P: AsRef<Path>,
-        F: Fn(&gio::FileInfo) -> bool + 'static,
+        F: Fn(&Path) -> bool + 'static,
     {
         let mut l = FileList::default();
-
-        let dir = gio::File::for_path(dir);
-        let dir_path = dir.path();
-        if dir_path.is_none() {
-            err_out!("Couldn't get dir path");
+        if !dir.as_ref().is_dir() {
+            err_out!("Not a directory: {:?}", dir.as_ref());
             return l;
         }
-        let dir_path = dir_path.unwrap();
-        if let Ok(enumerator) =
-            dir.enumerate_children("*", gio::FileQueryInfoFlags::NONE, gio::NONE_CANCELLABLE)
-        {
-            for itr in enumerator.into_iter() {
-                if itr.is_err() {
-                    err_out!("Enumeration failed: {:?}", itr.err());
+        if let Ok(read_dir) = std::fs::read_dir(dir) {
+            for entry in read_dir {
+                if entry.is_err() {
+                    err_out!("Enumeration failed: {:?}", entry.err());
                     continue;
                 }
-                let finfo = itr.unwrap();
-                let ftype = finfo.file_type();
-                if ftype == gio::FileType::Regular || ftype == gio::FileType::SymbolicLink {
-                    if !filter(&finfo) {
-                        err_out!("Filtered out");
-                        continue;
+                let entry = entry.unwrap();
+                if let Ok(ftype) = entry.file_type() {
+                    if ftype.is_file() || ftype.is_symlink() {
+                        if !filter(&entry.path()) {
+                            dbg_out!("Filtered out");
+                            continue;
+                        }
+                        l.0.push(entry.path());
                     }
-                    let name = finfo.name();
-                    let fullname = glib::build_filenamev(&[&dir_path, &name]);
-                    dbg_out!("Found file {:?}", &fullname);
-                    l.0.push(fullname);
                 }
             }
         }
@@ -71,14 +62,9 @@ impl FileList {
         l
     }
 
-    pub fn file_is_media(fileinfo: &gio::FileInfo) -> bool {
-        if let Some(gmtype) = fileinfo.content_type() {
-            let t = guess_type(&gmtype);
-            return matches!(t, MType::Image(_) | MType::Movie);
-        }
-
-        err_out!("Coudln't get file type");
-        false
+    pub fn file_is_media(fileinfo: &Path) -> bool {
+        let t = guess_type_for_file(fileinfo);
+        return matches!(t, MType::Image(_) | MType::Movie);
     }
 }
 
@@ -87,8 +73,9 @@ impl FileList {
 /// # Safety
 /// Dereference the finfo pointer.
 #[no_mangle]
-pub unsafe extern "C" fn fwk_file_is_media(finfo: *mut gio_sys::GFileInfo) -> bool {
-    let fileinfo = gio::FileInfo::from_glib_none(finfo);
+pub unsafe extern "C" fn fwk_file_is_media(file: *const c_char) -> bool {
+    let cfile = CStr::from_ptr(file);
+    let fileinfo = PathBuf::from(std::ffi::OsStr::from_bytes(cfile.to_bytes()));
     FileList::file_is_media(&fileinfo)
 }
 
@@ -113,7 +100,7 @@ pub unsafe extern "C" fn fwk_file_list_delete(l: *mut FileList) {
 #[no_mangle]
 pub unsafe extern "C" fn fwk_file_list_get_files_from_directory(
     dir: *const c_char,
-    filter: Option<extern "C" fn(*mut gio_sys::GFileInfo) -> bool>,
+    filter: Option<extern "C" fn(*const c_char) -> bool>,
 ) -> *mut FileList {
     let cstr = CStr::from_ptr(dir);
     match filter {
@@ -121,7 +108,14 @@ pub unsafe extern "C" fn fwk_file_list_get_files_from_directory(
             let f = Box::new(filter);
             Box::into_raw(Box::new(FileList::get_files_from_directory(
                 &PathBuf::from(&*cstr.to_string_lossy()),
-                move |finfo| f(finfo.to_glib_none().0),
+                move |p| {
+                    if let Ok(pc) = CString::new(p.as_os_str().as_bytes()) {
+                        f(pc.as_ptr())
+                    } else {
+                        err_out!("file path conversion failed.");
+                        false
+                    }
+                },
             )))
         }
         None => Box::into_raw(Box::new(FileList::get_files_from_directory(
diff --git a/src/engine/importer/directoryimporter.cpp b/src/engine/importer/directoryimporter.cpp
index 2a35ddd..d79eb10 100644
--- a/src/engine/importer/directoryimporter.cpp
+++ b/src/engine/importer/directoryimporter.cpp
@@ -2,7 +2,7 @@
 /*
  * niepce - engine/importer/directoryimporter.cpp
  *
- * Copyright (C) 2014-2020 Hubert Figuière
+ * Copyright (C) 2014-2021 Hubert Figuière
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -62,17 +62,12 @@ const std::string& DirectoryImporter::id() const
     return _id;
 }
 
-static bool filter_only_media(GFileInfo* info)
-{
-    return fwk::filter_only_media(Glib::wrap(info, true));
-}
-
 bool DirectoryImporter::list_source_content(const std::string & source,
                                             const SourceContentReady& callback)
 {
     auto files =
         fwk::wrapFileList(ffi::fwk_file_list_get_files_from_directory(
-                              source.c_str(), &filter_only_media));
+                              source.c_str(), &fwk::filter_only_media));
     DBG_OUT("files size: %lu", ffi::fwk_file_list_size(files.get()));
     std::list<ImportedFilePtr> content;
     for (size_t i = 0; i < ffi::fwk_file_list_size(files.get()); i++)
diff --git a/src/fwk/utils/files.cpp b/src/fwk/utils/files.cpp
index a4971d9..119295f 100644
--- a/src/fwk/utils/files.cpp
+++ b/src/fwk/utils/files.cpp
@@ -2,7 +2,7 @@
 /*
  * niepce - fwk/utils/files.cpp
  *
- * Copyright (C) 2007-2018 Hubert Figuiere
+ * Copyright (C) 2007-2021 Hubert Figuière
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -53,9 +53,12 @@ bool filter_none(const Glib::RefPtr<Gio::FileInfo> & )
 }
 
 
-bool filter_ext(const Glib::RefPtr<Gio::FileInfo> & file, const std::string & ext)
+bool filter_ext(const char* file, const std::string & ext)
 {
-    std::string file_ext = fwk::path_extension(file->get_name());
+    if (file == nullptr) {
+        return false;
+    }
+    std::string file_ext = file;
     boost::to_lower(file_ext);
     if(file_ext == ext) {
         return false;
@@ -63,15 +66,9 @@ bool filter_ext(const Glib::RefPtr<Gio::FileInfo> & file, const std::string & ex
     return true;
 }
 
-bool filter_xmp_out(const Glib::RefPtr<Gio::FileInfo> & file)
-{
-    static const std::string ext(".xmp");
-    return filter_ext(file, ext);
-}
-
-bool filter_only_media(const Glib::RefPtr<Gio::FileInfo> & file)
+bool filter_only_media(const char* file)
 {
-    return ffi::fwk_file_is_media(file->gobj());
+    return ffi::fwk_file_is_media(file);
 }
 
 
diff --git a/src/fwk/utils/files.hpp b/src/fwk/utils/files.hpp
index 1d3be81..6a65f43 100644
--- a/src/fwk/utils/files.hpp
+++ b/src/fwk/utils/files.hpp
@@ -2,7 +2,7 @@
 /*
  * niepce - fwk/utils/files.hpp
  *
- * Copyright (C) 2007-2018 Hubert Figuiere
+ * Copyright (C) 2007-2021 Hubert Figuière
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -37,10 +37,8 @@ namespace fwk {
 std::string make_tmp_dir(const std::string& base);
 
 bool filter_none(const Glib::RefPtr<Gio::FileInfo> & file);
-bool filter_ext(const Glib::RefPtr<Gio::FileInfo> & file,
-                const std::string & ext);
-bool filter_xmp_out(const Glib::RefPtr<Gio::FileInfo> & file);
-bool filter_only_media(const Glib::RefPtr<Gio::FileInfo> & file);
+bool filter_ext(const char* file, const std::string & ext);
+bool filter_only_media(const char* file);
 
 typedef std::shared_ptr<ffi::FileList> FileListPtr;
 
diff --git a/src/fwk/utils/modulemanager.cpp b/src/fwk/utils/modulemanager.cpp
index 0199482..0754265 100644
--- a/src/fwk/utils/modulemanager.cpp
+++ b/src/fwk/utils/modulemanager.cpp
@@ -3,8 +3,8 @@
  * copied from
  * gnote
  *
- * Copyright (C) 2009-2017 Hubert Figuiere
- * 
+ * Copyright (C) 2009-2021 Hubert Figuière
+ *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
  * to deal in the Software without restriction, including without limitation
@@ -58,9 +58,9 @@ namespace fwk {
   }
 
 
-  static bool filter(GFileInfo* f)
+  static bool filter(const char* f)
   {
-    return fwk::filter_ext(Glib::wrap(f, true), std::string(".") + G_MODULE_SUFFIX);
+    return fwk::filter_ext(f, std::string(".") + G_MODULE_SUFFIX);
   }
 
   void ModuleManager::load_modules()


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]