Skip to content

Commit

Permalink
ITunesIOSImporter: Request authorization synchronously
Browse files Browse the repository at this point in the history
This is required since the importer will otherwise run before the user
has accepted the prompt and thus import no tracks on first launch.
  • Loading branch information
fwcd committed Aug 19, 2024
1 parent 30bd316 commit f82feca
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/library/itunes/itunesiosimporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ class ITunesIOSImporter : public ITunesImporter {

private:
std::unique_ptr<ITunesDAO> m_dao;

void requestAuthorization();
};
31 changes: 31 additions & 0 deletions src/library/itunes/itunesiosimporter.mm
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "library/itunes/itunesiosimporter.h"

#import <MediaPlayer/MediaPlayer.h>
#import <dispatch/dispatch.h>
#include <gsl/pointers>

#include <QDateTime>
Expand Down Expand Up @@ -176,7 +177,37 @@ void importMediaItem(MPMediaItem* item) {
: ITunesImporter(pParentFeature), m_dao(std::move(dao)) {
}

void ITunesIOSImporter::requestAuthorization() {
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

[MPMediaLibrary requestAuthorization:^(MPMediaLibraryAuthorizationStatus status){
switch (status) {
case MPMediaLibraryAuthorizationStatusAuthorized:
qInfo() << "Successfully authorized iOS media library access";
break;
case MPMediaLibraryAuthorizationStatusRestricted:
qWarning() << "iOS media library access is restricted, Mixxx may not be able to access all tracks";
break;
case MPMediaLibraryAuthorizationStatusDenied:
qWarning() << "iOS media library access is denied, Mixxx will not be able to access any tracks";
break;
case MPMediaLibraryAuthorizationStatusNotDetermined:
qWarning() << "iOS media library access is not determined, Mixxx may not be able to access any tracks";
break;
default:
qWarning() << "Unknown iOS media library authorization status:" << status;
break;
}
dispatch_semaphore_signal(semaphore);
}];

qInfo() << "Requesting authorization for iOS media library access...";
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
}

ITunesImport ITunesIOSImporter::importLibrary() {
requestAuthorization();

ITunesImport iTunesImport;

std::unique_ptr<TreeItem> rootItem = TreeItem::newRoot(m_pParentFeature);
Expand Down

0 comments on commit f82feca

Please sign in to comment.