8 * Copyright (C) 2005-2013 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)
69 enum EFileFolderType {
70 EFILEFOLDER_TYPE_ALWAYS = 1<<0,
71 EFILEFOLDER_TYPE_ONCLICK = 1<<1,
72 EFILEFOLDER_TYPE_ONBROWSE = 1<<2,
74 EFILEFOLDER_MASK_ALL = 0xff,
75 EFILEFOLDER_MASK_ONCLICK = EFILEFOLDER_TYPE_ALWAYS
76 | EFILEFOLDER_TYPE_ONCLICK,
77 EFILEFOLDER_MASK_ONBROWSE = EFILEFOLDER_TYPE_ALWAYS
78 | EFILEFOLDER_TYPE_ONCLICK
79 | EFILEFOLDER_TYPE_ONBROWSE,
83 \brief Represents a file on a share
87 public CGUIListItem, public IArchivable, public ISerializable, public ISortable
91 CFileItem(const CFileItem& item);
92 CFileItem(const CGUIListItem& item);
93 CFileItem(const CStdString& strLabel);
94 CFileItem(const CStdString& strPath, bool bIsFolder);
95 CFileItem(const CSong& song);
96 CFileItem(const CStdString &path, const CAlbum& album);
97 CFileItem(const CArtist& artist);
98 CFileItem(const CGenre& genre);
99 CFileItem(const MUSIC_INFO::CMusicInfoTag& music);
100 CFileItem(const CVideoInfoTag& movie);
101 CFileItem(const EPG::CEpgInfoTag& tag);
102 CFileItem(const PVR::CPVRChannel& channel);
103 CFileItem(const PVR::CPVRRecording& record);
104 CFileItem(const PVR::CPVRTimerInfoTag& timer);
105 CFileItem(const CMediaSource& share);
106 virtual ~CFileItem(void);
107 virtual CGUIListItem *Clone() const { return new CFileItem(*this); };
109 const CStdString &GetPath() const { return m_strPath; };
110 void SetPath(const CStdString &path) { m_strPath = path; };
113 const CFileItem& operator=(const CFileItem& item);
114 virtual void Archive(CArchive& ar);
115 virtual void Serialize(CVariant& value) const;
116 virtual void ToSortable(SortItem &sortable);
117 virtual bool IsFileItem() const { return true; };
119 bool Exists(bool bUseCache = true) const;
122 \brief Check whether an item is a video item. Note that this returns true for
123 anything with a video info tag, so that may include eg. folders.
124 \return true if item is video, false otherwise.
126 bool IsVideo() const;
128 bool IsDiscStub() const;
131 \brief Check whether an item is a picture item. Note that this returns true for
132 anything with a picture info tag, so that may include eg. folders.
133 \return true if item is picture, false otherwise.
135 bool IsPicture() const;
136 bool IsLyrics() const;
139 \brief Check whether an item is an audio item. Note that this returns true for
140 anything with a music info tag, so that may include eg. folders.
141 \return true if item is audio, false otherwise.
143 bool IsAudio() const;
145 bool IsKaraoke() const;
146 bool IsCUESheet() const;
147 bool IsInternetStream(const bool bStrictCheck = false) const;
148 bool IsPlayList() const;
149 bool IsSmartPlayList() const;
150 bool IsLibraryFolder() const;
151 bool IsPythonScript() const;
152 bool IsPlugin() const;
153 bool IsScript() const;
154 bool IsAddonsPath() const;
155 bool IsSourcesPath() const;
157 bool IsDVDImage() const;
158 bool IsOpticalMediaFile() const;
159 bool IsDVDFile(bool bVobs = true, bool bIfos = true) const;
160 bool IsBDFile() const;
166 bool IsISO9660() const;
169 bool IsOnDVD() const;
170 bool IsOnLAN() const;
174 bool IsRemote() const;
178 bool IsStack() const;
179 bool IsMultiPath() const;
180 bool IsMusicDb() const;
181 bool IsVideoDb() const;
183 bool IsPVRChannel() const;
184 bool IsPVRRecording() const;
185 bool IsPVRTimer() const;
186 bool IsType(const char *ext) const;
187 bool IsVirtualDirectoryRoot() const;
188 bool IsReadOnly() const;
189 bool CanQueue() const;
190 void SetCanQueue(bool bYesNo);
191 bool IsParentFolder() const;
192 bool IsFileFolder(EFileFolderType types = EFILEFOLDER_MASK_ALL) const;
193 bool IsRemovable() const;
194 bool IsTuxBox() const;
195 bool IsMythTV() const;
196 bool IsHDHomeRun() const;
197 bool IsSlingbox() const;
200 bool IsLiveTV() const;
202 bool IsAndroidApp() const;
204 void RemoveExtension();
206 void FillInDefaultIcon();
207 void SetFileSizeLabel();
208 virtual void SetLabel(const CStdString &strLabel);
209 CURL GetAsUrl() const;
210 int GetVideoContentType() const; /* return VIDEODB_CONTENT_TYPE, but don't want to include videodb in this header */
211 bool IsLabelPreformated() const { return m_bLabelPreformated; }
212 void SetLabelPreformated(bool bYesNo) { m_bLabelPreformated=bYesNo; }
213 bool SortsOnTop() const { return m_specialSort == SortSpecialOnTop; }
214 bool SortsOnBottom() const { return m_specialSort == SortSpecialOnBottom; }
215 void SetSpecialSort(SortSpecial sort) { m_specialSort = sort; }
217 inline bool HasMusicInfoTag() const
219 return m_musicInfoTag != NULL;
222 MUSIC_INFO::CMusicInfoTag* GetMusicInfoTag();
224 inline const MUSIC_INFO::CMusicInfoTag* GetMusicInfoTag() const
226 return m_musicInfoTag;
229 inline bool HasVideoInfoTag() const
231 return m_videoInfoTag != NULL;
234 CVideoInfoTag* GetVideoInfoTag();
236 inline const CVideoInfoTag* GetVideoInfoTag() const
238 return m_videoInfoTag;
241 inline bool HasEPGInfoTag() const
243 return m_epgInfoTag != NULL;
246 EPG::CEpgInfoTag* GetEPGInfoTag();
248 inline const EPG::CEpgInfoTag* GetEPGInfoTag() const
253 inline bool HasPVRChannelInfoTag() const
255 return m_pvrChannelInfoTag != NULL;
258 PVR::CPVRChannel* GetPVRChannelInfoTag();
260 inline const PVR::CPVRChannel* GetPVRChannelInfoTag() const
262 return m_pvrChannelInfoTag;
265 inline bool HasPVRRecordingInfoTag() const
267 return m_pvrRecordingInfoTag != NULL;
270 PVR::CPVRRecording* GetPVRRecordingInfoTag();
272 inline const PVR::CPVRRecording* GetPVRRecordingInfoTag() const
274 return m_pvrRecordingInfoTag;
277 inline bool HasPVRTimerInfoTag() const
279 return m_pvrTimerInfoTag != NULL;
282 PVR::CPVRTimerInfoTag* GetPVRTimerInfoTag();
284 inline const PVR::CPVRTimerInfoTag* GetPVRTimerInfoTag() const
286 return m_pvrTimerInfoTag;
289 inline bool HasPictureInfoTag() const
291 return m_pictureInfoTag != NULL;
294 inline const CPictureInfoTag* GetPictureInfoTag() const
296 return m_pictureInfoTag;
299 CPictureInfoTag* GetPictureInfoTag();
302 \brief Get the local fanart for this item if it exists
303 \return path to the local fanart for this item, or empty if none exists
304 \sa GetFolderThumb, GetTBNFile
306 CStdString GetLocalFanart() const;
308 /*! \brief Assemble the filename of a particular piece of local artwork for an item.
309 No file existence check is typically performed.
310 \param artFile the art file to search for.
311 \param useFolder whether to look in the folder for the art file. Defaults to false.
312 \return the path to the local artwork.
315 CStdString GetLocalArt(const std::string &artFile, bool useFolder = false) const;
317 /*! \brief Assemble the filename of a particular piece of local artwork for an item,
318 and check for file existence.
319 \param artFile the art file to search for.
320 \param useFolder whether to look in the folder for the art file. Defaults to false.
321 \return the path to the local artwork if it exists, empty otherwise.
324 CStdString FindLocalArt(const std::string &artFile, bool useFolder) const;
326 // Gets the .tbn file associated with this item
327 CStdString GetTBNFile() const;
328 // Gets the folder image associated with this item (defaults to folder.jpg)
329 CStdString GetFolderThumb(const CStdString &folderJPG = "folder.jpg") const;
330 // Gets the correct movie title
331 CStdString GetMovieName(bool bUseFolderNames = false) const;
333 /*! \brief Find the base movie path (i.e. the item the user expects us to use to lookup the movie)
334 For folder items, with "use foldernames for lookups" it returns the folder.
335 Regardless of settings, for VIDEO_TS/BDMV it returns the parent of the VIDEO_TS/BDMV folder (if present)
337 \param useFolderNames whether we're using foldernames for lookups
338 \return the base movie folder
340 CStdString GetBaseMoviePath(bool useFolderNames) const;
342 // Gets the user thumb, if it exists
343 CStdString GetUserMusicThumb(bool alwaysCheckRemote = false, bool fallbackToFolder = false) const;
345 /*! \brief Get the path where we expect local metadata to reside.
346 For a folder, this is just the existing path (eg tvshow folder)
347 For a file, this is the parent path, with exceptions made for VIDEO_TS and BDMV files
349 Three cases are handled:
351 /foo/bar/movie_name/file_name -> /foo/bar/movie_name/
352 /foo/bar/movie_name/VIDEO_TS/file_name -> /foo/bar/movie_name/
353 /foo/bar/movie_name/BDMV/file_name -> /foo/bar/movie_name/
355 \sa URIUtils::GetParentPath
357 CStdString GetLocalMetadataPath() const;
359 // finds a matching local trailer file
360 CStdString FindTrailer() const;
362 virtual bool LoadMusicTag();
364 /* Returns the content type of this item if known */
365 const CStdString& GetMimeType() const { return m_mimetype; }
367 /* sets the mime-type if known beforehand */
368 void SetMimeType(const CStdString& mimetype) { m_mimetype = mimetype; } ;
370 /*! \brief Resolve the MIME type based on file extension or a web lookup
371 If m_mimetype is already set (non-empty), this function has no effect. For
372 http:// and shout:// streams, this will query the stream (blocking operation).
373 Set lookup=false to skip any internet lookups and always return immediately.
375 void FillInMimeType(bool lookup = true);
377 /* general extra info about the contents of the item, not for display */
378 void SetExtraInfo(const CStdString& info) { m_extrainfo = info; };
379 const CStdString& GetExtraInfo() const { return m_extrainfo; };
381 /*! \brief Update an item with information from another item
382 We take metadata information from the given item and supplement the current item
383 with that info. If tags exist in the new item we use the entire tag information.
384 Properties are appended, and labels, thumbnail and icon are updated if non-empty
386 \param item the item used to supplement information
387 \param replaceLabels whether to replace labels (defaults to true)
389 void UpdateInfo(const CFileItem &item, bool replaceLabels = true);
391 bool IsSamePath(const CFileItem *item) const;
393 bool IsAlbum() const;
395 /*! \brief Sets details using the information from the CVideoInfoTag object
396 Sets the videoinfotag and uses its information to set the label and path.
397 \param video video details to use and set
399 void SetFromVideoInfoTag(const CVideoInfoTag &video);
400 /*! \brief Sets details using the information from the CAlbum object
401 Sets the album in the music info tag and uses its information to set the
402 label and album-specific properties.
403 \param album album details to use and set
405 void SetFromAlbum(const CAlbum &album);
406 /*! \brief Sets details using the information from the CSong object
407 Sets the song in the music info tag and uses its information to set the
408 label, path, song-specific properties and artwork.
409 \param song song details to use and set
411 void SetFromSong(const CSong &song);
413 bool m_bIsShareOrDrive; ///< is this a root share/drive
414 int m_iDriveType; ///< If \e m_bIsShareOrDrive is \e true, use to get the share type. Types see: CMediaSource::m_iDriveType
415 CDateTime m_dateTime; ///< file creation date & time
416 int64_t m_dwSize; ///< file size (0 for folders)
417 CStdString m_strDVDLabel;
418 CStdString m_strTitle;
422 int m_lStartPartNumber;
424 LockType m_iLockMode;
425 CStdString m_strLockCode;
426 int m_iHasLock; // 0 - no lock 1 - lock, but unlocked 2 - locked
430 CStdString m_strPath; ///< complete path to item
432 SortSpecial m_specialSort;
433 bool m_bIsParentFolder;
435 bool m_bLabelPreformated;
436 CStdString m_mimetype;
437 CStdString m_extrainfo;
438 MUSIC_INFO::CMusicInfoTag* m_musicInfoTag;
439 CVideoInfoTag* m_videoInfoTag;
440 EPG::CEpgInfoTag* m_epgInfoTag;
441 PVR::CPVRChannel* m_pvrChannelInfoTag;
442 PVR::CPVRRecording* m_pvrRecordingInfoTag;
443 PVR::CPVRTimerInfoTag * m_pvrTimerInfoTag;
444 CPictureInfoTag* m_pictureInfoTag;
449 \brief A shared pointer to CFileItem
452 typedef boost::shared_ptr<CFileItem> CFileItemPtr;
455 \brief A vector of pointer to CFileItem
458 typedef std::vector< CFileItemPtr > VECFILEITEMS;
461 \brief Iterator for VECFILEITEMS
464 typedef std::vector< CFileItemPtr >::iterator IVECFILEITEMS;
467 \brief A map of pointers to CFileItem
470 typedef std::map<CStdString, CFileItemPtr > MAPFILEITEMS;
473 \brief Iterator for MAPFILEITEMS
476 typedef std::map<CStdString, CFileItemPtr >::iterator IMAPFILEITEMS;
479 \brief Pair for MAPFILEITEMS
482 typedef std::pair<CStdString, CFileItemPtr > MAPFILEITEMSPAIR;
484 typedef bool (*FILEITEMLISTCOMPARISONFUNC) (const CFileItemPtr &pItem1, const CFileItemPtr &pItem2);
485 typedef void (*FILEITEMFILLFUNC) (CFileItemPtr &item);
488 \brief Represents a list of files
489 \sa CFileItemList, CFileItem
491 class CFileItemList : public CFileItem
494 enum CACHE_TYPE { CACHE_NEVER = 0, CACHE_IF_SLOW, CACHE_ALWAYS };
497 CFileItemList(const CStdString& strPath);
498 virtual ~CFileItemList();
499 virtual void Archive(CArchive& ar);
500 CFileItemPtr operator[] (int iItem);
501 const CFileItemPtr operator[] (int iItem) const;
502 CFileItemPtr operator[] (const CStdString& strPath);
503 const CFileItemPtr operator[] (const CStdString& strPath) const;
506 void Add(const CFileItemPtr &pItem);
507 void AddFront(const CFileItemPtr &pItem, int itemPosition);
508 void Remove(CFileItem* pItem);
509 void Remove(int iItem);
510 CFileItemPtr Get(int iItem);
511 const CFileItemPtr Get(int iItem) const;
512 const VECFILEITEMS GetList() const { return m_items; }
513 CFileItemPtr Get(const CStdString& strPath);
514 const CFileItemPtr Get(const CStdString& strPath) const;
516 bool IsEmpty() const;
517 void Append(const CFileItemList& itemlist);
518 void Assign(const CFileItemList& itemlist, bool append = false);
519 bool Copy (const CFileItemList& item, bool copyItems = true);
520 void Reserve(int iCount);
521 void Sort(SortBy sortBy, SortOrder sortOrder, SortAttribute sortAttributes = SortAttributeNone);
522 /* \brief Sorts the items based on the given sorting options
524 In contrast to Sort (see above) this does not change the internal
525 state by storing the sorting method and order used and therefore
526 will always execute the sorting even if the list of items has
527 already been sorted with the same options before.
529 void Sort(SortDescription sortDescription);
531 void FillInDefaultIcons();
532 int GetFolderCount() const;
533 int GetFileCount() const;
534 int GetSelectedCount() const;
535 int GetObjectCount() const;
536 void FilterCueItems();
537 void RemoveExtensions();
538 void SetFastLookup(bool fastLookup);
539 bool Contains(const CStdString& fileName) const;
540 bool GetFastLookup() const { return m_fastLookup; };
542 /*! \brief stack a CFileItemList
543 By default we stack all items (files and folders) in a CFileItemList
544 \param stackFiles whether to stack all items or just collapse folders (defaults to true)
545 \sa StackFiles,StackFolders
547 void Stack(bool stackFiles = true);
549 SortOrder GetSortOrder() const { return m_sortDescription.sortOrder; }
550 SortBy GetSortMethod() const { return m_sortDescription.sortBy; }
551 /*! \brief load a CFileItemList out of the cache
553 The file list may be cached based on which window we're viewing in, as different
554 windows will be listing different portions of the same URL (eg viewing music files
555 versus viewing video files)
557 \param windowID id of the window that's loading this list (defaults to 0)
558 \return true if we loaded from the cache, false otherwise.
559 \sa Save,RemoveDiscCache
561 bool Load(int windowID = 0);
563 /*! \brief save a CFileItemList to the cache
565 The file list may be cached based on which window we're viewing in, as different
566 windows will be listing different portions of the same URL (eg viewing music files
567 versus viewing video files)
569 \param windowID id of the window that's saving this list (defaults to 0)
570 \return true if successful, false otherwise.
571 \sa Load,RemoveDiscCache
573 bool Save(int windowID = 0);
574 void SetCacheToDisc(CACHE_TYPE cacheToDisc) { m_cacheToDisc = cacheToDisc; }
575 bool CacheToDiscAlways() const { return m_cacheToDisc == CACHE_ALWAYS; }
576 bool CacheToDiscIfSlow() const { return m_cacheToDisc == CACHE_IF_SLOW; }
577 /*! \brief remove a previously cached CFileItemList from the cache
579 The file list may be cached based on which window we're viewing in, as different
580 windows will be listing different portions of the same URL (eg viewing music files
581 versus viewing video files)
583 \param windowID id of the window whose cache we which to remove (defaults to 0)
586 void RemoveDiscCache(int windowID = 0) const;
587 bool AlwaysCache() const;
589 void Swap(unsigned int item1, unsigned int item2);
591 /*! \brief Update an item in the item list
592 \param item the new item, which we match based on path to an existing item in the list
593 \return true if the item exists in the list (and was thus updated), false otherwise.
595 bool UpdateItem(const CFileItem *item);
597 void AddSortMethod(SortBy sortBy, int buttonLabel, const LABEL_MASKS &labelMasks, SortAttribute sortAttributes = SortAttributeNone);
598 void AddSortMethod(SortBy sortBy, SortAttribute sortAttributes, int buttonLabel, const LABEL_MASKS &labelMasks);
599 void AddSortMethod(SortDescription sortDescription, int buttonLabel, const LABEL_MASKS &labelMasks);
600 bool HasSortDetails() const { return m_sortDetails.size() != 0; };
601 const std::vector<SORT_METHOD_DETAILS> &GetSortDetails() const { return m_sortDetails; };
603 /*! \brief Specify whether this list should be sorted with folders separate from files
604 By default we sort with folders listed (and sorted separately) except for those sort modes
605 which should be explicitly sorted with folders interleaved with files (eg SORT_METHOD_FILES).
606 With this set the folder state will be ignored, allowing folders and files to sort interleaved.
607 \param sort whether to ignore the folder state.
609 void SetSortIgnoreFolders(bool sort) { m_sortIgnoreFolders = sort; };
610 bool GetReplaceListing() const { return m_replaceListing; };
611 void SetReplaceListing(bool replace);
612 void SetContent(const CStdString &content) { m_content = content; };
613 const CStdString &GetContent() const { return m_content; };
615 void ClearSortState();
617 void Sort(FILEITEMLISTCOMPARISONFUNC func);
618 void FillSortFields(FILEITEMFILLFUNC func);
619 CStdString GetDiscFileCache(int windowID) const;
622 \brief stack files in a CFileItemList
628 \brief stack folders in a CFileItemList
633 VECFILEITEMS m_items;
636 SortDescription m_sortDescription;
637 bool m_sortIgnoreFolders;
638 CACHE_TYPE m_cacheToDisc;
639 bool m_replaceListing;
640 CStdString m_content;
642 std::vector<SORT_METHOD_DETAILS> m_sortDetails;
644 CCriticalSection m_lock;