Skip to content

Commit

Permalink
ITunesFeature: Factor out makeImporter and guard macOS version
Browse files Browse the repository at this point in the history
  • Loading branch information
fwcd committed Mar 30, 2023
1 parent de5188d commit 6e80d16
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
48 changes: 29 additions & 19 deletions src/library/itunes/itunesfeature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ namespace {

const QString ITDB_PATH_KEY = "mixxx.itunesfeature.itdbpath";

bool isMacOSImporterAvailable() {
#ifdef __MACOS_ITUNES_LIBRARY__
// The iTunesLibrary framework is only available on macOS 10.13+
if (__builtin_available(macOS 10.13, *)) {
return true;
}
#endif
return false;
}

} // anonymous namespace

ITunesFeature::ITunesFeature(Library* pLibrary, UserSettingsPointer pConfig)
Expand Down Expand Up @@ -170,14 +180,11 @@ void ITunesFeature::activate(bool forceReload) {
if (!dbSetting.isEmpty() && QFile::exists(dbSetting)) {
m_dbfile = dbSetting;
} else {
#ifndef __MACOS_ITUNES_LIBRARY__
// No Path in settings, try the default
// if not on macOS, where we use the ITunesMacOSImporter if m_dbfile is empty
m_dbfile = getiTunesMusicPath();
#endif
}

if (!m_dbfile.isEmpty()) {
if (!m_dbfile.isEmpty() || !isMacOSImporterAvailable()) {
mixxx::FileInfo fileInfo(m_dbfile);
if (fileInfo.checkFileExists()) {
// Users of Mixxx <1.12.0 didn't support sandboxing. If we are sandboxed
Expand Down Expand Up @@ -273,6 +280,11 @@ void ITunesFeature::onRightClick(const QPoint& globalPos) {

QString ITunesFeature::getiTunesMusicPath() {
QString musicFolder;
if (isMacOSImporterAvailable()) {
qDebug() << "Using empty iTunes music path (which we interpret as "
"using ITunesMacOSImporter)";
return "";
}
#if defined(__APPLE__)
musicFolder = QStandardPaths::writableLocation(QStandardPaths::MusicLocation)
+ "/iTunes/iTunes Music Library.xml";
Expand All @@ -286,6 +298,18 @@ QString ITunesFeature::getiTunesMusicPath() {
return musicFolder;
}

std::unique_ptr<ITunesImporter> ITunesFeature::makeImporter() {
#ifdef __MACOS_ITUNES_LIBRARY__
if (isMacOSImporterAvailable()) {
qDebug() << "Using ITunesMacOSImporter to read default iTunes library";
return std::make_unique<ITunesMacOSImporter>(this, m_database, m_cancelImport);
}
#endif
qDebug() << "Using ITunesXMLImporter to read iTunes library from " << m_dbfile;
return std::make_unique<ITunesXMLImporter>(
this, m_dbfile, m_database, m_pathMapping, m_cancelImport);
}

// This method is executed in a separate thread
// via QtConcurrent::run
TreeItem* ITunesFeature::importLibrary() {
Expand All @@ -306,21 +330,7 @@ TreeItem* ITunesFeature::importLibrary() {
m_pathMapping.dbITunesRoot = kiTunesLocalhostToken;

ITunesImport iTunesImport;
std::unique_ptr<ITunesImporter> importer;

#ifdef __MACOS_ITUNES_LIBRARY__
if (m_dbfile.isEmpty()) {
qDebug() << "Using ITunesMacOSImporter to read default iTunes library";
importer = std::make_unique<ITunesMacOSImporter>(this, m_database, m_cancelImport);
} else {
qDebug() << "Using ITunesXMLImporter to read iTunes library from " << m_dbfile;
importer = std::make_unique<ITunesXMLImporter>(
this, m_dbfile, m_database, m_pathMapping, m_cancelImport);
}
#else
importer = std::make_unique<ITunesXMLImporter>(
this, m_dbfile, m_database, m_pathMapping, m_cancelImport);
#endif
std::unique_ptr<ITunesImporter> importer = makeImporter();

iTunesImport = importer->importLibrary();

Expand Down
1 change: 1 addition & 0 deletions src/library/itunes/itunesfeature.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class ITunesFeature : public BaseExternalLibraryFeature {
std::unique_ptr<BaseSqlTableModel> createPlaylistModelForPlaylist(
const QString& playlist) override;
static QString getiTunesMusicPath();
std::unique_ptr<ITunesImporter> makeImporter();
// returns the invisible rootItem for the sidebar model
TreeItem* importLibrary();
void guessMusicLibraryMountpoint(QXmlStreamReader& xml);
Expand Down

0 comments on commit 6e80d16

Please sign in to comment.