1 #include "pch.h"
2 #include "ppltasks.h"
3 
4 #include "Common/Thread/ThreadUtil.h"
5 
6 #include "StorageFolderBrowser.h"
7 #include "UWPUtil.h"
8 
9 using namespace Concurrency;
10 using namespace Windows::Storage;
11 using namespace Windows::Storage::Streams;
12 
13 static std::mutex initMutex;
14 
15 StorageFolderBrowser::StorageFolderBrowser(Windows::Storage::StorageFolder ^folder) : folder_(folder) {
__anon36366a800102() 16 	thread_.reset(new std::thread([this]() { this->threadfunc(); }));
17 
18 	path_ = FromPlatformString(folder->Path);
19 	displayName_ = FromPlatformString(folder->DisplayName);
20 }
21 
threadfunc()22 void StorageFolderBrowser::threadfunc() {
23 	SetCurrentThreadName("StorageFileLoader");
24 
25 	initMutex.lock();
26 
27 	/*
28 	auto opentask = create_task(folder_->GetItemsAsync()->OpenReadAsync()).then([this](IRandomAccessStreamWithContentType ^stream) {
29 		stream_ = stream;
30 		active_ = true;
31 	});
32 
33 	try {
34 		opentask.wait();
35 	}
36 	catch (const std::exception& e) {
37 		operationFailed_ = true;
38 		// TODO: What do we do?
39 		const char *what = e.what();
40 		ILOG("%s", what);
41 	}
42 	catch (Platform::COMException ^e) {
43 
44 	}
45 
46 	auto sizetask = create_task(file_->GetBasicPropertiesAsync()).then([this](Windows::Storage::FileProperties::BasicProperties ^props) {
47 		size_ = props->Size;
48 	});
49 	try {
50 		sizetask.wait();
51 	}
52 	catch (const std::exception& e) {
53 		const char *what = e.what();
54 		ILOG("%s", what);
55 	}
56 	catch (Platform::COMException ^e) {
57 		std::string what = FromPlatformString(e->ToString());
58 		ILOG("%s", what.c_str());
59 	}
60 	*/
61 	initMutex.unlock();
62 
63 	std::unique_lock<std::mutex> lock(mutex_);
64 	while (active_) {
65 		if (!operationRequested_) {
66 			cond_.wait(lock);
67 		}
68 		if (operationRequested_) {
69 			switch (operation_.type) {
70 			case OpType::LIST_DIRECTORY: {
71 
72 				/*
73 				Streams::Buffer ^buf = ref new Streams::Buffer(operation_.size);
74 				operationFailed_ = false;
75 				stream_->Seek(operation_.offset);
76 				auto task = create_task(stream_->ReadAsync(buf, operation_.size, Streams::InputStreamOptions::None));
77 				Streams::IBuffer ^output = nullptr;
78 				try {
79 					task.wait();
80 					output = task.get();
81 				}
82 				catch (const std::exception& e) {
83 					operationFailed_ = true;
84 					const char *what = e.what();
85 					ILOG("%s", what);
86 				}
87 				operationRequested_ = false;
88 				std::unique_lock<std::mutex> lock(mutexResponse_);
89 				response_.buffer = output;
90 				responseAvailable_ = true;
91 				condResponse_.notify_one();
92 				break;*/
93 			}
94 			default:
95 				operationRequested_ = false;
96 				break;
97 			}
98 		}
99 	}
100 }
101