Skip to content

Commit 990c12b

Browse files
committed
async processes now call dbus connect service only after they complete
1 parent 5c763af commit 990c12b

File tree

5 files changed

+88
-58
lines changed

5 files changed

+88
-58
lines changed

apps/cmstapp/code/control_box/controlbox.cpp

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ ControlBox::ControlBox(const QCommandLineParser& parser, QWidget *parent)
155155
settings = new QSettings(ORG, APP, this);
156156
notifyclient = NULL;
157157
onlineobjectpath.clear();
158+
pendingobjectpath.clear();
158159
socketserver = new QLocalServer(this);
159160
socketserver->removeServer(SOCKET_NAME); // remove any files that may have been left after a crash
160161
socketserver->listen(SOCKET_NAME);
@@ -739,6 +740,11 @@ void ControlBox::connectPressed()
739740
return;
740741
}
741742

743+
//Because of single selection mode list can only have 0 or 1 items in it.
744+
if (qtw == ui.tableWidget_wifi) pendingobjectpath = wifi_list.at(list.at(0)->row()).objpath.path();
745+
else if (qtw == ui.tableWidget_vpn) pendingobjectpath = vpn_list.at(list.at(0)->row()).objpath.path();
746+
else pendingobjectpath.clear();
747+
742748
// execute external program if specified
743749
if (! ui.lineEdit_beforeconnect->text().isEmpty() ) {
744750
if (list.at(0)->text() == ui.comboBox_beforeconnectserviceslist->currentText() ) {
@@ -750,29 +756,30 @@ void ControlBox::connectPressed()
750756
if (ui.checkBox_modifyservicefile->isChecked()) {
751757
GEN_Editor* gened = new GEN_Editor(this);
752758
gened->editInPlace(ui.comboBox_beforeconnectservicefile->currentText(), cmd, args);
753-
//delete gened;
759+
connect (gened, SIGNAL(finished(int)), this, SLOT(requestConnection()));
760+
// gened->deleteLater();
754761
} // program does require a root helper
755762
else {
756763
QProcess* proc = new QProcess(this);
757764
proc->startDetached(cmd, args);
758-
proc->waitForFinished(); // could be dangerous - will block until finished or timeout at 30 seconds
759-
delete proc;
765+
connect (proc, SIGNAL(finished(int)), this, SLOT(requestConnection()));
766+
// proc->deleteLater();
760767
} // program does not require root helper
761768
} // if service is correct
762769
} // if there is a command to execute
763-
764770

765-
// now request the connection. Because of single selection mode list can only have 0 or 1 items in it.
766-
QDBusInterface* iface_serv = NULL;
771+
// else request the connection now
772+
else requestConnection();
767773

