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
|
/***
* Copyright (C) Microsoft. All rights reserved.
* Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
****/
#include "pch.h"
#include "MainPage.xaml.h"
#include "cpprest/filestream.h"
using namespace WindowsLiveAuth;
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 Platform::Collections;
using namespace Windows::Security::Authentication::OnlineId;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
MainPage::MainPage() { InitializeComponent(); }
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The Parameter
/// property is typically used to configure the page.</param>
void MainPage::OnNavigatedTo(NavigationEventArgs ^ e)
{
(void)e; // Unused parameter
}
static web::live::live_client lv_client;
void MainPage::Button_Click_1(Platform::Object ^ sender, Windows::UI::Xaml::RoutedEventArgs ^ e)
{
try
{
auto ui_ctx = pplx::task_continuation_context::use_current();
std::vector<utility::string_t> scopes;
scopes.push_back(web::live::scopes::wl_basic);
scopes.push_back(web::live::scopes::wl_skydrive);
scopes.push_back(web::live::scopes::wl_skydrive_update);
lv_client.login(std::begin(scopes), std::end(scopes))
.then(
[this](bool ok) {
if (ok)
{
this->LogOutButton->Visibility = Windows::UI::Xaml::Visibility::Visible;
this->LogInButton->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
this->Block1->Text =
ref new Platform::String((L"access_token = \n" + lv_client.access_token()).c_str());
}
},
ui_ctx);
}
catch (...)
{
}
}
void MainPage::LogOutButton_Click(Platform::Object ^ sender, Windows::UI::Xaml::RoutedEventArgs ^ e)
{
auto ui_ctx = pplx::task_continuation_context::use_current();
lv_client.logout().then(
[this](bool) {
this->LogOutButton->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
this->LogInButton->Visibility = Windows::UI::Xaml::Visibility::Visible;
},
ui_ctx);
}
// The following functions let you get information for an arbitrary WL resource, upload a file, or download a file.
// Use the Live Connect Interactive SDK on MSDN to explore your WL data and then try the same here.
//
// Some other things to try:
//
// delete a file using lv_client.remove()
// copy or move a file using lv_client.copy() and lv_client.move().
// create a contact using lv_client.post()
// modify a calendar event using lv_client.put()
//
void MainPage::Button_Click_2(Platform::Object ^ sender, Windows::UI::Xaml::RoutedEventArgs ^ e)
{
auto ui_ctx = pplx::task_continuation_context::use_current();
lv_client.get(this->Box1->Text->Data())
.then(
[this](pplx::task<web::json::value> value) {
try
{
auto str = value.get().serialize();
this->Block1->Text = ref new Platform::String(str.c_str());
}
catch (std::exception& exc)
{
this->Block1->Text =
ref new Platform::String(utility::conversions::to_string_t(exc.what()).c_str());
}
},
ui_ctx);
}
void MainPage::UploadButton_Click(Platform::Object ^ sender, Windows::UI::Xaml::RoutedEventArgs ^ e)
{
this->Block1->Text = ref new Platform::String(L"Processing request...");
auto ui_ctx = pplx::task_continuation_context::use_current();
auto filePicker = ref new Windows::Storage::Pickers::FileOpenPicker();
filePicker->ViewMode = Windows::Storage::Pickers::PickerViewMode::List;
filePicker->FileTypeFilter->Append(ref new Platform::String(L".txt"));
filePicker->FileTypeFilter->Append(ref new Platform::String(L".jpg"));
filePicker->FileTypeFilter->Append(ref new Platform::String(L".pdf"));
filePicker->FileTypeFilter->Append(ref new Platform::String(L".docx"));
filePicker->FileTypeFilter->Append(ref new Platform::String(L".doc"));
auto file = filePicker->PickSingleFileAsync();
utility::string_t path = this->Box1->Text->Data();
pplx::create_task(file)
.then([path](Windows::Storage::StorageFile ^ file) {
if (file == nullptr)
{
throw std::exception("No file was picked");
}
auto full_path = path + L"/" + file->Name->Data();
return lv_client.upload(full_path, file);
})
.then(
[this](pplx::task<web::json::value> response) {
try
{
auto message = response.get().serialize();
this->Block1->Text = ref new Platform::String(message.c_str());
}
catch (std::exception& exc)
{
this->Block1->Text =
ref new Platform::String(utility::conversions::to_string_t(exc.what()).c_str());
}
},
ui_ctx);
}
void MainPage::DownloadButton_Click(Platform::Object ^ sender, Windows::UI::Xaml::RoutedEventArgs ^ e)
{
this->Block1->Text = ref new Platform::String(L"Processing request...");
auto ui_ctx = pplx::task_continuation_context::use_current();
utility::string_t path = this->Box1->Text->Data();
// Start by getting the file metadata from OneDrive. We need the file name.
lv_client.get(path)
.then([this](web::json::value file_info) {
if (!file_info.is_object()) throw std::exception("unexpected file info response format");
auto name = file_info[L"name"].as_string();
// Once we have the name, we can create a storage file in the downloads folder.
return pplx::create_task(Windows::Storage::DownloadsFolder::CreateFileAsync(
ref new Platform::String(name.c_str()), Windows::Storage::CreationCollisionOption::GenerateUniqueName));
})
.then([path, ui_ctx, this](Windows::Storage::StorageFile ^ file) {
if (file == nullptr) throw std::exception("unexpected file info response format");
auto name = file->Name;
// With a file reference in hand, we download the file.
return lv_client.download(path, file);
})
.then(
[this](pplx::task<size_t> response) {
try
{
response.wait();
this->Block1->Text = ref new Platform::String(L"Download complete.");
}
catch (std::exception& exc)
{
this->Block1->Text =
ref new Platform::String(utility::conversions::to_string_t(exc.what()).c_str());
}
},
ui_ctx);
}
|