Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/UWP/UWPHelpers/StorageAccess.cpp
5672 views
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 "pch.h"
19
#include "StorageAsync.h"
20
#include "StorageAccess.h"
21
#include "UWPUtil.h"
22
#include "Common/File/Path.h"
23
24
using namespace winrt;
25
using namespace winrt::Windows::Foundation;
26
using namespace winrt::Windows::Foundation::Collections;
27
using namespace winrt::Windows::Storage::AccessCache;
28
using namespace winrt::Windows::Storage;
29
using namespace winrt::Windows::ApplicationModel;
30
31
std::list<std::string> alist;
32
void AppendToAccessList(winrt::hstring path)
33
{
34
Path p(FromHString(path));
35
alist.push_back(p.ToString());
36
}
37
38
// Get value from app local settings
39
winrt::hstring GetDataFromLocalSettings(winrt::hstring key) {
40
ApplicationDataContainer localSettings = ApplicationData::Current().LocalSettings();
41
IPropertySet values = localSettings.Values();
42
if (!key.empty()) {
43
auto tokenRetrive = values.TryLookup(key);
44
if (tokenRetrive) {
45
winrt::hstring ConvertedToken = winrt::unbox_value<winrt::hstring>(tokenRetrive);
46
return ConvertedToken;
47
}
48
}
49
return L"";
50
}
51
52
std::string GetDataFromLocalSettings(std::string key) {
53
return FromHString(GetDataFromLocalSettings(ToHString(key)));
54
}
55
56
// Add or replace value in app local settings
57
bool AddDataToLocalSettings(winrt::hstring key, winrt::hstring data, bool replace) {
58
ApplicationDataContainer localSettings = ApplicationData::Current().LocalSettings();
59
IPropertySet values = localSettings.Values();
60
61
winrt::hstring testResult = GetDataFromLocalSettings(key);
62
if (testResult.empty()) {
63
values.Insert(key, winrt::box_value(data));
64
return true;
65
}
66
else if (replace) {
67
values.Remove(key);
68
values.Insert(key, winrt::box_value(data));
69
return true;
70
}
71
72
return false;
73
}
74
75
bool AddDataToLocalSettings(std::string key, std::string data, bool replace) {
76
return AddDataToLocalSettings(ToHString(key), ToHString(data), replace);
77
}
78
79
// Add folder to future list (to avoid request picker again)
80
void AddItemToFutureList(const winrt::Windows::Storage::IStorageItem& item) {
81
try {
82
if (item != nullptr) {
83
winrt::hstring folderToken = StorageApplicationPermissions::FutureAccessList().Add(item);
84
AppendToAccessList(item.Path());
85
}
86
}
87
catch (const winrt::hresult_error&) {
88
}
89
}
90
91
// Get item by key
92
// This function can be used when you store token in LocalSettings as custom key
93
winrt::Windows::Storage::IStorageItem GetItemByKey(winrt::hstring key) {
94
winrt::Windows::Storage::IStorageItem item = nullptr;
95
winrt::hstring itemToken = GetDataFromLocalSettings(key);
96
if (!itemToken.empty() && StorageApplicationPermissions::FutureAccessList().ContainsItem(itemToken)) {
97
ExecuteTask(item, StorageApplicationPermissions::FutureAccessList().GetItemAsync(itemToken));
98
}
99
100
return item;
101
}
102
103
std::list<std::string> GetFutureAccessList() {
104
if (alist.empty()) {
105
auto AccessList = StorageApplicationPermissions::FutureAccessList().Entries();
106
for (uint32_t it = 0; it != AccessList.Size(); ++it){
107
auto item = AccessList.GetAt(it);
108
try {
109
auto token = item.Token;
110
if (!token.empty() && StorageApplicationPermissions::FutureAccessList().ContainsItem(token)) {
111
winrt::Windows::Storage::IStorageItem storageItem = nullptr;
112
ExecuteTask(storageItem, StorageApplicationPermissions::FutureAccessList().GetItemAsync(token));
113
if (storageItem != nullptr) {
114
AppendToAccessList(storageItem.Path());
115
}
116
else {
117
StorageApplicationPermissions::FutureAccessList().Remove(token);
118
}
119
}
120
}
121
catch (const winrt::hresult_error&) {
122
}
123
}
124
125
AppendToAccessList(ApplicationData::Current().LocalFolder().Path());
126
AppendToAccessList(ApplicationData::Current().TemporaryFolder().Path());
127
}
128
return alist;
129
}
130
131