summaryrefslogtreecommitdiff
path: root/indra/llui/llfolderviewmodel.h
blob: 1b61212c0eadf6e9de74777a52de31e8df456ab2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/** 
 * @file llfolderviewmodel.h
 *
 * $LicenseInfo:firstyear=2001&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2010, Linden Research, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */
#ifndef LLFOLDERVIEWMODEL_H
#define LLFOLDERVIEWMODEL_H

#include "llfontgl.h"	// just for StyleFlags enum
#include "llfolderview.h"

// These are grouping of inventory types.
// Order matters when sorting system folders to the top.
enum EInventorySortGroup
{
	SG_SYSTEM_FOLDER,
	SG_TRASH_FOLDER,
	SG_NORMAL_FOLDER,
	SG_ITEM
};

class LLFontGL;
class LLInventoryModel;
class LLMenuGL;
class LLUIImage;
class LLUUID;
class LLFolderViewItem;
class LLFolderViewFolder;

class LLFolderViewFilter
{
public:
	enum EFilterModified
	{
		FILTER_NONE,				// nothing to do, already filtered
		FILTER_RESTART,				// restart filtering from scratch
		FILTER_LESS_RESTRICTIVE,	// existing filtered items will certainly pass this filter
		FILTER_MORE_RESTRICTIVE		// if you didn't pass the previous filter, you definitely won't pass this one
	};

public:

	LLFolderViewFilter() {}
	virtual ~LLFolderViewFilter() {}

	// +-------------------------------------------------------------------+
	// + Execution And Results
	// +-------------------------------------------------------------------+
	virtual bool 				check(const LLFolderViewModelItem* item) = 0;
	virtual bool				checkFolder(const LLFolderViewModelItem* folder) const = 0;

	virtual void 				setEmptyLookupMessage(const std::string& message) = 0;
	virtual std::string			getEmptyLookupMessage() const = 0;

	virtual bool				showAllResults() const = 0;

	virtual std::string::size_type getStringMatchOffset(LLFolderViewModelItem* item) const = 0;
	virtual std::string::size_type getFilterStringSize() const = 0;
	// +-------------------------------------------------------------------+
	// + Status
	// +-------------------------------------------------------------------+
	virtual bool 				isActive() const = 0;
	virtual bool 				isModified() const = 0;
	virtual void 				clearModified() = 0;
	virtual const std::string& 	getName() const = 0;
	virtual const std::string& 	getFilterText() = 0;
	//RN: this is public to allow system to externally force a global refilter
	virtual void 				setModified(EFilterModified behavior = FILTER_RESTART) = 0;

	// +-------------------------------------------------------------------+
	// + Count
	// +-------------------------------------------------------------------+
	virtual void 				setFilterCount(S32 count) = 0;
	virtual S32 				getFilterCount() const = 0;
	virtual void 				decrementFilterCount() = 0;

	// +-------------------------------------------------------------------+
	// + Default
	// +-------------------------------------------------------------------+
	virtual bool 				isDefault() const = 0;
	virtual bool 				isNotDefault() const = 0;
	virtual void 				markDefault() = 0;
	virtual void 				resetDefault() = 0;

	// +-------------------------------------------------------------------+
	// + Generation
	// +-------------------------------------------------------------------+
	virtual S32 				getCurrentGeneration() const = 0;
	virtual S32 				getFirstSuccessGeneration() const = 0;
	virtual S32 				getFirstRequiredGeneration() const = 0;
};

class LLFolderViewModelInterface
{
public:
	virtual ~LLFolderViewModelInterface() {}
	virtual void requestSortAll() = 0;

	virtual void sort(class LLFolderViewFolder*) = 0;
	virtual void filter() = 0;

	virtual bool contentsReady() = 0;
	virtual void setFolderView(LLFolderView* folder_view) = 0;
	virtual LLFolderViewFilter& getFilter() = 0;
	virtual const LLFolderViewFilter& getFilter() const = 0;
	virtual std::string getStatusText() = 0;

