atraczyk | f5be546 | 2016-08-31 14:23:06 -0400 | [diff] [blame] | 1 | /**************************************************************************
|
| 2 | * Copyright (C) 2016 by Savoir-faire Linux *
|
| 3 | * Author: Jäger Nicolas <nicolas.jager@savoirfairelinux.com> *
|
| 4 | * Author: Traczyk Andreas <traczyk.andreas@savoirfairelinux.com> *
|
| 5 | * *
|
| 6 | * This program is free software; you can redistribute it and/or modify *
|
| 7 | * it under the terms of the GNU General Public License as published by *
|
| 8 | * the Free Software Foundation; either version 3 of the License, or *
|
| 9 | * (at your option) any later version. *
|
| 10 | * *
|
| 11 | * This program is distributed in the hope that it will be useful, *
|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
| 14 | * GNU General Public License for more details. *
|
| 15 | * *
|
| 16 | * You should have received a copy of the GNU General Public License *
|
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
| 18 | **************************************************************************/
|
Nicolas Jager | b059a66 | 2016-08-30 13:17:30 -0400 | [diff] [blame] | 19 | #pragma once
|
| 20 | #include <pch.h>
|
| 21 |
|
| 22 | using namespace Platform;
|
atraczyk | 14ba30c | 2016-09-22 18:31:59 -0400 | [diff] [blame] | 23 | using namespace Platform::Collections;
|
Nicolas Jager | b059a66 | 2016-08-30 13:17:30 -0400 | [diff] [blame] | 24 | using namespace Windows::Storage;
|
| 25 |
|
atraczyk | 14ba30c | 2016-09-22 18:31:59 -0400 | [diff] [blame] | 26 | typedef Windows::UI::Xaml::Visibility VIS;
|
| 27 |
|
Nicolas Jager | b059a66 | 2016-08-30 13:17:30 -0400 | [diff] [blame] | 28 | namespace RingClientUWP
|
| 29 | {
|
| 30 | namespace Utils
|
| 31 | {
|
| 32 |
|
| 33 | task<bool>
|
| 34 | fileExists(StorageFolder^ folder, String^ fileName)
|
| 35 | {
|
| 36 | return create_task(folder->GetFileAsync(fileName))
|
| 37 | .then([](task<StorageFile^> taskResult)
|
| 38 | {
|
| 39 | bool exists;
|
| 40 | try {
|
| 41 | taskResult.get();
|
| 42 | exists = true;
|
| 43 | }
|
| 44 | catch (COMException ^e) {
|
| 45 | if (e->HResult == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) {
|
| 46 | exists = false;
|
| 47 | }
|
| 48 | else {
|
| 49 | throw;
|
| 50 | }
|
| 51 | }
|
| 52 | return exists;
|
| 53 | });
|
| 54 | }
|
| 55 |
|
atraczyk | 14ba30c | 2016-09-22 18:31:59 -0400 | [diff] [blame] | 56 | inline std::string
|
| 57 | makeString(const std::wstring& wstr)
|
Nicolas Jager | b059a66 | 2016-08-30 13:17:30 -0400 | [diff] [blame] | 58 | {
|
| 59 | auto wideData = wstr.c_str();
|
| 60 | int bufferSize = WideCharToMultiByte(CP_UTF8, 0, wideData, -1, nullptr, 0, NULL, NULL);
|
| 61 |
|
| 62 | std::unique_ptr<char[]> utf8;
|
| 63 | utf8.reset(new char[bufferSize]);
|
| 64 |
|
| 65 | if (WideCharToMultiByte(CP_UTF8, 0, wideData, -1, utf8.get(), bufferSize, NULL, NULL) == 0) {
|
| 66 | return std::string();
|
| 67 | }
|
| 68 |
|
| 69 | return std::string(utf8.get());
|
| 70 | }
|
| 71 |
|
atraczyk | 14ba30c | 2016-09-22 18:31:59 -0400 | [diff] [blame] | 72 | inline std::wstring
|
| 73 | makeWString(const std::string& str)
|
Nicolas Jager | b059a66 | 2016-08-30 13:17:30 -0400 | [diff] [blame] | 74 | {
|
| 75 | auto utf8Data = str.c_str();
|
| 76 | int bufferSize = MultiByteToWideChar(CP_UTF8, 0, utf8Data, -1, nullptr, 0);
|
| 77 |
|
| 78 | std::unique_ptr<wchar_t[]> wide;
|
| 79 | wide.reset(new wchar_t[bufferSize]);
|
| 80 |
|
| 81 | if (MultiByteToWideChar(CP_UTF8, 0, utf8Data, -1, wide.get(), bufferSize) == 0) {
|
| 82 | return std::wstring();
|
| 83 | }
|
| 84 |
|
| 85 | return std::wstring(wide.get());;
|
| 86 | }
|
| 87 |
|
atraczyk | 14ba30c | 2016-09-22 18:31:59 -0400 | [diff] [blame] | 88 | inline std::string
|
| 89 | toString(Platform::String ^str)
|
Nicolas Jager | b059a66 | 2016-08-30 13:17:30 -0400 | [diff] [blame] | 90 | {
|
| 91 | std::wstring wsstr(str->Data());
|
| 92 | return makeString(wsstr);
|
| 93 | }
|
| 94 |
|
atraczyk | 14ba30c | 2016-09-22 18:31:59 -0400 | [diff] [blame] | 95 | inline Platform::String^
|
| 96 | toPlatformString(const std::string& str)
|
Nicolas Jager | b059a66 | 2016-08-30 13:17:30 -0400 | [diff] [blame] | 97 | {
|
| 98 | std::wstring wsstr = makeWString(str);
|
| 99 | return ref new Platform::String(wsstr.c_str(), wsstr.length());
|
| 100 | }
|
| 101 |
|
atraczyk | 14ba30c | 2016-09-22 18:31:59 -0400 | [diff] [blame] | 102 | Platform::String^
|
| 103 | Trim(Platform::String^ s)
|
Nicolas Jager | 2647eaa | 2016-08-31 12:08:15 -0400 | [diff] [blame] | 104 | {
|
| 105 | const WCHAR* first = s->Begin();
|
| 106 | const WCHAR* last = s->End();
|
| 107 |
|
| 108 | while (first != last && iswspace(*first))
|
| 109 | ++first;
|
| 110 |
|
| 111 | while (first != last && iswspace(last[-1]))
|
| 112 | --last;
|
| 113 |
|
| 114 | return ref new Platform::String(first, static_cast<unsigned int>(last - first));
|
| 115 | }
|
| 116 |
|
Nicolas Jager | f494bda | 2016-09-16 08:43:43 -0400 | [diff] [blame] | 117 | /* fix some issue in the daemon --> <...@...> */
|
atraczyk | 14ba30c | 2016-09-22 18:31:59 -0400 | [diff] [blame] | 118 | Platform::String^
|
| 119 | TrimRingId(Platform::String^ s)
|
Nicolas Jager | f494bda | 2016-09-16 08:43:43 -0400 | [diff] [blame] | 120 | {
|
| 121 | const WCHAR* first = s->Begin();
|
| 122 | const WCHAR* last = s->End();
|
| 123 |
|
| 124 | while (first != last && *first != '<')
|
| 125 | ++first;
|
| 126 |
|
| 127 | while (first != last && last[-1] != '@')
|
| 128 | --last;
|
| 129 |
|
| 130 | first++;
|
| 131 | last--;
|
| 132 |
|
| 133 | return ref new Platform::String(first, static_cast<unsigned int>(last - first));
|
| 134 | }
|
| 135 |
|
atraczyk | 5c395ea | 2016-09-20 17:28:09 -0400 | [diff] [blame] | 136 | /* fix some issue in the daemon --> remove "@..." */
|
atraczyk | 14ba30c | 2016-09-22 18:31:59 -0400 | [diff] [blame] | 137 | Platform::String^
|
Nicolas Jager | d57809f | 2016-10-06 11:31:55 -0400 | [diff] [blame] | 138 |
|
atraczyk | 14ba30c | 2016-09-22 18:31:59 -0400 | [diff] [blame] | 139 | TrimRingId2(Platform::String^ s)
|
atraczyk | 5c395ea | 2016-09-20 17:28:09 -0400 | [diff] [blame] | 140 | {
|
| 141 | const WCHAR* first = s->Begin();
|
| 142 | const WCHAR* last = s->End();
|
| 143 |
|
| 144 | while (first != last && last[-1] != '@')
|
| 145 | --last;
|
| 146 |
|
| 147 | last--;
|
| 148 |
|
| 149 | return ref new Platform::String(first, static_cast<unsigned int>(last - first));
|
| 150 | }
|
| 151 |
|
atraczyk | 14ba30c | 2016-09-22 18:31:59 -0400 | [diff] [blame] | 152 | Platform::String^
|
Nicolas Jager | d57809f | 2016-10-06 11:31:55 -0400 | [diff] [blame] | 153 | TrimFrom(Platform::String^ s)
|
| 154 | {
|
| 155 | const WCHAR* first = s->Begin();
|
| 156 | const WCHAR* last = s->End();
|
| 157 |
|
| 158 | while (first != last && *first != ':')
|
| 159 | ++first;
|
| 160 |
|
| 161 | while (first != last && last[-1] != '>')
|
| 162 | --last;
|
| 163 |
|
| 164 | first++;
|
| 165 | last--;
|
| 166 |
|
| 167 | return ref new Platform::String(first, static_cast<unsigned int>(last - first));
|
| 168 | }
|
| 169 |
|
| 170 | Platform::String^
|
atraczyk | 14ba30c | 2016-09-22 18:31:59 -0400 | [diff] [blame] | 171 | GetNewGUID()
|
atraczyk | f5be546 | 2016-08-31 14:23:06 -0400 | [diff] [blame] | 172 | {
|
| 173 | GUID result;
|
| 174 | HRESULT hr = CoCreateGuid(&result);
|
| 175 |
|
| 176 | if (SUCCEEDED(hr)) {
|
| 177 | Guid guid(result);
|
| 178 | return guid.ToString();
|
| 179 | }
|
| 180 |
|
| 181 | throw Exception::CreateException(hr);
|
Nicolas Jager | b059a66 | 2016-08-30 13:17:30 -0400 | [diff] [blame] | 182 | }
|
atraczyk | f5be546 | 2016-08-31 14:23:06 -0400 | [diff] [blame] | 183 |
|
atraczyk | 21e55dd | 2016-09-19 15:46:55 -0400 | [diff] [blame] | 184 | std::string
|
| 185 | getStringFromFile(const std::string& filename)
|
| 186 | {
|
| 187 | std::ifstream file(filename);
|
| 188 | return std::string((std::istreambuf_iterator<char>(file)),
|
Nicolas Jager | d57809f | 2016-10-06 11:31:55 -0400 | [diff] [blame] | 189 | (std::istreambuf_iterator<char>()));
|
atraczyk | 21e55dd | 2016-09-19 15:46:55 -0400 | [diff] [blame] | 190 | }
|
| 191 |
|
atraczyk | 14ba30c | 2016-09-22 18:31:59 -0400 | [diff] [blame] | 192 | inline Map<String^,String^>^
|
| 193 | convertMap(const std::map<std::string, std::string>& m)
|
| 194 | {
|
| 195 | auto temp = ref new Map<String^,String^>;
|
| 196 | for (const auto& pair : m) {
|
| 197 | temp->Insert(
|
| 198 | Utils::toPlatformString(pair.first),
|
| 199 | Utils::toPlatformString(pair.second)
|
| 200 | );
|
| 201 | }
|
| 202 | return temp;
|
| 203 | }
|
| 204 |
|
| 205 | inline std::map<std::string, std::string>
|
| 206 | convertMap(Map<String^,String^>^ m)
|
| 207 | {
|
| 208 | std::map<std::string, std::string> temp;
|
| 209 | for (const auto& pair : m) {
|
| 210 | temp.insert(
|
| 211 | std::make_pair(
|
| 212 | Utils::toString(pair->Key),
|
| 213 | Utils::toString(pair->Value)));
|
| 214 | }
|
| 215 | return temp;
|
| 216 | }
|
| 217 |
|
atraczyk | f5be546 | 2016-08-31 14:23:06 -0400 | [diff] [blame] | 218 | }
|
| 219 | }
|