768-
if (qtw == ui.tableWidget_wifi) {
769-
iface_serv = new QDBusInterface(DBUS_CON_SERVICE, wifi_list.at(list.at(0)->row()).objpath.path(), "net.connman.Service", QDBusConnection::systemBus(), this);
770-
}
771-
else if (qtw == ui.tableWidget_vpn) {
772-
iface_serv = new QDBusInterface(DBUS_CON_SERVICE, vpn_list.at(list.at(0)->row()).objpath.path(), "net.connman.Service", QDBusConnection::systemBus(), this);
773-
}
774-
else return; // really not needed
774+
return;
775+
}
776+
777+
//
778+
// Slot to actually request a connection via DBUS. Called from the connectPressed() slot
779+
void ControlBox::requestConnection() {
780+
QDBusInterface* iface_serv = NULL;
775781

782+
iface_serv = new QDBusInterface(DBUS_CON_SERVICE, pendingobjectpath, "net.connman.Service", QDBusConnection::systemBus(), this);
776783
iface_serv->setTimeout(5); // need a short timeout to get the Agent
777784
QDBusMessage reply = iface_serv->call(QDBus::AutoDetect, "Connect");
778785
if (reply.errorName() != "org.freedesktop.DBus.Error.NoReply") shared::processReply(reply);

apps/cmstapp/code/control_box/controlbox.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,11 @@ class ControlBox : public QDialog
144144
bool b_usemate;
145145
QSettings* settings;
146146
QString onlineobjectpath;
147+
QString pendingobjectpath;
147148
QLocalServer* socketserver;
148149
QColor trayiconbackground;
149150
IconManager* iconman;
150151
float f_connmanversion;
151-
int conn_type;
152152

153153
// functions
154154
int managerRescan(const int& srv = 0);
@@ -178,6 +178,7 @@ class ControlBox : public QDialog
178178
void enableMoveButtons(int,int);
179179
void counterUpdated(const QDBusObjectPath&, const QString&, const QString&);
180180
void connectPressed();
181+
void requestConnection();
181182
void disconnectPressed();
182183
void removePressed();
183184
void dbsPropertyChanged(QString,QDBusVariant);

apps/cmstapp/code/gen_conf_ed/gen_conf_ed.cpp

Lines changed: 61 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,15 @@ DEALINGS IN THE SOFTWARE.
3636
# include "./gen_conf_ed.h"
3737
# include "../resource.h"
3838
# include "./code/trstring/tr_strings.h"
39-
39+
40+
// Function to execute a process which may modify a /cmst/config file
41+
// in /var/lib/connman or /var/lib/connman-vpn. It will send a finished(int)
42+
// signal when completed. Exit codes:
43+
// negative number = this function never really executed
44+
// 0 = everything completed normally
45+
// 1 = one of the DBUS calls failed to complete
46+
// 2 = the external process failed to start
47+
// 3 = the external process returned a nonzero exit code
4048
//
4149
// Constructor
4250
GEN_Editor::GEN_Editor(QWidget* parent) : QWidget(parent)
@@ -47,12 +55,12 @@ GEN_Editor::GEN_Editor(QWidget* parent) : QWidget(parent)
4755
path.clear();
4856
process.clear();
4957
args.clear();
50-
filecontents.clear();
51-
finished = false;
58+
filecontents.clear();\
59+
proc = NULL;
5260

5361
// signals and slots
5462
connect(this, SIGNAL(readCompleted()), this, SLOT(executeProcess()));
55-
connect(this, SIGNAL(processCompleted()), this, SLOT(editContents()));
63+
5664
return;
5765
}
5866

@@ -131,49 +139,65 @@ void GEN_Editor::storeContents(const QString& data)
131139
}
132140

133141
//
134-
// Slot to execute the process. This blocks untill the the process is complete or timeouts
135-
// connected to the readCompleted signal emited by storeContents
142+
// Slot to execute the process.
136143
void GEN_Editor::executeProcess()
137144
{
138-
QProcess* proc = new QProcess(this);
145+
proc = new QProcess(this);
139146
proc->start(process, args);
140-
if (! proc->waitForStarted() ) return;
141-
142-
if (! proc->waitForFinished()) return;
147+
if (! proc->waitForStarted() ) {
148+
emit finished(2);
149+
return;
150+
}
143151

144-
changed = QString(proc->readAll()).split('\n');
145-
emit processCompleted();
152+
connect (proc, SIGNAL(finished(int)), this, SLOT(processExitCode(int)));
146153

147154
return;
155+
156+
}
157+
158+
//
159+
// Slot to process the return codes from executeProcess
160+
void GEN_Editor::processExitCode(int exitcode)
161+
{
162+
if (exitcode == 0) {
163+
changed = QString(proc->readAll()).split('\n');
164+
editBuffer();
165+
}
166+
else
167+
emit (finished(3));
168+
169+
return;
148170
}
171+
149172

