source: src-qt4/pc-bluetoothtray/btTray.cpp @ 1f0939e

9.1-release9.2-releasereleng/10.0releng/10.0.1
Last change on this file since 1f0939e was 1f0939e, checked in by Kris Moore <kris@…>, 12 months ago
  • Initial merge of code from PC-BSD SVN repo for my private (pre-release) hacking
  • Property mode set to 100644
File size: 4.1 KB
Line 
1
2/* Qt */
3#include <QDebug>
4#include <QFile>
5#include <QImage>
6#include <QMenu>
7#include <QProcess>
8#include <QToolTip>
9#include <QTextStream>
10#include <QTimer>
11#include <QTranslator>
12#include <iostream>
13
14#include "btTray.h"
15#include <pcbsd-hardware.h>
16#include <pcbsd-utils.h>
17
18
19
20/* Update interval in ms */
21#define  UPDATE_MSEC 10000
22
23bool isBTDevice=TRUE;
24bool isFirstRun=TRUE;
25
26void BluetoothTray::programInit()
27{
28 
29  trayIcon = new QSystemTrayIcon(this);
30 
31  // Connect our double-click slot and message clicked slot
32  connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(slotTrayActivated(QSystemTrayIcon::ActivationReason)));
33  connect(trayIcon, SIGNAL( messageClicked() ), this,SLOT( startBluetoothManager() )); 
34  trayIcon->setIcon(QIcon(":inactiveBluetooth.png"));
35  trayIcon->show();
36 
37  //Start the service on a refresh loop
38  QTimer::singleShot(1000,this,SLOT(refreshTray() ));
39  qDebug() << "pc-bluetoothtray: starting up";
40}
41
42bool BluetoothTray::checkForBtDevices(){
43  bool oldStatus = isBTDevice;
44  //Run a couple quick checks to see if a bluetooth device is plugged in
45  QStringList result = Utils::listShellCommandSearch("ngctl list","ubt0");
46  if( result.isEmpty()){ isBTDevice=FALSE; } //no device has ever been plugged in since compupter turned on
47  //else if( result[result.length()-1].contains("disconnected") ){ isBTDevice=FALSE; } //last entry says the device has been removed
48  else{ isBTDevice=TRUE; }
49  //Determine if the status has changed
50  bool status = (oldStatus != isBTDevice);   
51  //Determine if the device is a new detection (but not the first time the computer is turned on)
52  if(status && isBTDevice && !isFirstRun){
53    //Show the popup message about a new device being discovered is not the first run after startup
54    trayIcon->showMessage( tr("New Bluetooth Device Detected"),tr("Click here to configure Bluetooth device configurations"),QSystemTrayIcon::NoIcon,5000);
55  }
56
57  if(isFirstRun){
58    status = TRUE;
59    isFirstRun=FALSE; 
60  }
61  return status; //TRUE if device status changed from previous
62
63}
64
65void BluetoothTray::refreshTray(){
66  //---DO NOT CALL THIS FUNCTION MANUALLY: AUTOMATICALLY REFRESHES---
67  //only redo the Tray if the device status has changed -- allows for lower memory footprint on refresh
68  if( checkForBtDevices() ){ 
69    trayIconMenu = new QMenu;
70    trayIconMenu->clear();
71    if(isBTDevice){ //device currently connected
72      trayIcon->setIcon(QIcon(":activeBluetooth.png")); //set proper tray icon
73      //Setup the menu options
74      trayIconMenu->addAction( tr("Start Bluetooth Manager"), this, SLOT(startBluetoothManager()));
75      trayIconMenu->addAction( tr("Restart Bluetooth Services"), this, SLOT(restartBluetooth()));
76      trayIconMenu->addAction( tr("Close Bluetooth Tray"), this, SLOT(closeTray()));
77    }else{  //no device connected
78      trayIcon->setIcon(QIcon(":inactiveBluetooth.png")); //set proper tray icon
79      //Setup the menu options
80      trayIconMenu->addAction( tr("Close Bluetooth Tray"), this, SLOT(closeTray()));
81    }
82    //Apply the menu to the Tray
83    trayIcon->setContextMenu(trayIconMenu);
84  }
85  QTimer::singleShot(UPDATE_MSEC,this,SLOT(refreshTray() ));
86}
87
88void BluetoothTray::slotTrayActivated(QSystemTrayIcon::ActivationReason reason) {
89   if(reason == QSystemTrayIcon::DoubleClick) {
90       startBluetoothManager();
91   }else{
92     //show the context menu
93     trayIcon->contextMenu()->popup(QCursor::pos());
94   }
95}
96
97void BluetoothTray::startBluetoothManager(){
98  qDebug() << "pc-bluetoothtray: Starting Bluetooth Manager";
99  //Start bluetooth manager completely seperate from the tray app
100  QString cmd = "pc-bluetoothmanager &"; //might need to be "sudo ..."
101  Utils::runShellCommand(cmd);
102}
103
104void BluetoothTray::restartBluetooth(){
105  qDebug() << "pc-bluetoothtray: Restarting Bluetooth Services";
106  trayIcon->showMessage( tr("Please Wait"),tr("Restarting Bluetooth Services"),QSystemTrayIcon::NoIcon,5000); 
107  //Restart/reconnect the bluetooth services/devices
108  Hardware::restartBT();
109}
110
111void BluetoothTray::closeTray(){
112  qDebug() << "pc-bluetoothtray: closing down";
113  //Close down the application
114  exit(0);
115}
116
117void BluetoothTray::slotSingleInstance()
118{
119  trayIcon->show();
120}
Note: See TracBrowser for help on using the repository browser.