Exemplo n.º 1
0
def open_app_at_startup(enabled=True):
    """
    This function adds/removes the current app bundle from Login items in macOS
    """
    if sys.platform == 'darwin':
        from Foundation import NSDictionary

        from Cocoa import NSBundle, NSURL
        from CoreFoundation import kCFAllocatorDefault
        # CF = CDLL(find_library('CoreFoundation'))
        from LaunchServices import (LSSharedFileListCreate,
                                    kLSSharedFileListSessionLoginItems,
                                    LSSharedFileListInsertItemURL,
                                    kLSSharedFileListItemHidden,
                                    kLSSharedFileListItemLast,
                                    LSSharedFileListItemRemove)

        app_path = NSBundle.mainBundle().bundlePath()
        url = NSURL.alloc().initFileURLWithPath_(app_path)
        login_items = LSSharedFileListCreate(
            kCFAllocatorDefault, kLSSharedFileListSessionLoginItems, None)
        props = NSDictionary.dictionaryWithObject_forKey_(
            True, kLSSharedFileListItemHidden)

        new_item = LSSharedFileListInsertItemURL(login_items,
                                                 kLSSharedFileListItemLast,
                                                 None, None, url, props, None)
        if not enabled:
            LSSharedFileListItemRemove(login_items, new_item)
Exemplo n.º 2
0
def open_app_at_startup(enabled=True):
    """
    This function adds/removes the current app bundle from Login items in macOS or most Linux desktops
    """
    if sys.platform == 'darwin':
        from Foundation import NSDictionary

        from Cocoa import NSBundle, NSURL
        from CoreFoundation import kCFAllocatorDefault
        # CF = CDLL(find_library('CoreFoundation'))
        from LaunchServices import (LSSharedFileListCreate,
                                    kLSSharedFileListSessionLoginItems,
                                    LSSharedFileListInsertItemURL,
                                    kLSSharedFileListItemHidden,
                                    kLSSharedFileListItemLast,
                                    LSSharedFileListItemRemove)

        app_path = NSBundle.mainBundle().bundlePath()
        url = NSURL.alloc().initFileURLWithPath_(app_path)
        login_items = LSSharedFileListCreate(
            kCFAllocatorDefault, kLSSharedFileListSessionLoginItems, None)
        props = NSDictionary.dictionaryWithObject_forKey_(
            True, kLSSharedFileListItemHidden)

        new_item = LSSharedFileListInsertItemURL(login_items,
                                                 kLSSharedFileListItemLast,
                                                 None, None, url, props, None)
        if not enabled:
            LSSharedFileListItemRemove(login_items, new_item)

    elif sys.platform.startswith('linux'):
        autostart_path = Path.home() / '.config' / 'autostart'

        if not autostart_path.exists():
            autostart_path.mkdir()

        autostart_file_path = autostart_path / 'vorta.desktop'

        if enabled:
            if Path('/.flatpak-info').exists():
                # Vorta runs as flatpak
                autostart_file_path.write_text(
                    LINUX_STARTUP_FILE.format(
                        'flatpak run com.borgbase.vorta'))
            else:
                autostart_file_path.write_text(
                    LINUX_STARTUP_FILE.format('vorta'))

        else:
            if autostart_file_path.exists():
                autostart_file_path.unlink()
    def add(self, to_add: str, uri: str = "file://localhost") -> None:
        """Append item to sidebar list items.

        :param str to_add: Path to item to append to sidebar list.
        :param str uri: URI of server where item resides if not on localhost.
        """

        if uri.startswith("afp") or uri.startswith("smb"):
            to_add = mount_share(uri + to_add)

        item = NSURL.alloc().initFileURLWithPath_(to_add)
        LSSharedFileListInsertItemURL(self.sflRef,
                                      kLSSharedFileListItemBeforeFirst, None,
                                      None, item, None, None)
        self.synchronize()
        self.update()
	def add(self, to_add, uri="file://localhost"):
		"""
		Append item to sidebar list items.

		Args:
			to_add (str): Path to item to append to sidebar list.

		Keyword Args:
			uri (str): URI of server where item resides if not on localhost.

		"""
		if uri.startswith("afp") or uri.startswith("smb"):
			path = "%s%s" % (uri, to_add)
			to_add = mount_share(path)
		item = NSURL.alloc().initFileURLWithPath_(to_add)
		LSSharedFileListInsertItemURL(self.sflRef, kLSSharedFileListItemBeforeFirst, None, None, item, None, None)
		self.synchronize()
		self.update()
    def add(self, to_add, uri="file://localhost"):
        """
		Append item to sidebar list items.

		Args:
			to_add (str): Path to item to append to sidebar list.

		Keyword Args:
			uri (str): URI of server where item resides if not on localhost.

		"""
        if uri.startswith("afp") or uri.startswith("smb"):
            path = "%s%s" % (uri, to_add)
            to_add = mount_share(path)
        item = NSURL.alloc().initFileURLWithPath_(to_add)
        LSSharedFileListInsertItemURL(self.sflRef,
                                      kLSSharedFileListItemBeforeFirst, None,
                                      None, item, None, None)
        self.synchronize()
        self.update()
