Skip to content

Commit

Permalink
Shell: Added PersistentData and updating menu with found servers
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Feb 4, 2013
1 parent 3b29b76 commit 45817b6
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 11 deletions.
8 changes: 5 additions & 3 deletions doomsday/tools/shell/shell-text/shell-text.pro
Expand Up @@ -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)
Expand All @@ -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 \
Expand All @@ -45,7 +46,8 @@ SOURCES += \
src/main.cpp \
src/openconnectiondialog.cpp \
src/shellapp.cpp \
src/statuswidget.cpp
src/statuswidget.cpp \
src/persistentdata.cpp

# Installation --------------------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion doomsday/tools/shell/shell-text/src/aboutdialog.cpp
Expand Up @@ -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);
Expand Down
19 changes: 18 additions & 1 deletion doomsday/tools/shell/shell-text/src/localserverdialog.cpp
Expand Up @@ -17,6 +17,7 @@
*/

#include "localserverdialog.h"
#include "persistentdata.h"
#include <de/shell/TextRootWidget>
#include <de/shell/LabelWidget>
#include <de/shell/MenuWidget>
Expand Down Expand Up @@ -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))
Expand All @@ -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()
Expand All @@ -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());
}
}
1 change: 1 addition & 0 deletions doomsday/tools/shell/shell-text/src/localserverdialog.h
Expand Up @@ -34,6 +34,7 @@ class LocalServerDialog : public de::shell::InputDialog

protected:
void prepare();
void finish(int result);

private:
struct Instance;
Expand Down
13 changes: 13 additions & 0 deletions doomsday/tools/shell/shell-text/src/openconnectiondialog.cpp
Expand Up @@ -17,6 +17,8 @@
*/

#include "openconnectiondialog.h"
#include "persistentdata.h"
#include <de/shell/LineEditWidget>

using namespace de;

Expand All @@ -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"));
}
Expand All @@ -36,3 +39,13 @@ String OpenConnectionDialog::address()
{
return text();
}

void OpenConnectionDialog::finish(int result)
{
InputDialog::finish(result);

if(result)
{
PersistentData::set("OpenConnection.address", text());
}
}
3 changes: 3 additions & 0 deletions doomsday/tools/shell/shell-text/src/openconnectiondialog.h
Expand Up @@ -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
52 changes: 52 additions & 0 deletions 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 <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>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</small>
*/

#include "persistentdata.h"
#include <QSettings>

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();
}
42 changes: 42 additions & 0 deletions 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 <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>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</small>
*/

#ifndef PERSISTENTDATA_H
#define PERSISTENTDATA_H

#include <de/String>

/**
* 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
46 changes: 41 additions & 5 deletions doomsday/tools/shell/shell-text/src/shellapp.cpp
Expand Up @@ -23,6 +23,7 @@
#include "openconnectiondialog.h"
#include "localserverdialog.h"
#include "aboutdialog.h"
#include "persistentdata.h"
#include <de/shell/LabelWidget>
#include <de/shell/MenuWidget>
#include <de/shell/Action>
Expand All @@ -38,6 +39,7 @@ using namespace shell;
struct ShellApp::Instance
{
ShellApp &self;
PersistentData persist;
MenuWidget *menu;
LogWidget *log;
CommandLineWidget *cli;
Expand Down Expand Up @@ -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"),
Expand All @@ -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()
Expand All @@ -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());
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion doomsday/tools/shell/shell-text/src/shellapp.h
Expand Up @@ -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();
Expand Down

0 comments on commit 45817b6

Please sign in to comment.