strawberry-audio-player-win.../src/device/mtpconnection.cpp

178 lines
5.7 KiB
C++
Raw Permalink Normal View History

2018-02-27 18:06:05 +01:00
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
2021-03-20 21:14:47 +01:00
* Copyright 2019-2021, Jonas Kvinge <jonas@jkvinge.net>
2018-02-27 18:06:05 +01:00
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Strawberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
2018-08-09 18:39:44 +02:00
*
2018-02-27 18:06:05 +01:00
*/
#include "config.h"
2020-06-14 23:54:18 +02:00
#include <cstdlib>
#include <cstdint>
2020-02-09 02:29:35 +01:00
#include <QList>
#include <QString>
#include <QUrl>
2018-02-27 18:06:05 +01:00
#include <QUrlQuery>
2020-07-20 00:57:42 +02:00
#include <QRegularExpression>
2018-02-27 18:06:05 +01:00
#include "core/logging.h"
#include "mtpconnection.h"
2018-02-27 18:06:05 +01:00
2021-06-20 19:04:08 +02:00
MtpConnection::MtpConnection(const QUrl &url, QObject *parent) : QObject(parent), device_(nullptr) {
2018-02-27 18:06:05 +01:00
QString hostname = url.host();
// Parse the URL
2024-04-09 23:20:26 +02:00
QRegularExpression host_re(QStringLiteral("^usb-(\\d+)-(\\d+)$"));
2018-02-27 18:06:05 +01:00
unsigned int bus_location = 0;
unsigned int device_num = 0;
QUrlQuery url_query(url);
2020-07-20 00:57:42 +02:00
QRegularExpressionMatch re_match = host_re.match(hostname);
if (re_match.hasMatch()) {
bus_location = re_match.captured(1).toUInt();
device_num = re_match.captured(2).toUInt();
}
2024-04-09 23:20:26 +02:00
else if (url_query.hasQueryItem(QStringLiteral("busnum"))) {
bus_location = url_query.queryItemValue(QStringLiteral("busnum")).toUInt();
device_num = url_query.queryItemValue(QStringLiteral("devnum")).toUInt();
}
else {
error_text_ = tr("Invalid MTP device: %1").arg(hostname);
qLog(Error) << error_text_;
2018-02-27 18:06:05 +01:00
return;
}
2024-04-09 23:20:26 +02:00
if (url_query.hasQueryItem(QStringLiteral("vendor"))) {
2020-06-15 17:59:02 +02:00
LIBMTP_raw_device_t *raw_device = static_cast<LIBMTP_raw_device_t*>(malloc(sizeof(LIBMTP_raw_device_t)));
2024-04-09 23:20:26 +02:00
raw_device->device_entry.vendor = url_query.queryItemValue(QStringLiteral("vendor")).toLatin1().data();
raw_device->device_entry.product = url_query.queryItemValue(QStringLiteral("product")).toLatin1().data();
raw_device->device_entry.vendor_id = url_query.queryItemValue(QStringLiteral("vendor_id")).toUShort();
raw_device->device_entry.product_id = url_query.queryItemValue(QStringLiteral("product_id")).toUShort();
raw_device->device_entry.device_flags = url_query.queryItemValue(QStringLiteral("quirks")).toUInt();
2018-02-27 18:06:05 +01:00
raw_device->bus_location = bus_location;
raw_device->devnum = device_num;
2021-06-20 19:04:08 +02:00
device_ = LIBMTP_Open_Raw_Device(raw_device); // NOLINT(clang-analyzer-unix.Malloc)
if (!device_) {
error_text_ = tr("Could not open MTP device.");
qLog(Error) << error_text_;
}
2018-02-27 18:06:05 +01:00
return;
}
// Get a list of devices from libmtp and figure out which one is ours
int count = 0;
LIBMTP_raw_device_t *raw_devices = nullptr;
LIBMTP_error_number_t error_number = LIBMTP_Detect_Raw_Devices(&raw_devices, &count);
if (error_number != LIBMTP_ERROR_NONE) {
error_text_ = tr("MTP error: %1").arg(ErrorString(error_number));
qLog(Error) << error_text_;
2018-02-27 18:06:05 +01:00
return;
}
LIBMTP_raw_device_t *raw_device = nullptr;
for (int i = 0; i < count; ++i) {
if (raw_devices[i].bus_location == bus_location && raw_devices[i].devnum == device_num) {
raw_device = &raw_devices[i];
break;
}
}
if (!raw_device) {
error_text_ = tr("MTP device not found.");
qLog(Error) << error_text_;
2018-02-27 18:06:05 +01:00
free(raw_devices);
return;
}
// Connect to the device
device_ = LIBMTP_Open_Raw_Device(raw_device);
if (!device_) {
error_text_ = tr("Could not open MTP device.");
qLog(Error) << error_text_;
}
2018-02-27 18:06:05 +01:00
free(raw_devices);
}
MtpConnection::~MtpConnection() {
if (device_) LIBMTP_Release_Device(device_);
}
QString MtpConnection::ErrorString(const LIBMTP_error_number_t error_number) {
switch(error_number) {
case LIBMTP_ERROR_NO_DEVICE_ATTACHED:
2024-04-09 23:20:26 +02:00
return QStringLiteral("No Devices have been found.");
case LIBMTP_ERROR_CONNECTING:
2024-04-09 23:20:26 +02:00
return QStringLiteral("There has been an error connecting.");
case LIBMTP_ERROR_MEMORY_ALLOCATION:
2024-04-09 23:20:26 +02:00
return QStringLiteral("Memory Allocation Error.");
case LIBMTP_ERROR_GENERAL:
default:
2024-04-09 23:20:26 +02:00
return QStringLiteral("Unknown error, please report this to the libmtp developers.");
case LIBMTP_ERROR_NONE:
2024-04-09 23:20:26 +02:00
return QStringLiteral("Successfully connected.");
}
}
2019-07-19 19:56:37 +02:00
bool MtpConnection::GetSupportedFiletypes(QList<Song::FileType> *ret) {
if (!device_) return false;
uint16_t *list = nullptr;
uint16_t length = 0;
2021-06-22 13:54:58 +02:00
if (LIBMTP_Get_Supported_Filetypes(device_, &list, &length) != 0 || !list || !length) {
2019-07-19 19:56:37 +02:00
return false;
2021-06-22 13:54:58 +02:00
}
2019-07-19 19:56:37 +02:00
for (int i = 0; i < length; ++i) {
2022-06-13 00:23:42 +02:00
switch (static_cast<LIBMTP_filetype_t>(list[i])) {
2023-02-18 14:09:27 +01:00
case LIBMTP_FILETYPE_WAV: *ret << Song::FileType::WAV; break;
2019-07-19 19:56:37 +02:00
case LIBMTP_FILETYPE_MP2:
2023-02-18 14:09:27 +01:00
case LIBMTP_FILETYPE_MP3: *ret << Song::FileType::MPEG; break;
case LIBMTP_FILETYPE_WMA: *ret << Song::FileType::ASF; break;
2019-07-19 19:56:37 +02:00
case LIBMTP_FILETYPE_MP4:
case LIBMTP_FILETYPE_M4A:
2023-02-18 14:09:27 +01:00
case LIBMTP_FILETYPE_AAC: *ret << Song::FileType::MP4; break;
2019-07-19 19:56:37 +02:00
case LIBMTP_FILETYPE_FLAC:
2023-02-18 14:09:27 +01:00
*ret << Song::FileType::FLAC;
*ret << Song::FileType::OggFlac;
2019-07-19 19:56:37 +02:00
break;
case LIBMTP_FILETYPE_OGG:
2023-02-18 14:09:27 +01:00
*ret << Song::FileType::OggVorbis;
*ret << Song::FileType::OggSpeex;
*ret << Song::FileType::OggFlac;
2019-07-19 19:56:37 +02:00
break;
default:
2022-06-13 00:23:42 +02:00
qLog(Error) << "Unknown MTP file format" << LIBMTP_Get_Filetype_Description(static_cast<LIBMTP_filetype_t>(list[i]));
2019-07-19 19:56:37 +02:00
break;
}
}
free(list);
return true;
}