File indexing completed on 2024-05-19 04:50:11

0001 /****************************************************************************************
0002  * Copyright (c) 2007 Nikolaj Hald Nielsen <nhn@kde.org>                                *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #include "AmpacheSettings.h"
0018 
0019 #include "AddServerDialog.h"
0020 #include "ui_AmpacheConfigWidget.h"
0021 
0022 #include <QTableWidget>
0023 #include <QHeaderView>
0024 #include <QDebug>
0025 
0026 #include <KPluginFactory>
0027 
0028 K_PLUGIN_FACTORY_WITH_JSON( ampachesettings, "amarok_service_ampache_config.json", registerPlugin<AmpacheSettings>(); )
0029 
0030 AmpacheSettings::AmpacheSettings( QWidget *parent, const QVariantList &args )
0031     : KCModule( parent, args )
0032     , m_configDialog(new Ui::AmpacheConfigWidget)
0033     , m_lastRowEdited(-1)
0034     , m_lastColumnEdited(-1)
0035 {
0036     m_configDialog->setupUi( this );
0037     m_configDialog->serverList->setMinimumWidth(700);
0038     m_configDialog->serverList->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
0039     m_configDialog->serverList->verticalHeader()->hide();
0040 
0041     connect ( m_configDialog->serverList, &QTableWidget::cellDoubleClicked, this, &AmpacheSettings::onCellDoubleClicked );
0042     connect ( m_configDialog->serverList, &QTableWidget::cellChanged, this, &AmpacheSettings::saveCellEdit );
0043     connect ( m_configDialog->addButton, &QPushButton::clicked, this, &AmpacheSettings::add );
0044     connect ( m_configDialog->removeButton, &QPushButton::clicked, this, &AmpacheSettings::remove );
0045 }
0046 
0047 AmpacheSettings::~AmpacheSettings()
0048 {
0049     delete m_configDialog;
0050 }
0051 
0052 void
0053 AmpacheSettings::serverNameChanged(const QString & text)
0054 {
0055    m_configDialog->addButton->setEnabled( !text.isEmpty() );
0056 }
0057 
0058 void
0059 AmpacheSettings::save()
0060 {
0061     m_config.save();
0062     KCModule::save();
0063 }
0064 
0065 void
0066 AmpacheSettings::load()
0067 {
0068     loadList();
0069     KCModule::load();
0070 }
0071 
0072 void
0073 AmpacheSettings::loadList()
0074 {
0075     QTableWidget* serverList = m_configDialog->serverList;
0076     serverList->setRowCount(m_config.servers().size());
0077     for( int i = 0; i < m_config.servers().size(); i++ )
0078     {
0079         AmpacheServerEntry entry = m_config.servers().at( i );
0080 
0081         serverList->setItem(i, 0, new QTableWidgetItem(entry.name));
0082         serverList->setItem(i, 1, new QTableWidgetItem(entry.url.url()));
0083         serverList->setItem(i, 2, new QTableWidgetItem(entry.username));
0084         QString starPassword = entry.password;
0085         starPassword.fill('*');
0086         QTableWidgetItem* password = new QTableWidgetItem(starPassword);
0087         password->setData(0xf00, entry.password);
0088         serverList->setItem(i, 3, password);
0089     }
0090     serverList->resizeColumnsToContents();
0091     int columnWidth = serverList->columnWidth(3) + serverList->columnViewportPosition(3);
0092     serverList->setMinimumWidth( qBound( 200, columnWidth, 700) );
0093 
0094 }
0095 
0096 void
0097 AmpacheSettings::defaults()
0098 {
0099 }
0100 
0101 void
0102 AmpacheSettings::add()
0103 {
0104     AddServerDialog dialog;
0105     if(dialog.exec() == QDialog::Accepted)
0106     {
0107         AmpacheServerEntry server;
0108         server.name = dialog.name();
0109         server.url = dialog.url();
0110         server.username = dialog.username();
0111         server.password = dialog.password();
0112         server.addToCollection = false;
0113         if( server.name.isEmpty())
0114             return;
0115         m_config.addServer( server );
0116     }
0117     loadList();
0118     Q_EMIT changed( true );
0119 }
0120 
0121 void
0122 AmpacheSettings::remove()
0123 {
0124     int index = m_configDialog->serverList->currentRow();
0125     m_configDialog->serverList->removeRow( index );
0126     m_config.removeServer( index );
0127 
0128     Q_EMIT changed( true );
0129 }
0130 
0131 void
0132 AmpacheSettings::onCellDoubleClicked(int row, int column)
0133 {
0134     QTableWidgetItem* item = m_configDialog->serverList->item(row, column);
0135     m_configDialog->serverList->editItem(item);
0136     m_lastRowEdited = row;
0137     m_lastColumnEdited = column;
0138 }
0139 
0140 void
0141 AmpacheSettings::saveCellEdit(int row, int column)
0142 {
0143     if(m_lastRowEdited != row || m_lastColumnEdited != column) //only worry about user edits
0144         return;
0145 //     kDebug( 14310 ) << Q_FUNC_INFO << row << column;
0146     QString newValue = m_configDialog->serverList->item(row, column)->text();
0147     AmpacheServerEntry server = m_config.servers().at(row);
0148     switch(column)
0149     {
0150         case 0:
0151             server.name = newValue;
0152             break;
0153         case 1:
0154             server.url = QUrl( newValue );
0155             break;
0156         case 2:
0157             server.username = newValue;
0158             break;
0159         case 3:
0160             server.password = newValue;
0161             break;
0162         default:
0163             qWarning() << Q_FUNC_INFO << "invalid column";
0164         
0165     }
0166     m_config.updateServer(row, server);
0167     m_configDialog->serverList->resizeColumnToContents(column);
0168     Q_EMIT changed( true );
0169 }
0170 
0171 
0172 #include "moc_AmpacheSettings.cpp"
0173 #include "AmpacheSettings.moc"
0174