diff options
author | charles kerr <charlesk@canonical.com> | 2015-12-29 09:56:55 -0600 |
---|---|---|
committer | charles kerr <charlesk@canonical.com> | 2015-12-29 09:56:55 -0600 |
commit | 6afbde41e01d0b01748f9623668158ccd8b51486 (patch) | |
tree | 090249b65e65bd357178233ba87af75cbf6b4ccc /src | |
parent | 051d78c18a3865536a0270bf150a538679dd45a6 (diff) | |
download | ayatana-indicator-sound-6afbde41e01d0b01748f9623668158ccd8b51486.tar.gz ayatana-indicator-sound-6afbde41e01d0b01748f9623668158ccd8b51486.tar.bz2 ayatana-indicator-sound-6afbde41e01d0b01748f9623668158ccd8b51486.zip |
use the InfoNotification class
Diffstat (limited to 'src')
-rw-r--r-- | src/CMakeLists.txt | 9 | ||||
-rw-r--r-- | src/info-notification.vala | 218 | ||||
-rw-r--r-- | src/service.vala | 223 |
3 files changed, 233 insertions, 217 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ba8f72a..92acc67 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -42,6 +42,13 @@ vala_add(indicator-sound-service notification.vala ) vala_add(indicator-sound-service + info-notification.vala + DEPENDS + notification + volume-control + options +) +vala_add(indicator-sound-service warn-notification.vala DEPENDS notification @@ -52,6 +59,8 @@ vala_add(indicator-sound-service sound-menu volume-control volume-control-pulse + notification + info-notification volume-warning options options-gsettings diff --git a/src/info-notification.vala b/src/info-notification.vala new file mode 100644 index 0000000..986c9ce --- /dev/null +++ b/src/info-notification.vala @@ -0,0 +1,218 @@ +/* + * Copyright 2015 Canonical Ltd. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 3. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * Authors: + * Charles Kerr <charles.kerr@canonical.com> + */ + +using Notify; + +public class IndicatorSound.InfoNotification: Notification +{ + protected override Notify.Notification create_notification() + { + return new Notify.Notification(_("Volume"), "", "audio-volume-muted"); + } + + public void show(VolumeControl.ActiveOutput active_output, + double volume, + bool is_high_volume) + { + if (!notify_server_supports("x-canonical-private-synchronous")) + return; + + /* Determine Label */ + string volume_label = get_notification_label (active_output); + + /* Choose an icon */ + string icon = get_volume_notification_icon (active_output, volume, is_high_volume); + + /* Reset the notification */ + var n = _notification; + n.update (_("Volume"), volume_label, icon); + n.clear_hints(); + n.set_hint ("x-canonical-non-shaped-icon", "true"); + n.set_hint ("x-canonical-private-synchronous", "true"); + n.set_hint ("x-canonical-value-bar-tint", is_high_volume ? "true" : "false"); + n.set_hint ("value", (int32)(volume * 100.0)); + show_(); + } + + private static string get_notification_label (VolumeControl.ActiveOutput active_output) + { + string volume_label = ""; + + switch (active_output) + { + case VolumeControl.ActiveOutput.SPEAKERS: + volume_label = _("Speakers"); + break; + case VolumeControl.ActiveOutput.HEADPHONES: + volume_label = _("Headphones"); + break; + case VolumeControl.ActiveOutput.BLUETOOTH_HEADPHONES: + volume_label = _("Bluetooth headphones"); + break; + case VolumeControl.ActiveOutput.BLUETOOTH_SPEAKER: + volume_label = _("Bluetooth speaker"); + break; + case VolumeControl.ActiveOutput.USB_SPEAKER: + volume_label = _("Usb speaker"); + break; + case VolumeControl.ActiveOutput.USB_HEADPHONES: + volume_label = _("Usb headphones"); + break; + case VolumeControl.ActiveOutput.HDMI_SPEAKER: + volume_label = _("HDMI speaker"); + break; + case VolumeControl.ActiveOutput.HDMI_HEADPHONES: + volume_label = _("HDMI headphones"); + break; + } + + return volume_label; + } + + private static string get_volume_notification_icon (VolumeControl.ActiveOutput active_output, + double volume, + bool is_high_volume) + { + string icon = ""; + + if (is_high_volume) { + switch (active_output) { + case VolumeControl.ActiveOutput.SPEAKERS: + icon = "audio-volume-high"; + break; + case VolumeControl.ActiveOutput.HEADPHONES: + icon = "audio-volume-high"; + break; + case VolumeControl.ActiveOutput.BLUETOOTH_HEADPHONES: + icon = "audio-volume-high"; + break; + case VolumeControl.ActiveOutput.BLUETOOTH_SPEAKER: + icon = "audio-volume-high"; + break; + case VolumeControl.ActiveOutput.USB_SPEAKER: + icon = "audio-volume-high"; + break; + case VolumeControl.ActiveOutput.USB_HEADPHONES: + icon = "audio-volume-high"; + break; + case VolumeControl.ActiveOutput.HDMI_SPEAKER: + icon = "audio-volume-high"; + break; + case VolumeControl.ActiveOutput.HDMI_HEADPHONES: + icon = "audio-volume-high"; + break; + } + } else { + icon = get_volume_icon (active_output, volume); + } + + return icon; + } + + private static string get_volume_icon (VolumeControl.ActiveOutput active_output, + double volume) + { + string icon = ""; + + switch (active_output) { + case VolumeControl.ActiveOutput.SPEAKERS: + if (volume <= 0.0) + icon = "audio-volume-muted"; + else if (volume <= 0.3) + icon = "audio-volume-low"; + else if (volume <= 0.7) + icon = "audio-volume-medium"; + else + icon = "audio-volume-high"; + break; + case VolumeControl.ActiveOutput.HEADPHONES: + if (volume <= 0.0) + icon = "audio-volume-muted"; + else if (volume <= 0.3) + icon = "audio-volume-low"; + else if (volume <= 0.7) + icon = "audio-volume-medium"; + else + icon = "audio-volume-high"; + break; + case VolumeControl.ActiveOutput.BLUETOOTH_HEADPHONES: + if (volume <= 0.0) + icon = "audio-volume-muted"; + else if (volume <= 0.3) + icon = "audio-volume-low"; + else if (volume <= 0.7) + icon = "audio-volume-medium"; + else + icon = "audio-volume-high"; + break; + case VolumeControl.ActiveOutput.BLUETOOTH_SPEAKER: + if (volume <= 0.0) + icon = "audio-volume-muted"; + else if (volume <= 0.3) + icon = "audio-volume-low"; + else if (volume <= 0.7) + icon = "audio-volume-medium"; + else + icon = "audio-volume-high"; + break; + case VolumeControl.ActiveOutput.USB_SPEAKER: + if (volume <= 0.0) + icon = "audio-volume-muted"; + else if (volume <= 0.3) + icon = "audio-volume-low"; + else if (volume <= 0.7) + icon = "audio-volume-medium"; + else + icon = "audio-volume-high"; + break; + case VolumeControl.ActiveOutput.USB_HEADPHONES: + if (volume <= 0.0) + icon = "audio-volume-muted"; + else if (volume <= 0.3) + icon = "audio-volume-low"; + else if (volume <= 0.7) + icon = "audio-volume-medium"; + else + icon = "audio-volume-high"; + break; + case VolumeControl.ActiveOutput.HDMI_SPEAKER: + if (volume <= 0.0) + icon = "audio-volume-muted"; + else if (volume <= 0.3) + icon = "audio-volume-low"; + else if (volume <= 0.7) + icon = "audio-volume-medium"; + else + icon = "audio-volume-high"; + break; + case VolumeControl.ActiveOutput.HDMI_HEADPHONES: + if (volume <= 0.0) + icon = "audio-volume-muted"; + else if (volume <= 0.3) + icon = "audio-volume-low"; + else if (volume <= 0.7) + icon = "audio-volume-medium"; + else + icon = "audio-volume-high"; + break; + } + return icon; + } +} + diff --git a/src/service.vala b/src/service.vala index d1b1c32..71d5b4c 100644 --- a/src/service.vala +++ b/src/service.vala @@ -30,19 +30,12 @@ public class IndicatorSound.Service: Object { _options = options; - info_notification = new Notify.Notification(_("Volume"), "", "audio-volume-muted"); - _volume_warning = volume_warning; _volume_warning.notify["active"].connect(() => { this.increment_volume_sync_action(); this.update_notification(); }); - BusWatcher.watch_namespace (GLib.BusType.SESSION, - "org.freedesktop.Notifications", - () => { debug("Notifications name appeared"); }, - () => { debug("Notifications name vanshed"); notify_server_caps_checked = false; }); - this.settings = new Settings ("com.canonical.indicator.sound"); this.settings.bind ("visible", this, "visible", SettingsBindFlags.GET); @@ -130,7 +123,7 @@ public class IndicatorSound.Service: Object { block_info_notifications = state.get_boolean(); if (block_info_notifications) { debug("Indicator is shown"); - close_notification(info_notification); + _info_notification.close(); } else { debug("Indicator is hidden"); } @@ -146,26 +139,6 @@ public class IndicatorSound.Service: Object { this.menus.@foreach ( (profile, menu) => menu.export (bus, @"/com/canonical/indicator/sound/$profile")); } - private void close_notification(Notify.Notification? n) { - return_if_fail (n != null); - if (n.id != 0) { - try { - n.close(); - } catch (GLib.Error e) { - GLib.warning("Unable to close notification: %s", e.message); - } - } - } - - private void show_notification(Notify.Notification? n) { - return_if_fail (n != null); - try { - n.show (); - } catch (GLib.Error e) { - GLib.warning ("Unable to show notification: %s", e.message); - } - } - ~Service() { debug("Destroying Service Object"); @@ -219,9 +192,9 @@ public class IndicatorSound.Service: Object { bool syncing_preferred_players = false; AccountsServiceUser? accounts_service = null; bool export_to_accounts_service = false; - private Notify.Notification info_notification; private Options _options; private VolumeWarning _volume_warning; + private IndicatorSound.InfoNotification _info_notification = new IndicatorSound.InfoNotification(); const double volume_step_percentage = 0.06; @@ -304,99 +277,8 @@ public class IndicatorSound.Service: Object { root_action.set_state (builder.end()); } - private bool notify_server_caps_checked = false; - private bool notify_server_supports_sync = false; private bool block_info_notifications = false; - private string get_volume_icon (double volume, VolumeControl.ActiveOutput active_output) - { - string icon = ""; - switch (active_output) - { - case VolumeControl.ActiveOutput.SPEAKERS: - if (volume <= 0.0) - icon = "audio-volume-muted"; - else if (volume <= 0.3) - icon = "audio-volume-low"; - else if (volume <= 0.7) - icon = "audio-volume-medium"; - else - icon = "audio-volume-high"; - break; - case VolumeControl.ActiveOutput.HEADPHONES: - if (volume <= 0.0) - icon = "audio-volume-muted"; - else if (volume <= 0.3) - icon = "audio-volume-low"; - else if (volume <= 0.7) - icon = "audio-volume-medium"; - else - icon = "audio-volume-high"; - break; - case VolumeControl.ActiveOutput.BLUETOOTH_HEADPHONES: - if (volume <= 0.0) - icon = "audio-volume-muted"; - else if (volume <= 0.3) - icon = "audio-volume-low"; - else if (volume <= 0.7) - icon = "audio-volume-medium"; - else - icon = "audio-volume-high"; - break; - case VolumeControl.ActiveOutput.BLUETOOTH_SPEAKER: - if (volume <= 0.0) - icon = "audio-volume-muted"; - else if (volume <= 0.3) - icon = "audio-volume-low"; - else if (volume <= 0.7) - icon = "audio-volume-medium"; - else - icon = "audio-volume-high"; - break; - case VolumeControl.ActiveOutput.USB_SPEAKER: - if (volume <= 0.0) - icon = "audio-volume-muted"; - else if (volume <= 0.3) - icon = "audio-volume-low"; - else if (volume <= 0.7) - icon = "audio-volume-medium"; - else - icon = "audio-volume-high"; - break; - case VolumeControl.ActiveOutput.USB_HEADPHONES: - if (volume <= 0.0) - icon = "audio-volume-muted"; - else if (volume <= 0.3) - icon = "audio-volume-low"; - else if (volume <= 0.7) - icon = "audio-volume-medium"; - else - icon = "audio-volume-high"; - break; - case VolumeControl.ActiveOutput.HDMI_SPEAKER: - if (volume <= 0.0) - icon = "audio-volume-muted"; - else if (volume <= 0.3) - icon = "audio-volume-low"; - else if (volume <= 0.7) - icon = "audio-volume-medium"; - else - icon = "audio-volume-high"; - break; - case VolumeControl.ActiveOutput.HDMI_HEADPHONES: - if (volume <= 0.0) - icon = "audio-volume-muted"; - else if (volume <= 0.3) - icon = "audio-volume-low"; - else if (volume <= 0.7) - icon = "audio-volume-medium"; - else - icon = "audio-volume-high"; - break; - } - return icon; - } - private string get_volume_root_icon_by_volume (double volume, VolumeControl.ActiveOutput active_output) { string icon = ""; @@ -486,42 +368,6 @@ public class IndicatorSound.Service: Object { return icon; } - private string get_volume_notification_icon (double volume, bool loud, VolumeControl.ActiveOutput active_output) { - string icon = ""; - if (loud) { - switch (active_output) - { - case VolumeControl.ActiveOutput.SPEAKERS: - icon = "audio-volume-high"; - break; - case VolumeControl.ActiveOutput.HEADPHONES: - icon = "audio-volume-high"; - break; - case VolumeControl.ActiveOutput.BLUETOOTH_HEADPHONES: - icon = "audio-volume-high"; - break; - case VolumeControl.ActiveOutput.BLUETOOTH_SPEAKER: - icon = "audio-volume-high"; - break; - case VolumeControl.ActiveOutput.USB_SPEAKER: - icon = "audio-volume-high"; - break; - case VolumeControl.ActiveOutput.USB_HEADPHONES: - icon = "audio-volume-high"; - break; - case VolumeControl.ActiveOutput.HDMI_SPEAKER: - icon = "audio-volume-high"; - break; - case VolumeControl.ActiveOutput.HDMI_HEADPHONES: - icon = "audio-volume-high"; - break; - } - } else { - icon = get_volume_icon (volume, active_output); - } - return icon; - } - private string get_volume_root_icon (double volume, bool mute, VolumeControl.ActiveOutput active_output) { string icon = ""; switch (active_output) @@ -594,68 +440,11 @@ public class IndicatorSound.Service: Object { return icon; } - private string get_notification_label () { - string volume_label = ""; - switch (volume_control.active_output) - { - case VolumeControl.ActiveOutput.SPEAKERS: - volume_label = _("Speakers"); - break; - case VolumeControl.ActiveOutput.HEADPHONES: - volume_label = _("Headphones"); - break; - case VolumeControl.ActiveOutput.BLUETOOTH_HEADPHONES: - volume_label = _("Bluetooth headphones"); - break; - case VolumeControl.ActiveOutput.BLUETOOTH_SPEAKER: - volume_label = _("Bluetooth speaker"); - break; - case VolumeControl.ActiveOutput.USB_SPEAKER: - volume_label = _("Usb speaker"); - break; - case VolumeControl.ActiveOutput.USB_HEADPHONES: - volume_label = _("Usb headphones"); - break; - case VolumeControl.ActiveOutput.HDMI_SPEAKER: - volume_label = _("HDMI speaker"); - break; - case VolumeControl.ActiveOutput.HDMI_HEADPHONES: - volume_label = _("HDMI headphones"); - break; - } - - return volume_label; - } - private void update_notification () { - - if (!notify_server_caps_checked) - { - GLib.message("service checking server caps"); - List<string> caps = Notify.get_server_caps (); - notify_server_supports_sync = caps.find_custom ("x-canonical-private-synchronous", strcmp) != null; - notify_server_caps_checked = true; - GLib.message("service done checking server caps"); - } - - if (!_volume_warning.active && notify_server_supports_sync && !block_info_notifications) - { - bool is_loud = _volume_warning.high_volume; - - string volume_label = get_notification_label (); - string icon = get_volume_notification_icon (volume_control.volume.volume, is_loud, volume_control.active_output); - - /* Reset the notification */ - GLib.message("service showing info notification"); - var n = this.info_notification; - n.update (_("Volume"), volume_label, icon); - n.clear_hints(); - n.set_hint ("x-canonical-non-shaped-icon", "true"); - n.set_hint ("x-canonical-private-synchronous", "true"); - n.set_hint ("x-canonical-value-bar-tint", is_loud ? "true" : "false"); - n.set_hint ("value", (int32)Math.round(get_volume_percent() * 100.0)); - show_notification(n); - GLib.message("service done showing info notification"); + if (!_volume_warning.active && !block_info_notifications) { + _info_notification.show(this.volume_control.active_output, + this.volume_control.volume.volume, + _volume_warning.high_volume); } } |