1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-03-20 07:49:42 +01:00

Refactor account dialogs into utils, and port tomahawk to automoc too

This commit is contained in:
Leo Franchi 2012-03-08 18:53:59 -05:00
parent f3324ce167
commit 6cbc8a9539
13 changed files with 357 additions and 305 deletions

View File

@ -19,7 +19,8 @@
#include "AccountFactoryWrapper.h"
#include "accounts/Account.h"
#include <accounts/AccountManager.h>
#include "accounts/AccountManager.h"
#include "guihelpers.h"
#include "AccountFactoryWrapperDelegate.h"
#include "delegateconfigwrapper.h"
#include "ui_AccountFactoryWrapper.h"
@ -92,40 +93,10 @@ AccountFactoryWrapper::load()
void
AccountFactoryWrapper::openAccountConfig( Account* account )
{
if( account->configurationWidget() )
{
#ifndef Q_WS_MAC
DelegateConfigWrapper dialog( account->configurationWidget(), QString("%1 Configuration" ).arg( account->accountFriendlyName() ), this );
QWeakPointer< DelegateConfigWrapper > watcher( &dialog );
int ret = dialog.exec();
if( !watcher.isNull() && ret == QDialog::Accepted )
{
// send changed config to resolver
account->saveConfig();
}
#else
// on osx a sheet needs to be non-modal
DelegateConfigWrapper* dialog = new DelegateConfigWrapper( account->configurationWidget(), QString("%1 Configuration" ).arg( account->accountFriendlyName() ), this, Qt::Sheet );
dialog->setProperty( "accountplugin", QVariant::fromValue< QObject* >( account ) );
connect( dialog, SIGNAL( finished( int ) ), this, SLOT( accountConfigClosed( int ) ) );
dialog->show();
#endif
}
TomahawkUtils::openAccountConfig( account, this, false );
}
void
AccountFactoryWrapper::accountConfigClosed( int value )
{
if( value == QDialog::Accepted )
{
DelegateConfigWrapper* dialog = qobject_cast< DelegateConfigWrapper* >( sender() );
Account* account = qobject_cast< Account* >( dialog->property( "accountplugin" ).value< QObject* >() );
account->saveConfig();
}
}
void
AccountFactoryWrapper::removeAccount( Tomahawk::Accounts::Account* acct )
{

View File

@ -49,7 +49,6 @@ signals:
public slots:
void openAccountConfig( Tomahawk::Accounts::Account* );
void accountConfigClosed( int value );
void removeAccount( Tomahawk::Accounts::Account* );
private slots:

View File

@ -70,63 +70,21 @@ SET( tomahawkSourcesGui ${tomahawkSourcesGui}
breakpad/BreakPad.cpp
utils/guihelpers.cpp
tomahawktrayicon.cpp
audiocontrols.cpp
settingsdialog.cpp
diagnosticsdialog.cpp
AccountDelegate.cpp
settingslistdelegate.cpp
delegateconfigwrapper.cpp
tomahawkwindow.cpp
LoadXSPFDialog.cpp
AccountFactoryWrapper.cpp
AccountFactoryWrapperDelegate.cpp
)
SET( tomahawkHeaders ${tomahawkHeaders}
tomahawkapp.h
web/api_v1.h
musicscanner.h
scanmanager.h
ubuntuunityhack.h
shortcuthandler.h
)
IF(LIBLASTFM_FOUND)
SET(tomahawkHeaders ${tomahawkHeaders}
scrobbler.h
)
ENDIF(LIBLASTFM_FOUND)
SET( tomahawkHeadersGui ${tomahawkHeadersGui}
sourcetree/sourcesmodel.h
sourcetree/sourcesproxymodel.h
sourcetree/sourcetreeview.h
sourcetree/sourcedelegate.h
sourcetree/animationhelper.h
sourcetree/items/sourcetreeitem.h
sourcetree/items/sourceitem.h
sourcetree/items/playlistitems.h
sourcetree/items/categoryitems.h
sourcetree/items/genericpageitems.h
sourcetree/items/temporarypageitem.h
sourcetree/items/groupitem.h
sourcetree/items/historyitem.h
tomahawktrayicon.h
audiocontrols.h
settingsdialog.h
diagnosticsdialog.h
AccountDelegate.h
settingslistdelegate.h
delegateconfigwrapper.h
tomahawkwindow.h
LoadXSPFDialog.h
AccountFactoryWrapper.h
AccountFactoryWrapperDelegate.h
)
SET( tomahawkUI ${tomahawkUI}
tomahawkwindow.ui
diagnosticsdialog.ui
@ -180,17 +138,11 @@ ENDIF( UNIX )
IF( APPLE )
INCLUDE_DIRECTORIES( ${CMAKE_SOURCE_DIR}/thirdparty/SPMediaKeyTap )
SET( tomahawkHeaders ${tomahawkHeaders} mac/tomahawkapp_mac.h mac/macshortcuthandler.h )
SET( tomahawkSources ${tomahawkSources} mac/tomahawkapp_mac.mm mac/macshortcuthandler.cpp )
IF(HAVE_SPARKLE)
SET( tomahawkHeaders ${tomahawkHeaders} ${SPARKLE}/Headers )
ENDIF(HAVE_SPARKLE)
ENDIF( APPLE )
IF(GLOOX_FOUND)
INCLUDE_DIRECTORIES( ${GLOOX_INCLUDE_DIR} )
SET( tomahawkHeaders ${tomahawkHeaders} xmppbot/xmppbot.h )
SET( tomahawkSources ${tomahawkSources} xmppbot/xmppbot.cpp )
ENDIF(GLOOX_FOUND)
@ -206,26 +158,26 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config.h.in
include( ${CMAKE_SOURCE_DIR}/lang/translations.cmake )
SET( final_src ${final_src} ${tomahawkMoc} ${tomahawkSources} ${tomahawkHeaders} ${trans_outfile})
SET( final_src ${final_src} ${tomahawkMoc} ${tomahawkSources} ${trans_outfile})
IF( BUILD_GUI )
LIST(APPEND tomahawkHeaders ${tomahawkHeadersGui})
LIST(APPEND tomahawkSources ${tomahawkSourcesGui})
qt4_wrap_ui( tomahawkUI_H ${tomahawkUI} )
ENDIF()
kde4_add_app_icon( tomahawkSources "${CMAKE_SOURCE_DIR}/data/icons/tomahawk-icon-*.png" )
qt4_add_resources( RC_SRCS "../resources.qrc" )
qt4_wrap_cpp( tomahawkMoc ${tomahawkHeaders} )
SET( final_src ${final_src} ${tomahawkUI_H} ${tomahawkMoc} ${tomahawkSources} ${RC_SRCS} )
IF( UNIX AND NOT APPLE )
ADD_EXECUTABLE( tomahawk ${final_src} )
SET_TARGET_PROPERTIES(tomahawk PROPERTIES AUTOMOC TRUE)
ENDIF( UNIX AND NOT APPLE )
IF( APPLE )
ADD_EXECUTABLE( tomahawk MACOSX_BUNDLE ${final_src} )
SET_TARGET_PROPERTIES(tomahawk PROPERTIES MACOSX_BUNDLE_INFO_PLIST "${CMAKE_BINARY_DIR}/Info.plist"
)
SET_TARGET_PROPERTIES(tomahawk PROPERTIES MACOSX_BUNDLE_INFO_PLIST "${CMAKE_BINARY_DIR}/Info.plist")
SET_TARGET_PROPERTIES(tomahawk PROPERTIES AUTOMOC TRUE)
ENDIF( APPLE )
IF( WIN32 )
ADD_EXECUTABLE( tomahawk WIN32 ${final_src} )

View File

@ -0,0 +1,114 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2010-2011, Leo Franchi <lfranchi@kde.org>
*
* Tomahawk 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.
*
* Tomahawk 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 Tomahawk. If not, see <http://www.gnu.org/licenses/>.
*/
#include "delegateconfigwrapper.h"
DelegateConfigWrapper::DelegateConfigWrapper( QWidget* conf, const QString& title, QWidget* parent, Qt::WindowFlags flags )
: QDialog( parent, flags )
, m_widget( conf )
, m_deleted( false )
{
m_widget->setWindowFlags( Qt::Sheet );
#ifdef Q_WS_MAC
m_widget->setVisible( true );
#endif
setWindowTitle( title );
QVBoxLayout* v = new QVBoxLayout( this );
v->setContentsMargins( 0, 0, 0, 0 );
v->addWidget( m_widget );
m_buttons = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this );
m_okButton = m_buttons->button( QDialogButtonBox::Ok );
connect( m_buttons, SIGNAL( clicked( QAbstractButton*) ), this, SLOT( closed( QAbstractButton* ) ) );
connect( this, SIGNAL( rejected() ), this, SLOT( rejected() ) );
v->addWidget( m_buttons );
setLayout( v );
#ifdef Q_WS_MAC
setSizeGripEnabled( false );
setMinimumSize( sizeHint() );
setMaximumSize( sizeHint() ); // to remove the resize grip on osx this is the only way
if( conf->metaObject()->indexOfSignal( "sizeHintChanged()" ) > -1 )
connect( conf, SIGNAL( sizeHintChanged() ), this, SLOT( updateSizeHint() ) );
#else
m_widget->setVisible( true );
#endif
}
void
DelegateConfigWrapper::setShowDelete( bool del )
{
if ( del )
m_deleteButton = m_buttons->addButton( tr( "Delete Account" ), QDialogButtonBox::DestructiveRole );
}
void
DelegateConfigWrapper::toggleOkButton( bool dataError )
{
// if dataError is True we want to set the button enabled to false
m_okButton->setEnabled( !dataError );
}
void
DelegateConfigWrapper::closed( QAbstractButton* b )
{
// let the config widget live to see another day
layout()->removeWidget( m_widget );
m_widget->setParent( 0 );
m_widget->setVisible( false );
QDialogButtonBox* buttons = qobject_cast< QDialogButtonBox* >( sender() );
if ( buttons->standardButton( b ) == QDialogButtonBox::Ok )
done( QDialog::Accepted );
else if ( b == m_deleteButton )
{
m_deleted = true;
emit closedWithDelete();
reject();
}
else
done( QDialog::Rejected );
}
void
DelegateConfigWrapper::rejected()
{
layout()->removeWidget( m_widget );
m_widget->setParent( 0 );
m_widget->setVisible( false );
}
void
DelegateConfigWrapper::updateSizeHint()
{
hide();
setSizeGripEnabled( false );
setMinimumSize( sizeHint() );
setMaximumSize( sizeHint() );
show();
}

View File

@ -28,91 +28,23 @@ class DelegateConfigWrapper : public QDialog
{
Q_OBJECT
public:
DelegateConfigWrapper( QWidget* conf, const QString& title, QWidget* parent, Qt::WindowFlags flags = 0 ) : QDialog( parent, flags ), m_widget( conf ), m_deleted( false )
{
m_widget->setWindowFlags( Qt::Sheet );
#ifdef Q_WS_MAC
m_widget->setVisible( true );
#endif
setWindowTitle( title );
QVBoxLayout* v = new QVBoxLayout( this );
v->setContentsMargins( 0, 0, 0, 0 );
v->addWidget( m_widget );
m_buttons = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this );
m_okButton = m_buttons->button( QDialogButtonBox::Ok );
connect( m_buttons, SIGNAL( clicked( QAbstractButton*) ), this, SLOT( closed( QAbstractButton* ) ) );
connect( this, SIGNAL( rejected() ), this, SLOT( rejected() ) );
v->addWidget( m_buttons );
setLayout( v );
#ifdef Q_WS_MAC
setSizeGripEnabled( false );
setMinimumSize( sizeHint() );
setMaximumSize( sizeHint() ); // to remove the resize grip on osx this is the only way
if( conf->metaObject()->indexOfSignal( "sizeHintChanged()" ) > -1 )
connect( conf, SIGNAL( sizeHintChanged() ), this, SLOT( updateSizeHint() ) );
#else
m_widget->setVisible( true );
#endif
}
DelegateConfigWrapper( QWidget* conf, const QString& title, QWidget* parent, Qt::WindowFlags flags = 0 );
~DelegateConfigWrapper() {}
void setShowDelete( bool del )
{
if ( del )
m_deleteButton = m_buttons->addButton( tr( "Delete Account" ), QDialogButtonBox::DestructiveRole );
}
void setShowDelete( bool del );
bool deleted() const { return m_deleted; }
public slots:
void toggleOkButton( bool dataError )
{
// if dataError is True we want to set the button enabled to false
m_okButton->setEnabled( !dataError );
}
void closed( QAbstractButton* b )
{
// let the config widget live to see another day
layout()->removeWidget( m_widget );
m_widget->setParent( 0 );
m_widget->setVisible( false );
QDialogButtonBox* buttons = qobject_cast< QDialogButtonBox* >( sender() );
if( buttons->standardButton( b ) == QDialogButtonBox::Ok )
done( QDialog::Accepted );
else if ( b == m_deleteButton )
{
m_deleted = true;
emit closedWithDelete();
reject();
}
else
done( QDialog::Rejected );
}
;
void closed( QAbstractButton* b );
// we get a rejected() signal emitted if the user presses escape (and no clicked() signal )
void rejected()
{
layout()->removeWidget( m_widget );
m_widget->setParent( 0 );
m_widget->setVisible( false );
}
void rejected();
void updateSizeHint()
{
hide();
setSizeGripEnabled( false );
setMinimumSize( sizeHint() );
setMaximumSize( sizeHint() );
show();
}
void updateSizeHint();
signals:
void closedWithDelete();

View File

@ -1,6 +1,7 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
* Copyright 2010-2011, Leo Franchi <lfranchi@kde.org>
*
* Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by

View File

@ -1,6 +1,7 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
* Copyright 2010-2011, Leo Franchi <lfranchi@kde.org>
*
* Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by

View File

@ -1,6 +1,7 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
* Copyright 2010-2011, Leo Franchi <lfranchi@kde.org>
*
* Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -19,6 +20,7 @@
#include "config.h"
#include "tomahawkutilsgui.h"
#include "logger.h"
#include <QtGui/QLayout>
#include <QtGui/QPainter>
#include <QtGui/QPixmap>
@ -36,7 +38,6 @@
#include <windowsx.h>
#endif
#include "logger.h"
namespace TomahawkUtils
{
@ -321,7 +322,7 @@ defaultPixmap( ImageType type, ImageMode mode, const QSize& size )
if ( subcache.contains( mode ) )
{
subsubcache = subcache.value( mode );
if ( subsubcache.contains( size.width() ) )
return subsubcache.value( size.width() );
}
@ -339,7 +340,7 @@ defaultPixmap( ImageType type, ImageMode mode, const QSize& size )
case DefaultArtistImage:
pixmap = QPixmap( RESPATH "images/no-artist-image-placeholder.png" );
break;
default:
break;
}
@ -352,7 +353,7 @@ defaultPixmap( ImageType type, ImageMode mode, const QSize& size )
if ( !size.isNull() )
pixmap = pixmap.scaled( size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
subsubcache.insert( size.width(), pixmap );
subcache.insert( mode, subsubcache );
cache.insert( type, subcache );
@ -360,4 +361,5 @@ defaultPixmap( ImageType type, ImageMode mode, const QSize& size )
return pixmap;
}
} // ns

