OSDN Git Service

df5e802a196e0e0b666d6ceff171832ad53fcbfa
[fontmanager/fontmanager.git] / applicationcontroller.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Takumi Asaki
4 ** All rights reserved.
5 ** Contact: Takumi Asaki (takumi.asaki@gmail.com)
6 **
7 ** This file is part of the fontmanager application.
8 **
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 **   * Redistributions of source code must retain the above copyright
15 **     notice, this list of conditions and the following disclaimer.
16 **   * Redistributions in binary form must reproduce the above copyright
17 **     notice, this list of conditions and the following disclaimer in
18 **     the documentation and/or other materials provided with the
19 **     distribution.
20 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
21 **     the names of its contributors may be used to endorse or promote
22 **     products derived from this software without specific prior written
23 **     permission.
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ****************************************************************************/
38
39 #include "applicationcontroller.h"
40
41 #include "fontconfigdefs.h"
42 #include "fontconfigmanager.h"
43 #include "installedfontinfo.h"
44 #include "fontsconfeditorcontroller.h"
45 #include "fontsconf.h"
46
47 #include <QtCore>
48
49 ApplicationController::ApplicationController(QObject *parent) :
50     QObject(parent), mFontDirExists(false), mShowSystemFont(false),
51     mUpdating(0),
52     mForceOverwrite(false), mWorking(false), mIgnoreUpdate(false),
53     mFontConfig(0)
54 {
55
56 }
57
58 void ApplicationController::init()
59 {
60     mFontConfig = new FontConfigManager(this);
61     setFontDir(QDir::homePath() + QLatin1String("/.fonts"));
62
63     QLocale curLocale;
64     mLang = curLocale.name();
65     int idx = mLang.indexOf(QLatin1Char('_'));
66     if (idx > 0)
67         mLang = mLang.left(idx);
68     mFontConfig->setCurrentLanguage(mLang);
69
70     connect(mFontConfig, SIGNAL(fcCacheFinished()), mFontConfig, SLOT(readFcList()));
71     connect(mFontConfig, SIGNAL(fontListUpdated()), SLOT(readFcListFinished()));
72     connect(mFontConfig, SIGNAL(fontListUpdated()), SLOT(syncInstalledFonts()));
73
74     connect(mFontConfig, SIGNAL(localFontsConfPathChanged()), SIGNAL(localFontsConfPathChanged()));
75     connect(mFontConfig, SIGNAL(localFontsConfExistsChanged()), SIGNAL(localFontsConfExistsChanged()));
76
77     connect(mFontConfig, SIGNAL(startUpdateFontsConfig()), SLOT(startUpdateLocalFontsConf()));
78     connect(mFontConfig, SIGNAL(fontsConfUpdated()), SLOT(localFontsConfUpdated()));
79     connect(mFontConfig, SIGNAL(endUpdateFontsConfig()), SLOT(endUpdateLocalFontsConf()));
80
81     connect(this, SIGNAL(localFontsConfChanged()), SLOT(updateAllEditorController()));
82     connect(this, SIGNAL(localFontsConfChanged()), SLOT(saveFontsConf()));
83
84     connect(mFontConfig, SIGNAL(warning(QString)), SIGNAL(alertDialog(QString)));
85
86     mFontConfig->setLocalFontsConfPath(QDir::homePath() + QLatin1String("/.fonts.conf"));
87
88     mFontConfig->readFontsConf();
89     mFontConfig->readFcList();
90
91     foreach (const QString &f, FontsConf::genericFamilies()) {
92         updateEditorController(f);
93     }
94
95 }
96
97 QString ApplicationController::version() const
98 {
99     return QLatin1String("0.4.2");
100 }
101
102 QString ApplicationController::currentLanguage() const
103 {
104     return mLang;
105 }
106
107 QString ApplicationController::fontDir() const
108 {
109     return mFontDirPath;
110 }
111
112 void ApplicationController::setFontDir(const QString &dirpath)
113 {
114     if (mFontDirPath != dirpath) {
115         mFontDirPath = dirpath;
116         emit fontDirChanged(mFontDirPath);
117         QDir fontDir(dirpath);
118         mFontDirExists = fontDir.exists();
119         emit fontDirExistsChanged();
120         if (mFontConfig)
121             mFontConfig->setLocalFontPath(dirpath);
122     }
123 }
124
125 bool ApplicationController::fontDirExists() const
126 {
127     return mFontDirExists;
128 }
129
130 bool ApplicationController::showSystemFont() const
131 {
132     return mShowSystemFont;
133 }
134
135 void ApplicationController::setShowSystemFont(bool show)
136 {
137     if (mShowSystemFont != show) {
138         mShowSystemFont = show;
139         emit showSystemFontChanged();
140     }
141 }
142
143 FontInfo *ApplicationController::checkFontInfo(const QUrl &path)
144 {
145     FontInfo *fInfo = new FontInfo(path.toLocalFile(), mFontConfig, this);
146     return fInfo;
147 }
148
149 InstalledFontInfo *ApplicationController::fontInfo(const QString &family, const QString &fullname) const
150 {
151     return mFontConfig->fontInfo(family, fullname);
152 }
153
154 QStringList ApplicationController::fontCount(const QString &fontpath) const
155 {
156     return mFontConfig->fontCount(fontpath);
157 }
158
159 QString ApplicationController::localeFamily(const QString &family) const
160 {
161     return mFontConfig->localeFamily(family);
162 }
163
164 bool ApplicationController::fontExists(FontInfo *fontinfo)
165 {
166     QFileInfo srcfont(fontinfo->fontPath());
167     QFileInfo dstfont(mFontDirPath + QLatin1String("/") + srcfont.fileName());
168     return dstfont.exists();
169 }
170
171 FontsConfEditorController *ApplicationController::editorController(const QString &family)
172 {
173     FontsConfEditorController *controller = mEditorController.value(family, 0);
174     if (!controller) {
175         controller = new FontsConfEditorController(family, this);
176         mEditorController.insert(family, controller);
177         connect(controller, SIGNAL(appendFamilyToConfig(QString,QString,QString)), SLOT(appendFamilyToConfig(QString,QString,QString)));
178         connect(controller, SIGNAL(removeFamilyFromList(QString,QString,QString)), SLOT(removeFamilyFromConfig(QString,QString,QString)));
179         updateEditorController(family);
180     }
181     return controller;
182 }
183
184 void ApplicationController::updateEditorController(const QString &family)
185 {
186     FontsConfEditorController *controller = editorController(family);
187
188     QStringList familyList;
189
190     controller->clear();
191
192     familyList = mFontConfig->prependFamilyFor(family);
193     foreach (const QString &f, familyList) {
194         InstalledFontInfo *info = fontInfo(f);
195         controller->appendFontsInfo(f, PREPEND_DEF, info);
196     }
197
198     familyList = mFontConfig->preferFamilyFor(family);
199     foreach (const QString &f, familyList) {
200         InstalledFontInfo *info = fontInfo(f);
201         controller->appendFontsInfo(f, PREFER_DEF, info);
202     }
203
204     familyList = mFontConfig->acceptFamilyFor(family);
205     foreach (const QString &f, familyList) {
206         InstalledFontInfo *info = fontInfo(f);
207         controller->appendFontsInfo(f, ACCEPT_DEF, info);
208     }
209
210     controller->syncFamilyList();
211 }
212
213 void ApplicationController::updateAllEditorController()
214 {
215     if (!mFontConfig->fontsConfModified() || mIgnoreUpdate) {
216         mIgnoreUpdate = false;
217         return;
218     }
219     foreach (const QString &f, mEditorController.keys()) {
220         updateEditorController(f);
221     }
222 }
223
224 bool ApplicationController::localFontsConfExists() const
225 {
226     if (!mFontConfig)
227         return false;
228     return QFile::exists(mFontConfig->localFontsConfPath());
229 }
230
231 QString ApplicationController::localFontsConfPath() const
232 {
233     if (!mFontConfig)
234         return QString();
235     return mFontConfig->localFontsConfPath();
236 }
237
238 QString ApplicationController::localFontsConf() const
239 {
240     if (!mFontConfig)
241         return QString();
242     return mFontConfig->localFontsConf();
243 }
244
245 bool ApplicationController::isEmptyFontsConf() const
246 {
247     if (!localFontsConfExists())
248         return true;
249     return !mFontConfig || mFontConfig->isEmptyLocalFontsConf();
250 }
251
252 bool ApplicationController::working() const
253 {
254     return mWorking;
255 }
256
257
258 void ApplicationController::startUpdateLocalFontsConf()
259 {
260     mUpdating++;
261 }
262
263 void ApplicationController::endUpdateLocalFontsConf()
264 {
265     mUpdating--;
266     Q_ASSERT(mUpdating >= 0);
267 }
268
269 void ApplicationController::localFontsConfUpdated()
270 {
271     if (mUpdating == 0)
272         emit localFontsConfChanged();
273 }
274
275 void ApplicationController::resetLocalFontsConf()
276 {
277     if (!mFontConfig)
278         return;
279     mFontConfig->resetFontsConf();
280 }
281
282 void ApplicationController::importSystemSettings(const QString &family)
283 {
284     mFontConfig->importSystemSettings(family);
285     if (mFontConfig->fontsConfModified()) {
286         updateEditorController(family);
287     }
288 }
289
290 void ApplicationController::createRecommendedSettings()
291 {
292     mFontConfig->createRecommendedSettings();
293 }
294
295 void ApplicationController::createFontDir()
296 {
297     QDir fontDir(mFontDirPath);
298     if (!fontDir.exists()) {
299         fontDir.mkpath(mFontDirPath);
300         mFontDirExists = fontDir.exists();
301         emit fontDirExistsChanged();
302     }
303 }
304
305 void ApplicationController::installFont(FontInfo *fontinfo)
306 {
307     QFileInfo srcfont(fontinfo->fontPath());
308     QFileInfo dstfont(mFontDirPath + QLatin1String("/") + srcfont.fileName());
309
310     QFile::copy(srcfont.absoluteFilePath(), dstfont.absoluteFilePath());
311
312     foreach (const QString &family, fontinfo->families()) {
313         FontsConfigProperties *prop = fontinfo->fontProperty(family);
314         mFontConfig->appendFontProperty(prop);
315     }
316
317     emit installFinished(srcfont.fileName());
318
319     mWorking = true;
320     emit workingChanged();
321
322     mFontConfig->runFcCache();
323 }
324
325 void ApplicationController::updateFontsConf(InstalledFontInfo *fontInfo)
326 {
327     mFontConfig->appendFontProperty(fontInfo);
328 }
329
330 void ApplicationController::uninstallFont(const QString &fontpath)
331 {
332     bool check = QFile::remove(fontpath);
333     if (check) {
334         emit uninstallFinished(fontpath);
335
336         mWorking = true;
337         emit workingChanged();
338
339         mFontConfig->runFcCache();
340     } else
341         emit alertDialog(tr("Could not remove Font '%1'").arg(fontpath));
342 }
343
344 void ApplicationController::syncInstalledFonts()
345 {
346     if (!mFontConfig)
347         return;
348     emit clearInstalledFontList();
349     for (int i = 0; i < mFontConfig->count(); i++) {
350         InstalledFontInfo *info = mFontConfig->fontInfo(i);
351         if (mShowSystemFont || !info->systemFont())
352             emit appendInstalledFont(info->localefamily(), info->localefullname());
353     }
354 }
355
356 void ApplicationController::syncInstallableFamilyFor(const QString &family)
357 {
358     if (!mFontConfig)
359         return;
360     emit clearInstallableFamilyListFor(family);
361     QStringList familyList = mFontConfig->installableFamily(family);
362     foreach (const QString &f, familyList) {
363         InstalledFontInfo *info = mFontConfig->fontInfo(f);
364         emit appendInstallableFamily(info->enfamily(), info->localefamily(), info->systemFont());
365     }
366 }
367
368 void ApplicationController::saveFontsConf()
369 {
370     if (!mFontConfig->fontsConfModified())
371         return;
372
373     mFontConfig->saveFontsConf();
374     mForceOverwrite = false;
375     emit localFontsConfPathChanged();
376 }
377
378 void ApplicationController::appendFamilyToConfig(const QString &family, const QString &value, const QString &priority)
379 {
380     FontsConfEditorController *controller = qobject_cast<FontsConfEditorController*>(sender());
381     if (controller)
382         mIgnoreUpdate = true;
383     if (priority == PREPEND_DEF)
384         mFontConfig->addPrependFamily(family, value);
385     else if (priority == APPEND_DEF)
386         mFontConfig->addAppendFamily(family, value);
387     else if (priority == PREFER_DEF)
388         mFontConfig->addPreferFamily(family, value);
389     else if (priority == ACCEPT_DEF)
390         mFontConfig->addAcceptFamily(family, value);
391 }
392
393 void ApplicationController::removeFamilyFromConfig(const QString &family, const QString &value, const QString &priority)
394 {
395     FontsConfEditorController *controller = qobject_cast<FontsConfEditorController*>(sender());
396     if (controller)
397         mIgnoreUpdate = true;
398     if (priority == PREPEND_DEF)
399         mFontConfig->removePrependFamily(family, value);
400     else if (priority == APPEND_DEF)
401         mFontConfig->removeAppendFamily(family, value);
402     else if (priority == PREFER_DEF)
403         mFontConfig->removePreferFamily(family, value);
404     else if (priority == ACCEPT_DEF)
405         mFontConfig->removeAcceptFamily(family, value);
406 }
407
408 void ApplicationController::readFcListFinished()
409 {
410     mWorking = false;
411     emit workingChanged();
412 }