Logo Search packages:      
Sourcecode: kdebluetooth version File versions

trayicon.cpp

/*
 *
 *  KBluetooth4 - KDE Bluetooth Framework
 *
 *  Copyright (C) 2008  Tom Patzig <tpatzig@suse.de>
 *
 *  This file is part of kbluetooth4.
 *
 *  kbluetooth4 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 2 of the License, or
 *  (at your option) any later version.
 *
 *  kbluetooth4 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 kbluetooth4; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
*/


#include "trayicon.h"
#include <KDebug>
#include <KApplication>

KBlueTray::KBlueTray(QObject* parent) : QObject(parent)
{
       modes << "Off" << "Discoverable" << "Connectable" ;  
       adapter = 0;
       authagent = 0;
       passagent = 0;
       
       setParent(kapp);

       tray = new QSystemTrayIcon(this);
       tray->setIcon(KIcon("preferences-system-bluetooth"));

       mainmenu = new QMenu();

//     kbluelockMenu = mainmenu->addMenu(KIcon("system-lock-screen"),i18n("KBlueLock"));
       kbluelockMenu = mainmenu->addMenu(KIcon("preferences-desktop-user-password"),i18n("KBlueLock"));
       

       lockEnableAction = kbluelockMenu->addAction(KIcon("system-lock-screen"),i18n("Enable"));
       lockEnableAction->setCheckable(true);
       lockEnableAction->setChecked(false);
       connect(lockEnableAction, SIGNAL(triggered(bool)), this, SLOT(enableLock()));

       lockConfigureAction = kbluelockMenu->addAction(KIcon("configure"), i18n("Configure..."));
       connect(lockConfigureAction,SIGNAL(triggered(bool)), this, SLOT(slotConfigureKBlueLock()));
       lockConfigureAction->setEnabled(false);

       sendToAction = mainmenu->addAction(KIcon("text-directory"), i18n("Send File"));
       connect(sendToAction, SIGNAL(triggered(bool)), this, SLOT(sendFile()));

       wizardAction = mainmenu->addAction(KIcon("preferences-system-bluetooth"), i18n("Device Manager"));
       connect(wizardAction, SIGNAL(triggered(bool)), this, SLOT(showManager()));
      
       adapterAction = mainmenu->addAction(KIcon("configure"), i18n("Bluetooth Adapters"));
       connect(adapterAction, SIGNAL(triggered(bool)), this, SLOT(showAdapterInterface()));

       aboutAction = mainmenu->addAction(KIcon("folder-remote"),i18n("About"));
       connect(aboutAction, SIGNAL(triggered(bool)), this, SLOT(showAboutInfo()));

       quitAction = mainmenu->addAction(KIcon("application-exit"),i18n("Quit"));
       connect(quitAction, SIGNAL(triggered(bool)), this, SLOT(slotQuitApp(bool)));
       
       tray->setContextMenu(mainmenu);

       Solid::Control::BluetoothManager &man = Solid::Control::BluetoothManager::self();
         dbus = new QDBusConnection("dbus");



       connect(&man,SIGNAL(interfaceAdded(const QString&)),this,SLOT(adapterAdded(const QString&)));
       connect(&man,SIGNAL(interfaceRemoved(const QString&)),this,SLOT(adapterRemoved(const QString&)));
       connect(&man,SIGNAL(defaultInterfaceChanged(const QString&)),this,SLOT(defaultAdapterChanged(const QString&)));

       if ( man.bluetoothInterfaces().size() > 0 ) {
            onlineMode(man);
      
       } else {
            offlineMode();
            

       }

       config = new KConfig("kbluetoothrc");
       confGroup = new KConfigGroup(config,"General");
/*
       if (confGroup->exists()) {
            avahiClient->receiver->setTargetPath(confGroup->readEntry("Path",avahiClient->receiver->getTargetPath()));
            avahiClient->setUserName(confGroup->readEntry("Nick",avahiClient->getUserName()));
       }
*/
      

      tray->setVisible(true);
      adapterConfig = 0;
      session = 0;
      progress = 0;
      devSelector = 0;
      lockSelector = 0;
      kblueLockEnabled = false;
      
}

KBlueTray::~KBlueTray()
{

      if (adapter) {
            if ( adapter->isPeriodicDiscoveryActive() ) 
                  adapter->stopPeriodicDiscovery();
            delete adapter;
      }

      if (session)
            delete session;
      if (progress)
            delete progress;
      if (devSelector)
//          delete devSelector;
      if (lockSelector)
//          delete lockSelector;
      
      delete mainmenu;
      delete confGroup;
      delete config;
      delete tray;
      
      qDebug() << "bye bye";

}