View File

@ -1,6 +1,7 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
* Copyright 2010-2011, Leo Franchi <lfranchi@kde.org>
*
* Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -48,8 +49,9 @@ namespace TomahawkUtils
DLLEXPORT int headerHeight();
DLLEXPORT void setHeaderHeight( int height );
DLLEXPORT QPixmap defaultPixmap( ImageType type, ImageMode mode, const QSize& size = QSize( 0, 0 ) );
}
#endif // TOMAHAWKUTILSGUI_H

View File

@ -20,8 +20,6 @@
#include "settingsdialog.h"
#include "config.h"
#include "utils/tomahawkutilsgui.h"
#include <QDesktopServices>
#include <QFileDialog>
#include <QMessageBox>
@ -38,6 +36,8 @@
#include "pipeline.h"
#include "resolver.h"
#include "ExternalResolverGui.h"
#include "utils/tomahawkutilsgui.h"
#include "guihelpers.h"
#include "scanmanager.h"
#include "settingslistdelegate.h"
#include "AccountDelegate.h"
@ -343,61 +343,6 @@ SettingsDialog::accountsFilterChanged( int )
}
void
SettingsDialog::openAccountConfig( Account* account, bool showDelete )
{
if( account->configurationWidget() )
{
#ifndef Q_OS_MAC
DelegateConfigWrapper dialog( account->configurationWidget(), QString("%1 Configuration" ).arg( account->accountFriendlyName() ), this );
dialog.setShowDelete( showDelete );
QWeakPointer< DelegateConfigWrapper > watcher( &dialog );
int ret = dialog.exec();
if ( !watcher.isNull() && dialog.deleted() )
{
AccountManager::instance()->removeAccount( account );
}
else if( !watcher.isNull() && ret == QDialog::Accepted )
{
// send changed config to resolver
account->saveConfig();
}
#else
// on osx a sheet needs to be non-modal
DelegateConfigWrapper* dialog = new DelegateConfigWrapper( account->configurationWidget(), QString("%1 Configuration" ).arg( account->accountFriendlyName() ), this, Qt::Sheet );
dialog->setShowDelete( showDelete );
dialog->setProperty( "accountplugin", QVariant::fromValue< QObject* >( account ) );
connect( dialog, SIGNAL( finished( int ) ), this, SLOT( accountConfigClosed( int ) ) );
connect( dialog, SIGNAL( closedWithDelete() ), this, SLOT( accountConfigDelete() ) );
dialog->show();
#endif
}
}
void
SettingsDialog::accountConfigClosed( int value )
{
if( value == QDialog::Accepted )
{
DelegateConfigWrapper* dialog = qobject_cast< DelegateConfigWrapper* >( sender() );
Account* account = qobject_cast< Account* >( dialog->property( "accountplugin" ).value< QObject* >() );
account->saveConfig();
}
}
void
SettingsDialog::accountConfigDelete()
{
DelegateConfigWrapper* dialog = qobject_cast< DelegateConfigWrapper* >( sender() );
Account* account = qobject_cast< Account* >( dialog->property( "accountplugin" ).value< QObject* >() );
Q_ASSERT( account );
AccountManager::instance()->removeAccount( account );
}
void
SettingsDialog::openAccountFactoryConfig( AccountFactory* factory )
{
@ -437,89 +382,17 @@ SettingsDialog::openAccountFactoryConfig( AccountFactory* factory )
void
SettingsDialog::createAccountFromFactory( AccountFactory* factory )
{
#ifdef Q_WS_MAC
// On mac we need to close the dialog we came from before showing another dialog, if we are b eing called from a dialog
if ( sender() )
{
Q_ASSERT( qobject_cast< AccountFactoryWrapper* >( sender() ) );
AccountFactoryWrapper* dialog = qobject_cast< AccountFactoryWrapper* >( sender() );
dialog->accept();
}
#endif
//if exited with OK, create it, if not, delete it immediately!
Account* account = factory->createAccount();
bool added = false;
if( account->configurationWidget() )
{
#ifdef Q_WS_MAC
// on osx a sheet needs to be non-modal
DelegateConfigWrapper* dialog = new DelegateConfigWrapper( account->configurationWidget(), QString("%1 Config" ).arg( account->accountFriendlyName() ), this, Qt::Sheet );
dialog->setProperty( "accountplugin", QVariant::fromValue< QObject* >( account ) );
connect( dialog, SIGNAL( finished( int ) ), this, SLOT( accountCreateConfigClosed( int ) ) );
if( account->configurationWidget()->metaObject()->indexOfSignal( "dataError(bool)" ) > -1 )
connect( account->configurationWidget(), SIGNAL( dataError( bool ) ), dialog, SLOT( toggleOkButton( bool ) ), Qt::UniqueConnection );
dialog->show();
#else
DelegateConfigWrapper dialog( account->configurationWidget(), QString("%1 Config" ).arg( account->accountFriendlyName() ), this );
QWeakPointer< DelegateConfigWrapper > watcher( &dialog );
if( account->configurationWidget()->metaObject()->indexOfSignal( "dataError(bool)" ) > -1 )
connect( account->configurationWidget(), SIGNAL( dataError( bool ) ), &dialog, SLOT( toggleOkButton( bool ) ), Qt::UniqueConnection );
int ret = dialog.exec();
if( !watcher.isNull() && ret == QDialog::Accepted ) // send changed config to account
added = true;
else // canceled, delete it
added = false;
handleAccountAdded( account, added );
#endif
}
else
{
// no config, so just add it
added = true;
handleAccountAdded( account, added );
}
TomahawkUtils::createAccountFromFactory( factory, this );
}
void
SettingsDialog::accountCreateConfigClosed( int finished )
SettingsDialog::openAccountConfig( Account* account, bool showDelete )
{
DelegateConfigWrapper* dialog = qobject_cast< DelegateConfigWrapper* >( sender() );
Account* account = qobject_cast< Account* >( dialog->property( "accountplugin" ).value< QObject* >() );
Q_ASSERT( account );
bool added = ( finished == QDialog::Accepted );
handleAccountAdded( account, added );
TomahawkUtils::openAccountConfig( account, this, showDelete );
}
void
SettingsDialog::handleAccountAdded( Account* account, bool added )
{
if ( added )
{
account->setEnabled( true );
account->setAutoConnect( true );
account->saveConfig();
TomahawkSettings::instance()->addAccount( account->accountId() );
AccountManager::instance()->addAccount( account );
AccountManager::instance()->hookupAndEnable( account );
}
else
{
// user pressed cancel
delete account;
}
}
void
SettingsDialog::installFromFile()

View File

@ -89,12 +89,8 @@ private slots:
void accountsFilterChanged( int );
void createAccountFromFactory( Tomahawk::Accounts::AccountFactory* );
void openAccountConfig( Tomahawk::Accounts::Account*, bool showDelete = false );
void openAccountFactoryConfig( Tomahawk::Accounts::AccountFactory* );
void accountConfigClosed( int value );
void accountConfigDelete();
void accountCreateConfigClosed( int value );
void installFromFile();
void scrollTo( const QModelIndex& );
@ -108,7 +104,6 @@ private slots:
private:
void createIcons();
void handleAccountAdded( Tomahawk::Accounts::Account* p, bool added );
Ui_StackedSettingsDialog* ui;

172
src/utils/guihelpers.cpp Normal file
View File

@ -0,0 +1,172 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2010-2011, Leo Franchi <lfranchi@kde.org>
*
* Tomahawk 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.
*
* Tomahawk 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 Tomahawk. If not, see <http://www.gnu.org/licenses/>.
*/
#include "guihelpers.h"
#include <QObject>
#include "accounts/Account.h"
#include "accounts/AccountManager.h"
#include "delegateconfigwrapper.h"
namespace TomahawkUtils
{
void
handleAccountAdded( Tomahawk::Accounts::Account* account, bool added )
{
if ( added )
{
account->setEnabled( true );
account->setAutoConnect( true );
account->saveConfig();
TomahawkSettings::instance()->addAccount( account->accountId() );
Tomahawk::Accounts::AccountManager::instance()->addAccount( account );
Tomahawk::Accounts::AccountManager::instance()->hookupAndEnable( account );
}
else
{
// user pressed cancel
delete account;
}
}
class UtilsObject : public QObject
{
Q_OBJECT
public:
UtilsObject( DelegateConfigWrapper* w ) : QObject( w ), m_w( w ) {}
public slots:
void
accountCreateConfigClosed( int ret )
{
Tomahawk::Accounts::Account* account = qobject_cast< Tomahawk::Accounts::Account* >( m_w->property( "accountplugin" ).value< QObject* >() );
Q_ASSERT( account );
bool added = ( ret == QDialog::Accepted );
handleAccountAdded( account, added );
}
void
accountConfigClosed( int ret )
{
if( ret == QDialog::Accepted )
{
Tomahawk::Accounts::Account* account = qobject_cast< Tomahawk::Accounts::Account* >( m_w->property( "accountplugin" ).value< QObject* >() );
account->saveConfig();
}
}
void
accountConfigDelete()
{
Tomahawk::Accounts::Account* account = qobject_cast< Tomahawk::Accounts::Account* >( m_w->property( "accountplugin" ).value< QObject* >() );
Q_ASSERT( account );
Tomahawk::Accounts::AccountManager::instance()->removeAccount( account );
}
private:
DelegateConfigWrapper* m_w;
};
void
createAccountFromFactory( Tomahawk::Accounts::AccountFactory* factory, QWidget* parent )
{
//if exited with OK, create it, if not, delete it immediately!
Tomahawk::Accounts::Account* account = factory->createAccount();
bool added = false;
if( account->configurationWidget() )
{
#ifdef Q_WS_MAC
// on osx a sheet needs to be non-modal
DelegateConfigWrapper* dialog = new DelegateConfigWrapper( account->configurationWidget(), QString("%1 Config" ).arg( account->accountFriendlyName() ), parent, Qt::Sheet );
dialog->setProperty( "accountplugin", QVariant::fromValue< QObject* >( account ) );
UtilsObject* obj = new UtilsObject( dialog );
QObject::connect( dialog, SIGNAL( finished( int ) ), obj, SLOT( accountCreateConfigClosed( int ) ) );
if( account->configurationWidget()->metaObject()->indexOfSignal( "dataError(bool)" ) > -1 )
QObject::connect( account->configurationWidget(), SIGNAL( dataError( bool ) ), dialog, SLOT( toggleOkButton( bool ) ), Qt::UniqueConnection );
dialog->show();
#else
DelegateConfigWrapper dialog( account->configurationWidget(), QString("%1 Config" ).arg( account->accountFriendlyName() ), parent );
QWeakPointer< DelegateConfigWrapper > watcher( &dialog );
if( account->configurationWidget()->metaObject()->indexOfSignal( "dataError(bool)" ) > -1 )
QObject::connect( account->configurationWidget(), SIGNAL( dataError( bool ) ), &dialog, SLOT( toggleOkButton( bool ) ), Qt::UniqueConnection );
int ret = dialog.exec();
if( !watcher.isNull() && ret == QDialog::Accepted ) // send changed config to account
added = true;
else // canceled, delete it
added = false;
handleAccountAdded( account, added );
#endif
}
else
{
// no config, so just add it
added = true;
handleAccountAdded( account, added );
}
}
void
openAccountConfig( Tomahawk::Accounts::Account* account, QWidget* parent, bool showDelete )
{
if( account->configurationWidget() )
{
#ifndef Q_OS_MAC
DelegateConfigWrapper dialog( account->configurationWidget(), QString("%1 Configuration" ).arg( account->accountFriendlyName() ), parent );
dialog.setShowDelete( showDelete );
QWeakPointer< DelegateConfigWrapper > watcher( &dialog );
int ret = dialog.exec();
if ( !watcher.isNull() && dialog.deleted() )
{
Tomahawk::Accounts::AccountManager::instance()->removeAccount( account );
}
else if( !watcher.isNull() && ret == QDialog::Accepted )
{
// send changed config to resolver
account->saveConfig();
}
#else
// on osx a sheet needs to be non-modal
DelegateConfigWrapper* dialog = new DelegateConfigWrapper( account->configurationWidget(), QString("%1 Configuration" ).arg( account->accountFriendlyName() ), parent, Qt::Sheet );
dialog->setShowDelete( showDelete );
dialog->setProperty( "accountplugin", QVariant::fromValue< QObject* >( account ) );
UtilsObject* obj = new UtilsObject( dialog );
QObject::connect( dialog, SIGNAL( finished( int ) ), obj, SLOT( accountConfigClosed( int ) ) );
QObject::connect( dialog, SIGNAL( closedWithDelete() ), obj, SLOT( accountConfigDelete() ) );
dialog->show();
#endif
}
}
} // namespace TomahawkUtils
#include "guihelpers.moc"

38
src/utils/guihelpers.h Normal file
View File

@ -0,0 +1,38 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2010-2011, Leo Franchi <lfranchi@kde.org>
*
* Tomahawk 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.
*
* Tomahawk 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 Tomahawk. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef TOMAHAWK_GUI_HELPERS_H
#define TOMAHAWK_GUI_HELPERS_H
#include "dllmacro.h"
class QWidget;
namespace Tomahawk {
namespace Accounts {
class AccountFactory;
class Account;
}
}
namespace TomahawkUtils
{
DLLEXPORT void createAccountFromFactory( Tomahawk::Accounts::AccountFactory*, QWidget* parent );
DLLEXPORT void openAccountConfig( Tomahawk::Accounts::Account*, QWidget* parent, bool showDelete = false );
}
#endif