diff --git a/src/gui/network_service.cpp b/src/gui/network_service.cpp new file mode 100644 index 000000000..7902d6018 --- /dev/null +++ b/src/gui/network_service.cpp @@ -0,0 +1,156 @@ +/* + Neutrino-GUI - DBoxII-Project + + Copyright (C) 2011 CoolStream International Ltd + + License: GPLv2 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include + +#include +#include "gui/network_service.h" +#include "mymenu.h" + +#include + +#include + +#define TOUCH_BASE "/var/etc/." + +struct network_service +{ + std::string name; + std::string cmd; + std::string options; + int enabled; +}; + +#define SERVICE_COUNT 4 +static struct network_service services[SERVICE_COUNT] = +{ + { "FTP", "vsftpd", "", 0 }, + { "Telnet", "telnetd", "-l/bin/login", 0 }, + { "DjMount", "djmount", "-o iocharset=utf8 /media/00upnp/", 0 }, + { "uShare", "ushare", "-D", 0 } +}; + +CNetworkService::CNetworkService(std::string cmd, std::string opts) +{ + command = cmd; + options = opts; + enabled = false; + + std::string file = TOUCH_BASE + cmd; + if (!access(file.c_str(), F_OK)) + enabled = true; +} + +void CNetworkService::Start() +{ + std::string cmd = command + " " + options; + printf("CNetworkService::Start: %s\n", cmd.c_str()); + system(cmd.c_str()); + enabled = true; + TouchFile(); +} + +void CNetworkService::Stop() +{ + std::string cmd = "killall " + command; + printf("CNetworkService::Stop: %s\n", cmd.c_str()); + system(cmd.c_str()); + enabled = false; + TouchFile(); +} + +void CNetworkService::TouchFile() +{ + std::string file = TOUCH_BASE + command; + printf("CNetworkService::TouchFile: %s %s\n", enabled ? "create" : "remove", file.c_str()); + if(enabled) { + FILE * fp = fopen(file.c_str(), "w"); + if (fp) + fclose(fp); + } else { + unlink(file.c_str()); + } +} + +bool CNetworkService::changeNotify(const neutrino_locale_t /*OptionName*/, void * data) +{ + int value = *(int *)data; + + printf("CNetworkService::changeNotify: %d (enabled %d)\n", value, enabled); + if (value != 0) + Start(); + else + Stop(); + + return false; +} + +CNetworkServiceSetup::CNetworkServiceSetup() +{ + width = w_max (40, 10); + selected = -1; +} + +CNetworkServiceSetup::~CNetworkServiceSetup() +{ +} + +int CNetworkServiceSetup::exec(CMenuTarget* parent, const std::string & /*actionKey*/) +{ + dprintf(DEBUG_DEBUG, "init network services setup menu\n"); + + if (parent) + parent->hide(); + + return showNetworkServiceSetup(); +} + +int CNetworkServiceSetup::showNetworkServiceSetup() +{ + int shortcut = 1; + + CMenuWidget* setup = new CMenuWidget(LOCALE_MAINSETTINGS_NETWORK, NEUTRINO_ICON_SETTINGS, width); + setup->setSelected(selected); + setup->addIntroItems(LOCALE_NETWORKMENU_SERVICES); + + CNetworkService * items[SERVICE_COUNT]; + + for(unsigned i = 0; i < SERVICE_COUNT; i++) { + items[i] = new CNetworkService(services[i].cmd, services[i].options); + services[i].enabled = items[i]->Enabled(); + setup->addItem(new CMenuOptionChooser(services[i].name.c_str(), &services[i].enabled, OPTIONS_OFF0_ON1_OPTIONS, OPTIONS_OFF0_ON1_OPTION_COUNT, true, items[i], CRCInput::convertDigitToKey(shortcut++), "")); + } + + int res = setup->exec (NULL, ""); + selected = setup->getSelected(); + delete setup; + + for(unsigned i = 0; i < SERVICE_COUNT; i++) + delete items[i]; + + return res; +} diff --git a/src/gui/network_service.h b/src/gui/network_service.h new file mode 100644 index 000000000..f186046c1 --- /dev/null +++ b/src/gui/network_service.h @@ -0,0 +1,61 @@ +/* + Neutrino-GUI - DBoxII-Project + + Copyright (C) 2011 CoolStream International Ltd + + License: GPLv2 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#ifndef __network_service__ +#define __network_service__ + +#include + +#include + +class CNetworkService : public CChangeObserver +{ + private: + std::string command; + std::string options; + bool enabled; + + void Stop(); + void Start(); + void TouchFile(); + + public: + CNetworkService(std::string cmd, std::string options); + bool Enabled() { return enabled; } + + bool changeNotify(const neutrino_locale_t OptionName, void * /*data*/); +}; + +class CNetworkServiceSetup : public CMenuTarget +{ + private: + int width, selected; + + int showNetworkServiceSetup(); + + public: + CNetworkServiceSetup(); + ~CNetworkServiceSetup(); + + int exec(CMenuTarget* parent, const std::string & actionKey); +}; + +#endif