source: src-qt4/pc-bluetoothmanager/btmaingui.cpp @ 0cfe72f

9.1-release9.2-releasereleng/10.0releng/10.0.1releng/10.0.2releng/10.0.3releng/10.1
Last change on this file since 0cfe72f was 0cfe72f, checked in by Kris Moore <kris@…>, 20 months ago

Add all the changes for related using of split pcbsd-utils libs

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