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