CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/UWP/UWPHelpers/StorageAccess.cpp
Views: 1401
1
// Copyright (c) 2023- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#include "StorageAsync.h"
19
#include "StorageAccess.h"
20
#include "UWPUtil.h"
21
#include "Common/File/Path.h"
22
23
using namespace Platform;
24
using namespace Windows::Foundation;
25
using namespace Windows::Foundation::Collections;
26
using namespace Windows::Storage::AccessCache;
27
using namespace Windows::ApplicationModel;
28
29
std::list<std::string> alist;
30
void AppendToAccessList(Platform::String^ path)
31
{
32
Path p(FromPlatformString(path));
33
alist.push_back(p.ToString());
34
}
35
36
// Get value from app local settings
37
Platform::String^ GetDataFromLocalSettings(Platform::String^ key) {
38
ApplicationDataContainer^ localSettings{ ApplicationData::Current->LocalSettings };
39
IPropertySet^ values{ localSettings->Values };
40
if (key != nullptr) {
41
Platform::Object^ tokenRetrive = values->Lookup(key);
42
if (tokenRetrive != nullptr) {
43
Platform::String^ ConvertedToken = (Platform::String^)tokenRetrive;
44
return ConvertedToken;
45
}
46
}
47
return nullptr;
48
}
49
50
std::string GetDataFromLocalSettings(std::string key) {
51
return FromPlatformString(GetDataFromLocalSettings(ToPlatformString(key)));
52
}
53
54
// Add or replace value in app local settings
55
bool AddDataToLocalSettings(Platform::String^ key, Platform::String^ data, bool replace) {
56
ApplicationDataContainer^ localSettings{ ApplicationData::Current->LocalSettings };
57
IPropertySet^ values{ localSettings->Values };
58
59
Platform::String^ testResult = GetDataFromLocalSettings(key);
60
if (testResult == nullptr) {
61
values->Insert(key, PropertyValue::CreateString(data));
62
return true;
63
}
64
else if (replace) {
65
values->Remove(key);
66
values->Insert(key, PropertyValue::CreateString(data));
67
return true;
68
}
69
70
return false;
71
}
72
73
bool AddDataToLocalSettings(std::string key, std::string data, bool replace) {
74
return AddDataToLocalSettings(ToPlatformString(key), ToPlatformString(data),replace);
75
}
76
77
// Add folder to future list (to avoid request picker again)
78
void AddItemToFutureList(IStorageItem^ item) {
79
try {
80
if (item != nullptr) {
81
Platform::String^ folderToken = AccessCache::StorageApplicationPermissions::FutureAccessList->Add(item);
82
AppendToAccessList(item->Path);
83
}
84
}
85
catch (Platform::COMException^ e) {
86
}
87
}
88
89
// Get item by key
90
// This function can be used when you store token in LocalSettings as custom key
91
IStorageItem^ GetItemByKey(Platform::String^ key) {
92
IStorageItem^ item;
93
Platform::String^ itemToken = GetDataFromLocalSettings(key);
94
if (itemToken != nullptr && AccessCache::StorageApplicationPermissions::FutureAccessList->ContainsItem(itemToken)) {
95
ExecuteTask(item, AccessCache::StorageApplicationPermissions::FutureAccessList->GetItemAsync(itemToken));
96
}
97
98
return item;
99
}
100
101
std::list<std::string> GetFutureAccessList() {
102
if (alist.empty()) {
103
auto AccessList = AccessCache::StorageApplicationPermissions::FutureAccessList->Entries;
104
for (auto it = 0; it != AccessList->Size; ++it){
105
auto item = AccessList->GetAt(it);
106
try {
107
auto token = item.Token;
108
if (token != nullptr && AccessCache::StorageApplicationPermissions::FutureAccessList->ContainsItem(token)) {
109
IStorageItem^ storageItem;
110
ExecuteTask(storageItem, AccessCache::StorageApplicationPermissions::FutureAccessList->GetItemAsync(token));
111
if (storageItem != nullptr) {
112
AppendToAccessList(storageItem->Path);
113
}
114
else {
115
AccessCache::StorageApplicationPermissions::FutureAccessList->Remove(token);
116
}
117
}
118
}
119
catch (Platform::COMException^ e) {
120
}
121
}
122
123
AppendToAccessList(ApplicationData::Current->LocalFolder->Path);
124
AppendToAccessList(ApplicationData::Current->TemporaryFolder->Path);
125
}
126
return alist;
127
}
128
129