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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
/*
Copyright (C) 2012 Sebastian Herbord. All rights reserved.
This file is part of Mod Organizer.
Mod Organizer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Mod Organizer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Mod Organizer. If not, see <http://www.gnu.org/licenses/>.
*/
#include "util.h"
#include "../env.h"
#include "../mainwindow.h"
#include "windows_error.h"
#include <fluorine_build_info.h>
#include <uibase/log.h>
#include <pthread.h>
#include <algorithm>
#include <cwctype>
#include <filesystem>
using namespace MOBase;
namespace MOShared
{
bool FileExists(const std::string& filename)
{
return std::filesystem::exists(filename);
}
bool FileExists(const std::wstring& filename)
{
return std::filesystem::exists(std::filesystem::path(filename));
}
bool FileExists(const std::wstring& searchPath, const std::wstring& filename)
{
std::wstringstream stream;
stream << searchPath << "\\" << filename;
return FileExists(stream.str());
}
std::string ToString(const std::wstring& source, bool utf8)
{
Q_UNUSED(utf8);
return QString::fromStdWString(source).toStdString();
}
std::wstring ToWString(const std::string& source, bool utf8)
{
Q_UNUSED(utf8);
return QString::fromStdString(source).toStdWString();
}
static std::locale makeUserLocale()
{
// std::locale("") reads LANG/LC_* env vars. If the user's system lacks the
// requested locale (e.g. en_US.UTF-8 not generated), the constructor throws
// runtime_error. Fall back to "C" so startup doesn't abort.
try {
return std::locale("");
} catch (const std::runtime_error&) {
return std::locale::classic();
}
}
static std::locale loc = makeUserLocale();
std::string& ToLowerInPlace(std::string& text)
{
std::transform(text.begin(), text.end(), text.begin(), [](char c) {
return std::tolower(static_cast<unsigned char>(c));
});
return text;
}
std::string ToLowerCopy(const std::string& text)
{
std::string result(text);
return ToLowerInPlace(result);
}
std::wstring& ToLowerInPlace(std::wstring& text)
{
std::transform(text.begin(), text.end(), text.begin(), [](wchar_t c) {
return std::towlower(c);
});
return text;
}
std::wstring ToLowerCopy(const std::wstring& text)
{
std::wstring result(text);
return ToLowerInPlace(result);
}
std::wstring ToLowerCopy(std::wstring_view text)
{
std::wstring result(text.begin(), text.end());
ToLowerInPlace(result);
return result;
}
bool CaseInsenstiveComparePred(wchar_t lhs, wchar_t rhs)
{
return std::tolower(lhs, loc) == std::tolower(rhs, loc);
}
bool CaseInsensitiveEqual(const std::wstring& lhs, const std::wstring& rhs)
{
return (lhs.length() == rhs.length()) &&
std::equal(lhs.begin(), lhs.end(), rhs.begin(),
[](wchar_t lhs, wchar_t rhs) -> bool {
return std::tolower(lhs, loc) == std::tolower(rhs, loc);
});
}
Version createVersionInfo()
{
// Fluorine Manager version is the user-facing one. The numeric components
// are injected by CMake from top-level FLUORINE_VERSION_* variables.
#if FLUORINE_IS_BETA_BUILD
// Beta builds tag themselves as Development pre-releases so the update
// checker can distinguish them from stable tags when comparing versions.
return Version(FLUORINE_VERSION_MAJOR, FLUORINE_VERSION_MINOR,
FLUORINE_VERSION_PATCH, 0, {Version::Development});
#else
return Version(FLUORINE_VERSION_MAJOR, FLUORINE_VERSION_MINOR,
FLUORINE_VERSION_PATCH, 0);
#endif
}
void SetThisThreadName(const QString& s)
{
// pthread_setname_np is limited to 16 chars including the null terminator.
std::string name = s.toStdString();
if (name.size() > 15) {
name.resize(15);
}
pthread_setname_np(pthread_self(), name.c_str());
}
char shortcutChar(const QAction* a)
{
const auto text = a->text();
for (int i = 0; i < text.size(); ++i) {
const auto c = text[i];
if (c == '&') {
if (i >= (text.size() - 1)) {
log::error("ampersand at the end");
return 0;
}
return text[i + 1].toLatin1();
}
}
log::error("action {} has no shortcut", text);
return 0;
}
void checkDuplicateShortcuts(const QMenu& m)
{
const auto actions = m.actions();
for (int i = 0; i < actions.size(); ++i) {
const auto* action1 = actions[i];
if (action1->isSeparator()) {
continue;
}
const char shortcut1 = shortcutChar(action1);
if (shortcut1 == 0) {
continue;
}
for (int j = i + 1; j < actions.size(); ++j) {
const auto* action2 = actions[j];
if (action2->isSeparator()) {
continue;
}
const char shortcut2 = shortcutChar(action2);
if (shortcut1 == shortcut2) {
log::error("duplicate shortcut {} for {} and {}", shortcut1, action1->text(),
action2->text());
break;
}
}
}
}
} // namespace MOShared
static bool g_exiting = false;
static bool g_canClose = false;
MainWindow* findMainWindow()
{
for (auto* tl : qApp->topLevelWidgets()) {
if (auto* mw = dynamic_cast<MainWindow*>(tl)) {
return mw;
}
}
return nullptr;
}
bool ExitModOrganizer(ExitFlags e)
{
if (g_exiting) {
return true;
}
g_exiting = true;
Guard g([&] {
g_exiting = false;
});
if (!e.testFlag(Exit::Force)) {
if (auto* mw = findMainWindow()) {
if (!mw->canExit()) {
return false;
}
}
}
g_canClose = true;
const int code = (e.testFlag(Exit::Restart) ? RestartExitCode : 0);
qApp->exit(code);
return true;
}
bool ModOrganizerCanCloseNow()
{
return g_canClose;
}
bool ModOrganizerExiting()
{
return g_exiting;
}
void ResetExitFlag()
{
g_exiting = false;
}
bool isNxmLink(const QString& link)
{
return link.startsWith("nxm://", Qt::CaseInsensitive) ||
link.startsWith("modl://", Qt::CaseInsensitive);
}
|