Files
cantata/devices/remotedevicepropertieswidget.cpp
2012-05-02 18:38:04 +00:00

141 lines
5.5 KiB
C++

/*
* Cantata
*
* Copyright (c) 2011-2012 Craig Drummond <craig.p.drummond@gmail.com>
*
* ----
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "remotedevicepropertieswidget.h"
#include "filenameschemedialog.h"
#include "covers.h"
#include <KDE/KGlobal>
#include <KDE/KLocale>
#include <KDE/KMessageBox>
#include <KDE/KDirSelectDialog>
#include <QtGui/QTabWidget>
#include <QtGui/QIcon>
#include "lineedit.h"
RemoteDevicePropertiesWidget::RemoteDevicePropertiesWidget(QWidget *parent)
: QWidget(parent)
, modified(false)
, saveable(false)
{
setupUi(this);
if (qobject_cast<QTabWidget *>(parent)) {
verticalLayout->setMargin(4);
}
type->addItem(i18n("Secure Shell (sshfs)"), (int)RemoteDevice::Prot_Sshfs);
type->addItem(i18n("Locally Mounted Folder"), (int)RemoteDevice::Prot_File);
folderButton->setIcon(QIcon::fromTheme("document-open"));
}
void RemoteDevicePropertiesWidget::update(const RemoteDevice::Details &d, bool create, bool isConnected)
{
setEnabled(!isConnected);
infoLabel->setVisible(create);
orig=d;
name->setText(d.name);
host->setText(d.host);
user->setText(d.user);
folder->setText(d.folder);
port->setValue(d.port);
for (int i=1; i<type->count(); ++i) {
if (type->itemData(i).toInt()==d.protocol) {
type->setCurrentIndex(i);
break;
}
}
name->setEnabled(d.protocol==RemoteDevice::Prot_File || !isConnected);
connect(name, SIGNAL(textChanged(const QString &)), this, SLOT(checkSaveable()));
connect(host, SIGNAL(textChanged(const QString &)), this, SLOT(checkSaveable()));
connect(user, SIGNAL(textChanged(const QString &)), this, SLOT(checkSaveable()));
connect(folder, SIGNAL(textChanged(const QString &)), this, SLOT(checkSaveable()));
connect(port, SIGNAL(valueChanged(int)), this, SLOT(checkSaveable()));
connect(type, SIGNAL(currentIndexChanged(int)), this, SLOT(setType()));
connect(folderButton, SIGNAL(clicked()), this, SLOT(browseSftpFolder()));
modified=false;
setType();
checkSaveable();
}
void RemoteDevicePropertiesWidget::setType()
{
int t=type->itemData(type->currentIndex()).toInt();
hostLabel->setEnabled(RemoteDevice::Prot_File!=t);
host->setEnabled(RemoteDevice::Prot_File!=t);
userLabel->setEnabled(RemoteDevice::Prot_File!=t);
user->setEnabled(RemoteDevice::Prot_File!=t);
portLabel->setEnabled(RemoteDevice::Prot_File!=t);
port->setEnabled(RemoteDevice::Prot_File!=t);
folder->setButtonVisible(RemoteDevice::Prot_File==t);
folder->lineEdit()->setCompletionModeDisabled(KGlobalSettings::CompletionAuto, RemoteDevice::Prot_File!=t);
folder->lineEdit()->setCompletionModeDisabled(KGlobalSettings::CompletionMan, RemoteDevice::Prot_File!=t);
folder->lineEdit()->setCompletionModeDisabled(KGlobalSettings::CompletionShell, RemoteDevice::Prot_File!=t);
folder->lineEdit()->setCompletionModeDisabled(KGlobalSettings::CompletionPopup, RemoteDevice::Prot_File!=t);
folder->lineEdit()->setCompletionModeDisabled(KGlobalSettings::CompletionPopupAuto, RemoteDevice::Prot_File!=t);
folder->lineEdit()->setCompletionMode(RemoteDevice::Prot_File==t ? KGlobalSettings::completionMode() : KGlobalSettings::CompletionNone);
if (RemoteDevice::Prot_Sshfs==t && 0==port->value()) {
port->setValue(22);
}
folderButton->setVisible(RemoteDevice::Prot_Sshfs==t);
}
void RemoteDevicePropertiesWidget::browseSftpFolder()
{
RemoteDevice::Details det=details();
KUrl url=KDirSelectDialog::selectDirectory(KUrl("sftp://"+ (det.user.isEmpty() ? QString() : (det.user+QChar('@')))
+ det.host + (det.port<=0 ? QString() : QString(QChar(':')+QString::number(det.port)))
+ (det.folder.startsWith("/") ? det.folder : (det.folder.isEmpty() ? QString("/") : det.folder))),
false, this, i18n("Select Music Folder"));
if (url.isValid() && QLatin1String("sftp")==url.protocol()) {
host->setText(url.host());
user->setText(url.user());
port->setValue(url.port());
folder->setText(url.path());
}
}
void RemoteDevicePropertiesWidget::checkSaveable()
{
RemoteDevice::Details det=details();
modified=det!=orig;
saveable=!det.isEmpty();
folderButton->setEnabled(!det.host.isEmpty() && det.port>0);
emit updated();
}
RemoteDevice::Details RemoteDevicePropertiesWidget::details()
{
RemoteDevice::Details det;
det.name=name->text().trimmed();
det.host=host->text().trimmed();
det.user=user->text().trimmed();
det.folder=folder->text().trimmed();
det.port=port->value();
det.protocol=(RemoteDevice::Protocol)type->itemData(type->currentIndex()).toInt();
return det;
}