]> git.sur5r.net Git - minitube/blob - src/main.cpp
Imported Upstream version 2.4
[minitube] / src / main.cpp
1 /* $BEGIN_LICENSE
2
3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
5
6 Minitube is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 Minitube is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Minitube.  If not, see <http://www.gnu.org/licenses/>.
18
19 $END_LICENSE */
20
21 #include <QtGui>
22 #if QT_VERSION >= 0x050000
23 #include <QtWidgets>
24 #endif
25 #include <qtsingleapplication.h>
26 #include "constants.h"
27 #include "mainwindow.h"
28 #include "searchparams.h"
29 #include "iconutils.h"
30 #ifdef APP_EXTRA
31 #include "extra.h"
32 #endif
33 #ifdef Q_OS_MAC
34 #include "mac_startup.h"
35 #endif
36
37 void showWindow(QtSingleApplication &app, const QString &dataDir) {
38     MainWindow *mainWin = new MainWindow();
39     mainWin->show();
40
41 #ifndef APP_MAC
42     QIcon appIcon;
43     if (QDir(dataDir).exists()) {
44         appIcon = IconUtils::icon(Constants::UNIX_NAME);
45     } else {
46         QString dataDir = qApp->applicationDirPath() + "/data";
47         const int iconSizes [] = { 16, 22, 32, 48, 64, 128, 256, 512 };
48         for (int i = 0; i < 8; i++) {
49             QString size = QString::number(iconSizes[i]);
50             QString png = dataDir + "/" + size + "x" + size + "/" + Constants::UNIX_NAME + ".png";
51             appIcon.addFile(png, QSize(iconSizes[i], iconSizes[i]));
52         }
53     }
54     if (appIcon.isNull()) appIcon.addFile(":/images/app.png");
55     mainWin->setWindowIcon(appIcon);
56 #endif
57
58     mainWin->connect(&app, SIGNAL(messageReceived(const QString &)),
59                     mainWin, SLOT(messageReceived(const QString &)));
60     app.setActivationWindow(mainWin, true);
61 }
62
63 int main(int argc, char **argv) {
64
65 #ifdef Q_OS_MAC
66     mac::MacMain();
67     // QFont::insertSubstitution(".Helvetica Neue DeskInterface", "Helvetica Neue");
68 #endif
69
70     QtSingleApplication app(argc, argv);
71     QString message = app.arguments().size() > 1 ? app.arguments().at(1) : QString();
72     if (message == QLatin1String("--help")) {
73         MainWindow::printHelp();
74         return 0;
75     }
76     if (app.sendMessage(message))
77         return 0;
78
79     app.setApplicationName(Constants::NAME);
80     app.setOrganizationName(Constants::ORG_NAME);
81     app.setOrganizationDomain(Constants::ORG_DOMAIN);
82     app.setApplicationVersion(Constants::VERSION);
83     app.setAttribute(Qt::AA_DontShowIconsInMenus);
84 #ifndef APP_WIN
85     app.setWheelScrollLines(1);
86 #endif
87
88 #ifdef APP_EXTRA
89     Extra::appSetup(&app);
90 #else
91     QFile cssFile(":/style.css");
92     cssFile.open(QFile::ReadOnly);
93     QString styleSheet = QLatin1String(cssFile.readAll());
94     app.setStyleSheet(styleSheet);
95 #endif
96
97     // qt translations
98     QTranslator qtTranslator;
99     qtTranslator.load("qt_" + QLocale::system().name(),
100                       QLibraryInfo::location(QLibraryInfo::TranslationsPath));
101     app.installTranslator(&qtTranslator);
102
103     // app translations
104 #ifdef PKGDATADIR
105     QString dataDir = QLatin1String(PKGDATADIR);
106 #else
107     QString dataDir = "";
108 #endif
109 #ifdef APP_MAC
110     QString localeDir = qApp->applicationDirPath() + "/../Resources/locale";
111 #else
112     QString localeDir = qApp->applicationDirPath() + "/locale";
113 #endif
114     if (!QDir(localeDir).exists()) {
115         localeDir = dataDir + "/locale";
116     }
117     // qDebug() << "Using locale dir" << localeDir << locale;
118     QTranslator translator;
119     translator.load(QLocale::system(), QString(), QString(), localeDir);
120     app.installTranslator(&translator);
121 #if QT_VERSION < 0x050000
122     QTextCodec::setCodecForTr(QTextCodec::codecForName("utf8"));
123 #endif
124
125     // Seed random number generator
126     qsrand(QDateTime::currentDateTime().toTime_t());
127
128     // all string literals are UTF-8
129     // QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
130
131 #ifdef APP_INTEGRITY
132     if (Extra::integrityCheck())
133 #endif
134         showWindow(app, dataDir);
135
136     return app.exec();
137 }