Multiple server support fixes

This commit is contained in:
craig.p.drummond
2012-06-08 17:54:59 +00:00
committed by craig.p.drummond
parent 8c9eb631f4
commit b99ec2b073
6 changed files with 75 additions and 50 deletions

View File

@@ -1094,6 +1094,7 @@ MainWindow::MainWindow(QWidget *parent)
connect(messageWidget, SIGNAL(visible(bool)), this, SLOT(messageWidgetVisibility(bool)));
readSettings();
updateConnectionsMenu();
fadeStop=Settings::self()->stopFadeDuration()>Settings::MinFade;
playlistsPage->refresh();
#ifndef Q_WS_WIN
@@ -1354,7 +1355,7 @@ void MainWindow::connectToMpd(const MPDConnectionDetails &details)
{
messageWidget->hide();
if (details!=MPDConnection::self()->getDetails()) {
if (!MPDConnection::self()->isConnected() || details!=MPDConnection::self()->getDetails()) {
libraryPage->clear();
albumsPage->clear();
folderPage->clear();
@@ -1365,11 +1366,10 @@ void MainWindow::connectToMpd(const MPDConnectionDetails &details)
#ifndef Q_WS_WIN
Dynamic::self()->stop();
#endif
showInformation(i18n("Connecting to %1").arg(details.description()));
outputsAction->setVisible(false);
emit setDetails(details);
}
showInformation(i18n("Connecting to %1").arg(details.description()));
outputsAction->setVisible(false);
emit setDetails(details);
}
void MainWindow::connectToMpd()
@@ -1411,6 +1411,7 @@ void MainWindow::showPreferencesDialog()
connect(&pref, SIGNAL(connectTo(const MPDConnectionDetails &)), this, SLOT(connectToMpd(const MPDConnectionDetails &)));
pref.exec();
updateConnectionsMenu();
showing=false;
}
}
@@ -1488,6 +1489,39 @@ void MainWindow::outputsUpdated(const QList<Output> &outputs)
}
}
void MainWindow::updateConnectionsMenu()
{
QList<MPDConnectionDetails> connections=Settings::self()->allConnections();
if (connections.count()<2) {
connectionsAction->setVisible(false);
} else {
connectionsAction->setVisible(true);
QSet<QString> cfg;
QSet<QString> menuItems;
QMenu *menu=connectionsAction->menu();
foreach (const MPDConnectionDetails &d, connections) {
cfg.insert(d.name);
}
foreach (QAction *act, menu->actions()) {
menuItems.insert(act->data().toString());
}
if (menuItems!=cfg) {
menu->clear();
qSort(connections);
QString current=Settings::self()->currentConnection();
foreach (const MPDConnectionDetails &d, connections) {
QAction *act=menu->addAction(d.name.isEmpty() ? i18n("Default") : d.name, this, SLOT(changeConnection()));
act->setData(d.name);
act->setCheckable(true);
act->setChecked(d.name==current);
act->setActionGroup(connectionsGroup);
}
}
}
}
void MainWindow::readSettings()
{
checkMpdDir();
@@ -1522,36 +1556,6 @@ void MainWindow::readSettings()
toggleMpris();
#endif
autoScrollPlayQueue=Settings::self()->playQueueScroll();
QList<MPDConnectionDetails> connections=Settings::self()->allConnections();
if (connections.count()<2) {
connectionsAction->setVisible(false);
} else {
connectionsAction->setVisible(true);
QSet<QString> cfg;
QSet<QString> menuItems;
QMenu *menu=connectionsAction->menu();
foreach (const MPDConnectionDetails &d, connections) {
cfg.insert(d.name);
}
foreach (QAction *act, menu->actions()) {
menuItems.insert(act->data().toString());
}
if (menuItems!=cfg) {
menu->clear();
qSort(connections);
QString current=Settings::self()->currentConnection();
foreach (const MPDConnectionDetails &d, connections) {
QAction *act=menu->addAction(d.name.isEmpty() ? i18n("Default") : d.name, this, SLOT(changeConnection()));
act->setData(d.name);
act->setCheckable(true);
act->setChecked(d.name==current);
act->setActionGroup(connectionsGroup);
}
}
}
}
void MainWindow::updateSettings()

View File

@@ -334,6 +334,7 @@ private Q_SLOTS:
void outputsUpdated(const QList<Output> &outputs);
private:
void updateConnectionsMenu();
void readSettings();
int calcMinHeight();
void addToPlayQueue(bool replace);

View File

