PageRenderTime 38ms CodeModel.GetById 22ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/newview/llsearchhistory.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 148 lines | 93 code | 26 blank | 29 comment | 12 complexity | a89e71a48fff9fd5aecbc929239571e5 MD5 | raw file
  1/**
  2 * @file llsearchhistory.cpp
  3 * @brief Search history container implementation
  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#include "llsearchhistory.h"
 29
 30#include "llfile.h"
 31#include "llsdserialize.h"
 32#include "llxmlnode.h"
 33
 34std::string LLSearchHistory::SEARCH_QUERY = "search_query";
 35std::string LLSearchHistory::SEARCH_HISTORY_FILE_NAME = "search_history.txt";
 36
 37LLSearchHistory::LLSearchHistory()
 38{
 39
 40}
 41
 42bool LLSearchHistory::load()
 43{
 44	// build filename for each user
 45	std::string resolved_filename = getHistoryFilePath();
 46	llifstream file(resolved_filename);
 47	if (!file.is_open())
 48	{
 49		return false;
 50	}
 51
 52	clearHistory();
 53
 54	// add each line in the file to the list
 55	std::string line;
 56	LLPointer<LLSDParser> parser = new LLSDNotationParser();
 57	while (std::getline(file, line)) 
 58	{
 59		LLSD s_item;
 60		std::istringstream iss(line);
 61		if (parser->parse(iss, s_item, line.length()) == LLSDParser::PARSE_FAILURE)
 62		{
 63			break;
 64		}
 65
 66		mSearchHistory.push_back(s_item);
 67	}
 68
 69	file.close();
 70
 71	return true;
 72}
 73
 74bool LLSearchHistory::save()
 75{
 76	// build filename for each user
 77	std::string resolved_filename = getHistoryFilePath();
 78	// open a file for writing
 79	llofstream file (resolved_filename);
 80	if (!file.is_open())
 81	{
 82		return false;
 83	}
 84
 85	search_history_list_t::const_iterator it = mSearchHistory.begin();
 86	for (; mSearchHistory.end() != it; ++it)
 87	{
 88		file << LLSDOStreamer<LLSDNotationFormatter>((*it).toLLSD()) << std::endl;
 89	}
 90
 91	file.close();
 92	return true;
 93}
 94
 95std::string LLSearchHistory::getHistoryFilePath()
 96{
 97	return gDirUtilp->getExpandedFilename(LL_PATH_PER_SL_ACCOUNT, SEARCH_HISTORY_FILE_NAME);
 98}
 99
100void LLSearchHistory::addEntry(const std::string& search_query)
101{
102	if(search_query.empty())
103	{
104		return;
105	}
106
107	search_history_list_t::iterator it = 
108		find(mSearchHistory.begin(), mSearchHistory.end(), search_query);
109
110	if(mSearchHistory.end() != it)
111	{
112		mSearchHistory.erase(it);
113	}
114
115	LLSearchHistoryItem item(search_query);
116	mSearchHistory.push_front(item);
117}
118
119bool LLSearchHistory::LLSearchHistoryItem::operator < (const LLSearchHistory::LLSearchHistoryItem& right)
120{
121	S32 result = LLStringUtil::compareInsensitive(search_query, right.search_query);
122
123	return result < 0;
124}
125
126bool LLSearchHistory::LLSearchHistoryItem::operator > (const LLSearchHistory::LLSearchHistoryItem& right)
127{
128	S32 result = LLStringUtil::compareInsensitive(search_query, right.search_query);
129
130	return result > 0;
131}
132
133bool LLSearchHistory::LLSearchHistoryItem::operator==(const LLSearchHistory::LLSearchHistoryItem& right)
134{
135	return 0 == LLStringUtil::compareInsensitive(search_query, right.search_query);
136}
137
138bool LLSearchHistory::LLSearchHistoryItem::operator==(const std::string& right)
139{
140	return 0 == LLStringUtil::compareInsensitive(search_query, right);
141}
142
143LLSD LLSearchHistory::LLSearchHistoryItem::toLLSD() const
144{
145	LLSD ret;
146	ret[SEARCH_QUERY] = search_query;
147	return ret;
148}