blob: f832d7434f667ca65070a80cfa6641f150c10383 [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^
Nicolas Jagerd57809f2016-10-06 11:31:55 -0400138
atraczyk14ba30c2016-09-22 18:31:59 -0400139TrimRingId2(Platform::String^ s)
atraczyk5c395ea2016-09-20 17:28:09 -0400140{
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
atraczyk14ba30c2016-09-22 18:31:59 -0400152Platform::String^
Nicolas Jagerd57809f2016-10-06 11:31:55 -0400153TrimFrom(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
170Platform::String^
atraczyk14ba30c2016-09-22 18:31:59 -0400171GetNewGUID()
atraczykf5be5462016-08-31 14:23:06 -0400172{
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 Jagerb059a662016-08-30 13:17:30 -0400182}
atraczykf5be5462016-08-31 14:23:06 -0400183
atraczyk21e55dd2016-09-19 15:46:55 -0400184std::string
185getStringFromFile(const std::string& filename)
186{
187 std::ifstream file(filename);
188 return std::string((std::istreambuf_iterator<char>(file)),
Nicolas Jagerd57809f2016-10-06 11:31:55 -0400189 (std::istreambuf_iterator<char>()));
atraczyk21e55dd2016-09-19 15:46:55 -0400190}
191
atraczyk14ba30c2016-09-22 18:31:59 -0400192inline Map<String^,String^>^
193convertMap(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
205inline std::map<std::string, std::string>
206convertMap(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
atraczykf5be5462016-08-31 14:23:06 -0400218}
219}