diff --git a/lib/applications/src/app.cpp b/lib/applications/src/app.cpp index 9d8f4ffa..9fde9dbd 100644 --- a/lib/applications/src/app.cpp +++ b/lib/applications/src/app.cpp @@ -12,7 +12,10 @@ namespace AppManager { void App::run(const std::vector& parameters) { if (!auth) { - throw libsystem::exceptions::RuntimeError("App is not authorized to run"); + requestAuth(); + + if (!auth) + return; } app_state = background ? RUNNING_BACKGROUND : RUNNING; @@ -84,7 +87,7 @@ namespace AppManager { std::string data = stream.read(); stream.close(); - label->setText(data); + label->setText("Voulez-vous autoriser l'application " + fullName + " à accéder aux permissions suivantes:\n" + data); win.addChild(label); auto *btn = new Button(35, 420, 250, 38); @@ -95,18 +98,16 @@ namespace AppManager { while (true) { win.updateAll(); + eventHandlerApp.update(); - if (btn->isTouched()) { - storage::FileStream streamP((storage::Path(PERMS_DIR) / "auth.list").str(), storage::APPEND); - streamP.write(path.str() + "\n"); - streamP.close(); - - manifest = storage::Path(PERMS_DIR) / (name + ".json"); - auth = true; + if(hardware::getHomeButton()) + { + break; + } - storage::FileStream newPermCopy(manifest.str(), storage::WRITE); - newPermCopy.write(data); - newPermCopy.close(); + if (btn->isTouched()) { + AppManager::addPermission(this); + break; } } } @@ -178,24 +179,20 @@ namespace AppManager { return 0; } - void askGui(const LuaFile* lua) { - App* app = lua->app; + void addPermission(App* app) { + app->auth = true; - /*if (lua->lua_gui.mainWindow == nullptr) { - for (auto it = appStack.begin(); it != appStack.end(); ++it) { - if (*it == app) { - app->app_state = App::AppState::NOT_RUNNING; - appStack.erase(it); - break; - } - } + storage::FileStream stream((storage::Path(PERMS_DIR) / "auth.list").str(), storage::APPEND); + stream.write(app->path.str() + "\n"); + stream.close(); - return; - }*/ + storage::FileStream oman(app->manifest.str(), storage::READ); + std::string manifest = oman.read(); + oman.close(); - // if (appStack.empty() || appStack.back() != app) { - // appStack.push_back(app); - // } + storage::FileStream nman((storage::Path(PERMS_DIR) / (app->fullName + ".json")).str(), storage::WRITE); + nman.write(manifest); + nman.close(); } void loadDir(const storage::Path& directory, bool root = false, std::string prefix = "") { @@ -237,7 +234,7 @@ namespace AppManager { directory / dir / "manifest.json", true ); - } else if (allowedFiles.find(appPath.str()) != std::string::npos) { + } else if (allowedFiles.find(appPath.str() + "\n") != std::string::npos) { app = std::make_shared( dir, storage::Path(directory) / dir / "app.lua", @@ -248,7 +245,7 @@ namespace AppManager { app = std::make_shared( dir, storage::Path(directory) / dir / "app.lua", - storage::Path(directory) / fullname / "manifest.json", + storage::Path(directory) / dir / "manifest.json", false ); } @@ -279,8 +276,6 @@ namespace AppManager { std::cerr << "Error: subdir \"" << (app.get()->path / "../" / manifest["subdir"]).str() << "\" does not exist" << std::endl; } - // Add app to list - //libsystem::log("Loaded app : " + app->toString() + "."); appList.push_back(app); if (manifest["autorun"].is_boolean()) { diff --git a/lib/applications/src/app.hpp b/lib/applications/src/app.hpp index 856b73ad..75292cbe 100644 --- a/lib/applications/src/app.hpp +++ b/lib/applications/src/app.hpp @@ -11,7 +11,7 @@ #define APP_DIR "/apps" #define SYSTEM_APP_DIR "/sys_apps" -#define PERMS_DIR "/system" +#define PERMS_DIR "/system/permissions" namespace AppManager @@ -109,7 +109,7 @@ namespace AppManager extern std::vector appStack; // stack of the apps that are using the GUI, the last one is shown on the screen int pushError(lua_State* L, sol::optional maybe_exception, sol::string_view description); - void askGui(const LuaFile* lua); + void addPermission(App* app); bool isAnyVisibleApp(); void init(); diff --git a/lib/lua/src/lua_gui.cpp b/lib/lua/src/lua_gui.cpp index e2e5657f..4c4dfb4d 100644 --- a/lib/lua/src/lua_gui.cpp +++ b/lib/lua/src/lua_gui.cpp @@ -205,7 +205,6 @@ std::string LuaGui::keyboard(const std::string &placeholder, const std::string & void LuaGui::setMainWindow(LuaWindow *window) { this->mainWindow = window; - AppManager::askGui(this->lua); } LuaWindow *LuaGui::getMainWindow() diff --git a/lib/storage/path.cpp b/lib/storage/path.cpp index 443ac416..0ab5e04c 100644 --- a/lib/storage/path.cpp +++ b/lib/storage/path.cpp @@ -394,4 +394,96 @@ namespace storage return (::rename(this->str().c_str(), to.str().c_str()) == 0); #endif } + + bool Path::copy(const Path &to) + { + #if defined(__linux__) || defined(_WIN32) || defined(_WIN64) || defined(__APPLE__) + try { + if (this->isfile()) { + // Copy single file + std::filesystem::copy_file(this->str(), to.str(), + std::filesystem::copy_options::overwrite_existing); + } + else if (this->isdir()) { + // Copy directory and its contents recursively + std::filesystem::copy(this->str(), to.str(), + std::filesystem::copy_options::recursive | + std::filesystem::copy_options::overwrite_existing); + } + return true; + } + catch (const std::filesystem::filesystem_error& e) { + std::cerr << "Copy failed: " << e.what() << std::endl; + return false; + } + #endif + + #ifdef ESP_PLATFORM + if (!this->exists()) { + return false; + } + + if (this->isfile()) { + // Copy single file + FILE* source = fopen(this->str().c_str(), "rb"); + if (!source) { + return false; + } + + FILE* dest = fopen(to.str().c_str(), "wb"); + if (!dest) { + fclose(source); + return false; + } + + const size_t bufferSize = 1024; + uint8_t buffer[bufferSize]; + size_t bytesRead; + + while ((bytesRead = fread(buffer, 1, bufferSize, source)) > 0) { + if (fwrite(buffer, 1, bytesRead, dest) != bytesRead) { + fclose(source); + fclose(dest); + return false; + } + } + + fclose(source); + fclose(dest); + return true; + } + else if (this->isdir()) { + // Create destination directory + if (!to.exists() && !to.newdir()) { + return false; + } + + // Copy directory contents recursively + std::vector entries = this->listdir(false); + bool success = true; + + for (const auto& entry : entries) { + Path sourcePath = *this / entry; + Path destPath = to / entry; + + if (sourcePath.isfile()) { + success &= sourcePath.copy(destPath); + } + else if (sourcePath.isdir()) { + if (!destPath.exists() && !destPath.newdir()) { + success = false; + break; + } + success &= sourcePath.copy(destPath); + } + } + + return success; + } + + return false; + #endif + + return false; + } } diff --git a/lib/storage/path.hpp b/lib/storage/path.hpp index 4fc4a577..dcfdfeec 100644 --- a/lib/storage/path.hpp +++ b/lib/storage/path.hpp @@ -48,6 +48,7 @@ namespace storage { bool remove (void) const; bool rename (const Path& to); + bool copy (const Path& to); std::vector m_steps; private: diff --git a/storage/apps/alarme/sub/.task/app.lua b/storage/apps/alarme/sub/.task/app.lua index 9a4d4f25..10521287 100644 --- a/storage/apps/alarme/sub/.task/app.lua +++ b/storage/apps/alarme/sub/.task/app.lua @@ -1,12 +1,10 @@ global_list = {} function checkAlarm() - print("checkAlarm") for i, v in ipairs(global_list) do local h = tonumber(v.time:sub(1, 2)) local m = tonumber(v.time:sub(4, 5)) - print("Comparing: " .. h .. " with " .. time:get("h")[1] .. " and " .. m .. " with " .. time:get("mi")[1]) if(h == time:get("h")[1] and m == time:get("mi")[1] and v.enabled == 1) then global_list[i].enabled = 0 saveTable("../../alarms.tab", global_list) diff --git a/storage/sys_apps/testbc/app.lua b/storage/sys_apps/testbc/app.lua deleted file mode 100644 index 51de30b0..00000000 --- a/storage/sys_apps/testbc/app.lua +++ /dev/null @@ -1,6 +0,0 @@ -function run() - print("this app in not in the background, will launch sub1 app for 5 seconds") - - window = gui:window() - gui:setWindow(window) -end \ No newline at end of file diff --git a/storage/sys_apps/testbc/manifest.json b/storage/sys_apps/testbc/manifest.json deleted file mode 100644 index 79717348..00000000 --- a/storage/sys_apps/testbc/manifest.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "access": [ - "gui", - "files", - "time" - ], - "background": false, - "subdir": "sub" -} \ No newline at end of file diff --git a/storage/sys_apps/testbc/sub/.sub1/app.lua b/storage/sys_apps/testbc/sub/.sub1/app.lua deleted file mode 100644 index b72a1e2f..00000000 --- a/storage/sys_apps/testbc/sub/.sub1/app.lua +++ /dev/null @@ -1,6 +0,0 @@ -function run() - time:setTimeout(function() - launch("testbc") - system.app:quit() - end, 1000) -end diff --git a/storage/sys_apps/testbc/sub/.sub1/manifest.json b/storage/sys_apps/testbc/sub/.sub1/manifest.json deleted file mode 100644 index 234d340e..00000000 --- a/storage/sys_apps/testbc/sub/.sub1/manifest.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "access": [ - "gui", - "files", - "time" - ], - "background": false, - "autorun": true -} \ No newline at end of file diff --git a/storage/system/.keyboard_test.json b/storage/system/.keyboard_test.json deleted file mode 100755 index 57f2f44d..00000000 --- a/storage/system/.keyboard_test.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "access": [ - "gui" - ] -} diff --git a/storage/system/battery.json b/storage/system/battery.json deleted file mode 100644 index 85a54e41..00000000 --- a/storage/system/battery.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "access": [ - "gui", - "files" - ] -} diff --git a/storage/system/demineur.json b/storage/system/demineur.json deleted file mode 100644 index 6ce7f31b..00000000 --- a/storage/system/demineur.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "access": ["files", "gsm", "gui", "hardware", "time", "web", "web_paxo"] -} diff --git a/storage/system/horloge.json b/storage/system/horloge.json deleted file mode 100644 index 85c66393..00000000 --- a/storage/system/horloge.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "access": ["files", "files_root", "contacts", "os", "gsm", "gui", "hardware", "time", "web", "web_paxo"] -} diff --git a/storage/system/keyboard_test.json b/storage/system/keyboard_test.json deleted file mode 100644 index 57f2f44d..00000000 --- a/storage/system/keyboard_test.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "access": [ - "gui" - ] -} diff --git a/storage/system/alarme..task.json b/storage/system/permissions/alarme..task.json similarity index 100% rename from storage/system/alarme..task.json rename to storage/system/permissions/alarme..task.json diff --git a/storage/system/alarme.json b/storage/system/permissions/alarme.json similarity index 100% rename from storage/system/alarme.json rename to storage/system/permissions/alarme.json diff --git a/storage/system/auth.list b/storage/system/permissions/auth.list similarity index 56% rename from storage/system/auth.list rename to storage/system/permissions/auth.list index 2aae6556..55ce59d1 100644 --- a/storage/system/auth.list +++ b/storage/system/permissions/auth.list @@ -1,13 +1,14 @@ +./storage/apps/alarme/sub/.task/app.lua +./storage/apps/snake/app.lua +./storage/apps/demineur/app.lua ./storage/apps/phone/app.lua -./storage/apps/test/app.lua +./storage/apps/calendrier/app.lua +./storage/apps/alarme/sub/.task/app.lua +./storage/apps/alarme/app.lua +./storage/apps/calendrier/app.lua ./storage/apps/messages/app.lua -./storage/apps/snake/app.lua +./storage/apps/phone/app.lua +./storage/apps/demineur/app.lua ./storage/apps/contacts/app.lua -./storage/apps/battery/app.lua -./storage/apps/keyboard_test/app.lua -./storage/apps/horloge/app.lua -./storage/apps/alarme/app.lua -./storage/apps/alarme/sub/.task/app.lua +./storage/apps/snake/app.lua ./storage/apps/calculatrice/app.lua -./storage/apps/demineur/app.lua -./storage/apps/calendrier/app.lua \ No newline at end of file diff --git a/storage/system/permissions/calculatrice.json b/storage/system/permissions/calculatrice.json new file mode 100644 index 00000000..d021a75b --- /dev/null +++ b/storage/system/permissions/calculatrice.json @@ -0,0 +1,3 @@ +{ + "access": ["files", "files_root", "os", "gsm", "gui", "time"] +} \ No newline at end of file diff --git a/storage/system/calendrier.json b/storage/system/permissions/calendrier.json similarity index 56% rename from storage/system/calendrier.json rename to storage/system/permissions/calendrier.json index aa89649f..a497aa5d 100644 --- a/storage/system/calendrier.json +++ b/storage/system/permissions/calendrier.json @@ -1,3 +1,4 @@ { - "access": ["files", "files_root", "gsm", "gui", "hardware", "time", "web", "web_paxo"] + "access": ["files", "files_root", "gsm", "gui", "hardware", "time", "web", "web_paxo"], + "name": "Calendrier" } diff --git a/storage/system/contacts.json b/storage/system/permissions/contacts.json similarity index 97% rename from storage/system/contacts.json rename to storage/system/permissions/contacts.json index aa89649f..581df353 100644 --- a/storage/system/contacts.json +++ b/storage/system/permissions/contacts.json @@ -1,3 +1,3 @@ { "access": ["files", "files_root", "gsm", "gui", "hardware", "time", "web", "web_paxo"] -} +} \ No newline at end of file diff --git a/storage/system/calculatrice.json b/storage/system/permissions/demineur.json similarity index 56% rename from storage/system/calculatrice.json rename to storage/system/permissions/demineur.json index aa89649f..66da4a13 100644 --- a/storage/system/calculatrice.json +++ b/storage/system/permissions/demineur.json @@ -1,3 +1,4 @@ { - "access": ["files", "files_root", "gsm", "gui", "hardware", "time", "web", "web_paxo"] + "access": ["files", "files_root", "gsm", "gui", "hardware", "time", "web", "web_paxo"], + "name": "Démineur" } diff --git a/storage/system/messages.json b/storage/system/permissions/messages.json similarity index 57% rename from storage/system/messages.json rename to storage/system/permissions/messages.json index aa89649f..1ebec42d 100644 --- a/storage/system/messages.json +++ b/storage/system/permissions/messages.json @@ -1,3 +1,4 @@ { - "access": ["files", "files_root", "gsm", "gui", "hardware", "time", "web", "web_paxo"] + "access": ["files", "files_root", "gsm", "gui", "hardware", "time", "web", "web_paxo"], + "name": "Messages" } diff --git a/storage/system/phone.json b/storage/system/permissions/phone.json similarity index 54% rename from storage/system/phone.json rename to storage/system/permissions/phone.json index aa89649f..2b10d3d4 100644 --- a/storage/system/phone.json +++ b/storage/system/permissions/phone.json @@ -1,3 +1,4 @@ { - "access": ["files", "files_root", "gsm", "gui", "hardware", "time", "web", "web_paxo"] -} + "access": ["files", "files_root", "gsm", "gui", "hardware", "time", "web", "web_paxo"], + "name": "Téléphone" +} \ No newline at end of file diff --git a/storage/system/snake.json b/storage/system/permissions/snake.json similarity index 97% rename from storage/system/snake.json rename to storage/system/permissions/snake.json index aa89649f..581df353 100644 --- a/storage/system/snake.json +++ b/storage/system/permissions/snake.json @@ -1,3 +1,3 @@ { "access": ["files", "files_root", "gsm", "gui", "hardware", "time", "web", "web_paxo"] -} +} \ No newline at end of file diff --git a/storage/system/test.json b/storage/system/test.json deleted file mode 100644 index 6ce7f31b..00000000 --- a/storage/system/test.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "access": ["files", "gsm", "gui", "hardware", "time", "web", "web_paxo"] -} diff --git a/storage/system/test2.json b/storage/system/test2.json deleted file mode 100644 index aa89649f..00000000 --- a/storage/system/test2.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "access": ["files", "files_root", "gsm", "gui", "hardware", "time", "web", "web_paxo"] -}