PageRenderTime 74ms CodeModel.GetById 40ms app.highlight 17ms RepoModel.GetById 13ms app.codeStats 0ms

/indra/newview/lllocationhistory.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 179 lines | 109 code | 32 blank | 38 comment | 19 complexity | 0e92ed187fda5d1286150e6c7884c399 MD5 | raw file
  1/** 
  2 * @file lllocationhistory.cpp
  3 * @brief Typed locations history
  4 *
  5 * $LicenseInfo:firstyear=2009&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 "lllocationhistory.h"
 30
 31#include <iomanip> // for std::setw()
 32
 33#include "llui.h"
 34#include "llsd.h"
 35#include "llsdserialize.h"
 36
 37LLLocationHistory::LLLocationHistory() :
 38	mFilename("typed_locations.txt")
 39{
 40}
 41
 42void LLLocationHistory::addItem(const LLLocationHistoryItem& item) {
 43	static LLUICachedControl<S32> max_items("LocationHistoryMaxSize", 100);
 44
 45	// check if this item doesn't duplicate any existing one
 46	location_list_t::iterator item_iter = std::find(mItems.begin(), mItems.end(),item);
 47	if(item_iter != mItems.end()) // if it already exists, erase the old one
 48	{
 49		mItems.erase(item_iter);	
 50	}
 51
 52	mItems.push_back(item);
 53	
 54	// If the vector size exceeds the maximum, purge the oldest items (at the start of the mItems vector).
 55	if ((S32)mItems.size() > max_items)
 56	{
 57		mItems.erase(mItems.begin(), mItems.end()-max_items);
 58	}
 59	llassert((S32)mItems.size() <= max_items);
 60	mChangedSignal(ADD);
 61}
 62
 63/*
 64 * @brief Try to find item in history. 
 65 * If item has been founded, it will be places into end of history.
 66 * @return true - item has founded
 67 */
 68bool LLLocationHistory::touchItem(const LLLocationHistoryItem& item) {
 69	bool result = false;
 70	location_list_t::iterator item_iter = std::find(mItems.begin(), mItems.end(), item);
 71
 72	// the last used item should be the first in the history
 73	if (item_iter != mItems.end()) {
 74		mItems.erase(item_iter);
 75		mItems.push_back(item);
 76		result = true;
 77	}
 78
 79	return result;
 80}
 81
 82void LLLocationHistory::removeItems()
 83{
 84	mItems.clear();
 85	mChangedSignal(CLEAR);
 86}
 87
 88bool LLLocationHistory::getMatchingItems(const std::string& substring, location_list_t& result) const
 89{
 90	// *TODO: an STL algorithm would look nicer
 91	result.clear();
 92
 93	std::string needle = substring;
 94	LLStringUtil::toLower(needle);
 95
 96	for (location_list_t::const_iterator it = mItems.begin(); it != mItems.end(); ++it)
 97	{
 98		std::string haystack = it->getLocation();
 99		LLStringUtil::toLower(haystack);
100
101		if (haystack.find(needle) != std::string::npos)
102			result.push_back(*it);
103	}
104	
105	return result.size();
106}
107
108void LLLocationHistory::dump() const
109{
110	llinfos << "Location history dump:" << llendl;
111	int i = 0;
112	for (location_list_t::const_iterator it = mItems.begin(); it != mItems.end(); ++it, ++i)
113	{
114	    llinfos << "#" << std::setw(2) << std::setfill('0') << i << ": " << it->getLocation() << llendl;
115	}
116}
117
118void LLLocationHistory::save() const
119{
120	// build filename for each user
121	std::string resolved_filename = gDirUtilp->getExpandedFilename(LL_PATH_PER_SL_ACCOUNT, mFilename);
122
123	if (resolved_filename.empty())
124	{
125		llinfos << "can't get path to location history filename - probably not logged in yet." << llendl;
126		return;
127	}
128
129	// open a file for writing
130	llofstream file (resolved_filename);
131	if (!file.is_open())
132	{
133		llwarns << "can't open location history file \"" << mFilename << "\" for writing" << llendl;
134		return;
135	}
136
137	for (location_list_t::const_iterator it = mItems.begin(); it != mItems.end(); ++it)
138	{
139		file << LLSDOStreamer<LLSDNotationFormatter>((*it).toLLSD()) << std::endl;
140	}
141
142	file.close();
143}
144
145void LLLocationHistory::load()
146{
147	llinfos << "Loading location history." << llendl;
148	
149	// build filename for each user
150	std::string resolved_filename = gDirUtilp->getExpandedFilename(LL_PATH_PER_SL_ACCOUNT, mFilename);
151	llifstream file(resolved_filename);
152
153	if (!file.is_open())
154	{
155		llwarns << "can't load location history from file \"" << mFilename << "\"" << llendl;
156		return;
157	}
158	
159	mItems.clear();// need to use a direct call of clear() method to avoid signal invocation
160	
161	// add each line in the file to the list
162	std::string line;
163	LLPointer<LLSDParser> parser = new LLSDNotationParser();
164	while (std::getline(file, line)) {
165		LLSD s_item;
166		std::istringstream iss(line);
167		if (parser->parse(iss, s_item, line.length()) == LLSDParser::PARSE_FAILURE)
168		{
169			llinfos<< "Parsing saved teleport history failed" << llendl;
170			break;
171		}
172
173		mItems.push_back(s_item);
174	}
175
176	file.close();
177	
178	mChangedSignal(LOAD);
179}