2 * KFontInst - KDE Font Installer
4 * Copyright 2003-2008 Craig Drummond <craig@kde.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; see the file COPYING. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
24 #include "KfiConstants.h"
26 #include "fontinstadaptor.h"
27 #include "kxftconfig.h"
28 #include "PolicyKitMechanism.h"
29 #include <QtCore/QCoreApplication>
30 #include <QtDBus/QDBusConnection>
31 #include <fontconfig/fontconfig.h>
34 static const int constTimeout
=10 * 60 * 1000; // Timeout after 10 minutes of inactivity...
35 static const char * constActionStr
=KFI_IFACE
;
37 #define BEGIN_COMMAND \
38 EStatus status(NotAuthorised); \
40 if(POLKIT_RESULT_YES==PolicyKitMechanism::instance()->canDoAction(constActionStr, key)) \
48 FontInst::FontInst(QObject
*parent
)
53 itsTimer
=new QTimer(this);
54 connect(itsTimer
, SIGNAL(timeout()), SLOT(timeout()));
55 itsTimer
->start(constTimeout
);
57 new FontinstAdaptor(this);
58 QDBusConnection::systemBus().registerObject("/FontInst", this);
65 int FontInst::disableFont(uint key
, const QString
&family
, uint style
, qulonglong writingSystems
,
66 uint face
, const QStringList
&fileData
)
71 if(fileData
.count() && 0==fileData
.count()%2)
73 CDisabledFonts::TFont
font(family
, style
, writingSystems
);
76 for(int i
=0; i
<fileData
.count() && ok
; i
+=2)
77 if(pathIsOk(fileData
[i
]))
78 font
.files
.append(CDisabledFonts::TFile(fileData
[i
], 0, fileData
[i
+1]));
82 if(ok
&& font
.files
.count())
84 if(1==font
.files
.count())
85 (*(font
.files
.begin())).face
=face
;
86 status
=itsDisabledFonts
.disable(font
) ? CommandOk
: CommandFailed
;
93 int FontInst::enableFont(uint key
, const QString
&family
, uint style
)
97 status
=itsDisabledFonts
.enable(family
, style
) ? CommandOk
: CommandFailed
;
102 int FontInst::deleteDisabledFont(uint key
, const QString
&family
, uint style
)
106 CDisabledFonts::TFont
font(family
, style
);
107 CDisabledFonts::TFontList::Iterator
it(itsDisabledFonts
.items().find(font
));
109 if(itsDisabledFonts
.modifiable() && itsDisabledFonts
.items().end()!=it
)
111 CDisabledFonts::TFileList::ConstIterator
fIt((*it
).files
.begin()),
112 fEnd((*it
).files
.end());
114 for(; fIt
!=fEnd
&& CommandFailed
!=status
; ++fIt
)
115 if(::unlink(QFile::encodeName((*fIt
).path
).constData()))
116 status
=CommandFailed
;
118 if(CommandFailed
!=status
)
120 itsDisabledFonts
.remove(it
);
121 itsDisabledFonts
.refresh();
126 status
=CommandFailed
;
131 int FontInst::reloadDisabledList(uint key
)
135 status
=itsDisabledFonts
.save() ? CommandOk
: CommandFailed
;
136 itsDisabledFonts
.load();
141 int FontInst::addToFc(uint key
, const QString
&dir
)
145 // We should only ever be adding "/usr/local/share/fonts" to font configs dir list...
146 if(0==dir
.indexOf(KFI_DEFAULT_SYS_FONTS_FOLDER
))
148 KXftConfig
xft(KXftConfig::Dirs
, true);
151 status
=xft
.apply() ? CommandOk
: CommandFailed
;
153 if(CommandOk
==status
)
157 status
=CommandFailed
;
162 int FontInst::configureX(uint key
, const QString
&dir
)
166 status
=pathIsOk(dir
) && Misc::configureForX11(dir
) ? CommandOk
: CommandFailed
;
171 int FontInst::copyFont(uint key
, const QString
&src
, const QString
&dest
)
175 if(pathIsOk(dest
) && QFile::copy(src
, dest
))
177 Misc::setFilePerms(dest
);
181 status
=CommandFailed
;
186 int FontInst::moveFont(uint key
, const QString
&src
, const QString
&dest
, uint uid
, uint gid
)
190 if(pathIsOk(0==uid
? dest
: src
) && QFile::rename(src
, dest
))
192 Misc::setFilePerms(dest
);
193 ::chown(QFile::encodeName(dest
).constData(), uid
, gid
);
197 status
=CommandFailed
;
202 int FontInst::deleteFont(uint key
, const QString
&font
)
206 status
=pathIsOk(font
) && QFile::remove(font
) ? CommandOk
: CommandFailed
;
211 int FontInst::createDir(uint key
, const QString
&dir
)
215 status
=pathIsOk(dir
) && KFI::Misc::createDir(dir
) ? CommandOk
: CommandFailed
;
220 int FontInst::createAfm(uint key
, const QString
&font
)
224 status
=pathIsOk(font
) && Misc::doCmd("pf2afm", QFile::encodeName(font
)) ? CommandOk
: CommandFailed
;
229 int FontInst::fcCache(uint key
)
233 status
=Misc::doCmd("fc-cache") ? CommandOk
: CommandFailed
;
238 void FontInst::timeout()
240 QCoreApplication::quit();
243 void FontInst::refreshDirList()
245 FcStrList
*list
=FcConfigGetFontDirs(FcInitLoadConfig());
250 while((dir
=FcStrListNext(list
)))
252 QString
d(Misc::dirSyntax((const char *)dir
));
254 if(0!=d
.indexOf(QDir::homePath()))
255 if(KFI_DEFAULT_SYS_FONTS_FOLDER
==d
)
261 // Place main folder at the top of the list - to speed things up!
263 itsFcDirs
.prepend(main
);
266 // Check that we are operating on a file withing a fonts folder...
267 bool FontInst::pathIsOk(const QString
&path
)
269 QStringList::ConstIterator
it(itsFcDirs
.begin()),
270 end(itsFcDirs
.end());
273 if(0==path
.indexOf(*it
))
276 // Perhaps dir was added? Refresh fontconfig list and try again...
279 it
=itsFcDirs
.begin();
283 if(0==path
.indexOf(*it
))
289 int main(int argc
, char **argv
)
291 QCoreApplication
app(argc
, argv
);
293 if(QDBusConnection::systemBus().registerService(KFI_IFACE
))
302 #include "FontInst.moc"