-
Notifications
You must be signed in to change notification settings - Fork 5
/
qube-apps
executable file
·372 lines (293 loc) · 11.1 KB
/
qube-apps
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#!/usr/bin/env python3
import sys
import os
import inspect
import subprocess
import json
import signal
from PySide2 import QtCore, QtWidgets, QtGui
def valid_package(app_id):
if (
app_id.startswith("org.freedesktop.")
or app_id.startswith("org.gnome.")
or app_id.startswith("org.kde.")
or app_id == ""
):
return False
return True
class SearchDialog(QtWidgets.QDialog):
def __init__(self):
super(SearchDialog, self).__init__()
self.setModal(True)
self.setWindowTitle("Search Flathub for apps")
self.setMinimumWidth(600)
self.setMinimumHeight(300)
self.input = QtWidgets.QLineEdit()
self.input.setPlaceholderText("Search Flathub for apps")
self.search_button = QtWidgets.QPushButton("Search")
self.search_button.clicked.connect(self.search_clicked)
search_layout = QtWidgets.QHBoxLayout()
search_layout.addWidget(self.input, stretch=1)
search_layout.addWidget(self.search_button)
self.results_layout = QtWidgets.QVBoxLayout()
self.results_layout.addWidget(
QtWidgets.QWidget()
) # Adding a blank widget seems to help
results_widget = QtWidgets.QWidget()
results_widget.setLayout(self.results_layout)
results_scrollarea = QtWidgets.QScrollArea()
results_scrollarea.setWidget(results_widget)
results_scrollarea.setWidgetResizable(True)
layout = QtWidgets.QVBoxLayout(self)
layout.addLayout(search_layout)
layout.addWidget(results_scrollarea, stretch=1)
def search_clicked(self):
print(f"Searching for: {self.input.text()}")
self.input.setEnabled(False)
self.search_button.setEnabled(False)
self.search_button.setText("Searching ...")
self.t = SearchThread(self.input.text())
self.t.success.connect(self.search_results)
self.t.start()
def search_results(self, results_json):
self.input.setEnabled(True)
self.search_button.setEnabled(True)
self.search_button.setText("Search")
# Clear the layout
children = []
for i in range(self.results_layout.count()):
child = self.results_layout.itemAt(i).widget()
if child:
children.append(child)
for child in children:
child.deleteLater()
# Add the results
results = json.loads(results_json)
if len(results) == 0:
self.results_layout.addWidget(QtWidgets.QLabel("App not found"))
else:
for result in results:
print(f"Result: {result}")
app = SearchApp(result["app_id"], result["name"])
self.results_layout.addWidget(app)
class SearchApp(QtWidgets.QWidget):
install = QtCore.Signal(str)
def __init__(self, app_id, name):
super(SearchApp, self).__init__()
self.app_id = app_id
self.name = name
name = QtWidgets.QLabel(self.name)
name.setStyleSheet("QLabel { font-weight: bold }")
self.info_button = QtWidgets.QPushButton("Info")
self.info_button.clicked.connect(self.info_clicked)
self.install_button = QtWidgets.QPushButton("Install")
self.install_button.clicked.connect(self.install_clicked)
layout = QtWidgets.QHBoxLayout()
layout.addWidget(name)
layout.addStretch()
layout.addWidget(self.info_button)
layout.addWidget(self.install_button)
self.setLayout(layout)
def info_clicked(self):
url = QtCore.QUrl(f"https://flathub.org/apps/details/{self.app_id}")
QtGui.QDesktopServices.openUrl(url)
def install_clicked(self):
print(f"Installing: {self.app_id}")
subprocess.run(
[
"/usr/bin/xterm",
"-e",
"/usr/bin/flatpak",
"install",
"--user",
"flathub",
self.app_id,
]
)
class SearchThread(QtCore.QThread):
success = QtCore.Signal(str)
def __init__(self, query):
super(SearchThread, self).__init__()
self.query = query
def run(self):
results = []
out = subprocess.check_output(
[
"/usr/bin/flatpak",
"search",
"--columns",
"application:f,name:f",
self.query,
]
).decode()
if "No matches found" in out:
self.success.emit(json.dumps(results))
return
for line in out.split("\n"):
line = line.strip()
if valid_package(line):
parts = line.split()
app_id = parts[0]
name = " ".join(parts[1:])
results.append({"app_id": app_id, "name": name})
self.success.emit(json.dumps(results))
class InstalledApp(QtWidgets.QWidget):
run = QtCore.Signal(str)
delete = QtCore.Signal(str)
def __init__(self, app_details):
super(InstalledApp, self).__init__()
self.app_details = app_details
name = QtWidgets.QLabel(self.app_details["Name"])
name.setStyleSheet("QLabel { font-weight: bold }")
size = QtWidgets.QLabel(self.app_details["Installed"])
size.setStyleSheet("QLabel { font-size: 10px }")
self.run_button = QtWidgets.QPushButton("Run")
self.run_button.clicked.connect(self.run_clicked)
self.delete_button = QtWidgets.QPushButton("Delete")
self.delete_button.clicked.connect(self.delete_clicked)
layout = QtWidgets.QHBoxLayout()
layout.addWidget(name)
layout.addWidget(size)
layout.addStretch()
layout.addWidget(self.run_button)
layout.addWidget(self.delete_button)
self.setLayout(layout)
def run_clicked(self):
print(f"Running: {self.app_details['ID']}")
self.run.emit(self.app_details["ID"])
def delete_clicked(self):
d = QtWidgets.QMessageBox()
d.setText(f"Are you sure you want to delete {self.app_details['Name']}?")
d.setStandardButtons(QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.Cancel)
ret = d.exec_()
if ret == QtWidgets.QMessageBox.Yes:
self.delete.emit(self.app_details["ID"])
class InstalledApps(QtWidgets.QWidget):
def __init__(self):
super(InstalledApps, self).__init__()
self.layout = QtWidgets.QVBoxLayout()
self.setLayout(self.layout)
self.update()
def update(self):
# Clear the layout
children = []
for i in range(self.layout.count()):
child = self.layout.itemAt(i).widget()
if child:
children.append(child)
for child in children:
child.deleteLater()
installed_apps = self.get_installed_apps()
if len(installed_apps) == 0:
label = QtWidgets.QLabel("No Flatpak apps are installed yet")
self.layout.addWidget(label)
else:
for name in sorted(list(installed_apps)):
app = InstalledApp(installed_apps[name])
app.run.connect(self.run_app)
app.delete.connect(self.delete_app)
self.layout.addWidget(app)
def run_app(self, id):
subprocess.Popen(["/usr/bin/flatpak", "run", "--user", id])
def delete_app(self, id):
print(f"Deleting: {id}")
subprocess.run(
["/usr/bin/xterm", "-e", "/usr/bin/flatpak", "uninstall", "--user", id]
)
self.update()
def get_installed_apps(self):
app_ids = []
out = subprocess.check_output(
["/usr/bin/flatpak", "list", "--user", "--columns", "application:f"]
).decode()
for line in out.split("\n"):
line = line.strip()
if valid_package(line):
app_ids.append(line.strip())
apps = {}
for app_id in app_ids:
out = subprocess.check_output(
[
"/usr/bin/flatpak",
"info",
"--user",
app_id,
]
).decode()
lines = out.split("\n")
app_details = {"Name": "".join(lines[0:3]).split("-")[0].strip()}
for line in lines[3:]:
try:
column_i = line.index(":")
key = line[0:column_i].strip()
val = line[column_i + 2 :].strip()
app_details[key] = val
except ValueError:
pass
apps[app_details["Name"]] = app_details
return apps
class QubeAppsWindow(QtWidgets.QMainWindow):
def __init__(self, app):
super(QubeAppsWindow, self).__init__()
self.app = app
self.setWindowTitle("Qube Apps")
self.setWindowIcon(QtGui.QIcon(self.get_icon_path()))
# Ensure Flathub is added
subprocess.run(
[
"/usr/bin/flatpak",
"remote-add",
"--user",
"--if-not-exists",
"flathub",
"https://dl.flathub.org/repo/flathub.flatpakrepo",
]
)
self.installed_apps = InstalledApps()
self.update_button = QtWidgets.QPushButton("Update Apps")
self.update_button.clicked.connect(self.update_button_clicked)
install_button = QtWidgets.QPushButton("Install New App")
install_button.clicked.connect(self.install_button_clicked)
buttons_layout = QtWidgets.QHBoxLayout()
buttons_layout.addStretch()
buttons_layout.addWidget(self.update_button)
buttons_layout.addWidget(install_button)
layout = QtWidgets.QVBoxLayout()
layout.addWidget(self.installed_apps)
layout.addStretch()
layout.addLayout(buttons_layout)
central_widget = QtWidgets.QWidget()
central_widget.setLayout(layout)
self.setCentralWidget(central_widget)
self.installed_apps.update()
self.show()
def update_button_clicked(self):
subprocess.run(["/usr/bin/xterm", "-e", "/usr/bin/flatpak", "--user", "update"])
def install_button_clicked(self):
d = SearchDialog()
d.exec_()
self.installed_apps.update()
def get_icon_path(self):
if sys.argv and sys.argv[0].startswith(sys.prefix):
# Installed systemwide
prefix = os.path.join(sys.prefix, "share/pixmaps")
else:
# Look for share directory relative to python file
prefix = os.path.join(
os.path.dirname(
os.path.abspath(inspect.getfile(inspect.currentframe()))
),
"share",
)
return os.path.join(prefix, "qube-apps.png")
def main():
# Allow Ctrl-C to smoothly quit the program instead of throwing an exception
def signal_handler(s, frame):
print("\nCtrl-C pressed, quitting")
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
app = QtWidgets.QApplication(sys.argv)
windows = QubeAppsWindow(app)
sys.exit(app.exec_())
if __name__ == "__main__":
main()