	virtual bool startDrag(std::vector<LLFolderViewModelItem*>& items) = 0;
};

// This is an abstract base class that users of the folderview classes
// would use to bridge the folder view with the underlying data
class LLFolderViewModelItem : public LLRefCount
{
public:
	LLFolderViewModelItem() { }
	virtual ~LLFolderViewModelItem() { }

	virtual void update() {}	//called when drawing
	virtual const std::string& getName() const = 0;
	virtual const std::string& getDisplayName() const = 0;
	virtual const std::string& getSearchableName() const = 0;

	virtual LLPointer<LLUIImage> getIcon() const = 0;
	virtual LLPointer<LLUIImage> getIconOpen() const { return getIcon(); }
	virtual LLPointer<LLUIImage> getIconOverlay() const { return NULL; }

	virtual LLFontGL::StyleFlags getLabelStyle() const = 0;
	virtual std::string getLabelSuffix() const = 0;

	virtual void openItem( void ) = 0;
	virtual void closeItem( void ) = 0;
	virtual void selectItem(void) = 0;

	virtual BOOL isItemRenameable() const = 0;
	virtual BOOL renameItem(const std::string& new_name) = 0;

	virtual BOOL isItemMovable( void ) const = 0;		// Can be moved to another folder
	virtual void move( LLFolderViewModelItem* parent_listener ) = 0;

	virtual BOOL isItemRemovable( void ) const = 0;		// Can be destroyed
	virtual BOOL removeItem() = 0;
	virtual void removeBatch(std::vector<LLFolderViewModelItem*>& batch) = 0;

	virtual BOOL isItemCopyable() const = 0;
	virtual BOOL copyToClipboard() const = 0;
	virtual BOOL cutToClipboard() const = 0;

	virtual BOOL isClipboardPasteable() const = 0;
	virtual void pasteFromClipboard() = 0;
	virtual void pasteLinkFromClipboard() = 0;

	virtual void buildContextMenu(LLMenuGL& menu, U32 flags) = 0;
	
	virtual bool potentiallyVisible() = 0; // is the item definitely visible or we haven't made up our minds yet?

	virtual bool filter( LLFolderViewFilter& filter) = 0;
	virtual bool passedFilter(S32 filter_generation = -1) = 0;
	virtual bool descendantsPassedFilter(S32 filter_generation = -1) = 0;
	virtual void setPassedFilter(bool passed, S32 filter_generation, std::string::size_type string_offset = std::string::npos, std::string::size_type string_size = 0) = 0;
	virtual void setPassedFolderFilter(bool passed, S32 filter_generation) = 0;
	virtual void dirtyFilter() = 0;
	virtual bool hasFilterStringMatch() = 0;
	virtual std::string::size_type getFilterStringOffset() = 0;
	virtual std::string::size_type getFilterStringSize() = 0;

	virtual S32	getLastFilterGeneration() const = 0;

	virtual bool hasChildren() const = 0;
	virtual void addChild(LLFolderViewModelItem* child) = 0;
	virtual void removeChild(LLFolderViewModelItem* child) = 0;

	// This method will be called to determine if a drop can be
	// performed, and will set drop to TRUE if a drop is
	// requested. Returns TRUE if a drop is possible/happened,
	// otherwise FALSE.
	virtual BOOL dragOrDrop(MASK mask, BOOL drop,
							EDragAndDropType cargo_type,
							void* cargo_data,
							std::string& tooltip_msg) = 0;

	virtual void requestSort() = 0;
	virtual S32 getSortVersion() = 0;
	virtual void setSortVersion(S32 version) = 0;
	virtual void setParent(LLFolderViewModelItem* parent) = 0;
	virtual bool hasParent() = 0;

protected:

	friend class LLFolderViewItem;
	virtual void setFolderViewItem(LLFolderViewItem* folder_view_item) = 0;

};


