1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
/*
* Copyright 2017 Samsung Electronics Co., Ltd
*
* Licensed under the Flora License, Version 1.1 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://floralicense.org/license/
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "helpers.h"
#include <Ecore_File.h>
#include <storage.h>
#include "common.h"
namespace gallery { namespace { namespace impl {
constexpr auto UNIQUE_PATH_RESERVE = 10;
int MEDIA_DB_CONNECTION_COUNTER = 0;
}}}
namespace gallery {
using ucl::Mutex;
Mutex &getMediaMutex()
{
static Mutex mutex{true};
return mutex;
}
Result getInternalStorageId(int &result)
{
struct StorageIdRec final {
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;
}
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 {
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 extractFileExtension(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 = extractFileExtension(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;
}
}}
|