Skip to content

Commit

Permalink
Merge pull request #154 from pieces-app/fix-share
Browse files Browse the repository at this point in the history
Fix bugs
  • Loading branch information
bishoy-at-pieces authored Sep 9, 2024
2 parents 4b17bc8 + ded1597 commit b4e0b2b
Show file tree
Hide file tree
Showing 25 changed files with 1,030 additions and 71 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,4 @@ cython_debug/
.idea/
Pieces/
Pieces.sublime-package
_debug.py
131 changes: 131 additions & 0 deletions _pieces_lib/notify/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
"""
Notify.
Copyright (c) 2013 - 2016 Isaac Muse <[email protected]>
License: MIT
"""
from __future__ import absolute_import
import sys

PY3 = (3, 0) <= sys.version_info < (4, 0)

if PY3:
binary_type = bytes # noqa
else:
binary_type = str

if sys.platform.startswith('win'):
_PLATFORM = "windows"
elif sys.platform == "darwin":
_PLATFORM = "macos"
else:
_PLATFORM = "linux"

if _PLATFORM == "windows":
from .notify_windows import get_notify, alert, setup, windows_icons, destroy
elif _PLATFORM == "macos":
from .notify_osx import get_notify, alert, setup, destroy
elif _PLATFORM == "linux":
from .notify_linux import get_notify, alert, setup, destroy

__all__ = ("info", "warning", "error", "setup_notifications", "destroy_notifications")


###################################
# Fallback Notifications
###################################
class NotifyFallback:
"""Fallback class."""

def __init__(self, *args, **kwargs):
"""Initialize class."""

self.sound = kwargs.get("sound", False)

def Show(self):
"""Fallback just plays an alert."""

if self.sound:
alert()


DEFAULT_NOTIFY = NotifyFallback


###################################
# Notification Calls
###################################
def info(title, message, sound=False):
"""Info notification."""

send_notify(title, message, sound, "Info")


def error(title, message, sound=False):
"""Error notification."""
send_notify(title, message, sound, "Error")


def warning(title, message, sound=False):
"""Warning notification."""

send_notify(title, message, sound, "Warning")


def send_notify(title, message, sound, level):
"""Send notification."""

if title is not None and isinstance(title, binary_type):
title = title.decode('utf-8')

if message is not None and isinstance(message, binary_type):
message = message.decode('utf-8')

if level is not None and isinstance(level, binary_type):
level = level.decode('utf-8')

def default_notify(title, message, sound):
"""Default fallback notify."""

DEFAULT_NOTIFY(title, message, sound=sound).Show()

notify = get_notify()
if _PLATFORM in ["macos", "linux"]:
notify(title, message, sound, default_notify)
elif _PLATFORM == "windows":
notify(title, message, sound, windows_icons[level], default_notify)
else:
default_notify(title, message, sound)


def play_alert():
"""Play alert sound."""

alert()


###################################
# Setup Notifications
###################################
def setup_notifications(app_name, img=None, **kwargs):
"""Setup notifications for all platforms."""

destroy()

if _PLATFORM == "windows" and img is not None and isinstance(img, binary_type):
img = img.decode('utf-8')

if isinstance(app_name, binary_type):
app_name = app_name.decode('utf-8')

setup(
app_name,
img,
**kwargs
)


def destroy_notifications():
"""Destroy notifications if possible."""

destroy()
141 changes: 141 additions & 0 deletions _pieces_lib/notify/notify_linux.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
"""
Notify Linux.
Copyright (c) 2013 - 2016 Isaac Muse <[email protected]>
License: MIT
"""
import subprocess
import os
from . import util

__all__ = ("get_notify", "alert", "setup", "destroy")

PLAYERS = ('paplay', 'aplay', 'play')


class Options:
"""Notification options."""

icon = None
notify = None
app_name = ""
sound = None
player = None

@classmethod
def clear(cls):
"""Clear."""

cls.icon = None
cls.notify = None
cls.app_name = ""
cls.sound = None
cls.player = None


def _alert(sound=None, player=None):
"""Play an alert sound for the OS."""

if sound is None and Options.sound is not None:
sound = Options.sound

if player is None and Options.player is not None:
player = Options.player

if player is not None and sound is not None:
try:
if player == 'play':
subprocess.call([player, '-q', sound])
else:
subprocess.call([player, sound])
except Exception:
pass


def alert():
"""Alert."""

_alert()


@staticmethod
def notify_osd_fallback(title, message, sound, fallback):
"""Ubuntu Notify OSD notifications fallback (just sound)."""

# Fallback to wxPython notification
fallback(title, message, sound)


try:
if subprocess.call(["notify-send", "--version"]) != 0:
raise ValueError("Notification support does not appear to be available")

@staticmethod
def notify_osd_call(title, message, sound, fallback):
"""Ubuntu Notify OSD notifications."""

try:
params = ["notify-send", "-a", Options.app_name, "-t", "3000"]
if Options.icon is not None:
params += ["-i", Options.icon]
if message is not None:
params += [title, message]
subprocess.call(params)

if sound:
# Play sound if desired
alert()
except Exception:
# Fallback to wxPython notification
fallback(title, message, sound)
except Exception:
notify_osd_call = None
print("no notify osd")


def setup_notify_osd(app_name):
"""Setup Notify OSD."""

if notify_osd_call is not None:
Options.app_name = app_name
Options.notify = notify_osd_call


def setup(app_name, icon, **kwargs):
"""Setup."""

Options.icon = None
sound = kwargs.get('sound')
if sound is not None and os.path.exists(sound):
Options.sound = sound

player = kwargs.get('sound_player')
if player is not None and player in PLAYERS and util.which(player):
Options.player = player

try:
if icon is None or not os.path.exists(icon):
raise ValueError("Icon does not appear to be valid")
Options.icon = icon
except Exception:
pass

if notify_osd_call is not None:
Options.app_name = app_name
Options.notify = notify_osd_call


def destroy():
"""Destroy."""

Options.clear()
Options.notify = notify_osd_fallback


def get_notify():
"""Get notification."""

return Options.notify


Options.notify = notify_osd_fallback
Loading

0 comments on commit b4e0b2b

Please sign in to comment.