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 a video item. Note that this returns true for
124 anything with a video info tag, so that may include eg. folders.
125 \return true if item is video, false otherwise.
127 bool IsVideo() const;
129 bool IsDiscStub() const;
132 \brief Check whether an item is a picture item. Note that this returns true for
133 anything with a picture info tag, so that may include eg. folders.
134 \return true if item is picture, false otherwise.
136 bool IsPicture() const;
137 bool IsLyrics() const;
140 \brief Check whether an item is an audio item. Note that this returns true for
141 anything with a music info tag, so that may include eg. folders.
142 \return true if item is audio, false otherwise.
144 bool IsAudio() const;
146 bool IsKaraoke() const;
147 bool IsCUESheet() const;
148 bool IsInternetStream(const bool bStrictCheck = false) const;
149 bool IsPlayList() const;
150 bool IsSmartPlayList() const;
151 bool IsLibraryFolder() const;
152 bool IsPythonScript() const;
153 bool IsPlugin() const;
154 bool IsScript() const;
155 bool IsAddonsPath() const;
156 bool IsSourcesPath() const;
158 bool IsDVDImage() const;
159 bool IsOpticalMediaFile() const;
160 bool IsDVDFile(bool bVobs = true, bool bIfos = true) const;
161 bool IsBDFile() const;
167 bool IsISO9660() const;
170 bool IsOnDVD() const;
171 bool IsOnLAN() const;
175 bool IsRemote() const;
179 bool IsStack() const;
180 bool IsMultiPath() const;
181 bool IsMusicDb() const;
182 bool IsVideoDb() const;
184 bool IsPVRChannel() const;
185 bool IsPVRRecording() const;
186 bool IsPVRTimer() const;
187 bool IsType(const char *ext) const;
188 bool IsVirtualDirectoryRoot() const;
189 bool IsReadOnly() const;
190 bool CanQueue() const;
191 void SetCanQueue(bool bYesNo);
192 bool IsParentFolder() const;
193 bool IsFileFolder(EFileFolderType types = EFILEFOLDER_MASK_ALL) const;
194 bool IsRemovable() const;
195 bool IsTuxBox() const;
196 bool IsMythTV() const;
197 bool IsHDHomeRun() const;
198 bool IsSlingbox() const;
201 bool IsLiveTV() const;
203 bool IsAndroidApp() const;
205 void RemoveExtension();
207 void FillInDefaultIcon();
208 void SetFileSizeLabel();
209 virtual void SetLabel(const CStdString &strLabel);
210 CURL GetAsUrl() const;
211 int GetVideoContentType() const; /* return VIDEODB_CONTENT_TYPE, but don't want to include videodb in this header */
212 bool IsLabelPreformated() const { return m_bLabelPreformated; }
213 void SetLabelPreformated(bool bYesNo) { m_bLabelPreformated=bYesNo; }
214 bool SortsOnTop() const { return m_specialSort == SortSpecialOnTop; }
215 bool SortsOnBottom() const { return m_specialSort == SortSpecialOnBottom; }
216 void SetSpecialSort(SortSpecial sort) { m_specialSort = sort; }
218 inline bool HasMusicInfoTag() const
220 return m_musicInfoTag != NULL;
223 MUSIC_INFO::CMusicInfoTag* GetMusicInfoTag();
225 inline const MUSIC_INFO::CMusicInfoTag* GetMusicInfoTag() const
227 return m_musicInfoTag;
230 inline bool HasVideoInfoTag() const
232 return m_videoInfoTag != NULL;
235 CVideoInfoTag* GetVideoInfoTag();
237 inline const CVideoInfoTag* GetVideoInfoTag() const
239 return m_videoInfoTag;
242 inline bool HasEPGInfoTag() const
244 return m_epgInfoTag != NULL;
247 EPG::CEpgInfoTag* GetEPGInfoTag();
249 inline const EPG::CEpgInfoTag* GetEPGInfoTag() const
254 inline bool HasPVRChannelInfoTag() const
256 return m_pvrChannelInfoTag != NULL;
259 PVR::CPVRChannel* GetPVRChannelInfoTag();
261 inline const PVR::CPVRChannel* GetPVRChannelInfoTag() const
263 return m_pvrChannelInfoTag;
266 inline bool HasPVRRecordingInfoTag() const
268 return m_pvrRecordingInfoTag != NULL;
271 PVR::CPVRRecording* GetPVRRecordingInfoTag();
273 inline const PVR::CPVRRecording* GetPVRRecordingInfoTag() const
275 return m_pvrRecordingInfoTag;
278 inline bool HasPVRTimerInfoTag() const
280 return m_pvrTimerInfoTag != NULL;
283 PVR::CPVRTimerInfoTag* GetPVRTimerInfoTag();
285 inline const PVR::CPVRTimerInfoTag* GetPVRTimerInfoTag() const
287 return m_pvrTimerInfoTag;
290 inline bool HasPictureInfoTag() const
292 return m_pictureInfoTag != NULL;
295 inline const CPictureInfoTag* GetPictureInfoTag() const
297 return m_pictureInfoTag;
300 CPictureInfoTag* GetPictureInfoTag();
303 \brief Get the local fanart for this item if it exists
304 \return path to the local fanart for this item, or empty if none exists
305 \sa GetFolderThumb, GetTBNFile
307 CStdString GetLocalFanart() const;
309 /*! \brief Assemble the filename of a particular piece of local artwork for an item.
310 No file existence check is typically performed.
311 \param artFile the art file to search for.
312 \param useFolder whether to look in the folder for the art file. Defaults to false.
313 \return the path to the local artwork.
316 CStdString GetLocalArt(const std::string &artFile, bool useFolder = false) const;
318 /*! \brief Assemble the filename of a particular piece of local artwork for an item,
319 and check for file existence.
320 \param artFile the art file to search for.
321 \param useFolder whether to look in the folder for the art file. Defaults to false.
322 \return the path to the local artwork if it exists, empty otherwise.
325 CStdString FindLocalArt(const std::string &artFile, bool useFolder) const;
327 // Gets the .tbn file associated with this item
328 CStdString GetTBNFile() const;
329 // Gets the folder image associated with this item (defaults to folder.jpg)
330 CStdString GetFolderThumb(const CStdString &folderJPG = "folder.jpg") const;
331 // Gets the correct movie title
332 CStdString GetMovieName(bool bUseFolderNames = false) const;
334 /*! \brief Find the base movie path (i.e. the item the user expects us to use to lookup the movie)
335 For folder items, with "use foldernames for lookups" it returns the folder.
336 Regardless of settings, for VIDEO_TS/BDMV it returns the parent of the VIDEO_TS/BDMV folder (if present)
338 \param useFolderNames whether we're using foldernames for lookups
339 \return the base movie folder
341 CStdString GetBaseMoviePath(bool useFolderNames) const;
343 // Gets the user thumb, if it exists
344 CStdString GetUserMusicThumb(bool alwaysCheckRemote = false, bool fallbackToFolder = false) const;
346 /*! \brief Get the path where we expect local metadata to reside.
347 For a folder, this is just the existing path (eg tvshow folder)
348 For a file, this is the parent path, with exceptions made for VIDEO_TS and BDMV files
350 Three cases are handled:
352 /foo/bar/movie_name/file_name -> /foo/bar/movie_name/
353 /foo/bar/movie_name/VIDEO_TS/file_name -> /foo/bar/movie_name/
354 /foo/bar/movie_name/BDMV/file_name -> /foo/bar/movie_name/
356 \sa URIUtils::GetParentPath
358 CStdString GetLocalMetadataPath() const;
360 // finds a matching local trailer file
361 CStdString FindTrailer() const;
363 virtual bool LoadMusicTag();
365 /* Returns the content type of this item if known */
366 const CStdString& GetMimeType() const { return m_mimetype; }
368 /* sets the mime-type if known beforehand */
369 void SetMimeType(const CStdString& mimetype) { m_mimetype = mimetype; } ;
371 /*! \brief Resolve the MIME type based on file extension or a web lookup
372 If m_mimetype is already set (non-empty), this function has no effect. For
373 http:// and shout:// streams, this will query the stream (blocking operation).
374 Set lookup=false to skip any internet lookups and always return immediately.
376 void FillInMimeType(bool lookup = true);
378 /* general extra info about the contents of the item, not for display */
379 void SetExtraInfo(const CStdString& info) { m_extrainfo = info; };
380 const CStdString& GetExtraInfo() const { return m_extrainfo; };
382 /*! \brief Update an item with information from another item
383 We take metadata information from the given item and supplement the current item
384 with that info. If tags exist in the new item we use the entire tag information.
385 Properties are appended, and labels, thumbnail and icon are updated if non-empty
387 \param item the item used to supplement information
388 \param replaceLabels whether to replace labels (defaults to true)
390 void UpdateInfo(const CFileItem &item, bool replaceLabels = true);
392 bool IsSamePath(const CFileItem *item) const;
394 bool IsAlbum() const;
396 /*! \brief Sets details using the information from the CVideoInfoTag object
397 Sets the videoinfotag and uses its information to set the label and path.
398 \param video video details to use and set
400 void SetFromVideoInfoTag(const CVideoInfoTag &video);
401 /*! \brief Sets details using the information from the CAlbum object
402 Sets the album in the music info tag and uses its information to set the
403 label and album-specific properties.
404 \param album album details to use and set
406 void SetFromAlbum(const CAlbum &album);
407 /*! \brief Sets details using the information from the CSong object
408 Sets the song in the music info tag and uses its information to set the
409 label, path, song-specific properties and artwork.
410 \param song song details to use and set
412 void SetFromSong(const CSong &song);
414 bool m_bIsShareOrDrive; ///< is this a root share/drive
415 int m_iDriveType; ///< If \e m_bIsShareOrDrive is \e true, use to get the share type. Types see: CMediaSource::m_iDriveType
416 CDateTime m_dateTime; ///< file creation date & time
417 int64_t m_dwSize; ///< file size (0 for folders)
418 CStdString m_strDVDLabel;
419 CStdString m_strTitle;
423 int m_lStartPartNumber;
425 LockType m_iLockMode;
426 CStdString m_strLockCode;
427 int m_iHasLock; // 0 - no lock 1 - lock, but unlocked 2 - locked
431 CStdString m_strPath; ///< complete path to item
433 SortSpecial m_specialSort;
434 bool m_bIsParentFolder;
436 bool m_bLabelPreformated;
437 CStdString m_mimetype;
438 CStdString m_extrainfo;
439 MUSIC_INFO::CMusicInfoTag* m_musicInfoTag;
440 CVideoInfoTag* m_videoInfoTag;
441 EPG::CEpgInfoTag* m_epgInfoTag;
442 PVR::CPVRChannel* m_pvrChannelInfoTag;
443 PVR::CPVRRecording* m_pvrRecordingInfoTag;
444 PVR::CPVRTimerInfoTag * m_pvrTimerInfoTag;
445 CPictureInfoTag* m_pictureInfoTag;
450 \brief A shared pointer to CFileItem
453 typedef boost::shared_ptr<CFileItem> CFileItemPtr;
456 \brief A vector of pointer to CFileItem
459 typedef std::vector< CFileItemPtr > VECFILEITEMS;
462 \brief Iterator for VECFILEITEMS
465 typedef std::vector< CFileItemPtr >::iterator IVECFILEITEMS;
468 \brief A map of pointers to CFileItem
471 typedef std::map<CStdString, CFileItemPtr > MAPFILEITEMS;
474 \brief Iterator for MAPFILEITEMS
477 typedef std::map<CStdString, CFileItemPtr >::iterator IMAPFILEITEMS;
480 \brief Pair for MAPFILEITEMS
483 typedef std::pair<CStdString, CFileItemPtr > MAPFILEITEMSPAIR;
485 typedef bool (*FILEITEMLISTCOMPARISONFUNC) (const CFileItemPtr &pItem1, const CFileItemPtr &pItem2);
486 typedef void (*FILEITEMFILLFUNC) (CFileItemPtr &item);
489 \brief Represents a list of files
490 \sa CFileItemList, CFileItem
492 class CFileItemList : public CFileItem
495 enum CACHE_TYPE { CACHE_NEVER = 0, CACHE_IF_SLOW, CACHE_ALWAYS };
498 CFileItemList(const CStdString& strPath);
499 virtual ~CFileItemList();
500 virtual void Archive(CArchive& ar);
501 CFileItemPtr operator[] (int iItem);
502 const CFileItemPtr operator[] (int iItem) const;
503 CFileItemPtr operator[] (const CStdString& strPath);
504 const CFileItemPtr operator[] (const CStdString& strPath) const;
507 void Add(const CFileItemPtr &pItem);
508 void AddFront(const CFileItemPtr &pItem, int itemPosition);
509 void Remove(CFileItem* pItem);
510 void Remove(int iItem);
511 CFileItemPtr Get(int iItem);
512 const CFileItemPtr Get(int iItem) const;
513 const VECFILEITEMS GetList() const { return m_items; }
514 CFileItemPtr Get(const CStdString& strPath);
515 const CFileItemPtr Get(const CStdString& strPath) const;
517 bool IsEmpty() const;
518 void Append(const CFileItemList& itemlist);
519 void Assign(const CFileItemList& itemlist, bool append = false);
520 bool Copy (const CFileItemList& item, bool copyItems = true);
521 void Reserve(int iCount);
522 void Sort(SortBy sortBy, SortOrder sortOrder, SortAttribute sortAttributes = SortAttributeNone);
523 /* \brief Sorts the items based on the given sorting options
525 In contrast to Sort (see above) this does not change the internal
526 state by storing the sorting method and order used and therefore
527 will always execute the sorting even if the list of items has
528 already been sorted with the same options before.
530 void Sort(SortDescription sortDescription);
532 void FillInDefaultIcons();
533 int GetFolderCount() const;
534 int GetFileCount() const;
535 int GetSelectedCount() const;
536 int GetObjectCount() const;
537 void FilterCueItems();
538 void RemoveExtensions();
539 void SetFastLookup(bool fastLookup);
540 bool Contains(const CStdString& fileName) const;
541 bool GetFastLookup() const { return m_fastLookup; };
543 /*! \brief stack a CFileItemList
544 By default we stack all items (files and folders) in a CFileItemList
545 \param stackFiles whether to stack all items or just collapse folders (defaults to true)
546 \sa StackFiles,StackFolders
548 void Stack(bool stackFiles = true);
550 SortOrder GetSortOrder() const { return m_sortDescription.sortOrder; }
551 SortBy GetSortMethod() const { return m_sortDescription.sortBy; }
552 /*! \brief load a CFileItemList out of the cache
554 The file list may be cached based on which window we're viewing in, as different
555 windows will be listing different portions of the same URL (eg viewing music files
556 versus viewing video files)
558 \param windowID id of the window that's loading this list (defaults to 0)
559 \return true if we loaded from the cache, false otherwise.
560 \sa Save,RemoveDiscCache
562 bool Load(int windowID = 0);
564 /*! \brief save a CFileItemList to the cache
566 The file list may be cached based on which window we're viewing in, as different
567 windows will be listing different portions of the same URL (eg viewing music files
568 versus viewing video files)
570 \param windowID id of the window that's saving this list (defaults to 0)
571 \return true if successful, false otherwise.
572 \sa Load,RemoveDiscCache
574 bool Save(int windowID = 0);
575 void SetCacheToDisc(CACHE_TYPE cacheToDisc) { m_cacheToDisc = cacheToDisc; }
576 bool CacheToDiscAlways() const { return m_cacheToDisc == CACHE_ALWAYS; }
577 bool CacheToDiscIfSlow() const { return m_cacheToDisc == CACHE_IF_SLOW; }
578 /*! \brief remove a previously cached CFileItemList from 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 whose cache we which to remove (defaults to 0)
587 void RemoveDiscCache(int windowID = 0) const;
588 bool AlwaysCache() const;
590 void Swap(unsigned int item1, unsigned int item2);
592 /*! \brief Update an item in the item list
593 \param item the new item, which we match based on path to an existing item in the list
594 \return true if the item exists in the list (and was thus updated), false otherwise.
596 bool UpdateItem(const CFileItem *item);
598 void AddSortMethod(SortBy sortBy, int buttonLabel, const LABEL_MASKS &labelMasks, SortAttribute sortAttributes = SortAttributeNone);
599 void AddSortMethod(SortBy sortBy, SortAttribute sortAttributes, int buttonLabel, const LABEL_MASKS &labelMasks);
600 void AddSortMethod(SortDescription sortDescription, int buttonLabel, const LABEL_MASKS &labelMasks);
601 bool HasSortDetails() const { return m_sortDetails.size() != 0; };
602 const std::vector<SORT_METHOD_DETAILS> &GetSortDetails() const { return m_sortDetails; };
604 /*! \brief Specify whether this list should be sorted with folders separate from files
605 By default we sort with folders listed (and sorted separately) except for those sort modes
606 which should be explicitly sorted with folders interleaved with files (eg SORT_METHOD_FILES).
607 With this set the folder state will be ignored, allowing folders and files to sort interleaved.
608 \param sort whether to ignore the folder state.
610 void SetSortIgnoreFolders(bool sort) { m_sortIgnoreFolders = sort; };
611 bool GetReplaceListing() const { return m_replaceListing; };
612 void SetReplaceListing(bool replace);
613 void SetContent(const CStdString &content) { m_content = content; };
614 const CStdString &GetContent() const { return m_content; };
616 void ClearSortState();
618 void Sort(FILEITEMLISTCOMPARISONFUNC func);
619 void FillSortFields(FILEITEMFILLFUNC func);
620 CStdString GetDiscFileCache(int windowID) const;
623 \brief stack files in a CFileItemList
629 \brief stack folders in a CFileItemList
634 VECFILEITEMS m_items;
637 SortDescription m_sortDescription;
638 bool m_sortIgnoreFolders;
639 CACHE_TYPE m_cacheToDisc;
640 bool m_replaceListing;
641 CStdString m_content;
643 std::vector<SORT_METHOD_DETAILS> m_sortDetails;
645 CCriticalSection m_lock;