diff options
author | Andrzej J.R. Hunt <andrzej@ahunt.org> | 2012-09-18 18:22:12 +0200 |
---|---|---|
committer | Andrzej J.R. Hunt <andrzej@ahunt.org> | 2012-09-18 18:22:37 +0200 |
commit | 64d1fcb11f4915df960ec81bc9168a557199090f (patch) | |
tree | 432a75871ebd631a6c7ba76cef94912ff4819fc7 /sd/source | |
parent | 6c1b61b0292359304863cd6e593b8f5d7a2cb94a (diff) |
Switch bluetooth discoverability on when remote dialog shown (Linux).
Change-Id: Ie0e4f7a353d77f5312deea06f0a316d8e1b1ed47
Diffstat (limited to 'sd/source')
-rw-r--r-- | sd/source/ui/dlg/RemoteDialog.cxx | 22 | ||||
-rw-r--r-- | sd/source/ui/dlg/RemoteDialog.hxx | 3 | ||||
-rw-r--r-- | sd/source/ui/inc/RemoteServer.hxx | 3 | ||||
-rw-r--r-- | sd/source/ui/remotecontrol/BluetoothServer.cxx | 185 | ||||
-rw-r--r-- | sd/source/ui/remotecontrol/BluetoothServer.hxx | 3 | ||||
-rw-r--r-- | sd/source/ui/remotecontrol/Server.cxx | 18 |
6 files changed, 201 insertions, 33 deletions
diff --git a/sd/source/ui/dlg/RemoteDialog.cxx b/sd/source/ui/dlg/RemoteDialog.cxx index d920d91c0527..28b41589904b 100644 --- a/sd/source/ui/dlg/RemoteDialog.cxx +++ b/sd/source/ui/dlg/RemoteDialog.cxx @@ -22,10 +22,15 @@ RemoteDialog::RemoteDialog( Window *pWindow ) : ModalDialog( pWindow, SdResId( DLG_PAIR_REMOTE ) ), mButtonConnect( this, SdResId( BTN_CONNECT ) ), mButtonCancel( this, SdResId( BTN_CANCEL ) ), - mClientBox( this, NULL, SdResId( LB_SERVERS ) ) + mClientBox( this, NULL, SdResId( LB_SERVERS ) ), + mPreviouslyDiscoverable() { FreeResource(); + mPreviouslyDiscoverable = RemoteServer::isBluetoothDiscoverable(); + if ( !mPreviouslyDiscoverable ) + RemoteServer::setBluetoothDiscoverable( true ); + vector<ClientInfo*> aClients( RemoteServer::getClients() ); for ( vector<ClientInfo*>::const_iterator aIt( aClients.begin() ); @@ -35,6 +40,8 @@ RemoteDialog::RemoteDialog( Window *pWindow ) : } mButtonConnect.SetClickHdl( LINK( this, RemoteDialog, HandleConnectButton ) ); + SetCloseHdl( LINK( this, RemoteDialog, CloseHdl ) ); + mButtonCancel.SetClickHdl( LINK( this, RemoteDialog, CloseHdl ) ); } RemoteDialog::~RemoteDialog() @@ -53,11 +60,20 @@ IMPL_LINK_NOARG(RemoteDialog, HandleConnectButton) OUString aPin ( mClientBox.getPin() ); if ( RemoteServer::connectClient( aEntry->m_pClientInfo, aPin ) ) { - Close(); - return 0; + return CloseHdl( 0 ); } else return 1; } +IMPL_LINK_NOARG( RemoteDialog, CloseHdl ) +{ + if ( !mPreviouslyDiscoverable ) + { + RemoteServer::setBluetoothDiscoverable( false ); + } + Close(); + return 0; +} + /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
\ No newline at end of file diff --git a/sd/source/ui/dlg/RemoteDialog.hxx b/sd/source/ui/dlg/RemoteDialog.hxx index 87782a68c269..ef1cf58b2222 100644 --- a/sd/source/ui/dlg/RemoteDialog.hxx +++ b/sd/source/ui/dlg/RemoteDialog.hxx @@ -27,8 +27,11 @@ private: OKButton mButtonConnect; CancelButton mButtonCancel; ClientBox mClientBox; + // Whether discoverability was enabled befor the dialog started. + bool mPreviouslyDiscoverable; DECL_DLLPRIVATE_LINK( HandleConnectButton, void * ); + DECL_LINK( CloseHdl, void * ); public: RemoteDialog( Window* pWindow ); ~RemoteDialog(); diff --git a/sd/source/ui/inc/RemoteServer.hxx b/sd/source/ui/inc/RemoteServer.hxx index 75b350e00751..931c8c26f171 100644 --- a/sd/source/ui/inc/RemoteServer.hxx +++ b/sd/source/ui/inc/RemoteServer.hxx @@ -69,6 +69,9 @@ namespace sd SD_DLLPUBLIC static sal_Bool connectClient( ClientInfo *pClient, rtl::OUString aPin ); + SD_DLLPUBLIC static bool isBluetoothDiscoverable(); + SD_DLLPUBLIC static void setBluetoothDiscoverable( bool aDiscoverable ); + // For the communicator static void removeCommunicator( Communicator* pCommunicator ); private: diff --git a/sd/source/ui/remotecontrol/BluetoothServer.cxx b/sd/source/ui/remotecontrol/BluetoothServer.cxx index 2e06322baf27..1521b1e38583 100644 --- a/sd/source/ui/remotecontrol/BluetoothServer.cxx +++ b/sd/source/ui/remotecontrol/BluetoothServer.cxx @@ -16,6 +16,7 @@ #include <sys/socket.h> #include <bluetooth/bluetooth.h> #include <bluetooth/rfcomm.h> +#define DBUS_TYPE_G_STRING_ANY_HASHTABLE (dbus_g_type_get_map( "GHashTable", G_TYPE_STRING, G_TYPE_VALUE )) #endif #ifdef WIN32 @@ -34,6 +35,48 @@ using namespace sd; +#if (defined(LINUX) && !defined(__FreeBSD_kernel__)) && defined(ENABLE_DBUS) +DBusGProxy* bluezGetDefaultAdapter( DBusGConnection* aConnection, + const gchar* aInterfaceType = "org.bluez.Adapter" ) +{ + GError *aError = NULL; + + DBusGProxy *aManager = NULL; + aManager = dbus_g_proxy_new_for_name( aConnection, "org.bluez", "/", + "org.bluez.Manager" ); + + if ( aManager == NULL ) + { + dbus_g_connection_unref( aConnection ); + return NULL; + } + + gboolean aResult; + // The following should be a DBusGObjectPath, however the necessary + // typedef is missing in older version of dbus-glib. + char *aAdapterPath = NULL; + aResult = dbus_g_proxy_call( aManager, "DefaultAdapter", &aError, + G_TYPE_INVALID, + DBUS_TYPE_G_OBJECT_PATH, &aAdapterPath, + G_TYPE_INVALID); + + g_object_unref( G_OBJECT( aManager )); + if ( !aResult || aError ) + { + if ( aError ) + g_error_free( aError ); + return NULL; + } + + DBusGProxy *aAdapter = NULL; + aAdapter = dbus_g_proxy_new_for_name( aConnection, "org.bluez", + aAdapterPath, aInterfaceType ); + g_free( aAdapterPath ); + + return aAdapter; +} +#endif // defined(LINUX) && defined(ENABLE_DBUS) + BluetoothServer::BluetoothServer( std::vector<Communicator*>* pCommunicators ): Thread( "BluetoothServer" ), mpCommunicators( pCommunicators ) @@ -46,56 +89,148 @@ BluetoothServer::~BluetoothServer() bool BluetoothServer::isDiscoverable() { - return true; +#if (defined(LINUX) && !defined(__FreeBSD_kernel__)) && defined(ENABLE_DBUS) + g_type_init(); + gboolean aResult; + + GError *aError = NULL; + + DBusGConnection *aConnection = NULL; + aConnection = dbus_g_bus_get( DBUS_BUS_SYSTEM, &aError ); + + if ( aError != NULL ) { + g_error_free (aError); + return false; + } + + DBusGProxy* aAdapter = bluezGetDefaultAdapter( aConnection ); + if ( aAdapter == NULL ) + { + dbus_g_connection_unref( aConnection ); + return false; + } + + GHashTable* aProperties; + aResult = dbus_g_proxy_call( aAdapter, "GetProperties", &aError, + G_TYPE_INVALID, + DBUS_TYPE_G_STRING_ANY_HASHTABLE, &aProperties, + G_TYPE_INVALID); + g_object_unref( G_OBJECT( aAdapter )); + dbus_g_connection_unref( aConnection ); + if ( !aResult || aError ) + { + if ( aError ) + g_error_free( aError ); + return false; + } + GVariant* aVariant = dbus_g_value_build_g_variant ( (GValue*) + g_hash_table_lookup( aProperties, "Discoverable" ) ); + gboolean aIsDiscoverable = g_variant_get_boolean( aVariant ); + g_free( aVariant ); + + g_free( aProperties ); + return aIsDiscoverable; +#else // defined(LINUX) && defined(ENABLE_DBUS) + return false; +#endif } -void BluetoothServer::execute() +void BluetoothServer::setDiscoverable( bool aDiscoverable ) { #if (defined(LINUX) && !defined(__FreeBSD_kernel__)) && defined(ENABLE_DBUS) - g_type_init(); + gboolean aResult; GError *aError = NULL; DBusGConnection *aConnection = NULL; aConnection = dbus_g_bus_get( DBUS_BUS_SYSTEM, &aError ); - if ( aError != NULL ) { + if ( aError != NULL ) + { g_error_free (aError); return; } + DBusGProxy* aAdapter = bluezGetDefaultAdapter( aConnection ); + if ( aAdapter == NULL ) + { + dbus_g_connection_unref( aConnection ); + return; + } - DBusGProxy *aManager = NULL; - aManager = dbus_g_proxy_new_for_name( aConnection, "org.bluez", "/", - "org.bluez.Manager" ); + GHashTable* aProperties; + aResult = dbus_g_proxy_call( aAdapter, "GetProperties", &aError, + G_TYPE_INVALID, + DBUS_TYPE_G_STRING_ANY_HASHTABLE, &aProperties, + G_TYPE_INVALID); - if ( aManager == NULL ) + if ( !aResult || aError ) { - dbus_g_connection_unref( aConnection ); + if ( aError ) + g_error_free( aError ); + return; + } + GVariant* aVariant = dbus_g_value_build_g_variant( (GValue*) + g_hash_table_lookup( aProperties, "Powered" ) ); + gboolean aPowered = g_variant_get_boolean( aVariant ); + g_free( aVariant ); + g_free( aProperties ); + if ( !aPowered ) + { + g_object_unref( G_OBJECT( aAdapter )); return; } - gboolean aResult; - // The following should be a DBusGObjectPath, however the necessary - // typedef is missing in older version of dbus-glib. - char *aAdapterPath = NULL; - aResult = dbus_g_proxy_call( aManager, "DefaultAdapter", &aError, - G_TYPE_INVALID, - DBUS_TYPE_G_OBJECT_PATH, &aAdapterPath, - G_TYPE_INVALID); + GValue aTimeout = G_VALUE_INIT; + g_value_init( &aTimeout, G_TYPE_UINT ); + g_value_set_uint( &aTimeout, 0 ); + aResult = dbus_g_proxy_call( aAdapter, "SetProperty", &aError, + G_TYPE_STRING, "DiscoverableTimeout", + G_TYPE_VALUE, &aTimeout, G_TYPE_INVALID, G_TYPE_INVALID); + if ( !aResult || aError ) + { + if ( aError ) + g_error_free( aError ); + return; + } - g_object_unref( G_OBJECT( aManager )); - if ( !aResult ) + GValue aDiscoverableGValue = G_VALUE_INIT; + g_value_init( &aDiscoverableGValue, G_TYPE_BOOLEAN ); + g_value_set_boolean( &aDiscoverableGValue, aDiscoverable ); + aResult = dbus_g_proxy_call( aAdapter, "SetProperty", &aError, + G_TYPE_STRING, "Discoverable", + G_TYPE_VALUE, &aDiscoverableGValue, G_TYPE_INVALID, G_TYPE_INVALID); + if ( !aResult || aError ) { - dbus_g_connection_unref( aConnection ); + if ( aError ) + g_error_free( aError ); return; } - DBusGProxy *aAdapter = NULL; - aAdapter = dbus_g_proxy_new_for_name( aConnection, "org.bluez", - aAdapterPath, "org.bluez.Service" ); - g_free( aAdapterPath ); + g_object_unref( G_OBJECT( aAdapter )); + dbus_g_connection_unref( aConnection ); +#else // defined(LINUX) && defined(ENABLE_DBUS) + return; +#endif +} + +void BluetoothServer::execute() +{ +#if (defined(LINUX) && !defined(__FreeBSD_kernel__)) && defined(ENABLE_DBUS) + g_type_init(); + + GError *aError = NULL; + + DBusGConnection *aConnection = NULL; + aConnection = dbus_g_bus_get( DBUS_BUS_SYSTEM, &aError ); + + if ( aError != NULL ) { + g_error_free (aError); + return; + } + + DBusGProxy* aAdapter = bluezGetDefaultAdapter( aConnection, "org.bluez.Service" ); if ( aAdapter == NULL ) { dbus_g_connection_unref( aConnection ); @@ -105,7 +240,7 @@ void BluetoothServer::execute() // Add the record -- the handle can be used to release it again, but we // don't bother as the record is automatically released when LO exits. guint aHandle; - aResult = dbus_g_proxy_call( aAdapter, "AddRecord", &aError, + gboolean aResult = dbus_g_proxy_call( aAdapter, "AddRecord", &aError, G_TYPE_STRING, BLUETOOTH_SERVICE_RECORD , G_TYPE_INVALID, G_TYPE_UINT, &aHandle, diff --git a/sd/source/ui/remotecontrol/BluetoothServer.hxx b/sd/source/ui/remotecontrol/BluetoothServer.hxx index 65bc3d9b2a1e..317a6f03ac2e 100644 --- a/sd/source/ui/remotecontrol/BluetoothServer.hxx +++ b/sd/source/ui/remotecontrol/BluetoothServer.hxx @@ -22,7 +22,8 @@ namespace sd public: static void setup( std::vector<Communicator*>* pCommunicators ); - bool isDiscoverable(); + static bool isDiscoverable(); + static void setDiscoverable( bool aDiscoverable ); private: BluetoothServer( std::vector<Communicator*>* pCommunicators ); ~BluetoothServer(); diff --git a/sd/source/ui/remotecontrol/Server.cxx b/sd/source/ui/remotecontrol/Server.cxx index e662d3215d24..c2f62a7f0eb8 100644 --- a/sd/source/ui/remotecontrol/Server.cxx +++ b/sd/source/ui/remotecontrol/Server.cxx @@ -177,7 +177,7 @@ void RemoteServer::presentationStarted( const css::uno::Reference< return; MutexGuard aGuard( spServer->mDataMutex ); for ( vector<Communicator*>::const_iterator aIt = spServer->mCommunicators.begin(); - aIt != spServer->mCommunicators.end(); ++aIt ) + aIt < spServer->mCommunicators.end(); aIt++ ) { (*aIt)->presentationStarted( rController ); } @@ -188,7 +188,7 @@ void RemoteServer::presentationStopped() return; MutexGuard aGuard( spServer->mDataMutex ); for ( vector<Communicator*>::const_iterator aIt = spServer->mCommunicators.begin(); - aIt != spServer->mCommunicators.end(); ++aIt ) + aIt < spServer->mCommunicators.end(); aIt++ ) { (*aIt)->disposeListener(); } @@ -200,7 +200,7 @@ void RemoteServer::removeCommunicator( Communicator* mCommunicator ) return; MutexGuard aGuard( spServer->mDataMutex ); for ( vector<Communicator*>::iterator aIt = spServer->mCommunicators.begin(); - aIt != spServer->mCommunicators.end(); ++aIt ) + aIt < spServer->mCommunicators.end(); aIt++ ) { if ( mCommunicator == *aIt ) { @@ -265,7 +265,7 @@ sal_Bool RemoteServer::connectClient( ClientInfo* pClient, rtl::OUString aPin ) spServer->mCommunicators.push_back( pCommunicator ); for ( vector<ClientInfoInternal*>::iterator aIt = spServer->mAvailableClients.begin(); - aIt != spServer->mAvailableClients.end(); ++aIt ) + aIt < spServer->mAvailableClients.end(); aIt++ ) { if ( pClient == *aIt ) { @@ -293,4 +293,14 @@ void SdDLL::RegisterRemotes() sd::DiscoveryService::setup(); } + +bool RemoteServer::isBluetoothDiscoverable() +{ + return BluetoothServer::isDiscoverable(); +} + +void RemoteServer::setBluetoothDiscoverable( bool aDiscoverable ) +{ + BluetoothServer::setDiscoverable( aDiscoverable ); +} /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |