-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcompop.cpp
More file actions
148 lines (131 loc) · 4.8 KB
/
compop.cpp
File metadata and controls
148 lines (131 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "stdafx.h"
#include "compop.h"
#include <QSerialPortInfo>
#include <QIcon>
#include <QPixmap>
#include <QSettings>
#include <QMessageBox>
compop::compop(QObject *parent)
: QObject(parent)
{
trayIconMenu = new QMenu();
actionNotifyConnect = new QAction("Notify on connect", this);
actionNotifyConnect->setCheckable(true);
connect(actionNotifyConnect, SIGNAL(triggered()), this, SLOT(saveSettings()));
trayIconMenu->addAction(actionNotifyConnect);
actionNotifyDisconnect = new QAction("Notify on disconnect", this);
actionNotifyDisconnect->setCheckable(true);
connect(actionNotifyDisconnect, SIGNAL(triggered()), this, SLOT(saveSettings()));
trayIconMenu->addAction(actionNotifyDisconnect);
#ifdef Q_OS_WIN
actionStartup = new QAction("Launch on startup", this);
actionStartup->setCheckable(true);
connect(actionStartup, SIGNAL(triggered()), this, SLOT(saveSettings()));
trayIconMenu->addAction(actionStartup);
#endif
trayIconMenu->addSeparator();
actionAbout = new QAction("About", this);
connect(actionAbout, SIGNAL(triggered()), this, SLOT(menuAbout()));
trayIconMenu->addAction(actionAbout);
trayIconMenu->addSeparator();
actionQuit = new QAction("Quit", this);
connect(actionQuit, SIGNAL(triggered()), this, SLOT(menuQuit()));
trayIconMenu->addAction(actionQuit);
trayIcon = new QSystemTrayIcon(QIcon(QPixmap(":/COMPop/Resources/systray.png")));
trayIcon->setContextMenu(trayIconMenu);
trayIcon->setToolTip("Found no COM ports");
trayIcon->setVisible(true);
loadSettings();
timer = new QTimer();
connect(timer, SIGNAL(timeout()), this, SLOT(timerTimeout()));
timer->start(POLLING_PERIOD);
}
compop::~compop()
{
delete timer;
trayIcon->setVisible(false);
delete trayIconMenu;
delete trayIcon;
}
void compop::timerTimeout()
{
static QStringList list;
static bool firstRun = true;
QStringList newList;
QString tooltip;
int i;
// enumerate available ports
QList<QSerialPortInfo> serList = QSerialPortInfo::availablePorts();
for(i=0; i<serList.count(); i++) {
#ifdef Q_OS_DARWIN
if(serList[i].portName().indexOf("cu.") > -1 || serList[i].portName().indexOf("tty.") == -1)
continue;
#endif
newList.append(serList[i].description() + QString(" (") + serList[i].portName() + QString(")"));
}
if(list != newList || firstRun) {
if(!firstRun) {
// search for new ports
if(actionNotifyConnect->isChecked()) {
for(i=0; i<newList.count(); i++) {
if(list.indexOf(newList[i]) < 0) {
trayIcon->showMessage("Port plugged in", newList[i], QSystemTrayIcon::Information, 20000);
}
}
}
// search for removed ports
if(actionNotifyDisconnect->isChecked()) {
for(i=0; i<list.count(); i++) {
if(newList.indexOf(list[i]) < 0) {
trayIcon->showMessage("Port plugged out", list[i], QSystemTrayIcon::Information, 20000);
}
}
}
}
firstRun = false;
list = newList;
if(list.isEmpty()) {
trayIcon->setToolTip("Found no COM ports");
}
else {
for(i=0; i<list.count(); i++) {
tooltip += list[i] + QString("\n");
}
trayIcon->setToolTip(tooltip);
}
}
}
void compop::loadSettings()
{
QSettings settings(QSettings::IniFormat, QSettings::UserScope, "Goebish Apps", "compop");
actionNotifyConnect->setChecked(settings.value("NotifyConnect", "1") == "1");
actionNotifyDisconnect->setChecked(settings.value("NotifyDisconnect", "1") == "1");
#ifdef Q_OS_WIN
actionStartup->setChecked(settings.value("Startup", "0") == "1");
#endif
}
// Slots
void compop::saveSettings()
{
QSettings settings(QSettings::IniFormat, QSettings::UserScope, "Goebish Apps", "compop");
settings.setValue("NotifyConnect", actionNotifyConnect->isChecked() ? "1" : "0");
settings.setValue("NotifyDisconnect", actionNotifyDisconnect->isChecked() ? "1" : "0");
#ifdef Q_OS_WIN
settings.setValue("Startup", actionStartup->isChecked() ? "1" : "0");
QSettings registry("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat);
if(actionStartup->isChecked()) {
registry.setValue("compop", QCoreApplication::applicationFilePath().replace("/","\\"));
}
else {
registry.remove("compop");
}
#endif
}
void compop::menuAbout()
{
QMessageBox::about(nullptr, "About COMPop", "COMPop Virtual COM port notifier\n(c) 2019 Goebish\nVersion 1.0.3\n\nUpdates:\nhttps://github.com/goebish/COMPop/releases");
}
void compop::menuQuit()
{
QApplication::quit();
}