]> git.sur5r.net Git - minitube/blob - src/main.cpp
Imported Upstream version 2.0
[minitube] / src / main.cpp
1 #include <QtGui>
2 #include <qtsingleapplication.h>
3 #include "constants.h"
4 #include "mainwindow.h"
5 #include "searchparams.h"
6 #include "utils.h"
7 #ifndef Q_WS_X11
8 #include "extra.h"
9 #endif
10 #ifdef Q_WS_MAC
11 #include "mac_startup.h"
12 #endif
13
14 #ifdef Q_WS_X11
15 QString getThemeName() {
16     QString themeName;
17
18     QProcess process;
19     process.start("dconf",
20                   QStringList() << "read" << "/org/gnome/desktop/interface/gtk-theme");
21     if (process.waitForFinished()) {
22         themeName = process.readAllStandardOutput();
23         themeName = themeName.trimmed();
24         themeName.remove('\'');
25         if (!themeName.isEmpty()) return themeName;
26     }
27
28     QString rcPaths = QString::fromLocal8Bit(qgetenv("GTK2_RC_FILES"));
29     if (!rcPaths.isEmpty()) {
30         QStringList paths = rcPaths.split(QLatin1String(":"));
31         foreach (const QString &rcPath, paths) {
32             if (!rcPath.isEmpty()) {
33                 QFile rcFile(rcPath);
34                 if (rcFile.exists() && rcFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
35                     QTextStream in(&rcFile);
36                     while(!in.atEnd()) {
37                         QString line = in.readLine();
38                         if (line.contains(QLatin1String("gtk-theme-name"))) {
39                             line = line.right(line.length() - line.indexOf(QLatin1Char('=')) - 1);
40                             line.remove(QLatin1Char('\"'));
41                             line = line.trimmed();
42                             themeName = line;
43                             break;
44                         }
45                     }
46                 }
47             }
48             if (!themeName.isEmpty())
49                 break;
50         }
51     }
52
53     // Fall back to gconf
54     if (themeName.isEmpty())
55         themeName = QGtkStyle::getGConfString(QLatin1String("/desktop/gnome/interface/gtk_theme"));
56
57     return themeName;
58 }
59 #endif
60
61 int main(int argc, char **argv) {
62
63 #ifdef Q_WS_MAC
64     mac::MacMain();
65 #endif
66
67     QtSingleApplication app(argc, argv);
68     QString message = app.arguments().size() > 1 ? app.arguments().at(1) : QString();
69     if (message == QLatin1String("--help")) {
70         MainWindow::printHelp();
71         return 0;
72     }
73     if (app.sendMessage(message))
74         return 0;
75
76     app.setApplicationName(QLatin1String(Constants::NAME));
77     app.setOrganizationName(QLatin1String(Constants::ORG_NAME));
78     app.setOrganizationDomain(QLatin1String(Constants::ORG_DOMAIN));
79     app.setWheelScrollLines(1);
80     app.setAttribute(Qt::AA_DontShowIconsInMenus);
81
82 #ifndef Q_WS_X11
83     Extra::appSetup(&app);
84 #else
85     bool isGtk = app.style()->metaObject()->className() == QLatin1String("QGtkStyle");
86     if (isGtk) {
87         app.setProperty("gtk", isGtk);
88         QString themeName = getThemeName();
89         app.setProperty("style", themeName);
90     }
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     QString localeDir = qApp->applicationDirPath() + "/locale";
110     if (!QDir(localeDir).exists()) {
111         localeDir = dataDir + "/locale";
112     }
113     // qDebug() << "Using locale dir" << localeDir << locale;
114     QTranslator translator;
115     translator.load(QLocale::system(), localeDir);
116     app.installTranslator(&translator);
117     QTextCodec::setCodecForTr(QTextCodec::codecForName("utf8"));
118
119     MainWindow mainWin;
120     mainWin.setWindowTitle(Constants::NAME);
121
122 #ifndef Q_WS_X11
123     Extra::windowSetup(&mainWin);
124 #else
125     mainWin.setProperty("style", app.property("style"));
126 #endif
127
128 // no window icon on Mac
129 #ifndef APP_MAC
130     QIcon appIcon;
131     if (QDir(dataDir).exists()) {
132         appIcon = Utils::icon(Constants::UNIX_NAME);
133     } else {
134         dataDir = qApp->applicationDirPath() + "/data";
135         const int iconSizes [] = { 16, 22, 32, 48, 64, 128, 256, 512 };
136         for (int i = 0; i < 8; i++) {
137             QString size = QString::number(iconSizes[i]);
138             QString png = dataDir + "/" + size + "x" + size + "/" + Constants::UNIX_NAME + ".png";
139             appIcon.addFile(png, QSize(iconSizes[i], iconSizes[i]));
140         }
141     }
142     if (appIcon.isNull()) {
143         appIcon.addFile(":/images/app.png");
144     }
145     mainWin.setWindowIcon(appIcon);
146 #endif
147
148     mainWin.connect(&app, SIGNAL(messageReceived(const QString &)), &mainWin, SLOT(messageReceived(const QString &)));
149     app.setActivationWindow(&mainWin, true);
150
151     // all string literals are UTF-8
152     // QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
153
154     if (app.arguments().size() > 1) {
155         QString query = app.arguments().at(1);
156         if (query.startsWith(QLatin1String("--"))) {
157             mainWin.messageReceived(query);
158             return 0;
159         } else {
160             SearchParams *searchParams = new SearchParams();
161             searchParams->setKeywords(query);
162             mainWin.showMedia(searchParams);
163         }
164     }
165
166     mainWin.show();
167
168     // Seed random number generator
169     qsrand(QDateTime::currentDateTime().toTime_t());
170
171     return app.exec();
172 }