class LLFolderViewModelItemCommon : public LLFolderViewModelItem
{
public:
	LLFolderViewModelItemCommon(LLFolderViewModelInterface& root_view_model)
	:	mSortVersion(-1),
		mPassedFilter(true),
		mPassedFolderFilter(true),
		mStringMatchOffsetFilter(std::string::npos),
		mStringFilterSize(0),
		mFolderViewItem(NULL),
		mLastFilterGeneration(-1),
		mLastFolderFilterGeneration(-1),
		mMostFilteredDescendantGeneration(-1),
		mParent(NULL),
		mRootViewModel(root_view_model)
	{
		mChildren.clear();
	}

	void requestSort() { mSortVersion = -1; }
	S32 getSortVersion() { return mSortVersion; }
	void setSortVersion(S32 version) { mSortVersion = version;}

	S32	getLastFilterGeneration() const { return mLastFilterGeneration; }
	S32	getLastFolderFilterGeneration() const { return mLastFolderFilterGeneration; }
	void dirtyFilter()
	{
		mLastFilterGeneration = -1;
		mLastFolderFilterGeneration = -1;

		// bubble up dirty flag all the way to root
		if (mParent)
		{
			mParent->dirtyFilter();
		}	
	}
	bool hasFilterStringMatch();
	std::string::size_type getFilterStringOffset();
	std::string::size_type getFilterStringSize();
	
	typedef std::list<LLFolderViewModelItem*> child_list_t;

	virtual void addChild(LLFolderViewModelItem* child) 
	{ 
		// Avoid duplicates: bail out if that child is already present in the list
		// Note: this happens when models are created before views
		child_list_t::const_iterator iter;
		for (iter = mChildren.begin(); iter != mChildren.end(); iter++)
		{
			if (child == *iter)
			{
				return;
			}
		}
		mChildren.push_back(child); 
		child->setParent(this); 
		dirtyFilter();
		requestSort();
	}
	virtual void removeChild(LLFolderViewModelItem* child) 
	{ 
		mChildren.remove(child); 
		child->setParent(NULL); 
		dirtyFilter();
	}
	
	virtual void clearChildren()
	{
		// As this is cleaning the whole list of children wholesale, we do need to delete the pointed objects
		// This is different and not equivalent to calling removeChild() on each child
		std::for_each(mChildren.begin(), mChildren.end(), DeletePointer());
		mChildren.clear();
		dirtyFilter();
	}
	
	child_list_t::const_iterator getChildrenBegin() const { return mChildren.begin(); }
	child_list_t::const_iterator getChildrenEnd() const { return mChildren.end(); }
	child_list_t::size_type getChildrenCount() const { return mChildren.size(); }
	
	void setPassedFilter(bool passed, S32 filter_generation, std::string::size_type string_offset = std::string::npos, std::string::size_type string_size = 0)
	{
		mPassedFilter = passed;
		mLastFilterGeneration = filter_generation;
		mStringMatchOffsetFilter = string_offset;
		mStringFilterSize = string_size;
	}

	void setPassedFolderFilter(bool passed, S32 filter_generation)
	{
		mPassedFolderFilter = passed;
		mLastFolderFilterGeneration = filter_generation;
	}

	virtual bool potentiallyVisible()
	{
		return passedFilter() // we've passed the filter
			|| getLastFilterGeneration() < mRootViewModel.getFilter().getFirstSuccessGeneration() // or we don't know yet
			|| descendantsPassedFilter();
	}

	virtual bool passedFilter(S32 filter_generation = -1) 
	{ 
		if (filter_generation < 0) 
			filter_generation = mRootViewModel.getFilter().getFirstSuccessGeneration();

		bool passed_folder_filter = mPassedFolderFilter && mLastFolderFilterGeneration >= filter_generation;
		bool passed_filter = mPassedFilter && mLastFilterGeneration >= filter_generation;
		return passed_folder_filter
				&& (descendantsPassedFilter(filter_generation)
					|| passed_filter);
	}