Exemplo n.º 6
0
def open_app_at_startup(enabled=True):
    """
    This function adds/removes the current app bundle from Login items in macOS or most Linux desktops
    """
    if sys.platform == 'darwin':
        from Foundation import NSDictionary

        from Cocoa import NSBundle, NSURL
        from CoreFoundation import kCFAllocatorDefault
        # CF = CDLL(find_library('CoreFoundation'))
        from LaunchServices import (LSSharedFileListCreate,
                                    kLSSharedFileListSessionLoginItems,
                                    LSSharedFileListInsertItemURL,
                                    kLSSharedFileListItemHidden,
                                    kLSSharedFileListItemLast,
                                    LSSharedFileListItemRemove)

        app_path = NSBundle.mainBundle().bundlePath()
        url = NSURL.alloc().initFileURLWithPath_(app_path)
        login_items = LSSharedFileListCreate(
            kCFAllocatorDefault, kLSSharedFileListSessionLoginItems, None)
        props = NSDictionary.dictionaryWithObject_forKey_(
            True, kLSSharedFileListItemHidden)

        new_item = LSSharedFileListInsertItemURL(login_items,
                                                 kLSSharedFileListItemLast,
                                                 None, None, url, props, None)
        if not enabled:
            LSSharedFileListItemRemove(login_items, new_item)
    elif sys.platform.startswith('linux'):
        config_path = QtCore.QStandardPaths.writableLocation(
            QtCore.QStandardPaths.ConfigLocation)
        autostart_file_path = Path(config_path) / 'autostart' / 'vorta.desktop'
        if enabled:
            dir_entry_point = get_setuptools_script_dir()
            autostart_file_path.write_text(
                LINUX_STARTUP_FILE.format(dir_entry_point))
        else:
            if autostart_file_path.exists():
                autostart_file_path.unlink()
Exemplo n.º 7
0
from CoreFoundation import CFArrayGetCount, CFArrayGetValueAtIndex, kCFAllocatorDefault
from Foundation import NSBundle
from LaunchServices import kLSSharedFileListFavoriteItems
from objc import loadBundleFunctions
from sys import argv

# For some reason, these functions cannot be imported directly and must be
# manually loaded from the SharedFileList bundle
SFL_bundle = NSBundle.bundleWithIdentifier_('com.apple.coreservices.SharedFileList')
functions = [
    ('LSSharedFileListCreate', '^{OpaqueLSSharedFileListRef=}^{__CFAllocator=}^{__CFString=}@'),
    ('LSSharedFileListCopySnapshot', '^{__CFArray=}^{OpaqueLSSharedFileListRef=}o^I'),
    ('LSSharedFileListInsertItemURL', '^{OpaqueLSSharedFileListItemRef=}^{OpaqueLSSharedFileListRef=}^{OpaqueLSSharedFileListItemRef=}^{__CFString=}^{OpaqueIconRef=}^{__CFURL=}^{__CFDictionary=}^{__CFArray=}'),
    ('kLSSharedFileListItemBeforeFirst', '^{OpaqueLSSharedFileListItemRef=}'),
]
loadBundleFunctions(SFL_bundle, globals(), functions)

# The path to added to the Finder's favorites
path = argv[1]

# Make it an URL object (which is a valid favorite item)
item = NSURL.alloc().initFileURLWithPath_(path)

# Retrieve the favorite items list
favorite_items = LSSharedFileListCreate(kCFAllocatorDefault,
                                        kLSSharedFileListFavoriteItems, None)

