~holger-seelig/titania/4.0

« back to all changes in this revision

Viewing changes to Titania/Titania/Browser/BrowserHistory.cpp

  • Committer: Holger Seelig
  • Date: 2018-01-29 16:56:56 UTC
  • Revision ID: holger.seelig@yahoo.de-20180129165656-4rraq0q1qeioe8jm
Fixed Environment>Browser bug.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: C++; coding: utf-8; tab-width: 3; indent-tabs-mode: tab; c-basic-offset: 3 -*-
2
 
 *******************************************************************************
3
 
 *
4
 
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
 
 *
6
 
 * Copyright create3000, Scheffelstraße 31a, Leipzig, Germany 2011.
7
 
 *
8
 
 * All rights reserved. Holger Seelig <holger.seelig@yahoo.de>.
9
 
 *
10
 
 * THIS IS UNPUBLISHED SOURCE CODE OF create3000.
11
 
 *
12
 
 * The copyright notice above does not evidence any actual of intended
13
 
 * publication of such source code, and is an unpublished work by create3000.
14
 
 * This material contains CONFIDENTIAL INFORMATION that is the property of
15
 
 * create3000.
16
 
 *
17
 
 * No permission is granted to copy, distribute, or create derivative works from
18
 
 * the contents of this software, in whole or in part, without the prior written
19
 
 * permission of create3000.
20
 
 *
21
 
 * NON-MILITARY USE ONLY
22
 
 *
23
 
 * All create3000 software are effectively free software with a non-military use
24
 
 * restriction. It is free. Well commented source is provided. You may reuse the
25
 
 * source in any way you please with the exception anything that uses it must be
26
 
 * marked to indicate is contains 'non-military use only' components.
27
 
 *
28
 
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
29
 
 *
30
 
 * Copyright 1999, 2016 Holger Seelig <holger.seelig@yahoo.de>.
31
 
 *
32
 
 * This file is part of the Titania Project.
33
 
 *
34
 
 * Titania is free software: you can redistribute it and/or modify it under the
35
 
 * terms of the GNU General Public License version 3 only, as published by the
36
 
 * Free Software Foundation.
37
 
 *
38
 
 * Titania is distributed in the hope that it will be useful, but WITHOUT ANY
39
 
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
40
 
 * A PARTICULAR PURPOSE. See the GNU General Public License version 3 for more
41
 
 * details (a copy is included in the LICENSE file that accompanied this code).
42
 
 *
43
 
 * You should have received a copy of the GNU General Public License version 3
44
 
 * along with Titania.  If not, see <http://www.gnu.org/licenses/gpl.html> for a
45
 
 * copy of the GPLv3 License.
46
 
 *
47
 
 * For Silvio, Joy and Adi.
48
 
 *
49
 
 ******************************************************************************/
50
 
 
51
 
#include "BrowserHistory.h"
52
 
 
53
 
#include <Titania/X3D/Execution/X3DScene.h>
54
 
#include <sstream>
55
 
 
56
 
namespace titania {
57
 
namespace puck {
58
 
 
59
 
const io::character      BrowserHistory::Grammar::tab ('\t');
60
 
const io::inverse_string BrowserHistory::Grammar::string ("\t");
61
 
 
62
 
BrowserHistory::BrowserHistory (X3D::X3DBrowser* const browser) :
63
 
         X3D::X3DOutput (),
64
 
        sigc::trackable (),
65
 
                browser (browser),
66
 
                   list (),
67
 
