Skip to content

Commit

Permalink
Ajout de la fonction de suppression d'application, et de corrections …
Browse files Browse the repository at this point in the history
…de bugs de redimentionnement
  • Loading branch information
SuperAtraction committed Aug 11, 2023
1 parent fc1eb94 commit 7ccd005
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 47 deletions.
Binary file modified Olop
Binary file not shown.
2 changes: 1 addition & 1 deletion Olop.pro.user
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 11.0.0, 2023-07-31T17:13:11. -->
<!-- Written by QtCreator 11.0.1, 2023-08-07T19:37:02. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
Expand Down
125 changes: 83 additions & 42 deletions assets/index.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

<html lang="fr">
<head>
<meta charset="UTF-8">
Expand Down Expand Up @@ -234,6 +235,12 @@
overflow: auto;
}

.window-content iframe {
width: 100%;
height: 100%;
border: none;
}

.content-shifted {
margin-left: 0;
transition: margin-left 0.3s ease;
Expand Down Expand Up @@ -456,6 +463,9 @@
}

function updateMinSize(windowElement, useIframe){
if (!useIframe) { // Si ce n'est pas une iframe, ne faites rien et retournez directement
return;
}
setTimeout(() => {
initialHeight = windowElement.outerHeight();
initialWidth = windowElement.outerWidth();
Expand Down Expand Up @@ -490,11 +500,11 @@
}

setTimeout(() => {
if (useIframe) {
var iframe = $(ui.element).find('iframe').get(0);
resizeIframe(iframe);
}
}, 500);
if (useIframe) {
var iframe = $(ui.element).find('iframe').get(0);
resizeIframe(iframe);
}
}, 100);
}
});
}, 10)}
Expand Down Expand Up @@ -570,39 +580,26 @@
windowElement.append(windowTitleBar).append(windowContent);

windowElement.ready(function() {
$(windowElement).on('resize', function() {
$('.window').each(function() {
var $this = $(this);
$this.draggable('option', 'containment', [
0,
0,
$(window).width() - $this.outerWidth(true),
$(window).height() - $this.outerHeight(true)
]);
});
});

windowElement.draggable({
containment: "window",
drag: function(event, ui) {
var maxX = $(window).width();
var maxY = $(window).height();

if(ui.position.left > maxX) {
ui.position.left = maxX;
}
if(ui.position.top > maxY) {
ui.position.top = maxY;
}
if(ui.position.left < 0) {
ui.position.left = 0;
}
if(ui.position.top < 0) {
ui.position.top = 0;
handle: ".window-title-bar",
containment: "parent"
});
updateMinSize(windowElement, useIframe);
const contentObserver = new MutationObserver((mutationsList, observer) => {
for (const mutation of mutationsList) {
if (mutation.type === 'childList' || mutation.type === 'characterData' || mutation.type === 'attributes') {
// Mettre à jour la taille minimale si le contenu change
setTimeout(() => {
updateMinSize(windowElement, useIframe);
}, 500);
}
}
});
updateMinSize(windowElement, useIframe);
});

// Configuration de l'observateur pour surveiller les mutations de type childList (ajouts/suppressions d'éléments)
const config = { attributes: true, childList: true, characterData: true, subtree: true };
contentObserver.observe(windowContent.get(0), config);
});

return windowElement.get(0);
Expand Down Expand Up @@ -630,18 +627,22 @@
if (windowElement.length > 0) {
var windowContentElement = windowElement.find('.window-content');
var windowContentId = windowContentElement.attr('id');

var iframeElement = $('<iframe></iframe>').attr('id', windowContentId).attr('src', iframeSrc);
windowContentElement.replaceWith(iframeElement);
resizeIframe(iframeElement.get(0));

setTimeout(() => {
updateMinSize($("#"+windowId), true);
}, 100); // Le délai donne à l'iframe le temps de se charger
} else {
console.error('Aucune fenêtre trouvée avec l\'ID:', windowId);
}
}

