Mana
Loading...
Searching...
No Matches
recorder.cpp
Go to the documentation of this file.
1/*
2 * A chat recorder
3 * Copyright (C) 2008 Lloyd Bryant <lloyd_bryant@netzero.net>
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include "gui/recorder.h"
23
24#include "client.h"
25#include "event.h"
26
27#include "gui/chatwindow.h"
28
29#include "gui/widgets/button.h"
30#include "gui/widgets/layout.h"
31
32#include "utils/gettext.h"
33#include "utils/stringutils.h"
34
36 const std::string &title,
37 const std::string &buttonTxt)
38 : Window(title)
39 , mChat(chat)
40{
41 setWindowName("Recorder");
42 const int offsetX = 2 * getPadding() + 10;
43 const int offsetY = getTitleBarHeight() + getPadding() + 10;
44
45 auto *button = new Button(buttonTxt, "activate", this);
46
47 // 123 is the default chat window height. If you change this in Chat, please
48 // change it here as well
49 setDefaultSize(button->getWidth() + offsetX, button->getHeight() +
50 offsetY, WindowAlignment::BottomLeft, 0, 123);
51
52 place(0, 0, button);
53
54 Layout &layout = getLayout();
56
58}
59
60Recorder::~Recorder() = default;
61
62void Recorder::record(const std::string &msg)
63{
64 if (mStream.is_open())
65 mStream << msg << std::endl;
66}
67
68void Recorder::setRecordingFile(const std::string &msg)
69{
70 std::string msgCopy = msg;
71 trim(msgCopy);
72
73 if (msgCopy.empty())
74 {
75 if (mStream.is_open())
76 {
77 mStream.close();
78 setVisible(false);
79
80 /*
81 * Message should go after mStream is closed so that it isn't
82 * recorded.
83 */
84 serverNotice(_("Finishing recording."));
85 }
86 else
87 {
88 serverNotice(_("Not currently recording."));
89 }
90 }
91 else if (mStream.is_open())
92 {
93 serverNotice(_("Already recording."));
94 }
95 else
96 {
97 /*
98 * Message should go before mStream is opened so that it isn't
99 * recorded.
100 */
101 serverNotice(_("Starting to record..."));
102 const std::string file = Client::getLocalDataDirectory() + "/" + msgCopy;
103
104 mStream.open(file.c_str(), std::ios_base::trunc);
105
106 if (mStream.is_open())
107 setVisible(true);
108 else
109 serverNotice(_("Failed to start recording."));
110 }
111}
112
113void Recorder::action(const gcn::ActionEvent &event)
114{
115 setRecordingFile(std::string());
116}
Button widget.
Definition button.h:38
The chat window.
Definition chatwindow.h:73
static const std::string & getLocalDataDirectory()
Definition client.h:181
void setRowHeight(int n, int h)
Definition layout.h:221
This class is an helper for setting the position of widgets.
Definition layout.h:281
@ AUTO_SET
Uses the share as the new size.
Definition layout.h:304
void setRecordingFile(const std::string &msg)
Sets the file being recorded to.
Definition recorder.cpp:68
std::ofstream mStream
Definition recorder.h:72
Recorder(ChatWindow *chat, const std::string &title=_("Recording..."), const std::string &buttonTxt=_("Stop recording"))
Definition recorder.cpp:35
void action(const gcn::ActionEvent &event) override
called when the button is pressed
Definition recorder.cpp:113
~Recorder() override
void record(const std::string &msg)
Outputs the message to the recorder file.
Definition recorder.cpp:62
A window.
Definition window.h:59
virtual void setVisible(bool visible)
Overloads window setVisible by Guichan to allow sticky window handling.
Definition window.cpp:282
Layout & getLayout()
Gets the layout handler for this window.
Definition window.cpp:714
void setWindowName(const std::string &name)
Sets the name of the window.
Definition window.h:272
LayoutCell & place(int x, int y, gcn::Widget *, int w=1, int h=1)
Adds a widget to the window and sets it at given cell.
Definition window.cpp:737
void setDefaultSize()
Set the default win pos and size to the current ones.
Definition window.cpp:545
void loadWindowState()
Reads the position (and the size for resizable windows) in the configuration based on the given strin...
Definition window.cpp:467
void serverNotice(const std::string &message)
Definition event.h:319
#define _(s)
Definition gettext.h:38
std::string & trim(std::string &str)
Trims spaces off the end and the beginning of the given string.