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 IsPythonScript() const;
151 bool IsPlugin() const;
152 bool IsScript() const;
153 bool IsAddonsPath() const;
154 bool IsSourcesPath() const;
156 bool IsDVDImage() const;
157 bool IsOpticalMediaFile() const;
158 bool IsDVDFile(bool bVobs = true, bool bIfos = true) const;
159 bool IsBDFile() const;
165 bool IsISO9660() const;
168 bool IsOnDVD() const;
169 bool IsOnLAN() const;
173 bool IsRemote() const;
177 bool IsStack() const;
178 bool IsMultiPath() const;
179 bool IsMusicDb() const;
180 bool IsVideoDb() const;
182 bool IsPVRChannel() const;
183 bool IsPVRRecording() const;
184 bool IsPVRTimer() const;
185 bool IsType(const char *ext) const;
186 bool IsVirtualDirectoryRoot() const;
187 bool IsReadOnly() const;
188 bool CanQueue() const;
189 void SetCanQueue(bool bYesNo);
190 bool IsParentFolder() const;
191 bool IsFileFolder(EFileFolderType types = EFILEFOLDER_MASK_ALL) const;
192 bool IsRemovable() const;
193 bool IsTuxBox() const;
194 bool IsMythTV() const;
195 bool IsHDHomeRun() const;
196 bool IsSlingbox() const;
199 bool IsLiveTV() const;
201 bool IsAndroidApp() const;
203 void RemoveExtension();
205 void FillInDefaultIcon();
206 void SetFileSizeLabel();
207 virtual void SetLabel(const CStdString &strLabel);
208 CURL GetAsUrl() const;
209 int GetVideoContentType() const; /* return VIDEODB_CONTENT_TYPE, but don't want to include videodb in this header */
210 bool IsLabelPreformated() const { return m_bLabelPreformated; }
211 void SetLabelPreformated(bool bYesNo) { m_bLabelPreformated=bYesNo; }
212 bool SortsOnTop() const { return m_specialSort == SortSpecialOnTop; }
213 bool SortsOnBottom() const { return m_specialSort == SortSpecialOnBottom; }
214 void SetSpecialSort(SortSpecial sort) { m_specialSort = sort; }
216 inline bool HasMusicInfoTag() const
218 return m_musicInfoTag != NULL;
221 MUSIC_INFO::CMusicInfoTag* GetMusicInfoTag();
223 inline const MUSIC_INFO::CMusicInfoTag* GetMusicInfoTag() const
225 return m_musicInfoTag;
228 inline bool HasVideoInfoTag() const
230 return m_videoInfoTag != NULL;
233 CVideoInfoTag* GetVideoInfoTag();
235 inline const CVideoInfoTag* GetVideoInfoTag() const
237 return m_videoInfoTag;
240 inline bool HasEPGInfoTag() const
242 return m_epgInfoTag != NULL;
245 EPG::CEpgInfoTag* GetEPGInfoTag();
247 inline const EPG::CEpgInfoTag* GetEPGInfoTag() const
252 inline bool HasPVRChannelInfoTag() const
254 return m_pvrChannelInfoTag != NULL;
257 PVR::CPVRChannel* GetPVRChannelInfoTag();
259 inline const PVR::CPVRChannel* GetPVRChannelInfoTag() const
261 return m_pvrChannelInfoTag;
264 inline bool HasPVRRecordingInfoTag() const
266 return m_pvrRecordingInfoTag != NULL;
269 PVR::CPVRRecording* GetPVRRecordingInfoTag();
271 inline const PVR::CPVRRecording* GetPVRRecordingInfoTag() const
273 return m_pvrRecordingInfoTag;
276 inline bool HasPVRTimerInfoTag() const
278 return m_pvrTimerInfoTag != NULL;
281 PVR::CPVRTimerInfoTag* GetPVRTimerInfoTag();
283 inline const PVR::CPVRTimerInfoTag* GetPVRTimerInfoTag() const
285 return m_pvrTimerInfoTag;
288 inline bool HasPictureInfoTag() const
290 return m_pictureInfoTag != NULL;
293 inline const CPictureInfoTag* GetPictureInfoTag() const
295 return m_pictureInfoTag;
298 CPictureInfoTag* GetPictureInfoTag();
301 \brief Get the local fanart for this item if it exists
302 \return path to the local fanart for this item, or empty if none exists
303 \sa GetFolderThumb, GetTBNFile
305 CStdString GetLocalFanart() const;
307 /*! \brief Assemble the filename of a particular piece of local artwork for an item.
308 No file existence check is typically performed.
309 \param artFile the art file to search for.
310 \param useFolder whether to look in the folder for the art file. Defaults to false.
311 \return the path to the local artwork.
314 CStdString GetLocalArt(const std::string &artFile, bool useFolder = false) const;
316 /*! \brief Assemble the filename of a particular piece of local artwork for an item,
317 and check for file existence.
318 \param artFile the art file to search for.
319 \param useFolder whether to look in the folder for the art file. Defaults to false.
320 \return the path to the local artwork if it exists, empty otherwise.
323 CStdString FindLocalArt(const std::string &artFile, bool useFolder) const;
325 // Gets the .tbn file associated with this item
326 CStdString GetTBNFile() const;
327 // Gets the folder image associated with this item (defaults to folder.jpg)
328 CStdString GetFolderThumb(const CStdString &folderJPG = "folder.jpg") const;
329 // Gets the correct movie title
330 CStdString GetMovieName(bool bUseFolderNames = false) const;
332 /*! \brief Find the base movie path (i.e. the item the user expects us to use to lookup the movie)
333 For folder items, with "use foldernames for lookups" it returns the folder.
334 Regardless of settings, for VIDEO_TS/BDMV it returns the parent of the VIDEO_TS/BDMV folder (if present)
336 \param useFolderNames whether we're using foldernames for lookups
337 \return the base movie folder
339 CStdString GetBaseMoviePath(bool useFolderNames) const;
341 // Gets the user thumb, if it exists
342 CStdString GetUserMusicThumb(bool alwaysCheckRemote = false, bool fallbackToFolder = false) const;
344 /*! \brief Get the path where we expect local metadata to reside.
345 For a folder, this is just the existing path (eg tvshow folder)
346 For a file, this is the parent path, with exceptions made for VIDEO_TS and BDMV files
348 Three cases are handled:
350 /foo/bar/movie_name/file_name -> /foo/bar/movie_name/
351 /foo/bar/movie_name/VIDEO_TS/file_name -> /foo/bar/movie_name/
352 /foo/bar/movie_name/BDMV/file_name -> /foo/bar/movie_name/
354 \sa URIUtils::GetParentPath
356 CStdString GetLocalMetadataPath() const;
358 // finds a matching local trailer file
359 CStdString FindTrailer() const;
361 virtual bool LoadMusicTag();
363 /* Returns the content type of this item if known */
364 const CStdString& GetMimeType() const { return m_mimetype; }
366 /* sets the mime-type if known beforehand */
367 void SetMimeType(const CStdString& mimetype) { m_mimetype = mimetype; } ;
369 /*! \brief Resolve the MIME type based on file extension or a web lookup
370 If m_mimetype is already set (non-empty), this function has no effect. For
371 http:// and shout:// streams, this will query the stream (blocking operation).
372 Set lookup=false to skip any internet lookups and always return immediately.
374 void FillInMimeType(bool lookup = true);
376 /* general extra info about the contents of the item, not for display */
377 void SetExtraInfo(const CStdString& info) { m_extrainfo = info; };
378 const CStdString& GetExtraInfo() const { return m_extrainfo; };
380 /*! \brief Update an item with information from another item
381 We take metadata information from the given item and supplement the current item
382 with that info. If tags exist in the new item we use the entire tag information.
383 Properties are appended, and labels, thumbnail and icon are updated if non-empty
385 \param item the item used to supplement information
386 \param replaceLabels whether to replace labels (defaults to true)
388 void UpdateInfo(const CFileItem &item, bool replaceLabels = true);
390 bool IsSamePath(const CFileItem *item) const;
392 bool IsAlbum() const;
394 /*! \brief Sets details using the information from the CVideoInfoTag object
395 Sets the videoinfotag and uses its information to set the label and path.
396 \param video video details to use and set
398 void SetFromVideoInfoTag(const CVideoInfoTag &video);
399 /*! \brief Sets details using the information from the CAlbum object
400 Sets the album in the music info tag and uses its information to set the
401 label and album-specific properties.
402 \param album album details to use and set
404 void SetFromAlbum(const CAlbum &album);
405 /*! \brief Sets details using the information from the CSong object
406 Sets the song in the music info tag and uses its information to set the
407 label, path, song-specific properties and artwork.
408 \param song song details to use and set
410 void SetFromSong(const CSong &song);
412 bool m_bIsShareOrDrive; ///< is this a root share/drive
413 int m_iDriveType; ///< If \e m_bIsShareOrDrive is \e true, use to get the share type. Types see: CMediaSource::m_iDriveType
414 CDateTime m_dateTime; ///< file creation date & time
415 int64_t m_dwSize; ///< file size (0 for folders)
416 CStdString m_strDVDLabel;
417 CStdString m_strTitle;
421 int m_lStartPartNumber;
423 LockType m_iLockMode;
424 CStdString m_strLockCode;
425 int m_iHasLock; // 0 - no lock 1 - lock, but unlocked 2 - locked
429 CStdString m_strPath; ///< complete path to item
431 SortSpecial m_specialSort;
432 bool m_bIsParentFolder;
434 bool m_bLabelPreformated;
435 CStdString m_mimetype;
436 CStdString m_extrainfo;
437 MUSIC_INFO::CMusicInfoTag* m_musicInfoTag;
438 CVideoInfoTag* m_videoInfoTag;
439 EPG::CEpgInfoTag* m_epgInfoTag;
440 PVR::CPVRChannel* m_pvrChannelInfoTag;
441 PVR::CPVRRecording* m_pvrRecordingInfoTag;
442 PVR::CPVRTimerInfoTag * m_pvrTimerInfoTag;
443 CPictureInfoTag* m_pictureInfoTag;
448 \brief A shared pointer to CFileItem
451 typedef boost::shared_ptr<CFileItem> CFileItemPtr;
454 \brief A vector of pointer to CFileItem
457 typedef std::vector< CFileItemPtr > VECFILEITEMS;
460 \brief Iterator for VECFILEITEMS
463 typedef std::vector< CFileItemPtr >::iterator IVECFILEITEMS;
466 \brief A map of pointers to CFileItem
469 typedef std::map<CStdString, CFileItemPtr > MAPFILEITEMS;
472 \brief Iterator for MAPFILEITEMS
475 typedef std::map<CStdString, CFileItemPtr >::iterator IMAPFILEITEMS;
478 \brief Pair for MAPFILEITEMS
481 typedef std::pair<CStdString, CFileItemPtr > MAPFILEITEMSPAIR;
483 typedef bool (*FILEITEMLISTCOMPARISONFUNC) (const CFileItemPtr &pItem1, const CFileItemPtr &pItem2);
484 typedef void (*FILEITEMFILLFUNC) (CFileItemPtr &item);
487 \brief Represents a list of files
488 \sa CFileItemList, CFileItem
490 class CFileItemList : public CFileItem
493 enum CACHE_TYPE { CACHE_NEVER = 0, CACHE_IF_SLOW, CACHE_ALWAYS };
496 CFileItemList(const CStdString& strPath);
497 virtual ~CFileItemList();
498 virtual void Archive(CArchive& ar);
499 CFileItemPtr operator[] (int iItem);
500 const CFileItemPtr operator[] (int iItem) const;
501 CFileItemPtr operator[] (const CStdString& strPath);
502 const CFileItemPtr operator[] (const CStdString& strPath) const;
505 void Add(const CFileItemPtr &pItem);
506 void AddFront(const CFileItemPtr &pItem, int itemPosition);
507 void Remove(CFileItem* pItem);
508 void Remove(int iItem);
509 CFileItemPtr Get(int iItem);
510 const CFileItemPtr Get(int iItem) const;
511 const VECFILEITEMS GetList() const { return m_items; }
512 CFileItemPtr Get(const CStdString& strPath);
513 const CFileItemPtr Get(const CStdString& strPath) const;
515 bool IsEmpty() const;
516 void Append(const CFileItemList& itemlist);
517 void Assign(const CFileItemList& itemlist, bool append = false);
518 bool Copy (const CFileItemList& item, bool copyItems = true);
519 void Reserve(int iCount);
520 void Sort(SORT_METHOD sortMethod, SortOrder sortOrder);
521 /* \brief Sorts the items based on the given sorting options
523 In contrast to Sort (see above) this does not change the internal
524 state by storing the sorting method and order used and therefore
525 will always execute the sorting even if the list of items has
526 already been sorted with the same options before.
528 void Sort(SortDescription sortDescription);
530 void FillInDefaultIcons();
531 int GetFolderCount() const;
532 int GetFileCount() const;
533 int GetSelectedCount() const;
534 int GetObjectCount() const;
535 void FilterCueItems();
536 void RemoveExtensions();
537 void SetFastLookup(bool fastLookup);
538 bool Contains(const CStdString& fileName) const;
539 bool GetFastLookup() const { return m_fastLookup; };
541 /*! \brief stack a CFileItemList
542 By default we stack all items (files and folders) in a CFileItemList
543 \param stackFiles whether to stack all items or just collapse folders (defaults to true)
544 \sa StackFiles,StackFolders
546 void Stack(bool stackFiles = true);
548 SortOrder GetSortOrder() const { return m_sortOrder; }
549 SORT_METHOD GetSortMethod() const { return m_sortMethod; }
550 /*! \brief load a CFileItemList out of the cache
552 The file list may be cached based on which window we're viewing in, as different
553 windows will be listing different portions of the same URL (eg viewing music files
554 versus viewing video files)
556 \param windowID id of the window that's loading this list (defaults to 0)
557 \return true if we loaded from the cache, false otherwise.
558 \sa Save,RemoveDiscCache
560 bool Load(int windowID = 0);
562 /*! \brief save a CFileItemList to the cache
564 The file list may be cached based on which window we're viewing in, as different
565 windows will be listing different portions of the same URL (eg viewing music files
566 versus viewing video files)
568 \param windowID id of the window that's saving this list (defaults to 0)
569 \return true if successful, false otherwise.
570 \sa Load,RemoveDiscCache
572 bool Save(int windowID = 0);
573 void SetCacheToDisc(CACHE_TYPE cacheToDisc) { m_cacheToDisc = cacheToDisc; }
574 bool CacheToDiscAlways() const { return m_cacheToDisc == CACHE_ALWAYS; }
575 bool CacheToDiscIfSlow() const { return m_cacheToDisc == CACHE_IF_SLOW; }
576 /*! \brief remove a previously cached CFileItemList from the cache
578 The file list may be cached based on which window we're viewing in, as different
579 windows will be listing different portions of the same URL (eg viewing music files
580 versus viewing video files)
582 \param windowID id of the window whose cache we which to remove (defaults to 0)
585 void RemoveDiscCache(int windowID = 0) const;
586 bool AlwaysCache() const;
588 void Swap(unsigned int item1, unsigned int item2);
590 /*! \brief Update an item in the item list
591 \param item the new item, which we match based on path to an existing item in the list
592 \return true if the item exists in the list (and was thus updated), false otherwise.
594 bool UpdateItem(const CFileItem *item);
596 void AddSortMethod(SORT_METHOD method, int buttonLabel, const LABEL_MASKS &labelMasks);
597 bool HasSortDetails() const { return m_sortDetails.size() != 0; };
598 const std::vector<SORT_METHOD_DETAILS> &GetSortDetails() const { return m_sortDetails; };
600 /*! \brief Specify whether this list should be sorted with folders separate from files
601 By default we sort with folders listed (and sorted separately) except for those sort modes
602 which should be explicitly sorted with folders interleaved with files (eg SORT_METHOD_FILES).
603 With this set the folder state will be ignored, allowing folders and files to sort interleaved.
604 \param sort whether to ignore the folder state.
606 void SetSortIgnoreFolders(bool sort) { m_sortIgnoreFolders = sort; };
607 bool GetReplaceListing() const { return m_replaceListing; };
608 void SetReplaceListing(bool replace);
609 void SetContent(const CStdString &content) { m_content = content; };
610 const CStdString &GetContent() const { return m_content; };
612 void ClearSortState();
614 void Sort(FILEITEMLISTCOMPARISONFUNC func);
615 void FillSortFields(FILEITEMFILLFUNC func);
616 CStdString GetDiscFileCache(int windowID) const;
619 \brief stack files in a CFileItemList
625 \brief stack folders in a CFileItemList
630 VECFILEITEMS m_items;
633 SORT_METHOD m_sortMethod;
634 SortOrder m_sortOrder;
635 bool m_sortIgnoreFolders;
636 CACHE_TYPE m_cacheToDisc;
637 bool m_replaceListing;
638 CStdString m_content;
640 std::vector<SORT_METHOD_DETAILS> m_sortDetails;
642 CCriticalSection m_lock;