forked from AdrianTM/mx-packageinstaller
-
Notifications
You must be signed in to change notification settings - Fork 11
/
remotes.cpp
139 lines (117 loc) · 4.78 KB
/
remotes.cpp
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
#include "remotes.h"
#include <QDebug>
#include <QLabel>
#include <QLayout>
#include <QLineEdit>
#include <QMessageBox>
#include <QPushButton>
#include "cmd.h"
ManageRemotes::ManageRemotes(QWidget *parent, const QString &user)
: QDialog(parent),
user(user)
{
setWindowTitle(tr("Manage Flatpak Remotes"));
changed = false;
auto *layout = new QGridLayout();
setLayout(layout);
comboUser = new QComboBox(this);
comboUser->addItem(tr("For all users"));
comboUser->addItem(tr("For current user"));
if (user == "--system ") {
comboUser->setCurrentIndex(0);
} else {
comboUser->setCurrentIndex(1);
}
comboRemote = new QComboBox(this);
editAddRemote = new QLineEdit(this);
editAddRemote->setMinimumWidth(400);
editAddRemote->setPlaceholderText(tr("enter Flatpak remote URL"));
editInstallFlatpakref = new QLineEdit(this);
editInstallFlatpakref->setPlaceholderText(tr("enter Flatpakref location to install app"));
auto *label = new QLabel(tr("Add or remove flatpak remotes (repos), or install apps using flatpakref URL or path"));
layout->addWidget(label, 0, 0, 1, 5);
label->setAlignment(Qt::AlignCenter);
layout->addWidget(comboUser, 1, 0, 1, 4);
layout->addWidget(comboRemote, 2, 0, 1, 4);
layout->addWidget(editAddRemote, 3, 0, 1, 4);
layout->addWidget(editInstallFlatpakref, 4, 0, 1, 4);
auto *remove = new QPushButton(tr("Remove remote"));
remove->setIcon(QIcon::fromTheme("remove"));
remove->setAutoDefault(false);
layout->addWidget(remove, 2, 4, 1, 1);
auto *add = new QPushButton(tr("Add remote"));
add->setIcon(QIcon::fromTheme("add"));
add->setAutoDefault(false);
layout->addWidget(add, 3, 4, 1, 1);
auto *install = new QPushButton(tr("Install app"));
install->setIcon(QIcon::fromTheme("run-install"));
install->setAutoDefault(false);
layout->addWidget(install, 4, 4, 1, 1);
auto *cancel = new QPushButton(tr("Close"));
cancel->setIcon(QIcon::fromTheme("window-close"));
cancel->setAutoDefault(false);
layout->addWidget(cancel, 5, 4, 1, 1);
connect(cancel, &QPushButton::clicked, this, &ManageRemotes::close);
connect(remove, &QPushButton::clicked, this, &ManageRemotes::removeItem);
connect(add, &QPushButton::clicked, this, &ManageRemotes::addItem);
connect(install, &QPushButton::clicked, this, &ManageRemotes::setInstall);
connect(comboUser, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &ManageRemotes::userSelected);
listFlatpakRemotes();
}
void ManageRemotes::removeItem()
{
if (comboRemote->currentText() == QLatin1String("flathub")) {
QMessageBox::information(this, tr("Not removable"),
tr("Flathub is the main Flatpak remote and won't be removed"));
return;
}
changed = true;
const QString remote = comboRemote->currentText().section(" -- ", 0, 0);
QString user = comboRemote->currentText().section(" -- ", 1, 1);
user = user.isEmpty() ? "" : user.prepend("--");
Cmd().run("flatpak remote-delete " + remote + ' ' + user);
comboRemote->removeItem(comboRemote->currentIndex());
}
void ManageRemotes::addItem()
{
setCursor(QCursor(Qt::BusyCursor));
QString location = editAddRemote->text();
QString name = editAddRemote->text().section('/', -1).section('.', 0, 0); // obtain the name before .flatpakremo
if (!Cmd().run("flatpak remote-add " + user + "--if-not-exists " + name + ' ' + location)) {
setCursor(QCursor(Qt::ArrowCursor));
QMessageBox::critical(this, tr("Error adding remote"),
tr("Could not add remote - command returned an error. Please double-check the remote "
"address and try again"));
} else {
changed = true;
setCursor(QCursor(Qt::ArrowCursor));
QMessageBox::information(this, tr("Success"), tr("Remote added successfully"));
editAddRemote->clear();
comboRemote->addItem(name);
}
}
void ManageRemotes::setInstall()
{
install_ref = editInstallFlatpakref->text();
close();
}
void ManageRemotes::userSelected(int index)
{
if (index == 0) {
user = QStringLiteral("--system ");
} else {
user = QStringLiteral("--user ");
setCursor(QCursor(Qt::BusyCursor));
Cmd().run("flatpak --user remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo");
setCursor(QCursor(Qt::ArrowCursor));
}
listFlatpakRemotes();
}
// List the flatpak remote and load them into combobox
void ManageRemotes::listFlatpakRemotes() const
{
qDebug() << "+++" << __PRETTY_FUNCTION__ << "+++";
comboRemote->clear();
QStringList list = Cmd().getOut("flatpak remote-list").replace('\t', " -- ").split('\n');
comboRemote->addItems(list);
}