2010-07-04 17:01:24 +02:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-07-04 17:01:24 +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/>.
|
|
|
|
*/
|
|
|
|
|
2014-02-06 14:48:00 +01:00
|
|
|
#include "deviceproperties.h"
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#include <QFutureWatcher>
|
|
|
|
#include <QScrollBar>
|
|
|
|
#include <QtConcurrentRun>
|
|
|
|
|
2010-08-29 15:49:40 +02:00
|
|
|
#include "connecteddevice.h"
|
2010-07-04 17:01:24 +02:00
|
|
|
#include "devicelister.h"
|
|
|
|
#include "devicemanager.h"
|
|
|
|
#include "ui_deviceproperties.h"
|
|
|
|
#include "core/utilities.h"
|
2010-08-29 17:32:36 +02:00
|
|
|
#include "transcoder/transcoder.h"
|
2010-07-04 17:01:24 +02:00
|
|
|
#include "ui/iconloader.h"
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
DeviceProperties::DeviceProperties(QWidget* parent)
|
|
|
|
: QDialog(parent),
|
|
|
|
ui_(new Ui_DeviceProperties),
|
|
|
|
manager_(nullptr),
|
|
|
|
updating_formats_(false) {
|
2010-07-04 17:01:24 +02:00
|
|
|
ui_->setupUi(this);
|
|
|
|
|
2010-08-29 15:49:40 +02:00
|
|
|
connect(ui_->open_device, SIGNAL(clicked()), SLOT(OpenDevice()));
|
|
|
|
|
2010-07-04 17:01:24 +02:00
|
|
|
// Maximum height of the icon widget
|
2014-02-07 16:34:20 +01:00
|
|
|
ui_->icon->setMaximumHeight(
|
|
|
|
ui_->icon->iconSize().height() +
|
|
|
|
ui_->icon->horizontalScrollBar()->sizeHint().height() +
|
|
|
|
ui_->icon->spacing() * 2 + 5);
|
2010-07-04 17:01:24 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
DeviceProperties::~DeviceProperties() { delete ui_; }
|
2010-07-04 17:01:24 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void DeviceProperties::SetDeviceManager(DeviceManager* manager) {
|
2010-07-04 17:01:24 +02:00
|
|
|
manager_ = manager;
|
2014-02-07 16:34:20 +01:00
|
|
|
connect(manager_, SIGNAL(dataChanged(QModelIndex, QModelIndex)),
|
|
|
|
SLOT(ModelChanged()));
|
|
|
|
connect(manager_, SIGNAL(rowsInserted(QModelIndex, int, int)),
|
|
|
|
SLOT(ModelChanged()));
|
|
|
|
connect(manager_, SIGNAL(rowsRemoved(QModelIndex, int, int)),
|
|
|
|
SLOT(ModelChanged()));
|
2010-07-04 17:01:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DeviceProperties::ShowDevice(int row) {
|
2010-07-04 17:18:37 +02:00
|
|
|
if (ui_->icon->count() == 0) {
|
2010-08-29 17:32:36 +02:00
|
|
|
// Only load the icons the first time the dialog is shown
|
2010-07-04 17:18:37 +02:00
|
|
|
QStringList icon_names = QStringList()
|
2014-02-07 16:34:20 +01:00
|
|
|
<< "drive-removable-media-usb-pendrive"
|
|
|
|
<< "multimedia-player-ipod-mini-blue"
|
|
|
|
<< "multimedia-player-ipod-mini-gold"
|
|
|
|
<< "multimedia-player-ipod-mini-green"
|
|
|
|
<< "multimedia-player-ipod-mini-pink"
|
|
|
|
<< "multimedia-player-ipod-mini-silver"
|
|
|
|
<< "multimedia-player-ipod-nano-black"
|
|
|
|
<< "multimedia-player-ipod-nano-white"
|
|
|
|
<< "multimedia-player-ipod-nano-green"
|
|
|
|
<< "multimedia-player-ipod-shuffle"
|
|
|
|
<< "multimedia-player-ipod-standard-color"
|
|
|
|
<< "multimedia-player-ipod-standard-monochrome"
|
|
|
|
<< "multimedia-player-ipod-U2-color"
|
|
|
|
<< "multimedia-player-ipod-U2-monochrome"
|
|
|
|
<< "ipodtouchicon"
|
|
|
|
<< "phone"
|
|
|
|
<< "phone-google-nexus-one"
|
|
|
|
<< "phone-htc-g1-white"
|
|
|
|
<< "phone-nokia-n900"
|
|
|
|
<< "phone-palm-pre";
|
|
|
|
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const QString& icon_name : icon_names) {
|
2014-02-07 16:34:20 +01:00
|
|
|
QListWidgetItem* item = new QListWidgetItem(IconLoader::Load(icon_name),
|
|
|
|
QString(), ui_->icon);
|
2010-07-04 17:18:37 +02:00
|
|
|
item->setData(Qt::UserRole, icon_name);
|
|
|
|
}
|
2010-08-29 17:32:36 +02:00
|
|
|
|
|
|
|
// Load the transcode formats the first time the dialog is shown
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const TranscoderPreset& preset : Transcoder::GetAllPresets()) {
|
2010-08-29 17:32:36 +02:00
|
|
|
ui_->transcode_format->addItem(preset.name_, preset.type_);
|
|
|
|
}
|
|
|
|
ui_->transcode_format->model()->sort(0);
|
2010-07-04 17:18:37 +02:00
|
|
|
}
|
|
|
|
|
2010-07-04 17:01:24 +02:00
|
|
|
index_ = manager_->index(row);
|
|
|
|
|
|
|
|
// Basic information
|
|
|
|
ui_->name->setText(index_.data(DeviceManager::Role_FriendlyName).toString());
|
2010-07-18 00:06:19 +02:00
|
|
|
|
2010-07-04 17:01:24 +02:00
|
|
|
// Find the right icon
|
|
|
|
QString icon_name = index_.data(DeviceManager::Role_IconName).toString();
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < ui_->icon->count(); ++i) {
|
2010-07-04 17:01:24 +02:00
|
|
|
if (ui_->icon->item(i)->data(Qt::UserRole).toString() == icon_name) {
|
|
|
|
ui_->icon->setCurrentRow(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateHardwareInfo();
|
2010-08-29 15:49:40 +02:00
|
|
|
UpdateFormats();
|
2010-07-04 17:01:24 +02:00
|
|
|
|
|
|
|
show();
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void DeviceProperties::AddHardwareInfo(int row, const QString& key,
|
|
|
|
const QString& value) {
|
2010-07-04 17:01:24 +02:00
|
|
|
ui_->hardware_info->setItem(row, 0, new QTableWidgetItem(key));
|
|
|
|
ui_->hardware_info->setItem(row, 1, new QTableWidgetItem(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeviceProperties::ModelChanged() {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!isVisible()) return;
|
2010-07-04 17:01:24 +02:00
|
|
|
|
|
|
|
if (!index_.isValid())
|
2014-02-07 16:34:20 +01:00
|
|
|
reject(); // Device went away
|
2010-08-29 15:49:40 +02:00
|
|
|
else {
|
2010-07-04 17:01:24 +02:00
|
|
|
UpdateHardwareInfo();
|
2010-08-29 15:49:40 +02:00
|
|
|
UpdateFormats();
|
|
|
|
}
|
2010-07-04 17:01:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DeviceProperties::UpdateHardwareInfo() {
|
|
|
|
// Hardware information
|
|
|
|
QString id = index_.data(DeviceManager::Role_UniqueId).toString();
|
|
|
|
if (DeviceLister* lister = manager_->GetLister(index_.row())) {
|
|
|
|
QVariantMap info = lister->DeviceHardwareInfo(id);
|
|
|
|
|
2010-08-10 20:45:34 +02:00
|
|
|
// Remove empty items
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const QString& key : info.keys()) {
|
2010-08-10 20:45:34 +02:00
|
|
|
if (info[key].isNull() || info[key].toString().isEmpty())
|
|
|
|
info.remove(key);
|
|
|
|
}
|
|
|
|
|
2010-07-04 17:01:24 +02:00
|
|
|
ui_->hardware_info_stack->setCurrentWidget(ui_->hardware_info_page);
|
|
|
|
ui_->hardware_info->clear();
|
|
|
|
ui_->hardware_info->setRowCount(2 + info.count());
|
|
|
|
|
|
|
|
int row = 0;
|
|
|
|
AddHardwareInfo(row++, tr("Model"), lister->DeviceModel(id));
|
|
|
|
AddHardwareInfo(row++, tr("Manufacturer"), lister->DeviceManufacturer(id));
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const QString& key : info.keys()) {
|
2010-07-04 17:01:24 +02:00
|
|
|
AddHardwareInfo(row++, tr(key.toAscii()), info[key].toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
ui_->hardware_info->sortItems(0);
|
|
|
|
} else {
|
2014-02-07 16:34:20 +01:00
|
|
|
ui_->hardware_info_stack->setCurrentWidget(
|
|
|
|
ui_->hardware_info_not_connected_page);
|
2010-07-04 17:01:24 +02:00
|
|
|
}
|
2010-07-29 22:03:24 +02:00
|
|
|
|
|
|
|
// Size
|
|
|
|
quint64 total = index_.data(DeviceManager::Role_Capacity).toLongLong();
|
|
|
|
|
|
|
|
QVariant free_var = index_.data(DeviceManager::Role_FreeSpace);
|
|
|
|
if (free_var.isValid()) {
|
|
|
|
quint64 free = free_var.toLongLong();
|
|
|
|
|
|
|
|
ui_->free_space_bar->set_total_bytes(total);
|
|
|
|
ui_->free_space_bar->set_free_bytes(free);
|
|
|
|
ui_->free_space_bar->show();
|
|
|
|
} else {
|
|
|
|
ui_->free_space_bar->hide();
|
|
|
|
}
|
2010-07-04 17:01:24 +02:00
|
|
|
}
|
2010-07-04 17:18:37 +02:00
|
|
|
|
2010-08-29 15:49:40 +02:00
|
|
|
void DeviceProperties::UpdateFormats() {
|
|
|
|
QString id = index_.data(DeviceManager::Role_UniqueId).toString();
|
|
|
|
DeviceLister* lister = manager_->GetLister(index_.row());
|
2014-02-06 14:48:00 +01:00
|
|
|
std::shared_ptr<ConnectedDevice> device =
|
2010-08-29 15:49:40 +02:00
|
|
|
manager_->GetConnectedDevice(index_.row());
|
|
|
|
|
2010-08-29 17:32:36 +02:00
|
|
|
// Transcode mode
|
2010-08-29 21:22:21 +02:00
|
|
|
MusicStorage::TranscodeMode mode = MusicStorage::TranscodeMode(
|
2010-08-29 17:32:36 +02:00
|
|
|
index_.data(DeviceManager::Role_TranscodeMode).toInt());
|
|
|
|
switch (mode) {
|
2010-08-29 21:22:21 +02:00
|
|
|
case MusicStorage::Transcode_Always:
|
2010-08-29 17:32:36 +02:00
|
|
|
ui_->transcode_all->setChecked(true);
|
|
|
|
break;
|
|
|
|
|
2010-08-29 21:22:21 +02:00
|
|
|
case MusicStorage::Transcode_Never:
|
2010-08-29 17:32:36 +02:00
|
|
|
ui_->transcode_off->setChecked(true);
|
|
|
|
break;
|
|
|
|
|
2010-08-29 21:22:21 +02:00
|
|
|
case MusicStorage::Transcode_Unsupported:
|
2010-08-29 17:32:36 +02:00
|
|
|
default:
|
|
|
|
ui_->transcode_unsupported->setChecked(true);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-08-29 15:49:40 +02:00
|
|
|
// If there's no lister then the device is physically disconnected
|
|
|
|
if (!lister) {
|
|
|
|
ui_->formats_stack->setCurrentWidget(ui_->formats_page_not_connected);
|
|
|
|
ui_->open_device->setEnabled(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there's a lister but no device then the user just needs to open the
|
|
|
|
// device. This will cause a rescan so we don't do it automatically.
|
|
|
|
if (!device) {
|
|
|
|
ui_->formats_stack->setCurrentWidget(ui_->formats_page_not_connected);
|
|
|
|
ui_->open_device->setEnabled(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!updating_formats_) {
|
|
|
|
// Get the device's supported formats list. This takes a long time and it
|
|
|
|
// blocks, so do it in the background.
|
2010-08-30 14:22:15 +02:00
|
|
|
supported_formats_.clear();
|
|
|
|
|
2014-02-06 14:48:00 +01:00
|
|
|
QFuture<bool> future = QtConcurrent::run(std::bind(
|
2010-08-30 14:22:15 +02:00
|
|
|
&ConnectedDevice::GetSupportedFiletypes, device, &supported_formats_));
|
|
|
|
QFutureWatcher<bool>* watcher = new QFutureWatcher<bool>(this);
|
2010-08-29 15:49:40 +02:00
|
|
|
watcher->setFuture(future);
|
|
|
|
|
|
|
|
connect(watcher, SIGNAL(finished()), SLOT(UpdateFormatsFinished()));
|
|
|
|
|
|
|
|
ui_->formats_stack->setCurrentWidget(ui_->formats_page_loading);
|
|
|
|
updating_formats_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-04 17:18:37 +02:00
|
|
|
void DeviceProperties::accept() {
|
|
|
|
QDialog::accept();
|
|
|
|
|
2010-08-29 17:32:36 +02:00
|
|
|
// Transcode mode
|
2010-08-29 21:22:21 +02:00
|
|
|
MusicStorage::TranscodeMode mode = MusicStorage::Transcode_Unsupported;
|
2010-08-29 17:32:36 +02:00
|
|
|
if (ui_->transcode_all->isChecked())
|
2010-08-29 21:22:21 +02:00
|
|
|
mode = MusicStorage::Transcode_Always;
|
2010-08-29 17:32:36 +02:00
|
|
|
else if (ui_->transcode_off->isChecked())
|
2010-08-29 21:22:21 +02:00
|
|
|
mode = MusicStorage::Transcode_Never;
|
2010-08-29 17:32:36 +02:00
|
|
|
else if (ui_->transcode_unsupported->isChecked())
|
2010-08-29 21:22:21 +02:00
|
|
|
mode = MusicStorage::Transcode_Unsupported;
|
2010-08-29 17:32:36 +02:00
|
|
|
|
|
|
|
// Transcode format
|
2014-02-07 16:34:20 +01:00
|
|
|
Song::FileType format = Song::FileType(
|
|
|
|
ui_->transcode_format->itemData(ui_->transcode_format->currentIndex())
|
|
|
|
.toInt());
|
2010-08-29 17:32:36 +02:00
|
|
|
|
2014-11-04 21:46:10 +01:00
|
|
|
// By default no icon is selected and thus no current item is selected
|
2014-11-05 11:51:38 +01:00
|
|
|
QString icon_name;
|
|
|
|
if (ui_->icon->currentItem() != nullptr) {
|
2014-11-04 21:46:10 +01:00
|
|
|
icon_name = ui_->icon->currentItem()->data(Qt::UserRole).toString();
|
|
|
|
}
|
|
|
|
|
2014-11-05 11:51:38 +01:00
|
|
|
manager_->SetDeviceOptions(index_.row(), ui_->name->text(), icon_name, mode,
|
|
|
|
format);
|
2010-07-04 17:18:37 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void DeviceProperties::OpenDevice() { manager_->Connect(index_.row()); }
|
2010-08-29 15:49:40 +02:00
|
|
|
|
|
|
|
void DeviceProperties::UpdateFormatsFinished() {
|
2010-08-30 14:22:15 +02:00
|
|
|
QFutureWatcher<bool>* watcher = static_cast<QFutureWatcher<bool>*>(sender());
|
2010-08-29 15:49:40 +02:00
|
|
|
watcher->deleteLater();
|
|
|
|
updating_formats_ = false;
|
|
|
|
|
2010-08-30 14:22:15 +02:00
|
|
|
if (!watcher->future().result()) {
|
|
|
|
supported_formats_.clear();
|
|
|
|
}
|
2010-08-29 15:49:40 +02:00
|
|
|
|
|
|
|
// Hide widgets if there are no supported types
|
2010-08-30 14:22:15 +02:00
|
|
|
ui_->supported_formats_container->setVisible(!supported_formats_.isEmpty());
|
|
|
|
ui_->transcode_unsupported->setEnabled(!supported_formats_.isEmpty());
|
2010-08-29 15:49:40 +02:00
|
|
|
|
2010-08-30 14:22:15 +02:00
|
|
|
if (ui_->transcode_unsupported->isChecked() && supported_formats_.isEmpty()) {
|
2010-08-29 17:32:36 +02:00
|
|
|
ui_->transcode_off->setChecked(true);
|
|
|
|
}
|
|
|
|
|
2010-08-29 15:49:40 +02:00
|
|
|
// Populate supported types list
|
|
|
|
ui_->supported_formats->clear();
|
2014-02-10 14:29:07 +01:00
|
|
|
for (Song::FileType type : supported_formats_) {
|
2010-08-29 15:49:40 +02:00
|
|
|
QListWidgetItem* item = new QListWidgetItem(Song::TextForFiletype(type));
|
|
|
|
ui_->supported_formats->addItem(item);
|
|
|
|
}
|
2010-08-29 18:12:55 +02:00
|
|
|
ui_->supported_formats->sortItems();
|
|
|
|
|
|
|
|
// Set the format combobox item
|
2014-02-07 16:34:20 +01:00
|
|
|
TranscoderPreset preset = Transcoder::PresetForFileType(
|
|
|
|
Song::FileType(index_.data(DeviceManager::Role_TranscodeFormat).toInt()));
|
2010-08-29 18:12:55 +02:00
|
|
|
if (preset.type_ == Song::Type_Unknown) {
|
|
|
|
// The user hasn't chosen a format for this device yet, so work our way down
|
|
|
|
// a list of some preferred formats, picking the first one that is supported
|
2014-02-07 16:34:20 +01:00
|
|
|
preset = Transcoder::PresetForFileType(
|
|
|
|
Transcoder::PickBestFormat(supported_formats_));
|
2010-08-29 18:12:55 +02:00
|
|
|
}
|
2014-02-07 16:34:20 +01:00
|
|
|
ui_->transcode_format->setCurrentIndex(
|
|
|
|
ui_->transcode_format->findText(preset.name_));
|
2010-08-29 15:49:40 +02:00
|
|
|
|
|
|
|
ui_->formats_stack->setCurrentWidget(ui_->formats_page);
|
|
|
|
}
|