Make tdebluezauth and obexd fork and be unique, adding a way to start/stop them

tdeio updated

Signed-off-by: Emanoil Kotsev <deloptes@gmail.com>
pull/1/head
Emanoil Kotsev 5 年前
父节点 64c6f3a575
当前提交 3d6464ce7e

@ -45,44 +45,30 @@ AdapterImpl::~AdapterImpl() {
void AdapterImpl::powerOn(bool state) {
// https://www.kernel.org/doc/Documentation/rfkill.txt
TQString hci;
uint hciId;
TQString path = getPath();
int start=path.find("hci",0,false);
if (start !=-1)
hci = path.remove(0,start);
// I did not find a better way at the moment
// according docs it should be possible to use sysfs
// to obtain the hci id assigned by the kernel
FILE *fp;
char buffer[128];
fp = popen(TQFile::encodeName("/usr/sbin/rfkill list"), "r");
while (fgets(buffer, sizeof(buffer), fp)) {
if (TQString(buffer).find(hci,0,false) != -1)
break;
}
pclose(fp);
TQString readResult(buffer);
if ( start=readResult.find(':',0,false) != -1 )
hciId = readResult.left(start).toUInt();
// http://jwhsmith.net/2015/02/manipulating-rfkill-using-devices-programmatically/
struct rfkill_event event;
struct rfkill_event event = {0};
TQFile file("/dev/rfkill");
event.idx = hciId;
event.type = RFKILL_TYPE_BLUETOOTH;
if (!file.open(IO_ReadWrite)) {
tqDebug("Failed to open %s", file.name().utf8().data());
return;
}
while(read(file.handle(), &event, sizeof(event)) > 0){
if(event.type == RFKILL_TYPE_BLUETOOTH) {
tqDebug("Bluetooth chip switches: soft(%d), hard(%d).\n",
event.soft, event.hard);
break;
}
}
event.op = RFKILL_OP_CHANGE;
if (state)
event.soft = 0;
else
event.soft = 1;
if (!file.open(IO_WriteOnly)) {
tqDebug("Failed to open %s", file.name().utf8().data());
return;
}
if(write(file.handle(), &event, sizeof(event)) < 0){
tqDebug("Failed to write to %s", file.name().utf8().data());
}

@ -22,6 +22,7 @@
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <dcopclient.h>
#include "trayicon.h"
#include "application.h"
@ -29,15 +30,15 @@
TDEBluetoothApp::TDEBluetoothApp() :
KUniqueApplication()
{
m_config = new TDEConfig("tdebluezrc");
// Enable autostart
m_config->setGroup("General");
m_config->readBoolEntry("AutoStart", true);
bool authAgentStart = m_config->readBoolEntry("authAgent", true);
bool obexServerStart = m_config->readBoolEntry("obexSrv", true);
authAgent = 0;
obexServer = 0;
// m_config = new TDEConfig("tdebluezrc");
// // Enable autostart
// m_config->setGroup("General");
// m_config->readBoolEntry("AutoStart", true);
// bool authAgentStart = m_config->readBoolEntry("authAgent", true);
// bool obexServerStart = m_config->readBoolEntry("obexSrv", true);
//
// authAgent = 0;
// obexServer = 0;
manager = new ObjectManagerImpl("org.bluez", "/", this, "ObjectManager");
if (!manager->isConnectedToDBUS())
@ -77,33 +78,14 @@ TDEBluetoothApp::TDEBluetoothApp() :
trayIcon = new TrayIcon(this);
setMainWidget(trayIcon);
if (authAgentStart)
{
if (!startAuthAgent())
tqWarning("TDEBluezAuth failed to start.");
}
if (obexServerStart)
{
if (!startObexSrv())
{
tqWarning("TDEBluezOBEX failed to start.");
}
// else
// connect(trayIcon, SIGNAL(setObexSrv(bool)), this, SLOT(slotObexSrv(bool)));
}
}
TDEBluetoothApp::~TDEBluetoothApp()
{
delete trayIcon;
if (!stopAuthAgent())
authAgent->kill();
delete authAgent;
stopAuthAgent();
stopObexSrv();
if (!stopObexSrv())
obexServer->kill();
delete obexServer;
delete trayIcon;
if (manager->isConnectedToDBUS())
{
@ -125,114 +107,109 @@ TDEBluetoothApp::~TDEBluetoothApp()
adapters.clear();
}
delete manager;
if (m_config->isDirty())
m_config->sync();
delete m_config;
}
bool TDEBluetoothApp::startAuthAgent()
{
authAgent = new TQProcess();
authAgent->addArgument("tdebluezauth");
authAgent->addArgument("--nofork");
if (!authAgent->start())
kdDebug() << k_funcinfo << endl;
if (findPidOf("tdebluezauth") > 0)
return true;
TQProcess authAgent(TQString("tdebluezauth"), this);
if (!authAgent.start())
return false;
TQObject::connect(authAgent, TQT_SIGNAL(processExited()), this,
TQT_SLOT(slotprocresult()));
return true;
}
bool TDEBluetoothApp::stopAuthAgent()
{
if (authAgent)
{
TQObject::disconnect(authAgent, 0, 0, 0);
int pid = authAgent->processIdentifier();
if (authAgent->isRunning())
{
authAgent->tryTerminate();
wait(&pid);
}
if (authAgent->isRunning())
return false;
else
authAgent = 0;
}
return true;
kdDebug() << k_funcinfo << endl;
if (findPidOf("tdebluezauth") == 0)
return true;
DCOPClient *client = kapp->dcopClient();
if (client>0)
{
TQByteArray data;
return client->send("tdebluezauth","MainApplication-Interface","quit()",data);
}
return false;
}
bool TDEBluetoothApp::startObexSrv()
{
obexServer = new TQProcess();
obexServer->addArgument("/usr/lib/bluetooth/obexd");
// obexServer->addArgument("--nofork");
if (!obexServer->start())
kdDebug() << k_funcinfo << endl;
if (findPidOf("obexd") > 0)
return true;
TQProcess obexServer(TQString("/usr/lib/bluetooth/obexd"));
if (!obexServer.start())
return false;
TQObject::connect(obexServer, TQT_SIGNAL(processExited()), this,
TQT_SLOT(slotprocresult()));
return true;
}
bool TDEBluetoothApp::stopObexSrv()
{
if (obexServer)
{
TQObject::disconnect(obexServer, 0, 0, 0);
int pid = obexServer->processIdentifier();
if (obexServer->isRunning())
{
obexServer->tryTerminate();
wait(&pid);
}
if (obexServer->isRunning())
return false;
else
obexServer = 0;
}
return true;
kdDebug() << k_funcinfo << endl;
TQ_UINT32 pid = findPidOf("obexd");
if (pid == 0)
return true;
FILE *fp;
fp = popen(TQFile::encodeName(TQString("kill %1").arg(pid)), "r");
if(!fp){
tqDebug("Could not open pipe for output.");
return false;
}
if (pclose(fp) == -1)
tqDebug("Error: Failed to close kill stream");
return true;
}
void TDEBluetoothApp::slotprocresult() {
TQ_UINT32 TDEBluetoothApp::findPidOf(TQString name) {
kdDebug() << k_funcinfo << endl;
FILE *fp;
char buffer[6];
fp = popen(TQFile::encodeName("pidof " + name), "r");
if(!fp){
tqDebug("Could not open pipe for output.");
return false;
}
TQString readResult;
while (fgets(buffer, sizeof(buffer), fp) != NULL) {
readResult.append(buffer);
}
if (pclose(fp) == -1)
tqDebug("Error: Failed to close pidof stream");
kdDebug() << "PID: " << readResult.toUInt() << endl;
return readResult.toUInt();
}
//
//void TDEBluetoothApp::slotprocresult() {
// kdDebug() << k_funcinfo << endl;
//}
void TDEBluetoothApp::slotAdapterAdded(const TQString &adapter)
{
AdapterImpl *a = new AdapterImpl("org.bluez", adapter);
a->setConnection((*(manager->getConnection())));
adapters.insert(adapter, a);
if (!authAgent || !authAgent->isRunning())
startAuthAgent();
if (!obexServer || !obexServer->isRunning())
startObexSrv();
}
void TDEBluetoothApp::slotAdapterRemoved(const TQString &adapter)
{
adapters.remove(adapter);
if (adapters.isEmpty())
{
if (authAgent->isRunning())
stopAuthAgent();
if (obexServer->isRunning())
stopObexSrv();
}
}
// TODO: run agents only if enabled in config
//void TDEBluetoothApp::slotPowerOnChanged(const TQString &adapter, bool state) {
// kdDebug() << k_funcinfo << endl;
// if(state) {
// if (!authAgent->isRunning())
// startAuthAgent();
// }
// else {
// if (authAgent->isRunning())
// stopAuthAgent();
// }
//}
void TDEBluetoothApp::slotDeviceAdded(const TQString &device)
{
DeviceImpl *d = new DeviceImpl("org.bluez", device);

@ -51,24 +51,25 @@ public:
bool stopAuthAgent();
bool startObexSrv();
bool stopObexSrv();
bool stopAgent(const TQString name);
TrayIcon *trayIcon;
ObjectManagerImpl *manager;
AdaptersMap adapters;
DevicesMap devices;
private:
TQ_UINT32 findPidOf(TQString name);
TrayIcon *trayIcon;
public slots:
void slotAdapterAdded(const TQString& adapter);
void slotAdapterRemoved(const TQString& adapter);
// void slotPowerOnChanged(const TQString&, bool);
void slotDeviceAdded(const TQString& device);
void slotDeviceRemoved(const TQString& device);
void slotprocresult();
// void slotprocresult();
private:
TDEConfig *m_config;
TQProcess *authAgent;
TQProcess *obexServer;
};
#endif // APPLICATION_H_

@ -42,13 +42,29 @@ TrayIcon::TrayIcon(TDEBluetoothApp* app)
manager = app->manager;
// iconState = IDLE;
TDEConfig *config = TDEGlobal::config();
m_config = TDEGlobal::config();
// config->setGroup("UI");
// alwaysVisible = config->readBoolEntry("alwaysVisible", true);
config->setGroup("General");
int agentstate = config->readBoolEntry("authAgent", true);
int obexserver = config->readBoolEntry("obexSrv", true);
int autostart = config->readBoolEntry("AutoStart", true);
m_config->setGroup("General");
bool agentstate = m_config->readBoolEntry("authAgent", true);
bool obexserver = m_config->readBoolEntry("obexSrv", true);
bool autostart = m_config->readBoolEntry("AutoStart", true);
if (agentstate)
{
if (!app->startAuthAgent())
tqWarning("TDEBluezAuth failed to start.");
}
if (obexserver)
{
if (!app->startObexSrv())
{
tqWarning("TDEBluezOBEX failed to start.");
}
// else
// connect(trayIcon, SIGNAL(setObexSrv(bool)), this, SLOT(slotObexSrv(bool)));
}
blinkTimer = new TQTimer(this);
connect(blinkTimer, SIGNAL(timeout()), this, TQT_SLOT(updateIcon()));
@ -439,10 +455,8 @@ void TrayIcon::updateIcon()
void TrayIcon::slotIconAutostartToggled(bool state)
{
kdDebug() << k_funcinfo << endl;
TDEConfig *config = TDEGlobal::config();
config->setGroup("General");
config->writeEntry("AutoStart", state);
m_config->setGroup("General");
m_config->writeEntry("AutoStart", state);
}
void TrayIcon::slotServiceConfig()
@ -510,16 +524,17 @@ void TrayIcon::slotObexSrv(bool state)
{
kdDebug() << k_funcinfo << endl;
TDEConfig *config = TDEGlobal::config();
config->setGroup("General");
m_config->setGroup("General");
m_config->writeEntry("obexSrv", state);
// kdDebug() << "Dirty config" << m_config->isDirty() << endl;
// if (m_config->isDirty())
// m_config->sync();
if (state)
{
if (!app->startObexSrv())
{
// TODO: pop up warning
}
else
config->writeEntry("obexSrv", state);
}
else
{
@ -527,23 +542,23 @@ void TrayIcon::slotObexSrv(bool state)
{
// TODO: pop up warning
}
else
config->writeEntry("obexSrv", state);
}
}
void TrayIcon::slotIconAgentToggled(bool state)
{
TDEConfig *config = TDEGlobal::config();
config->setGroup("General");
kdDebug() << k_funcinfo << state << endl;
m_config->setGroup("General");
m_config->writeEntry("authAgent", state);
// kdDebug() << "Dirty config" << m_config->isDirty() << endl;
// if (m_config->isDirty())
// m_config->sync();
if (state)
{
if (!app->startAuthAgent())
{
// TODO: pop up warning
}
else
config->writeEntry("authAgent", state);
}
else
{
@ -551,8 +566,6 @@ void TrayIcon::slotIconAgentToggled(bool state)
{
// TODO: pop up warning
}
else
config->writeEntry("authAgent", state);
}
}
@ -648,28 +661,30 @@ void TrayIcon::mousePressEvent(TQMouseEvent *e)
void TrayIcon::slotQuitSelected()
{
if (autostartIconAction->isChecked())
return;
if (autostartIconAction->isChecked()) {
if (m_config->isDirty())
m_config->sync();
return;
}
// Ask if the user want to simply quit or disable
// automatic start of TDEBluetooth
int autoStart = KMessageBox::questionYesNoCancel(0,
i18n("Should TDEBluetooth still be restarted when you login?"),
i18n("Automatically Start TDEBluetooth?"), i18n("Start"),
i18n("Do Not Start"));
TDEConfig *config = TDEGlobal::config();
config->setGroup("General");
m_config->setGroup("General");
if (autoStart == KMessageBox::Yes)
{
config->writeEntry("AutoStart", true);
m_config->writeEntry("AutoStart", true);
}
else if (autoStart == KMessageBox::No)
{
config->writeEntry("AutoStart", false);
}
else
{
return;
m_config->writeEntry("AutoStart", false);
}
if (m_config->isDirty())
m_config->sync();
}
void TrayIcon::slotShowHelp()
@ -717,12 +732,21 @@ void TrayIcon::slotAddAdapter(const TQString& path)
connect(ad_a, SIGNAL(toggled(bool)), this, SLOT(slotPowerOn(bool)));
showPowerOnActionMap.insert(path, ad_a);
setTrayIconAdapterState(path, name, powered);
if (obexIconAction->isChecked()) {
app->startObexSrv();
}
}
void TrayIcon::slotRemoveAdapter(const TQString& path)
{
showPowerOnActionMap.remove(path);
iconAdapterChanged();
if (showPowerOnActionMap.isEmpty())
{
app->stopObexSrv();
}
}
void TrayIcon::iconAdapterChanged()

@ -110,15 +110,7 @@ protected:
void mousePressEvent(TQMouseEvent *e);
private:
// bool obexserv;
// bool alwaysVisible;
// bool autostart;
// bool agentstate;
bool acceptClose;
// bool noAdapter;
// bool adapterRemoved;
// bool isPowered;
KPixmap iconIdle;
KPixmap iconConnected;
KPixmap iconNoAdapter;
@ -147,6 +139,7 @@ private:
TDEActionMenu *helpActionMenu;
TDEBluetoothApp* app;
TDEConfig *m_config;
KHelpMenu *helpMenu;
@ -160,7 +153,7 @@ private:
void setTrayIconAdapterState(const TQString &adapter, const TQString &name,
bool state);
signals:
signals:
// void setObexSrv(bool);
void setPowerOn(bool);

@ -22,7 +22,6 @@
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <tqdbusobjectpath.h>
#include <adapterImpl.h>
@ -36,14 +35,17 @@ TDEBluezAuth::TDEBluezAuth() :
KUniqueApplication()
{
authAgent = 0;
manager = new TDEBluetooth::ObjectManagerImpl("org.bluez", "/", this, "ObjectManager");
if (!manager->isConnectedToDBUS()) {
tqDebug( "ObjectManager is not connected to DBus" );
return;
}
// init session connection to dbus
if (!initDBUS())
if (!initDBUS()) {
tqDebug( "Failed to initialize the connection to DBus" );
exit();
}
}
TDEBluezAuth::~TDEBluezAuth()
@ -99,6 +101,7 @@ bool TDEBluezAuth::initDBUS(){
"The object will only be addressable through unique name '%s'",
DBUS_AUTH_SERVICE_NAME,
m_connection.uniqueName().local8Bit().data());
return false;
}
authAgent = new AuthAgent(this->manager);

@ -61,7 +61,7 @@ int main(int argc, char **argv)
TDEAboutData::License_GPL,
"(C) 2004 Fred Schaettgen", 0, 0,
"kdebluetooth@schaettgen.de");
about.addAuthor("Emanoil Kotsev", I18N_NOOP("Bluez5 and port to TDE"), "emanoil.kotsev@fincom.at");
about.addAuthor("Emanoil Kotsev", I18N_NOOP("Bluez5 and port to TDE"), "deloptes@gmail.com");
TDECmdLineArgs::init(argc, argv, &about);
TDECmdLineArgs::addCmdLineOptions(options);
TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();

