1 /****************************************************************************
3 ** Copyright (C) 2011 Takumi Asaki
4 ** All rights reserved.
5 ** Contact: Takumi Asaki (takumi.asaki@gmail.com)
7 ** This file is part of the fontmanager application.
9 ** You may use this file under the terms of the BSD license as follows:
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
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
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
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."
37 ****************************************************************************/
39 #include "applicationcontroller.h"
41 #include "fontconfigdefs.h"
42 #include "fontconfigmanager.h"
43 #include "installedfontinfo.h"
44 #include "fontsconfeditorcontroller.h"
45 #include "fontsconf.h"
49 ApplicationController::ApplicationController(QObject *parent) :
50 QObject(parent), mFontDirExists(false), mShowSystemFont(false),
52 mForceOverwrite(false), mWorking(false), mIgnoreUpdate(false),
58 void ApplicationController::init()
60 mFontConfig = new FontConfigManager(this);
61 setFontDir(QDir::homePath() + QLatin1String("/.fonts"));
64 mLang = curLocale.name();
65 int idx = mLang.indexOf(QLatin1Char('_'));
67 mLang = mLang.left(idx);
68 mFontConfig->setCurrentLanguage(mLang);
70 connect(mFontConfig, SIGNAL(fcCacheFinished()), mFontConfig, SLOT(readFcList()));
71 connect(mFontConfig, SIGNAL(fontListUpdated()), SLOT(readFcListFinished()));
72 connect(mFontConfig, SIGNAL(fontListUpdated()), SLOT(syncInstalledFonts()));
74 connect(mFontConfig, SIGNAL(localFontsConfPathChanged()), SIGNAL(localFontsConfPathChanged()));
75 connect(mFontConfig, SIGNAL(localFontsConfExistsChanged()), SIGNAL(localFontsConfExistsChanged()));
77 connect(mFontConfig, SIGNAL(startUpdateFontsConfig()), SLOT(startUpdateLocalFontsConf()));
78 connect(mFontConfig, SIGNAL(fontsConfUpdated()), SLOT(localFontsConfUpdated()));
79 connect(mFontConfig, SIGNAL(endUpdateFontsConfig()), SLOT(endUpdateLocalFontsConf()));
81 connect(this, SIGNAL(localFontsConfChanged()), SLOT(updateAllEditorController()));
82 connect(this, SIGNAL(localFontsConfChanged()), SLOT(saveFontsConf()));
84 connect(mFontConfig, SIGNAL(warning(QString)), SIGNAL(alertDialog(QString)));
86 mFontConfig->setLocalFontsConfPath(QDir::homePath() + QLatin1String("/.fonts.conf"));
88 mFontConfig->readFontsConf();
89 mFontConfig->readFcList();
91 foreach (const QString &f, FontsConf::genericFamilies()) {
92 updateEditorController(f);
97 QString ApplicationController::version() const
99 return QLatin1String("0.4.2");
102 QString ApplicationController::currentLanguage() const
107 QString ApplicationController::fontDir() const
112 void ApplicationController::setFontDir(const QString &dirpath)
114 if (mFontDirPath != dirpath) {
115 mFontDirPath = dirpath;
116 emit fontDirChanged(mFontDirPath);
117 QDir fontDir(dirpath);
118 mFontDirExists = fontDir.exists();
119 emit fontDirExistsChanged();
121 mFontConfig->setLocalFontPath(dirpath);
125 bool ApplicationController::fontDirExists() const
127 return mFontDirExists;
130 bool ApplicationController::showSystemFont() const
132 return mShowSystemFont;
135 void ApplicationController::setShowSystemFont(bool show)
137 if (mShowSystemFont != show) {
138 mShowSystemFont = show;
139 emit showSystemFontChanged();
143 FontInfo *ApplicationController::checkFontInfo(const QUrl &path)
145 FontInfo *fInfo = new FontInfo(path.toLocalFile(), mFontConfig, this);
149 InstalledFontInfo *ApplicationController::fontInfo(const QString &family, const QString &fullname) const
151 return mFontConfig->fontInfo(family, fullname);
154 QStringList ApplicationController::fontCount(const QString &fontpath) const
156 return mFontConfig->fontCount(fontpath);
159 QString ApplicationController::localeFamily(const QString &family) const
161 return mFontConfig->localeFamily(family);
164 bool ApplicationController::fontExists(FontInfo *fontinfo)
166 QFileInfo srcfont(fontinfo->fontPath());
167 QFileInfo dstfont(mFontDirPath + QLatin1String("/") + srcfont.fileName());
168 return dstfont.exists();
171 FontsConfEditorController *ApplicationController::editorController(const QString &family)
173 FontsConfEditorController *controller = mEditorController.value(family, 0);
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);
184 void ApplicationController::updateEditorController(const QString &family)
186 FontsConfEditorController *controller = editorController(family);
188 QStringList familyList;
192 familyList = mFontConfig->prependFamilyFor(family);
193 foreach (const QString &f, familyList) {
194 InstalledFontInfo *info = fontInfo(f);
195 controller->appendFontsInfo(f, PREPEND_DEF, info);
198 familyList = mFontConfig->preferFamilyFor(family);
199 foreach (const QString &f, familyList) {
200 InstalledFontInfo *info = fontInfo(f);
201 controller->appendFontsInfo(f, PREFER_DEF, info);
204 familyList = mFontConfig->acceptFamilyFor(family);
205 foreach (const QString &f, familyList) {
206 InstalledFontInfo *info = fontInfo(f);
207 controller->appendFontsInfo(f, ACCEPT_DEF, info);
210 controller->syncFamilyList();
213 void ApplicationController::updateAllEditorController()
215 if (!mFontConfig->fontsConfModified() || mIgnoreUpdate) {
216 mIgnoreUpdate = false;
219 foreach (const QString &f, mEditorController.keys()) {
220 updateEditorController(f);
224 bool ApplicationController::localFontsConfExists() const
228 return QFile::exists(mFontConfig->localFontsConfPath());
231 QString ApplicationController::localFontsConfPath() const
235 return mFontConfig->localFontsConfPath();
238 QString ApplicationController::localFontsConf() const
242 return mFontConfig->localFontsConf();
245 bool ApplicationController::isEmptyFontsConf() const
247 if (!localFontsConfExists())
249 return !mFontConfig || mFontConfig->isEmptyLocalFontsConf();
252 bool ApplicationController::working() const
258 void ApplicationController::startUpdateLocalFontsConf()
263 void ApplicationController::endUpdateLocalFontsConf()
266 Q_ASSERT(mUpdating >= 0);
269 void ApplicationController::localFontsConfUpdated()
272 emit localFontsConfChanged();
275 void ApplicationController::resetLocalFontsConf()
279 mFontConfig->resetFontsConf();
282 void ApplicationController::importSystemSettings(const QString &family)
284 mFontConfig->importSystemSettings(family);
285 if (mFontConfig->fontsConfModified()) {
286 updateEditorController(family);
290 void ApplicationController::createRecommendedSettings()
292 mFontConfig->createRecommendedSettings();
295 void ApplicationController::createFontDir()
297 QDir fontDir(mFontDirPath);
298 if (!fontDir.exists()) {
299 fontDir.mkpath(mFontDirPath);
300 mFontDirExists = fontDir.exists();
301 emit fontDirExistsChanged();
305 void ApplicationController::installFont(FontInfo *fontinfo)
307 QFileInfo srcfont(fontinfo->fontPath());
308 QFileInfo dstfont(mFontDirPath + QLatin1String("/") + srcfont.fileName());
310 QFile::copy(srcfont.absoluteFilePath(), dstfont.absoluteFilePath());
312 foreach (const QString &family, fontinfo->families()) {
313 FontsConfigProperties *prop = fontinfo->fontProperty(family);
314 mFontConfig->appendFontProperty(prop);
317 emit installFinished(srcfont.fileName());
320 emit workingChanged();
322 mFontConfig->runFcCache();
325 void ApplicationController::updateFontsConf(InstalledFontInfo *fontInfo)
327 mFontConfig->appendFontProperty(fontInfo);
330 void ApplicationController::uninstallFont(const QString &fontpath)
332 bool check = QFile::remove(fontpath);
334 emit uninstallFinished(fontpath);
337 emit workingChanged();
339 mFontConfig->runFcCache();
341 emit alertDialog(tr("Could not remove Font '%1'").arg(fontpath));
344 void ApplicationController::syncInstalledFonts()
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());
356 void ApplicationController::syncInstallableFamilyFor(const QString &family)
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());
368 void ApplicationController::saveFontsConf()
370 if (!mFontConfig->fontsConfModified())
373 mFontConfig->saveFontsConf();
374 mForceOverwrite = false;
375 emit localFontsConfPathChanged();
378 void ApplicationController::appendFamilyToConfig(const QString &family, const QString &value, const QString &priority)
380 FontsConfEditorController *controller = qobject_cast<FontsConfEditorController*>(sender());
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);
393 void ApplicationController::removeFamilyFromConfig(const QString &family, const QString &value, const QString &priority)
395 FontsConfEditorController *controller = qobject_cast<FontsConfEditorController*>(sender());
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);
408 void ApplicationController::readFcListFinished()
411 emit workingChanged();