function resizeIframe(iframe){
var Window = iframe.parentNode;
$(iframe).height($(Window).height() - $(Window).find('.window-title-bar').outerHeight()); // soustrayez la hauteur de la barre de titre
$(iframe).width($(Window).width());
function resizeIframe(iframe) {
var windowContainer = iframe.parentNode;
$(iframe).height($(windowContainer).height() - $(windowContainer).find('.window-title-bar').outerHeight(true));
$(iframe).width($(windowContainer).width());
}

function generateUniqueIdForWC() {
Expand All @@ -651,7 +652,6 @@
function closeWindow(button, isdb) {
if (isdb === 1) {
// Si la fenêtre est minimisée, fermer et supprimer la fenêtre de la liste des fenêtres minimisées
console.log('Close window and remove:', button.parentNode.parentNode.parentNode);
button.parentNode.parentNode.parentNode.remove();

var id = button.parentNode.parentNode.id;
Expand All @@ -661,8 +661,20 @@
}

} else {
console.log('Close window:', button.parentNode.parentNode);
button.parentNode.parentNode.remove();
var id = button.parentNode.parentNode.id;
var index = minimizedWindows.indexOf(id);
if (index !== -1) {
minimizedWindows.splice(index, 1);
}

}
var windowContentElement = button.parentNode.parentNode.querySelector('.window-content');
if (windowContentElement) {
const observer = windowContentElement.observer;
if (observer) {
observer.disconnect();
}
}
}

Expand Down Expand Up @@ -820,13 +832,42 @@
}

function getAppDescription(nom, version, developpeur, description, app) {
return `${description}<div style='text-align: right;'><i>${nom} version ${version}, <br>développé par ${developpeur}</i><br><button type="button" class="button" onclick="closeWindow(this,1);">Annuler</button>&nbsp;<button type="button" class="button" onclick="Launch(this, '${app}');">Lancer</button></div><br>`;
return `${description}<div style='text-align: right;'><i>${nom} version ${version}, <br>développé par ${developpeur}</i><br><span style='text-align: left'><button type="button" class="button" onclick="deleteApp(this, '`+app+`');">Déinstaller</button></span><button type="button" class="button" onclick="closeWindow(this,1);">Annuler</button>&nbsp;<button type="button" class="button" onclick="Launch(this, '`+app+`');">Lancer</button></div><br>`;
}

function Launch(button, app) {
$(button).parent().parent().load("/Launch/1/" + app + "-OLOP-" + $(button).parent().parent().attr("id"));
}

function deleteApp(button, app){
var contentID = button.closest('.window-content').id;
$.ajax({
url: "/remove/" + app + "-OLOP-" + contentID,
type: 'GET',
success: function(responseText) {
if (responseText !== "OK") {
$("#" + contentID).html(responseText);
}
},
error: function(xhr, textStatus, errorThrown) {
// Gérez les erreurs ici si nécessaire
console.error("Erreur lors de la requête :", textStatus);
}
});

}

function tmpHTML(id, time, content){
var oldContent = $("#"+id).html();
$("#"+id).html(content);
setTimeout(() => {
$("#"+id).fadeOut("slow", function () {
$("#"+id).html(oldContent);
$("#"+id).fadeIn("slow");
});
}, time);
}

