Clementine-audio-player-Mac.../src/devices/mtpconnection.cpp

101 lines
3.0 KiB
C++
Raw Normal View History

2010-08-14 18:39:45 +02:00
/* This file is part of Clementine.
Copyright 2010, David Sansome <me@davidsansome.com>
2010-08-14 18:39:45 +02:00
Clementine 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.
Clementine 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 Clementine. If not, see <http://www.gnu.org/licenses/>.
*/
#include "mtpconnection.h"
#include <QRegExp>
2015-04-15 18:26:09 +02:00
#include <QUrlQuery>
2020-09-18 16:15:19 +02:00
#include <QtDebug>
#include "core/logging.h"
2010-08-14 18:39:45 +02:00
MtpConnection::MtpConnection(const QUrl& url) : device_(nullptr) {
QString hostname = url.host();
2010-08-14 18:39:45 +02:00
// Parse the URL
QRegExp host_re("^usb-(\\d+)-(\\d+)$");
unsigned int bus_location;
unsigned int device_num;
2019-01-18 01:38:12 +01:00
QUrlQuery url_query(url);
if (host_re.indexIn(hostname) >= 0) {
bus_location = host_re.cap(1).toUInt();
device_num = host_re.cap(2).toUInt();
2019-01-18 01:38:12 +01:00
} else if (url_query.hasQueryItem("busnum")) {
bus_location = url_query.queryItemValue("busnum").toUInt();
device_num = url_query.queryItemValue("devnum").toUInt();
} else {
2011-04-22 18:50:29 +02:00
qLog(Warning) << "Invalid MTP device:" << hostname;
2010-08-14 18:39:45 +02:00
return;
}
2015-04-15 18:26:09 +02:00
if (url_query.hasQueryItem("vendor")) {
LIBMTP_raw_device_t* raw_device =
(LIBMTP_raw_device_t*)malloc(sizeof(LIBMTP_raw_device_t));
raw_device->device_entry.vendor =
2015-04-15 18:26:09 +02:00
url_query.queryItemValue("vendor").toLatin1().data();
raw_device->device_entry.product =
2015-04-15 18:26:09 +02:00
url_query.queryItemValue("product").toLatin1().data();
raw_device->device_entry.vendor_id =
2015-04-15 18:26:09 +02:00
url_query.queryItemValue("vendor_id").toUShort();
raw_device->device_entry.product_id =
2015-04-15 18:26:09 +02:00
url_query.queryItemValue("product_id").toUShort();
raw_device->device_entry.device_flags =
2015-04-15 18:26:09 +02:00
url_query.queryItemValue("quirks").toUInt();
raw_device->bus_location = bus_location;
raw_device->devnum = device_num;
device_ = LIBMTP_Open_Raw_Device(raw_device);
return;
}
2010-08-14 18:39:45 +02:00
// Get a list of devices from libmtp and figure out which one is ours
int count = 0;
LIBMTP_raw_device_t* raw_devices = nullptr;
2010-08-14 18:39:45 +02:00
LIBMTP_error_number_t err = LIBMTP_Detect_Raw_Devices(&raw_devices, &count);
if (err != LIBMTP_ERROR_NONE) {
2011-04-22 18:50:29 +02:00
qLog(Warning) << "MTP error:" << err;
2010-08-14 18:39:45 +02:00
return;
}
LIBMTP_raw_device_t* raw_device = nullptr;
for (int i = 0; i < count; ++i) {
2010-08-14 18:39:45 +02:00
if (raw_devices[i].bus_location == bus_location &&
raw_devices[i].devnum == device_num) {
raw_device = &raw_devices[i];
break;
}
}
if (!raw_device) {
2011-04-22 18:50:29 +02:00
qLog(Warning) << "MTP device not found";
2010-08-14 18:39:45 +02:00
free(raw_devices);
return;
}
// Connect to the device
device_ = LIBMTP_Open_Raw_Device(raw_device);
free(raw_devices);
}
MtpConnection::~MtpConnection() {
if (device_) LIBMTP_Release_Device(device_);
2010-08-14 18:39:45 +02:00
}