void KBlueTray::onlineMode(Solid::Control::BluetoothManager& man)
{
      kDebug() << "online Mode";

      secure = man.security();
      authagent = new AuthAgent(this);
      secure->setAuthorizationAgent(authagent);
      passagent = new PasskeyAgent(this);
      secure->setPasskeyAgent(passagent);

      online = true;

      adapter = new Solid::Control::BluetoothInterface(man.defaultInterface());
      connect(adapter,SIGNAL(nameChanged(const QString&)),this,SLOT(slotNameChanged(const QString&)));
      connect(adapter,SIGNAL(modeChanged(Solid::Control::BluetoothInterface::Mode)),this,SLOT(slotModeChanged(Solid::Control::BluetoothInterface::Mode)));
      connect(adapter,SIGNAL(remoteDeviceConnected(const QString&)),this,SLOT(slotRemoteDeviceConnected(const QString&)));
      connect(adapter,SIGNAL(remoteDeviceDisconnected(const QString&)),this,SLOT(slotRemoteDeviceDisconnected(const QString&)));


      toolTipInfo =     "<b>Name:</b> " + adapter->name() + "<br/>" \
                  +     "<b>Address:</b> " + adapter->address() + "<br/>" \
                  +           "<b>Mode:</b> " + modes.at(adapter->mode()); 

      QString lockDisabled = toolTipInfo + "<br/>" + "<b>KBlueLock:</b> Disabled"; 
      tray->setToolTip(lockDisabled);
      
      sendToAction->setEnabled(true);
      wizardAction->setEnabled(true);
      adapterAction->setEnabled(true);
      kbluelockMenu->setEnabled(true);


}

void KBlueTray::offlineMode()
{
      kDebug() << "offline Mode";
      online = false;

      sendToAction->setEnabled(false);
      wizardAction->setEnabled(false);
      adapterAction->setEnabled(false);
      kbluelockMenu->setEnabled(false);
      
      tray->setToolTip("No Bluetooth Adapter");
            
      if (authagent)
            delete authagent;
      if (passagent)
            delete passagent;
      if (adapter) {
            disconnect(adapter,0,0,0);
            delete adapter;
      }
}

void KBlueTray::adapterAdded(const QString& ubi)
{
       kDebug() << "adapterAdded";

       if ( !online ) {
            kDebug() << "new default adapter UBI: " << ubi;
            onlineMode(Solid::Control::BluetoothManager::self());
       }
}

void KBlueTray::adapterRemoved(const QString& ubi)
{
       kDebug() << "adapterRemoved";
       Solid::Control::BluetoothManager &man = Solid::Control::BluetoothManager::self();
       if (man.bluetoothInterfaces().size() == 0) {
            offlineMode();
            kDebug() << "default Adapter removed";
       }
}

void KBlueTray::defaultAdapterChanged(const QString& ubi)
{
      kDebug() << "default Adapter changed " << ubi;
      defaultAdapterUBI = ubi;

}

void KBlueTray::slotQuitApp(bool set)
{
        kapp->quit();
}

void KBlueTray::showAboutInfo()
{
        KAboutData aboutData("kbluetooth4", 0, ki18n("KBluetooth4"), "0.1",
                           ki18n("KDE4 Bluetooth Framework"), KAboutData::License_GPL_V2);
        aboutData.addAuthor(ki18n("Tom Patzig"), KLocalizedString(), "tpatzig@suse.de", "http://www.kde.org/");
        aboutData.setProgramLogo(KIcon("preferences-system-bluetooth"));

        KAboutApplicationDialog about(&aboutData, mainmenu);
//        about.setIcon( KIcon( "preferences-system-bluetooth" ) );
        about.exec();
}

void KBlueTray::showAdapterInterface()
{
      if (!adapterConfig)
            adapterConfig = new AdapterConfig(this);

      adapterConfig->initialize();

}

void KBlueTray::showWizard()
{
      BTWizard wiz;
      wiz.exec();
}

void KBlueTray::showManager()
{
//    DeviceMan manager;
      kDebug() << "Starting Device Manager";
      KProcess process;
      process.setProgram("kbluetooth4-devicemanager");
      process.startDetached();

      
//    manager.exec();
}

void KBlueTray::sendFile()
{
      QString fileName = KFileDialog::getOpenFileName(KUrl("./"),"*",mainmenu, tr("Select File"));
        if (!fileName.isEmpty()) {
            fileToSend = fileName;
            devSelector = new DeviceSel(this,QString("computer,phone").split(','));
            connect(devSelector,SIGNAL(deviceSelected(const QString&,const QString&)),this,SLOT(slotSendFile(const QString&,const QString&)));

        }


}

