| 1 |
/*** |
| 2 |
* Copyright (C) Microsoft. All rights reserved. |
| 3 |
* Licensed under the MIT license. See LICENSE.txt file in the project root for full license information. |
| 4 |
****/ |
| 5 |
|
| 6 |
#include "pch.h" |
| 7 |
|
| 8 |
#include "MainPage.xaml.h" |
| 9 |
|
| 10 |
#include "cpprest/filestream.h" |
| 11 |
|
| 12 |
using namespace WindowsLiveAuth; |
| 13 |
|
| 14 |
using namespace Platform; |
| 15 |
using namespace Windows::Foundation; |
| 16 |
using namespace Windows::Foundation::Collections; |
| 17 |
using namespace Windows::UI::Xaml; |
| 18 |
using namespace Windows::UI::Xaml::Controls; |
| 19 |
using namespace Windows::UI::Xaml::Controls::Primitives; |
| 20 |
using namespace Windows::UI::Xaml::Data; |
| 21 |
using namespace Windows::UI::Xaml::Input; |
| 22 |
using namespace Windows::UI::Xaml::Media; |
| 23 |
using namespace Windows::UI::Xaml::Navigation; |
| 24 |
|
| 25 |
using namespace Platform::Collections; |
| 26 |
using namespace Windows::Security::Authentication::OnlineId; |
| 27 |
|
| 28 |
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238 |
| 29 |
|
| 30 |
MainPage::MainPage() { InitializeComponent(); } |
| 31 |
|
| 32 |
/// <summary> |
| 33 |
/// Invoked when this page is about to be displayed in a Frame. |
| 34 |
/// </summary> |
| 35 |
/// <param name="e">Event data that describes how this page was reached. The Parameter |
| 36 |
/// property is typically used to configure the page.</param> |
| 37 |
void MainPage::OnNavigatedTo(NavigationEventArgs ^ e) |
| 38 |
{ |
| 39 |
(void)e; // Unused parameter |
| 40 |
} |
| 41 |
|
| 42 |
static web::live::live_client lv_client; |
| 43 |
|
| 44 |
void MainPage::Button_Click_1(Platform::Object ^ sender, Windows::UI::Xaml::RoutedEventArgs ^ e) |
| 45 |
{ |
| 46 |
try |
| 47 |
{ |
| 48 |
auto ui_ctx = pplx::task_continuation_context::use_current(); |
| 49 |
|
| 50 |
std::vector<utility::string_t> scopes; |
| 51 |
scopes.push_back(web::live::scopes::wl_basic); |
| 52 |
scopes.push_back(web::live::scopes::wl_skydrive); |
| 53 |
scopes.push_back(web::live::scopes::wl_skydrive_update); |
| 54 |
lv_client.login(std::begin(scopes), std::end(scopes)) |
| 55 |
.then( |
| 56 |
[this](bool ok) { |
| 57 |
if (ok) |
| 58 |
{ |
| 59 |
this->LogOutButton->Visibility = Windows::UI::Xaml::Visibility::Visible; |
| 60 |
this->LogInButton->Visibility = Windows::UI::Xaml::Visibility::Collapsed; |
| 61 |
|
| 62 |
this->Block1->Text = |
| 63 |
ref new Platform::String((L"access_token = \n" + lv_client.access_token()).c_str()); |
| 64 |
} |
| 65 |
}, |
| 66 |
ui_ctx); |
| 67 |
} |
| 68 |
catch (...) |
| 69 |
{ |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
void MainPage::LogOutButton_Click(Platform::Object ^ sender, Windows::UI::Xaml::RoutedEventArgs ^ e) |
| 74 |
{ |
| 75 |
auto ui_ctx = pplx::task_continuation_context::use_current(); |
| 76 |
|
| 77 |
lv_client.logout().then( |
| 78 |
[this](bool) { |
| 79 |
this->LogOutButton->Visibility = Windows::UI::Xaml::Visibility::Collapsed; |
| 80 |
this->LogInButton->Visibility = Windows::UI::Xaml::Visibility::Visible; |
| 81 |
}, |
| 82 |
ui_ctx); |
| 83 |
} |
| 84 |
|
| 85 |
// The following functions let you get information for an arbitrary WL resource, upload a file, or download a file. |
| 86 |
// Use the Live Connect Interactive SDK on MSDN to explore your WL data and then try the same here. |
| 87 |
// |
| 88 |
// Some other things to try: |
| 89 |
// |
| 90 |
// delete a file using lv_client.remove() |
| 91 |
// copy or move a file using lv_client.copy() and lv_client.move(). |
| 92 |
// create a contact using lv_client.post() |
| 93 |
// modify a calendar event using lv_client.put() |
| 94 |
// |
| 95 |
void MainPage::Button_Click_2(Platform::Object ^ sender, Windows::UI::Xaml::RoutedEventArgs ^ e) |
| 96 |
{ |
| 97 |
auto ui_ctx = pplx::task_continuation_context::use_current(); |
| 98 |
|
| 99 |
lv_client.get(this->Box1->Text->Data()) |
| 100 |
.then( |
| 101 |
[this](pplx::task<web::json::value> value) { |
| 102 |
try |
| 103 |
{ |
| 104 |
auto str = value.get().serialize(); |
| 105 |
this->Block1->Text = ref new Platform::String(str.c_str()); |
| 106 |
} |
| 107 |
catch (std::exception& exc) |
| 108 |
{ |
| 109 |
this->Block1->Text = |
| 110 |
ref new Platform::String(utility::conversions::to_string_t(exc.what()).c_str()); |
| 111 |
} |
| 112 |
}, |
| 113 |
ui_ctx); |
| 114 |
} |
| 115 |
|
| 116 |
void MainPage::UploadButton_Click(Platform::Object ^ sender, Windows::UI::Xaml::RoutedEventArgs ^ e) |
| 117 |
{ |
| 118 |
this->Block1->Text = ref new Platform::String(L"Processing request..."); |
| 119 |
|
| 120 |
auto ui_ctx = pplx::task_continuation_context::use_current(); |
| 121 |
|
| 122 |
auto filePicker = ref new Windows::Storage::Pickers::FileOpenPicker(); |
| 123 |
filePicker->ViewMode = Windows::Storage::Pickers::PickerViewMode::List; |
| 124 |
filePicker->FileTypeFilter->Append(ref new Platform::String(L".txt")); |
| 125 |
filePicker->FileTypeFilter->Append(ref new Platform::String(L".jpg")); |
| 126 |
filePicker->FileTypeFilter->Append(ref new Platform::String(L".pdf")); |
| 127 |
filePicker->FileTypeFilter->Append(ref new Platform::String(L".docx")); |
| 128 |
filePicker->FileTypeFilter->Append(ref new Platform::String(L".doc")); |
| 129 |
|
| 130 |
auto file = filePicker->PickSingleFileAsync(); |
| 131 |
|
| 132 |
utility::string_t path = this->Box1->Text->Data(); |
| 133 |
|
| 134 |
pplx::create_task(file) |
| 135 |
.then([path](Windows::Storage::StorageFile ^ file) { |
| 136 |
if (file == nullptr) |
| 137 |
{ |
| 138 |
throw std::exception("No file was picked"); |
| 139 |
} |
| 140 |
|
| 141 |
auto full_path = path + L"/" + file->Name->Data(); |
| 142 |
|
| 143 |
return lv_client.upload(full_path, file); |
| 144 |
}) |
| 145 |
.then( |
| 146 |
[this](pplx::task<web::json::value> response) { |
| 147 |
try |
| 148 |
{ |
| 149 |
auto message = response.get().serialize(); |
| 150 |
this->Block1->Text = ref new Platform::String(message.c_str()); |
| 151 |
} |
| 152 |
catch (std::exception& exc) |
| 153 |
{ |
| 154 |
this->Block1->Text = |
| 155 |
ref new Platform::String(utility::conversions::to_string_t(exc.what()).c_str()); |
| 156 |
} |
| 157 |
}, |
| 158 |
ui_ctx); |
| 159 |
} |
| 160 |
|
| 161 |
void MainPage::DownloadButton_Click(Platform::Object ^ sender, Windows::UI::Xaml::RoutedEventArgs ^ e) |
| 162 |
{ |
| 163 |
this->Block1->Text = ref new Platform::String(L"Processing request..."); |
| 164 |
|
| 165 |
auto ui_ctx = pplx::task_continuation_context::use_current(); |
| 166 |
utility::string_t path = this->Box1->Text->Data(); |
| 167 |
|
| 168 |
// Start by getting the file metadata from OneDrive. We need the file name. |
| 169 |
lv_client.get(path) |
| 170 |
.then([this](web::json::value file_info) { |
| 171 |
if (!file_info.is_object()) throw std::exception("unexpected file info response format"); |
| 172 |
|
| 173 |
auto name = file_info[L"name"].as_string(); |
| 174 |
|
| 175 |
// Once we have the name, we can create a storage file in the downloads folder. |
| 176 |
|
| 177 |
return pplx::create_task(Windows::Storage::DownloadsFolder::CreateFileAsync( |
| 178 |
ref new Platform::String(name.c_str()), Windows::Storage::CreationCollisionOption::GenerateUniqueName)); |
| 179 |
}) |
| 180 |
.then([path, ui_ctx, this](Windows::Storage::StorageFile ^ file) { |
| 181 |
if (file == nullptr) throw std::exception("unexpected file info response format"); |
| 182 |
auto name = file->Name; |
| 183 |
// With a file reference in hand, we download the file. |
| 184 |
return lv_client.download(path, file); |
| 185 |
}) |
| 186 |
.then( |
| 187 |
[this](pplx::task<size_t> response) { |
| 188 |
try |
| 189 |
{ |
| 190 |
response.wait(); |
| 191 |
this->Block1->Text = ref new Platform::String(L"Download complete."); |
| 192 |
} |
| 193 |
catch (std::exception& exc) |
| 194 |
{ |
| 195 |
this->Block1->Text = |
| 196 |
ref new Platform::String(utility::conversions::to_string_t(exc.what()).c_str()); |
| 197 |
} |
| 198 |
}, |
| 199 |
ui_ctx); |
| 200 |
} |