Debian Codegraph @

components/verifier/accuraterip/config.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 "config.h"
14 #include "database.h"
15 #include "drive.h"
16 #include "driveoffsets.h"
17
18 const String BoCA::ConfigureAccurateRip::ConfigID = "AccurateRip";
19 const String BoCA::ConfigureAccurateRip::ConfigCategoryRipperID = "Ripper";
20
21 const String BoCA::ConfigureAccurateRip::ConfigEnabledID = "Enabled";
22 const Bool BoCA::ConfigureAccurateRip::ConfigEnabledDefault = True;
23
24 const String BoCA::ConfigureAccurateRip::ConfigPathID = "Path";
25 const String BoCA::ConfigureAccurateRip::ConfigPathDefault = "http://accuraterip.com/accuraterip/";
26
27 const String BoCA::ConfigureAccurateRip::ConfigEnableCacheID = "EnableCache";
28 const Bool BoCA::ConfigureAccurateRip::ConfigEnableCacheDefault = True;
29
30 const String BoCA::ConfigureAccurateRip::ConfigExpireCacheID = "ExpireCache";
31 const Int BoCA::ConfigureAccurateRip::ConfigExpireCacheDefault = 30;
32
33 const String BoCA::ConfigureAccurateRip::ConfigNotifyMissingEntryID = "NotifyMissingEntry";
34 const Bool BoCA::ConfigureAccurateRip::ConfigNotifyMissingEntryDefault = False;
35
36 const String BoCA::ConfigureAccurateRip::ConfigNotifySuccessID = "NotifySuccess";
37 const Bool BoCA::ConfigureAccurateRip::ConfigNotifySuccessDefault = False;
38
39 const String BoCA::ConfigureAccurateRip::ResourcesFolder = "freac.verifier.accuraterip";
40
41 BoCA::ConfigureAccurateRip::ConfigureAccurateRip()
42 {
43 const Config *config = Config::Get();
44 I18n *i18n = I18n::Get();
45
46 i18n->SetContext("Verifiers::AccurateRip");
47
48 /* Get configuration.
49 */
50 enableAccurateRip = config->GetIntValue(ConfigID, ConfigEnabledID, ConfigEnabledDefault);
51
52 enableCache = config->GetIntValue(ConfigID, ConfigEnableCacheID, ConfigEnableCacheDefault);
53 expireCache = CacheDaysToIndex(config->GetIntValue(ConfigID, ConfigExpireCacheID, ConfigExpireCacheDefault));
54
55 notifyMissing = config->GetIntValue(ConfigID, ConfigNotifyMissingEntryID, ConfigNotifyMissingEntryDefault);
56 notifySuccess = config->GetIntValue(ConfigID, ConfigNotifySuccessID, ConfigNotifySuccessDefault);
57
58 /* General settings group.
59 */
60 group_settings = new GroupBox(i18n->TranslateString("General settings"), Point(7, 11), Size(450, 67));
61
62 check_enabled = new CheckBox(i18n->TranslateString("Enable AccurateRip"), Point(10, 13), Size(200, 0), &enableAccurateRip);
63 check_enabled->onAction.Connect(&ConfigureAccurateRip::ToggleEnabled, this);
64
65 text_path = new Text(i18n->AddColon(i18n->TranslateString("Database URL")), Point(27, 41));
66 edit_path = new EditBox(config->GetStringValue(ConfigID, ConfigPathID, ConfigPathDefault), Point(text_path->GetUnscaledTextWidth() + 34, 38), Size(100, 0));
67
68 list_path = new List();
69 list_path->AddEntry(ConfigPathDefault);
70
71 edit_path->SetDropDownList(list_path);
72
73 group_settings->Add(check_enabled);
74
75 group_settings->Add(text_path);
76 group_settings->Add(edit_path);
77
78 /* Drives group.
79 */
80 group_drives = new GroupBox(i18n->TranslateString("Drives"), Point(7, 90), Size(450, 113));
81
82 text_drive = new Text(i18n->AddColon(i18n->TranslateString("Drive")), Point(9, 13));
83 combo_drive = new ComboBox(Point(text_drive->GetUnscaledTextWidth() + 16, 10), Size(100, 0));
84
85 UpdateDriveList();
86
87 combo_drive->SelectNthEntry(config->GetIntValue(ConfigCategoryRipperID, "ActiveDrive", 0));
88 combo_drive->onSelectEntry.Connect(&ConfigureAccurateRip::SelectDrive, this);
89
90 text_status = new Text(i18n->AddColon(i18n->TranslateString("Offset configuration")), Point(27, 36));
91 text_drivedb = new Text(i18n->AddColon(i18n->TranslateString("Offset in drive database")), Point(27, 54));
92 text_detected = new Text(i18n->AddColon(i18n->TranslateString("Detected offset")), Point(27, 72));
93 text_configured = new Text(i18n->AddColon(i18n->TranslateString("Configured offset")), Point(27, 90));
94
95 Int maxTextSize = Math::Max(Math::Max(text_status->GetUnscaledTextWidth(), text_drivedb->GetUnscaledTextWidth()),
96 Math::Max(text_detected->GetUnscaledTextWidth(), text_configured->GetUnscaledTextWidth()));
97
98 text_status_value = new Text(NIL, Point(maxTextSize + 33, 36));
99 text_drivedb_offset = new Text(NIL, Point(maxTextSize + 33, 54));
100 text_detected_offset = new Text(NIL, Point(maxTextSize + 33, 72));
101 text_configured_offset = new Text(NIL, Point(maxTextSize + 33, 90));
102
103 button_configure = new Button(i18n->TranslateString("Detect offset now"), Point(88, 68), Size(0, 0));
104 button_configure->SetOrientation(OR_UPPERRIGHT);
105 button_configure->SetWidth(Math::Max(80, button_configure->GetUnscaledTextWidth() + 16));
106 button_configure->SetX(button_configure->GetWidth() + 10);
107 button_configure->onAction.Connect(&ConfigureAccurateRip::DetectOffset, this);
108
109 group_drives->Add(text_drive);
110 group_drives->Add(combo_drive);
111 group_drives->Add(text_status);
112 group_drives->Add(text_status_value);
113 group_drives->Add(text_drivedb);
114 group_drives->Add(text_drivedb_offset);
115 group_drives->Add(text_detected);
116 group_drives->Add(text_detected_offset);
117 group_drives->Add(text_configured);
118 group_drives->Add(text_configured_offset);
119 group_drives->Add(button_configure);
120
121 /* Cache settings group.
122 */
123 group_cache = new GroupBox(i18n->TranslateString("Cache"), Point(7, 215), Size(450, 65));
124
125 check_cache = new CheckBox(i18n->TranslateString("Enable cache"), Point(10, 13), Size(200, 0), &enableCache);
126 check_cache->onAction.Connect(&ConfigureAccurateRip::ToggleCache, this);
127
128 text_expire = new Text(i18n->AddColon(i18n->TranslateString("Expires after")), Point(27, 40));
129
130 slider_expire = new Slider(Point(text_expire->GetUnscaledTextWidth() + 34, 38), Size(200, 0), OR_HORZ, &expireCache, 0, 7);
131 slider_expire->onValueChange.Connect(&ConfigureAccurateRip::SetExpireTime, this);
132
133 text_expire_value = new Text(NIL, Point(0, 40));
134
135 group_cache->Add(check_cache);
136
137 group_cache->Add(text_expire);
138 group_cache->Add(slider_expire);
139 group_cache->Add(text_expire_value);
140
141 /* Options settings group.
142 */
143 group_options = new GroupBox(i18n->TranslateString("Options"), Point(7, 292), Size(450, 65));
144
145 check_notify_missing = new CheckBox(i18n->TranslateString("Notify when a disc is not found in the database"), Point(10, 13), Size(430, 0), &notifyMissing);
146 check_notify_success = new CheckBox(i18n->TranslateString("Notify when all tracks have been successfully verified"), Point(10, 38), Size(430, 0), &notifySuccess);
147
148 group_options->Add(check_notify_missing);
149 group_options->Add(check_notify_success);
150
151 Add(group_settings);
152 Add(group_drives);
153 Add(group_cache);
154 Add(group_options);
155
156 ToggleEnabled();
157 SetExpireTime();
158 SelectDrive();
159
160 /* Adjust element widths.
161 */
162 Font font = text_status_value->GetFont();
163
164 maxTextSize = Math::Max(Math::Max(font.GetUnscaledTextSizeX(i18n->TranslateString("not configured")), font.GetUnscaledTextSizeX(i18n->TranslateString("automatically configured"))),
165 Math::Max(font.GetUnscaledTextSizeX(i18n->TranslateString("configured by offset detection")), font.GetUnscaledTextSizeX(i18n->TranslateString("manually configured"))));
166
167 Int line1Size = maxTextSize + 10;
168 Int line3Size = font.GetUnscaledTextSizeX(i18n->TranslateString("not detected")) + button_configure->GetWidth() + 18;
169
170 group_settings->SetWidth(Math::Max(450, text_status_value->GetX() + Math::Max(line1Size, line3Size)));
171 group_drives->SetWidth(group_settings->GetWidth());
172 group_cache->SetWidth(group_settings->GetWidth());
173 group_options->SetWidth(group_settings->GetWidth());
174
175 edit_path->SetWidth(group_settings->GetWidth() - text_path->GetUnscaledTextWidth() - 44);
176 combo_drive->SetWidth(group_drives->GetWidth() - text_drive->GetUnscaledTextWidth() - 26);
177
178 maxTextSize = Math::Max(Math::Max(font.GetUnscaledTextSizeX(i18n->TranslateString("%1 day(s)").Replace("%1", "0")), font.GetUnscaledTextSizeX(i18n->TranslateString("%1 week(s)").Replace("%1", "0"))),
179 Math::Max(Math::Max(font.GetUnscaledTextSizeX(i18n->TranslateString("%1 month(s)").Replace("%1", "0")), font.GetUnscaledTextSizeX(i18n->TranslateString("%1 year(s)").Replace("%1", "0"))),
180 font.GetUnscaledTextSizeX(i18n->TranslateString("never"))));
181
182 text_expire_value->SetX(group_cache->GetWidth() - maxTextSize - 10);
183 slider_expire->SetWidth(group_cache->GetWidth() - maxTextSize - slider_expire->GetX() - 17);
184
185 maxTextSize = Math::Max(check_enabled->GetUnscaledTextWidth(), check_cache->GetUnscaledTextWidth());
186
187 check_enabled->SetWidth(maxTextSize + 20);
188 check_cache->SetWidth(maxTextSize + 20);
189
190 check_notify_missing->SetWidth(group_options->GetWidth() - 20);
191 check_notify_success->SetWidth(check_notify_missing->GetWidth());
192
193 SetSize(Size(group_cache->GetWidth() + 14, 364));
194 }
195
196 BoCA::ConfigureAccurateRip::~ConfigureAccurateRip()
197 {
198 DeleteObject(group_settings);
199 DeleteObject(check_enabled);
200
201 DeleteObject(edit_path);
202 DeleteObject(text_path);
203 DeleteObject(list_path);
204
205 DeleteObject(group_drives);
206 DeleteObject(text_drive);
207 DeleteObject(combo_drive);
208 DeleteObject(text_status);
209 DeleteObject(text_status_value);
210 DeleteObject(text_drivedb);
211 DeleteObject(text_drivedb_offset);
212 DeleteObject(text_detected);
213 DeleteObject(text_detected_offset);
214 DeleteObject(text_configured);
215 DeleteObject(text_configured_offset);
216 DeleteObject(button_configure);
217
218 DeleteObject(group_cache);
219 DeleteObject(check_cache);
220 DeleteObject(text_expire);
221 DeleteObject(slider_expire);
222 DeleteObject(text_expire_value);
223
224 DeleteObject(group_options);
225 DeleteObject(check_notify_missing);
226 DeleteObject(check_notify_success);
227 }
228
229 Int BoCA::ConfigureAccurateRip::CacheDaysToIndex(Int days)
230 {
231 if (days == 0) return 7;
232 else if (days < 4) return 0;
233 else if (days < 11) return 1;
234 else if (days < 22) return 2;
235 else if (days < 60) return 3;
236 else if (days < 135) return 4;
237 else if (days < 270) return 5;
238 else return 6;
239 }
240
241 Int BoCA::ConfigureAccurateRip::CacheIndexToDays(Int index)
242 {
243 if (index == 0) return 1;
244 else if (index == 1) return 7;
245 else if (index == 2) return 14;
246 else if (index == 3) return 30;
247 else if (index == 4) return 90;
248 else if (index == 5) return 180;
249 else if (index == 6) return 365;
250 else return 0;
251 }
252
253 Void BoCA::ConfigureAccurateRip::ToggleEnabled()
254 {
255 if (enableAccurateRip)
256 {
257 text_path->Activate();
258 edit_path->Activate();
259
260 group_drives->Activate();
261 group_cache->Activate();
262 }
263 else
264 {
265 text_path->Deactivate();
266 edit_path->Deactivate();
267
268 group_drives->Deactivate();
269 group_cache->Deactivate();
270 }
271 }
272
273 Void BoCA::ConfigureAccurateRip::ToggleCache()
274 {
275 if (enableCache)
276 {
277 text_expire->Activate();
278 slider_expire->Activate();
279 text_expire_value->Activate();
280 }
281 else
282 {
283 text_expire->Deactivate();
284 slider_expire->Deactivate();
285 text_expire_value->Deactivate();
286 }
287 }
288
289 Void BoCA::ConfigureAccurateRip::SetExpireTime()
290 {
291 I18n *i18n = I18n::Get();
292
293 i18n->SetContext("Verifiers::AccurateRip");
294
295 switch (expireCache)
296 {
297 case 0: text_expire_value->SetText(i18n->TranslateString("%1 day(s)").Replace("%1", "1")); break;
298 case 1: text_expire_value->SetText(i18n->TranslateString("%1 week(s)").Replace("%1", "1")); break;
299 case 2: text_expire_value->SetText(i18n->TranslateString("%1 week(s)").Replace("%1", "2")); break;
300 case 3: text_expire_value->SetText(i18n->TranslateString("%1 month(s)").Replace("%1", "1")); break;
301 case 4: text_expire_value->SetText(i18n->TranslateString("%1 month(s)").Replace("%1", "3")); break;
302 case 5: text_expire_value->SetText(i18n->TranslateString("%1 month(s)").Replace("%1", "6")); break;
303 case 6: text_expire_value->SetText(i18n->TranslateString("%1 year(s)").Replace("%1", "1")); break;
304 case 7: text_expire_value->SetText(i18n->TranslateString("never")); break;
305 }
306 }
307
308 Void BoCA::ConfigureAccurateRip::UpdateDriveList()
309 {
310 AS::Registry &boca = AS::Registry::Get();
311 AS::DeviceInfoComponent *info = boca.CreateDeviceInfoComponent();
312
313 if (info != NIL)
314 {
315 if (info->GetNumberOfDevices() == 0) group_drives->Deactivate();
316 else group_drives->Activate();
317
318 String selectedPath = selectedDrivePath;
319
320 combo_drive->RemoveAllEntries();
321
322 for (Int i = 0; i < info->GetNumberOfDevices(); i++)
323 {
324 const Device &device = info->GetNthDeviceInfo(i);
325 Bool selected = (device.path == selectedPath);
326
327 combo_drive->AddEntry(device.GetName());
328
329 if (selected) combo_drive->SelectNthEntry(i);
330 }
331
332 boca.DeleteComponent(info);
333 }
334 }
335
336 Void BoCA::ConfigureAccurateRip::SelectDrive()
337 {
338 AS::Registry &boca = AS::Registry::Get();
339 AS::DeviceInfoComponent *info = boca.CreateDeviceInfoComponent();
340
341 if (info == NIL || info->GetNumberOfDevices() == 0)
342 {
343 if (info != NIL) boca.DeleteComponent(info);
344
345 group_drives->Deactivate();
346
347 return;
348 }
349
350 Config *config = Config::Get();
351
352 /* Find configuration level.
353 */
354 Int driveNumber = combo_drive->GetSelectedEntryNumber();
355 Drive drive(config, driveNumber);
356 ConfigurationLevel configurationLevel = drive.GetConfigurationLevel();
357
358 if (configurationLevel == ConfigurationLevelUnconfigured) configurationLevel = drive.ConfigureOffset(True, False);
359
360 /* Assign configuration level status.
361 */
362 I18n *i18n = I18n::Get();
363
364 i18n->SetContext("Verifiers::AccurateRip");
365
366 switch (configurationLevel)
367 {
368 case ConfigurationLevelUnconfigured: text_status_value->SetText(i18n->TranslateString("not configured")); break;
369 case ConfigurationLevelAutomatic: text_status_value->SetText(i18n->TranslateString("automatically configured")); break;
370 case ConfigurationLevelVerified: text_status_value->SetText(i18n->TranslateString("configured by offset detection")); break;
371 case ConfigurationLevelManual:
372 case ConfigurationLevelManualOverride: text_status_value->SetText(i18n->TranslateString("manually configured")); break;
373 }
374
375 /* Find offset in database.
376 */
377 DriveOffsets *offsets = DriveOffsets::Get();
378 const Device &device = info->GetNthDeviceInfo(driveNumber);
379 Int drivedbOffset = offsets->FindDriveOffset(device).offset;
380
381 if (drivedbOffset == DriveOffset::Invalid) text_drivedb_offset->SetText(i18n->TranslateString("not present"));
382 else text_drivedb_offset->SetText(String::FromInt(drivedbOffset));
383
384 /* Find detected offset.
385 */
386 Int detectedOffset = drive.GetDetectedOffset();
387
388 if (detectedOffset == DriveOffset::Invalid) text_detected_offset->SetText(i18n->TranslateString("not detected"));
389 else text_detected_offset->SetText(String::FromInt(detectedOffset));
390
391 /* Find configured offset.
392 */
393 String driveID = device.GetID();
394
395 Bool useOffset = config->GetIntValue(ConfigCategoryRipperID, String("UseOffsetDrive-").Append(driveID), False);
396 Int offset = config->GetIntValue(ConfigCategoryRipperID, String("ReadOffsetDrive-").Append(driveID), 0);
397
398 if (useOffset) text_configured_offset->SetText(String::FromInt(offset));
399 else text_configured_offset->SetText(i18n->TranslateString("disabled"));
400
401 /* Save device path.
402 */
403 selectedDrivePath = device.path;
404
405 boca.DeleteComponent(info);
406 }
407
408 Void BoCA::ConfigureAccurateRip::DetectOffset()
409 {
410 Config *config = Config::Get();
411
412 Drive drive(config, combo_drive->GetSelectedEntryNumber());
413
414 drive.ConfigureOffset();
415
416 /* Update offset configuration.
417 */
418 UpdateDriveList();
419 }
420
421 Int BoCA::ConfigureAccurateRip::SaveSettings()
422 {
423 Config *config = Config::Get();
424
425 config->SetIntValue(ConfigID, ConfigEnabledID, enableAccurateRip);
426
427 String path = edit_path->GetText();
428
429 if (!path.EndsWith("/")) path.Append("/");
430 if (path.StartsWith("https://")) path.Replace("https://", "http://");
431
432 if (config->GetStringValue(ConfigID, ConfigPathID, ConfigPathDefault) != path) Database::InitializeSessionCache();
433
434 config->SetStringValue(ConfigID, ConfigPathID, path);
435
436 config->SetIntValue(ConfigID, ConfigEnableCacheID, enableCache);
437 config->SetIntValue(ConfigID, ConfigExpireCacheID, CacheIndexToDays(expireCache));
438
439 config->SetIntValue(ConfigID, ConfigNotifyMissingEntryID, notifyMissing);
440 config->SetIntValue(ConfigID, ConfigNotifySuccessID, notifySuccess);
441
442 return Success();
443 }