blob: 879542c1f2239a3a3dc4cc00af87d09816bb6f3a [file] [log] [blame]
atraczykf5be5462016-08-31 14:23:06 -04001/**************************************************************************
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 Jagerb059a662016-08-30 13:17:30 -040019#pragma once
20#include <pch.h>
21
22using namespace Platform;
atraczyk14ba30c2016-09-22 18:31:59 -040023using namespace Platform::Collections;
Nicolas Jagerb059a662016-08-30 13:17:30 -040024using namespace Windows::Storage;
25
atraczyk14ba30c2016-09-22 18:31:59 -040026typedef Windows::UI::Xaml::Visibility VIS;
27
Nicolas Jagerb059a662016-08-30 13:17:30 -040028namespace RingClientUWP
29{
30namespace Utils
31{
32
33task<bool>
34fileExists(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
atraczyk14ba30c2016-09-22 18:31:59 -040056inline std::string
57makeString(const std::wstring& wstr)
Nicolas Jagerb059a662016-08-30 13:17:30 -040058{
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
atraczyk14ba30c2016-09-22 18:31:59 -040072inline std::wstring
73makeWString(const std::string& str)
Nicolas Jagerb059a662016-08-30 13:17:30 -040074{
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
atraczyk14ba30c2016-09-22 18:31:59 -040088inline std::string
89toString(Platform::String ^str)
Nicolas Jagerb059a662016-08-30 13:17:30 -040090{
91 std::wstring wsstr(str->Data());
92 return makeString(wsstr);
93}
94
atraczyk14ba30c2016-09-22 18:31:59 -040095inline Platform::String^
96toPlatformString(const std::string& str)
Nicolas Jagerb059a662016-08-30 13:17:30 -040097{
98 std::wstring wsstr = makeWString(str);
99 return ref new Platform::String(wsstr.c_str(), wsstr.length());
100}
101
atraczyk14ba30c2016-09-22 18:31:59 -0400102Platform::String^
103Trim(Platform::String^ s)
Nicolas Jager2647eaa2016-08-31 12:08:15 -0400104{
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 Jagerf494bda2016-09-16 08:43:43 -0400117/* fix some issue in the daemon --> <...@...> */
atraczyk14ba30c2016-09-22 18:31:59 -0400118Platform::String^
119TrimRingId(Platform::String^ s)
Nicolas Jagerf494bda2016-09-16 08:43:43 -0400120{
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
atraczyk5c395ea2016-09-20 17:28:09 -0400136/* fix some issue in the daemon --> remove "@..." */
atraczyk14ba30c2016-09-22 18:31:59 -0400137Platform::String^
138TrimRingId2(Platform::String^ s)
atraczyk5c395ea2016-09-20 17:28:09 -0400139{
140 const WCHAR* first = s->Begin();
141 const WCHAR* last = s->End();
142
143 while (first != last && last[-1] != '@')
144 --last;
145
146 last--;
147
148 return ref new Platform::String(first, static_cast<unsigned int>(last - first));
149}
150
atraczyk14ba30c2016-09-22 18:31:59 -0400151Platform::String^
152GetNewGUID()
atraczykf5be5462016-08-31 14:23:06 -0400153{
154 GUID result;
155 HRESULT hr = CoCreateGuid(&result);
156
157 if (SUCCEEDED(hr)) {
158 Guid guid(result);
159 return guid.ToString();
160 }
161
162 throw Exception::CreateException(hr);
Nicolas Jagerb059a662016-08-30 13:17:30 -0400163}
atraczykf5be5462016-08-31 14:23:06 -0400164
atraczyk21e55dd2016-09-19 15:46:55 -0400165std::string
166getStringFromFile(const std::string& filename)
167{
168 std::ifstream file(filename);
169 return std::string((std::istreambuf_iterator<char>(file)),
170 (std::istreambuf_iterator<char>()));
171}
172
atraczyk14ba30c2016-09-22 18:31:59 -0400173inline Map<String^,String^>^
174convertMap(const std::map<std::string, std::string>& m)
175{
176 auto temp = ref new Map<String^,String^>;
177 for (const auto& pair : m) {
178 temp->Insert(
179 Utils::toPlatformString(pair.first),
180 Utils::toPlatformString(pair.second)
181 );
182 }
183 return temp;
184}
185
186inline std::map<std::string, std::string>
187convertMap(Map<String^,String^>^ m)
188{
189 std::map<std::string, std::string> temp;
190 for (const auto& pair : m) {
191 temp.insert(
192 std::make_pair(
193 Utils::toString(pair->Key),
194 Utils::toString(pair->Value)));
195 }
196 return temp;
197}
198
atraczykf5be5462016-08-31 14:23:06 -0400199}
200}