void KBlueTray::slotSendFile(const QString& mac,const QString& /* ubi */ )
{
      session = new ObexSession(this,mac,"ftp");
      
      connect(session,SIGNAL(connected()),this,SLOT(obexSessionReady()));

}

void KBlueTray::obexSessionReady()
{
      kDebug() << "is connected: " << session->isConnected();

      session->sendFile(fileToSend);
      
      connect(session,SIGNAL(transferStarted(const QString&,const QString&,qulonglong)),this,SLOT(slotFileSendStarted(const QString,const QString&,qulonglong)));
      connect(session,SIGNAL(transferProgress(qulonglong)),this,SLOT(slotTransferProgress(qulonglong)));
      connect(session,SIGNAL(transferCompleted()),this,SLOT(slotFileSendFinished()));

}

void KBlueTray::slotFileSendStarted(const QString& filename ,const QString& path ,qulonglong size)
{
      totalFileSize = size;

      kDebug() << "file send started: " << path << filename;
      kDebug() << "size: " << size;

      progress = new QProgressDialog(i18n("Sending ") + filename,i18n("Cancel"),0,100);
      progress->setAutoClose(true);
      progress->setWindowIcon(KIcon("preferences-system-bluetooth"));
      progress->show();
      connect(progress,SIGNAL(canceled()),this,SLOT(slotFileSendCanceled()));
      
}

void KBlueTray::slotTransferProgress(qulonglong val)
{
      kDebug() << "Transfered: " << val;

            
      qulonglong prog = ( ( val * 100 ) / totalFileSize) ;

      kDebug() << "Progress: " << prog;

      progress->setValue(prog);

}

void KBlueTray::slotFileSendFinished()
{
      kDebug() << "Transfer completed";

      session->disconnect();
      session->close();

      delete session;
      session = 0;

}

void KBlueTray::slotFileSendCanceled()
{
      session->cancel();
      session->disconnect();
      session->close();

      delete session;
      delete devSelector;

}

void KBlueTray::enableLock()
{
      if (lockEnableAction->isChecked() ) {
            if (lockDevice.isEmpty()) {
                  lockSelector = new DeviceSel(this,QString("phone").split(','));
                  lockSelector->setOkButtonText("Select");
                  connect(lockSelector,SIGNAL(deviceSelected(const QString&,const QString&)),this,SLOT(enableBlueLock(const QString&,const QString&)));
                  connect(lockSelector,SIGNAL(selectorCancelled()),this,SLOT(slotBlueSelectorCancelled()));
            } else 
                  enableBlueLock("",lockDevice);

      } else 
            disableBlueLock();
}


void KBlueTray::enableBlueLock(QString  mac ,const QString& ubi)
{
      lockDevice = ubi;
      if (!lockDevice.isEmpty()) {

            if (mac.isEmpty())
                  mac = ubi.split('/').at(4);

            lockConfigureAction->setEnabled(true);

            kDebug() << "KBlueLock enabled";
            kDebug() << "lockdevice: " << lockDevice;
            if (! adapter->isPeriodicDiscoveryActive()) {
                  adapter->startPeriodicDiscovery();
                  kDebug() << "Periodic Discovery Started";
                  connect(adapter,SIGNAL(remoteDeviceFound(const QString&,int,int)),this,SLOT(slotTargetDeviceFound(const QString&,int,int)));
            }

            connect(adapter,SIGNAL(remoteDeviceDisappeared(const QString)),this,SLOT(slotValidateLockDev(const QString&)));
            
            QString lockEnabled = toolTipInfo + "<br/>" + "<b>KBlueLock:</b> Enabled for " + adapter->getRemoteName(mac); 
            tray->setToolTip(lockEnabled);
            
            
      } else 
            lockEnableAction->setChecked(false);

}

void KBlueTray::disableBlueLock()
{

      kDebug() << "KBlueLock disabled";
      kDebug() << "lockdevice: " << lockDevice;
      
      lockConfigureAction->setEnabled(false);

      if ( adapter->isPeriodicDiscoveryActive() ) {
            adapter->stopPeriodicDiscovery();
            kDebug() << "Periodic Discovery Stopped";

      }
      disconnect(adapter,SIGNAL(remoteDeviceDisappeared(const QString)),this,SLOT(slotValidateLockDev(const QString&)));
      disconnect(adapter,SIGNAL(remoteDeviceFound(const QString&,int,int)),this,SLOT(slotTargetDeviceFound(const QString&,int,int)));
      QString lockDisabled = toolTipInfo + "<br/>" + "<b>KBlueLock:</b> Disabled"; 
      tray->setToolTip(lockDisabled);
      kblueLockEnabled = false;


}

