From 45817b617d6e37d74fad29565250311e0508a6b9 Mon Sep 17 00:00:00 2001 From: skyjake Date: Mon, 4 Feb 2013 22:21:48 +0200 Subject: [PATCH] Shell: Added PersistentData and updating menu with found servers --- .../tools/shell/shell-text/shell-text.pro | 8 +-- .../shell/shell-text/src/aboutdialog.cpp | 2 +- .../shell-text/src/localserverdialog.cpp | 19 ++++++- .../shell/shell-text/src/localserverdialog.h | 1 + .../shell-text/src/openconnectiondialog.cpp | 13 +++++ .../shell-text/src/openconnectiondialog.h | 3 ++ .../shell/shell-text/src/persistentdata.cpp | 52 +++++++++++++++++++ .../shell/shell-text/src/persistentdata.h | 42 +++++++++++++++ .../tools/shell/shell-text/src/shellapp.cpp | 46 ++++++++++++++-- .../tools/shell/shell-text/src/shellapp.h | 3 +- 10 files changed, 178 insertions(+), 11 deletions(-) create mode 100644 doomsday/tools/shell/shell-text/src/persistentdata.cpp create mode 100644 doomsday/tools/shell/shell-text/src/persistentdata.h diff --git a/doomsday/tools/shell/shell-text/shell-text.pro b/doomsday/tools/shell/shell-text/shell-text.pro index 1d78b5f558..d1b1b735bf 100644 --- a/doomsday/tools/shell/shell-text/shell-text.pro +++ b/doomsday/tools/shell/shell-text/shell-text.pro @@ -15,7 +15,7 @@ VERSION = 1.0.0 CONFIG -= app_bundle -DEFINES += LIBSHELL_VERSION=\\\"$$VERSION\\\" +DEFINES += SHELL_VERSION=\\\"$$VERSION\\\" include(../../../dep_deng2.pri) include(../../../dep_shell.pri) @@ -33,7 +33,8 @@ HEADERS += \ src/main.h \ src/openconnectiondialog.h \ src/shellapp.h \ - src/statuswidget.h + src/statuswidget.h \ + src/persistentdata.h SOURCES += \ src/aboutdialog.cpp \ @@ -45,7 +46,8 @@ SOURCES += \ src/main.cpp \ src/openconnectiondialog.cpp \ src/shellapp.cpp \ - src/statuswidget.cpp + src/statuswidget.cpp \ + src/persistentdata.cpp # Installation -------------------------------------------------------------- diff --git a/doomsday/tools/shell/shell-text/src/aboutdialog.cpp b/doomsday/tools/shell/shell-text/src/aboutdialog.cpp index b21dfd4981..8f94c7230f 100644 --- a/doomsday/tools/shell/shell-text/src/aboutdialog.cpp +++ b/doomsday/tools/shell/shell-text/src/aboutdialog.cpp @@ -28,7 +28,7 @@ AboutDialog::AboutDialog() label->setLabel(tr("Doomsday Shell %1\nCopyright (c) %2\n\n" "The Shell is a utility for controlling and monitoring " "Doomsday servers using a text-based (curses) user interface.") - .arg(LIBSHELL_VERSION) + .arg(SHELL_VERSION) .arg("2013 Deng Team")); label->setExpandsToFitLines(true); diff --git a/doomsday/tools/shell/shell-text/src/localserverdialog.cpp b/doomsday/tools/shell/shell-text/src/localserverdialog.cpp index 4bf134c928..e73c586a5b 100644 --- a/doomsday/tools/shell/shell-text/src/localserverdialog.cpp +++ b/doomsday/tools/shell/shell-text/src/localserverdialog.cpp @@ -17,6 +17,7 @@ */ #include "localserverdialog.h" +#include "persistentdata.h" #include #include #include @@ -88,7 +89,6 @@ LocalServerDialog::LocalServerDialog() : d(new Instance) .setInput(Rule::Top, label().rule().bottom() + 1); d->port->setPrompt(tr("TCP port: ")); - d->port->setText("13209"); d->port->rule() .setInput(Rule::Width, Const(16)) @@ -110,6 +110,11 @@ LocalServerDialog::LocalServerDialog() : d(new Instance) setPrompt(tr("Options: ")); setAcceptLabel(tr("Start local server")); + + // Values. + d->choice->select (PersistentData::geti("LocalServer.gameMode")); + d->port->setText (PersistentData::get ("LocalServer.port", "13209")); + lineEdit().setText(PersistentData::get ("LocalServer.options")); } LocalServerDialog::~LocalServerDialog() @@ -133,3 +138,15 @@ void LocalServerDialog::prepare() root().setFocus(d->choice); } + +void LocalServerDialog::finish(int result) +{ + InputDialog::finish(result); + + if(result) + { + PersistentData::set("LocalServer.gameMode", d->choice->selection()); + PersistentData::set("LocalServer.port", d->port->text()); + PersistentData::set("LocalServer.options", lineEdit().text()); + } +} diff --git a/doomsday/tools/shell/shell-text/src/localserverdialog.h b/doomsday/tools/shell/shell-text/src/localserverdialog.h index bcb805fb19..ff6fc8d6e2 100644 --- a/doomsday/tools/shell/shell-text/src/localserverdialog.h +++ b/doomsday/tools/shell/shell-text/src/localserverdialog.h @@ -34,6 +34,7 @@ class LocalServerDialog : public de::shell::InputDialog protected: void prepare(); + void finish(int result); private: struct Instance; diff --git a/doomsday/tools/shell/shell-text/src/openconnectiondialog.cpp b/doomsday/tools/shell/shell-text/src/openconnectiondialog.cpp index d51be045fe..b310b9d66c 100644 --- a/doomsday/tools/shell/shell-text/src/openconnectiondialog.cpp +++ b/doomsday/tools/shell/shell-text/src/openconnectiondialog.cpp @@ -17,6 +17,8 @@ */ #include "openconnectiondialog.h" +#include "persistentdata.h" +#include using namespace de; @@ -28,6 +30,7 @@ OpenConnectionDialog::OpenConnectionDialog(String const &name) : shell::InputDia "\"10.0.1.1:13209\".")); setPrompt(tr("Address: ")); + lineEdit().setText(PersistentData::get("OpenConnection.address")); setAcceptLabel(tr("Connect to server")); } @@ -36,3 +39,13 @@ String OpenConnectionDialog::address() { return text(); } + +void OpenConnectionDialog::finish(int result) +{ + InputDialog::finish(result); + + if(result) + { + PersistentData::set("OpenConnection.address", text()); + } +} diff --git a/doomsday/tools/shell/shell-text/src/openconnectiondialog.h b/doomsday/tools/shell/shell-text/src/openconnectiondialog.h index 61166199e7..6009c8fac8 100644 --- a/doomsday/tools/shell/shell-text/src/openconnectiondialog.h +++ b/doomsday/tools/shell/shell-text/src/openconnectiondialog.h @@ -34,6 +34,9 @@ class OpenConnectionDialog : public de::shell::InputDialog * was rejected, the returned string is empy. */ de::String address(); + +protected: + void finish(int result); }; #endif // OPENCONNECTIONDIALOG_H diff --git a/doomsday/tools/shell/shell-text/src/persistentdata.cpp b/doomsday/tools/shell/shell-text/src/persistentdata.cpp new file mode 100644 index 0000000000..8c198153ba --- /dev/null +++ b/doomsday/tools/shell/shell-text/src/persistentdata.cpp @@ -0,0 +1,52 @@ +/** @file persistentdata.cpp Data that persists even after restarting the app. + * + * @authors Copyright © 2013 Jaakko Keränen + * + * @par License + * GPL: http://www.gnu.org/licenses/gpl.html + * + * 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; either version 2 of the License, or (at your + * option) any later version. 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, see: + * http://www.gnu.org/licenses + */ + +#include "persistentdata.h" +#include + +using namespace de; + +PersistentData::PersistentData() +{} + +PersistentData::~PersistentData() +{} + +void PersistentData::set(String const &name, String const &value) +{ + QSettings st; + st.setValue(name, value); +} + +void PersistentData::set(String const &name, int value) +{ + QSettings st; + st.setValue(name, value); +} + +String PersistentData::get(String const &name, String const &defaultValue) +{ + QSettings st; + return st.value(name, defaultValue).toString(); +} + +int PersistentData::geti(String const &name, int defaultValue) +{ + QSettings st; + return st.value(name, defaultValue).toInt(); +} diff --git a/doomsday/tools/shell/shell-text/src/persistentdata.h b/doomsday/tools/shell/shell-text/src/persistentdata.h new file mode 100644 index 0000000000..ca0c4e2dda --- /dev/null +++ b/doomsday/tools/shell/shell-text/src/persistentdata.h @@ -0,0 +1,42 @@ +/** @file persistentdata.h Data that persists even after restarting the app. + * + * @authors Copyright © 2013 Jaakko Keränen + * + * @par License + * GPL: http://www.gnu.org/licenses/gpl.html + * + * 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; either version 2 of the License, or (at your + * option) any later version. 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, see: + * http://www.gnu.org/licenses + */ + +#ifndef PERSISTENTDATA_H +#define PERSISTENTDATA_H + +#include + +/** + * Data that persists even after restarting the app. + * + * A singleton class. + */ +class PersistentData +{ +public: + PersistentData(); + ~PersistentData(); + + static void set(de::String const &name, de::String const &value); + static void set(de::String const &name, int value); + + static de::String get(de::String const &name, de::String const &defaultValue = ""); + static int geti(de::String const &name, int defaultValue = 0); +}; + +#endif // PERSISTENTDATA_H diff --git a/doomsday/tools/shell/shell-text/src/shellapp.cpp b/doomsday/tools/shell/shell-text/src/shellapp.cpp index 9d25e56a93..c3d33e9e9d 100644 --- a/doomsday/tools/shell/shell-text/src/shellapp.cpp +++ b/doomsday/tools/shell/shell-text/src/shellapp.cpp @@ -23,6 +23,7 @@ #include "openconnectiondialog.h" #include "localserverdialog.h" #include "aboutdialog.h" +#include "persistentdata.h" #include #include #include @@ -38,6 +39,7 @@ using namespace shell; struct ShellApp::Instance { ShellApp &self; + PersistentData persist; MenuWidget *menu; LogWidget *log; CommandLineWidget *cli; @@ -93,12 +95,10 @@ struct ShellApp::Instance // Main menu. menu = new MenuWidget(MenuWidget::Popup); menu->appendItem(new Action(tr("Connect to..."), - KeyEvent("o"), - &self, SLOT(askToOpenConnection())), "O"); + &self, SLOT(askToOpenConnection()))); menu->appendItem(new Action(tr("Disconnect"), &self, SLOT(closeConnection()))); menu->appendSeparator(); menu->appendItem(new Action(tr("Start local server"), &self, SLOT(askToStartLocalServer()))); - menu->appendItem(new Action(tr("Look for servers"), &self, SLOT(lookForServers()))); menu->appendSeparator(); menu->appendItem(new Action(tr("About"), &self, SLOT(showAbout()))); menu->appendItem(new Action(tr("Quit Shell"), @@ -120,6 +120,7 @@ struct ShellApp::Instance // Signals. QObject::connect(cli, SIGNAL(commandEntered(de::String)), &self, SLOT(sendCommandToServer(de::String))); QObject::connect(menu, SIGNAL(closed()), &self, SLOT(menuClosed())); + QObject::connect(&finder, SIGNAL(updated()), &self, SLOT(updateMenuWithFoundServers())); } ~Instance() @@ -131,6 +132,13 @@ struct ShellApp::Instance ShellApp::ShellApp(int &argc, char **argv) : CursesApp(argc, argv), d(new Instance(*this)) { + // Metadata. + setOrganizationDomain ("dengine.net"); + setOrganizationName ("Deng Team"); + setApplicationName ("doomsday-shell-text"); + setApplicationVersion (SHELL_VERSION); + + // Configure the log buffer. LogBuffer &buf = LogBuffer::appBuffer(); buf.setMaxEntryCount(50); // buffered here rather than appBuffer buf.addSink(d->log->logSink()); @@ -211,9 +219,37 @@ void ShellApp::askToStartLocalServer() } } -void ShellApp::lookForServers() +void ShellApp::updateMenuWithFoundServers() +{ + // Remove old servers. + for(int i = 2; i < d->menu->itemCount() - 3; ++i) + { + if(d->menu->itemAction(i).label()[0].isDigit()) + { + d->menu->removeItem(i); + --i; + } + } + + int pos = 2; + foreach(Address const &sv, d->finder.foundServers()) + { + String label = sv.asText() + String(" (%1; %2/%3)") + .arg(d->finder.name(sv).left(20)) + .arg(d->finder.playerCount(sv)) + .arg(d->finder.maxPlayers(sv)); + + d->menu->insertItem(pos++, new Action(label, this, SLOT(connectToFoundServer()))); + } +} + +void ShellApp::connectToFoundServer() { - d->finder.start(); + String label = d->menu->itemAction(d->menu->cursor()).label(); + + LOG_INFO("Selected: ") << label; + + openConnection(label.left(label.indexOf('(') - 1)); } void ShellApp::sendCommandToServer(String command) diff --git a/doomsday/tools/shell/shell-text/src/shellapp.h b/doomsday/tools/shell/shell-text/src/shellapp.h index e1f4ef6c2b..b89a8ee265 100644 --- a/doomsday/tools/shell/shell-text/src/shellapp.h +++ b/doomsday/tools/shell/shell-text/src/shellapp.h @@ -37,7 +37,8 @@ public slots: void showAbout(); void askToOpenConnection(); void askToStartLocalServer(); - void lookForServers(); + void updateMenuWithFoundServers(); + void connectToFoundServer(); void closeConnection(); void sendCommandToServer(de::String command); void handleIncomingPackets();