PageRenderTime 303ms CodeModel.GetById 141ms app.highlight 11ms RepoModel.GetById 149ms app.codeStats 1ms

/indra/newview/llurlhistory.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 139 lines | 87 code | 18 blank | 34 comment | 9 complexity | d77aee4daadd82fb55860dadfcea7289 MD5 | raw file
  1/**
  2 * @file llurlhistory.cpp
  3 * @brief Manages a list of recent URLs
  4 *
  5 * $LicenseInfo:firstyear=2007&license=viewerlgpl$
  6 * Second Life Viewer Source Code
  7 * Copyright (C) 2010, Linden Research, Inc.
  8 * 
  9 * This library is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU Lesser General Public
 11 * License as published by the Free Software Foundation;
 12 * version 2.1 of the License only.
 13 * 
 14 * This library is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * Lesser General Public License for more details.
 18 * 
 19 * You should have received a copy of the GNU Lesser General Public
 20 * License along with this library; if not, write to the Free Software
 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 22 * 
 23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 24 * $/LicenseInfo$
 25 */
 26
 27#include "llviewerprecompiledheaders.h"
 28
 29#include "llurlhistory.h"
 30
 31#include "lldir.h"
 32#include "llsdserialize.h"
 33
 34LLSD LLURLHistory::sHistorySD;
 35
 36const int MAX_URL_COUNT = 10;
 37
 38/////////////////////////////////////////////////////////////////////////////
 39
 40// static
 41bool LLURLHistory::loadFile(const std::string& filename)
 42{
 43	LLSD data;
 44	{
 45		std::string temp_str = gDirUtilp->getLindenUserDir() + gDirUtilp->getDirDelimiter();
 46
 47		llifstream file((temp_str + filename));
 48
 49		if (file.is_open())
 50		{
 51			llinfos << "Loading history.xml file at " << filename << llendl;
 52			LLSDSerialize::fromXML(data, file);
 53		}
 54
 55		if (data.isUndefined())
 56		{
 57			llinfos << "file missing, ill-formed, "
 58				"or simply undefined; not changing the"
 59				" file" << llendl;
 60			sHistorySD = LLSD();
 61			return false;
 62		}
 63	}
 64	sHistorySD = data;
 65	return true;
 66}
 67
 68// static
 69bool LLURLHistory::saveFile(const std::string& filename)
 70{	
 71	std::string temp_str = gDirUtilp->getLindenUserDir();
 72	if( temp_str.empty() )
 73	{
 74		llinfos << "Can't save URL history - no user directory set yet." << llendl;
 75		return false;
 76	}
 77
 78	temp_str += gDirUtilp->getDirDelimiter() + filename;
 79	llofstream out(temp_str);
 80	if (!out.good())
 81	{
 82		llwarns << "Unable to open " << filename << " for output." << llendl;
 83		return false;
 84	}
 85
 86	LLSDSerialize::toXML(sHistorySD, out);
 87
 88	out.close();
 89	return true;
 90}
 91// static
 92// This function returns a portion of the history llsd that contains the collected
 93// url history
 94LLSD LLURLHistory::getURLHistory(const std::string& collection)
 95{
 96	if(sHistorySD.has(collection))
 97	{
 98		return sHistorySD[collection];
 99	}
100	return LLSD();
101}
102
103// static
104void LLURLHistory::addURL(const std::string& collection, const std::string& url)
105{
106	if(! url.empty())
107	{
108		sHistorySD[collection].insert(0, url);
109		LLURLHistory::limitSize(collection);
110	}
111}
112// static
113void LLURLHistory::removeURL(const std::string& collection, const std::string& url)
114{
115	LLSD::array_iterator iter = sHistorySD[collection].beginArray();
116	LLSD::array_iterator end = sHistorySD[collection].endArray();
117	for(int index = 0; index < sHistorySD[collection].size(); index++)
118	{
119		if(sHistorySD[collection].get(index).asString() == url)
120		{
121			sHistorySD[collection].erase(index);
122		}
123	}
124}
125
126// static
127void LLURLHistory::clear(const std::string& collection)
128{
129	sHistorySD[ collection ] = LLSD();
130}
131
132void LLURLHistory::limitSize(const std::string& collection)
133{
134	while(sHistorySD[collection].size() > MAX_URL_COUNT)
135	{
136		sHistorySD[collection].erase(MAX_URL_COUNT);
137	}
138}
139