150173
//
151-
// Slot to edit the contents of the config file. Connected to the processCompleted
152-
// signal emited by executeProcess
153-
void GEN_Editor::editContents()
174+
// Slot to edit the buffer containing the contents of the config file. Connected to processExitCode() function
175+
void GEN_Editor::editBuffer()
154176
{
155-
if (changed.size() <= 0 ) return;
156-
157-
for (int i = 0; i < filecontents.size(); ++i) {
158-
if (filecontents.at(i).size() == 0) {
159-
filecontents.removeAt(i);
160-
continue;
161-
}
162-
for (int j = 0; j < changed.size(); ++j) {
163-
if (changed.at(j).size() == 0) {
164-
changed.removeAt(j);
177+
if (changed.size() > 0 ) {
178+
for (int i = 0; i < filecontents.size(); ++i) {
179+
if (filecontents.at(i).size() == 0) {
180+
filecontents.removeAt(i);
165181
continue;
166182
}
167-
if(filecontents.at(i).split('=').at(0).simplified() == changed.at(j).split('=').at(0).simplified()) {
168-
filecontents.removeAt(i);
169-
filecontents.insert(i, changed.at(j));
170-
changed.removeAt(j);
171-
} // if
172-
}// j for
173-
} // i for
174-
if (changed.size() > 0) filecontents.append(changed);
175-
176-
this->writeFile();
183+
for (int j = 0; j < changed.size(); ++j) {
184+
if (changed.at(j).size() == 0) {
185+
changed.removeAt(j);
186+
continue;
187+
}
188+
if(filecontents.at(i).split('=').at(0).simplified() == changed.at(j).split('=').at(0).simplified()) {
189+
filecontents.removeAt(i);
190+
filecontents.insert(i, changed.at(j));
191+
changed.removeAt(j);
192+
} // if
193+
}// j for
194+
} // i for
195+
if (changed.size() > 0) filecontents.append(changed);
196+
197+
this->writeFile();
198+
} // if we changed something
199+
200+
else emit finished(0);
177201

178202
return;
179203
}
@@ -195,9 +219,7 @@ void GEN_Editor::writeCompleted(qint64 bytes)
195219
msg = tr("%L1 Bytes written").arg(bytes);
196220
}
197221

198-
finished = true;
199-
200-
qDebug() << msg;
222+
emit finished(0);
201223

202224
return;
203225
}
@@ -212,7 +234,7 @@ void GEN_Editor::callbackErrorHandler(QDBusError err)
212234
QMessageBox::Ok,
213235
QMessageBox::Ok);
214236

215-
finished = true;
237+
emit finished(1);
216238

217239
return;
218240
}

apps/cmstapp/code/gen_conf_ed/gen_conf_ed.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ class GEN_Editor : public QWidget
4242
public:
4343
GEN_Editor(QWidget*);
4444
void editInPlace(const QString&, const QString&, const QStringList&);
45-
inline bool isFinished() {return finished;}
4645

4746
private:
4847
// members
@@ -52,7 +51,7 @@ class GEN_Editor : public QWidget
5251
QString process;
5352
QStringList args;
5453
QStringList changed;
55-
bool finished;
54+
QProcess* proc;
5655

5756
//functions
5857
void readFile();
@@ -61,13 +60,14 @@ class GEN_Editor : public QWidget
6160
private slots:
6261
void storeContents(const QString&);
6362
void executeProcess();
64-
void editContents();
63+
void processExitCode(int);
64+
void editBuffer();
6565
void writeCompleted(qint64);
6666
void callbackErrorHandler(QDBusError);
6767

6868
signals:
6969
void readCompleted();
70-
void processCompleted();
70+
void finished(int exitcode = -1);
7171

7272
};
7373

apps/resource.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ DEALINGS IN THE SOFTWARE.
3535
///////////////////////////////// Program Values ///////////////////////
3636
//
3737
// Program Info (may be visible, but don't mark for tranalation)
38-
#define VERSION "2020.03.22-1"
38+
#define VERSION "2020.03.23-1"
3939

4040
#define RELEASE_DATE "7 March 2020"
4141
#define COPYRIGHT_DATE "2013-2020"

0 commit comments

Comments
 (0)