Debian Codegraph @

src/dialogs/config/config_language.cpp

raw symbols SCIP
1 /* fre:ac - free audio converter
2 * Copyright (C) 2001-2023 Robert Kausch <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation, either version 2 of
7 * the License, or (at your option) any later version.
8 *
9 * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
10 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
11 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */
12
13 #include <dialogs/config/config_language.h>
14
15 #include <config.h>
16
17 #ifdef __WIN32__
18 # include <windows.h>
19 #else
20 # include <stdlib.h>
21 # include <unistd.h>
22 #endif
23
24 freac::ConfigureLanguage::ConfigureLanguage()
25 {
26 BoCA::Config *config = BoCA::Config::Get();
27 BoCA::I18n *i18n = BoCA::I18n::Get();
28
29 i18n->SetContext("Configuration::Language");
30
31 group_info = new GroupBox(i18n->TranslateString("Information"), Point(7, 66), Size(552, 77));
32
33 text_info = new Text(NIL, Point(9, 11));
34 text_info_data = new Text(NIL, Point(9, 11));
35 link_url = new Hyperlink(String(), NIL, Point(37, text_info->GetFont().GetUnscaledTextSizeY() * 3 + 20));
36
37 group_info->Add(text_info);
38 group_info->Add(text_info_data);
39 group_info->Add(link_url);
40
41 group_language = new GroupBox(i18n->TranslateString("Language"), Point(7, 11), Size(552, 43));
42
43 text_language = new Text(i18n->AddColon(i18n->TranslateString("Select language")), Point(9, 15));
44
45 combo_language = new ComboBox(Point(text_language->GetUnscaledTextWidth() + 17, 12), Size(442, 0));
46 combo_language->onSelectEntry.Connect(&ConfigureLanguage::SelectLanguage, this);
47
48 group_language->Add(text_language);
49 group_language->Add(combo_language);
50
51 btn_edit = new Button(i18n->TranslateString("Edit language file"), Point(390, 11), Size(130, 0));
52 btn_edit->onAction.Connect(&ConfigureLanguage::EditLanguageFile, this);
53
54 btn_edit->SetWidth(Math::Max(80, btn_edit->GetUnscaledTextWidth() + 14));
55 btn_edit->SetX(542 - btn_edit->GetWidth());
56
57 combo_language->SetWidth(525 - text_language->GetUnscaledTextWidth());
58
59 for (Int i = 0; i < i18n->GetNOfLanguages(); i++)
60 {
61 combo_language->AddEntry(i18n->GetNthLanguageName(i));
62
63 if (config->GetStringValue(Config::CategorySettingsID, Config::SettingsLanguageID, Config::SettingsLanguageDefault) == i18n->GetNthLanguageID(i)) combo_language->SelectNthEntry(i);
64 }
65
66 #if defined __WIN32__
67 if (File(GUI::Application::GetApplicationDirectory().Append("translator.exe")).Exists())
68 #elif defined __APPLE__
69 if (Directory(GUI::Application::GetApplicationDirectory().Append("translator.app")).Exists())
70 #else
71 if (File(GUI::Application::GetApplicationDirectory().Append("smooth-translator")).Exists() ||
72 File(GUI::Application::GetApplicationDirectory().Append("translator")).Exists())
73 #endif
74 {
75 combo_language->SetWidth(combo_language->GetWidth() - btn_edit->GetWidth() - 8);
76
77 group_language->Add(btn_edit);
78 }
79
80 SelectLanguage();
81
82 Add(group_language);
83 Add(group_info);
84
85 SetSize(Size(566, 180));
86 }
87
88 freac::ConfigureLanguage::~ConfigureLanguage()
89 {
90 DeleteObject(group_language);
91 DeleteObject(text_language);
92 DeleteObject(combo_language);
93 DeleteObject(btn_edit);
94
95 DeleteObject(group_info);
96 DeleteObject(text_info);
97 DeleteObject(text_info_data);
98 DeleteObject(link_url);
99 }
100
101 Void freac::ConfigureLanguage::SelectLanguage()
102 {
103 BoCA::I18n *i18n = BoCA::I18n::Get();
104
105 i18n->SetContext("Configuration::Language");
106
107 if (combo_language->GetSelectedEntry() != NIL)
108 {
109 Int numAuthorLines = i18n->GetNthLanguageAuthor(combo_language->GetSelectedEntryNumber()).Explode("\n").Length();
110 String authorLineBreaks = String().FillN('\n', numAuthorLines);
111
112 text_info->SetText(String(i18n->AddColon(i18n->TranslateString("Language"))).Append(" \n")
113 .Append(i18n->AddColon(i18n->TranslateString("Encoding"))).Append(" \n")
114 .Append(i18n->AddColon(i18n->TranslateString("Author(s)"))).Append(" ").Append(authorLineBreaks)
115 .Append(i18n->AddColon(i18n->TranslateString("URL"))).Append(" "));
116
117 text_info_data->SetText(String(i18n->GetNthLanguageName(combo_language->GetSelectedEntryNumber())).Append("\n")
118 .Append(i18n->GetNthLanguageEncoding(combo_language->GetSelectedEntryNumber())).Append("\n")
119 .Append(i18n->GetNthLanguageAuthor(combo_language->GetSelectedEntryNumber())));
120
121 text_info_data->SetX(9 + text_info->GetUnscaledTextWidth());
122
123 Float scaleFactor = Surface().GetSurfaceDPI() / 96.0;
124
125 link_url->SetText(i18n->GetNthLanguageURL(combo_language->GetSelectedEntryNumber()));
126 link_url->SetURL(i18n->GetNthLanguageURL(combo_language->GetSelectedEntryNumber()));
127 link_url->SetPosition(text_info->GetPosition() + Point(text_info->GetUnscaledTextWidth(), Math::Round((2 + numAuthorLines) * (text_info->GetScaledTextHeight() - text_info_data->GetScaledTextHeight()) / scaleFactor)));
128
129 link_url->Paint(SP_PAINT);
130
131 group_info->SetHeight(text_info->GetUnscaledTextHeight() + 20);
132
133 if (i18n->GetNthLanguageID(combo_language->GetSelectedEntryNumber()) == "internal") btn_edit->Deactivate();
134 else btn_edit->Activate();
135 }
136 }
137
138 Void freac::ConfigureLanguage::EditLanguageFile()
139 {
140 BoCA::I18n *i18n = BoCA::I18n::Get();
141
142 Float scaleFactor = Application::GetScaleFactor();
143 String scaleParam = scaleFactor != 0 ? String("--scale:").Append(String::FromFloat(scaleFactor)) : String();
144
145 #if defined __WIN32__
146 ShellExecute(0, String("open"), GUI::Application::GetApplicationDirectory().Append("translator.exe"), String(scaleParam).Append(" \"").Append(GUI::Application::GetApplicationDirectory().Append("lang").Append(Directory::GetDirectoryDelimiter()).Append(i18n->GetNthLanguageID(combo_language->GetSelectedEntryNumber()))).Append("\""), String("."), SW_SHOW);
147 #else
148 String command = String("\"").Append(GUI::Application::GetApplicationDirectory()).Append(File(GUI::Application::GetApplicationDirectory().Append("smooth-translator")).Exists() ? "smooth-translator" : "translator").Append("\"");
149
150 #if defined __APPLE__
151 /* Check if macOS version is at least 10.6 (Darwin 10.0) as the open
152 * command does not support the --args flag on earlier releases of macOS.
153 */
154 Buffer<char> buffer(32);
155 FILE *pstdin = popen("sysctl kern.osrelease", "r");
156
157 fscanf(pstdin, String("%[^\n]").Append(String::FromInt(buffer.Size() - 1)), (char *) buffer);
158
159 pclose(pstdin);
160
161 String osrelease = (char *) buffer;
162
163 if (!osrelease.StartsWith("kern.osrelease: ") || osrelease.Tail(osrelease.Length() - 16).ToInt() >= 10) command = String("open \"").Append(GUI::Application::GetApplicationDirectory()).Append("translator.app\" --args");
164 else command = String("\"").Append(GUI::Application::GetApplicationDirectory()).Append("translator.app/Contents/MacOS/translator\"");
165 #endif
166
167 const char *cmd = command.Append(" ").Append(scaleParam).Append(" \"").Append(String(Config::Get()->resourcesPath).Append("lang").Append(Directory::GetDirectoryDelimiter()).Append(i18n->GetNthLanguageID(combo_language->GetSelectedEntryNumber()))).Append("\"");
168
169 if (!fork()) { execl("/bin/sh", "sh", "-c", cmd, NULL); exit(0); }
170 #endif
171 }
172
173 Int freac::ConfigureLanguage::SaveSettings()
174 {
175 BoCA::Config *config = BoCA::Config::Get();
176 BoCA::I18n *i18n = BoCA::I18n::Get();
177
178 config->SetStringValue(Config::CategorySettingsID, Config::SettingsLanguageID, i18n->GetNthLanguageID(combo_language->GetSelectedEntryNumber()));
179
180 return Success();
181 }