                  index (-1)
68
 
{
69
 
        browser -> initialized () .addInterest (&BrowserHistory::set_splashScreen, this);
70
 
}
71
 
 
72
 
void
73
 
BrowserHistory::set_splashScreen ()
74
 
{
75
 
        browser -> initialized () .removeInterest (&BrowserHistory::set_splashScreen, this);
76
 
        browser -> initialized () .addInterest (&BrowserHistory::set_initialized, this);
77
 
}
78
 
 
79
 
void
80
 
BrowserHistory::set_initialized ()
81
 
{
82
 
        try
83
 
        {
84
 
                const X3D::X3DScenePtr scene (browser -> getExecutionContext ());
85
 
 
86
 
                if (scene and scene -> getMetaData ("titania-history") == "FALSE")
87
 
                        return;
88
 
        }
89
 
        catch (const X3D::X3DError &)
90
 
        { }
91
 
 
92
 
        addURL (browser -> getExecutionContext () -> getTitle (), browser -> getWorldURL ());
93
 
 
94
 
        processInterests ();
95
 
}
96
 
 
97
 
void
98
 
BrowserHistory::connect (const X3D::SFBool & initialized)
99
 
{
100
 
        initialized .removeInterest (&BrowserHistory::connect, this);
101
 
        initialized .addInterest (&BrowserHistory::set_initialized, this);
102
 
 
103
 
        processInterests ();
104
 
}
105
 
 
106
 
void
107
 
BrowserHistory::assign (const int index, std::vector <Page> && list)
108
 
{
109
 
        this -> index = index;
110
 
        this -> list  = std::move (list);
111
 
}
112
 
 
113
 
void
114
 
BrowserHistory::addURL (const std::string & title, const basic::uri & URL)
115
 
{
116
 
        if (index >= 0 and list [index] .second == URL)
117
 
                return;
118
 
 
119
 
        list .resize (index + 1);
120
 
 
121
 
        list .emplace_back (title, URL);
122
 
 
123
 
        ++ index;
124
 
}
125
 
 
126
 
void
127
 
BrowserHistory::setIndex (const int value)
128
 
{
129
 
        if (value < 0 or value >= (int) list .size ())
130
 
                return;
131
 
 
132
 
        index = value;
133
 
 
134
 
        browser -> initialized () .removeInterest (&BrowserHistory::set_initialized, this);
135
 
        browser -> initialized () .addInterest (&BrowserHistory::connect, this);
136
 
 
137
 
        browser -> loadURL ({ list [index] .second .str () }, { });
138
 
}
139
 
 
140
 
bool
141
 
BrowserHistory::hasPreviousPage () const
142
 
{
143
 
        return index > 0;
144
 
}
145
 
 
146
 
bool
147
 
BrowserHistory::hasNextPage () const
148
 
{
149
 
        return index + 1 < (int) list .size ();
150
 
}
151
 
 
152
 
void
153
 
BrowserHistory::previousPage ()
154
 
{
155
 
        setIndex (index - 1);
156
 
}
157
 
 
158
 
void
159
 
BrowserHistory::nextPage ()
160
 
{
161
 
        setIndex (index + 1);
162
 
}
163
 
 
164
 
void
165
 
BrowserHistory::fromString (const std::string & string)
166
 
{
167
 
        std::istringstream isstream (string);
168
 
 
169
 
        isstream .imbue (std::locale::classic ());
170
 
 
171
 
        fromStream (isstream);
172
 
}
173
 
 
174
 
void
175
 
BrowserHistory::fromStream (std::istream & istream)
176
 
{
177
 
        int index = -1;
178
 
 
179
 
        std::vector <Page> list;
180
 
 
181
 
        if (not (istream >> index))
182
 
                return;
183
 
 
184
 
        if (index < -1)
185
 
                return;
186
 
 
187
 
        if (index == -1)
188
 
        {
189
 
                assign (index, std::move (list));
190
 
                return;
191
 
        }
192
 
 
193
 
        if (not Grammar::tab (istream))
194
 
                return;
195
 
 
196
 
        while (istream)
197
 
        {
198
 
                std::pair <std::string, std::string> page;
199
 
 
200
 
                if (not Grammar::string (istream, page .first))
201
 
                        return;
202
 
 
203
 
                Grammar::string (istream, page .second);
204
 
 
205
 
                list .emplace_back (std::move (page));
206
 
        }
207
 
 
208
 
        if (index < (int) list .size ())
209
 
                assign (index, std::move (list));
210
 
}
211
 
 
212
 
std::string
213
 
BrowserHistory::toString () const
214
 
{
215
 
        std::ostringstream osstream;
216
 
 
217
 
        osstream .imbue (std::locale::classic ());
218
 
 
219
 
        toStream (osstream);
220
 
 
221
 
        return osstream .str ();
222
 
}
223
 
 
224
 
void
225
 
BrowserHistory::toStream (std::ostream & ostream) const
226
 
{
227
 
        ostream << index;
228
 
 
229
 
        for (const auto & pair : list)
230
 
                ostream << Grammar::tab () << pair .first << Grammar::tab () << pair .second;
231
 
}
232
 
 
233
 
} // puck
234
 
} // titania