add purge logins action

This commit is contained in:
Pierre Ducroquet 2023-06-23 07:24:53 +02:00
parent 7e19bc6e84
commit 4ef55e2f6d
4 changed files with 102 additions and 34 deletions

View File

@ -199,148 +199,176 @@
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.ui" line="95"/>
<location filename="mainwindow.ui" line="96"/>
<source>Billing</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.ui" line="112"/>
<location filename="mainwindow.ui" line="113"/>
<source>&amp;Quit</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.ui" line="115"/>
<location filename="mainwindow.ui" line="116"/>
<source>Ctrl+Q</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.ui" line="132"/>
<location filename="mainwindow.ui" line="133"/>
<source>Graph per month</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.ui" line="137"/>
<location filename="mainwindow.ui" line="138"/>
<source>Bills of last month</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.ui" line="142"/>
<location filename="mainwindow.ui" line="143"/>
<source>Fidelity</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.ui" line="147"/>
<location filename="mainwindow.ui" line="148"/>
<source>Services</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.ui" line="153"/>
<source>Purge expired logins</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.ui" line="82"/>
<source>&amp;Login</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.ui" line="124"/>
<location filename="mainwindow.ui" line="125"/>
<source>&amp;New login</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.ui" line="127"/>
<location filename="mainwindow.ui" line="128"/>
<source>Ctrl+L</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.cpp" line="143"/>
<location filename="mainwindow.cpp" line="90"/>
<source>Expired token</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.cpp" line="90"/>
<source>It seems this token is expired. Please login again to continue.</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.cpp" line="148"/>
<source>Validate credential</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.cpp" line="143"/>
<location filename="mainwindow.cpp" line="148"/>
<source>Have you validated the OVH credential request?</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.cpp" line="185"/>
<location filename="mainwindow.cpp" line="190"/>
<source>Info %1</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.cpp" line="200"/>
<location filename="mainwindow.cpp" line="205"/>
<source>IPMI not activated</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.cpp" line="200"/>
<location filename="mainwindow.cpp" line="205"/>
<source>IPMI is not activated on this server</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.cpp" line="209"/>
<location filename="mainwindow.cpp" line="214"/>
<source>IPMI KVM not available</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.cpp" line="209"/>
<location filename="mainwindow.cpp" line="214"/>
<source>IPMI KVM is not available on this server</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.cpp" line="233"/>
<location filename="mainwindow.cpp" line="238"/>
<source>Waiting for task... %1</source>
<oldsource>Waiting for task...</oldsource>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.cpp" line="253"/>
<location filename="mainwindow.cpp" line="258"/>
<source>KVM %1</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.cpp" line="262"/>
<location filename="mainwindow.cpp" line="264"/>
<location filename="mainwindow.cpp" line="267"/>
<location filename="mainwindow.cpp" line="269"/>
<source>JNLP KVM</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.cpp" line="262"/>
<location filename="mainwindow.cpp" line="267"/>
<source>This server only supports a Java WebStart KVM, it will thus not be embedded in this window.
Java WebStart should be starting right now, wait until it finishes loading to close this dialog.</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.cpp" line="264"/>
<location filename="mainwindow.cpp" line="269"/>
<source>Failed to launch javaws. Make sure it is properly installed.</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.cpp" line="363"/>
<location filename="mainwindow.cpp" line="368"/>
<source>Target archive</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.cpp" line="363"/>
<location filename="mainwindow.cpp" line="368"/>
<source>Zip files (*.zip)</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.cpp" line="367"/>
<location filename="mainwindow.cpp" line="372"/>
<source>OVH...</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.cpp" line="367"/>
<location filename="mainwindow.cpp" line="372"/>
<source>OVH decided that getting a bill require a &apos;true&apos; login. I&apos;m going to show you a web browser for that.</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.cpp" line="378"/>
<location filename="mainwindow.cpp" line="383"/>
<source>Failed to open archive</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.cpp" line="378"/>
<location filename="mainwindow.cpp" line="383"/>
<source>Failed to open archive...</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.cpp" line="474"/>
<source>Logins cleaned up</source>
<translation type="unfinished"></translation>
</message>
<message numerus="yes">
<location filename="mainwindow.cpp" line="474"/>
<source>%n login(s) purged</source>
<translation type="unfinished">
<numerusform></numerusform>
<numerusform></numerusform>
</translation>
</message>
</context>
<context>
<name>OvhWebAuthentication</name>

