forked from duchuule/vba10
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExportPage.xaml.cpp
259 lines (199 loc) · 7.75 KB
/
ExportPage.xaml.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
//
// ExportPage.xaml.cpp
// Implementation of the ExportPage class
//
#include "pch.h"
#include "ExportPage.xaml.h"
#include "App.xaml.h"
#include "SelectFilePane.xaml.h"
#include "SelectFilesPane.xaml.h"
#include "Definitions.h"
#include "ppltasks_extra.h"
#include "AdControl.xaml.h"
using namespace VBA10;
using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Xaml::Data;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Navigation;
using namespace Windows::UI::Popups;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
ExportPage::ExportPage()
{
InitializeComponent();
//create ad control
if (App::HasAds)
{
AdControl^ adControl = ref new AdControl();
LayoutRoot->Children->Append(adControl);
adControl->SetValue(Grid::RowProperty, 2);
}
}
void ExportPage::Page_Loaded(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
//try re-sign in silently because access token expires every 1 hour
if (EmulatorSettings::Current->SignedIn)
{
//live::live_client* LiveClient = new live::live_client();
App::LiveClient->login(L"wl.skydrive_update wl.signin", true)
.then([this](bool isLoggedIn)
{
signin_Completed(isLoggedIn);
});
}
}
void ExportPage::SignInbtn_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
App::LiveClient->login(L"wl.skydrive_update wl.signin", false)
.then([this](bool isLoggedIn)
{
signin_Completed(isLoggedIn);
});
}
void ExportPage::signin_Completed(bool isLoggedIn)
{
auto loader = Windows::ApplicationModel::Resources::ResourceLoader::GetForViewIndependentUse();
if (isLoggedIn)
{
this->SignInbtn->Content = loader->GetString("SignedInText");
this->SignInbtn->IsEnabled = false;
this->exportOneDrivebtn->IsEnabled = true;
EmulatorSettings::Current->SignedIn = true;
//get the export folder id
if (App::ExportFolderID == "")
{
App::LiveClient->get(L"/me/skydrive/files")
.then([this](task<web::json::value> tv)
{
try
{
auto v = tv.get();
//int test = v[L"data"].as_array().size();
for (const auto& it : (v[L"data"]).as_array())
{
auto album = it;
wstring name = album[L"name"].as_string();
wstring type = album[L"type"].as_string();
if (name == EXPORT_FOLDER && (type == L"folder" || type == L"album"))
{
App::ExportFolderID = ref new String(album[L"id"].as_string().c_str());
break;
}
}
if (App::ExportFolderID == "") //need to create the folder
{
web::json::value data;
data[U("name")] = web::json::value::string(EXPORT_FOLDER);
create_task(App::LiveClient->post(L"/me/skydrive", data))
.then([](web::json::value v)
{
App::ExportFolderID = ref new String(v[L"id"].as_string().c_str());
});
}
}
//catch (const concurrency::task_canceled &) {}
catch (...) {}
});
}
}
else
{
this->SignInbtn->Content = loader->GetString("SignInText");
this->SignInbtn->IsEnabled = true;
this->exportOneDrivebtn->IsEnabled = false;
EmulatorSettings::Current->SignedIn = false;
}
}
void ExportPage::exportOneDrivebtn_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
auto loader = Windows::ApplicationModel::Resources::ResourceLoader::GetForViewIndependentUse();
//get a list of rom
Vector<Platform::String ^> ^romNames = ref new Vector<Platform::String ^>();
for (int i = 0; i < App::ROMDB->AllROMDBEntries->Size; i++)
romNames->Append(App::ROMDB->AllROMDBEntries->GetAt(i)->DisplayName);
//open panel to let user select rom
Popup ^statePopup = ref new Popup();
statePopup->IsLightDismissEnabled = true;
SelectFilePane ^pane = ref new SelectFilePane(romNames, loader->GetString("SelectROMText"));
statePopup->Child = pane;
pane->Width = titleBar->ActualWidth;//statePopup->Width;
pane->MaxHeight = Window::Current->Bounds.Height - 48; //statePopup->MaxHeight;
pane->FileSelectedCallback = ref new FileSelectedDelegate([=](int selectedIndex)
{
ROMDBEntry^ entry = App::ROMDB->AllROMDBEntries->GetAt(selectedIndex);
//get list of save files
Search::QueryOptions ^options = ref new Search::QueryOptions();
options->FileTypeFilter->Append("*");
options->IndexerOption = Search::IndexerOption::DoNotUseIndexer;
options->UserSearchFilter = "\"" + entry->DisplayName + "\"";
create_task(entry->Folder->CreateFileQueryWithOptions(options)->GetFilesAsync())
.then([this, loader](IVectorView<StorageFile ^> ^files)
{
//open panel to let user select file
Popup ^statePopup = ref new Popup();
statePopup->IsLightDismissEnabled = true;
Vector<Platform::String ^> ^fileNames = ref new Vector<Platform::String ^>();
for (int i = 0; i < files->Size; i++)
fileNames->Append(files->GetAt(i)->Name);
SelectFilesPane ^pane = ref new SelectFilesPane(fileNames, loader->GetString("SelectFileExportText"));
statePopup->Child = pane;
pane->Width = titleBar->ActualWidth;//statePopup->Width;
pane->MaxHeight = Window::Current->Bounds.Height - 48; //statePopup->MaxHeight;
pane->FilesSelectedCallback = ref new FilesSelectedDelegate([=](IVector<int>^ selectedIndices)
{
if (App::ExportFolderID != "")
{
vector<task<web::json::value>> tasks;
for (int i = 0; i < selectedIndices->Size; i++)
{
auto file = files->GetAt(selectedIndices->GetAt(i));
String^ path = App::ExportFolderID + L"/files/" + file->Name; //need to handle space in name
tasks.emplace_back(App::LiveClient->upload(web::uri::encode_uri(path->Data()), file));
}
when_all(begin(tasks), end(tasks)).then([this, loader](task<std::vector<web::json::value>> t)
{
try
{
t.get();
this->Dispatcher->RunAsync(CoreDispatcherPriority::Normal, ref new DispatchedHandler([loader]()
{
MessageDialog ^dialog = ref new MessageDialog(loader->GetString("UploadSuccessText"));
dialog->ShowAsync();
}));
}
catch (const std::exception &)
{
this->Dispatcher->RunAsync(CoreDispatcherPriority::Normal, ref new DispatchedHandler([loader]()
{
MessageDialog ^dialog = ref new MessageDialog(loader->GetString("NetworkErrorText"));
dialog->ShowAsync();
}));
}
catch (Exception^) {}
});
}
else
{
MessageDialog ^dialog = ref new MessageDialog(loader->GetString("ExportFolderError"), loader->GetString("ErrorText"));
dialog->ShowAsync();
}
//int test = selectedIndices->Size;
});
auto transform = ((UIElement^)titleBar)->TransformToVisual(nullptr);
Windows::Foundation::Point point = transform->TransformPoint(Windows::Foundation::Point());
statePopup->HorizontalOffset = point.X + 1; //+ selectStateBtn->ActualWidth / 2.0f - pane->Width / 2.0f;
statePopup->VerticalOffset = point.Y + titleBar->ActualHeight;
statePopup->IsOpen = true;
});
});
auto transform = ((UIElement^)titleBar)->TransformToVisual(nullptr);
Windows::Foundation::Point point = transform->TransformPoint(Windows::Foundation::Point());
statePopup->HorizontalOffset = point.X + 1; //+ selectStateBtn->ActualWidth / 2.0f - pane->Width / 2.0f;
statePopup->VerticalOffset = point.Y + titleBar->ActualHeight;
statePopup->IsOpen = true;
}