function update() {
$("#button-update").html("Recherche de mises à jour...")
$("#info1").show("slide");
Expand Down
2 changes: 1 addition & 1 deletion assets/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ <h2>Thèmes :</h2>
<div id="settings-theme-manual">
Vous êtes en mode manuel.<br><br>
<a href="#" onclick='toggleTheme("toggle")' class="flat-button"><span id="ToggleText">Changer de theme</span></a>
</div><script>
</div><i>Oh non ! On ne peut pas encore sauvegarder les paramètres...</i><script>
function updateTTheme() {
var currentHour = new Date().getHours();
if (currentHour >= FirstTATheme || currentHour < secondTATheme) {
Expand Down
2 changes: 1 addition & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ int main(int argc, char *argv[])
QApplication app(argc, argv);

if(!MAIN::INIT()){
QMessageBox::critical(nullptr, "Erreur critique", "Erreur critique d'init !");
QMessageBox::critical(nullptr, "Olop - Erreur critique", "Erreur critique d'init !");
exit(-1);
}
int port = MAIN::SERVER();
Expand Down
74 changes: 72 additions & 2 deletions olop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ int MAIN::SERVER(){
QMessageBox::critical(MAIN::w, "Erreur", "Erreur lors de l'installation de l'application "+app[1]);
return "Erreur lors de l'installation de l'application "+app[1];
}
MAIN::w->ui->Web->page()->runJavaScript("showNotification(0, \"Installation d'une application\", \"L\'application " + app[1] + " est prête à être installée.<br>Elle sera installée lorsque vous la démarrerez.\");");
MAIN::w->ui->Web->page()->runJavaScript("showNotification(0, \"Installation d'une application\", \"L\'application " + QUrl::toPercentEncoding(app[1]) + " est prête à être installée.<br>Elle sera installée lorsque vous la démarrerez.\");");
MAIN::w->activateWindow();
MAIN::w->raise();
MAIN::w->showNormal();
Expand Down Expand Up @@ -179,6 +179,26 @@ httpServer.route("/Launch/1/<arg>", [=](const QUrl &Url) {
return QString("Chargement...");
});

httpServer.route("/remove/<arg>", [=](const QUrl Url){
QWebEnginePage *page = w->ui->Web->page();
auto args = Url.toDisplayString().split("-OLOP-");
auto app = args[0];
auto id = args[1];
auto decodedApp(APP::decodeApp(lireFichier(app)));
qDebug() << app;
auto réponse = QMessageBox::question(w, "Confirmation", "Êtes-vous sûr de vouloir désinstaller "+decodedApp[1]+" ?", QMessageBox::Yes | QMessageBox::No);
if(réponse == QMessageBox::Yes){
page->runJavaScript("$(\"#"+id+"\").html(\"Suppression...\");");
deleteFile(app);
supprimerDossier(app.split(".app")[0]+"/");
page->runJavaScript("loadPage(\"home\");");
return "Suppression terminée.";
}else {
page->runJavaScript("tmpHTML(\""+id+"\", 4000, \"La suppression de l'application "+decodedApp[1]+" a été annulée.\");");
return "OK";
}
});

httpServer.route("/stop/", [](){
exit(0);
return "";
Expand Down Expand Up @@ -267,6 +287,40 @@ QString MAIN::lireFichier(const QString& cheminFichier) {
return contenu;
}

bool MAIN::supprimerDossier(const QString& cheminDossier) {
QDir dossier(cheminDossier);

if (!dossier.exists()) {
qDebug() << "Le dossier n'existe pas!";
return false;
}

// Supprimer tous les fichiers dans le dossier
Q_FOREACH(QFileInfo info, dossier.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden | QDir::Files, QDir::DirsFirst)) {
if (info.isFile()) {
QFile fichier(info.filePath());

if (!fichier.remove()) {
qDebug() << "Impossible de supprimer le fichier" << info.fileName();
return false;
}
}
else {
supprimerDossier(info.absoluteFilePath());
}
}

// Supprimer tous les dossiers vides
Q_FOREACH(QFileInfo info, dossier.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden | QDir::Dirs, QDir::DirsFirst)) {
if (!QDir(info.absoluteFilePath()).removeRecursively()) {
qDebug() << "Impossible de supprimer le dossier" << info.absoluteFilePath();
return false;
}
}

return dossier.rmdir(cheminDossier);
}

bool MAIN::deleteFile(const QString& filePath) {
QFile file(filePath);

Expand Down Expand Up @@ -363,6 +417,14 @@ QString MAIN::detectLanguageJS(QWebEnginePage* page) {
return QString();
}

APP::~APP(){
for (auto server : httpServers) {
delete server;
}

httpServers.clear();
}

QString APP::LIST(const QString& directoryPath)
{
QStringList fileList = MAIN::getListOfFilesInDirectory(directoryPath);
Expand Down Expand Up @@ -419,13 +481,21 @@ int APP::HTTPSERVER(QString dir){
const auto port = httpServer->listen(QHostAddress::Any);
if (!port) {
qDebug() << QCoreApplication::translate("Olop", "Server failed to listen on a port.");
exit(0);
return 0;
}

httpServer->route("/<arg>", [=](QUrl Url) {
return MAIN::lireFichier(dir+"/"+Url.toDisplayString());
});

httpServer->route("/stop", [=]() {
QTimer::singleShot(300, httpServer, [httpServer]() {
httpServer->deleteLater();
});
return "Application arrêtée";
});


// Ajouter le nouveau serveur HTTP à la liste
httpServers.append(httpServer);

Expand Down
3 changes: 3 additions & 0 deletions olop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

class MAIN {
public:
~MAIN();
static const QString HOME;
static const QString VERSION;
static const QString O_DIR;
Expand All @@ -25,6 +26,7 @@ class MAIN {
static bool copyFile(const QString& sourceFilePath, const QString& destinationFilePath);
static bool moveFile(const QString& sourceFilePath, const QString& destinationFilePath);
static bool deleteFile(const QString& filePath);
static bool supprimerDossier(const QString& cheminDossier);
static bool mkdir(QString path);
static QStringList getListOfFilesInDirectory(const QString& directoryPath);
static QString lireFichier(const QString& cheminFichier);
Expand All @@ -33,6 +35,7 @@ class MAIN {

class APP {
public:
~APP();
static QStringList decodeApp(const QString data);
static QStringList decodeApp(const QByteArray data);
static QString LIST(const QString& directoryPath);
Expand Down

0 comments on commit 7ccd005

Please sign in to comment.