Debian Codegraph @

src/mediaplayer.cpp

raw ◀ ▶
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 #ifdef ADD_MPRIS
34 #include <QDBusConnection>
35 #include <QDBusConnectionInterface>
36 #endif
37
38 static void printUsage(const QStringList& arguments)
39 {
40 qDebug() << "usage:"
41 << arguments.at(0).toUtf8().constData() << "[options] uri";
42 qDebug();
43 qDebug() << "options:";
44 qDebug() << "\t-w --windowed: start windowed";
45 }
46
47 MediaPlayer::MediaPlayer(int& argc, char** argv)
48 : QApplication(argc, argv)
49 , m_view(nullptr)
50 , m_playUri(QUrl())
51 , m_windowed(false)
52 {
53 }
54
55 bool MediaPlayer::setup()
56 {
57 QStringList args = arguments();
58 m_windowed = args.removeAll("-w") + args.removeAll("--windowed") > 0;
59 bool testability = args.removeAll("-testability") > 0;
60
61 // The testability driver is only loaded by QApplication but not by
62 // QGuiApplication.
63 // However, QApplication depends on QWidget which would add some
64 // unneeded overhead => Let's load the testability driver on our own.
65 if (testability) {
66 QLibrary testLib(QLatin1String("qttestability"));
67 if (testLib.load()) {
68 typedef void (*TasInitialize)(void);
69 TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
70 if (initFunction) {
71 initFunction();
72 } else {
73 qCritical("Library qttestability resolve failed!");
74 }
75 } else {
76 qCritical("Library qttestability load failed!");
77 }
78 }
79
80 // TODO: move this to SDK/ShareMenu library
81 // qmlRegisterType<ShareFile>("SDKHelper", 1, 0, "ShareFile");
82
83 m_view = new QQuickView();
84 // m_view->engine()->addImageProvider("video", new ThumbnailProvider);
85 m_view->setColor(QColor("black"));
86 m_view->setResizeMode(QQuickView::SizeRootObjectToView);
87 m_view->setTitle(gettext("Media Player"));
88
89 // remove non url args
90 QStringList newArgs(args);
91 Q_FOREACH (const QString& arg, args) {
92 if (arg.startsWith("--"))
93 newArgs.removeOne(arg);
94 }
95 args = newArgs;
96
97 if (args.count() >= 2) {
98 setPlayUri(args[1]);
99 }
100
101 m_view->rootContext()->setContextProperty("mpApplication", this);
102 m_view->rootContext()->setContextProperty("screenWidth", m_view->size().width());
103 m_view->rootContext()->setContextProperty("screenHeight", m_view->size().height());
104
•105 bool bMpris = false;
106
107 #ifdef ADD_MPRIS
108 QDBusConnection cConnection = QDBusConnection::sessionBus ();
109 QDBusConnectionInterface *pInterface = cConnection.interface ();
110 bMpris = !pInterface->isServiceRegistered ("com.lomiri.MediaHub.Service");
111 QString sPath = mediaPlayerDirectory ();
112 bool bInstalled = isRunningInstalled ();
113
114 if (bInstalled == false)
115 {
116 QDir pDir (sPath);
117 sPath = pDir.absoluteFilePath ("../data");
118 sPath = pDir.cleanPath (sPath);
119 }
120
121 sPath += "/lomiri-mediaplayer-app.png";
122 this->m_view->rootContext ()->setContextProperty ("APP_ICON", QVariant (sPath));
123 #endif
124
125 this->m_view->rootContext ()->setContextProperty ("ADD_MPRIS", QVariant (bMpris));
126
127 connect(m_view, SIGNAL(widthChanged(int)), SLOT(onWidthChanged(int)));
128 connect(m_view, SIGNAL(heightChanged(int)), SLOT(onHeightChanged(int)));
129 connect(m_view->engine(), SIGNAL(quit()), SLOT(quit()));
130
131 // Set the orientation changes that this app is interested in being signaled about
132 QApplication::primaryScreen()->setOrientationUpdateMask(Qt::PortraitOrientation | Qt::LandscapeOrientation | Qt::InvertedPortraitOrientation | Qt::InvertedLandscapeOrientation);
133
134 QUrl source(mediaPlayerDirectory() + "/qml/player.qml");
135 m_view->setSource(source);
136 m_view->setWidth(1200);
137 m_view->setHeight(675);
138 m_view->show();
139
140 return true;
141 }
142
143 void MediaPlayer::setPlayUri(QUrl uri)
144 {
145 QUrl playUri;
146
147 if (uri.scheme() == "video" || uri.scheme() == "audio") {
148 uri.setScheme("file");
149 }
150
151 if (uri.isRelative()) {
152 uri = QUrl::fromLocalFile(QDir::current().absoluteFilePath(uri.toString()));
153 }
154
155 // Check if it's a local file
156 if (uri.isValid() && uri.isLocalFile()) {
157 QFileInfo info(uri.toLocalFile());
158 if (info.exists() && info.isFile()) {
159 playUri = uri;
160 } else {
161 qWarning() << "File not found:" << uri << info.exists() << info.isFile();
162 }
163 // Otherwise see if it's a remote stream
164 } else if (uri.isValid()) {
165 playUri = uri;
166 } else {
167 qWarning() << "Invalid uri:" << uri;
168 }
169
170 if (playUri != m_playUri) {
171 m_playUri = playUri;
172 emit playUriChanged();
173 }
174 }
175
176 bool MediaPlayer::windowed()
177 {
178 return m_windowed;
179 }
180
181 int MediaPlayer::getStoredPosition()
182 {
183 QString fileName = m_playUri.toString().mid(m_playUri.toString().lastIndexOf("/"));
184 return m_settings.value(fileName, -1).toInt();
185 }
186
187 MediaPlayer::~MediaPlayer()
188 {
189 if (m_view) {
190 delete m_view;
191 }
192 m_settings.sync();
193 }
194
195 void MediaPlayer::toggleFullscreen()
196 {
197 QWindow::Visibility newVisibility = m_view->visibility() == QWindow::FullScreen ? QWindow::Windowed : QWindow::FullScreen;
198 m_view->setVisibility(newVisibility);
199 }
200
201 void MediaPlayer::leaveFullScreen()
202 {
203 if (m_view->visibility() == QWindow::FullScreen) {
204 m_view->setVisibility(QWindow::Windowed);
205 }
206 }
207
208 void MediaPlayer::onWidthChanged(int width)
209 {
210 m_view->rootContext()->setContextProperty("screenWidth", width);
211 }
212
213 void MediaPlayer::onHeightChanged(int height)
214 {
215 m_view->rootContext()->setContextProperty("screenHeight", height);
216 }
217
218 QList<QUrl> MediaPlayer::copyFiles(const QList<QUrl>& urls)
219 {
220 static QString moviesDir = QStandardPaths::writableLocation(QStandardPaths::MoviesLocation);
221
222 QList<QUrl> result;
223
224 Q_FOREACH (const QUrl& url, urls) {
225 if (!url.isLocalFile()) {
226 qWarning() << "Remote files not supported:" << url;
227 continue;
228 }
229
230 QFileInfo originalFile(url.toLocalFile());
231
232 QString filename = originalFile.fileName();
233 QString suffix = originalFile.completeSuffix();
234 QString filenameWithoutSuffix;
235 if (suffix.isEmpty()) {
236 QMimeDatabase mdb;
237 QMimeType mt = mdb.mimeTypeForFile(originalFile.absoluteFilePath());
238
239 // If the filename doesn't have an extension add one from the
240 // detected mimetype
241 if (!mt.preferredSuffix().isEmpty()) {
242 suffix = mt.preferredSuffix();
243 }
244 filenameWithoutSuffix = filename;
245 } else {
246 filenameWithoutSuffix = originalFile.baseName();
247 }
248
249 QFileInfo newFile(moviesDir, QString("%1.%2").arg(filenameWithoutSuffix).arg(suffix));
250 if (newFile.exists()) {
251 // find a alternative name
252 int index = 1;
253 do {
254 newFile = QFileInfo(moviesDir,
255 QString("%1(%2).%3")
256 .arg(filenameWithoutSuffix)
257 .arg(index)
258 .arg(suffix));
259 index++;
260 } while (newFile.exists());
261 }
262
263 if (QFile::copy(originalFile.absoluteFilePath(), newFile.absoluteFilePath())) {
264 result << QUrl::fromLocalFile(newFile.absoluteFilePath());
265 } else {
266 qWarning() << "Fail to copy file from:" << originalFile.absoluteFilePath() << "to" << newFile.absoluteFilePath();
267 }
268 }
269 return result;
270 }
271
272 void MediaPlayer::storePosition(int duration)
273 {
274 if (m_playUri.isEmpty()) {
275 return;
276 }
277
278 QString fileName = m_playUri.toString().mid(m_playUri.toString().lastIndexOf("/"));
279
280 if (duration > 0) {
281 m_settings.setValue(fileName, duration);
282 } else if (duration == -1) {
283 m_settings.remove(fileName);
284 }
285 }
286
287 bool MediaPlayer::isAudio(const QString& file)
288 {
289 if (file.isEmpty()) {
290 return false;
291 }
292 QMimeDatabase mdb;
293 QMimeType mt = mdb.mimeTypeForFile(file);
294 if (!mt.isValid()) {
295 return false;
296 } else {
297 return mt.name().startsWith(QLatin1String("audio"));
298 }
299 }