@@ -55,6 +55,7 @@ void ServerSettings::load()
QString currentCon=Settings::self()->currentConnection();
MPDConnectionDetails current=MPDConnection::self()->getDetails();
qSort(all);
combo->clear();
int idx=0;
int cur=0;
@@ -76,6 +77,7 @@ void ServerSettings::save()
void ServerSettings::mpdConnectionStateChanged(bool c)
{
enableWidgets(!c || MPDConnection::self()->getDetails()!=getDetails());
connectButton->setEnabled(false);
}
void ServerSettings::showDetails(int index)
@@ -87,13 +89,14 @@ void ServerSettings::showDetails(int index)
void ServerSettings::toggleConnection()
{
bool con=hostLabel->isEnabled();
bool con=removeButton->isEnabled();
enableWidgets(!con);
if (con) {
emit connectTo(getDetails());
} else {
emit disconnectFromMpd();
}
connectButton->setEnabled(false);
}
void ServerSettings::saveAs()
@@ -129,7 +132,9 @@ void ServerSettings::saveAs()
}
MPDConnectionDetails details=getDetails();
details.name=name;
details.name=name==combo->itemText(0) && combo->itemData(0).toString().isEmpty() ? QString() : name;
bool needToReconnect=MPDConnection::self()->isConnected() && MPDConnection::self()->getDetails()==Settings::self()->connectionDetails(details.name);
Settings::self()->saveConnectionDetails(details);
if (found) {
if (idx!=currentIndex) {
@@ -137,6 +142,12 @@ void ServerSettings::saveAs()
}
} else {
combo->addItem(details.name, details.name);
combo->setCurrentIndex(combo->count()-1);
}
if (needToReconnect) {
emit disconnectFromMpd();
emit connectTo(details);
}
break;
}
@@ -147,25 +158,28 @@ void ServerSettings::remove()
int index=combo->currentIndex();
QString name=combo->itemData(index).toString();
if (combo->count()>1 && MessageBox::Yes==MessageBox::questionYesNo(this, i18n("Delete %1?").arg(name))) {
bool isLast=index==(combo->count()-1);
Settings::self()->removeConnectionDetails(combo->itemText(index));
combo->removeItem(index);
combo->setCurrentIndex(isLast ? index-1 : index);
showDetails(combo->currentIndex());
}
}
void ServerSettings::enableWidgets(bool e)
{
host->setEnabled(e);
port->setEnabled(e);
password->setEnabled(e);
dir->setEnabled(e);
hostLabel->setEnabled(e);
portLabel->setEnabled(e);
passwordLabel->setEnabled(e);
dirLabel->setEnabled(e);
// host->setEnabled(e);
// port->setEnabled(e);
// password->setEnabled(e);
// dir->setEnabled(e);
// hostLabel->setEnabled(e);
// portLabel->setEnabled(e);
// passwordLabel->setEnabled(e);
// dirLabel->setEnabled(e);
connectButton->setText(e ? i18n("Connect") : i18n("Disconnect"));
connectButton->setIcon(QIcon::fromTheme(e ? "network-connect" : "network-disconnect"));
removeButton->setEnabled(e);
saveButton->setEnabled(e);
// saveButton->setEnabled(e);
}
void ServerSettings::setDetails(const MPDConnectionDetails &details)

View File

@@ -53,6 +53,9 @@ private:
void setDetails(const MPDConnectionDetails &details);
MPDConnectionDetails getDetails() const;
void enableWidgets(bool e);
private:
bool isCurrentConnection;
};
#endif

View File

@@ -120,7 +120,7 @@ static MpdDefaults mpdDefaults;
#define CFG_GET_SIZE(CFG, KEY) (CFG.readEntry(KEY, QSize()))
#define CFG_SET_VALUE(CFG, KEY, V) (CFG.writeEntry(KEY, V))
#define HAS_GROUP(GRP) (KGlobal::config()->hasGroup(GRP))
#define REMOVE_GROUP(KEY) (cfg.deleteGroup(KEY))
#define REMOVE_GROUP(GRP) (KGlobal::config()->deleteGroup(GRP))
#define REMOVE_ENTRY(KEY) (cfg.deleteEntry(KEY))
#define GET_STRING(KEY, DEF) CFG_GET_STRING(cfg, KEY, DEF)
#define GET_STRINGLIST(KEY, DEF) CFG_GET_STRINGLIST(cfg, KEY, DEF)
@@ -138,7 +138,7 @@ static MpdDefaults mpdDefaults;
#define GET_SIZE(KEY) (cfg.contains(KEY) ? cfg.value(KEY).toSize() : QSize())
#define SET_VALUE(KEY, V) (cfg.setValue(KEY, V))
#define HAS_GROUP(GRP) (cfg.contains(GRP))
#define REMOVE_GROUP(KEY) (cfg.remove(KEY))
#define REMOVE_GROUP(GRP) (cfg.remove(GRP))
#define REMOVE_ENTRY(KEY) (cfg.remove(KEY))
#endif
@@ -233,7 +233,7 @@ QList<MPDConnectionDetails> Settings::allConnections()
QList<MPDConnectionDetails> connections;
foreach (const QString &grp, groups) {
if (grp.startsWith("Connection")) {
if (HAS_GROUP(grp) && grp.startsWith("Connection")) {
connections.append(connectionDetails(grp=="Connection" ? QString() : grp.mid(11)));
}
}
@@ -557,6 +557,9 @@ QString Settings::streamUrl()
void Settings::removeConnectionDetails(const QString &v)
{
REMOVE_GROUP(connGroupName(v));
#ifdef ENABLE_KDE_SUPPORT
KGlobal::config()->sync();
#endif
}
void Settings::saveConnectionDetails(const MPDConnectionDetails &v)

View File

@@ -316,7 +316,7 @@ void MPDConnection::disconnectFromMPD()
void MPDConnection::setDetails(const MPDConnectionDetails &det)
{
bool changedDir=det.dir!=details.dir;
if (details!=det) {
if (details!=det || State_Connected!=state) {
DBUG << "setDetails" << det.hostname << det.port << (det.password.isEmpty() ? false : true);
bool wasConnected=State_Connected==state;
disconnectFromMPD();