Skip to content

Commit a5c49ff

Browse files
committed
Implemented new properties selection.dir.count and selection.dir.empty for issue #90.
1 parent 13a8aa6 commit a5c49ff

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

src/Context.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ namespace shellanything
7575

7676
std::string selection_path ;
7777
std::string selection_dir ;
78+
std::string selection_dir_count ;
79+
std::string selection_dir_empty ;
7880
std::string selection_parent_path ;
7981
std::string selection_parent_filename;
8082
std::string selection_filename ;
@@ -109,7 +111,7 @@ namespace shellanything
109111
//${selection.filename_noext} is selection.filename without file extension
110112
//${selection.filename.extension} is the file extension of the clicked element.
111113

112-
//build properties for this specific element
114+
// Build properties for this specific element
113115
std::string element_selection_path = element;
114116
std::string element_selection_dir = isFile ? ra::filesystem::GetParentPath(element_selection_path) : element;
115117
std::string element_selection_parent_path = ra::filesystem::GetParentPath(element_selection_path);
@@ -155,8 +157,27 @@ namespace shellanything
155157
selection_charset .append( element_selection_charset );
156158
}
157159

160+
// Directory based properties
161+
if (elements.size() == 1)
162+
{
163+
const std::string & element = elements[0];
164+
bool isDir = ra::filesystem::DirectoryExistsUtf8(element.c_str());
165+
if (isDir)
166+
{
167+
ra::strings::StringVector files;
168+
bool files_found = ra::filesystem::FindFilesUtf8(files, element.c_str(), 0);
169+
if (files_found)
170+
{
171+
selection_dir_count = ra::strings::ToString(files.size());
172+
selection_dir_empty = (files.size() == 0 ? "true" : "false");
173+
}
174+
}
175+
}
176+
158177
pmgr.SetProperty("selection.path" , selection_path );
159178
pmgr.SetProperty("selection.dir" , selection_dir );
179+
pmgr.SetProperty("selection.dir.count" , selection_dir_count );
180+
pmgr.SetProperty("selection.dir.empty" , selection_dir_empty );
160181
pmgr.SetProperty("selection.parent.path" , selection_parent_path );
161182
pmgr.SetProperty("selection.parent.filename" , selection_parent_filename);
162183
pmgr.SetProperty("selection.filename" , selection_filename );

test/TestContext.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#include "shellanything/Context.h"
2727
#include "PropertyManager.h"
2828
#include "rapidassist/process.h"
29+
#include "rapidassist/filesystem.h"
30+
#include "rapidassist/testing.h"
2931

3032
namespace shellanything { namespace test
3133
{
@@ -217,6 +219,76 @@ namespace shellanything { namespace test
217219
context.UnregisterProperties();
218220
}
219221
//--------------------------------------------------------------------------------------------------
222+
TEST_F(TestContext, testRegisterPropertiesSingleDirectory)
223+
{
224+
PropertyManager & pmgr = PropertyManager::GetInstance();
225+
226+
Context context;
227+
#ifdef _WIN32
228+
{
229+
Context::ElementList elements;
230+
elements.push_back("C:\\" );
231+
context.SetElements(elements);
232+
}
233+
#else
234+
//TODO: complete with known path to files
235+
#endif
236+
237+
ASSERT_FALSE( pmgr.HasProperty("selection.dir.count") );
238+
ASSERT_FALSE( pmgr.HasProperty("selection.dir.empty") );
239+
240+
//act
241+
context.RegisterProperties();
242+
243+
//assert
244+
ASSERT_TRUE( pmgr.HasProperty("selection.dir.count") );
245+
ASSERT_TRUE( pmgr.HasProperty("selection.dir.empty") );
246+
247+
std::string selection_dir_count = pmgr.Expand("${selection.dir.count}");
248+
std::string selection_dir_empty = pmgr.Expand("${selection.dir.empty}");
249+
250+
ASSERT_TRUE(ra::strings::IsNumeric(selection_dir_count.c_str()));
251+
ASSERT_EQ( std::string("false"), selection_dir_empty);
252+
253+
context.UnregisterProperties();
254+
}
255+
//--------------------------------------------------------------------------------------------------
256+
TEST_F(TestContext, testRegisterPropertiesEmptyDirectory)
257+
{
258+
PropertyManager & pmgr = PropertyManager::GetInstance();
259+
260+
//create an empty directory
261+
std::string temp_dir = ra::filesystem::GetTemporaryDirectory();
262+
std::string empty_dir = temp_dir + ra::testing::GetTestQualifiedName();
263+
ASSERT_TRUE(ra::filesystem::CreateDirectory(empty_dir.c_str()));
264+
265+
Context context;
266+
Context::ElementList elements;
267+
elements.push_back(empty_dir);
268+
context.SetElements(elements);
269+
270+
ASSERT_FALSE( pmgr.HasProperty("selection.dir.count") );
271+
ASSERT_FALSE( pmgr.HasProperty("selection.dir.empty") );
272+
273+
//act
274+
context.RegisterProperties();
275+
276+
//assert
277+
ASSERT_TRUE( pmgr.HasProperty("selection.dir.count") );
278+
ASSERT_TRUE( pmgr.HasProperty("selection.dir.empty") );
279+
280+
std::string selection_dir_count = pmgr.Expand("${selection.dir.count}");
281+
std::string selection_dir_empty = pmgr.Expand("${selection.dir.empty}");
282+
283+
ASSERT_EQ( std::string("0"), selection_dir_count);
284+
ASSERT_EQ( std::string("true"), selection_dir_empty);
285+
286+
context.UnregisterProperties();
287+
288+
//cleanup
289+
ra::filesystem::DeleteDirectory(empty_dir.c_str());
290+
}
291+
//--------------------------------------------------------------------------------------------------
220292
TEST_F(TestContext, testRegisterPropertiesMultipleFiles)
221293
{
222294
PropertyManager & pmgr = PropertyManager::GetInstance();

0 commit comments

Comments
 (0)