From de5a2b7d651735499fe3c00f067784bea0d31f85 Mon Sep 17 00:00:00 2001 From: Leo Franchi Date: Mon, 14 Mar 2011 16:58:36 -0400 Subject: [PATCH] fix menus for jreen and jabber plugins. now added when connected, removed when disconnected. and no crashy. --- src/libtomahawk/sip/SipPlugin.h | 3 +++ src/sip/SipHandler.cpp | 2 +- src/sip/jabber/jabber.cpp | 46 +++++++++++++++++++++++++------- src/sip/jabber/jabber.h | 6 ++++- src/sip/jreen/jabber.cpp | 47 ++++++++++++++++++++++++++------- src/sip/jreen/jabber.h | 2 ++ src/tomahawkwindow.cpp | 25 +++++++++++++++--- src/tomahawkwindow.h | 2 ++ 8 files changed, 108 insertions(+), 25 deletions(-) diff --git a/src/libtomahawk/sip/SipPlugin.h b/src/libtomahawk/sip/SipPlugin.h index b94989511..0fa150ba6 100644 --- a/src/libtomahawk/sip/SipPlugin.h +++ b/src/libtomahawk/sip/SipPlugin.h @@ -38,6 +38,9 @@ signals: void peerOnline( const QString& ); void peerOffline( const QString& ); void msgReceived( const QString& from, const QString& msg ); + + void addMenu( QMenu* menu ); + void removeMenu( QMenu* menu ); }; Q_DECLARE_INTERFACE( SipPlugin, "tomahawk.Sip/1.0" ) diff --git a/src/sip/SipHandler.cpp b/src/sip/SipHandler.cpp index 061720a5e..b4f5297a8 100644 --- a/src/sip/SipHandler.cpp +++ b/src/sip/SipHandler.cpp @@ -286,6 +286,6 @@ SipHandler::onError( int code, const QString& msg ) else { SipPlugin* sip = qobject_cast(sender()); - QTimer::singleShot( 10000, sip, SLOT( connect() ) ); + QTimer::singleShot( 10000, sip, SLOT( connectPlugin() ) ); } } diff --git a/src/sip/jabber/jabber.cpp b/src/sip/jabber/jabber.cpp index 40c948b02..89c5cfcbe 100644 --- a/src/sip/jabber/jabber.cpp +++ b/src/sip/jabber/jabber.cpp @@ -9,15 +9,9 @@ JabberPlugin::JabberPlugin() : p( 0 ), - m_menu ( 0 ) + m_menu ( 0 ), + m_addFriendAction( 0 ) { - if( !accountName().isEmpty() ) - m_menu = new QMenu(QString("Jabber (").append(accountName()).append(")")); - - m_addFriendAction = m_menu->addAction("Add Friend..."); - - connect(m_addFriendAction, SIGNAL(triggered()), - this, SLOT(showAddFriendDialog())); } void @@ -86,8 +80,8 @@ JabberPlugin::connectPlugin( bool startup ) QObject::connect( p, SIGNAL( peerOffline( QString ) ), SIGNAL( peerOffline( QString ) ) ); QObject::connect( p, SIGNAL( msgReceived( QString, QString ) ), SIGNAL( msgReceived( QString, QString ) ) ); - QObject::connect( p, SIGNAL( connected() ), SIGNAL( connected() ) ); - QObject::connect( p, SIGNAL( disconnected() ), SIGNAL( disconnected() ) ); + QObject::connect( p, SIGNAL( connected() ), SLOT( onConnected() ) ); + QObject::connect( p, SIGNAL( disconnected() ), SIGNAL( onDisconnected() ) ); QObject::connect( p, SIGNAL( authError( int, QString ) ), SLOT( onAuthError( int, QString ) ) ); p->resolveHostSRV(); @@ -95,6 +89,38 @@ JabberPlugin::connectPlugin( bool startup ) return true; } +void +JabberPlugin::onConnected() +{ + if( !m_menu ) { + m_menu = new QMenu( QString( "Jabber (").append( accountName() ).append( ")" ) ); + + m_addFriendAction = m_menu->addAction( "Add Friend..." ); + + connect( m_addFriendAction, SIGNAL( triggered() ), + this, SLOT( showAddFriendDialog() ) ) ; + + emit addMenu( m_menu ); + } + + emit connected(); +} + +void +JabberPlugin::onDisconnected() +{ + if( m_menu && m_addFriendAction ) { + emit removeMenu( m_menu ); + + delete m_menu; + m_menu = 0; + m_addFriendAction = 0; + } + + emit disconnected(); +} + + void JabberPlugin::onAuthError( int code, const QString& message ) diff --git a/src/sip/jabber/jabber.h b/src/sip/jabber/jabber.h index 1ea95e52a..025cb03c1 100644 --- a/src/sip/jabber/jabber.h +++ b/src/sip/jabber/jabber.h @@ -32,6 +32,8 @@ public slots: void disconnectPlugin() { + onDisconnected(); + if ( p ) p->disconnect(); @@ -60,7 +62,9 @@ public slots: private slots: void onAuthError( int, const QString& ); void showAddFriendDialog(); - + void onConnected(); + void onDisconnected(); + private: Jabber_p* p; QMenu* m_menu; diff --git a/src/sip/jreen/jabber.cpp b/src/sip/jreen/jabber.cpp index 6f02e7af2..147f59d0d 100644 --- a/src/sip/jreen/jabber.cpp +++ b/src/sip/jreen/jabber.cpp @@ -10,14 +10,9 @@ JabberPlugin::JabberPlugin() : p( 0 ) + , m_menu( 0 ) + , m_addFriendAction( 0 ) { - m_menu = new QMenu(QString("JREEN (").append(accountName()).append(")")); - m_addFriendAction = m_menu->addAction("Add Friend..."); - QAction *connectAction = m_menu->addAction("Connect"); - - connect(m_addFriendAction, SIGNAL(triggered()), - this, SLOT(showAddFriendDialog())); - connect(connectAction, SIGNAL(triggered()), SLOT(connectPlugin())); } JabberPlugin::~JabberPlugin() @@ -92,8 +87,8 @@ JabberPlugin::connectPlugin( bool startup ) QObject::connect( p, SIGNAL( peerOffline( QString ) ), SIGNAL( peerOffline( QString ) ) ); QObject::connect( p, SIGNAL( msgReceived( QString, QString ) ), SIGNAL( msgReceived( QString, QString ) ) ); - QObject::connect( p, SIGNAL( connected() ), SIGNAL( connected() ) ); - QObject::connect( p, SIGNAL( disconnected() ), SIGNAL( disconnected() ) ); + QObject::connect( p, SIGNAL( connected() ), SIGNAL( onConnected() ) ); + QObject::connect( p, SIGNAL( disconnected() ), SIGNAL( onDisconnected() ) ); return true; } @@ -101,6 +96,8 @@ JabberPlugin::connectPlugin( bool startup ) void JabberPlugin::disconnectPlugin() { + onDisconnected(); + if ( p ) p->disconnect(); @@ -108,6 +105,38 @@ JabberPlugin::disconnectPlugin() p = 0; } +void +JabberPlugin::onConnected() +{ + if( !m_menu ) { + m_menu = new QMenu( QString( "JREEN (" ).append( accountName() ).append(")" ) ); + m_addFriendAction = m_menu->addAction( "Add Friend..." ); + QAction *connectAction = m_menu->addAction( "Connect" ); + + connect( m_addFriendAction, SIGNAL(triggered() ), + this, SLOT( showAddFriendDialog() ) ); + connect( connectAction, SIGNAL( triggered() ), SLOT( connectPlugin() ) ); + + emit addMenu( m_menu ); + } + + emit connected(); +} + +void +JabberPlugin::onDisconnected() +{ + if( m_menu && m_addFriendAction ) { + emit removeMenu( m_menu ); + + delete m_menu; + m_menu = 0; + m_addFriendAction = 0; // deleted by menu + } + + emit disconnected(); +} + void JabberPlugin::sendMsg(const QString& to, const QString& msg) { diff --git a/src/sip/jreen/jabber.h b/src/sip/jreen/jabber.h index 32a38b588..430c69058 100644 --- a/src/sip/jreen/jabber.h +++ b/src/sip/jreen/jabber.h @@ -35,6 +35,8 @@ public slots: private slots: void showAddFriendDialog(); + void onConnected(); + void onDisconnected(); private: Jabber_p* p; diff --git a/src/tomahawkwindow.cpp b/src/tomahawkwindow.cpp index 1265b01e7..4f04f84a6 100644 --- a/src/tomahawkwindow.cpp +++ b/src/tomahawkwindow.cpp @@ -157,10 +157,8 @@ TomahawkWindow::TomahawkWindow( QWidget* parent ) // propagate sip menu foreach( SipPlugin *plugin, APP->sipHandler()->plugins() ) { - if ( plugin->menu() ) - { - ui->menuNetwork->addMenu( plugin->menu() ); - } + connect( plugin, SIGNAL( addMenu( QMenu* ) ), this, SLOT( pluginMenuAdded( QMenu* ) ) ); + connect( plugin, SIGNAL( removeMenu( QMenu* ) ), this, SLOT( pluginMenuRemoved( QMenu* ) ) ); } loadSettings(); @@ -337,6 +335,25 @@ TomahawkWindow::addPeerManually() Servent::instance()->connectToPeer( addr, port, key ); } +void +TomahawkWindow::pluginMenuAdded( QMenu* menu ) +{ + ui->menuNetwork->addMenu( menu ); +} + +void +TomahawkWindow::pluginMenuRemoved( QMenu* menu ) +{ + foreach( QAction* action, ui->menuNetwork->actions() ) + { + if( action->menu() == menu ) + { + ui->menuNetwork->removeAction( action ); + return; + } + } +} + void TomahawkWindow::loadSpiff() diff --git a/src/tomahawkwindow.h b/src/tomahawkwindow.h index 9e1d2df87..4d729186c 100644 --- a/src/tomahawkwindow.h +++ b/src/tomahawkwindow.h @@ -45,6 +45,8 @@ public slots: void loadSpiff(); void showSettingsDialog(); void updateCollectionManually(); + void pluginMenuAdded(QMenu*); + void pluginMenuRemoved(QMenu*); private slots: void onSipConnected();