1*4882a593Smuzhiyun /****************************************************************************
2*4882a593Smuzhiyun **
3*4882a593Smuzhiyun ** Copyright (C) 2015 The Qt Company Ltd.
4*4882a593Smuzhiyun ** Contact: http://www.qt.io/licensing/
5*4882a593Smuzhiyun **
6*4882a593Smuzhiyun ** This file is part of the examples of the Qt Toolkit.
7*4882a593Smuzhiyun **
8*4882a593Smuzhiyun ** $QT_BEGIN_LICENSE:BSD$
9*4882a593Smuzhiyun ** You may use this file under the terms of the BSD license as follows:
10*4882a593Smuzhiyun **
11*4882a593Smuzhiyun ** "Redistribution and use in source and binary forms, with or without
12*4882a593Smuzhiyun ** modification, are permitted provided that the following conditions are
13*4882a593Smuzhiyun ** met:
14*4882a593Smuzhiyun ** * Redistributions of source code must retain the above copyright
15*4882a593Smuzhiyun ** notice, this list of conditions and the following disclaimer.
16*4882a593Smuzhiyun ** * Redistributions in binary form must reproduce the above copyright
17*4882a593Smuzhiyun ** notice, this list of conditions and the following disclaimer in
18*4882a593Smuzhiyun ** the documentation and/or other materials provided with the
19*4882a593Smuzhiyun ** distribution.
20*4882a593Smuzhiyun ** * Neither the name of The Qt Company Ltd nor the names of its
21*4882a593Smuzhiyun ** contributors may be used to endorse or promote products derived
22*4882a593Smuzhiyun ** from this software without specific prior written permission.
23*4882a593Smuzhiyun **
24*4882a593Smuzhiyun **
25*4882a593Smuzhiyun ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26*4882a593Smuzhiyun ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27*4882a593Smuzhiyun ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28*4882a593Smuzhiyun ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29*4882a593Smuzhiyun ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30*4882a593Smuzhiyun ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31*4882a593Smuzhiyun ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32*4882a593Smuzhiyun ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33*4882a593Smuzhiyun ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34*4882a593Smuzhiyun ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35*4882a593Smuzhiyun ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36*4882a593Smuzhiyun **
37*4882a593Smuzhiyun ** $QT_END_LICENSE$
38*4882a593Smuzhiyun **
39*4882a593Smuzhiyun ****************************************************************************/
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun #include "musicplayer.h"
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun #include <QApplication>
44*4882a593Smuzhiyun #include <QCommandLineParser>
45*4882a593Smuzhiyun #include <QCommandLineOption>
46*4882a593Smuzhiyun #include <QDesktopWidget>
47*4882a593Smuzhiyun #include <QFileInfo>
48*4882a593Smuzhiyun #include <QMimeDatabase>
49*4882a593Smuzhiyun #include <QSettings>
50*4882a593Smuzhiyun #include <QIcon>
51*4882a593Smuzhiyun #include <QDir>
52*4882a593Smuzhiyun #include <QUrl>
53*4882a593Smuzhiyun #include <QDebug>
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun //! [0]
associateFileTypes()57*4882a593Smuzhiyun static bool associateFileTypes()
58*4882a593Smuzhiyun {
59*4882a593Smuzhiyun QString displayName = QGuiApplication::applicationDisplayName();
60*4882a593Smuzhiyun QString filePath = QCoreApplication::applicationFilePath();
61*4882a593Smuzhiyun QString fileName = QFileInfo(filePath).fileName();
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun const QString key = QStringLiteral("HKEY_CURRENT_USER\\Software\\Classes\\Applications\\") + fileName;
64*4882a593Smuzhiyun QSettings settings(key, QSettings::NativeFormat);
65*4882a593Smuzhiyun if (settings.status() != QSettings::NoError) {
66*4882a593Smuzhiyun qWarning() << "Cannot access registry key" << key;
67*4882a593Smuzhiyun return false;
68*4882a593Smuzhiyun }
69*4882a593Smuzhiyun settings.setValue(QStringLiteral("FriendlyAppName"), displayName);
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun settings.beginGroup(QStringLiteral("SupportedTypes"));
72*4882a593Smuzhiyun QMimeDatabase mimeDatabase;
73*4882a593Smuzhiyun foreach (const QString &fileType, MusicPlayer::supportedMimeTypes()) {
74*4882a593Smuzhiyun foreach (QString suffix, mimeDatabase.mimeTypeForName(fileType).suffixes()) {
75*4882a593Smuzhiyun suffix.prepend('.');
76*4882a593Smuzhiyun settings.setValue(suffix, QString());
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun settings.endGroup();
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun settings.beginGroup(QStringLiteral("shell"));
82*4882a593Smuzhiyun settings.beginGroup(QStringLiteral("open"));
83*4882a593Smuzhiyun settings.setValue(QStringLiteral("FriendlyAppName"), displayName);
84*4882a593Smuzhiyun settings.beginGroup(QStringLiteral("Command"));
85*4882a593Smuzhiyun settings.setValue(QStringLiteral("."),
86*4882a593Smuzhiyun QLatin1Char('"') + QDir::toNativeSeparators(filePath) + QStringLiteral("\" \"%1\""));
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun return true;
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun //! [0]
91*4882a593Smuzhiyun
main(int argc,char * argv[])92*4882a593Smuzhiyun int main(int argc, char *argv[])
93*4882a593Smuzhiyun {
94*4882a593Smuzhiyun QApplication app(argc, argv);
95*4882a593Smuzhiyun QCoreApplication::setApplicationName(QStringLiteral("MusicPlayer"));
96*4882a593Smuzhiyun QCoreApplication::setApplicationVersion( QLatin1String(QT_VERSION_STR));
97*4882a593Smuzhiyun QCoreApplication::setOrganizationName(QStringLiteral("QtWinExtras"));
98*4882a593Smuzhiyun QCoreApplication::setOrganizationDomain("qt-project.org");
99*4882a593Smuzhiyun QGuiApplication::setApplicationDisplayName(QStringLiteral("QtWinExtras Music Player"));
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun if (!associateFileTypes())
102*4882a593Smuzhiyun return -1;
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun QCommandLineParser parser;
105*4882a593Smuzhiyun parser.setApplicationDescription(QGuiApplication::applicationDisplayName());
106*4882a593Smuzhiyun parser.addHelpOption();
107*4882a593Smuzhiyun parser.addVersionOption();
108*4882a593Smuzhiyun parser.addPositionalArgument(QStringLiteral("url"), MusicPlayer::tr("The URL to open."));
109*4882a593Smuzhiyun parser.process(app);
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun MusicPlayer player;
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun if (!parser.positionalArguments().isEmpty())
114*4882a593Smuzhiyun player.playUrl(QUrl::fromUserInput(parser.positionalArguments().constFirst(), QDir::currentPath(), QUrl::AssumeLocalFile));
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun const QRect availableGeometry = QApplication::desktop()->availableGeometry(&player);
117*4882a593Smuzhiyun player.resize(availableGeometry.width() / 6, availableGeometry.height() / 17);
118*4882a593Smuzhiyun player.show();
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun return app.exec();
121*4882a593Smuzhiyun }
122