source: src-qt4/pc-bluetoothmanager/btmaingui.cpp @ 1f0939e

9.1-release9.2-releasereleng/10.0releng/10.0.1releng/10.0.2releng/10.0.3
Last change on this file since 1f0939e was 1f0939e, checked in by Kris Moore <kris@…>, 18 months ago
  • Initial merge of code from PC-BSD SVN repo for my private (pre-release) hacking
  • Property mode set to 100644
File size: 13.7 KB
Line 
1#include "btmaingui.h"
2#include "ui_btmaingui.h"
3#include <pcbsd-hardware.h>
4#include <pcbsd-utils.h>
5#include <QDebug>
6#include <QMessageBox>
7
8/* UI handles
9 * Comp settings Tab: pushStartTray   pushChangeCompName  pushDisconnectDevice  pushRestart  lineCompName  listConnectedDevices
10 * New Device Tab:  pushAddDevice  pushScanNew  listNewDevice  newDeviceInfo
11 * Old Device Tab:  pushConfigureOld  pushRemoveOld  listOldDevice oldDeviceInfo pushConnectDevice
12 */
13
14QStringList oldSaveBdaddrList, oldSaveKeyList, oldSavePinList, oldSaveNameList, newSaveBdaddrList, currentSaveDeviceList;
15
16btmaingui::btmaingui(QWidget *parent) :
17    QMainWindow(parent),
18    ui(new Ui::btmaingui)
19{
20    ui->setupUi(this);
21    firstRun();
22}
23
24btmaingui::~btmaingui()
25{
26    delete ui;
27}
28
29void btmaingui::firstRun(){
30  //Setup Signals/Slots
31  connect(ui->pushScanNew,SIGNAL(clicked()),this,SLOT(scanForDevices()));
32  connect(ui->tabWidget,SIGNAL(currentChanged(int)),this,SLOT(tabChanged(int)));
33  connect(ui->pushRemoveOld,SIGNAL(clicked()),this,SLOT(removeOldDevice()));
34  connect(ui->listOldDevice,SIGNAL(currentRowChanged(int)),this,SLOT(updateOldDeviceInfo(int)));
35  connect(ui->listNewDevice,SIGNAL(currentRowChanged(int)),this,SLOT(refreshGUIslot(int)));
36  connect(ui->listConnectedDevices,SIGNAL(currentRowChanged(int)),this,SLOT(refreshGUIslot(int)));
37  connect(ui->pushAddDevice,SIGNAL(clicked()),this,SLOT(addNewDevice()));
38  connect(ui->pushConfigureOld,SIGNAL(clicked()),this,SLOT(configureOldDevice()));
39  connect(ui->pushDisconnectDevice,SIGNAL(clicked()),this,SLOT(disconnectDevice()));
40  connect(ui->pushConnectDevice,SIGNAL(clicked()),this,SLOT(connectDevice()));
41  connect(ui->pushRestart,SIGNAL(clicked()),this,SLOT(restartBluetooth()));
42  connect(ui->pushChangeCompName,SIGNAL(clicked()),this,SLOT(changeCompName()));
43  connect(ui->pushStartTray,SIGNAL(clicked()),this,SLOT(startTrayApplication()));
44  //Start on the system settings tab
45  ui->tabWidget->setCurrentIndex(0);
46  //Load the information for each Tab as needed
47  updateCompInfo(); //Load info for computer settings tab
48  refreshOldDeviceList(); //Load info for the saved devices tab (fill needed arrays)
49  refreshGUI();
50}
51
52void btmaingui::tabChanged(int newTab){
53
54  if(newTab==0){  //Computer Settings
55    updateCompInfo();
56  }else if(newTab==1){  //New Device Tab
57    if(newSaveBdaddrList.isEmpty() ){ refreshGUI(); scanForDevices(); }
58  }else if(newTab==2){  //Old Device Tab
59    refreshOldDeviceList();
60  }else{
61    qDebug() << "BlueToothManager: TabChanged Error: Tab #"<< newTab << " does not exist";
62  }
63  refreshGUI();
64}
65
66void btmaingui::refreshGUI(){
67  //Get the current Tab
68  int tab = ui->tabWidget->currentIndex();
69  refreshGUIslot(tab);
70}
71 
72void btmaingui::refreshGUIslot(int tab){
73  //Setup the dynamic enables/disables for each tab
74  if(tab==0){ //Computer Setting Tab
75    if(ui->listConnectedDevices->currentRow() == -1){ui->pushDisconnectDevice->setEnabled(FALSE);}
76    else{ui->pushDisconnectDevice->setEnabled(TRUE);}
77    ui->pushRestart->setEnabled(TRUE);
78  }else if(tab==1){
79    if(ui->listNewDevice->currentRow() == -1){ui->pushAddDevice->setEnabled(FALSE);}
80    else{ui->pushAddDevice->setEnabled(TRUE);}
81    ui->pushScanNew->setEnabled(TRUE);
82  }else if(tab==2){
83    if(ui->listOldDevice->currentRow() == -1){
84      ui->pushConfigureOld->setEnabled(FALSE);
85      ui->pushRemoveOld->setEnabled(FALSE);
86      ui->pushConnectDevice->setEnabled(FALSE);   
87    }else{
88      ui->pushConfigureOld->setEnabled(TRUE);
89      ui->pushRemoveOld->setEnabled(TRUE);
90      ui->pushConnectDevice->setEnabled(TRUE);
91    }
92   
93  }else{
94    qDebug() << "Attempted to refresh an unknown tab number:" << tab;
95  }
96}
97
98void btmaingui::scanForDevices(){
99   ui->pushScanNew->setEnabled(FALSE);
100   ui->pushAddDevice->setEnabled(FALSE);
101   //Clear the GUI for new results
102   ui->newDeviceInfo->clear();
103   ui->newDeviceInfo->append(tr("Searching for discoverable Bluetooth devices"));
104   ui->listNewDevice->clear();
105   newSaveBdaddrList.clear();
106   //Start scanning and put the results in the listbox
107   QStringList bdaddrList = Hardware::findBTdevices();
108   QStringList connectionList = Utils::runShellCommand("hccontrol read_connection_list");
109   for(int i=0; i<bdaddrList.length(); i++){
110     if( !connectionList.contains(bdaddrList[i]) && !oldSaveBdaddrList.contains(bdaddrList[i]) ){ //Check if it is a new device (not connected or saved)
111       QString name = Hardware::getBTRemoteName(bdaddrList[i],TRUE);
112       newSaveBdaddrList << bdaddrList[i];
113       ui->listNewDevice->addItem(name+" ("+bdaddrList[i]+")"); //Add it to the new device list
114     }
115   }
116   //Clear the info box
117   ui->newDeviceInfo->clear();
118   if(ui->listNewDevice->count()==0){     //No Devices found
119     ui->newDeviceInfo->append(tr("No new Bluetooth devices discovered! Please put your device into \"discovery\" mode and rescan."));
120   }
121   ui->pushScanNew->setEnabled(TRUE);
122   refreshGUI();
123}
124
125void btmaingui::refreshOldDeviceList(){
126  //Clear the list for updating
127  ui->listOldDevice->clear(); 
128  ui->listOldDevice->setCurrentRow(-1);
129  ui->oldDeviceInfo->clear();
130  ui->oldDeviceInfo->append(tr("Reading saved Bluetooth devices. Please wait."));
131  //Make sure there is root permissions to read/write the settings
132  if( !rootPermissions() ){
133    ui->oldDeviceInfo->clear();
134    ui->oldDeviceInfo->append(tr("This program must be run with root permissions to view/edit saved configurations"));
135    return;
136  }
137   
138  //Get all the Old Devices saved in hcsecd.conf and load them into memory
139  QStringList oldBdaddrList = Hardware::readAllSavedBTDevices();
140  oldSaveKeyList.clear(); oldSavePinList.clear(); oldSaveNameList.clear(); oldSaveBdaddrList.clear(); //reset the saved variables
141 
142  //Stop earlier if there is no saved devices
143  if(oldBdaddrList.isEmpty()){
144    ui->oldDeviceInfo->clear();
145    ui->oldDeviceInfo->append(tr("No saved configurations found"));
146    return;
147  }
148  for(int i=0; i<oldBdaddrList.length(); i++){
149    QStringList temp = Hardware::readSavedBTDevice(oldBdaddrList[i]);
150    oldSaveBdaddrList << oldBdaddrList[i];
151    oldSaveNameList << temp[1];
152    oldSaveKeyList << temp[2];
153    oldSavePinList << temp[3];
154  }
155 
156  //Display the name/bdaddr of all the saved devices
157  for(int i=0; i<oldSaveBdaddrList.length();i++){
158    ui->listOldDevice->addItem(oldSaveNameList[i]+" ("+oldSaveBdaddrList[i]+")");
159  }
160  ui->oldDeviceInfo->clear();
161}
162
163void btmaingui::removeOldDevice(){
164  //Get the BDADDR of the selected device
165  int row = ui->listOldDevice->currentRow();
166  if(row == -1){
167   //No device selected
168   QMessageBox::warning(this,tr("Warning"),tr("No device has been selected. Please choose a device first."));
169   return;
170  }
171  QString bdaddr = oldSaveBdaddrList[row];
172  //Remove the Device from hcsecs.conf
173  Hardware::rmBTdevice(bdaddr,TRUE); //restart the network after removing the device
174  //Refresh the Device List
175  refreshOldDeviceList();
176}
177
178void btmaingui::addNewDevice(){
179  //Check for root permissions
180  if( !rootPermissions() ){
181    //No root permissions to save device
182    QMessageBox::warning(this,tr("Invalid Permissions"),tr("This program must be run with root permissions to save device settings"));
183    return;
184  }
185 
186  //Get the BDADDR of the selected device
187  int row = ui->listNewDevice->currentRow();
188  if(row == -1){
189    //No device selected
190    QMessageBox::warning(this,tr("Warning"),tr("No device has been selected. Please choose a device first."));
191    return;
192  }
193  QString bdaddr = newSaveBdaddrList[row];
194 
195  //Ask for PIN and KEY for configuration
196  QStringList requests;
197  requests << tr("PIN Code (Examples: 0000, 1234, or none)") << tr("Link Key (Leave blank for automatic)");
198  QStringList outputs = Utils::quickUserInputBox(tr("Device Configuration"),requests);
199  if( outputs.length() != requests.length() ){ //rememberempty strings are valid
200    //Error/cancelled operation - do nothing
201    qDebug() << "Add new device: Cancelled";
202    return;
203  }
204  QString pin = outputs[0];
205  QString key = outputs[1];
206  //Quick Check for pin validity
207  if( pin.length()!=4 && !pin.isEmpty() ){ 
208    QMessageBox::warning(this,tr("Warning"),tr("Invalid Pin length: Defaulting to none"));
209    pin = "";
210  }
211  if(pin == "none"){ pin=""; }
212 
213  ui->pushAddDevice->setEnabled(FALSE);
214  //Add the entry to hcsecd.conf
215  Hardware::addBTdevice(bdaddr,"",key,pin);
216  //Move to the Saved devices tab
217  ui->tabWidget->setCurrentIndex(2);
218  refreshOldDeviceList();
219  refreshGUI();
220 
221  //Initiate pairing of device
222  QString result = Hardware::pairBTDevice(bdaddr);
223 
224  //Rescan for devices in the background
225  scanForDevices();
226 
227}
228
229void btmaingui::updateCompInfo(){
230  //Clear the display for action
231  ui->listConnectedDevices->clear();
232  //Get computer host/device names and display it
233  QStringList compName = Hardware::getBTdevNames(); //compName[0]=host name; compName[1]=device name
234  ui->lineCompName->setText(compName[0]+" ("+compName[1]+")");
235  //List all active connections (if any)
236  currentSaveDeviceList.clear();
237  QStringList connectionList = Utils::runShellCommand("hccontrol read_connection_list");
238  if(connectionList.length() > 1){
239    for(int i=1; i<connectionList.length(); i++){ //skip the first line (labels)
240      QString bdaddr = connectionList[i].section(" ",0,0,QString::SectionSkipEmpty).simplified();
241      currentSaveDeviceList << bdaddr;
242      QString name = Hardware::getBTRemoteName(bdaddr,TRUE);
243      ui->listConnectedDevices->addItem(name+" ("+bdaddr+")");
244    }
245  }
246}
247
248void btmaingui::updateNewDeviceInfo(){
249 
250}
251
252void btmaingui::updateOldDeviceInfo(int row){
253  //qDebug() << row << oldBdaddrList << oldNameList << oldKeyList << oldPinList;
254  //Display the hcsecd configuration for the selected device
255  ui->oldDeviceInfo->clear();
256  if(row != -1){
257    ui->oldDeviceInfo->append("BD_ADDR: "+oldSaveBdaddrList[row]);
258    ui->oldDeviceInfo->append("Name:    "+oldSaveNameList[row]);
259    ui->oldDeviceInfo->append("Key:     "+oldSaveKeyList[row]);
260    ui->oldDeviceInfo->append("PIN:     "+oldSavePinList[row]);
261  }
262  refreshGUI();
263}
264
265bool btmaingui::rootPermissions(){
266  QString userID = Utils::runShellCommand("id -u").join("");
267  if( userID.toInt() == 0){ return TRUE; }
268  else{ return FALSE; }
269}
270
271void btmaingui::configureOldDevice(){
272  //Get the selected device information
273  int row = ui->listOldDevice->currentRow();
274  if(row == -1){
275    //No device selected
276    QMessageBox::warning(this,tr("Warning"),tr("No device has been selected. Please choose a device first."));
277    return;
278  }
279  QString bdaddr = oldSaveBdaddrList[row];
280 
281  //Get the new PIN and KEY
282  QStringList requests;
283  requests << tr("PIN Code (Examples: 0000, 1234, or none)") << tr("Link Key (Leave blank for automatic)");
284  QStringList outputs = Utils::quickUserInputBox(tr("Device Configuration"),requests);
285  if( outputs.length() != requests.length() ){ //remember empty strings are valid
286    //Error/cancelled operation - do nothing
287    qDebug() << "Configure old device: Cancelled";
288    return;
289  }
290  QString pin = outputs[0];
291  QString key = outputs[1];
292  //Quick Check for pin validity
293  if(pin == "none"){ pin=""; }
294 
295  //Add the entry to hcsecd.conf
296  Hardware::addBTdevice(bdaddr,"",key,pin);
297 
298  //Refresh the Tab
299  refreshOldDeviceList();
300}
301
302void btmaingui::disconnectDevice(){
303  //Get the bdaddr of the selected device
304  int row = ui->listConnectedDevices->currentRow();
305  if(row == -1){
306    //No device selected
307    QMessageBox::warning(this,tr("Warning"),tr("No device has been selected. Please choose a device first."));
308    return;
309  }
310  QString bdaddr = currentSaveDeviceList[row];
311 
312  //Call the disconnect routine
313  Hardware::disconnectBTDevice(bdaddr);
314 
315  //Refresh the Box
316  updateCompInfo();
317}
318
319void btmaingui::connectDevice(){
320  //Get the selected device
321  int row = ui->listOldDevice->currentRow();
322  if(row == -1){
323    //No device selected
324    QMessageBox::warning(this,tr("Warning"),tr("No device has been selected. Please choose a device first."));
325    return;
326  }
327  QString bdaddr = oldSaveBdaddrList[row];
328 
329  //Initiate connection (pairing)
330  QString result = Hardware::pairBTDevice(bdaddr);
331 
332  //Determine if the connection was successful
333  if(!result.isEmpty()){
334    //Error making the connection
335    QMessageBox::warning(this,tr("Pairing Error"),tr("Could not pair to this device. Please verify your device configuration before trying again."));
336    qDebug() << "Bluetooth Connection: " + result; //display the full error on the command line
337    return;
338  }
339 
340  //Connection successful - cleanup GUI
341  ui->tabWidget->setCurrentIndex(0); //move to the Computer settings tab
342  updateCompInfo();
343  refreshGUI();
344 
345}
346
347void btmaingui::restartBluetooth(){
348  ui->pushRestart->setEnabled(FALSE);
349  Hardware::restartBT();
350  //refresh the GUI
351  updateCompInfo();
352  refreshGUI();
353 
354}
355
356void btmaingui::changeCompName(){
357  //Get the desired computer name
358  QStringList requests;
359  requests << tr("New Bluetooth Computer name");
360  QStringList outputs = Utils::quickUserInputBox(tr("New Bluetooth Name"),requests);
361  //Check for invalid names
362  if( outputs.join(" ").simplified().isEmpty() ){ 
363    //Error/cancelled operation - do nothing
364    qDebug() << "Change Computer Name: Cancelled";
365    return;
366  }else if(outputs.join(" ").contains("(") || outputs.join(" ").contains(")") || outputs.join(" ").contains(":") ){
367    QMessageBox::warning(this,tr("Invalid Name"),tr("Device name must not contain symbols"));
368    return;
369  }
370  QString newName = outputs[0];
371 
372  //Retrieve the current device names
373  QStringList oNames = Hardware::getBTdevNames();
374 
375  //Change the current computer name
376  Hardware::setBTdevName(newName,oNames[1]);
377 
378  //Refresh the GUI
379  updateCompInfo();
380 
381}
382
383void btmaingui::slotSingleInstance()
384{
385  this->showNormal();
386  this->activateWindow();
387  this->raise();
388  //this->show();
389}
390
391void btmaingui::startTrayApplication(){
392  //Startup pc-bluetoothtray with sudo
393  QString cmd = "sudo pc-bluetoothtray &";
394  Utils::runShellCommand(cmd);
395}
396
Note: See TracBrowser for help on using the repository browser.