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

0001 /****************************************************************************************
0002  * Copyright (c) 2013 Konrad Zemek <konrad.zemek@gmail.com>                             *
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 "CreateProviderDialog.h"
0018 
0019 #include "statsyncing/Controller.h"
0020 #include "statsyncing/Provider.h"
0021 #include "core/support/Components.h"
0022 
0023 #include <KLocalizedString>
0024 
0025 #include <QLabel>
0026 #include <QVBoxLayout>
0027 #include <QRadioButton>
0028 #include <KConfigGroup>
0029 #include <QPushButton>
0030 
0031 namespace StatSyncing
0032 {
0033 
0034 CreateProviderDialog::CreateProviderDialog( QWidget *parent, Qt::WindowFlags f )
0035     : KAssistantDialog( parent, f )
0036 {
0037     setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Minimum );
0038     setWindowTitle( i18n( "Add Synchronization Target" ) );
0039     setModal( true );
0040     buttonBox()->button(QDialogButtonBox::Help)->setVisible(false);
0041 
0042     m_providerButtons.setExclusive( true );
0043     m_layout = new QVBoxLayout;
0044 
0045     QWidget *providerTypeWidget = new QWidget;
0046     QVBoxLayout *mainLayout = new QVBoxLayout;
0047 
0048     QLabel *warning = new QLabel( i18n( "<span style=\"color:red; font-weight:bold;\">"
0049                                   "Important:</span> before synchronizing tracks with a "
0050                                   "file-based target always make sure that "
0051                                   "the database file is not currently in use!" ) );
0052     warning->setWordWrap( true );
0053     mainLayout->addLayout( m_layout );
0054     mainLayout->addSpacing( 10 );
0055     mainLayout->addStretch();
0056     mainLayout->addWidget( warning );
0057 
0058     providerTypeWidget->setLayout( mainLayout );
0059 
0060     m_providerTypePage = new KPageWidgetItem( providerTypeWidget,
0061                                               i18n( "Choose Target Type" ) );
0062     providerTypeWidget->hide();
0063     addPage( m_providerTypePage );
0064 
0065     connect( this, &CreateProviderDialog::accepted, this, &CreateProviderDialog::slotAccepted );
0066 }
0067 
0068 CreateProviderDialog::~CreateProviderDialog()
0069 {
0070 }
0071 
0072 void
0073 CreateProviderDialog::addProviderType( const QString &id, const QString &prettyName,
0074                                        const QIcon &icon,
0075                                        ProviderConfigWidget *configWidget )
0076 {
0077     QRadioButton *providerTypeButton = new QRadioButton;
0078     providerTypeButton->setText( prettyName );
0079     providerTypeButton->setIcon( icon );
0080 
0081     m_providerButtons.addButton( providerTypeButton );
0082     m_idForButton.insert( providerTypeButton, id );
0083 
0084     m_layout->insertWidget( buttonInsertPosition( prettyName ), providerTypeButton );
0085 
0086     KPageWidgetItem *configPage =
0087             new KPageWidgetItem( configWidget, i18n( "Configure Target" ) );
0088     m_configForButton.insert( providerTypeButton, configPage );
0089     addPage( configPage );
0090     setAppropriate( configPage, false );
0091 
0092     connect( providerTypeButton, &QAbstractButton::toggled,
0093              this, &CreateProviderDialog::providerButtonToggled );
0094 
0095     if( !m_providerButtons.checkedButton() )
0096         providerTypeButton->setChecked( true );
0097 }
0098 
0099 int
0100 CreateProviderDialog::buttonInsertPosition( const QString &prettyName )
0101 {
0102     for( int i = 0; i < m_layout->count(); ++i )
0103     {
0104         const QRadioButton * const button =
0105                 dynamic_cast<const QRadioButton*>( m_layout->itemAt( i )->widget() );
0106 
0107         if( button != nullptr && prettyName.localeAwareCompare( button->text() ) <= 0 )
0108             return i;
0109     }
0110 
0111     // Nothing found, place the button at the end
0112     return -1;
0113 }
0114 
0115 void
0116 CreateProviderDialog::providerButtonToggled( bool checked )
0117 {
0118     KPageWidgetItem *configPage = m_configForButton[sender()];
0119     setAppropriate( configPage, checked );
0120 }
0121 
0122 void
0123 CreateProviderDialog::slotAccepted()
0124 {
0125     QAbstractButton *checkedButton = m_providerButtons.checkedButton();
0126     if( !checkedButton ) return;
0127 
0128     const QString id = m_idForButton[checkedButton];
0129     KPageWidgetItem *configPage = m_configForButton[checkedButton];
0130     const ProviderConfigWidget *configWidget =
0131             qobject_cast<ProviderConfigWidget*>( configPage->widget() );
0132 
0133     Q_EMIT providerConfigured( id, configWidget->config() );
0134 }
0135 
0136 } // namespace StatSyncing