8 * Copyright (C) 2005-2012 Team XBMC
11 * This Program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
16 * This Program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with XBMC; see the file COPYING. If not, see
23 * <http://www.gnu.org/licenses/>.
27 #include "guilib/GUIListItem.h"
28 #include "utils/Archive.h"
29 #include "utils/ISerializable.h"
30 #include "utils/ISortable.h"
31 #include "XBDateTime.h"
32 #include "utils/SortUtils.h"
33 #include "utils/LabelFormatter.h"
34 #include "GUIPassword.h"
35 #include "threads/CriticalSection.h"
38 #include "boost/shared_ptr.hpp"
53 class CPVRTimerInfoTag;
55 class CPictureInfoTag;
64 /* special startoffset used to indicate that we wish to resume */
65 #define STARTOFFSET_RESUME (-1)
70 \brief Represents a file on a share
74 public CGUIListItem, public IArchivable, public ISerializable, public ISortable
78 CFileItem(const CFileItem& item);
79 CFileItem(const CGUIListItem& item);
80 CFileItem(const CStdString& strLabel);
81 CFileItem(const CStdString& strPath, bool bIsFolder);
82 CFileItem(const CSong& song);
83 CFileItem(const CStdString &path, const CAlbum& album);
84 CFileItem(const CArtist& artist);
85 CFileItem(const CGenre& genre);
86 CFileItem(const MUSIC_INFO::CMusicInfoTag& music);
87 CFileItem(const CVideoInfoTag& movie);
88 CFileItem(const EPG::CEpgInfoTag& tag);
89 CFileItem(const PVR::CPVRChannel& channel);
90 CFileItem(const PVR::CPVRRecording& record);
91 CFileItem(const PVR::CPVRTimerInfoTag& timer);
92 CFileItem(const CMediaSource& share);
93 virtual ~CFileItem(void);
94 virtual CGUIListItem *Clone() const { return new CFileItem(*this); };
96 const CStdString &GetPath() const { return m_strPath; };
97 void SetPath(const CStdString &path) { m_strPath = path; };
100 const CFileItem& operator=(const CFileItem& item);
101 virtual void Archive(CArchive& ar);
102 virtual void Serialize(CVariant& value) const;
103 virtual void ToSortable(SortItem &sortable);
104 virtual bool IsFileItem() const { return true; };
106 bool Exists(bool bUseCache = true) const;
107 bool IsVideo() const;
108 bool IsDiscStub() const;
109 bool IsPicture() const;
110 bool IsLyrics() const;
111 bool IsAudio() const;
112 bool IsKaraoke() const;
113 bool IsCUESheet() const;
114 bool IsLastFM() const;
115 bool IsInternetStream(const bool bStrictCheck = false) const;
116 bool IsPlayList() const;
117 bool IsSmartPlayList() const;
118 bool IsPythonScript() const;
119 bool IsPlugin() const;
120 bool IsScript() const;
121 bool IsAddonsPath() const;
122 bool IsSourcesPath() const;
124 bool IsDVDImage() const;
125 bool IsOpticalMediaFile() const;
126 bool IsDVDFile(bool bVobs = true, bool bIfos = true) const;
127 bool IsBDFile() const;
133 bool IsISO9660() const;
136 bool IsOnDVD() const;
137 bool IsOnLAN() const;
141 bool IsRemote() const;
145 bool IsStack() const;
146 bool IsMultiPath() const;
147 bool IsMusicDb() const;
148 bool IsVideoDb() const;
150 bool IsPVRChannel() const;
151 bool IsPVRRecording() const;
152 bool IsPVRTimer() const;
153 bool IsType(const char *ext) const;
154 bool IsVirtualDirectoryRoot() const;
155 bool IsReadOnly() const;
156 bool CanQueue() const;
157 void SetCanQueue(bool bYesNo);
158 bool IsParentFolder() const;
159 bool IsFileFolder() const;
160 bool IsRemovable() const;
161 bool IsTuxBox() const;
162 bool IsMythTV() const;
163 bool IsHDHomeRun() const;
164 bool IsSlingbox() const;
167 bool IsLiveTV() const;
169 bool IsAndroidApp() const;
171 void RemoveExtension();
173 void FillInDefaultIcon();
174 void SetFileSizeLabel();
175 virtual void SetLabel(const CStdString &strLabel);
176 CURL GetAsUrl() const;
177 int GetVideoContentType() const; /* return VIDEODB_CONTENT_TYPE, but don't want to include videodb in this header */
178 bool IsLabelPreformated() const { return m_bLabelPreformated; }
179 void SetLabelPreformated(bool bYesNo) { m_bLabelPreformated=bYesNo; }
180 bool SortsOnTop() const { return m_specialSort == SortSpecialOnTop; }
181 bool SortsOnBottom() const { return m_specialSort == SortSpecialOnBottom; }
182 void SetSpecialSort(SortSpecial sort) { m_specialSort = sort; }
184 inline bool HasMusicInfoTag() const
186 return m_musicInfoTag != NULL;
189 MUSIC_INFO::CMusicInfoTag* GetMusicInfoTag();
191 inline const MUSIC_INFO::CMusicInfoTag* GetMusicInfoTag() const
193 return m_musicInfoTag;
196 inline bool HasVideoInfoTag() const
198 return m_videoInfoTag != NULL;
201 CVideoInfoTag* GetVideoInfoTag();
203 inline const CVideoInfoTag* GetVideoInfoTag() const
205 return m_videoInfoTag;
208 inline bool HasEPGInfoTag() const
210 return m_epgInfoTag != NULL;
213 EPG::CEpgInfoTag* GetEPGInfoTag();
215 inline const EPG::CEpgInfoTag* GetEPGInfoTag() const
220 inline bool HasPVRChannelInfoTag() const
222 return m_pvrChannelInfoTag != NULL;
225 PVR::CPVRChannel* GetPVRChannelInfoTag();
227 inline const PVR::CPVRChannel* GetPVRChannelInfoTag() const
229 return m_pvrChannelInfoTag;
232 inline bool HasPVRRecordingInfoTag() const
234 return m_pvrRecordingInfoTag != NULL;
237 PVR::CPVRRecording* GetPVRRecordingInfoTag();
239 inline const PVR::CPVRRecording* GetPVRRecordingInfoTag() const
241 return m_pvrRecordingInfoTag;
244 inline bool HasPVRTimerInfoTag() const
246 return m_pvrTimerInfoTag != NULL;
249 PVR::CPVRTimerInfoTag* GetPVRTimerInfoTag();
251 inline const PVR::CPVRTimerInfoTag* GetPVRTimerInfoTag() const
253 return m_pvrTimerInfoTag;
256 inline bool HasPictureInfoTag() const
258 return m_pictureInfoTag != NULL;
261 inline const CPictureInfoTag* GetPictureInfoTag() const
263 return m_pictureInfoTag;
266 CPictureInfoTag* GetPictureInfoTag();
269 \brief Get the local fanart for this item if it exists
270 \return path to the local fanart for this item, or empty if none exists
271 \sa GetFolderThumb, GetTBNFile
273 CStdString GetLocalFanart() const;
275 /*! \brief Assemble the filename of a particular piece of local artwork for an item.
276 No file existence check is typically performed.
277 \param artFile the art file to search for.
278 \param useFolder whether to look in the folder for the art file. Defaults to false.
279 \return the path to the local artwork.
282 CStdString GetLocalArt(const std::string &artFile, bool useFolder = false) const;
284 /*! \brief Assemble the filename of a particular piece of local artwork for an item,
285 and check for file existence.
286 \param artFile the art file to search for.
287 \param useFolder whether to look in the folder for the art file. Defaults to false.
288 \return the path to the local artwork if it exists, empty otherwise.
291 CStdString FindLocalArt(const std::string &artFile, bool useFolder) const;
293 // Gets the .tbn file associated with this item
294 CStdString GetTBNFile() const;
295 // Gets the folder image associated with this item (defaults to folder.jpg)
296 CStdString GetFolderThumb(const CStdString &folderJPG = "folder.jpg") const;
297 // Gets the correct movie title
298 CStdString GetMovieName(bool bUseFolderNames = false) const;
300 /*! \brief Find the base movie path (i.e. the item the user expects us to use to lookup the movie)
301 For folder items, with "use foldernames for lookups" it returns the folder.
302 Regardless of settings, for VIDEO_TS/BDMV it returns the parent of the VIDEO_TS/BDMV folder (if present)
304 \param useFolderNames whether we're using foldernames for lookups
305 \return the base movie folder
307 CStdString GetBaseMoviePath(bool useFolderNames) const;
309 // Gets the user thumb, if it exists
310 CStdString GetUserMusicThumb(bool alwaysCheckRemote = false, bool fallbackToFolder = false) const;
312 /*! \brief Get the path where we expect local metadata to reside.
313 For a folder, this is just the existing path (eg tvshow folder)
314 For a file, this is the parent path, with exceptions made for VIDEO_TS and BDMV files
316 Three cases are handled:
318 /foo/bar/movie_name/file_name -> /foo/bar/movie_name/
319 /foo/bar/movie_name/VIDEO_TS/file_name -> /foo/bar/movie_name/
320 /foo/bar/movie_name/BDMV/file_name -> /foo/bar/movie_name/
322 \sa URIUtils::GetParentPath
324 CStdString GetLocalMetadataPath() const;
326 // finds a matching local trailer file
327 CStdString FindTrailer() const;
329 virtual bool LoadMusicTag();
331 /* returns the content type of this item if known. will lookup for http streams */
332 const CStdString& GetMimeType(bool lookup = true) const;
334 /* sets the mime-type if known beforehand */
335 void SetMimeType(const CStdString& mimetype) { m_mimetype = mimetype; } ;
337 /* general extra info about the contents of the item, not for display */
338 void SetExtraInfo(const CStdString& info) { m_extrainfo = info; };
339 const CStdString& GetExtraInfo() const { return m_extrainfo; };
341 /*! \brief Update an item with information from another item
342 We take metadata information from the given item and supplement the current item
343 with that info. If tags exist in the new item we use the entire tag information.
344 Properties are appended, and labels, thumbnail and icon are updated if non-empty
346 \param item the item used to supplement information
347 \param replaceLabels whether to replace labels (defaults to true)
349 void UpdateInfo(const CFileItem &item, bool replaceLabels = true);
351 bool IsSamePath(const CFileItem *item) const;
353 bool IsAlbum() const;
355 /*! \brief Sets details using the information from the CVideoInfoTag object
356 Sets the videoinfotag and uses its information to set the label and path.
357 \param video video details to use and set
359 void SetFromVideoInfoTag(const CVideoInfoTag &video);
360 /*! \brief Sets details using the information from the CAlbum object
361 Sets the album in the music info tag and uses its information to set the
362 label and album-specific properties.
363 \param album album details to use and set
365 void SetFromAlbum(const CAlbum &album);
366 /*! \brief Sets details using the information from the CSong object
367 Sets the song in the music info tag and uses its information to set the
368 label, path, song-specific properties and artwork.
369 \param song song details to use and set
371 void SetFromSong(const CSong &song);
373 bool m_bIsShareOrDrive; ///< is this a root share/drive
374 int m_iDriveType; ///< If \e m_bIsShareOrDrive is \e true, use to get the share type. Types see: CMediaSource::m_iDriveType
375 CDateTime m_dateTime; ///< file creation date & time
376 int64_t m_dwSize; ///< file size (0 for folders)
377 CStdString m_strDVDLabel;
378 CStdString m_strTitle;
382 int m_lStartPartNumber;
384 LockType m_iLockMode;
385 CStdString m_strLockCode;
386 int m_iHasLock; // 0 - no lock 1 - lock, but unlocked 2 - locked
390 CStdString m_strPath; ///< complete path to item
392 SortSpecial m_specialSort;
393 bool m_bIsParentFolder;
395 bool m_bLabelPreformated;
396 CStdString m_mimetype;
397 CStdString m_extrainfo;
398 MUSIC_INFO::CMusicInfoTag* m_musicInfoTag;
399 CVideoInfoTag* m_videoInfoTag;
400 EPG::CEpgInfoTag* m_epgInfoTag;
401 PVR::CPVRChannel* m_pvrChannelInfoTag;
402 PVR::CPVRRecording* m_pvrRecordingInfoTag;
403 PVR::CPVRTimerInfoTag * m_pvrTimerInfoTag;
404 CPictureInfoTag* m_pictureInfoTag;
409 \brief A shared pointer to CFileItem
412 typedef boost::shared_ptr<CFileItem> CFileItemPtr;
415 \brief A vector of pointer to CFileItem
418 typedef std::vector< CFileItemPtr > VECFILEITEMS;
421 \brief Iterator for VECFILEITEMS
424 typedef std::vector< CFileItemPtr >::iterator IVECFILEITEMS;
427 \brief A map of pointers to CFileItem
430 typedef std::map<CStdString, CFileItemPtr > MAPFILEITEMS;
433 \brief Iterator for MAPFILEITEMS
436 typedef std::map<CStdString, CFileItemPtr >::iterator IMAPFILEITEMS;
439 \brief Pair for MAPFILEITEMS
442 typedef std::pair<CStdString, CFileItemPtr > MAPFILEITEMSPAIR;
444 typedef bool (*FILEITEMLISTCOMPARISONFUNC) (const CFileItemPtr &pItem1, const CFileItemPtr &pItem2);
445 typedef void (*FILEITEMFILLFUNC) (CFileItemPtr &item);
448 \brief Represents a list of files
449 \sa CFileItemList, CFileItem
451 class CFileItemList : public CFileItem
454 enum CACHE_TYPE { CACHE_NEVER = 0, CACHE_IF_SLOW, CACHE_ALWAYS };
457 CFileItemList(const CStdString& strPath);
458 virtual ~CFileItemList();
459 virtual void Archive(CArchive& ar);
460 CFileItemPtr operator[] (int iItem);
461 const CFileItemPtr operator[] (int iItem) const;
462 CFileItemPtr operator[] (const CStdString& strPath);
463 const CFileItemPtr operator[] (const CStdString& strPath) const;
466 void Add(const CFileItemPtr &pItem);
467 void AddFront(const CFileItemPtr &pItem, int itemPosition);
468 void Remove(CFileItem* pItem);
469 void Remove(int iItem);
470 CFileItemPtr Get(int iItem);
471 const CFileItemPtr Get(int iItem) const;
472 CFileItemPtr Get(const CStdString& strPath);
473 const CFileItemPtr Get(const CStdString& strPath) const;
475 bool IsEmpty() const;
476 void Append(const CFileItemList& itemlist);
477 void Assign(const CFileItemList& itemlist, bool append = false);
478 bool Copy (const CFileItemList& item);
479 void Reserve(int iCount);
480 void Sort(SORT_METHOD sortMethod, SortOrder sortOrder);
481 /* \brief Sorts the items based on the given sorting options
483 In contrast to Sort (see above) this does not change the internal
484 state by storing the sorting method and order used and therefore
485 will always execute the sorting even if the list of items has
486 already been sorted with the same options before.
488 void Sort(SortDescription sortDescription);
490 void FillInDefaultIcons();
491 int GetFolderCount() const;
492 int GetFileCount() const;
493 int GetSelectedCount() const;
494 int GetObjectCount() const;
495 void FilterCueItems();
496 void RemoveExtensions();
497 void SetFastLookup(bool fastLookup);
498 bool Contains(const CStdString& fileName) const;
499 bool GetFastLookup() const { return m_fastLookup; };
501 /*! \brief stack a CFileItemList
502 By default we stack all items (files and folders) in a CFileItemList
503 \param stackFiles whether to stack all items or just collapse folders (defaults to true)
504 \sa StackFiles,StackFolders
506 void Stack(bool stackFiles = true);
508 SortOrder GetSortOrder() const { return m_sortOrder; }
509 SORT_METHOD GetSortMethod() const { return m_sortMethod; }
510 /*! \brief load a CFileItemList out of the cache
512 The file list may be cached based on which window we're viewing in, as different
513 windows will be listing different portions of the same URL (eg viewing music files
514 versus viewing video files)
516 \param windowID id of the window that's loading this list (defaults to 0)
517 \return true if we loaded from the cache, false otherwise.
518 \sa Save,RemoveDiscCache
520 bool Load(int windowID = 0);
522 /*! \brief save a CFileItemList to the cache
524 The file list may be cached based on which window we're viewing in, as different
525 windows will be listing different portions of the same URL (eg viewing music files
526 versus viewing video files)
528 \param windowID id of the window that's saving this list (defaults to 0)
529 \return true if successful, false otherwise.
530 \sa Load,RemoveDiscCache
532 bool Save(int windowID = 0);
533 void SetCacheToDisc(CACHE_TYPE cacheToDisc) { m_cacheToDisc = cacheToDisc; }
534 bool CacheToDiscAlways() const { return m_cacheToDisc == CACHE_ALWAYS; }
535 bool CacheToDiscIfSlow() const { return m_cacheToDisc == CACHE_IF_SLOW; }
536 /*! \brief remove a previously cached CFileItemList from the cache
538 The file list may be cached based on which window we're viewing in, as different
539 windows will be listing different portions of the same URL (eg viewing music files
540 versus viewing video files)
542 \param windowID id of the window whose cache we which to remove (defaults to 0)
545 void RemoveDiscCache(int windowID = 0) const;
546 bool AlwaysCache() const;
548 void Swap(unsigned int item1, unsigned int item2);
550 /*! \brief Update an item in the item list
551 \param item the new item, which we match based on path to an existing item in the list
552 \return true if the item exists in the list (and was thus updated), false otherwise.
554 bool UpdateItem(const CFileItem *item);
556 void AddSortMethod(SORT_METHOD method, int buttonLabel, const LABEL_MASKS &labelMasks);
557 bool HasSortDetails() const { return m_sortDetails.size() != 0; };
558 const std::vector<SORT_METHOD_DETAILS> &GetSortDetails() const { return m_sortDetails; };
560 /*! \brief Specify whether this list should be sorted with folders separate from files
561 By default we sort with folders listed (and sorted separately) except for those sort modes
562 which should be explicitly sorted with folders interleaved with files (eg SORT_METHOD_FILES).
563 With this set the folder state will be ignored, allowing folders and files to sort interleaved.
564 \param sort whether to ignore the folder state.
566 void SetSortIgnoreFolders(bool sort) { m_sortIgnoreFolders = sort; };
567 bool GetReplaceListing() const { return m_replaceListing; };
568 void SetReplaceListing(bool replace);
569 void SetContent(const CStdString &content) { m_content = content; };
570 const CStdString &GetContent() const { return m_content; };
572 void ClearSortState();
574 void Sort(FILEITEMLISTCOMPARISONFUNC func);
575 void FillSortFields(FILEITEMFILLFUNC func);
576 CStdString GetDiscFileCache(int windowID) const;
579 \brief stack files in a CFileItemList
585 \brief stack folders in a CFileItemList
590 VECFILEITEMS m_items;
593 SORT_METHOD m_sortMethod;
594 SortOrder m_sortOrder;
595 bool m_sortIgnoreFolders;
596 CACHE_TYPE m_cacheToDisc;
597 bool m_replaceListing;
598 CStdString m_content;
600 std::vector<SORT_METHOD_DETAILS> m_sortDetails;
602 CCriticalSection m_lock;