Add pws to be able to start apps
[apps/onscreenapp.git] / pws / launcher.cpp
1 /*
2  * Copyright (c) 2020 Collabora Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <json-c/json.h>
18
19 #include "launcher.h"
20 #include "pws.h"
21
22 int
23 Launcher::setup_pws_connection(void)
24 {
25         pws = pws_data_source_init(afm_sock_name.toStdString().c_str());
26         if (!pws)
27                 return -1;
28
29         connection_set = true;
30         return 0;
31 }
32
33 bool
34 Launcher::connection_is_set(void)
35 {
36         return connection_set;
37 }
38
39 int
40 Launcher::start(const QString &app)
41 {
42         int rid = -1;
43
44         if (!connection_set)
45                 return rid;
46
47         rid = pws_start_process(pws, app.toStdString().c_str());
48         if (rid > 0)
49                 applications.insert(app, rid);
50
51         return rid;
52 }
53
54 bool
55 Launcher::terminate(const QString &app)
56 {
57         if (!connection_set)
58                 return -1;
59
60         if (pws_stop_process(pws, app.toStdString().c_str()))
61                 return true;
62
63         return false;
64 }
65
66 bool
67 Launcher::is_running(const QString &app)
68 {
69         int rid = -1;
70
71         if (!connection_set)
72                 return false;
73
74         rid = pws_check_process_is_running(pws, app.toStdString().c_str());
75         /* remove it from QHash if it was there and current no longer shows up */
76         if (rid > 0) {
77                 return true;
78         } else {
79                 if (applications.contains(app))
80                         applications.remove(app);
81         }
82
83         return false;
84 }
85
86 size_t
87 Launcher::get_list_runnables(QString *qstr)
88 {
89         size_t items = 0;
90         struct json_object *json;
91
92         if (!connection_set)
93                 return false;
94
95         items = pws_get_list_runnables(pws, &json);
96         if (json)
97                 *qstr = QString(json_object_to_json_string(json));
98         else
99                 *qstr = nullptr;
100
101         /* necessary as pws_get_list_runnables won't free() the json reply on
102          * its own */
103         pws_data_source_reply_destroy(pws);
104
105         return items;
106 }