Debian Codegraph @

src/mediaplayer.cpp

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 * Copyright (C) 2013 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16 #include "mediaplayer.h"
17
18 #include "config.h"
19 #include <QDebug>
20 #include <QDir>
21 #include <QGuiApplication>
22 #include <QLibrary>
23 #include <QMimeDatabase>
24 #include <QQmlContext>
25 #include <QQmlEngine>
26 #include <QScreen>
27 #include <QStandardPaths>
28 #include <QString>
29 #include <QStringList>
30 #include <QUrl>
31 #include <libintl.h>
32
33 static void printUsage(const QStringList& arguments)
34 {
35 qDebug() << "usage:"
36 << arguments.at(0).toUtf8().constData() << "[options] uri";
37 qDebug();
38 qDebug() << "options:";
39 qDebug() << "\t-w --windowed: start windowed";
40 }
41
42 MediaPlayer::MediaPlayer(int& argc, char** argv)
43 : QApplication(argc, argv)
44 , m_view(nullptr)
45 , m_playUri(QUrl())
46 , m_windowed(false)
47 {
48 }
49
50 bool MediaPlayer::setup()
51 {
52 QStringList args = arguments();
53 m_windowed = args.removeAll("-w") + args.removeAll("--windowed") > 0;
54 bool testability = args.removeAll("-testability") > 0;
55
56 // The testability driver is only loaded by QApplication but not by
57 // QGuiApplication.
58 // However, QApplication depends on QWidget which would add some
59 // unneeded overhead => Let's load the testability driver on our own.
60 if (testability) {
61 QLibrary testLib(QLatin1String("qttestability"));
62 if (testLib.load()) {
63 typedef void (*TasInitialize)(void);
64 TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
65 if (initFunction) {
66 initFunction();
67 } else {
68 qCritical("Library qttestability resolve failed!");
69 }
70 } else {
71 qCritical("Library qttestability load failed!");
72 }
73 }
74
75 // TODO: move this to SDK/ShareMenu library
76 // qmlRegisterType<ShareFile>("SDKHelper", 1, 0, "ShareFile");
77
78 m_view = new QQuickView();
79 // m_view->engine()->addImageProvider("video", new ThumbnailProvider);
80 m_view->setColor(QColor("black"));
81 m_view->setResizeMode(QQuickView::SizeRootObjectToView);
82 m_view->setTitle(gettext("Media Player"));
83
84 // remove non url args
85 QStringList newArgs(args);
86 Q_FOREACH (const QString& arg, args) {
87 if (arg.startsWith("--"))
88 newArgs.removeOne(arg);
89 }
90 args = newArgs;
91
92 if (args.count() >= 2) {
93 setPlayUri(args[1]);
94 }
95
96 m_view->rootContext()->setContextProperty("mpApplication", this);
97 m_view->rootContext()->setContextProperty("screenWidth", m_view->size().width());
98 m_view->rootContext()->setContextProperty("screenHeight", m_view->size().height());
99
100 connect(m_view, SIGNAL(widthChanged(int)), SLOT(onWidthChanged(int)));
101 connect(m_view, SIGNAL(heightChanged(int)), SLOT(onHeightChanged(int)));
102 connect(m_view->engine(), SIGNAL(quit()), SLOT(quit()));
103
104 // Set the orientation changes that this app is interested in being signaled about
105 QApplication::primaryScreen()->setOrientationUpdateMask(Qt::PortraitOrientation | Qt::LandscapeOrientation | Qt::InvertedPortraitOrientation | Qt::InvertedLandscapeOrientation);
106
107 QUrl source(mediaPlayerDirectory() + "/qml/player.qml");
108 m_view->setSource(source);
109 m_view->setWidth(1200);
110 m_view->setHeight(675);
111 m_view->show();
112
113 return true;
114 }
115
116 void MediaPlayer::setPlayUri(QUrl uri)
117 {
118 QUrl playUri;
119
120 if (uri.scheme() == "video" || uri.scheme() == "audio") {
121 uri.setScheme("file");
122 }
123
124 if (uri.isRelative()) {
125 uri = QUrl::fromLocalFile(QDir::current().absoluteFilePath(uri.toString()));
126 }
127
128 // Check if it's a local file
129 if (uri.isValid() && uri.isLocalFile()) {
130 QFileInfo info(uri.toLocalFile());
131 if (info.exists() && info.isFile()) {
132 playUri = uri;
133 } else {
134 qWarning() << "File not found:" << uri << info.exists() << info.isFile();
135 }
136 // Otherwise see if it's a remote stream
137 } else if (uri.isValid()) {
138 playUri = uri;
139 } else {
140 qWarning() << "Invalid uri:" << uri;
141 }
142
143 if (playUri != m_playUri) {
144 m_playUri = playUri;
145 emit playUriChanged();
146 }
147 }
148
149 bool MediaPlayer::windowed()
150 {
151 return m_windowed;
152 }
153
154 int MediaPlayer::getStoredPosition()
155 {
156 QString fileName = m_playUri.toString().mid(m_playUri.toString().lastIndexOf("/"));
157 return m_settings.value(fileName, -1).toInt();
158 }
159
160 MediaPlayer::~MediaPlayer()
161 {
162 if (m_view) {
163 delete m_view;
164 }
165 m_settings.sync();
166 }
167
168 void MediaPlayer::toggleFullscreen()
169 {
170 QWindow::Visibility newVisibility = m_view->visibility() == QWindow::FullScreen ? QWindow::Windowed : QWindow::FullScreen;
171 m_view->setVisibility(newVisibility);
172 }
173
174 void MediaPlayer::leaveFullScreen()
175 {
176 if (m_view->visibility() == QWindow::FullScreen) {
177 m_view->setVisibility(QWindow::Windowed);
178 }
179 }
180
181 void MediaPlayer::onWidthChanged(int width)
182 {
183 m_view->rootContext()->setContextProperty("screenWidth", width);
184 }
185
186 void MediaPlayer::onHeightChanged(int height)
187 {
188 m_view->rootContext()->setContextProperty("screenHeight", height);
189 }
190
191 QList<QUrl> MediaPlayer::copyFiles(const QList<QUrl>& urls)
192 {
193 static QString moviesDir = QStandardPaths::writableLocation(QStandardPaths::MoviesLocation);
194
195 QList<QUrl> result;
196
197 Q_FOREACH (const QUrl& url, urls) {
198 if (!url.isLocalFile()) {
199 qWarning() << "Remote files not supported:" << url;
200 continue;
201 }
202
203 QFileInfo originalFile(url.toLocalFile());
204
205 QString filename = originalFile.fileName();
206 QString suffix = originalFile.completeSuffix();
207 QString filenameWithoutSuffix;
208 if (suffix.isEmpty()) {
209 QMimeDatabase mdb;
210 QMimeType mt = mdb.mimeTypeForFile(originalFile.absoluteFilePath());
211
212 // If the filename doesn't have an extension add one from the
213 // detected mimetype
214 if (!mt.preferredSuffix().isEmpty()) {
215 suffix = mt.preferredSuffix();
216 }
217 filenameWithoutSuffix = filename;
218 } else {
219 filenameWithoutSuffix = originalFile.baseName();
220 }
221
222 QFileInfo newFile(moviesDir, QString("%1.%2").arg(filenameWithoutSuffix).arg(suffix));
223 if (newFile.exists()) {
224 // find a alternative name
225 int index = 1;
226 do {
227 newFile = QFileInfo(moviesDir,
228 QString("%1(%2).%3")
229 .arg(filenameWithoutSuffix)
230 .arg(index)
231 .arg(suffix));
232 index++;
233 } while (newFile.exists());
234 }
235
236 if (QFile::copy(originalFile.absoluteFilePath(), newFile.absoluteFilePath())) {
237 result << QUrl::fromLocalFile(newFile.absoluteFilePath());
238 } else {
239 qWarning() << "Fail to copy file from:" << originalFile.absoluteFilePath() << "to" << newFile.absoluteFilePath();
240 }
241 }
242 return result;
243 }
244
245 void MediaPlayer::storePosition(int duration)
246 {
247 if (m_playUri.isEmpty()) {
248 return;
249 }
250
251 QString fileName = m_playUri.toString().mid(m_playUri.toString().lastIndexOf("/"));
252
253 if (duration > 0) {
254 m_settings.setValue(fileName, duration);
255 } else if (duration == -1) {
256 m_settings.remove(fileName);
257 }
258 }
259
260 bool MediaPlayer::isAudio(const QString& file)
261 {
262 if (file.isEmpty()) {
263 return false;
264 }
265 QMimeDatabase mdb;
266 QMimeType mt = mdb.mimeTypeForFile(file);
267 if (!mt.isValid()) {
268 return false;
269 } else {
270 return mt.name().startsWith(QLatin1String("audio"));
271 }
272 }