	virtual bool descendantsPassedFilter(S32 filter_generation = -1)
	{ 
		if (filter_generation < 0) filter_generation = mRootViewModel.getFilter().getFirstSuccessGeneration();
		return mMostFilteredDescendantGeneration >= filter_generation; 
	}


protected:
	virtual void setParent(LLFolderViewModelItem* parent) { mParent = parent; }
	virtual bool hasParent() { return mParent != NULL; }

	S32						mSortVersion;
	bool					mPassedFilter;
	bool					mPassedFolderFilter;
	std::string::size_type	mStringMatchOffsetFilter;
	std::string::size_type	mStringFilterSize;

	S32						mLastFilterGeneration;
	S32						mLastFolderFilterGeneration;
	S32						mMostFilteredDescendantGeneration;

	child_list_t			mChildren;
	LLFolderViewModelItem*	mParent;
	LLFolderViewModelInterface& mRootViewModel;

	void setFolderViewItem(LLFolderViewItem* folder_view_item) { mFolderViewItem = folder_view_item;}
	LLFolderViewItem*		mFolderViewItem;
};



class LLFolderViewModelCommon : public LLFolderViewModelInterface
{
public:
	LLFolderViewModelCommon()
	:	mTargetSortVersion(0),
		mFolderView(NULL)
	{}

	virtual void requestSortAll()
	{
		// sort everything
		mTargetSortVersion++;
	}
	virtual std::string getStatusText();
	virtual void filter();

	void setFolderView(LLFolderView* folder_view) { mFolderView = folder_view;}

protected:
	bool needsSort(class LLFolderViewModelItem* item);

	S32 mTargetSortVersion;
	LLFolderView* mFolderView;

};

template <typename SORT_TYPE, typename ITEM_TYPE, typename FOLDER_TYPE, typename FILTER_TYPE>
class LLFolderViewModel : public LLFolderViewModelCommon
{
public:
	LLFolderViewModel(){}
	virtual ~LLFolderViewModel() {}

	typedef SORT_TYPE		SortType;
	typedef ITEM_TYPE		ItemType;
	typedef FOLDER_TYPE		FolderType;
	typedef FILTER_TYPE		FilterType;

	virtual SortType& getSorter()					 { return mSorter; }
	virtual const SortType& getSorter() const 		 { return mSorter; }
	virtual void setSorter(const SortType& sorter) 	 { mSorter = sorter; requestSortAll(); }

	virtual FilterType& getFilter() 				 { return mFilter; }
	virtual const FilterType& getFilter() const		 { return mFilter; }
	virtual void setFilter(const FilterType& filter) { mFilter = filter; }

	// By default, we assume the content is available. If a network fetch mechanism is implemented for the model,
	// this method needs to be overloaded and return the relevant fetch status.
	virtual bool contentsReady()					{ return true; }


	struct ViewModelCompare
	{
		ViewModelCompare(const SortType& sorter)
		:	mSorter(sorter)
		{}

		bool operator () (const LLFolderViewItem* a, const LLFolderViewItem* b) const
		{
			return mSorter(static_cast<const ItemType*>(a->getViewModelItem()), static_cast<const ItemType*>(b->getViewModelItem()));
		}

		bool operator () (const LLFolderViewFolder* a, const LLFolderViewFolder* b) const
		{
			return mSorter(static_cast<const ItemType*>(a->getViewModelItem()), static_cast<const ItemType*>(b->getViewModelItem()));
		}

		const SortType& mSorter;
	};

	void sort(LLFolderViewFolder* folder)
	{
		if (needsSort(folder->getViewModelItem()))
		{
			folder->sortFolders(ViewModelCompare(getSorter()));
			folder->sortItems(ViewModelCompare(getSorter()));
			folder->getViewModelItem()->setSortVersion(mTargetSortVersion);
			folder->requestArrange();
		}
	}

protected:
	SortType		mSorter;
	FilterType		mFilter;
};

#endif // LLFOLDERVIEWMODEL_H