]> git.sur5r.net Git - minitube/blobdiff - src/main.cpp
Upload 3.9.3-2 to unstable
[minitube] / src / main.cpp
index 399e6f081a940dc60d929e79ef22c65d1fe49b04..26768f0f64ce5f56bc381da84c4e72fdd36a65f9 100644 (file)
-#include <QtGui>
-#include <qtsingleapplication.h>
+/* $BEGIN_LICENSE
+
+This file is part of Minitube.
+Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
+
+Minitube is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+Minitube is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with Minitube.  If not, see <http://www.gnu.org/licenses/>.
+
+$END_LICENSE */
+
+#include <QtNetwork>
+#include <QtWidgets>
+
 #include "constants.h"
+#include "iconutils.h"
+#include "updateutils.h"
+
 #include "mainwindow.h"
 #include "searchparams.h"
-#include "utils.h"
-#ifndef Q_WS_X11
+#include <qtsingleapplication.h>
+#ifdef APP_EXTRA
 #include "extra.h"
 #endif
-#ifdef Q_WS_MAC
+#ifdef Q_OS_MAC
 #include "mac_startup.h"
 #endif
 
-#ifdef Q_WS_X11
-QString getThemeName() {
-    QString themeName;
-
-    QProcess process;
-    process.start("dconf",
-                  QStringList() << "read" << "/org/gnome/desktop/interface/gtk-theme");
-    if (process.waitForFinished()) {
-        themeName = process.readAllStandardOutput();
-        themeName = themeName.trimmed();
-        themeName.remove('\'');
-        if (!themeName.isEmpty()) return themeName;
-    }
+void showWindow(QtSingleApplication &app, const QString &pkgDataDir) {
+    MainWindow *mainWin = new MainWindow();
 
-    QString rcPaths = QString::fromLocal8Bit(qgetenv("GTK2_RC_FILES"));
-    if (!rcPaths.isEmpty()) {
-        QStringList paths = rcPaths.split(QLatin1String(":"));
-        foreach (const QString &rcPath, paths) {
-            if (!rcPath.isEmpty()) {
-                QFile rcFile(rcPath);
-                if (rcFile.exists() && rcFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
-                    QTextStream in(&rcFile);
-                    while(!in.atEnd()) {
-                        QString line = in.readLine();
-                        if (line.contains(QLatin1String("gtk-theme-name"))) {
-                            line = line.right(line.length() - line.indexOf(QLatin1Char('=')) - 1);
-                            line.remove(QLatin1Char('\"'));
-                            line = line.trimmed();
-                            themeName = line;
-                            break;
-                        }
-                    }
-                }
-            }
-            if (!themeName.isEmpty())
-                break;
+#ifndef APP_MAC
+    QIcon appIcon;
+    if (!pkgDataDir.isEmpty()) {
+        appIcon = IconUtils::icon(Constants::UNIX_NAME);
+    } else {
+        QString dataDir = qApp->applicationDirPath() + "/data";
+        const int iconSizes[] = {16, 22, 32, 48, 64, 128, 256, 512};
+        for (int i = 0; i < 8; i++) {
+            QString size = QString::number(iconSizes[i]);
+            QString png = dataDir + '/' + size + 'x' + size + '/' + Constants::UNIX_NAME +
+                          QLatin1String(".png");
+            appIcon.addFile(png, QSize(iconSizes[i], iconSizes[i]));
         }
     }
+    if (appIcon.isNull()) appIcon.addFile(":/images/app.png");
+    mainWin->setWindowIcon(appIcon);
+#endif
 
-    // Fall back to gconf
-    if (themeName.isEmpty())
-        themeName = QGtkStyle::getGConfString(QLatin1String("/desktop/gnome/interface/gtk_theme"));
+    mainWin->connect(&app, SIGNAL(messageReceived(const QString &)), mainWin,
+                     SLOT(messageReceived(const QString &)));
+    app.setActivationWindow(mainWin, true);
 
-    return themeName;
+    mainWin->show();
 }
-#endif
 
 int main(int argc, char **argv) {
+#ifndef QT_NO_DEBUG_OUTPUT
+    qSetMessagePattern("[%{function}] %{message}");
+#endif
 
-#ifdef Q_WS_MAC
+#ifdef MEDIA_MPV
+    QSurfaceFormat format = QSurfaceFormat::defaultFormat();
+#ifdef APP_MAC
+    format.setMajorVersion(4);
+    format.setMinorVersion(1);
+#endif
+    format.setProfile(QSurfaceFormat::CoreProfile);
+    QSurfaceFormat::setDefaultFormat(format);
+#endif
+
+#ifdef Q_OS_MAC
     mac::MacMain();
 #endif
 
+    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
+
     QtSingleApplication app(argc, argv);
-    QString message = app.arguments().size() > 1 ? app.arguments().at(1) : QString();
-    if (message == QLatin1String("--help")) {
-        MainWindow::printHelp();
-        return 0;
+    QString message;
+    if (app.arguments().size() > 1) {
+        message = app.arguments().at(1);
+        if (message == QLatin1String("--help")) {
+            MainWindow::printHelp();
+            return 0;
+        }
     }
-    if (app.sendMessage(message))
-        return 0;
+    if (app.sendMessage(message)) return 0;
 
-    app.setApplicationName(QLatin1String(Constants::NAME));
-    app.setOrganizationName(QLatin1String(Constants::ORG_NAME));
-    app.setOrganizationDomain(QLatin1String(Constants::ORG_DOMAIN));
-    app.setWheelScrollLines(1);
+    app.setApplicationName(Constants::NAME);
+    app.setOrganizationName(Constants::ORG_NAME);
+    app.setOrganizationDomain(Constants::ORG_DOMAIN);
+    app.setApplicationVersion(Constants::VERSION);
     app.setAttribute(Qt::AA_DontShowIconsInMenus);
+    app.setWheelScrollLines(1);
+    app.setAttribute(Qt::AA_UseHighDpiPixmaps);
 
-#ifndef Q_WS_X11
+#ifdef APP_EXTRA
     Extra::appSetup(&app);
 #else
-    bool isGtk = app.style()->metaObject()->className() == QLatin1String("QGtkStyle");
-    if (isGtk) {
-        app.setProperty("gtk", isGtk);
-        QString themeName = getThemeName();
-        app.setProperty("style", themeName);
-    }
     QFile cssFile(":/style.css");
     cssFile.open(QFile::ReadOnly);
     QString styleSheet = QLatin1String(cssFile.readAll());
     app.setStyleSheet(styleSheet);
+    cssFile.close();
 #endif
 
     // qt translations
     QTranslator qtTranslator;
-    qtTranslator.load("qt_" + QLocale::system().name(),
+    qtTranslator.load(QLatin1String("qt_") + QLocale::system().name(),
                       QLibraryInfo::location(QLibraryInfo::TranslationsPath));
     app.installTranslator(&qtTranslator);
 
-    // app translations
+    QString pkgDataDir;
 #ifdef PKGDATADIR
-    QString dataDir = QLatin1String(PKGDATADIR);
+    pkgDataDir = QLatin1String(PKGDATADIR);
+#endif
+
+    // app translations
+#ifdef APP_MAC
+    QString localeDir = qApp->applicationDirPath() + QLatin1String("/../Resources/locale");
 #else
-    QString dataDir = "";
+    QString localeDir = qApp->applicationDirPath() + QLatin1String("/locale");
 #endif
-    QString localeDir = qApp->applicationDirPath() + "/locale";
     if (!QDir(localeDir).exists()) {
-        localeDir = dataDir + "/locale";
+        localeDir = pkgDataDir + QLatin1String("/locale");
     }
-    // qDebug() << "Using locale dir" << localeDir << locale;
+    qDebug() << "Using locale dir" << localeDir << QLocale::system();
     QTranslator translator;
-    translator.load(QLocale::system(), localeDir);
+    translator.load(QLocale::system(), QString(), QString(), localeDir);
     app.installTranslator(&translator);
-    QTextCodec::setCodecForTr(QTextCodec::codecForName("utf8"));
 
-    MainWindow mainWin;
-    mainWin.setWindowTitle(Constants::NAME);
+    QNetworkProxyFactory::setUseSystemConfiguration(true);
 
-#ifndef Q_WS_X11
-    Extra::windowSetup(&mainWin);
-#else
-    mainWin.setProperty("style", app.property("style"));
-#endif
-
-// no window icon on Mac
-#ifndef APP_MAC
-    QIcon appIcon;
-    if (QDir(dataDir).exists()) {
-        appIcon = Utils::icon(Constants::UNIX_NAME);
-    } else {
-        dataDir = qApp->applicationDirPath() + "/data";
-        const int iconSizes [] = { 16, 22, 32, 48, 64, 128, 256, 512 };
-        for (int i = 0; i < 8; i++) {
-            QString size = QString::number(iconSizes[i]);
-            QString png = dataDir + "/" + size + "x" + size + "/" + Constants::UNIX_NAME + ".png";
-            appIcon.addFile(png, QSize(iconSizes[i], iconSizes[i]));
-        }
-    }
-    if (appIcon.isNull()) {
-        appIcon.addFile(":/images/app.png");
-    }
-    mainWin.setWindowIcon(appIcon);
-#endif
-
-    mainWin.connect(&app, SIGNAL(messageReceived(const QString &)), &mainWin, SLOT(messageReceived(const QString &)));
-    app.setActivationWindow(&mainWin, true);
-
-    // all string literals are UTF-8
-    // QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
-
-    if (app.arguments().size() > 1) {
-        QString query = app.arguments().at(1);
-        if (query.startsWith(QLatin1String("--"))) {
-            mainWin.messageReceived(query);
-            return 0;
-        } else {
-            SearchParams *searchParams = new SearchParams();
-            searchParams->setKeywords(query);
-            mainWin.showMedia(searchParams);
-        }
-    }
+    IconUtils::setSizes({16, 24, 32, 88, 128});
 
-    mainWin.show();
+    UpdateUtils::init();
 
-    // Seed random number generator
-    qsrand(QDateTime::currentDateTime().toTime_t());
+    showWindow(app, pkgDataDir);
 
     return app.exec();
 }