1 /*
2     SPDX-FileCopyrightText: 2010 Joris Guisson <joris.guisson@gmail.com>
3     SPDX-License-Identifier: GPL-2.0-or-later
4 */
5 
6 #ifndef KT_MEDIAFILE_H
7 #define KT_MEDIAFILE_H
8 
9 #include <Phonon/MediaSource>
10 #include <QSharedPointer>
11 #include <QString>
12 #include <QWeakPointer>
13 
14 #include <torrent/torrentfilestream.h>
15 #include <util/constants.h>
16 
17 namespace bt
18 {
19 class TorrentInterface;
20 class TorrentFileStream;
21 }
22 
23 namespace kt
24 {
25 class MediaPlayer;
26 
27 const bt::Uint32 INVALID_INDEX = 0xFFFFFFFF;
28 
29 /**
30     Class representing a multimedia file of a torrent
31 */
32 class MediaFile
33 {
34 public:
35     /**
36         Constructor for single file torrents
37         @param tc The TorrentInterface
38     */
39     MediaFile(bt::TorrentInterface *tc);
40 
41     /**
42         Constructor for multi file torrents
43         @param tc The TorrentInterface
44         @param idx The index of the file in the torrent
45     */
46     MediaFile(bt::TorrentInterface *tc, int idx);
47 
48     /**
49         Copy constructor
50         @param mf The MediaFile to copy
51     */
52     MediaFile(const MediaFile &mf);
53     ~MediaFile();
54 
55     /// Get the path of the MediaFile
56     QString path() const;
57 
58     /// Get the name of the MediaFile
59     QString name() const;
60 
61     /// Is a preview available
62     bool previewAvailable() const;
63 
64     /// Is it fully available
65     bool fullyAvailable() const;
66 
67     /// Get the download percentage
68     float downloadPercentage() const;
69 
70     /// Get the torrent of this MediaFile
torrent()71     bt::TorrentInterface *torrent() const
72     {
73         return tc;
74     }
75 
76     /// Get the size of the MediaFile
77     bt::Uint64 size() const;
78 
79     /// Get the first chunk of the file
80     bt::Uint32 firstChunk() const;
81 
82     /// Get the last chunk of the file
83     bt::Uint32 lastChunk() const;
84 
85     /// Create a TorrentFileStream object for this MediaFile and return a weak pointer to it
86     bt::TorrentFileStream::WPtr stream();
87 
88     /// Is this a video ?
89     bool isVideo() const;
90 
91     typedef QSharedPointer<MediaFile> Ptr;
92     typedef QWeakPointer<MediaFile> WPtr;
93 
94 private:
95     bt::TorrentInterface *tc;
96     bt::Uint32 idx;
97     bt::TorrentFileStream::Ptr tfs;
98 };
99 
100 /**
101     A MediaFileRef is a reference to a MediaFile
102     which keeps a weak pointer and the path of the file as backup.
103     If the weak pointer can no longer resolve to a strong pointer
104     the actual path can still be used.
105 
106     This can also be used for files only (without an actual MediaFile)
107 */
108 class MediaFileRef
109 {
110 public:
111     /// Default constructor
112     MediaFileRef();
113 
114     /// Simple file mode constructor
115     MediaFileRef(const QString &p);
116 
117     /// Strong pointer constructor
118     MediaFileRef(MediaFile::Ptr ptr);
119 
120     /// Copy constructor
121     MediaFileRef(const MediaFileRef &other);
122     ~MediaFileRef();
123 
124     /// Get the MediaFile
mediaFile()125     MediaFile::Ptr mediaFile()
126     {
127         return ptr.toStrongRef();
128     }
129 
130     /// Get the path
path()131     QString path() const
132     {
133         return file_path;
134     }
135 
136     /// Get the name of the file
137     QString name() const;
138 
139     /// Assignment operator
140     MediaFileRef &operator=(const MediaFileRef &other);
141 
142     /// Comparison operator
143     bool operator==(const MediaFileRef &other) const;
144 
145     /// Negative comparison operator
146     bool operator!=(const MediaFileRef &other) const;
147 
148     /// Create a Phonon::MediaSource for this MediaFileRef
149     Phonon::MediaSource createMediaSource(MediaPlayer *p);
150 
151 private:
152     MediaFile::WPtr ptr;
153     QString file_path;
154 };
155 
156 }
157 
158 #endif // KT_MEDIAFILE_H
159