# Add the item to the top of the list
LSSharedFileListInsertItemURL(favorite_items, kLSSharedFileListItemBeforeFirst,
                              None, None, item, None, None)
Exemplo n.º 8
0
def open_app_at_startup(enabled=True):
    """
    On macOS, this function adds/removes the current app bundle from Login items
    while on Linux it adds a .desktop file at ~/.config/autostart
    """
    if sys.platform == 'darwin':
        from Foundation import NSDictionary

        from Cocoa import NSBundle, NSURL
        from CoreFoundation import kCFAllocatorDefault
        # CF = CDLL(find_library('CoreFoundation'))
        from LaunchServices import (LSSharedFileListCreate,
                                    kLSSharedFileListSessionLoginItems,
                                    LSSharedFileListInsertItemURL,
                                    kLSSharedFileListItemHidden,
                                    kLSSharedFileListItemLast,
                                    LSSharedFileListItemRemove)

        app_path = NSBundle.mainBundle().bundlePath()
        url = NSURL.alloc().initFileURLWithPath_(app_path)
        login_items = LSSharedFileListCreate(
            kCFAllocatorDefault, kLSSharedFileListSessionLoginItems, None)
        props = NSDictionary.dictionaryWithObject_forKey_(
            True, kLSSharedFileListItemHidden)

        new_item = LSSharedFileListInsertItemURL(login_items,
                                                 kLSSharedFileListItemLast,
                                                 None, None, url, props, None)
        if not enabled:
            LSSharedFileListItemRemove(login_items, new_item)

    elif sys.platform.startswith('linux'):
        from appdirs import user_config_dir
        from pathlib import Path

        is_flatpak = Path('/.flatpak-info').exists()

        with open(
                Path(__file__).parent /
                "assets/metadata/com.borgbase.Vorta.desktop") as desktop_file:
            desktop_file_text = desktop_file.read()

        # Find XDG_CONFIG_HOME unless when running in flatpak
        if is_flatpak:
            autostart_path = Path.home() / '.config' / 'autostart'
        else:
            autostart_path = Path(user_config_dir("autostart"))

        if not autostart_path.exists():
            autostart_path.mkdir(parents=True, exist_ok=True)

        autostart_file_path = autostart_path / 'vorta.desktop'

        if enabled:
            # Replace command for flatpak if appropriate and start in background
            desktop_file_text = desktop_file_text.replace(
                "Exec=vorta", "Exec=flatpak run com.borgbase.Vorta --daemonize"
                if is_flatpak else "Exec=vorta --daemonize")
            # Add autostart delay
            desktop_file_text += (AUTOSTART_DELAY)

            autostart_file_path.write_text(desktop_file_text)
        elif autostart_file_path.exists():
            autostart_file_path.unlink()
Exemplo n.º 9
0
# For some reason, these functions cannot be imported directly and must be
# manually loaded from the SharedFileList bundle
SFL_bundle = NSBundle.bundleWithIdentifier_(
    'com.apple.coreservices.SharedFileList')
functions = [
    ('LSSharedFileListCreate',
     '^{OpaqueLSSharedFileListRef=}^{__CFAllocator=}^{__CFString=}@'),
    ('LSSharedFileListCopySnapshot',
     '^{__CFArray=}^{OpaqueLSSharedFileListRef=}o^I'),
    ('LSSharedFileListInsertItemURL',
     '^{OpaqueLSSharedFileListItemRef=}^{OpaqueLSSharedFileListRef=}^{OpaqueLSSharedFileListItemRef=}^{__CFString=}^{OpaqueIconRef=}^{__CFURL=}^{__CFDictionary=}^{__CFArray=}'
     ),
    ('kLSSharedFileListItemBeforeFirst', '^{OpaqueLSSharedFileListItemRef=}'),
]
loadBundleFunctions(SFL_bundle, globals(), functions)

# The path to added to the Finder's favorites
path = argv[1]

# Make it an URL object (which is a valid favorite item)
item = NSURL.alloc().initFileURLWithPath_(path)

# Retrieve the favorite items list
favorite_items = LSSharedFileListCreate(kCFAllocatorDefault,
                                        kLSSharedFileListFavoriteItems, None)

# Add the item to the top of the list
LSSharedFileListInsertItemURL(favorite_items, kLSSharedFileListItemBeforeFirst,
                              None, None, item, None, None)