Debian Codegraph @

components/extension/notifier/config.cpp

raw symbols SCIP
1 /* fre:ac - free audio converter
2 * Copyright (C) 2001-2020 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 "config.h"
14
15 using namespace BoCA::AS;
16
17 using namespace smooth::IO;
18 using namespace smooth::GUI::Dialogs;
19
20 const String BoCA::ConfigureNotifier::ConfigID = "Notifier";
21
22 const String BoCA::ConfigureNotifier::ResourcesFolder = "freac.extension.notifier";
23 const String BoCA::ConfigureNotifier::DefaultMessage = "Conversion process finished!";
24
25 BoCA::ConfigureNotifier::ConfigureNotifier()
26 {
27 /* Get configuration.
28 */
29 const Config *config = Config::Get();
30
31 enableNotifications = config->GetIntValue(ConfigID, "EnableNotifications", False);
32
33 playSound = config->GetIntValue(ConfigID, "PlaySound", True);
34 displayMessage = config->GetIntValue(ConfigID, "DisplayMessage", False);
35 minimumDuration = config->GetIntValue(ConfigID, "MinimumDuration", False);
36
37 String soundfile = config->GetStringValue(ConfigID, "SoundFile", String("<sounds>/finished.flac").Replace("/", Directory::GetDirectoryDelimiter()));
38 String message = config->GetStringValue(ConfigID, "Message", DefaultMessage);
39
40 Int seconds = config->GetIntValue(ConfigID, "Seconds", 30);
41
42 /* Create UI elements.
43 */
44 I18n *i18n = I18n::Get();
45
46 i18n->SetContext("Extensions::Notifier::Configuration");
47
48 group_notify = new GroupBox(i18n->TranslateString("Notifications"), Point(7, 11), Size(450, 141));
49
50 check_notify = new CheckBox(i18n->TranslateString("Notify on finished conversion"), Point(10, 13), Size(group_notify->GetWidth() - 20, 0), &enableNotifications);
51 check_notify->onAction.Connect(&ConfigureNotifier::OnToggleNotifications, this);
52
53 /* Play a sound.
54 */
55 check_sound = new CheckBox(i18n->AddColon(i18n->TranslateString("Play sound")), Point(27, 40), Size(100, 0), &playSound);
56 check_sound->onAction.Connect(&ConfigureNotifier::OnToggleNotifications, this);
57
58 check_message = new CheckBox(i18n->AddColon(i18n->TranslateString("Display message")), Point(27, 86), Size(100, 0), &displayMessage);
59 check_message->onAction.Connect(&ConfigureNotifier::OnToggleNotifications, this);
60
61 Int maxTextSize = Math::Max(check_sound->GetUnscaledTextWidth(), check_message->GetUnscaledTextWidth());
62
63 check_sound->SetWidth(maxTextSize + 21);
64 check_message->SetWidth(maxTextSize + 21);
65
66 list_soundfile = new List();
67 list_soundfile->AddEntry(String("<sounds>/finished.flac").Replace("/", Directory::GetDirectoryDelimiter()));
68
69 edit_soundfile = new EditBox(soundfile, Point(check_sound->GetWidth() + 35, 39), Size(group_notify->GetWidth() - check_sound->GetWidth() - 133, 0));
70 edit_soundfile->SetDropDownList(list_soundfile);
71
72 btn_browse = new Button(i18n->TranslateString("Select"), Point(90, 38), Size());
73 btn_browse->SetOrientation(OR_UPPERRIGHT);
74 btn_browse->onAction.Connect(&ConfigureNotifier::OnBrowse, this);
75
76 Directory dir(Utilities::GetBoCADirectory().Append(ResourcesFolder));
77
78 if (!dir.Exists()) dir = Utilities::GetBoCADirectory().Append("../freac/").Append(ResourcesFolder);
79
80 btn_preview = new Button(ImageLoader::Load(File(String(dir).Append("/icons/play.png"))), Point(edit_soundfile->GetX() + edit_soundfile->GetWidth() - 43 + (i18n->IsActiveLanguageRightToLeft() ? 22 : 0), 59), Size(25, 25));
81 btn_preview->onAction.Connect(&ConfigureNotifier::OnPreview, this);
82 btn_preview->SetFlags(BF_NOFRAME);
83
84 btn_stop = new Button(ImageLoader::Load(File(String(dir).Append("/icons/stop.png"))), Point(edit_soundfile->GetX() + edit_soundfile->GetWidth() - 21 - (i18n->IsActiveLanguageRightToLeft() ? 22 : 0), 59), Size(25, 25));
85 btn_stop->onAction.Connect(&ConfigureNotifier::OnStop, this);
86 btn_stop->SetFlags(BF_NOFRAME);
87
88 Registry &boca = Registry::Get();
89
90 if (boca.GetNumberOfComponentsOfType(COMPONENT_TYPE_OUTPUT) == 0)
91 {
92 check_sound->Deactivate();
93 check_message->Deactivate();
94 list_soundfile->Deactivate();
95 edit_soundfile->Deactivate();
96 btn_browse->Deactivate();
97 btn_preview->Deactivate();
98 btn_stop->Deactivate();
99 }
100
101 /* Display a message.
102 */
103 edit_message = new EditBox(i18n->TranslateString(message), Point(check_message->GetWidth() + 35, 85), Size(group_notify->GetWidth() - check_message->GetWidth() - 45, 0));
104
105 /* Long running conversions only.
106 */
107 Int textWidth = Math::Max(Font().GetUnscaledTextSizeX(i18n->TranslateString("second(s)")), Font().GetUnscaledTextSizeX(i18n->TranslateString("minute(s)")));
108
109 combo_units = new ComboBox(Point(textWidth + 35, 112), Size(textWidth + 25, 0));
110 combo_units->SetOrientation(OR_UPPERRIGHT);
111 combo_units->AddEntry(i18n->TranslateString("second(s)"));
112 combo_units->AddEntry(i18n->TranslateString("minute(s)"));
113
114 if (seconds % 60 != 0) combo_units->SelectNthEntry(0);
115 else combo_units->SelectNthEntry(1);
116
117 list_amount = new List();
118 list_amount->AddEntry("1");
119 list_amount->AddEntry("2");
120 list_amount->AddEntry("3");
121 list_amount->AddEntry("4");
122 list_amount->AddEntry("5");
123 list_amount->AddEntry("10");
124 list_amount->AddEntry("15");
125 list_amount->AddEntry("30");
126 list_amount->AddEntry("45");
127 list_amount->AddEntry("60");
128
129 Int numberWidth = Font().GetUnscaledTextSizeX("00");
130
131 edit_amount = new EditBox(String::FromInt(seconds / (combo_units->GetSelectedEntryNumber() == 0 ? 1 : 60)), Point(combo_units->GetX() + numberWidth + 33, 112), Size(numberWidth + 25, 0));
132 edit_amount->SetOrientation(OR_UPPERRIGHT);
133 edit_amount->SetFlags(EDB_NUMERIC);
134 edit_amount->SetDropDownList(list_amount);
135
136 check_time = new CheckBox(i18n->AddColon(i18n->TranslateString("Only for conversions running longer than")), Point(27, 113), Size(group_notify->GetWidth() - edit_amount->GetX() - 35, 0), &minimumDuration);
137 check_time->onAction.Connect(&ConfigureNotifier::OnToggleNotifications, this);
138
139 /* Add elements.
140 */
141 group_notify->Add(check_notify);
142
143 group_notify->Add(check_sound);
144 group_notify->Add(edit_soundfile);
145
146 group_notify->Add(btn_browse);
147
148 group_notify->Add(btn_preview);
149 group_notify->Add(btn_stop);
150
151 group_notify->Add(check_message);
152 group_notify->Add(edit_message);
153
154 group_notify->Add(check_time);
155 group_notify->Add(edit_amount);
156 group_notify->Add(combo_units);
157
158 Add(group_notify);
159
160 OnToggleNotifications();
161
162 SetSize(group_notify->GetSize() + Size(14, 16));
163 }
164
165 BoCA::ConfigureNotifier::~ConfigureNotifier()
166 {
167 DeleteObject(group_notify);
168 DeleteObject(check_notify);
169
170 DeleteObject(check_sound);
171 DeleteObject(edit_soundfile);
172 DeleteObject(list_soundfile);
173
174 DeleteObject(btn_browse);
175
176 DeleteObject(btn_preview);
177 DeleteObject(btn_stop);
178
179 DeleteObject(check_message);
180 DeleteObject(edit_message);
181
182 DeleteObject(check_time);
183 DeleteObject(edit_amount);
184 DeleteObject(list_amount);
185 DeleteObject(combo_units);
186
187 }
188
189 Void BoCA::ConfigureNotifier::OnToggleNotifications()
190 {
191 if (enableNotifications)
192 {
193 Registry &boca = Registry::Get();
194
195 if (boca.GetNumberOfComponentsOfType(COMPONENT_TYPE_OUTPUT) > 0) check_sound->Activate();
196
197 check_message->Activate();
198 check_time->Activate();
199
200 /* Play a sound.
201 */
202 if (playSound && check_sound->IsActive())
203 {
204 edit_soundfile->Activate();
205
206 btn_browse->Activate();
207
208 btn_preview->Activate();
209 btn_stop->Activate();
210 }
211 else
212 {
213 edit_soundfile->Deactivate();
214
215 btn_browse->Deactivate();
216
217 btn_preview->Deactivate();
218 btn_stop->Deactivate();
219 }
220
221 /* Display a message.
222 */
223 if (displayMessage)
224 {
225 edit_message->Activate();
226 }
227 else
228 {
229 edit_message->Deactivate();
230 }
231
232 /* Long running conversions only.
233 */
234 if (minimumDuration)
235 {
236 edit_amount->Activate();
237 combo_units->Activate();
238 }
239 else
240 {
241 edit_amount->Deactivate();
242 combo_units->Deactivate();
243 }
244 }
245 else
246 {
247 check_sound->Deactivate();
248 edit_soundfile->Deactivate();
249
250 btn_browse->Deactivate();
251
252 btn_preview->Deactivate();
253 btn_stop->Deactivate();
254
255 check_message->Deactivate();
256 edit_message->Deactivate();
257
258 check_time->Deactivate();
259 edit_amount->Deactivate();
260 combo_units->Deactivate();
261 }
262 }
263
264 Void BoCA::ConfigureNotifier::OnBrowse()
265 {
266 /* Create dialog.
267 */
268 FileSelection dialog;
269 Directory dir(Utilities::GetBoCADirectory().Append(ResourcesFolder).Append("/sounds"));
270
271 if (!dir.Exists()) dir = Utilities::GetBoCADirectory().Append("../freac/").Append(ResourcesFolder).Append("/sounds");
272
273 dialog.SetParentWindow(container->GetContainerWindow());
274 dialog.SetInitialPath(File(edit_soundfile->GetText().Replace("<sounds>", dir)).GetFilePath());
275
276 /* Get available file types.
277 */
278 Array<String> types;
279 Array<String> extensions;
280
281 Registry &boca = Registry::Get();
282
283 for (Int i = 0; i < boca.GetNumberOfComponents(); i++)
284 {
285 if (boca.GetComponentType(i) != BoCA::COMPONENT_TYPE_DECODER) continue;
286
287 const Array<FileFormat *> &formats = boca.GetComponentFormats(i);
288
289 foreach (FileFormat *format, formats)
290 {
291 const Array<String> &formatExtensions = format->GetExtensions();
292 String extension;
293
294 foreach (const String &formatExtension, formatExtensions)
295 {
296 extension.Append("*.").Append(formatExtension);
297
298 if (foreachindex < formatExtensions.Length() - 1) extension.Append("; ");
299 }
300
301 types.Add(format->GetName().Append(" (").Append(extension).Append(")"));
302 extensions.Add(extension);
303 }
304 }
305
306 /* Add file types to dialog.
307 */
308 String fileTypes;
309
310 foreach (const String &extension, extensions)
311 {
312 if (!fileTypes.Contains(extension)) fileTypes.Append(foreachindex > 0 ? ";" : NIL).Append(extension);
313 }
314
315 I18n *i18n = I18n::Get();
316
317 i18n->SetContext("Joblist");
318
319 dialog.AddFilter(i18n->TranslateString("Audio Files"), fileTypes);
320
321 foreach (const String &type, types) dialog.AddFilter(type, extensions.GetNth(foreachindex));
322
323 dialog.AddFilter(i18n->TranslateString("All Files"), "*.*");
324
325 /* Show dialog.
326 */
327 if (dialog.ShowDialog() == Success()) edit_soundfile->SetText(dialog.GetFileName());
328 }
329
330 Void BoCA::ConfigureNotifier::OnPreview()
331 {
332 Directory dir(Utilities::GetBoCADirectory().Append(ResourcesFolder).Append("/sounds"));
333
334 if (!dir.Exists()) dir = Utilities::GetBoCADirectory().Append("../freac/").Append(ResourcesFolder).Append("/sounds");
335
336 player.Play(edit_soundfile->GetText().Replace("<sounds>", dir));
337 }
338
339 Void BoCA::ConfigureNotifier::OnStop()
340 {
341 player.Stop();
342 }
343
344 Int BoCA::ConfigureNotifier::SaveSettings()
345 {
346 Config *config = Config::Get();
347 I18n *i18n = I18n::Get();
348
349 i18n->SetContext("Extensions::Notifier");
350
351 config->SetIntValue(ConfigID, "EnableNotifications", enableNotifications);
352
353 config->SetIntValue(ConfigID, "PlaySound", playSound);
354 config->SetIntValue(ConfigID, "DisplayMessage", displayMessage);
355 config->SetIntValue(ConfigID, "MinimumDuration", minimumDuration);
356
357 config->SetStringValue(ConfigID, "SoundFile", edit_soundfile->GetText());
358
359 if (edit_message->GetText() == i18n->TranslateString(DefaultMessage)) config->SetStringValue(ConfigID, "Message", DefaultMessage);
360 else config->SetStringValue(ConfigID, "Message", edit_message->GetText());
361
362 config->SetIntValue(ConfigID, "Seconds", edit_amount->GetText().ToInt() * (combo_units->GetSelectedEntryNumber() == 0 ? 1 : 60));
363
364 return Success();
365 }