| 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 <smooth.h> |
| 14 | |
| 15 | #include "notifier.h" |
| 16 | #include "config.h" |
| 17 | |
| 18 | using namespace smooth::GUI::Dialogs; |
| 19 | |
| 20 | const String &BoCA::Notifier::GetComponentSpecs() |
| 21 | { |
| 22 | I18n *i18n = I18n::Get(); |
| 23 | |
| 24 | i18n->SetContext("Components::Extensions"); |
| 25 | |
| 26 | static String componentSpecs = String(" \ |
| 27 | \ |
| 28 | <?xml version=\"1.0\" encoding=\"UTF-8\"?> \ |
| 29 | <component> \ |
| 30 | <name>").Append(i18n->TranslateString("Notifications")).Append("</name> \ |
| 31 | <version>1.0</version> \ |
| 32 | <id>notifier-ext</id> \ |
| 33 | <type>extension</type> \ |
| 34 | </component> \ |
| 35 | \ |
| 36 | "); |
| 37 | |
| 38 | return componentSpecs; |
| 39 | } |
| 40 | |
| 41 | BoCA::Notifier::Notifier() |
| 42 | { |
| 43 | configLayer = NIL; |
| 44 | |
| 45 | /* Connect slots. |
| 46 | */ |
| 47 | Engine *engine = Engine::Get(); |
| 48 | |
| 49 | engine->onStartConversion.Connect(&Notifier::OnStartConversion, this); |
| 50 | engine->onFinishConversion.Connect(&Notifier::OnFinishConversion, this); |
| 51 | engine->onCancelConversion.Connect(&Notifier::OnCancelConversion, this); |
| 52 | } |
| 53 | |
| 54 | BoCA::Notifier::~Notifier() |
| 55 | { |
| 56 | if (configLayer != NIL) Object::DeleteObject(configLayer); |
| 57 | |
| 58 | /* Disconnect slots. |
| 59 | */ |
| 60 | Engine *engine = Engine::Get(); |
| 61 | |
| 62 | engine->onStartConversion.Disconnect(&Notifier::OnStartConversion, this); |
| 63 | engine->onFinishConversion.Disconnect(&Notifier::OnFinishConversion, this); |
| 64 | engine->onCancelConversion.Disconnect(&Notifier::OnCancelConversion, this); |
| 65 | } |
| 66 | |
| 67 | Void BoCA::Notifier::OnStartConversion(Int id, const Array<Track> &tracks) |
| 68 | { |
| 69 | /* Add conversion start time to list. |
| 70 | */ |
| 71 | conversions.Add(S::System::System::Clock(), id); |
| 72 | } |
| 73 | |
| 74 | Void BoCA::Notifier::OnFinishConversion(Int id) |
| 75 | { |
| 76 | /* Get configuration. |
| 77 | */ |
| 78 | Config *config = Config::Get(); |
| 79 | |
| 80 | Bool enableNotifications = config->GetIntValue(ConfigureNotifier::ConfigID, "EnableNotifications", False); |
| 81 | |
| 82 | Bool playSound = config->GetIntValue(ConfigureNotifier::ConfigID, "PlaySound", True); |
| 83 | Bool displayMessage = config->GetIntValue(ConfigureNotifier::ConfigID, "DisplayMessage", False); |
| 84 | Bool minimumDuration = config->GetIntValue(ConfigureNotifier::ConfigID, "MinimumDuration", False); |
| 85 | |
| 86 | String soundfile = config->GetStringValue(ConfigureNotifier::ConfigID, "SoundFile", String("<sounds>/finished.flac").Replace("/", Directory::GetDirectoryDelimiter())); |
| 87 | String message = config->GetStringValue(ConfigureNotifier::ConfigID, "Message", ConfigureNotifier::DefaultMessage); |
| 88 | |
| 89 | Int seconds = config->GetIntValue(ConfigureNotifier::ConfigID, "Seconds", 30); |
| 90 | |
| 91 | /* Check conversion duration. |
| 92 | */ |
| 93 | UnsignedInt64 duration = S::System::System::Clock() - conversions.Get(id); |
| 94 | |
| 95 | conversions.Remove(id); |
| 96 | |
| 97 | if (!enableNotifications || (minimumDuration && duration / 1000.0 < seconds)) return; |
| 98 | |
| 99 | /* Only notify for the last conversion. |
| 100 | */ |
| 101 | if (conversions.Length() > 0) return; |
| 102 | |
| 103 | /* Play sound if requested. |
| 104 | */ |
| 105 | if (playSound) |
| 106 | { |
| 107 | Directory dir(Utilities::GetBoCADirectory().Append(ConfigureNotifier::ResourcesFolder).Append("/sounds")); |
| 108 | |
| 109 | if (!dir.Exists()) dir = Utilities::GetBoCADirectory().Append("../freac/").Append(ConfigureNotifier::ResourcesFolder).Append("/sounds"); |
| 110 | |
| 111 | player.Play(String(soundfile).Replace("<sounds>", dir)); |
| 112 | } |
| 113 | |
| 114 | /* DisplayMessage if requested. |
| 115 | */ |
| 116 | if (displayMessage) |
| 117 | { |
| 118 | I18n *i18n = I18n::Get(); |
| 119 | |
| 120 | i18n->SetContext("Extensions::Notifier"); |
| 121 | |
| 122 | QuickMessage(i18n->TranslateString(message), i18n->TranslateString("Info"), Message::Buttons::Ok, Message::Icon::Information); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | Void BoCA::Notifier::OnCancelConversion(Int id) |
| 127 | { |
| 128 | /* Remove conversion from list. |
| 129 | */ |
| 130 | conversions.Remove(id); |
| 131 | } |
| 132 | |
| 133 | ConfigLayer *BoCA::Notifier::GetConfigurationLayer() |
| 134 | { |
| 135 | if (configLayer == NIL) configLayer = new ConfigureNotifier(); |
| 136 | |
| 137 | return configLayer; |
| 138 | } |