[niepce] Create the directory for the thumbnail cache if needed
- From: Hubert FiguiÃre <hub src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [niepce] Create the directory for the thumbnail cache if needed
- Date: Sat, 12 Nov 2011 06:17:20 +0000 (UTC)
commit f8d2d16d1c9951346cc59ad5c0d759170da104f5
Author: Hub Figuiere <hub figuiere net>
Date: Tue Nov 8 07:58:39 2011 -0800
Create the directory for the thumbnail cache if needed
src/engine/library/thumbnailcache.cpp | 14 ++++++++++----
src/engine/library/thumbnailcache.hpp | 1 +
src/fwk/utils/pathutils.cpp | 22 ++++++++++++++++++++++
src/fwk/utils/pathutils.hpp | 6 +++++-
4 files changed, 38 insertions(+), 5 deletions(-)
---
diff --git a/src/engine/library/thumbnailcache.cpp b/src/engine/library/thumbnailcache.cpp
index 273d77e..10e4d90 100644
--- a/src/engine/library/thumbnailcache.cpp
+++ b/src/engine/library/thumbnailcache.cpp
@@ -77,7 +77,9 @@ Glib::RefPtr<Gdk::Pixbuf> getThumbnail(const LibFile::Ptr & f, int w, int h, con
DBG_OUT("creating thumbnail for %s",filename.c_str());
fwk::MimeType mime_type(filename);
-
+ if(!fwk::ensure_path_for_file(cached)) {
+ ERR_OUT("coudln't create directories for %s", cached.c_str());
+ }
DBG_OUT("MIME type %s", mime_type.string().c_str());
@@ -161,9 +163,14 @@ void ThumbnailCache::execute(const ThumbnailTask::Ptr & task)
std::string ThumbnailCache::path_for_thumbnail(const std::string & filename, int size) const
{
- std::string subdir = size ? boost::lexical_cast<std::string>(size) : "full";
// todo compute a hash
- return m_cacheDir + "/" + subdir + "/" + fwk::path_basename(filename) + ".png";
+ return dir_for_thumbnail(size) + fwk::path_basename(filename) + ".png";
+}
+
+std::string ThumbnailCache::dir_for_thumbnail(int size) const
+{
+ std::string subdir = size ? boost::lexical_cast<std::string>(size) : "full";
+ return m_cacheDir + "/" + subdir + "/";
}
bool ThumbnailCache::is_thumbnail_cached(const std::string & /*file*/, const std::string & thumb)
@@ -171,7 +178,6 @@ bool ThumbnailCache::is_thumbnail_cached(const std::string & /*file*/, const std
return fwk::path_exists(thumb);
}
-
}
/*
Local Variables:
diff --git a/src/engine/library/thumbnailcache.hpp b/src/engine/library/thumbnailcache.hpp
index 001cd2e..90cb273 100644
--- a/src/engine/library/thumbnailcache.hpp
+++ b/src/engine/library/thumbnailcache.hpp
@@ -72,6 +72,7 @@ private:
std::tr1::weak_ptr<fwk::NotificationCenter> m_notif_center;
std::string path_for_thumbnail(const std::string & filename, int size) const;
+ std::string dir_for_thumbnail(int size) const;
};
}
diff --git a/src/fwk/utils/pathutils.cpp b/src/fwk/utils/pathutils.cpp
index 00a101e..4efc0bc 100644
--- a/src/fwk/utils/pathutils.cpp
+++ b/src/fwk/utils/pathutils.cpp
@@ -20,6 +20,7 @@
#include <giomm/file.h>
+#include "fwk/base/debug.hpp"
#include "pathutils.hpp"
namespace fwk {
@@ -66,6 +67,14 @@ std::string path_stem(const std::string & path)
return std::string(path.begin(), path.begin() + idx);
}
+std::string path_dirname(const std::string & path)
+{
+ std::string::size_type slash_idx = path.find_last_of("/");
+ if(slash_idx == std::string::npos) {
+ return "";
+ }
+ return std::string(path.begin(), path.begin() + slash_idx + 1);
+}
/** return the extension of a path
/foo/bar/baz.txt returns .txt
@@ -99,6 +108,19 @@ bool path_exists(const std::string & path)
return file->query_exists();
}
+bool ensure_path_for_file(const std::string & path)
+{
+ try {
+ Glib::RefPtr<Gio::File> dir = Gio::File::create_for_path(path_dirname(path));
+ if(dir->query_exists()) {
+ return true;
+ }
+ return dir->make_directory_with_parents();
+ }
+ catch(...) {
+ return false;
+ }
+}
void _path_remove_recursive(const Glib::RefPtr<Gio::File> & dir)
{
diff --git a/src/fwk/utils/pathutils.hpp b/src/fwk/utils/pathutils.hpp
index 4dd0a59..9c8454a 100644
--- a/src/fwk/utils/pathutils.hpp
+++ b/src/fwk/utils/pathutils.hpp
@@ -36,6 +36,10 @@ std::string path_basename(const std::string & path);
*/
std::string path_stem(const std::string & path);
+/** return the dirname of a path. Assume the last component is a file.
+ /foo/bar/baz.txt returns /foo/bar/
+ */
+std::string path_dirname(const std::string & path);
/** return the extension of a path
/foo/bar/baz.txt returns .txt
@@ -47,7 +51,7 @@ std::string path_replace_extension(const std::string & path, const char * ext);
bool path_exists(const std::string & path);
-
+bool ensure_path_for_file(const std::string & path);
void path_remove_recursive(const std::string & path);
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]