mirror of
https://codeberg.org/WinDurango/WinDurango.git
synced 2026-04-18 02:23:34 -04:00
521 lines
13 KiB
C++
521 lines
13 KiB
C++
#include "Interfaces/Storage/Directory.h"
|
|
|
|
/*
|
|
* This is a Dir Class and all the funcs inside
|
|
* are specifically for managing this dir.
|
|
*/
|
|
bool wd::impl::winrt::interfaces::storage::WinRTDirectory::open()
|
|
{
|
|
if (dir != nullptr)
|
|
{
|
|
return false;
|
|
}
|
|
try
|
|
{
|
|
StorageFolder sf = ApplicationData::Current().LocalFolder();
|
|
|
|
for (const auto &part : path)
|
|
{
|
|
hstring partStr = hstring(part.wstring());
|
|
sf = sf.GetFolderAsync(partStr).get();
|
|
}
|
|
|
|
if (sf)
|
|
{
|
|
dir = sf;
|
|
return true;
|
|
}
|
|
}
|
|
catch (const hresult_error &ex)
|
|
{
|
|
return false;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/*
|
|
* Todo:
|
|
* Add logging
|
|
*
|
|
* TargetPath is the filename btw
|
|
* any path will be ignored.
|
|
*/
|
|
std::shared_ptr<wd::common::interfaces::storage::File> wd::impl::winrt::interfaces::storage::WinRTDirectory::CreateFile(
|
|
std::filesystem::path targetPath)
|
|
{
|
|
if (dir == nullptr)
|
|
{
|
|
return nullptr;
|
|
}
|
|
try
|
|
{
|
|
auto file =
|
|
dir.CreateFileAsync(hstring(targetPath.filename().wstring()), CreationCollisionOption::OpenIfExists).get();
|
|
std::shared_ptr<wd::impl::winrt::interfaces::storage::WinRTFile> fileRT =
|
|
std::make_shared<wd::impl::winrt::interfaces::storage::WinRTFile>(path / targetPath.filename());
|
|
return fileRT;
|
|
}
|
|
catch (const hresult_error &ex)
|
|
{
|
|
return nullptr;
|
|
}
|
|
}
|
|
|
|
/*
|
|
* TODO: Use Shared Pointers
|
|
*/
|
|
std::shared_ptr<wd::common::interfaces::storage::Directory> wd::impl::winrt::interfaces::storage::WinRTDirectory::
|
|
CreateFolder(std::filesystem::path targetPath)
|
|
{
|
|
if (dir == nullptr)
|
|
{
|
|
return nullptr;
|
|
}
|
|
try
|
|
{
|
|
auto file =
|
|
dir.CreateFolderAsync(hstring(targetPath.filename().wstring()), CreationCollisionOption::OpenIfExists)
|
|
.get();
|
|
std::shared_ptr<wd::impl::winrt::interfaces::storage::WinRTDirectory> folderRT =
|
|
std::make_shared<wd::impl::winrt::interfaces::storage::WinRTDirectory>(path / targetPath.filename());
|
|
return folderRT;
|
|
}
|
|
catch (const hresult_error &ex)
|
|
{
|
|
return nullptr;
|
|
}
|
|
}
|
|
|
|
std::filesystem::path wd::impl::winrt::interfaces::storage::WinRTDirectory::dirpath()
|
|
{
|
|
return path;
|
|
}
|
|
|
|
bool wd::impl::winrt::interfaces::storage::WinRTDirectory::rename(std::string newName)
|
|
{
|
|
if (dir == nullptr)
|
|
{
|
|
return false;
|
|
}
|
|
try
|
|
{
|
|
dir.RenameAsync(to_hstring(newName)).get();
|
|
path.replace_filename(newName);
|
|
return true;
|
|
}
|
|
catch (const hresult_error &ex)
|
|
{
|
|
return false;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool wd::impl::winrt::interfaces::storage::WinRTDirectory::remove()
|
|
{
|
|
if (dir == nullptr)
|
|
{
|
|
return false;
|
|
}
|
|
try
|
|
{
|
|
dir.DeleteAsync().get();
|
|
return true;
|
|
}
|
|
catch (const hresult_error &ex)
|
|
{
|
|
return false;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool MoveFolder(StorageFolder source, StorageFolder destinationContainer)
|
|
{
|
|
try
|
|
{
|
|
StorageFolder destinationFolder =
|
|
destinationContainer.CreateFolderAsync(source.Name(), CreationCollisionOption::OpenIfExists).get();
|
|
|
|
for (auto file : source.GetFilesAsync().get())
|
|
{
|
|
file.MoveAsync(destinationFolder, file.Name(), NameCollisionOption::GenerateUniqueName).get();
|
|
}
|
|
|
|
for (auto folder : source.GetFoldersAsync().get())
|
|
{
|
|
MoveFolder(folder, destinationFolder);
|
|
}
|
|
return true;
|
|
}
|
|
catch (const hresult_error &ex)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
bool wd::impl::winrt::interfaces::storage::WinRTDirectory::move(std::filesystem::path targetPath)
|
|
{
|
|
if (dir == nullptr)
|
|
{
|
|
return false;
|
|
}
|
|
try
|
|
{
|
|
StorageFolder target = ApplicationData::Current().LocalFolder();
|
|
|
|
for (const auto &part : targetPath)
|
|
{
|
|
hstring partStr = hstring(part.wstring());
|
|
target = target.GetFolderAsync(partStr).get();
|
|
}
|
|
|
|
if (MoveFolder(dir, target))
|
|
{
|
|
path = targetPath;
|
|
return true;
|
|
}
|
|
}
|
|
catch (const hresult_error &ex)
|
|
{
|
|
return false;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool CopyFolder(StorageFolder source, StorageFolder destinationContainer)
|
|
{
|
|
try
|
|
{
|
|
StorageFolder destinationFolder =
|
|
destinationContainer.CreateFolderAsync(source.Name(), CreationCollisionOption::OpenIfExists).get();
|
|
|
|
for (auto file : source.GetFilesAsync().get())
|
|
{
|
|
file.CopyAsync(destinationFolder, file.Name(), NameCollisionOption::GenerateUniqueName).get();
|
|
}
|
|
|
|
for (auto folder : source.GetFoldersAsync().get())
|
|
{
|
|
CopyFolder(folder, destinationFolder);
|
|
}
|
|
return true;
|
|
}
|
|
catch (const hresult_error &ex)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
bool wd::impl::winrt::interfaces::storage::WinRTDirectory::copy(std::filesystem::path targetPath)
|
|
{
|
|
if (dir == nullptr)
|
|
{
|
|
return false;
|
|
}
|
|
try
|
|
{
|
|
StorageFolder target = ApplicationData::Current().LocalFolder();
|
|
|
|
for (const auto &part : targetPath)
|
|
{
|
|
hstring partStr = hstring(part.wstring());
|
|
target = target.GetFolderAsync(partStr).get();
|
|
}
|
|
|
|
if (CopyFolder(dir, target))
|
|
{
|
|
path = targetPath;
|
|
return true;
|
|
}
|
|
}
|
|
catch (const hresult_error &ex)
|
|
{
|
|
return false;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool wd::impl::winrt::interfaces::storage::WinRTDirectory::dirExists(std::string name)
|
|
{
|
|
if (dir == nullptr)
|
|
{
|
|
return false;
|
|
}
|
|
try
|
|
{
|
|
auto item = dir.GetItemAsync(to_hstring(name)).get();
|
|
return item && item.IsOfType(StorageItemTypes::Folder);
|
|
}
|
|
catch (const hresult_error &ex)
|
|
{
|
|
return false;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool wd::impl::winrt::interfaces::storage::WinRTDirectory::fileExists(std::string name)
|
|
{
|
|
if (dir == nullptr)
|
|
{
|
|
return false;
|
|
}
|
|
try
|
|
{
|
|
auto item = dir.GetItemAsync(to_hstring(name)).get();
|
|
return item && item.IsOfType(StorageItemTypes::File);
|
|
}
|
|
catch (const hresult_error &ex)
|
|
{
|
|
return false;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
concurrencpp::result<bool> wd::impl::winrt::interfaces::storage::WinRTDirectory::openAsync()
|
|
{
|
|
if (dir != nullptr)
|
|
{
|
|
co_return false;
|
|
}
|
|
try
|
|
{
|
|
StorageFolder sf = ApplicationData::Current().LocalFolder();
|
|
|
|
for (const auto &part : path)
|
|
{
|
|
hstring partStr = hstring(part.wstring());
|
|
sf = co_await sf.GetFolderAsync(partStr);
|
|
}
|
|
|
|
if (sf)
|
|
{
|
|
dir = sf;
|
|
co_return true;
|
|
}
|
|
}
|
|
catch (const hresult_error &ex)
|
|
{
|
|
co_return false;
|
|
}
|
|
co_return false;
|
|
}
|
|
|
|
concurrencpp::result<std::shared_ptr<wd::common::interfaces::storage::File>> wd::impl::winrt::interfaces::storage::WinRTDirectory::CreateFileAsync(std::filesystem::path targetPath)
|
|
{
|
|
if (dir == nullptr)
|
|
{
|
|
co_return nullptr;
|
|
}
|
|
try
|
|
{
|
|
auto file = co_await dir.CreateFileAsync(hstring(targetPath.filename().wstring()), CreationCollisionOption::OpenIfExists);
|
|
std::shared_ptr<wd::impl::winrt::interfaces::storage::WinRTFile> fileRT =
|
|
std::make_shared<wd::impl::winrt::interfaces::storage::WinRTFile>(path / targetPath.filename());
|
|
co_return fileRT;
|
|
}
|
|
catch (const hresult_error &ex)
|
|
{
|
|
co_return nullptr;
|
|
}
|
|
}
|
|
|
|
concurrencpp::result<std::shared_ptr<wd::common::interfaces::storage::Directory>> wd::impl::winrt::interfaces::storage::WinRTDirectory::CreateFolderAsync(std::filesystem::path targetPath)
|
|
{
|
|
if (dir == nullptr)
|
|
{
|
|
co_return nullptr;
|
|
}
|
|
try
|
|
{
|
|
auto file = co_await dir.CreateFolderAsync(hstring(targetPath.filename().wstring()), CreationCollisionOption::OpenIfExists);
|
|
std::shared_ptr<wd::impl::winrt::interfaces::storage::WinRTDirectory> folderRT =
|
|
std::make_shared<wd::impl::winrt::interfaces::storage::WinRTDirectory>(path / targetPath.filename());
|
|
co_return folderRT;
|
|
}
|
|
catch (const hresult_error &ex)
|
|
{
|
|
co_return nullptr;
|
|
}
|
|
}
|
|
|
|
concurrencpp::result<std::filesystem::path> wd::impl::winrt::interfaces::storage::WinRTDirectory::dirpathAsync()
|
|
{
|
|
co_return path;
|
|
}
|
|
|
|
concurrencpp::result<bool> wd::impl::winrt::interfaces::storage::WinRTDirectory::renameAsync(std::string newName)
|
|
{
|
|
if (dir == nullptr)
|
|
{
|
|
co_return false;
|
|
}
|
|
try
|
|
{
|
|
co_await dir.RenameAsync(to_hstring(newName));
|
|
path.replace_filename(newName);
|
|
co_return true;
|
|
}
|
|
catch (const hresult_error &ex)
|
|
{
|
|
co_return false;
|
|
}
|
|
co_return false;
|
|
}
|
|
|
|
concurrencpp::result<bool> wd::impl::winrt::interfaces::storage::WinRTDirectory::removeAsync()
|
|
{
|
|
if (dir == nullptr)
|
|
{
|
|
co_return false;
|
|
}
|
|
try
|
|
{
|
|
co_await dir.DeleteAsync();
|
|
co_return true;
|
|
}
|
|
catch (const hresult_error &ex)
|
|
{
|
|
co_return false;
|
|
}
|
|
co_return false;
|
|
}
|
|
|
|
concurrencpp::result<bool> MoveFolderAsync(StorageFolder source, StorageFolder destinationContainer)
|
|
{
|
|
try
|
|
{
|
|
StorageFolder destinationFolder = co_await destinationContainer.CreateFolderAsync(source.Name(), CreationCollisionOption::OpenIfExists);
|
|
|
|
for (auto file : co_await source.GetFilesAsync())
|
|
{
|
|
co_await file.MoveAsync(destinationFolder, file.Name(), NameCollisionOption::GenerateUniqueName);
|
|
}
|
|
|
|
for (auto folder : co_await source.GetFoldersAsync())
|
|
{
|
|
co_await MoveFolderAsync(folder, destinationFolder);
|
|
}
|
|
co_return true;
|
|
}
|
|
catch (const hresult_error &ex)
|
|
{
|
|
co_return false;
|
|
}
|
|
}
|
|
|
|
concurrencpp::result<bool> wd::impl::winrt::interfaces::storage::WinRTDirectory::moveAsync(std::filesystem::path targetPath)
|
|
{
|
|
if (dir == nullptr)
|
|
{
|
|
co_return false;
|
|
}
|
|
try
|
|
{
|
|
StorageFolder target = ApplicationData::Current().LocalFolder();
|
|
|
|
for (const auto &part : targetPath)
|
|
{
|
|
hstring partStr = hstring(part.wstring());
|
|
target = co_await target.GetFolderAsync(partStr);
|
|
}
|
|
|
|
if (co_await MoveFolderAsync(dir, target))
|
|
{
|
|
path = targetPath;
|
|
co_return true;
|
|
}
|
|
}
|
|
catch (const hresult_error &ex)
|
|
{
|
|
co_return false;
|
|
}
|
|
co_return false;
|
|
}
|
|
|
|
concurrencpp::result<bool> CopyFolderAsync(StorageFolder source, StorageFolder destinationContainer)
|
|
{
|
|
try
|
|
{
|
|
StorageFolder destinationFolder = co_await destinationContainer.CreateFolderAsync(source.Name(), CreationCollisionOption::OpenIfExists);
|
|
|
|
for (auto file : co_await source.GetFilesAsync())
|
|
{
|
|
co_await file.CopyAsync(destinationFolder, file.Name(), NameCollisionOption::GenerateUniqueName);
|
|
}
|
|
|
|
for (auto folder : co_await source.GetFoldersAsync())
|
|
{
|
|
co_await CopyFolderAsync(folder, destinationFolder);
|
|
}
|
|
co_return true;
|
|
}
|
|
catch (const hresult_error &ex)
|
|
{
|
|
co_return false;
|
|
}
|
|
}
|
|
|
|
concurrencpp::result<bool> wd::impl::winrt::interfaces::storage::WinRTDirectory::copyAsync(std::filesystem::path targetPath)
|
|
{
|
|
if (dir == nullptr)
|
|
{
|
|
co_return false;
|
|
}
|
|
try
|
|
{
|
|
StorageFolder target = ApplicationData::Current().LocalFolder();
|
|
|
|
for (const auto &part : targetPath)
|
|
{
|
|
hstring partStr = hstring(part.wstring());
|
|
target = co_await target.GetFolderAsync(partStr);
|
|
}
|
|
|
|
if (co_await CopyFolderAsync(dir, target))
|
|
{
|
|
path = targetPath;
|
|
co_return true;
|
|
}
|
|
}
|
|
catch (const hresult_error &ex)
|
|
{
|
|
co_return false;
|
|
}
|
|
co_return false;
|
|
}
|
|
|
|
concurrencpp::result<bool> wd::impl::winrt::interfaces::storage::WinRTDirectory::dirExistsAsync(std::string name)
|
|
{
|
|
if (dir == nullptr)
|
|
{
|
|
co_return false;
|
|
}
|
|
try
|
|
{
|
|
auto item = co_await dir.GetItemAsync(to_hstring(name));
|
|
co_return item && item.IsOfType(StorageItemTypes::Folder);
|
|
}
|
|
catch (const hresult_error &ex)
|
|
{
|
|
co_return false;
|
|
}
|
|
co_return false;
|
|
}
|
|
|
|
concurrencpp::result<bool> wd::impl::winrt::interfaces::storage::WinRTDirectory::fileExistsAsync(std::string name)
|
|
{
|
|
if (dir == nullptr)
|
|
{
|
|
co_return false;
|
|
}
|
|
try
|
|
{
|
|
auto item = co_await dir.GetItemAsync(to_hstring(name));
|
|
co_return item && item.IsOfType(StorageItemTypes::File);
|
|
}
|
|
catch (const hresult_error &ex)
|
|
{
|
|
co_return false;
|
|
}
|
|
co_return false;
|
|
}
|