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.95(0.5RC)");
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(insertFamilyToConfig(QString,QString,QString,int)), SLOT(insertFamilyToConfig(QString,QString,QString,int)));
179 connect(controller, SIGNAL(removeFamilyFromList(QString,QString,QString)), SLOT(removeFamilyFromConfig(QString,QString,QString)));
180 updateEditorController(family);
185 void ApplicationController::updateEditorController(const QString &family)
187 FontsConfEditorController *controller = editorController(family);
189 QStringList familyList;
193 familyList = mFontConfig->prependFamilyFor(family);
194 foreach (const QString &f, familyList) {
195 InstalledFontInfo *info = fontInfo(f);
196 controller->appendFontsInfo(f, PREPEND_DEF, info);
199 familyList = mFontConfig->preferFamilyFor(family);
200 foreach (const QString &f, familyList) {
201 InstalledFontInfo *info = fontInfo(f);
202 controller->appendFontsInfo(f, PREFER_DEF, info);
205 familyList = mFontConfig->acceptFamilyFor(family);
206 foreach (const QString &f, familyList) {
207 InstalledFontInfo *info = fontInfo(f);
208 controller->appendFontsInfo(f, ACCEPT_DEF, info);
211 controller->syncFamilyList();
214 QUrl ApplicationController::backupDir() const
216 return QUrl(QDir::homePath() + QLatin1String("/MyDocs/Documents"));
219 QString ApplicationController::defaultBackupFilename() const
221 QString backupdir = backupDir().toString();
222 QString backupfile = backupdir + QLatin1String("/fonts.conf");
224 while (QFile::exists(backupfile)) {
225 backupfile = backupdir + QString::fromAscii("/fonts-%1.conf").arg(++idx);
230 QString ApplicationController::url2path(const QUrl &url) const
232 QString path = url.toLocalFile();
233 if (path.startsWith(QDir::homePath()))
234 path.replace(0, QDir::homePath().length(), QLatin1String("~"));
238 void ApplicationController::updateAllEditorController()
240 if (!mFontConfig->fontsConfModified() || mIgnoreUpdate) {
241 mIgnoreUpdate = false;
244 foreach (const QString &f, mEditorController.keys()) {
245 updateEditorController(f);
249 bool ApplicationController::localFontsConfExists() const
253 return QFile::exists(mFontConfig->localFontsConfPath());
256 QString ApplicationController::localFontsConfPath() const
260 return mFontConfig->localFontsConfPath();
263 QString ApplicationController::localFontsConf() const
267 return mFontConfig->localFontsConf();
270 bool ApplicationController::isEmptyFontsConf() const
272 if (!localFontsConfExists())
274 return !mFontConfig || mFontConfig->isEmptyLocalFontsConf();
277 bool ApplicationController::working() const
283 void ApplicationController::startUpdateLocalFontsConf()
288 void ApplicationController::endUpdateLocalFontsConf()
291 Q_ASSERT(mUpdating >= 0);
294 void ApplicationController::localFontsConfUpdated()
297 emit localFontsConfChanged();
300 void ApplicationController::resetLocalFontsConf()
304 mFontConfig->resetFontsConf();
307 void ApplicationController::importSystemSettings(const QString &family)
309 mFontConfig->importSystemSettings(family);
310 if (mFontConfig->fontsConfModified()) {
311 updateEditorController(family);
315 void ApplicationController::createRecommendedSettings()
317 mFontConfig->createRecommendedSettings();
320 void ApplicationController::backupConfig(const QString &filename)
322 mFontConfig->backupFontsConf(filename);
323 emit backupConfigFinished(filename);
326 void ApplicationController::restoreConfig(const QString &filename)
328 mFontConfig->restoreFontsConf(filename);
329 emit restoreConfigFinished(filename);
332 void ApplicationController::createFontDir()
334 QDir fontDir(mFontDirPath);
335 if (!fontDir.exists()) {
336 fontDir.mkpath(mFontDirPath);
337 mFontDirExists = fontDir.exists();
338 emit fontDirExistsChanged();
342 void ApplicationController::installFont(FontInfo *fontinfo)
344 QFileInfo srcfont(fontinfo->fontPath());
345 QFileInfo dstfont(mFontDirPath + QLatin1String("/") + srcfont.fileName());
347 QFile::copy(srcfont.absoluteFilePath(), dstfont.absoluteFilePath());
349 foreach (const QString &family, fontinfo->families()) {
350 FontsConfigProperties *prop = fontinfo->fontProperty(family);
351 mFontConfig->appendFontProperty(prop);
354 emit installFinished(srcfont.fileName());
357 emit workingChanged();
359 mFontConfig->runFcCache();
362 void ApplicationController::updateFontsConf(InstalledFontInfo *fontInfo)
364 mFontConfig->appendFontProperty(fontInfo);
367 void ApplicationController::uninstallFont(const QString &fontpath)
369 bool check = QFile::remove(fontpath);
371 emit uninstallFinished(fontpath);
374 emit workingChanged();
376 mFontConfig->runFcCache();
378 emit alertDialog(tr("Could not remove Font '%1'").arg(fontpath));
381 void ApplicationController::syncInstalledFonts()
385 emit clearInstalledFontList();
386 for (int i = 0; i < mFontConfig->count(); i++) {
387 InstalledFontInfo *info = mFontConfig->fontInfo(i);
388 if (mShowSystemFont || !info->systemFont())
389 emit appendInstalledFont(info->localefamily(), info->localefullname());
393 void ApplicationController::syncInstallableFamilyFor(const QString &family)
397 emit clearInstallableFamilyListFor(family);
398 QStringList familyList = mFontConfig->installableFamily(family);
399 foreach (const QString &f, familyList) {
400 InstalledFontInfo *info = mFontConfig->fontInfo(f);
401 emit appendInstallableFamily(info->enfamily(), info->localefamily(), info->systemFont());
405 void ApplicationController::saveFontsConf()
407 if (!mFontConfig->fontsConfModified())
410 mFontConfig->saveFontsConf();
411 mForceOverwrite = false;
412 emit localFontsConfPathChanged();
415 void ApplicationController::appendFamilyToConfig(const QString &family, const QString &value, const QString &priority)
417 FontsConfEditorController *controller = qobject_cast<FontsConfEditorController*>(sender());
419 mIgnoreUpdate = true;
420 if (priority == PREPEND_DEF)
421 mFontConfig->addPrependFamily(family, value);
422 else if (priority == APPEND_DEF)
423 mFontConfig->addAppendFamily(family, value);
424 else if (priority == PREFER_DEF)
425 mFontConfig->addPreferFamily(family, value);
426 else if (priority == ACCEPT_DEF)
427 mFontConfig->addAcceptFamily(family, value);
430 void ApplicationController::insertFamilyToConfig(const QString &family, const QString &value, const QString &priority, int index)
432 FontsConfEditorController *controller = qobject_cast<FontsConfEditorController*>(sender());
434 mIgnoreUpdate = true;
435 if (priority == PREPEND_DEF)
436 mFontConfig->insertPrependFamily(family, value, index);
437 else if (priority == APPEND_DEF)
438 mFontConfig->insertAppendFamily(family, value, index);
439 else if (priority == PREFER_DEF)
440 mFontConfig->insertPreferFamily(family, value, index);
441 else if (priority == ACCEPT_DEF)
442 mFontConfig->insertAcceptFamily(family, value, index);
445 void ApplicationController::removeFamilyFromConfig(const QString &family, const QString &value, const QString &priority)
447 FontsConfEditorController *controller = qobject_cast<FontsConfEditorController*>(sender());
449 mIgnoreUpdate = true;
450 if (priority == PREPEND_DEF)
451 mFontConfig->removePrependFamily(family, value);
452 else if (priority == APPEND_DEF)
453 mFontConfig->removeAppendFamily(family, value);
454 else if (priority == PREFER_DEF)
455 mFontConfig->removePreferFamily(family, value);
456 else if (priority == ACCEPT_DEF)
457 mFontConfig->removeAcceptFamily(family, value);
460 void ApplicationController::readFcListFinished()
463 emit workingChanged();