Merge pull request #631 from samj1912/picard988

PICARD-988: Extract file paths from NSURL while drag'n'drop
This commit is contained in:
Laurent Monin
2017-02-19 11:35:20 +01:00
committed by GitHub

View File

@@ -19,6 +19,7 @@
import os
import re
import sys
from functools import partial
from PyQt4 import QtCore, QtGui
from picard import config, log
@@ -31,6 +32,14 @@ from picard.plugin import ExtensionPoint
from picard.ui.ratingwidget import RatingWidget
from picard.ui.collectionmenu import CollectionMenu
if sys.platform == 'darwin':
try:
from Foundation import NSURL
NSURL_IMPORTED = True
except ImportError:
NSURL_IMPORTED = False
log.warning("Unable to import NSURL, file drag'n'drop might not work correctly")
class BaseAction(QtGui.QAction):
NAME = "Unknown"
@@ -467,9 +476,19 @@ class BaseTreeView(QtGui.QTreeWidget):
files = []
new_files = []
for url in urls:
log.debug("Dropped the URL: %r", url.toString(QtCore.QUrl.RemoveUserInfo))
if url.scheme() == "file" or not url.scheme():
# Dropping a file from iTunes gives a filename with a NULL terminator
filename = os.path.normpath(os.path.realpath(unicode(url.toLocalFile()).rstrip("\0")))
# Workaround for https://bugreports.qt.io/browse/QTBUG-40449
# OSX Urls follow the NSURL scheme and need to be converted
if sys.platform == 'darwin' and unicode(url.path()).startswith('/.file/id='):
if NSURL_IMPORTED:
filename = os.path.normpath(os.path.realpath(unicode(NSURL.URLWithString_(str(url.toString())).filePathURL().path()).rstrip("\0")))
log.debug('OSX NSURL path detected. Dropped File is: %r', filename)
else:
log.error("Unable to get appropriate file path for %r", url.toString(QtCore.QUrl.RemoveUserInfo))
else:
# Dropping a file from iTunes gives a filename with a NULL terminator
filename = os.path.normpath(os.path.realpath(unicode(url.toLocalFile()).rstrip("\0")))
file = BaseTreeView.tagger.files.get(filename)
if file:
files.append(file)