Skip to content

Commit

Permalink
Update 1.8.0:
Browse files Browse the repository at this point in the history
* Added command line options for listing and removing trusted devices.
  • Loading branch information
brookiestein committed Aug 17, 2024
1 parent 5cbb24b commit bb60878
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 14 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.28)

project(BtScreenLocker
VERSION 1.7.0
VERSION 1.8.0
LANGUAGES CXX
DESCRIPTION "Daemon to lock your screen if trusted Bluetooth devices go away."
)
Expand Down
28 changes: 16 additions & 12 deletions src/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ void Connection::connect()
{
const unsigned int length = 18; /* MAC Addresses are 17 characters long */
QSharedPointer<char> address(new char[length]);
struct sockaddr_rc target = {
.rc_family = AF_BLUETOOTH,
.rc_channel = 2
};

struct sockaddr_rc target = {0};
std::memset(&target, 0, sizeof(target));

target.rc_family = AF_BLUETOOTH;
target.rc_channel = 2;

/* Try to connect to every single trusted device until one gets connected. */
for (const auto &device : m_trustedDevices) {
Expand Down Expand Up @@ -53,18 +55,20 @@ void Connection::connect()
}

int status = ::connect(sock, (struct sockaddr *) &target, sizeof(target));
if (status == 0) {
if (status < 0) {
m_logger.log(
tr("Couldn't connect to Bluetooth device: %1 - %2.\n"
"It may be far or has Bluetooth disabled.").arg(address.data(), name),
Q_FUNC_INFO, Logger::WARNING
);

close(sock);
m_logger.log(tr("Connected to Bluetooth device: %1 - %2!").arg(address.data(), name), Q_FUNC_INFO);
m_connectedDevices.push_back(device);
continue;
}

m_logger.log(
tr("Couldn't connect to Bluetooth device: %1 - %2.\n"
"It may be far or has Bluetooth disabled.").arg(address.data(), name),
Q_FUNC_INFO, Logger::WARNING
);
close(sock);
m_logger.log(tr("Connected to Bluetooth device: %1 - %2!").arg(address.data(), name), Q_FUNC_INFO);
m_connectedDevices.push_back(device);
}
}

Expand Down
40 changes: 40 additions & 0 deletions src/listener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,46 @@ Listener::~Listener()
m_settings->endGroup();
}

void Listener::listDevices()
{
qInfo().nospace() << '\n' << "These are all trusted devices:";
int i {1};
for (const auto &device : m_settings->allKeys()) {
qInfo().noquote().nospace() << i++ << ". " << device;
}
qInfo();
}

void Listener::removeDevice(QString device)
{
bool ok {false};
int deviceNumber = device.toInt(&ok);
if (ok) {
if (deviceNumber <= 0 or deviceNumber > m_settings->allKeys().size()) {
m_logger.log(
tr("Device #%1 doesn't exist in the trusted devices list.").arg(QString::number(deviceNumber)),
Q_FUNC_INFO, Logger::FATAL
);
}

device = m_settings->allKeys()[deviceNumber - 1];
goto success;
}

/* Alternatively user can remove a device by its fullname. */
if (not m_settings->allKeys().contains(device)) {
m_logger.log(
tr("Device '%1' doesn't exist in the trusted devices list.").arg(device),
Q_FUNC_INFO,
Logger::FATAL
);
}

success:
m_settings->remove(device);
m_logger.log(tr("Device '%1' is not a trusted device anymore.").arg(device), Q_FUNC_INFO);
}

void Listener::start()
{
if (m_restarting) {
Expand Down
2 changes: 2 additions & 0 deletions src/listener.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class Listener : public QObject
public:
explicit Listener(ScreenLocker &locker, bool autorestart = false, int seconds = -1, QObject *parent = nullptr);
~Listener();
void listDevices();
void removeDevice(QString device);
void start();
void startDiscovery();
signals:
Expand Down
18 changes: 17 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,22 @@ int main(int argc, char *argv[])
ScreenLocker locker;
Listener listener(locker, autorestart, seconds);
a.connect(&listener, &Listener::lockScreen, &locker, &ScreenLocker::lockScreen);
a.connect(&listener, &Listener::quit, &a, &QApplication::quit);
a.connect(&listener, &Listener::quit, &a, &QApplication::quit, Qt::QueuedConnection);
/* When screen is locked, no scan is done.
* Connect to this signal to start scanning when screen is unlocked again.
*/
a.connect(&locker, &ScreenLocker::activeChanged, &listener, &Listener::activeChanged);

if (parser.isSet("list-devices")) {
listener.listDevices();
a.quit();
}

if (parser.isSet("remove-device")) {
listener.removeDevice(parser.value("remove-device"));
a.quit();
}

if (parser.isSet("discover")) {
listener.startDiscovery();
} else {
Expand Down Expand Up @@ -182,10 +192,16 @@ QList<QCommandLineOption> commandLineOptions(const char *name)
QObject::tr("Set language. (Available: English (default), and Spanish"), "language")
);

options.append(QCommandLineOption(QStringList() << "L" << "list-devices",
QObject::tr("List trusted devices.")));

options.append(QCommandLineOption(QStringList() << "p" << "pause",
QObject::tr("Pause an already running %1 instance.").arg(name))
);

options.append(QCommandLineOption(QStringList() << "R" << "remove-device",
QObject::tr("Remove #th trusted device. Use -L to see all trusted devices."), "#"));

options.append(QCommandLineOption(QStringList() << "r" << "resume",
QObject::tr("Resume an already running and paused %1 instance.").arg(name))
);
Expand Down
Binary file modified translations/BtScreenLocker_es_US.qm
Binary file not shown.
20 changes: 20 additions & 0 deletions translations/BtScreenLocker_es_US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,18 @@ Si en algún momento el dispositivo: %3 vuelve a estar disponible de nuevo, por
<source> Restarting in %1 seconds.</source>
<translation> Reiniciando en %1 segundos.</translation>
</message>
<message>
<source>Device &apos;%1&apos; doesn&apos;t exist in the trusted devices list.</source>
<translation>El dispositivo &apos;%1&apos; no existe en la lista de dispositivos de confianza.</translation>
</message>
<message>
<source>Device &apos;%1&apos; is not a trusted device anymore.</source>
<translation>El dispositivo &apos;%1&apos; ya no es un dispositivo de confianza.</translation>
</message>
<message>
<source>Device #%1 doesn&apos;t exist in the trusted devices list.</source>
<translation>El dispositivo #%1 no existe en la lista de dispositivos de confianza.</translation>
</message>
</context>
<context>
<name>QObject</name>
Expand Down Expand Up @@ -463,6 +475,14 @@ Mensaje de error: %1</translation>
<source>Allow %1 to restart itself when your Bluetooth device becomes unavailable and availabe again, e.g. when your machine suspends. Default is 10 seconds.</source>
<translation>Permitir que %1 se reinicie cuando tu dispositivo Bluetooth se vuelve inaccesible and vuelve a estar disponible de nuevo, por ejemplo, cuando tu computadora se suspende. 10 segundos por defecto.</translation>
</message>
<message>
<source>List trusted devices.</source>
<translation>Enlistar dispositivos de confianza.</translation>
</message>
<message>
<source>Remove #th trusted device. Use -L to see all trusted devices.</source>
<translation>Eliminar el dispositivo de confianza #x. Usa -L para ver todos los dispositivos de confianza.</translation>
</message>
</context>
<context>
<name>ScreenLocker</name>
Expand Down

0 comments on commit bb60878

Please sign in to comment.