source: SMSSender/gateways/Smartphone/src/ui/vcsettingswidget.cpp @ 278:9353e6aea3ab

new-gtwy-smartphone
Last change on this file since 278:9353e6aea3ab was 278:9353e6aea3ab, checked in by Sämy Zehnder <saemy.zehnder@…>, 7 years ago
  • Implemented encryption for communication with smartphones.
  • Handling connection aborts when smartphone silently vanishes.
  • Moved encryption code to SUtils.
File size: 4.6 KB
Line 
1/*
2  Smartphone plugin - The smssender plugin for connecting to smartphones.
3  Copyright (C) 2012, gorrión. See http://smssender.gorrion.ch
4
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  GNU General Public License for more details.
14
15  You should have received a copy of the GNU General Public License
16  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17*/
18#include "vcsettingswidget.h"
19
20#include <QBitArray>
21#include <QInputDialog>
22#include <QList>
23#include <QString>
24#include <QMessageBox>
25
26#include <qrencode.h>
27
28#include <stdexceptions.h>
29
30#include "business/bcaccount.h"
31#include "business/bcgateway.h"
32#include "business/bcsmartphoneconnector.h"
33#include "business/bcsmartphonemanager.h"
34
35namespace Smartphone {
36
37VCSettingsWidget::VCSettingsWidget(QWidget *parent)
38    : IAccountSettingsWidget(parent)
39{
40    ui.setupUi(this);
41
42    BCSmartphoneManager *smartphoneManager = BCGateway::instance()->smartphoneManager();
43
44    connect(smartphoneManager, SIGNAL(deviceRegistered(Smartphone)), this, SLOT(onDeviceRegistered(Smartphone)));
45    connect(smartphoneManager->connector(), SIGNAL(deviceConnected(Smartphone)), this, SLOT(onDeviceConnected(Smartphone)));
46    connect(smartphoneManager->connector(), SIGNAL(deviceDisconnected(Smartphone)), this, SLOT(onDeviceDisconnected(Smartphone)));
47
48    while (smartphoneManager->pcName().isEmpty()) {
49        bool ok;
50        QString name = QInputDialog::getText(this, tr("Select name"),
51                                             tr("Please select a unique name for this SMSSender instance.\nYou will see this name on your smartphone to help you decide whom you want to grant access."),
52                                             QLineEdit::Normal, "", &ok);
53        smartphoneManager->setPcName(name);
54    }
55}
56
57void VCSettingsWidget::generateQRCode() {
58    if (initialDevice_.secret().isNull()) {
59        initialDevice_.setSecret(BCGateway::instance()->smartphoneManager()->generateUnidentifiedSecret());
60    }
61
62    QString data = BCGateway::instance()->smartphoneManager()->connector()->createConnectionRequestPackage(initialDevice_);
63
64    // create the QR code
65    QRcode *code = QRcode_encodeString(data.toUtf8(), 0, QR_ECLEVEL_L, QR_MODE_8, 1);
66
67    QBitArray codeData(code->width*code->width);
68    for (int i = 0; i < codeData.size(); ++i) {
69        codeData.setBit(i, code->data[i] & 0x01); // Should the pixel be black?
70    }
71    ui.qrCode->setCode(codeData);
72
73    QRcode_free(code);
74}
75
76void VCSettingsWidget::onDeviceRegistered(const Smartphone &device) {
77    if (!initialDevice_.isNull()) {
78        return; // Do not listen to different devices if one was already linked to this account
79    }
80
81    SET_IF_DIFFERENT(device_, device);
82}
83void VCSettingsWidget::onDeviceConnected(const Smartphone &device) {
84    if (!initialDevice_.isNull()) {
85        if (device != device_) {
86            return;
87        }
88    }
89    device_ = device;
90
91    ui.lblConnectionStatus->setText(tr("Connected to %1").arg(device_.name()));
92    ui.lblConnectionStatus->setStyleSheet("background: url(\":/images/connected.png\") left middle no-repeat; padding-left: 16px;");
93}
94void VCSettingsWidget::onDeviceDisconnected(const Smartphone &device) {
95    if (device != device_) {
96        return;
97    }
98
99    if (initialDevice_.isNull() || initialDevice_.name().isEmpty()) {
100        ui.lblConnectionStatus->setText(tr("No connection to your phone"));
101    } else {
102        ui.lblConnectionStatus->setText(tr("No connection to %1").arg(initialDevice_.name()));
103    }
104    ui.lblConnectionStatus->setStyleSheet("background: url(\":/images/disconnected.png\") left middle no-repeat; padding-left: 16px;");
105}
106
107
108void VCSettingsWidget::loadFromAccount(IAccount* account) {
109    Q_ASSERT(account);
110    Q_ASSERT(account->gateway() == BCGateway::instance());
111
112    BCAccount *acc = static_cast<BCAccount*>(account);
113
114    device_ = acc->device();
115    initialDevice_ = device_;
116    if (acc->gateway()->smartphoneManager()->connector()->isDeviceConnected(device_)) {
117        onDeviceConnected(device_);
118    } else {
119        onDeviceDisconnected(device_);
120    }
121
122    generateQRCode();
123}
124
125void VCSettingsWidget::saveToAccount(IAccount* account) {
126    Q_ASSERT(account);
127    Q_ASSERT(account->gateway() == BCGateway::instance());
128
129    BCAccount* acc = static_cast<BCAccount*>(account);
130
131    acc->setDevice(device_);
132}
133
134}
Note: See TracBrowser for help on using the repository browser.