forked from KikoPlayProject/KikoPlay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobalobjects.cpp
194 lines (176 loc) · 6.49 KB
/
globalobjects.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include "globalobjects.h"
#include "Play/Danmu/danmupool.h"
#include "Play/Danmu/Render/danmurender.h"
#include "Play/Playlist/playlist.h"
#include "Play/Video/mpvplayer.h"
#include "Play/Danmu/blocker.h"
#include "Play/Danmu/danmuprovider.h"
#include "MediaLibrary/animeprovider.h"
#include "MediaLibrary/labelmodel.h"
#include "LANServer/lanserver.h"
#include "Download/downloadmodel.h"
#include "Play/Danmu/Manager/danmumanager.h"
#include "Script/scriptmanager.h"
#include "Download/autodownloadmanager.h"
#include "UI/stylemanager.h"
#include "Common/notifier.h"
#include "Common/logger.h"
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QApplication>
#include <QSqlError>
#include <QFileInfo>
MPVPlayer *GlobalObjects::mpvplayer=nullptr;
DanmuPool *GlobalObjects::danmuPool=nullptr;
DanmuRender *GlobalObjects::danmuRender=nullptr;
PlayList *GlobalObjects::playlist=nullptr;
Blocker *GlobalObjects::blocker=nullptr;
QThread *GlobalObjects::workThread=nullptr;
QSettings *GlobalObjects::appSetting=nullptr;
DanmuProvider *GlobalObjects::danmuProvider=nullptr;
AnimeProvider *GlobalObjects::animeProvider=nullptr;
DownloadModel *GlobalObjects::downloadModel=nullptr;
DanmuManager *GlobalObjects::danmuManager=nullptr;
LANServer *GlobalObjects::lanServer=nullptr;
ScriptManager *GlobalObjects::scriptManager=nullptr;
AutoDownloadManager *GlobalObjects::autoDownloadManager=nullptr;
QMainWindow *GlobalObjects::mainWindow=nullptr;
QFont* GlobalObjects::iconfont;
QString GlobalObjects::dataPath;
namespace {
const char *mt_db_names[]={"Comment_M", "Bangumi_M","Download_M"};
const char *wt_db_names[]={"Comment_W", "Bangumi_W","Download_W"};
}
constexpr const char *GlobalObjects::normalFont;
constexpr const char *GlobalObjects::kikoVersion;
void GlobalObjects::init()
{
dataPath = QCoreApplication::applicationDirPath() + "/data/";
#ifdef CONFIG_UNIX_DATA
const QFileInfo fileinfoConfig(QDir::homePath() + "/.config");
/* Test Linux/MacOS style environment */
if (fileinfoConfig.exists() || fileinfoConfig.isDir() || fileinfoConfig.isWritable()) {
dataPath = QDir::homePath() + "/.config/kikoplay/data/";
}
#endif
QDir dir;
if(!dir.exists(dataPath))
dir.mkpath(dataPath);
registerCustomSettingType();
appSetting=new QSettings(dataPath+"settings.ini",QSettings::IniFormat);
Logger::logger();
initDatabase(mt_db_names);
auto locNames = GlobalObjects::appSetting->value("KikoPlay/Language", "").toStringList();
if(locNames.join("").isEmpty()) {
locNames = QLocale().uiLanguages();
}
static QTranslator translator;
for(const auto &locName:locNames) {
auto loc = QLocale(locName);
if(loc.uiLanguages().first().startsWith("en-")) {
break;
}
if(translator.load(loc, "", "", ":/res/lang")) {
qApp->installTranslator(&translator);
break;
}
}
Notifier::getNotifier();
workThread=new QThread();
workThread->setObjectName(QStringLiteral("workThread"));
workThread->start(QThread::NormalPriority);
mpvplayer=new MPVPlayer();
danmuPool=new DanmuPool();
danmuRender=new DanmuRender();
QObject::connect(mpvplayer,&MPVPlayer::positionChanged, danmuPool,&DanmuPool::mediaTimeElapsed);
QObject::connect(mpvplayer,&MPVPlayer::positionJumped,danmuPool,&DanmuPool::mediaTimeJumped);
playlist=new PlayList();
QObject::connect(playlist, &PlayList::currentMatchChanged, danmuPool, &DanmuPool::setPoolID);
blocker=new Blocker();
QObject *workObj=new QObject();
workObj->moveToThread(workThread);
QMetaObject::invokeMethod(workObj,[workObj](){
initDatabase(wt_db_names);
workObj->deleteLater();
},Qt::QueuedConnection);
scriptManager=new ScriptManager();
danmuProvider=new DanmuProvider();
animeProvider=new AnimeProvider();
downloadModel=new DownloadModel();
danmuManager=new DanmuManager();
lanServer=new LANServer();
iconfont=new QFont();
autoDownloadManager=new AutoDownloadManager();
int fontId = QFontDatabase::addApplicationFont(":/res/iconfont.ttf");
QStringList fontFamilies = QFontDatabase::applicationFontFamilies(fontId);
iconfont->setFamily(fontFamilies.at(0));
QApplication::setFont(QFont("Microsoft Yahei UI", 10));
}
void GlobalObjects::clear()
{
workThread->quit();
workThread->wait();
mpvplayer->deleteLater();
danmuRender->deleteLater();
danmuPool->deleteLater();
playlist->deleteLater();
blocker->deleteLater();
danmuProvider->deleteLater();
animeProvider->deleteLater();
downloadModel->deleteLater();
danmuManager->deleteLater();
lanServer->deleteLater();
autoDownloadManager->deleteLater();
scriptManager->deleteLater();
appSetting->deleteLater();
}
QSqlDatabase GlobalObjects::getDB(int db)
{
if(QThread::currentThread()->objectName()==QStringLiteral("workThread"))
{
return QSqlDatabase::database(wt_db_names[db]);
}
return QSqlDatabase::database(mt_db_names[db]);
}
void GlobalObjects::initDatabase(const char *db_names[])
{
const int db_count=3;
const char *db_files[]={"comment","bangumi","download"};
for(int i=0;i<db_count;++i)
{
setDatabase(db_names[i],db_files[i]);
}
}
void GlobalObjects::setDatabase(const char *name, const char *file)
{
QSqlDatabase database = QSqlDatabase::addDatabase("QSQLITE",name);
QString dbFile(dataPath+file+".db");
bool dbFileExist = QFile::exists(dbFile);
database.setDatabaseName(dbFile);
database.open();
QSqlQuery query(database);
query.exec("PRAGMA foreign_keys = ON;");
if(!dbFileExist)
{
QFile sqlFile(QString(":/res/db/%1.sql").arg(file));
sqlFile.open(QFile::ReadOnly);
if(sqlFile.isOpen())
{
QStringList sqls=QString(sqlFile.readAll()).split(';', Qt::SkipEmptyParts);
for(const QString &sql:sqls)
{
query.exec(sql);
}
}
}
}
void GlobalObjects::registerCustomSettingType()
{
using ShortCutInfo = QPair<QString, QPair<QString,QString>>;
qRegisterMetaType<QList<ShortCutInfo>>("QList<ShortCutInfo>");
qRegisterMetaTypeStreamOperators<QList<ShortCutInfo>>("QList<ShortCutInfo>");
qRegisterMetaType<QVector<QPair<QString, QString>>>("QVector<QPair<QString, QString>>");
qRegisterMetaTypeStreamOperators<QVector<QPair<QString, QString>>>("QVector<QPair<QString, QString>>");
qRegisterMetaType<QVector<QStringList>>("QVector<QStringList>");
qRegisterMetaTypeStreamOperators<QVector<QStringList>>("QVector<QStringList>");
}