Clementine-audio-player-Mac.../src/main.cpp

225 lines
6.9 KiB
C++
Raw Normal View History

/* This file is part of Clementine.
Copyright 2010, David Sansome <me@davidsansome.com>
Clementine 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.
Clementine 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 Clementine. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef Q_OS_WIN32
# define _WIN32_WINNT 0x0500
# include <windows.h>
# include <iostream>
#endif // Q_OS_WIN32
#include "config.h"
#include "core/commandlineoptions.h"
2010-08-03 21:29:49 +02:00
#include "core/encoding.h"
#include "core/mac_startup.h"
#include "core/network.h"
#include "core/player.h"
#include "core/potranslator.h"
#include "core/song.h"
#include "engines/enginebase.h"
#include "library/directory.h"
#include "radio/lastfmservice.h"
#include "ui/equalizer.h"
#include "ui/iconloader.h"
#include "ui/mainwindow.h"
2009-12-24 20:16:07 +01:00
#include "qtsingleapplication.h"
#include "qtsinglecoreapplication.h"
2010-01-17 17:10:26 +01:00
#include <QtDebug>
2010-02-23 19:33:09 +01:00
#include <QLibraryInfo>
#include <QTranslator>
#include <QDir>
2010-03-24 22:34:32 +01:00
#include <glib-object.h>
#include <glib/gutils.h>
#include <echonest/Config.h>
2010-03-24 22:34:32 +01:00
#ifdef Q_WS_X11
# include <QDBusConnection>
# include <QDBusMetaType>
# include "core/mpris.h"
# include "widgets/osd.h"
2010-03-24 22:34:32 +01:00
#endif
2009-12-24 20:16:07 +01:00
#ifdef HAVE_GSTREAMER
# include <gst/gst.h>
class GstEnginePipeline;
#endif
// Load sqlite plugin on windows and mac.
#ifdef HAVE_STATIC_SQLITE
# include <QtPlugin>
Q_IMPORT_PLUGIN(qsqlite)
#endif
void LoadTranslation(const QString& prefix, const QString& path,
const QString& override_language = QString()) {
2010-03-25 13:57:11 +01:00
#if QT_VERSION < 0x040700
// QTranslator::load will try to open and read "clementine" if it exists,
// without checking if it's a file first.
2010-03-25 13:57:11 +01:00
// This was fixed in Qt 4.7
QFileInfo maybe_clementine_directory(path + "/clementine");
if (maybe_clementine_directory.exists() && !maybe_clementine_directory.isFile())
return;
2010-03-25 13:57:11 +01:00
#endif
QString language = override_language.isEmpty() ?
QLocale::system().name() : override_language;
QTranslator* t = new PoTranslator;
if (t->load(prefix + "_" + language, path))
QCoreApplication::installTranslator(t);
else
delete t;
}
2010-07-25 03:44:46 +02:00
void GLog(const gchar* domain,
GLogLevelFlags level,
const gchar* message,
gpointer user_data) {
qDebug() << "GLOG" << message;
}
2009-12-24 20:16:07 +01:00
int main(int argc, char *argv[]) {
#ifdef Q_OS_DARWIN
// Do Mac specific startup to get media keys working.
// This must go before QApplication initialisation.
mac::MacMain();
#endif
2010-01-08 17:21:22 +01:00
QCoreApplication::setApplicationName("Clementine");
QCoreApplication::setApplicationVersion(CLEMENTINE_VERSION_STRING);
2010-01-08 17:21:22 +01:00
QCoreApplication::setOrganizationName("Clementine");
2009-12-24 20:16:07 +01:00
QCoreApplication::setOrganizationDomain("davidsansome.com");
// This makes us show up nicely in gnome-volume-control
g_type_init();
g_set_application_name(QCoreApplication::applicationName().toLocal8Bit());
2010-07-25 03:44:46 +02:00
g_log_set_default_handler(&GLog, NULL);
qRegisterMetaType<Directory>("Directory");
2009-12-24 20:16:07 +01:00
qRegisterMetaType<DirectoryList>("DirectoryList");
qRegisterMetaType<Subdirectory>("Subdirectory");
qRegisterMetaType<SubdirectoryList>("SubdirectoryList");
2009-12-24 20:16:07 +01:00
qRegisterMetaType<SongList>("SongList");
qRegisterMetaType<PlaylistItemList>("PlaylistItemList");
2010-04-04 22:45:03 +02:00
qRegisterMetaType<Engine::State>("Engine::State");
qRegisterMetaType<Engine::SimpleMetaBundle>("Engine::SimpleMetaBundle");
2010-04-07 19:39:07 +02:00
qRegisterMetaType<Equalizer::Params>("Equalizer::Params");
qRegisterMetaTypeStreamOperators<Equalizer::Params>("Equalizer::Params");
qRegisterMetaType<const char*>("const char*");
qRegisterMetaType<QNetworkReply*>("QNetworkReply*");
qRegisterMetaType<QNetworkReply**>("QNetworkReply**");
2009-12-24 20:16:07 +01:00
#ifdef HAVE_GSTREAMER
qRegisterMetaType<GstBuffer*>("GstBuffer*");
qRegisterMetaType<GstElement*>("GstElement*");
qRegisterMetaType<GstEnginePipeline*>("GstEnginePipeline*");
#endif
lastfm::ws::ApiKey = LastFMService::kApiKey;
lastfm::ws::SharedSecret = LastFMService::kSecret;
// Parse commandline options - need to do this before starting the
// QApplication so it works without an X server
CommandlineOptions options(argc, argv);
if (!options.Parse())
return 1;
{
// Only start a core application now so we can check if there's another
// Clementine running without needing an X server.
QtSingleCoreApplication a(argc, argv);
if (a.isRunning()) {
if (options.is_empty()) {
qDebug() << "Clementine is already running - activating existing window";
}
if (a.sendMessage(options.Serialize(), 5000)) {
return 0;
}
// Couldn't send the message so start anyway
}
}
// Detect technically invalid usage of non-ASCII in ID3v1 tags.
UniversalEncodingHandler handler;
TagLib::ID3v1::Tag::setStringHandler(&handler);
2010-01-17 17:10:26 +01:00
QtSingleApplication a(argc, argv);
#ifdef Q_OS_DARWIN
QCoreApplication::setLibraryPaths(
QStringList() << QCoreApplication::applicationDirPath() + "/../PlugIns");
#endif
a.setQuitOnLastWindowClosed(false);
a.isRunning(); // Otherwise QtLocalPeer won't lock the lockfile :S
2010-01-17 17:10:26 +01:00
2010-08-26 14:25:31 +02:00
#ifndef Q_OS_DARWIN
// Gnome on Ubuntu has menu icons disabled by default. I think that's a bad
// idea, and makes some menus in Clementine look confusing.
QCoreApplication::setAttribute(Qt::AA_DontShowIconsInMenus, false);
2010-08-26 14:25:31 +02:00
#else
QCoreApplication::setAttribute(Qt::AA_DontShowIconsInMenus, true);
#endif
// Resources
Q_INIT_RESOURCE(data);
Q_INIT_RESOURCE(translations);
// Has the user forced a different language?
QString language = options.language();
if (language.isEmpty()) {
QSettings s;
s.beginGroup("General");
language = s.value("language").toString();
}
2010-02-23 19:33:09 +01:00
// Translations
LoadTranslation("qt", QLibraryInfo::location(QLibraryInfo::TranslationsPath), language);
LoadTranslation("clementine", ":/translations", language);
LoadTranslation("clementine", a.applicationDirPath(), language);
LoadTranslation("clementine", QDir::currentPath(), language);
// Icons
IconLoader::Init();
Echonest::Config::instance()->setAPIKey("DFLFLJBUF4EGTXHIG");
Echonest::Config::instance()->setNetworkAccessManager(new NetworkAccessManager);
2010-03-24 22:07:16 +01:00
// MPRIS DBus interface.
#ifdef Q_WS_X11
qDBusRegisterMetaType<DBusStatus>();
qDBusRegisterMetaType<Version>();
qDBusRegisterMetaType<QImage>();
QDBusConnection::sessionBus().registerService("org.mpris.clementine");
MPRIS mpris;
2010-03-24 22:07:16 +01:00
#endif
// Seed the random number generator
srand(time(NULL));
2010-02-23 19:33:09 +01:00
// Window
MainWindow w(options.engine());
2010-01-17 17:10:26 +01:00
QObject::connect(&a, SIGNAL(messageReceived(QByteArray)), &w, SLOT(CommandlineOptionsReceived(QByteArray)));
w.CommandlineOptionsReceived(options);
2009-12-24 20:16:07 +01:00
return a.exec();
}