Skip to content

Commit

Permalink
ITunesFeature: Move ITunesDAO ownership into ITunesFeature
Browse files Browse the repository at this point in the history
  • Loading branch information
fwcd committed Apr 24, 2023
1 parent fd429b6 commit 36f0775
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 28 deletions.
12 changes: 8 additions & 4 deletions src/library/itunes/itunesfeature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "library/baseexternaltrackmodel.h"
#include "library/basetrackcache.h"
#include "library/dao/settingsdao.h"
#include "library/itunes/itunesdao.h"
#include "library/itunes/itunesimporter.h"
#include "library/itunes/ituneslocalhosttoken.h"
#include "library/itunes/itunesxmlimporter.h"
Expand Down Expand Up @@ -306,15 +307,15 @@ QString ITunesFeature::getiTunesMusicPath() {
return musicFolder;
}

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

// This method is executed in a separate thread
Expand All @@ -328,7 +329,10 @@ TreeItem* ITunesFeature::importLibrary() {

ScopedTransaction transaction(m_database);

std::unique_ptr<ITunesImporter> importer = makeImporter();
ITunesDAO dao;
dao.initialize(m_database);

std::unique_ptr<ITunesImporter> importer = makeImporter(dao);
ITunesImport iTunesImport = importer->importLibrary();

// Even if an error occurred, commit the transaction. The file may have been
Expand Down
3 changes: 2 additions & 1 deletion src/library/itunes/itunesfeature.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <atomic>

#include "library/baseexternallibraryfeature.h"
#include "library/itunes/itunesdao.h"
#include "library/itunes/itunesimporter.h"
#include "library/trackcollection.h"
#include "library/treeitem.h"
Expand Down Expand Up @@ -42,7 +43,7 @@ class ITunesFeature : public BaseExternalLibraryFeature {
std::unique_ptr<BaseSqlTableModel> createPlaylistModelForPlaylist(
const QString& playlist) override;
static QString getiTunesMusicPath();
std::unique_ptr<ITunesImporter> makeImporter();
std::unique_ptr<ITunesImporter> makeImporter(ITunesDAO& dao);
// returns the invisible rootItem for the sidebar model
TreeItem* importLibrary();
void guessMusicLibraryMountpoint(QXmlStreamReader& xml);
Expand Down
10 changes: 6 additions & 4 deletions src/library/itunes/itunesmacosimporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <QString>
#include <atomic>

#include "library/itunes/itunesdao.h"
#include "library/itunes/itunesimporter.h"
#include "library/libraryfeature.h"

Expand All @@ -12,16 +13,17 @@
class ITunesMacOSImporter : public ITunesImporter {
public:
ITunesMacOSImporter(LibraryFeature* parentFeature,
const QSqlDatabase& database,
const std::atomic<bool>& cancelImport);
const std::atomic<bool>& cancelImport,
ITunesDAO& dao);

ITunesImport importLibrary() override;

private:
LibraryFeature* m_parentFeature;
const std::atomic<bool>& m_cancelImport;
// The values behind these references are owned by the parent `ITunesFeature`,
// (note that the DAO internally contains a database reference),
// thus there is an implicit contract here that this `ITunesMacOSImporter` cannot
// outlive the feature (which should not happen anyway, since importers are short-lived).
const QSqlDatabase& m_database;
const std::atomic<bool>& m_cancelImport;
ITunesDAO& m_dao;
};
18 changes: 8 additions & 10 deletions src/library/itunes/itunesmacosimporter.mm
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ QString qStringFrom(NSString* nsString) {

class ImporterImpl {
public:
ImporterImpl(
const QSqlDatabase& database, const std::atomic<bool>& cancelImport)
: m_cancelImport(cancelImport) {
m_dao.initialize(database);
ImporterImpl(const std::atomic<bool>& cancelImport, ITunesDAO& dao)
: m_cancelImport(cancelImport), m_dao(dao) {
}

void importPlaylists(NSArray<ITLibPlaylist*>* playlists) {
Expand Down Expand Up @@ -72,7 +70,7 @@ void appendPlaylistTree(TreeItem& item) {
const std::atomic<bool>& m_cancelImport;

QHash<unsigned long long, int> m_dbIdByPersistentId;
ITunesDAO m_dao;
ITunesDAO& m_dao;

int dbIdFromPersistentId(NSNumber* boxedPersistentId) {
// Map a persistent ID as used by iTunes to an (incrementing) database
Expand Down Expand Up @@ -198,11 +196,11 @@ void importMediaItem(ITLibMediaItem* item) {
} // anonymous namespace

ITunesMacOSImporter::ITunesMacOSImporter(LibraryFeature* parentFeature,
const QSqlDatabase& database,
const std::atomic<bool>& cancelImport)
const std::atomic<bool>& cancelImport,
ITunesDAO& dao)
: m_parentFeature(parentFeature),
m_database(database),
m_cancelImport(cancelImport) {
m_cancelImport(cancelImport),
m_dao(dao) {
}

ITunesImport ITunesMacOSImporter::importLibrary() {
Expand All @@ -214,7 +212,7 @@ void importMediaItem(ITLibMediaItem* item) {

if (library) {
std::unique_ptr<TreeItem> rootItem = TreeItem::newRoot(m_parentFeature);
ImporterImpl impl(m_database, m_cancelImport);
ImporterImpl impl(m_cancelImport, m_dao);

impl.importPlaylists(library.allPlaylists);
impl.importMediaItems(library.allMediaItems);
Expand Down
9 changes: 4 additions & 5 deletions src/library/itunes/itunesxmlimporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,14 @@ const QString kRemote = "Remote";

ITunesXMLImporter::ITunesXMLImporter(LibraryFeature* parentFeature,
const QString& xmlFilePath,
const QSqlDatabase& database,
const std::atomic<bool>& cancelImport)
const std::atomic<bool>& cancelImport,
ITunesDAO& dao)
: m_parentFeature(parentFeature),
m_xmlFilePath(xmlFilePath),
m_xmlFile(xmlFilePath),
m_xml(&m_xmlFile),
m_cancelImport(cancelImport) {
m_dao.initialize(database);

m_cancelImport(cancelImport),
m_dao(dao) {
// By default set m_mixxxItunesRoot and m_dbItunesRoot to strip out
// file://localhost/ from the URL. When we load the user's iTunes XML
// configuration we may replace this with something based on the detected
Expand Down
8 changes: 4 additions & 4 deletions src/library/itunes/itunesxmlimporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class ITunesXMLImporter : public ITunesImporter {
public:
ITunesXMLImporter(LibraryFeature* parentFeature,
const QString& xmlFilePath,
const QSqlDatabase& database,
const std::atomic<bool>& cancelImport);
const std::atomic<bool>& cancelImport,
ITunesDAO& dao);

ITunesImport importLibrary() override;

Expand All @@ -26,12 +26,12 @@ class ITunesXMLImporter : public ITunesImporter {
const QString m_xmlFilePath;
QFile m_xmlFile;
QXmlStreamReader m_xml;
const std::atomic<bool>& m_cancelImport;
// The values behind the references are owned by the parent `ITunesFeature`
// (note that the DAO internally contains a database reference),
// thus there is an implicit contract here that this `ITunesXMLImporter` cannot
// outlive the feature (which should not happen anyway, since importers are short-lived).
ITunesDAO m_dao;
const std::atomic<bool>& m_cancelImport;
ITunesDAO& m_dao;

ITunesPathMapping m_pathMapping;
QHash<QString, int> m_playlistIdByPersistentId;
Expand Down

0 comments on commit 36f0775

Please sign in to comment.