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, Field field) const;
117 void ToSortable(SortItem &sortable, const Fields &fields) const;
118 virtual bool IsFileItem() const { return true; };
120 bool Exists(bool bUseCache = true) const;
123 \brief Check whether an item is an optical media folder or its parent.
124 This will return the non-empty path to the playable entry point of the media
125 one or two levels down (VIDEO_TS.IFO for DVDs or index.bdmv for BDs).
126 The returned path will be empty if folder does not meet this criterion.
127 \return non-empty string if item is optical media folder, empty otherwise.
129 std::string GetOpticalMediaPath() const;
131 \brief Check whether an item is a video item. Note that this returns true for
132 anything with a video info tag, so that may include eg. folders.
133 \return true if item is video, false otherwise.
135 bool IsVideo() const;
137 bool IsDiscStub() const;
140 \brief Check whether an item is a picture item. Note that this returns true for
141 anything with a picture info tag, so that may include eg. folders.
142 \return true if item is picture, false otherwise.
144 bool IsPicture() const;
145 bool IsLyrics() const;
148 \brief Check whether an item is an audio item. Note that this returns true for
149 anything with a music info tag, so that may include eg. folders.
150 \return true if item is audio, false otherwise.
152 bool IsAudio() const;
154 bool IsKaraoke() const;
155 bool IsCUESheet() const;
156 bool IsInternetStream(const bool bStrictCheck = false) const;
157 bool IsPlayList() const;
158 bool IsSmartPlayList() const;
159 bool IsLibraryFolder() const;
160 bool IsPythonScript() const;
161 bool IsPlugin() const;
162 bool IsScript() const;
163 bool IsAddonsPath() const;
164 bool IsSourcesPath() const;
166 bool IsDVDImage() const;
167 bool IsOpticalMediaFile() const;
168 bool IsDVDFile(bool bVobs = true, bool bIfos = true) const;
169 bool IsBDFile() const;
175 bool IsISO9660() const;
178 bool IsOnDVD() const;
179 bool IsOnLAN() const;
183 bool IsRemote() const;
187 bool IsStack() const;
188 bool IsMultiPath() const;
189 bool IsMusicDb() const;
190 bool IsVideoDb() const;
192 bool IsPVRChannel() const;
193 bool IsPVRRecording() const;
194 bool IsPVRTimer() const;
195 bool IsType(const char *ext) const;
196 bool IsVirtualDirectoryRoot() const;
197 bool IsReadOnly() const;
198 bool CanQueue() const;
199 void SetCanQueue(bool bYesNo);
200 bool IsParentFolder() const;
201 bool IsFileFolder(EFileFolderType types = EFILEFOLDER_MASK_ALL) const;
202 bool IsRemovable() const;
203 bool IsTuxBox() const;
204 bool IsMythTV() const;
205 bool IsHDHomeRun() const;
206 bool IsSlingbox() const;
209 bool IsLiveTV() const;
211 bool IsAndroidApp() const;
213 void RemoveExtension();
215 void FillInDefaultIcon();
216 void SetFileSizeLabel();
217 virtual void SetLabel(const CStdString &strLabel);
218 CURL GetAsUrl() const;
219 int GetVideoContentType() const; /* return VIDEODB_CONTENT_TYPE, but don't want to include videodb in this header */
220 bool IsLabelPreformated() const { return m_bLabelPreformated; }
221 void SetLabelPreformated(bool bYesNo) { m_bLabelPreformated=bYesNo; }
222 bool SortsOnTop() const { return m_specialSort == SortSpecialOnTop; }
223 bool SortsOnBottom() const { return m_specialSort == SortSpecialOnBottom; }
224 void SetSpecialSort(SortSpecial sort) { m_specialSort = sort; }
226 inline bool HasMusicInfoTag() const
228 return m_musicInfoTag != NULL;
231 MUSIC_INFO::CMusicInfoTag* GetMusicInfoTag();
233 inline const MUSIC_INFO::CMusicInfoTag* GetMusicInfoTag() const
235 return m_musicInfoTag;
238 inline bool HasVideoInfoTag() const
240 return m_videoInfoTag != NULL;
243 CVideoInfoTag* GetVideoInfoTag();
245 inline const CVideoInfoTag* GetVideoInfoTag() const
247 return m_videoInfoTag;
250 inline bool HasEPGInfoTag() const
252 return m_epgInfoTag != NULL;
255 EPG::CEpgInfoTag* GetEPGInfoTag();
257 inline const EPG::CEpgInfoTag* GetEPGInfoTag() const
262 inline bool HasPVRChannelInfoTag() const
264 return m_pvrChannelInfoTag != NULL;
267 PVR::CPVRChannel* GetPVRChannelInfoTag();
269 inline const PVR::CPVRChannel* GetPVRChannelInfoTag() const
271 return m_pvrChannelInfoTag;
274 inline bool HasPVRRecordingInfoTag() const
276 return m_pvrRecordingInfoTag != NULL;
279 PVR::CPVRRecording* GetPVRRecordingInfoTag();
281 inline const PVR::CPVRRecording* GetPVRRecordingInfoTag() const
283 return m_pvrRecordingInfoTag;
286 inline bool HasPVRTimerInfoTag() const
288 return m_pvrTimerInfoTag != NULL;
291 PVR::CPVRTimerInfoTag* GetPVRTimerInfoTag();
293 inline const PVR::CPVRTimerInfoTag* GetPVRTimerInfoTag() const
295 return m_pvrTimerInfoTag;
298 inline bool HasPictureInfoTag() const
300 return m_pictureInfoTag != NULL;
303 inline const CPictureInfoTag* GetPictureInfoTag() const
305 return m_pictureInfoTag;
308 CPictureInfoTag* GetPictureInfoTag();
311 \brief Get the local fanart for this item if it exists
312 \return path to the local fanart for this item, or empty if none exists
313 \sa GetFolderThumb, GetTBNFile
315 CStdString GetLocalFanart() const;
317 /*! \brief Assemble the filename of a particular piece of local artwork for an item.
318 No file existence check is typically performed.
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.
324 CStdString GetLocalArt(const std::string &artFile, bool useFolder = false) const;
326 /*! \brief Assemble the filename of a particular piece of local artwork for an item,
327 and check for file existence.
328 \param artFile the art file to search for.
329 \param useFolder whether to look in the folder for the art file. Defaults to false.
330 \return the path to the local artwork if it exists, empty otherwise.
333 CStdString FindLocalArt(const std::string &artFile, bool useFolder) const;
335 /*! \brief Whether or not to skip searching for local art.
336 \return true if local art should be skipped for this item, false otherwise.
337 \sa GetLocalArt, FindLocalArt
339 bool SkipLocalArt() const;
341 // Gets the .tbn file associated with this item
342 CStdString GetTBNFile() const;
343 // Gets the folder image associated with this item (defaults to folder.jpg)
344 CStdString GetFolderThumb(const CStdString &folderJPG = "folder.jpg") const;
345 // Gets the correct movie title
346 CStdString GetMovieName(bool bUseFolderNames = false) const;
348 /*! \brief Find the base movie path (i.e. the item the user expects us to use to lookup the movie)
349 For folder items, with "use foldernames for lookups" it returns the folder.
350 Regardless of settings, for VIDEO_TS/BDMV it returns the parent of the VIDEO_TS/BDMV folder (if present)
352 \param useFolderNames whether we're using foldernames for lookups
353 \return the base movie folder
355 CStdString GetBaseMoviePath(bool useFolderNames) const;
357 // Gets the user thumb, if it exists
358 CStdString GetUserMusicThumb(bool alwaysCheckRemote = false, bool fallbackToFolder = false) const;
360 /*! \brief Get the path where we expect local metadata to reside.
361 For a folder, this is just the existing path (eg tvshow folder)
362 For a file, this is the parent path, with exceptions made for VIDEO_TS and BDMV files
364 Three cases are handled:
366 /foo/bar/movie_name/file_name -> /foo/bar/movie_name/
367 /foo/bar/movie_name/VIDEO_TS/file_name -> /foo/bar/movie_name/
368 /foo/bar/movie_name/BDMV/file_name -> /foo/bar/movie_name/
370 \sa URIUtils::GetParentPath
372 CStdString GetLocalMetadataPath() const;
374 // finds a matching local trailer file
375 CStdString FindTrailer() const;
377 virtual bool LoadMusicTag();
379 /* Returns the content type of this item if known */
380 const CStdString& GetMimeType() const { return m_mimetype; }
382 /* sets the mime-type if known beforehand */
383 void SetMimeType(const CStdString& mimetype) { m_mimetype = mimetype; } ;
385 /*! \brief Resolve the MIME type based on file extension or a web lookup
386 If m_mimetype is already set (non-empty), this function has no effect. For
387 http:// and shout:// streams, this will query the stream (blocking operation).
388 Set lookup=false to skip any internet lookups and always return immediately.
390 void FillInMimeType(bool lookup = true);
392 /* general extra info about the contents of the item, not for display */
393 void SetExtraInfo(const CStdString& info) { m_extrainfo = info; };
394 const CStdString& GetExtraInfo() const { return m_extrainfo; };
396 /*! \brief Update an item with information from another item
397 We take metadata information from the given item and supplement the current item
398 with that info. If tags exist in the new item we use the entire tag information.
399 Properties are appended, and labels, thumbnail and icon are updated if non-empty
401 \param item the item used to supplement information
402 \param replaceLabels whether to replace labels (defaults to true)
404 void UpdateInfo(const CFileItem &item, bool replaceLabels = true);
406 bool IsSamePath(const CFileItem *item) const;
408 bool IsAlbum() const;
410 /*! \brief Sets details using the information from the CVideoInfoTag object
411 Sets the videoinfotag and uses its information to set the label and path.
412 \param video video details to use and set
414 void SetFromVideoInfoTag(const CVideoInfoTag &video);
415 /*! \brief Sets details using the information from the CAlbum object
416 Sets the album in the music info tag and uses its information to set the
417 label and album-specific properties.
418 \param album album details to use and set
420 void SetFromAlbum(const CAlbum &album);
421 /*! \brief Sets details using the information from the CSong object
422 Sets the song in the music info tag and uses its information to set the
423 label, path, song-specific properties and artwork.
424 \param song song details to use and set
426 void SetFromSong(const CSong &song);
428 bool m_bIsShareOrDrive; ///< is this a root share/drive
429 int m_iDriveType; ///< If \e m_bIsShareOrDrive is \e true, use to get the share type. Types see: CMediaSource::m_iDriveType
430 CDateTime m_dateTime; ///< file creation date & time
431 int64_t m_dwSize; ///< file size (0 for folders)
432 CStdString m_strDVDLabel;
433 CStdString m_strTitle;
437 int m_lStartPartNumber;
439 LockType m_iLockMode;
440 CStdString m_strLockCode;
441 int m_iHasLock; // 0 - no lock 1 - lock, but unlocked 2 - locked
445 CStdString m_strPath; ///< complete path to item
447 SortSpecial m_specialSort;
448 bool m_bIsParentFolder;
450 bool m_bLabelPreformated;
451 CStdString m_mimetype;
452 CStdString m_extrainfo;
453 MUSIC_INFO::CMusicInfoTag* m_musicInfoTag;
454 CVideoInfoTag* m_videoInfoTag;
455 EPG::CEpgInfoTag* m_epgInfoTag;
456 PVR::CPVRChannel* m_pvrChannelInfoTag;
457 PVR::CPVRRecording* m_pvrRecordingInfoTag;
458 PVR::CPVRTimerInfoTag * m_pvrTimerInfoTag;
459 CPictureInfoTag* m_pictureInfoTag;
464 \brief A shared pointer to CFileItem
467 typedef boost::shared_ptr<CFileItem> CFileItemPtr;
470 \brief A vector of pointer to CFileItem
473 typedef std::vector< CFileItemPtr > VECFILEITEMS;
476 \brief Iterator for VECFILEITEMS
479 typedef std::vector< CFileItemPtr >::iterator IVECFILEITEMS;
482 \brief A map of pointers to CFileItem
485 typedef std::map<CStdString, CFileItemPtr > MAPFILEITEMS;
488 \brief Iterator for MAPFILEITEMS
491 typedef std::map<CStdString, CFileItemPtr >::iterator IMAPFILEITEMS;
494 \brief Pair for MAPFILEITEMS
497 typedef std::pair<CStdString, CFileItemPtr > MAPFILEITEMSPAIR;
499 typedef bool (*FILEITEMLISTCOMPARISONFUNC) (const CFileItemPtr &pItem1, const CFileItemPtr &pItem2);
500 typedef void (*FILEITEMFILLFUNC) (CFileItemPtr &item);
503 \brief Represents a list of files
504 \sa CFileItemList, CFileItem
506 class CFileItemList : public CFileItem
509 enum CACHE_TYPE { CACHE_NEVER = 0, CACHE_IF_SLOW, CACHE_ALWAYS };
512 CFileItemList(const CStdString& strPath);
513 virtual ~CFileItemList();
514 virtual void Archive(CArchive& ar);
515 CFileItemPtr operator[] (int iItem);
516 const CFileItemPtr operator[] (int iItem) const;
517 CFileItemPtr operator[] (const CStdString& strPath);
518 const CFileItemPtr operator[] (const CStdString& strPath) const;
521 void Add(const CFileItemPtr &pItem);
522 void AddFront(const CFileItemPtr &pItem, int itemPosition);
523 void Remove(CFileItem* pItem);
524 void Remove(int iItem);
525 CFileItemPtr Get(int iItem);
526 const CFileItemPtr Get(int iItem) const;
527 const VECFILEITEMS GetList() const { return m_items; }
528 CFileItemPtr Get(const CStdString& strPath);
529 const CFileItemPtr Get(const CStdString& strPath) const;
531 bool IsEmpty() const;
532 void Append(const CFileItemList& itemlist);
533 void Assign(const CFileItemList& itemlist, bool append = false);
534 bool Copy (const CFileItemList& item, bool copyItems = true);
535 void Reserve(int iCount);
536 void Sort(SortBy sortBy, SortOrder sortOrder, SortAttribute sortAttributes = SortAttributeNone);
537 /* \brief Sorts the items based on the given sorting options
539 In contrast to Sort (see above) this does not change the internal
540 state by storing the sorting method and order used and therefore
541 will always execute the sorting even if the list of items has
542 already been sorted with the same options before.
544 void Sort(SortDescription sortDescription);
546 void FillInDefaultIcons();
547 int GetFolderCount() const;
548 int GetFileCount() const;
549 int GetSelectedCount() const;
550 int GetObjectCount() const;
551 void FilterCueItems();
552 void RemoveExtensions();
553 void SetFastLookup(bool fastLookup);
554 bool Contains(const CStdString& fileName) const;
555 bool GetFastLookup() const { return m_fastLookup; };
557 /*! \brief stack a CFileItemList
558 By default we stack all items (files and folders) in a CFileItemList
559 \param stackFiles whether to stack all items or just collapse folders (defaults to true)
560 \sa StackFiles,StackFolders
562 void Stack(bool stackFiles = true);
564 SortOrder GetSortOrder() const { return m_sortDescription.sortOrder; }
565 SortBy GetSortMethod() const { return m_sortDescription.sortBy; }
566 /*! \brief load a CFileItemList out of the cache
568 The file list may be cached based on which window we're viewing in, as different
569 windows will be listing different portions of the same URL (eg viewing music files
570 versus viewing video files)
572 \param windowID id of the window that's loading this list (defaults to 0)
573 \return true if we loaded from the cache, false otherwise.
574 \sa Save,RemoveDiscCache
576 bool Load(int windowID = 0);
578 /*! \brief save a CFileItemList to the cache
580 The file list may be cached based on which window we're viewing in, as different
581 windows will be listing different portions of the same URL (eg viewing music files
582 versus viewing video files)
584 \param windowID id of the window that's saving this list (defaults to 0)
585 \return true if successful, false otherwise.
586 \sa Load,RemoveDiscCache
588 bool Save(int windowID = 0);
589 void SetCacheToDisc(CACHE_TYPE cacheToDisc) { m_cacheToDisc = cacheToDisc; }
590 bool CacheToDiscAlways() const { return m_cacheToDisc == CACHE_ALWAYS; }
591 bool CacheToDiscIfSlow() const { return m_cacheToDisc == CACHE_IF_SLOW; }
592 /*! \brief remove a previously cached CFileItemList from the cache
594 The file list may be cached based on which window we're viewing in, as different
595 windows will be listing different portions of the same URL (eg viewing music files
596 versus viewing video files)
598 \param windowID id of the window whose cache we which to remove (defaults to 0)
601 void RemoveDiscCache(int windowID = 0) const;
602 bool AlwaysCache() const;
604 void Swap(unsigned int item1, unsigned int item2);
606 /*! \brief Update an item in the item list
607 \param item the new item, which we match based on path to an existing item in the list
608 \return true if the item exists in the list (and was thus updated), false otherwise.
610 bool UpdateItem(const CFileItem *item);
612 void AddSortMethod(SortBy sortBy, int buttonLabel, const LABEL_MASKS &labelMasks, SortAttribute sortAttributes = SortAttributeNone);
613 void AddSortMethod(SortBy sortBy, SortAttribute sortAttributes, int buttonLabel, const LABEL_MASKS &labelMasks);
614 void AddSortMethod(SortDescription sortDescription, int buttonLabel, const LABEL_MASKS &labelMasks);
615 bool HasSortDetails() const { return m_sortDetails.size() != 0; };
616 const std::vector<SORT_METHOD_DETAILS> &GetSortDetails() const { return m_sortDetails; };
618 /*! \brief Specify whether this list should be sorted with folders separate from files
619 By default we sort with folders listed (and sorted separately) except for those sort modes
620 which should be explicitly sorted with folders interleaved with files (eg SORT_METHOD_FILES).
621 With this set the folder state will be ignored, allowing folders and files to sort interleaved.
622 \param sort whether to ignore the folder state.
624 void SetSortIgnoreFolders(bool sort) { m_sortIgnoreFolders = sort; };
625 bool GetReplaceListing() const { return m_replaceListing; };
626 void SetReplaceListing(bool replace);
627 void SetContent(const CStdString &content) { m_content = content; };
628 const CStdString &GetContent() const { return m_content; };
630 void ClearSortState();
632 void Sort(FILEITEMLISTCOMPARISONFUNC func);
633 void FillSortFields(FILEITEMFILLFUNC func);
634 CStdString GetDiscFileCache(int windowID) const;
637 \brief stack files in a CFileItemList
643 \brief stack folders in a CFileItemList
648 VECFILEITEMS m_items;
651 SortDescription m_sortDescription;
652 bool m_sortIgnoreFolders;
653 CACHE_TYPE m_cacheToDisc;
654 bool m_replaceListing;
655 CStdString m_content;
657 std::vector<SORT_METHOD_DETAILS> m_sortDetails;
659 CCriticalSection m_lock;