CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/UWP/UWPHelpers/StorageAccess.cpp
Views: 1401
// Copyright (c) 2023- PPSSPP Project.12// This program is free software: you can redistribute it and/or modify3// it under the terms of the GNU General Public License as published by4// the Free Software Foundation, version 2.0 or later versions.56// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License 2.0 for more details.1011// A copy of the GPL 2.0 should have been included with the program.12// If not, see http://www.gnu.org/licenses/1314// Official git repository and contact information can be found at15// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.1617#include "StorageAsync.h"18#include "StorageAccess.h"19#include "UWPUtil.h"20#include "Common/File/Path.h"2122using namespace Platform;23using namespace Windows::Foundation;24using namespace Windows::Foundation::Collections;25using namespace Windows::Storage::AccessCache;26using namespace Windows::ApplicationModel;2728std::list<std::string> alist;29void AppendToAccessList(Platform::String^ path)30{31Path p(FromPlatformString(path));32alist.push_back(p.ToString());33}3435// Get value from app local settings36Platform::String^ GetDataFromLocalSettings(Platform::String^ key) {37ApplicationDataContainer^ localSettings{ ApplicationData::Current->LocalSettings };38IPropertySet^ values{ localSettings->Values };39if (key != nullptr) {40Platform::Object^ tokenRetrive = values->Lookup(key);41if (tokenRetrive != nullptr) {42Platform::String^ ConvertedToken = (Platform::String^)tokenRetrive;43return ConvertedToken;44}45}46return nullptr;47}4849std::string GetDataFromLocalSettings(std::string key) {50return FromPlatformString(GetDataFromLocalSettings(ToPlatformString(key)));51}5253// Add or replace value in app local settings54bool AddDataToLocalSettings(Platform::String^ key, Platform::String^ data, bool replace) {55ApplicationDataContainer^ localSettings{ ApplicationData::Current->LocalSettings };56IPropertySet^ values{ localSettings->Values };5758Platform::String^ testResult = GetDataFromLocalSettings(key);59if (testResult == nullptr) {60values->Insert(key, PropertyValue::CreateString(data));61return true;62}63else if (replace) {64values->Remove(key);65values->Insert(key, PropertyValue::CreateString(data));66return true;67}6869return false;70}7172bool AddDataToLocalSettings(std::string key, std::string data, bool replace) {73return AddDataToLocalSettings(ToPlatformString(key), ToPlatformString(data),replace);74}7576// Add folder to future list (to avoid request picker again)77void AddItemToFutureList(IStorageItem^ item) {78try {79if (item != nullptr) {80Platform::String^ folderToken = AccessCache::StorageApplicationPermissions::FutureAccessList->Add(item);81AppendToAccessList(item->Path);82}83}84catch (Platform::COMException^ e) {85}86}8788// Get item by key89// This function can be used when you store token in LocalSettings as custom key90IStorageItem^ GetItemByKey(Platform::String^ key) {91IStorageItem^ item;92Platform::String^ itemToken = GetDataFromLocalSettings(key);93if (itemToken != nullptr && AccessCache::StorageApplicationPermissions::FutureAccessList->ContainsItem(itemToken)) {94ExecuteTask(item, AccessCache::StorageApplicationPermissions::FutureAccessList->GetItemAsync(itemToken));95}9697return item;98}99100std::list<std::string> GetFutureAccessList() {101if (alist.empty()) {102auto AccessList = AccessCache::StorageApplicationPermissions::FutureAccessList->Entries;103for (auto it = 0; it != AccessList->Size; ++it){104auto item = AccessList->GetAt(it);105try {106auto token = item.Token;107if (token != nullptr && AccessCache::StorageApplicationPermissions::FutureAccessList->ContainsItem(token)) {108IStorageItem^ storageItem;109ExecuteTask(storageItem, AccessCache::StorageApplicationPermissions::FutureAccessList->GetItemAsync(token));110if (storageItem != nullptr) {111AppendToAccessList(storageItem->Path);112}113else {114AccessCache::StorageApplicationPermissions::FutureAccessList->Remove(token);115}116}117}118catch (Platform::COMException^ e) {119}120}121122AppendToAccessList(ApplicationData::Current->LocalFolder->Path);123AppendToAccessList(ApplicationData::Current->TemporaryFolder->Path);124}125return alist;126}127128129