enum {
FLAG_THUMBNAIL = 1,
FLAG_REMOVE = 2,
+ FLAG_SAVE = 4,
FLAGS_FROM_MEDIA_DB = (FLAG_THUMBNAIL | FLAG_REMOVE),
- FLAGS_SIMPLE_FILE = 0
+ FLAGS_SIMPLE_FILE = (FLAG_SAVE)
};
public:
void cancelThumbnailPathGet() const;
ucl::Result removeFile();
+ ucl::Result saveToDevice();
protected:
friend class ucl::RefCountObj<MediaItem>;
#ifndef __GALLERY_HELPERS_H__
#define __GALLERY_HELPERS_H__
-#include <string>
+#include "types.h"
-namespace gallery {
+namespace gallery { namespace util {
- template <class T>
- bool getProperty(T obj, int (*get)(T obj, char **value),
- std::string &result, bool optional = false);
+ template <class GETTER, class V, class ...ARGS>
+ ucl::Result get(GETTER &&getter, V &result, ARGS &&...args);
- template <class T, class V>
- bool getProperty(T obj, int (*get)(T obj, V *value),
- V &result, bool mayBeZero = false);
-}
+ template <class GETTER, class V, class ...ARGS>
+ ucl::Result getNz(GETTER &&getter, V &result, ARGS &&...args);
+}}
#include "helpers.hpp"
#include "ucl/util/logging.h"
-namespace gallery {
+namespace gallery { namespace util { namespace himpl {
- template <class T>
- inline bool getProperty(T obj,
- int (*get)(T obj, char **value),
- std::string &result, bool optional)
+ template <class GETTER, class ...ARGS>
+ inline ucl::Result get(GETTER &&getter, bool optional,
+ std::string &result, ARGS &&...args)
{
char *value = nullptr;
- const int ret = get(obj, &value);
+ const int ret = getter(std::forward<ARGS>(args)..., &value);
if ((ret != 0) || (!optional && !value)) {
UCL_ELOG("get() failed: %d", ret);
- return false;
+ return ucl::RES_FAIL;
}
if (value) {
result.clear();
}
- return true;
+ return ucl::RES_OK;
}
- template <class T, class V>
- inline bool getProperty(T obj, int (*get)(T obj, V *value),
- V &result, bool mayBeZero)
+ template <class GETTER, class V, class ...ARGS>
+ inline ucl::Result get(GETTER &&getter, bool optional,
+ V &result, ARGS &&...args)
{
- const int ret = get(obj, &result);
- if ((ret != 0) || (!mayBeZero && !result)) {
+ V value = {};
+ const int ret = getter(std::forward<ARGS>(args)..., &value);
+ if ((ret != 0) || (!optional && !value)) {
UCL_ELOG("get() failed: %d", ret);
- return false;
+ return ucl::RES_FAIL;
}
- return true;
+
+ result = value;
+
+ return ucl::RES_OK;
+ }
+}}}
+
+namespace gallery { namespace util {
+
+ template <class GETTER, class V, class ...ARGS>
+ inline ucl::Result get(GETTER &&getter, V &result, ARGS &&...args)
+ {
+ return himpl::get(std::forward<GETTER>(getter), true,
+ result, std::forward<ARGS>(args)...);
+ }
+
+ template <class GETTER, class V, class ...ARGS>
+ inline ucl::Result getNz(GETTER &&getter, V &result, ARGS &&...args)
+ {
+ return himpl::get(std::forward<GETTER>(getter), false,
+ result, std::forward<ARGS>(args)...);
}
-}
+}}
#include "model/MediaItem.h"
#include <Ecore_File.h>
+#include <storage.h>
#include "BaseJob.h"
}
}
- if (!getProperty(media, media_info_get_media_id, m_mediaId)) {
- LOG_RETURN(RES_FAIL, "getProperty(media_id) failed!");
- }
+ FAIL_RETURN(util::getNz(media_info_get_media_id, m_mediaId, media),
+ "media_info_get_media_id() failed!");
- if (!getProperty(media, media_info_get_file_path, m_filePath)) {
- LOG_RETURN(RES_FAIL, "getProperty(file_path) failed!");
- }
+ FAIL_RETURN(util::getNz(media_info_get_file_path, m_filePath, media),
+ "media_info_get_file_path() failed!");
m_isValid = true;
Result MediaItem::initThumbPath(const media_info_h media) const
{
- if (!getProperty(media, media_info_get_thumbnail_path,
- m_thumbPath, true)) {
- LOG_RETURN(RES_FAIL, "getProperty(thumbnail_path) failed!");
- }
+ FAIL_RETURN(util::get(media_info_get_thumbnail_path, m_thumbPath,
+ media), "media_info_get_thumbnail_path() failed!");
return RES_OK;
}
Result MediaItem::removeFile()
{
if (!(m_flags & FLAG_REMOVE)) {
- LOG_RETURN(RES_NOT_SUPPORTED, "Operation not suported!");
+ LOG_RETURN(RES_NOT_SUPPORTED, "Operation not supported!");
}
if (!ecore_file_can_write(m_filePath.c_str())) {
LOG_RETURN(RES_FAIL, "File can't be removed!");
}
if (!ecore_file_remove(m_filePath.c_str())) {
- FLOG("ecore_file_remove() failed! Attemting to rescan....");
+ FLOG("ecore_file_remove() failed! Attempting to rescan....");
MutexLock lock(getMediaMutex());
const int ret = media_content_scan_file(m_filePath.c_str());
if (ret != 0) {
return RES_OK;
}
+ Result MediaItem::saveToDevice()
+ {
+ if (!(m_flags & FLAG_SAVE)) {
+ LOG_RETURN(RES_NOT_SUPPORTED, "Operation not supported!");
+ }
+
+ int storageId = 0;
+ FAIL_RETURN(getInternalStorageId(storageId),
+ "getInternalStorageId() failed!");
+
+ std::string imagesDir;
+ FAIL_RETURN(util::getNz(storage_get_directory, imagesDir,
+ storageId, STORAGE_DIRECTORY_IMAGES),
+ "storage_get_directory() failed!");
+
+ const std::string savePath = util::makeUniqueFilePath(
+ m_filePath, imagesDir);
+
+ if (!ecore_file_cp(m_filePath.c_str(), savePath.c_str())) {
+ LOG_RETURN(RES_FAIL, "ecore_file_cp() failed!");
+ }
+
+ {
+ MutexLock lock(getMediaMutex());
+ const int ret = media_content_scan_file(savePath.c_str());
+ if (ret == 0) {
+ return RES_OK;
+ }
+ ELOG("media_content_scan_file() failed: %d", ret);
+ }
+
+ if (!ecore_file_remove(savePath.c_str())) {
+ WLOG("ecore_file_remove() failed!");
+ }
+
+ return RES_FAIL;
+ }
+
Result MediaItem::getThumbnailPath(ThumbnailPathGetCb cb) const
{
if (!(m_flags & FLAG_THUMBNAIL)) {
- LOG_RETURN(RES_NOT_SUPPORTED, "Operation not suported!");
+ LOG_RETURN(RES_NOT_SUPPORTED, "Operation not supported!");
}
if (!cb) {
return RES_INVALID_ARGUMENTS;
return RES_FALSE;
}
- auto cbProxy = util::makeUnique(new ThumbCbProxy{this, cb});
+ auto cbProxy = ucl::util::makeUnique(new ThumbCbProxy{this, cb});
{
MutexLock lock(getMediaMutex());
#include "helpers.h"
+#include <Ecore_File.h>
+#include <storage.h>
+
#include "common.h"
+namespace gallery { namespace { namespace impl {
+
+ constexpr auto UNIQUE_PATH_RESERVE = 10;
+}}}
+
namespace gallery {
using namespace ucl;
static Mutex mutex{true};
return mutex;
}
+
+ Result getInternalStorageId(int &result)
+ {
+ struct StorageIdRec {
+ int id;
+ bool valid;
+ } storageIdRec {0, false};
+
+ const int ret = storage_foreach_device_supported(
+ [](int storageId, storage_type_e type,
+ storage_state_e state, const char *path, void *userData)
+ {
+ if ((type != STORAGE_TYPE_INTERNAL) ||
+ (state != STORAGE_STATE_MOUNTED)) {
+ return true;
+ }
+ auto &storageIdRec = *static_cast<StorageIdRec *>(userData);
+ storageIdRec.id = storageId;
+ storageIdRec.valid = true;
+ return false;
+ },
+ &storageIdRec);
+ if (ret != 0) {
+ LOG_RETURN(RES_FAIL,
+ "storage_foreach_device_supported() failed: %d", ret);
+ }
+
+ if (!storageIdRec.valid) {
+ LOG_RETURN(RES_FAIL, "Writable internal storage not found!");
+ }
+
+ result = storageIdRec.id;
+
+ return RES_OK;
+ }
}
+
+namespace gallery { namespace util {
+
+ std::string extractFileName(const std::string &path)
+ {
+ const auto bsPos = path.rfind('/');
+ if (bsPos == (path.size() - 1)) {
+ return {};
+ }
+ return path.substr(bsPos + 1);
+ }
+
+ std::string extractExtension(const std::string &name)
+ {
+ const auto dotPos = name.rfind('.');
+ if ((dotPos == std::string::npos) ||
+ (dotPos == 0) || (dotPos == (name.size() - 1))) {
+ return {};
+ }
+ return name.substr(dotPos + 1);
+ }
+
+ void splitFilePath(const std::string &path, std::string &directory,
+ std::string &baseName, std::string &extension)
+ {
+ splitFileName(path, baseName, extension);
+ if (isNotEmpty(baseName)) {
+ directory = path.substr(0, (path.size() - baseName.size() -
+ (isNotEmpty(extension) ? (extension.size() - 1) : 0)));
+ } else {
+ directory = path;
+ }
+ }
+
+ void splitFileName(const std::string &path,
+ std::string &baseName, std::string &extension)
+ {
+ baseName = extractFileName(path);
+ if (isNotEmpty(baseName)) {
+ extension = extractExtension(baseName);
+ if (isNotEmpty(extension)) {
+ baseName.resize(baseName.size() - extension.size() - 1);
+ }
+ } else {
+ extension.clear();
+ }
+ }
+
+ std::string makeUniqueFilePath(const std::string &srcPath,
+ const std::string &dstDir)
+ {
+ std::string baseName;
+ std::string extension;
+ splitFileName(srcPath, baseName, extension);
+
+ std::string result;
+ result.reserve(dstDir.size() + baseName.size() +
+ extension.size() + impl::UNIQUE_PATH_RESERVE);
+
+ result = dstDir;
+ if (isNotEmpty(result) && (result.back() != '/')) {
+ result += '/';
+ }
+ result += baseName;
+
+ const auto baseSize = result.size();
+
+ for (int counter = 2; ; ++counter) {
+ if (isNotEmpty(extension)) {
+ result += '.';
+ result += extension;
+ }
+ if (!ecore_file_exists(result.c_str())) {
+ break;
+ }
+ result.resize(baseSize);
+ result += " (";
+ result += std::to_string(counter);
+ result += ')';
+ }
+
+ return result;
+ }
+}}
MediaType toMediaType(media_content_type_e contentType);
ucl::Mutex &getMediaMutex();
+
+ ucl::Result getInternalStorageId(int &result);
}
+namespace gallery { namespace util {
+
+ std::string extractFileName(const std::string &path);
+
+ std::string extractFileExtension(const std::string &name);
+
+ void splitFilePath(const std::string &path, std::string &directory,
+ std::string &baseName, std::string &extension);
+
+ void splitFileName(const std::string &path,
+ std::string &baseName, std::string &extension);
+
+ std::string makeUniqueFilePath(const std::string &srcPath,
+ const std::string &dstDir);
+}}
+
#include "helpers.hpp"
#endif // __GALLERY_MODEL_HELPERS_H__
Result Instance::handleAppControl(app_control_h appControl)
{
std::string operation;
- if (!getProperty(appControl, app_control_get_operation, operation)) {
- ELOG("app_control_get_operation() failed!");
- }
+ FAIL_LOG(util::getNz(app_control_get_operation, operation, appControl),
+ "app_control_get_operation() failed!");
app_control_launch_mode_e mode = APP_CONTROL_LAUNCH_MODE_SINGLE;
- if (!getProperty(appControl, app_control_get_launch_mode, mode, true)) {
- ELOG("app_control_get_launch_mode() failed!");
- mode = APP_CONTROL_LAUNCH_MODE_SINGLE;
- }
+ FAIL_LOG(util::get(app_control_get_launch_mode, mode, appControl),
+ "app_control_get_launch_mode() failed!");
DLOG("operation: %s; mode: %d;", operation.c_str(), mode);
if (operation == APP_CONTROL_OPERATION_VIEW) {
std::string uri;
- if (!getProperty(appControl, app_control_get_uri, uri)) {
- ELOG("app_control_get_uri() failed!");
- return RES_FAIL;
- }
+ FAIL_RETURN(util::getNz(app_control_get_uri, uri, appControl),
+ "app_control_get_uri() failed!");
createViewerPage(uri);
} else {
WLOG("Operation not supported for current mode!");