]> git.sur5r.net Git - minitube/blob - src/main.cpp
91f2ed87e20b887956dfd0203395850c5add7780
[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 int main(int argc, char **argv) {
38
39 #ifdef Q_OS_MAC
40     mac::MacMain();
41     QFont::insertSubstitution(".Helvetica Neue DeskInterface", "Helvetica Neue");
42 #endif
43
44     QtSingleApplication app(argc, argv);
45     QString message = app.arguments().size() > 1 ? app.arguments().at(1) : QString();
46     if (message == QLatin1String("--help")) {
47         MainWindow::printHelp();
48         return 0;
49     }
50     if (app.sendMessage(message))
51         return 0;
52
53     app.setApplicationName(Constants::NAME);
54     app.setOrganizationName(Constants::ORG_NAME);
55     app.setOrganizationDomain(Constants::ORG_DOMAIN);
56     app.setApplicationVersion(Constants::VERSION);
57     app.setAttribute(Qt::AA_DontShowIconsInMenus);
58 #ifndef APP_WIN
59     app.setWheelScrollLines(1);
60 #endif
61
62 #ifdef APP_EXTRA
63     Extra::appSetup(&app);
64 #else
65     QFile cssFile(":/style.css");
66     cssFile.open(QFile::ReadOnly);
67     QString styleSheet = QLatin1String(cssFile.readAll());
68     app.setStyleSheet(styleSheet);
69 #endif
70
71     // qt translations
72     QTranslator qtTranslator;
73     qtTranslator.load("qt_" + QLocale::system().name(),
74                       QLibraryInfo::location(QLibraryInfo::TranslationsPath));
75     app.installTranslator(&qtTranslator);
76
77     // app translations
78 #ifdef PKGDATADIR
79     QString dataDir = QLatin1String(PKGDATADIR);
80 #else
81     QString dataDir = "";
82 #endif
83 #ifdef APP_MAC
84     QString localeDir = qApp->applicationDirPath() + "/../Resources/locale";
85 #else
86     QString localeDir = qApp->applicationDirPath() + "/locale";
87 #endif
88     if (!QDir(localeDir).exists()) {
89         localeDir = dataDir + "/locale";
90     }
91     // qDebug() << "Using locale dir" << localeDir << locale;
92     QTranslator translator;
93     translator.load(QLocale::system(), QString(), QString(), localeDir);
94     app.installTranslator(&translator);
95 #if QT_VERSION < 0x050000
96     QTextCodec::setCodecForTr(QTextCodec::codecForName("utf8"));
97 #endif
98
99     MainWindow mainWin;
100     mainWin.show();
101
102     // no window icon on Mac
103 #ifndef APP_MAC
104     QIcon appIcon;
105     if (QDir(dataDir).exists()) {
106         appIcon = IconUtils::icon(Constants::UNIX_NAME);
107     } else {
108         dataDir = qApp->applicationDirPath() + "/data";
109         const int iconSizes [] = { 16, 22, 32, 48, 64, 128, 256, 512 };
110         for (int i = 0; i < 8; i++) {
111             QString size = QString::number(iconSizes[i]);
112             QString png = dataDir + "/" + size + "x" + size + "/" +
113                     Constants::UNIX_NAME + ".png";
114             appIcon.addFile(png, QSize(iconSizes[i], iconSizes[i]));
115         }
116     }
117     if (appIcon.isNull()) {
118         appIcon.addFile(":/images/app.png");
119     }
120     mainWin.setWindowIcon(appIcon);
121 #endif
122
123     mainWin.connect(&app, SIGNAL(messageReceived(const QString &)),
124                     &mainWin, SLOT(messageReceived(const QString &)));
125     app.setActivationWindow(&mainWin, true);
126
127     // all string literals are UTF-8
128     // QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
129
130     // Seed random number generator
131     qsrand(QDateTime::currentDateTime().toTime_t());
132
133     return app.exec();
134 }