void KBlueTray::slotValidateLockDev(const QString& ubi)
{
      if (ubi == lockDevice) {
            
            kDebug() << "KBlueLock armed -- Lock Device disappeared";
            QDBusConnection dbusconn = dbus->connectToBus(QDBusConnection::SessionBus, "dbus");
            QString service = "org.freedesktop.ScreenSaver";
              QString path = "/ScreenSaver";
            QString method = "Lock";
            QString iface = "org.freedesktop.ScreenSaver";

            QDBusInterface* interface = new QDBusInterface(service,path,iface,dbusconn);
            interface->call(method);
      }
}


void KBlueTray::slotConfigureKBlueLock()
{
            if ( adapter->isPeriodicDiscoveryActive() ) 
                  adapter->stopPeriodicDiscovery();
                  kDebug() << "Periodic Discovery Stopped";
                  

            lockSelector = new DeviceSel(this,QString("phone").split(','));
            lockSelector->setOkButtonText("Select");
            connect(lockSelector,SIGNAL(deviceSelected(const QString&,const QString&)),this,SLOT(enableBlueLock(const QString&,const QString&)));
            connect(lockSelector,SIGNAL(selectorCancelled()),this,SLOT(slotBlueSelectorCancelled()));

}


void KBlueTray::slotBlueSelectorCancelled()
{

      disconnect(adapter,SIGNAL(remoteDeviceDisappeared(const QString)),this,SLOT(slotValidateLockDev(const QString&)));
      disconnect(adapter,SIGNAL(remoteDeviceFound(const QString&,int,int)),this,SLOT(slotTargetDeviceFound(const QString&,int,int)));
      kblueLockEnabled = false;
      enableBlueLock("",lockDevice);

}
void KBlueTray::slotTargetDeviceFound(const QString& ubi,int /*dclass*/,int /*rssi*/)
{
      if ((!kblueLockEnabled) && (ubi == lockDevice)) {
            kDebug() << "Target device found - Kbluelock ready";
            tray->showMessage("KBluetooth","kbluelock is enabled now");
            kblueLockEnabled = true;
      }

}

void KBlueTray::slotModeChanged(Solid::Control::BluetoothInterface::Mode mode)
{
      kDebug() << "Mode Changed";   

      QString modeTip;
      QString toolTip =       "<b>Name:</b> " + adapter->name() + "<br/>" \
                  +     "<b>Address:</b> " + adapter->address() + "<br/>" \
                  +           "<b>Mode:</b> " + modes.at(mode); 

      toolTipInfo = toolTip;

      if (!kblueLockEnabled)
            modeTip = toolTipInfo + "<br/>" + "<b>KBlueLock:</b> Disabled"; 
      else
            modeTip = toolTipInfo + "<br/>" + "<b>KBlueLock:</b> Enabled for " + adapter->getRemoteName(adapter->address()); 

      tray->setToolTip(modeTip);


}

void KBlueTray::slotNameChanged(const QString& name)
{
      kDebug() << "Name changed to " << name;

      QString nameTip;
      QString toolTip =       "<b>Name:</b> " + name + "<br/>" \
                  +     "<b>Address:</b> " + adapter->address() + "<br/>" \
                  +           "<b>Mode:</b> " + modes.at(adapter->mode()); 

      toolTipInfo = toolTip;

      if (!kblueLockEnabled)
            nameTip = toolTipInfo + "<br/>" + "<b>KBlueLock:</b> Disabled"; 
      else
            nameTip = toolTipInfo + "<br/>" + "<b>KBlueLock:</b> Enabled for " + adapter->getRemoteName(adapter->address()); 

      tray->setToolTip(nameTip);
      
}

void KBlueTray::slotRemoteDeviceConnected(const QString& address)
{
      kDebug() << "Connection to " << address << "established";
      tray->showMessage("KBluetooth",i18n("Connected to ") + adapter->getRemoteName(address));
}

void KBlueTray::slotRemoteDeviceDisconnected(const QString& address)
{
      kDebug() << "Disconnected from " << address << "lost";
      tray->showMessage("KBluetooth",i18n("Disconnected from ") + adapter->getRemoteName(address));
}


Generated by  Doxygen 1.6.0   Back to index