-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmozc_engine_factory.cc
More file actions
79 lines (73 loc) · 2.41 KB
/
Copy pathmozc_engine_factory.cc
File metadata and controls
79 lines (73 loc) · 2.41 KB
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
/*
* Copyright (C) 2020~2020 by CSSlayer
* wengxt@gmail.com
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; see the file COPYING. If not,
* see <http://www.gnu.org/licenses/>.
*/
#include <fcitx-utils/fs.h>
#include <fcitx-utils/misc.h>
#include <fcitx-utils/stringutils.h>
#include <fcitx/addonfactory.h>
#include <fcitx/addoninstance.h>
#include <cstdlib>
#include <string_view>
#include "base/system_util.h"
#include "mozc_engine.h"
#include "unix/fcitx5/i18nwrapper.h"
namespace fcitx {
class MozcEngineFactory : public AddonFactory {
public:
AddonInstance* create(AddonManager* manager) override {
// We don't have a direct way to detect, so we simply try.
auto baseDirectory = makeUniqueCPtr(
realpath(mozc::SystemUtil::GetServerDirectory().data(), nullptr));
int numberOfSlash = 0;
if (baseDirectory) {
std::string_view view(baseDirectory.get());
for (auto c : view) {
if (c == '/') {
numberOfSlash += 1;
}
}
if (view.empty()) {
baseDirectory.reset();
}
}
// Make sure we don't deadloop.
while (baseDirectory && numberOfSlash >= 0) {
auto path = stringutils::joinPath(baseDirectory.get(), "share/locale");
if (fs::isdir(path)) {
registerDomain("fcitx5-mozc", path.data());
}
baseDirectory = cdUp(baseDirectory.get());
if (baseDirectory && std::string_view(baseDirectory.get()).empty()) {
baseDirectory.reset();
}
numberOfSlash -= 1;
}
return new MozcEngine(manager->instance());
}
private:
UniqueCPtr<char> cdUp(const char* path) {
return makeUniqueCPtr(
realpath(stringutils::joinPath(path, "..").data(), nullptr));
}
};
} // namespace fcitx
#ifdef FCITX_ADDON_FACTORY_V2
FCITX_ADDON_FACTORY_V2(mozc, fcitx::MozcEngineFactory)
#else
FCITX_ADDON_FACTORY(fcitx::MozcEngineFactory)
#endif