View File

@ -37,27 +37,25 @@ MainWindow::MainWindow(QWidget *parent)
{
ui->setupUi(this);
api = new OvhApi{"https://eu.api.ovh.com/1.0", this};
serverKvmMapper = new QSignalMapper(this);
connect(serverKvmMapper, &QSignalMapper::mappedString, this, &MainWindow::startKVMRequest);
QSettings settings;
QAction *loginAction = nullptr;
int loginActions = 0;
if (settings.childGroups().contains("logins")) {
settings.beginGroup("logins");
for (auto ck: settings.childKeys()) {
// Add a menu item...
loginActions++;
loginAction = addLoginAction(ck, settings.value(ck).toString());
loginActions.append(loginAction);
}
}
if (loginActions == 1) {
if (loginActions.size() == 1) {
// singleShot so it jumps back to the main event loop
QTimer::singleShot(0, loginAction, &QAction::trigger);
//loginAction->trigger();
}
api = new OvhApi{"https://eu.api.ovh.com/1.0", this};
}
QAction *MainWindow::addLoginAction(const QString &ck, const QString &display) {
@ -86,6 +84,13 @@ QCoro::Task<> MainWindow::login(const QString &ck) {
qDebug() << "ck";
api->setConsumerKey(ck);
// Check it works...
auto meCheck = (co_await api->get("/me")).object();
if (meCheck.keys().contains("class") && meCheck["class"].toString() == "Client::Forbidden" ) {
QMessageBox::warning(this, tr("Expired token"), tr("It seems this token is expired. Please login again to continue."));
co_return;
}
// And fetch all infos...
qDebug() << "fetch";
auto servers = co_await api->get("/dedicated/server");
@ -444,3 +449,29 @@ void MainWindow::on_actionServices_triggered()
view->exec();
}
QCoro::Task<> MainWindow::on_actionPurgeLogins_triggered()
{
QList<QAction *> validLoginActions;
auto ovh = new OvhApi{"https://eu.api.ovh.com/1.0", this};
for (auto loginAction: loginActions) {
qDebug() << "checking for " << loginAction;
ovh->setConsumerKey(loginAction->data().toString());
auto meForCk = co_await ovh->get("/me");
qDebug() << meForCk;
QJsonObject me = meForCk.object();
if (me.keys().size() == 2 && me.keys().contains("class") && me["class"].toString() == "Client::Forbidden" ) {
// This is an invalid token
// Delete from settings
QSettings settings;
settings.beginGroup("logins");
settings.remove(loginAction->data().toString());
// Delete action, will purge menu entry and so on
loginAction->deleteLater();
} else {
validLoginActions.append(loginAction);
}
}
QMessageBox::information(this, tr("Logins cleaned up"), tr("%n login(s) purged", nullptr, loginActions.size() - validLoginActions.size()));
loginActions = validLoginActions;
}

View File

@ -46,9 +46,12 @@ private slots:
void on_actionServices_triggered();
QCoro::Task<> on_actionPurgeLogins_triggered();
private:
QAction *addLoginAction(const QString &ck, const QString &display);
QMap<QString, QTreeWidgetItem*> topLevelNodes;
QList<QAction *> loginActions;
QSignalMapper *serverKvmMapper;
Ui::MainWindow *ui;
OvhApi *api;

View File

@ -86,6 +86,7 @@
<normaloff>.</normaloff>.</iconset>
</property>
<addaction name="actionLogin"/>
<addaction name="actionPurgeLogins"/>
</widget>
<addaction name="menuLogin"/>
<addaction name="actionQuit"/>
@ -147,6 +148,11 @@
<string>Services</string>
</property>
</action>
<action name="actionPurgeLogins">
<property name="text">
<string>Purge expired logins</string>
</property>
</action>
</widget>
<resources>
<include location="resources.qrc"/>