sbLocalDatabaseTreeView.h
Go to the documentation of this file.
1 /*
2 //
3 // BEGIN SONGBIRD GPL
4 //
5 // This file is part of the Songbird web player.
6 //
7 // Copyright(c) 2005-2011 POTI, Inc.
8 // http://songbirdnest.com
9 //
10 // This file may be licensed under the terms of of the
11 // GNU General Public License Version 2 (the "GPL").
12 //
13 // Software distributed under the License is distributed
14 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
15 // express or implied. See the GPL for the specific language
16 // governing rights and limitations.
17 //
18 // You should have received a copy of the GPL along with this
19 // program. If not, go to http://www.gnu.org/licenses/gpl.html
20 // or write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 // END SONGBIRD GPL
24 //
25 */
26 
27 #ifndef __SBLOCALDATABASETREEVIEW_H__
28 #define __SBLOCALDATABASETREEVIEW_H__
29 
30 #include "sbSelectionListUtils.h"
31 
32 #include <nsIClassInfo.h>
33 #include <nsIObserver.h>
34 #include <nsITreeView.h>
35 #include <nsITreeSelection.h>
36 #include <nsIWeakReference.h>
37 #include <nsIWeakReferenceUtils.h>
38 
39 #include <sbILocalDatabaseGUIDArray.h>
40 #include <sbILocalDatabaseTreeView.h>
41 #include <sbIMediacoreEventListener.h>
42 #include <sbIMediaListViewTreeView.h>
43 #include <sbIMediaListViewSelection.h>
44 #include <sbIPlayQueueService.h>
45 
46 #include <nsCOMPtr.h>
47 #include <nsDataHashtable.h>
48 #include <nsInterfaceHashtable.h>
49 #include <nsISerializable.h>
50 #include <nsClassHashtable.h>
51 #include <nsStringGlue.h>
52 #include <nsTArray.h>
53 #include <nsTObserverArray.h>
54 
55 #include <sbWeakReference.h>
56 
57 class nsIObjectInputStream;
58 class nsIObjectOutputStream;
59 class nsISupportsArray;
60 class nsITreeBoxObject;
61 class nsITreeColumn;
62 class nsITreeSelection;
65 class sbILibrary;
66 class sbILibrarySort;
67 class sbIMediacoreEvent;
68 class sbIMediaList;
69 class sbIMediaListView;
70 class sbIPropertyArray;
71 class sbIPropertyInfo;
72 class sbIPropertyManager;
78 
80  public nsIClassInfo,
81  public nsIObserver,
88 
89 {
90  friend class sbFilterTreeSelection;
92 
93  typedef nsresult (*PR_CALLBACK sbSelectionEnumeratorCallbackFunc)
94  (PRUint32 aIndex, const nsAString& aId, const nsAString& aGuid, void* aUserData);
95 
96 public:
98  NS_DECL_NSICLASSINFO
99  NS_DECL_NSIOBSERVER
100  NS_DECL_NSITREEVIEW
101  NS_DECL_SBILOCALDATABASEGUIDARRAYLISTENER
102  NS_DECL_SBIMEDIALISTVIEWTREEVIEW
103  NS_DECL_SBILOCALDATABASETREEVIEW
104  NS_DECL_SBIMEDIACOREEVENTLISTENER
105  NS_DECL_SBIMEDIALISTVIEWSELECTIONLISTENER
106  NS_DECL_SBIPLAYQUEUESERVICELISTENER
107 
109 
111 
112  nsresult Init(sbLocalDatabaseMediaListView* aListView,
114  sbIPropertyArray* aCurrentSort,
116 
117  nsresult Rebuild();
118 
119  // Getter and setter methods to toggle rebuild prevention.
120  void SetShouldPreventRebuild(PRBool aShouldPreventRebuild);
121  PRBool GetShouldPreventRebuild();
122 
123  void ClearMediaListView();
124 
126 
127 protected:
128  nsresult TokenizeProperties(const nsAString& aProperties,
129  nsISupportsArray* aAtomArray);
130 
131  // Event Listener Handlers
132  nsresult OnStop();
133 
134  nsresult OnTrackChange();
135  nsresult OnTrackChange(sbIMediaListView *aView,
136  PRUint32 aIndex);
137 
138  nsresult OnTrackIndexChange(sbIMediacoreEvent *aEvent);
139 
140 private:
141 
142  enum PageCacheStatus {
143  eNotCached,
144  ePending,
145  eCached
146  };
147 
148  enum MediaListType {
149  eLibrary,
150  eSimple,
151  eDistinct
152  };
153 
154  nsresult GetPropertyForTreeColumn(nsITreeColumn* aTreeColumn,
155  nsAString& aProperty);
156 
157  nsresult GetTreeColumnForProperty(const nsAString& aProperty,
158  nsITreeColumn** aTreeColumn);
159 
160  nsresult GetCellPropertyValue(PRInt32 aIndex,
161  nsITreeColumn *aTreeColumn,
162  nsAString& _retval);
163 
164  nsresult SaveSelectionList();
165 
166  nsresult RestoreSelection();
167 
168  nsresult EnumerateSelection(sbSelectionEnumeratorCallbackFunc aFunc,
169  void* aUserData);
170 
171  nsresult GetUniqueIdForIndex(PRUint32 aIndex, nsAString& aId);
172 
173  void SetSelectionIsAll(PRBool aSelectionIsAll);
174 
175  void ClearSelectionList();
176 
177  nsresult UpdateColumnSortAttributes(const nsAString& aProperty,
178  PRBool aDirection);
179 
180  static nsresult PR_CALLBACK
181  SelectionListSavingEnumeratorCallback(PRUint32 aIndex,
182  const nsAString& aId,
183  const nsAString& aGuid,
184  void* aUserData);
185 
186  static nsresult PR_CALLBACK
187  SelectionListGuidsEnumeratorCallback(PRUint32 aIndex,
188  const nsAString& aId,
189  const nsAString& aGuid,
190  void* aUserData);
191 
192  inline PRUint32 TreeToArray(PRInt32 aRow) {
193  return (PRUint32) (mFakeAllRow ? aRow - 1 : aRow);
194  }
195 
196  inline PRUint32 ArrayToTree(PRUint32 aIndex) {
197  return (PRInt32) (mFakeAllRow ? aIndex + 1 : aIndex);
198  }
199 
200  inline PRBool IsAllRow(PRInt32 aRow) {
201  return mFakeAllRow && aRow == 0;
202  }
203 
204  inline nsresult GetColumnPropertyInfo(nsITreeColumn* aColumn,
205  sbIPropertyInfo** aPropertyInfo);
206 
207  nsresult GetPropertyInfoAndValue(PRInt32 aRow,
208  nsITreeColumn* aColumn,
209  nsAString& aValue,
210  sbIPropertyInfo** aPropertyInfo);
211 
212  nsresult GetPlayingProperty(PRUint32 aIndex,
213  nsISupportsArray* properties);
214 
215  nsresult GetOriginNotInMainLibraryProperty(PRUint32 aIndex,
216  nsISupportsArray* properties);
217 
218  nsresult GetItemDisabledStatus(PRUint32 aIndex,
219  nsISupportsArray* properties);
220 
221  nsresult GetPlayQueueStatus(PRUint32 aIndex,
222  nsISupportsArray* properties);
223 
224  nsresult GetIsListReadOnly(PRBool *aOutIsReadOnly);
225 
226  nsresult GetBag(PRUint32 aIndex,
228 
229  nsresult GetBag(const nsAString& aGuid,
231 
232  // Cached property manager
233  nsCOMPtr<sbIPropertyManager> mPropMan;
234 
235  // Indicates if we are currently viewing content that is on a device
236  PRBool mViewingDeviceContent;
237 
238  // Type of media list this tree view is of
239  MediaListType mListType;
240 
241  // The media list view that this tree view is a view of. This pointer is
242  // set to null when the view is destroyed.
243  sbLocalDatabaseMediaListView* mMediaListView;
244  sbIMediaListViewSelection* mViewSelection;
245 
246  // The async guid array given to us by our view
247  nsCOMPtr<sbILocalDatabaseGUIDArray> mArray;
248 
249  // The cached length of the guid array
250  PRUint32 mArrayLength;
251 
252  // The fetch size of the guid array. This is used to compute the size of
253  // our pages when tracking their cached status
254  PRUint32 mFetchSize;
255 
256  // The property cache that is linked with the guid array
257  nsCOMPtr<sbILocalDatabasePropertyCache> mPropertyCache;
258 
259  // Current sort property
260  nsString mCurrentSortProperty;
261 
262  // Stuff the tree view needs to track
263  nsCOMPtr<nsITreeSelection> mSelection;
264  nsCOMPtr<nsITreeSelection> mRealSelection;
265  nsCOMPtr<nsITreeBoxObject> mTreeBoxObject;
266 
267  // Weak listener
268  nsCOMPtr<nsIWeakReference> mObserver;
269 
270  // Do we manage our selection? Filters do, playlists don't
271  PRBool mManageSelection;
272 
273  // Saved list of selected rows and associated guids used to restore selection
274  // between rebuilds
275  PRBool mHaveSavedSelection;
276  sbSelectionList mSelectionList;
277 
278  // Mouse state
279  PRUint32 mMouseState;
280  PRInt32 mMouseStateRow;
281  nsCOMPtr<nsITreeColumn> mMouseStateColumn;
282 
283  // Currently playing track UID and index
284  nsString mPlayingItemUID;
285  nsCOMPtr<nsIWeakReference> mMediacoreManager;
286 
287  // True when the everything is selected
288  PRPackedBool mSelectionIsAll;
289 
290  // Current sort direction
291  PRPackedBool mCurrentSortDirectionIsAscending;
292 
293  // Should we include a fake "All" row in the tree
294  PRPackedBool mFakeAllRow;
295 
296  // True when we have a listener added to the playback service
297  PRPackedBool mIsListeningToPlayback;
298 
299  // True when the tree should prevent rebuilding itself.
300  PRPackedBool mShouldPreventRebuild;
301 
302  nsString mLocalizedAll;
303 
304  static PRInt32 const NOT_SET = -1;
305 
306  PRInt32 mFirstCachedRow;
307  PRInt32 mLastCachedRow;
308 
309  // Cached reference to play queue service
310  nsCOMPtr<sbIPlayQueueService> mPlayQueueService;
311 
312  // Cached play queue index
313  PRUint32 mPlayQueueIndex;
314 
321  class GuidArray
322  {
323  public:
327  GuidArray() : mItems(0) {}
331  void SetCapacity(PRUint32 aCapacity) {
332  mTempGuids.SetCapacity(aCapacity);
333  mTempGuidChars.SetCapacity(aCapacity);
334  }
339  nsresult Append(nsString const & aGuid) {
340  // If the array is not big enough append
341  if (mItems < mTempGuids.Length()) {
342  mTempGuids[mItems] = aGuid;
343  mTempGuidChars[mItems] = mTempGuids[mItems].get();
344  }
345  else {
346  // When appending, mItems should always equal the length of mTempGuids
347  NS_ASSERTION(mItems == mTempGuids.Length(),
348  "sbLocalDatabaseTreeView::GuidArray's array is out of sync");
349  nsString * newGuid = mTempGuids.AppendElement(aGuid);
350  NS_ENSURE_TRUE(newGuid, NS_ERROR_OUT_OF_MEMORY);
351 
352  mTempGuidChars.AppendElement(newGuid->get());
353  }
354  ++mItems;
355  return NS_OK;
356  }
360  void Reset() {
361  mItems = 0;
362  }
366  PRUint32 Length() const {
367  return mItems;
368  }
372  PRUnichar const * const * AsCharArray() const {
373  return mTempGuidChars.Elements();
374  }
375  private:
376  nsTArray<nsString> mTempGuids;
377  nsTArray<PRUnichar const *> mTempGuidChars;
378  PRUint32 mItems;
379  };
380  GuidArray mGuidWorkArray;
381 };
382 
384 {
386 public:
388  NS_DECL_NSISERIALIZABLE
389 
390  nsresult Init();
391  nsresult ToString(nsAString& aStr);
392 
394 
395 protected:
396  nsCOMPtr<sbILibrarySort> mSort;
397 
399 
400  PRPackedBool mSelectionIsAll;
401 
402 };
403 
404 #endif /* __SBLOCALDATABASETREEVIEW_H__ */
nsDataHashtable< nsStringHashKey, nsString > sbSelectionList
A tree view that views a media list view.
nsRefPtr< sbLocalDatabaseMediaListViewSelectionState > mSelection
return NS_OK
Manage the selection of items within a view. This interface is a subset of nsITreeViewSelection. Note that if you are part of the user interface, you probably want to be calling nsITreeView.selection instead - otherwise things can get subtly out of sync.
[USER CODE SHOULD NOT REFERENCE THIS CLASS]
onPageChanged aValue
Definition: FeedWriter.js:1395
NS_DECL_ISUPPORTS NS_DECL_NSICLASSINFO NS_DECL_NSIOBSERVER NS_DECL_NSITREEVIEW NS_DECL_SBILOCALDATABASEGUIDARRAYLISTENER NS_DECL_SBIMEDIALISTVIEWTREEVIEW NS_DECL_SBILOCALDATABASETREEVIEW NS_DECL_SBIMEDIACOREEVENTLISTENER NS_DECL_SBIMEDIALISTVIEWSELECTIONLISTENER NS_DECL_SBIPLAYQUEUESERVICELISTENER sbLocalDatabaseTreeView()
[USER CODE SHOULD NOT REFERENCE THIS CLASS]
nsresult TokenizeProperties(const nsAString &aProperties, nsISupportsArray *aAtomArray)
Parses a string and separates space-delimited substrings into nsIAtom elements.
[USER CODE SHOULD NOT REFERENCE THIS CLASS]
void SetShouldPreventRebuild(PRBool aShouldPreventRebuild)
A brief description of the contents of this interface.
A distinct view on a given media list.
Definition of the sbIMediacoreEvent interface.
nsCOMPtr< sbILibrarySort > mSort
nsresult Init(sbLocalDatabaseMediaListView *aListView, sbILocalDatabaseGUIDArray *aArray, sbIPropertyArray *aCurrentSort, sbLocalDatabaseTreeViewState *aState)
Listener for the Play Queue service.
nsresult OnTrackIndexChange(sbIMediacoreEvent *aEvent)
An interface used to describe a metadata property for use by the UI and other sbILibrary interfaces (...
nsISerializable
Media library abstraction.
Definition: sbILibrary.idl:82
A sort that can be applied to a media list view.
[USER CODE SHOULD NOT REFERENCE THIS CLASS]
Listener interface that gets notified on selection changes.
[USER CODE SHOULD NOT REFERENCE THIS CLASS]
restoreWindow aState
nsresult GetState(sbLocalDatabaseTreeViewState **aState)
An interface to carry around arrays of nsIProperty instances. Users of this interface should only QI ...
Manager for system wide metadata properties.
NS_DECL_ISUPPORTS NS_DECL_NSISERIALIZABLE nsresult Init()