From 4909844ad6f96d1de80302cf1f61addc83d2c0b8 Mon Sep 17 00:00:00 2001 From: Thilo Graf Date: Wed, 10 Dec 2014 10:41:34 +0100 Subject: [PATCH] COPKGManager: add member to handle line of shell lines prepared for use as callback in shell window class, currently it is not possible to get line output from shellwindow object, this should help soon also added ne locales --- data/locale/deutsch.locale | 2 +- data/locale/english.locale | 2 +- src/gui/opkg_manager.cpp | 70 +++++++++++++++++++--------------- src/gui/opkg_manager.h | 4 ++ src/gui/widget/shellwindow.cpp | 9 ++++- src/gui/widget/shellwindow.h | 46 +++++++++++++++++----- 6 files changed, 90 insertions(+), 43 deletions(-) diff --git a/data/locale/deutsch.locale b/data/locale/deutsch.locale index 015bff3db..03e18de81 100644 --- a/data/locale/deutsch.locale +++ b/data/locale/deutsch.locale @@ -1196,7 +1196,7 @@ menu.hint_next Weiter zum nächsten Menü.\nDie Taste 'Menü' schließt alle Men menu.hint_next_brief Weiter zum nächsten Menü menu.hint_numeric_adjust Bei numerischer Programmwahl die Kanalliste am neu gewählten Programm ausrichten menu.hint_opkg Software-Pakete installieren oder vorhandene aktualisieren -menu.hint_opkg_install_local_package Paket von USB-Stick, SD oder Freigabe installieren. +menu.hint_opkg_install_local_package Paket von USB-Stick, SD, Freigabe oder lokalem Ordner installieren. menu.hint_opkg_upgrade Aktualisiert alle installierten Pakete auf die neueste verfügbare Version menu.hint_osd Farben, Schriftarten, Anzeigegröße, Ansichtsoptionen der Menüs und mehr menu.hint_osd_language Wählen Sie ihre Menü-Sprache diff --git a/data/locale/english.locale b/data/locale/english.locale index 58195a60e..3e69ab85c 100644 --- a/data/locale/english.locale +++ b/data/locale/english.locale @@ -1196,7 +1196,7 @@ menu.hint_next Continue to next menu\nPress menu key to close all menus menu.hint_next_brief Continue to next menu menu.hint_numeric_adjust Adjust channel list mode on numeric zap menu.hint_opkg Install or update software packages -menu.hint_opkg_install_local_package Install package from USB, SD or share. +menu.hint_opkg_install_local_package Install package from USB, SD, share or local directory. menu.hint_opkg_upgrade Updates all installed packages to the most recent version available menu.hint_osd Colors, fonts, screen size\nGUI look and feel options menu.hint_osd_language Select OSD language diff --git a/src/gui/opkg_manager.cpp b/src/gui/opkg_manager.cpp index 38eb3a9a6..fc2407b95 100644 --- a/src/gui/opkg_manager.cpp +++ b/src/gui/opkg_manager.cpp @@ -191,7 +191,7 @@ int COPKGManager::exec(CMenuTarget* parent, const string &actionKey) force = "--force-reinstall "; } int r = execCmd(pkg_types[OM_INSTALL] + force + actionKey, true, true); - DisplayInfoMessage(actionKey.c_str()); + if (r) { showError(g_Locale->getText(LOCALE_OPKG_FAILURE_INSTALL), strerror(errno), pkg_types[OM_INSTALL] + force + actionKey); } else @@ -385,7 +385,6 @@ int COPKGManager::showMenu() if (badpackage(it->second.name)) continue; it->second.forwarder = new CMenuForwarder(it->second.desc, true, NULL , this, it->second.name.c_str()); - getPkgInfo(it->second.name, "Size"); it->second.forwarder->setHint("", it->second.desc); menu->addItem(it->second.forwarder); pkg_vec.push_back(&it->second); @@ -397,7 +396,7 @@ int COPKGManager::showMenu() menu->hide (); - if (installed) + if (!has_err && installed) DisplayInfoMessage(g_Locale->getText(LOCALE_OPKG_SUCCESS_INSTALL)); delete menu; return res; @@ -544,12 +543,15 @@ int COPKGManager::execCmd(const char *cmdstr, bool verbose, bool acknowledge) fprintf(stderr, "execCmd(%s)\n", cmdstr); string cmd = string(cmdstr); int res = 0; - bool has_err = false; + has_err = false; tmp_str.clear(); - string err_msg = ""; + err_msg = ""; if (verbose) { -// cmd += " 2>&1"; - CShellWindow(cmd, (verbose ? CShellWindow::VERBOSE : 0) | (acknowledge ? CShellWindow::ACKNOWLEDGE_MSG : 0), &res); + sigc::slot1 sl; + sl = sigc::mem_fun(*this, &COPKGManager::handleShellOutput); + CShellWindow shell(cmd, (verbose ? CShellWindow::VERBOSE : 0) | (acknowledge ? CShellWindow::ACKNOWLEDGE_MSG : 0), &res, false); + shell.OnShellOutputLoop.connect(sl); + shell.exec(); } else { cmd += " 2>&1"; pid_t pid = 0; @@ -564,41 +566,49 @@ int COPKGManager::execCmd(const char *cmdstr, bool verbose, bool acknowledge) string line(buf); trim(line); dprintf(DEBUG_INFO, "[COPKGManager] [%s - %d] %s [error %d]\n", __func__, __LINE__, line.c_str(), has_err); - - //check for collected errors and build a message for screen if errors available - if (has_err){ - dprintf(DEBUG_NORMAL, "[COPKGManager] [%s - %d] cmd: %s\nresult: %s\n", __func__, __LINE__, cmd.c_str(), line.c_str()); - size_t pos1 = line.find(" * "); - if (pos1 != string::npos){ - string str = line.substr(pos1, line.length()-pos1); - err_msg += str.replace(pos1, 3,"") + "\n"; - } - size_t pos01 = line.find("wget returned 4"); - //find obvious errors - if (pos01 != string::npos) - err_msg = "Network error! Online update not possible."; - }else{ - size_t pos2 = line.find("Collected errors:"); - if (pos2 != string::npos) - has_err = true; - } - if (!has_err) - tmp_str += line + "\n"; + handleShellOutput(line); } fclose(f); } if (has_err){ - DisplayErrorMessage(err_msg.c_str()); +// if (!verbose) +// DisplayErrorMessage(err_msg.c_str()); return -1; } return res; } -void COPKGManager::showError(const char* local_msg, char* err_msg, const string& command) +void COPKGManager::handleShellOutput(string& cur_line) +{ + //check for collected errors and build a message for screen if errors available + if (has_err){ + dprintf(DEBUG_NORMAL, "[COPKGManager] [%s - %d] result: %s\n", __func__, __LINE__, cur_line.c_str()); + size_t pos1 = cur_line.find(" * "); + if (pos1 != string::npos){ + string str = cur_line.substr(pos1, cur_line.length()-pos1); + err_msg += str.replace(pos1, 3,"") + "\n"; + } + + //find obvious errors + //download error: + size_t pos01 = cur_line.find("wget returned 4"); + if (pos01 != string::npos) + err_msg = "Network error! Online update not possible. Please check your network connection!\n"; + }else{ + size_t pos2 = cur_line.find("Collected errors:"); + if (pos2 != string::npos) + has_err = true; + } + if (!has_err) + tmp_str += cur_line + "\n"; +} + +void COPKGManager::showError(const char* local_msg, char* err_message, const string& command) { string msg = local_msg ? string(local_msg) + "\n" : ""; - msg += string(err_msg) + ":\n"; + msg += err_msg + "\n"; + msg += string(err_message) + ":\n"; msg += command; DisplayErrorMessage(msg.c_str()); } diff --git a/src/gui/opkg_manager.h b/src/gui/opkg_manager.h index edc7eef81..3c2298008 100644 --- a/src/gui/opkg_manager.h +++ b/src/gui/opkg_manager.h @@ -58,6 +58,9 @@ class COPKGManager : public CMenuTarget bool expert_mode; int menu_offset; std::string *local_dir; + + bool has_err; + std::string err_msg; int execCmd(const char* cmdstr, bool verbose = false, bool acknowledge = false); int execCmd(std::string cmdstr, bool verbose = false, bool acknowledge = false) { @@ -73,6 +76,7 @@ class COPKGManager : public CMenuTarget bool badpackage(std::string &s); void showError(const char* local_msg, char* err_msg, const std::string& command); int doUpdate(); + void handleShellOutput(std::string& cur_line); struct pkg { std::string name; diff --git a/src/gui/widget/shellwindow.cpp b/src/gui/widget/shellwindow.cpp index 60ac40d73..d7e4c7f9a 100644 --- a/src/gui/widget/shellwindow.cpp +++ b/src/gui/widget/shellwindow.cpp @@ -46,7 +46,7 @@ #include #include -CShellWindow::CShellWindow(const std::string &Command, const int Mode, int *Res) +CShellWindow::CShellWindow(const std::string &Command, const int Mode, int *Res, bool auto_exec) { textBox = NULL; frameBuffer = CFrameBuffer::getInstance(); @@ -55,7 +55,8 @@ CShellWindow::CShellWindow(const std::string &Command, const int Mode, int *Res) mode = Mode; res = Res; - exec(); + if (auto_exec) + exec(); } void CShellWindow::exec() @@ -152,6 +153,10 @@ void CShellWindow::exec() lines.push_back(line); incomplete = true; } + + //callback for line handler + std::string s_output = std::string((output)); + OnShellOutputLoop(s_output); if (lines.size() > lines_max) lines.pop_front(); txt = ""; diff --git a/src/gui/widget/shellwindow.h b/src/gui/widget/shellwindow.h index 130c7f8ce..45d4cda84 100644 --- a/src/gui/widget/shellwindow.h +++ b/src/gui/widget/shellwindow.h @@ -32,9 +32,18 @@ #include #include +#include -class CShellWindow +class CShellWindow : public sigc::trackable { + private: + int mode; + std::string command; + int* res; + CFrameBuffer *frameBuffer; + CTextBox *textBox; + void showResult(); + public: enum shellwindow_modes { @@ -42,16 +51,35 @@ class CShellWindow ACKNOWLEDGE = 2, ACKNOWLEDGE_MSG = 4 }; - CShellWindow(const std::string &Command, const int Mode = 0, int* Res = NULL); + CShellWindow(const std::string &Command, const int Mode = 0, int* Res = NULL, bool auto_exec = true); ~CShellWindow(); - private: - int mode; - std::string command; - int* res; - CFrameBuffer *frameBuffer; - CTextBox *textBox; void exec(); - void showResult(); + + /*! + signal/event handler runs on loop in exec method + this allows to use the shell output lines in other objects eg. for evaluation of error or status data + example for implamentation in your class: + ...your code... + //assuming in your class is declared a member function named YourMemberFunction(std::string& arg), parameter is a string as rev: + //Tis function should handle the shell output! + + //declare a slot with return value as 'void' and parameter as 'string', here by rev! + sigc::slot1 sl; + + //fill the slot with your member function in your class that do evaluate the output lines + sl = sigc::mem_fun(*this, &CYourClass::YourMemberFunction); + + //create the CShellWindow object in verbose mode, important: parameter 'auto_exec' must be set to 'false', so it is possible to connect the slot before engages the exec() methode + CShellWindow shell(cmd, (verbose ? CShellWindow::VERBOSE : 0) | (acknowledge ? CShellWindow::ACKNOWLEDGE_MSG : 0), &res, false); + + //connect slot + shell.OnShellOutputLoop.connect(sl); + + //now exec... + shell.exec(); + ...other code... + */ + sigc::signal OnShellOutputLoop; }; #endif