diff options
author | Igor Nazarov <i.nazarov@samsung.com> | 2017-06-26 12:39:58 +0300 |
---|---|---|
committer | Igor Nazarov <i.nazarov@samsung.com> | 2017-06-26 12:44:31 +0300 |
commit | 6340a6c69fb31fbc6dbed97dec21961a8ae53622 (patch) | |
tree | 02988bfe7b9f675d468b9468960c65588410c237 | |
parent | d0e0b48fa1b9ce1075c7475ddc88d4c101a979ab (diff) | |
download | gallery-6340a6c69fb31fbc6dbed97dec21961a8ae53622.tar.gz gallery-6340a6c69fb31fbc6dbed97dec21961a8ae53622.tar.bz2 gallery-6340a6c69fb31fbc6dbed97dec21961a8ae53622.zip |
Fix of: TizenRefApp-8771 Saving image to Gear failssubmit/tizen_4.0/20170811.094300submit/tizen/20170725.094758submit/tizen/20170703.071445
- Added acuireMediaDbConnection/acuireMediaDbConnection functions;
- Added copyFile() temp function.
Change-Id: If5277a52c0af5b3ed9eeb2bc599ed9149bbd5315
-rw-r--r-- | src/common.h | 1 | ||||
-rw-r--r-- | src/model/Gallery.cpp | 11 | ||||
-rw-r--r-- | src/model/MediaItem.cpp | 51 | ||||
-rw-r--r-- | src/model/helpers.cpp | 32 | ||||
-rw-r--r-- | src/model/helpers.h | 3 |
5 files changed, 87 insertions, 11 deletions
diff --git a/src/common.h b/src/common.h index 43890c0..eff0cb0 100644 --- a/src/common.h +++ b/src/common.h @@ -39,6 +39,7 @@ namespace gallery { using ucl::RES_INVALID_ARGUMENTS; using ucl::RES_ILLEGAL_STATE; using ucl::RES_NOT_SUPPORTED; + using ucl::RES_IO_ERROR; using ucl::RES_INVALID_DATA; using ucl::RES_FATAL; diff --git a/src/model/Gallery.cpp b/src/model/Gallery.cpp index a2c9acc..ad7c1c1 100644 --- a/src/model/Gallery.cpp +++ b/src/model/Gallery.cpp @@ -30,10 +30,7 @@ namespace gallery { Gallery::~Gallery() { if (m_isMediaDbConnected) { - const int ret = media_content_disconnect(); - if (ret != 0) { - WLOG("media_content_disconnect() failed: %d", ret); - } + releaseMediaDbConnection(); } } @@ -46,10 +43,8 @@ namespace gallery { Result Gallery::prepare() { - const int ret = media_content_connect(); - if (ret != 0) { - LOG_RETURN(RES_FAIL, "media_content_connect() failed: %d", ret); - } + FAIL_RETURN(acquireMediaDbConnection(), + "acquireMediaDbConnection() failed!"); m_isMediaDbConnected = true; diff --git a/src/model/MediaItem.cpp b/src/model/MediaItem.cpp index a0db727..c20148a 100644 --- a/src/model/MediaItem.cpp +++ b/src/model/MediaItem.cpp @@ -52,6 +52,50 @@ namespace gallery { namespace { namespace impl { return MediaType::OTHERS; } + + // Temp function until ecore_file_cp() fix. + Result copyFile(const std::string &src, const std::string &dst) + { + FILE *f1 = fopen(src.c_str(), "rb"); + if (!f1) { + LOG_RETURN(RES_IO_ERROR, "fopen(rb) failed!"); + } + + FILE *f2 = fopen(dst.c_str(), "wb"); + if (!f2) { + fclose(f1); + LOG_RETURN(RES_IO_ERROR, "fopen(wb) failed!"); + } + + bool badCopy = false; + + constexpr auto COPY_BUF_SIZE = 16384; + char buf[COPY_BUF_SIZE]; + while (!feof(f1)) { + const auto num = fread(buf, 1, sizeof(buf), f1); + if (num == 0) { + badCopy = true; + LOG_BREAK(RES_IO_ERROR, "fread() failed!"); + } + if (fwrite(buf, 1, num, f2) != num) { + badCopy = true; + LOG_BREAK(RES_IO_ERROR, "fwrite() failed!"); + } + } + + fclose(f1); + fclose(f2); + + if (badCopy) { + const int r = remove(dst.c_str()); + if (r != 0) { + WLOG("remove() failed: %d;", r); + } + return RES_IO_ERROR; + } + + return RES_OK; + } }}} namespace gallery { @@ -384,13 +428,14 @@ namespace gallery { 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!"); - } + FAIL_RETURN(impl::copyFile(m_filePath, savePath), "copyFile() failed!"); { MutexLock lock(getMediaMutex()); + FAIL_RETURN(acquireMediaDbConnection(), + "acquireMediaDbConnection() failed!"); const int ret = media_content_scan_file(savePath.c_str()); + releaseMediaDbConnection(); if (ret == 0) { return RES_OK; } diff --git a/src/model/helpers.cpp b/src/model/helpers.cpp index 34c5a00..9bf5042 100644 --- a/src/model/helpers.cpp +++ b/src/model/helpers.cpp @@ -24,6 +24,8 @@ namespace gallery { namespace { namespace impl { constexpr auto UNIQUE_PATH_RESERVE = 10; + + int MEDIA_DB_CONNECTION_COUNTER = 0; }}} namespace gallery { @@ -70,6 +72,36 @@ namespace gallery { return RES_OK; } + + Result acquireMediaDbConnection() + { + if (impl::MEDIA_DB_CONNECTION_COUNTER > 0) { + ++impl::MEDIA_DB_CONNECTION_COUNTER; + return RES_OK; + } + + FAIL_RETURN(util::call(media_content_connect), + "media_content_connect() failed!"); + + impl::MEDIA_DB_CONNECTION_COUNTER = 1; + + return RES_OK; + } + + void releaseMediaDbConnection() + { + if (impl::MEDIA_DB_CONNECTION_COUNTER == 0) { + WLOG("Not connected!"); + return; + } + + if (impl::MEDIA_DB_CONNECTION_COUNTER == 1) { + FAIL_LOG(util::call(media_content_disconnect), + "media_content_disconnect() failed!"); + } + + --impl::MEDIA_DB_CONNECTION_COUNTER; + } } namespace gallery { namespace util { diff --git a/src/model/helpers.h b/src/model/helpers.h index 4fa988f..2c956c4 100644 --- a/src/model/helpers.h +++ b/src/model/helpers.h @@ -30,6 +30,9 @@ namespace gallery { ucl::Mutex &getMediaMutex(); ucl::Result getInternalStorageId(int &result); + + ucl::Result acquireMediaDbConnection(); + void releaseMediaDbConnection(); } namespace gallery { namespace util { |