-
Notifications
You must be signed in to change notification settings - Fork 2
/
mthemedaemon.cpp
453 lines (374 loc) · 15.1 KB
/
mthemedaemon.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/***************************************************************************
**
** Copyright (C) 2010, 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/
#include "mthemedaemon.h"
#include <QSvgRenderer>
#include <QPixmap>
#include <QDir>
#include <QSettings>
#include <QTimer>
#include <QColor>
#include <QCoreApplication>
#include <QStringList>
#include <MGConfItem>
#include <QPainter>
#include <QDesktopServices>
#include "mthemedaemonclient.h"
#include "mimagedirectory.h"
#include "mthemedaemonprotocol.h"
#include "mpixmaphandle.h"
#include "msystemdirectories.h"
using namespace M::MThemeDaemonProtocol;
#ifndef NOIMAGEDIRECTORIES
bool MThemeDaemon::printGraphicalFiles(false);
MThemeDaemon::MThemeDaemon(Type type) :
mostUsedPixmaps(this, type),
type(type)
{
// check that the base theme ("fallback") directory exists.
QDir baseThemeDir(systemThemeDirectory() + QDir::separator() + "base" + QDir::separator() +
"meegotouch" + QDir::separator() + "icons");
if (!baseThemeDir.exists()) {
// we don't have the base theme ("fallback") directory, so exit!
qFatal("MThemeDaemon: base theme not found: %s. Please install a base theme package (like nemo-theme-default)",
qPrintable(baseThemeDir.absolutePath()));
}
}
MThemeDaemon::~MThemeDaemon()
{
if (type == RemoteDaemon) {
mostUsedPixmaps.save();
mostUsedPixmaps.clear();
}
qDeleteAll(themeImageDirs);
themeImageDirs.clear();
}
#else
MThemeDaemon::MThemeDaemon( )
{
}
#endif
QString MThemeDaemon::systemThemeDirectory()
{
#ifdef Q_OS_WIN
QDir appDir(QCoreApplication::applicationDirPath());
appDir.cdUp();
appDir.cd("share");
appDir.cd("themes");
return appDir.absolutePath();
#else
return "/usr/share/themes";
#endif
}
QString MThemeDaemon::systemThemeCacheDirectory()
{
return MSystemDirectories::cacheDirectory() + QLatin1String("themedaemon") + QDir::separator();
}
#ifndef NOIMAGEDIRECTORIES
void MThemeDaemon::addClient(MThemeDaemonClient *client)
{
Q_UNUSED(client);
}
void MThemeDaemon::removeClient(MThemeDaemonClient *client)
{
// release all pixmaps
while (client->pixmaps.count() > 0) {
PixmapIdentifier id = client->pixmaps.begin().key();
releasePixmap(client, id);
}
}
bool MThemeDaemon::pixmap(MThemeDaemonClient *client, const PixmapIdentifier &id, MPixmapHandle *handle)
{
// has the client already requested this pixmap?
if (client->pixmaps.contains(id)) {
qWarning() << Q_FUNC_INFO << " The requested pixmap" << id.imageId << "is already loaded for the client" << client->name();
return false;
}
// try to find the image resource from the client specific paths
ImageResource *resource = client->findImageResource(id.imageId);
if (!resource) {
// application paths didn't contain the image resource, now check from theme
resource = findImageResource(id.imageId);
if (!resource) {
qWarning() << Q_FUNC_INFO << "[MISS] The requested pixmap" << id.imageId << "was not found for client" << client->name();
client->pixmaps.insert(id, NULL);
*handle = MPixmapHandle();
return true;
} else if (type == RemoteDaemon) {
mostUsedPixmaps.increaseRequestCount(id, resource);
}
}
if (printGraphicalFiles) {
qWarning() << Q_FUNC_INFO << qPrintable(QString("%1: ID: %2 file: %3").arg(client->name()).arg(id.imageId).arg(resource->absoluteFilePath()));
}
// save the information that this client has a reference of this pixmap
client->pixmaps.insert(id, resource);
// we have found the correct image resource.
*handle = resource->fetchPixmap(id.size);
return true;
}
bool MThemeDaemon::releasePixmap(MThemeDaemonClient *client, const PixmapIdentifier &id)
{
QHash<PixmapIdentifier, ImageResource *>::iterator i = client->pixmaps.find(id);
if (i == client->pixmaps.end()) {
// the client has no reference to this pixmap
qWarning() << Q_FUNC_INFO << " Pixmap" << id.imageId << "was not loaded by the client" << client->name();
return false;
}
ImageResource *resource = i.value();
client->pixmaps.erase(i);
if (resource == NULL) {
// the pixmap was an invalid pixmap, no need to release anything
return true;
}
// find a cache entry for the pixmap to be released.
resource->releasePixmap(id.size);
return true;
}
QString MThemeDaemon::currentTheme() const
{
return currentThemeName;
}
QStringList MThemeDaemon::themeInheritanceChain() const
{
return themeInheritance;
}
QStringList MThemeDaemon::themeLibraryNames() const
{
return themeLibraries;
}
ImageResource *MThemeDaemon::findImageResource(const QString &imageId)
{
ImageResource *resource = NULL;
// try to find the image first from current theme,
// then from theme that the current one is inherited from, and so on.
foreach(MThemeImagesDirectory * imdir, themeImageDirs) {
resource = imdir->findImage(imageId);
if (resource)
break;
}
return resource;
}
void MThemeDaemon::reloadImagePaths(const QString &locale, bool forceReload)
{
QList<MThemeImagesDirectory*> newThemeImageDirs;
foreach(const QString & theme, themeInheritance) {
const QString path = theme + "meegotouch";
bool foundDir = false;
if (!forceReload) {
// Creating a MThemeImagesDirectory is an expensive operation.
// Check whether there is already a MThemeImagesDirectory instance
// that can be reused.
for (int i = 0; i < themeImageDirs.size(); ++i) {
foundDir = (themeImageDirs[i]->path() == path) && (themeImageDirs[i]->locale() == locale);
if (foundDir) {
newThemeImageDirs.append(themeImageDirs[i]);
themeImageDirs[i] = 0;
break;
}
}
}
if (!foundDir) {
// No existing MThemeImagesDirectory could be reused, create a new one
newThemeImageDirs.append(new MThemeImagesDirectory(path, locale));
}
}
qDeleteAll(themeImageDirs);
themeImageDirs = newThemeImageDirs;
}
const QSettings *themeFile(const QString &theme)
{
// Determine whether this is a m theme:
// step 1: we need to have index.theme file
const QString themeIndexFileName = MThemeDaemon::systemThemeDirectory() + QDir::separator() + theme + QDir::separator() + "index.theme";
if (!QFile::exists(themeIndexFileName))
return NULL;
// step 2: it needs to be a valid ini file
const QSettings *themeIndexFile = new QSettings(themeIndexFileName, QSettings::IniFormat);
if (themeIndexFile->status() != QSettings::NoError) {
delete themeIndexFile;
return NULL;
}
// step 3: we need to have X-MeeGoTouch-Metatheme group in index.theme
// here we accept also the old X-DUI-Metatheme, because the old and new files
// are in the same directory. remove "X-DUI-Metatheme" when libdui 0.19 and
// duitheme with it is phased out.
if ( (!themeIndexFile->childGroups().contains(QString("X-DUI-Metatheme")))
&& (!themeIndexFile->childGroups().contains(QString("X-MeeGoTouch-Metatheme"))) )
{
delete themeIndexFile;
return NULL;
}
return themeIndexFile;
}
bool MThemeDaemon::activateTheme(const QString &newTheme, const QString &locale, const QList<MThemeDaemonClient *>& clientList, QList<PixmapCacheEntry*>& pixmapsToDelete, bool forceReload)
{
if (!forceReload && newTheme == currentThemeName) {
// TODO: check need for warning
return false;
}
QDir dir(systemThemeDirectory() + QDir::separator() + newTheme);
if (!dir.exists()) {
qWarning() << Q_FUNC_INFO << "Theme" << newTheme << "does not exist! Not changing theme";
return false;
}
#ifdef MTHEME_PRINT_DEBUG
qDebug() << Q_FUNC_INFO << "Changing theme from" << currentThemeName << "to" << newTheme;
#endif
// Change the theme
// 1: find out the inheritance chain for the new theme
QString tmpTheme = newTheme;
QStringList newThemeInheritanceChain;
QSet<QString> themeLibraryNames;
while (true) {
const QSettings *themeIndexFile = themeFile(tmpTheme);
if (!themeIndexFile) {
// TODO: print out warning, theme inheritance contains invalid theme
return false;
}
newThemeInheritanceChain.append(tmpTheme);
QString parentTheme = themeIndexFile->value("X-MeeGoTouch-Metatheme/X-Inherits", "").toString();
// remove when duitheme is phased out
if ( parentTheme.isEmpty() )
{
parentTheme = themeIndexFile->value("X-DUI-Metatheme/X-Inherits", "").toString();
}
// end remove
QStringList libraryNames = themeIndexFile->value("X-MeeGoTouch-Metatheme/X-Libraries", "").toString().split(",", QString::SkipEmptyParts);
// remove when duitheme is phased out
if ( libraryNames.isEmpty() )
{
libraryNames = themeIndexFile->value("X-DUI-Metatheme/X-Libraries", "")
.toString().split(",", QString::SkipEmptyParts);
}
// end remove
foreach(const QString& libname, libraryNames) {
themeLibraryNames.insert(libname.trimmed());
}
delete themeIndexFile;
if (parentTheme.isEmpty()) {
break;
}
tmpTheme = parentTheme;
// check that there is no cyclic dependencies
foreach(const QString & themeName, newThemeInheritanceChain) {
if (tmpTheme == themeName) {
qWarning() << Q_FUNC_INFO << "Cyclic dependency in theme:" << newTheme;
return false;
}
}
}
themeLibraries = themeLibraryNames.toList();
themeInheritance = newThemeInheritanceChain;
QString linkToCurrentTheme = MThemeDaemon::systemThemeCacheDirectory() + QDir::separator() + "currentTheme";
QFile::remove(linkToCurrentTheme);
QFile::link(QString("/usr/share/themes") + QDir::separator() + themeInheritance.first(), linkToCurrentTheme);
#ifdef MTHEME_PRINT_DEBUG
qDebug() << Q_FUNC_INFO << " New theme inheritance chain is" << themeInheritance;
#endif
// include the path to theme inheritance chain
for (int i = 0; i != themeInheritance.count(); ++i) {
themeInheritance[i] = systemThemeDirectory() + QDir::separator() + themeInheritance[i] + QDir::separator();
}
// 2. save the most used list for the old theme
if (!currentThemeName.isEmpty() && type == RemoteDaemon) {
mostUsedPixmaps.save();
}
// 3. release all most used pixmaps
if (type == RemoteDaemon) {
mostUsedPixmaps.clear();
}
currentThemeName = newTheme;
// 4: go trough all clients and release all pixmaps + put them into load queue
// plus send a packet for all clients, so they can start releasing and requesting new pixmaps, (so the daemon wouldn't need to reload all pixmaps that aren't needed anymore)
foreach(MThemeDaemonClient * client, clientList) {
QHash<PixmapIdentifier, ImageResource *>::iterator i;
while(!client->pixmaps.isEmpty()) {
i = client->pixmaps.begin();
ImageResource* resource = i.value();
PixmapIdentifier id = i.key();
client->pixmapsToReload.append(id);
client->pixmaps.erase(i);
if(resource != NULL) {
PixmapCacheEntry *cacheEntry = resource->releaseWithoutDelete(id.size);
if(cacheEntry != NULL) {
pixmapsToDelete.append(cacheEntry);
}
}
}
client->reloadImagePaths(themeInheritance);
}
// 5: release rest of the old theme resources and load the new ones
reloadImagePaths(locale, forceReload);
// 6. load the "preload" list
if (type == RemoteDaemon) {
mostUsedPixmaps.load();
}
return true;
}
void MThemeDaemon::changeLocale(const QString &newLocale, const QList<MThemeDaemonClient *>& clientList, QHash<MThemeDaemonClient *, QList<PixmapIdentifier> >& pixmapsToReload)
{
QList<QPair<MThemeDaemonClient *, QList<QPair<PixmapIdentifier, ImageResource *> > > > potentialIconsFromClients;
foreach(MThemeDaemonClient * client, clientList) {
QHash<PixmapIdentifier, ImageResource *>::iterator i = client->pixmaps.begin();
QHash<PixmapIdentifier, ImageResource *>::iterator end = client->pixmaps.end();
QList<QPair<PixmapIdentifier, ImageResource *> > potentialReloadlist;
QList<PixmapIdentifier> pixmapsToBeReloaded;
// get all icons from this client
for (; i != end; ++i) {
bool reload = false;
foreach(MThemeImagesDirectory * imdir, themeImageDirs) {
if (imdir->isLocalizedResource(i.key().imageId)) {
reload = true;
break;
}
}
if (reload) {
// this image was locale specific icon so mark it to be reloaded
pixmapsToBeReloaded.append(i.key());
if (type == RemoteDaemon) {
mostUsedPixmaps.reload(i.key(), i.value());
}
} else {
// this image was icon but not locale specific so mark it as potential
potentialReloadlist.append(qMakePair(i.key(), i.value()));
}
}
// release all old locale specific icons
foreach(const PixmapIdentifier & id, pixmapsToBeReloaded) {
releasePixmap(client, id);
}
pixmapsToReload.insert(client, pixmapsToBeReloaded);
potentialIconsFromClients.append(qMakePair(client, potentialReloadlist));
}
// load the new locale specific images
foreach(MThemeImagesDirectory * imdir, themeImageDirs) {
imdir->reloadLocalizedResources(newLocale);
}
//if new locale contains some new locale specific resources, reload them as well
typedef QPair<PixmapIdentifier, ImageResource *> pi_ir;
typedef QPair<MThemeDaemonClient *, QList<pi_ir> > c_pi_ir;
foreach(const c_pi_ir & potentialIconsFromClient, potentialIconsFromClients) {
foreach(const pi_ir & potentialIcon, potentialIconsFromClient.second) {
foreach(MThemeImagesDirectory * imdir, themeImageDirs) {
if (imdir->isLocalizedResource(potentialIcon.first.imageId)) {
releasePixmap(potentialIconsFromClient.first, potentialIcon.first);
if (type == RemoteDaemon) {
mostUsedPixmaps.reload(potentialIcon.first, potentialIcon.second);
}
pixmapsToReload[potentialIconsFromClient.first].append(potentialIcon.first);
break;
}
}
}
}
}
#endif