@ -9,19 +9,5 @@
#
#################################################
include_directories(
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_BINARY_DIR}
${CMAKE_SOURCE_DIR}
${CMAKE_SOURCE_DIR}/src/libtdebluez
${CMAKE_SOURCE_DIR}/src/tdeioslave/bluetooth
# ${CMAKE_BINARY_DIR}/src
${CMAKE_BINARY_DIR}/src/tdeioslave/bluetooth
${TDE_INCLUDE_DIR}
${TQT_INCLUDE_DIRS}
${DBUS_INCLUDE_DIRS}
${DBUS_TQT_INCLUDE_DIRS}
)
add_subdirectory( bluetooth )
add_subdirectory( obex )

@ -9,6 +9,17 @@
#
#################################################
include_directories(
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_BINARY_DIR}
${CMAKE_SOURCE_DIR}
${CMAKE_SOURCE_DIR}/src/libtdebluez
${CMAKE_SOURCE_DIR}/src/tdeioslave/bluetooth
${TDE_INCLUDE_DIR}
${TQT_INCLUDE_DIRS}
${DBUS_INCLUDE_DIRS}
${DBUS_TQT_INCLUDE_DIRS}
)
##### tdeio_bluetooth (static) #############################
set( target tdeio_bluetooth )

@ -47,10 +47,9 @@ private:
TDEBluetooth::ObjectManagerImpl *m;
private slots:
void slotAddDevice(const TQString &address/*, int devclass, short rssi*/);
void slotAddDevice(const TQString &address);
void slotAddService(const KURL &url, const TQString uuid);
void slotRemoveDevice(const TQString &address/*, int devclass, short rssi*/);
void slotRemoveDevice(const TQString &address);
void slotAdapterPowerOnChanged(const TQString & path, bool state) ;
};

@ -9,7 +9,6 @@
#
#################################################
include_directories(
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_BINARY_DIR}

正在加载...
取消
保存