add more spacing
[personal-kdebase.git] / workspace / kcontrol / kfontinst / kio / FontInst.cpp
blob05d04d63ec4c34c71d5792a7c659a845414a494e
1 /*
2 * KFontInst - KDE Font Installer
4 * Copyright 2003-2008 Craig Drummond <craig@kde.org>
6 * ----
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"
25 #include "FontInst.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>
32 #include <unistd.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); \
39 itsTimer->stop(); \
40 if(POLKIT_RESULT_YES==PolicyKitMechanism::instance()->canDoAction(constActionStr, key)) \
41 { \
43 #define END_COMMAND \
44 } \
45 itsTimer->start(); \
46 return status;
48 FontInst::FontInst(QObject *parent)
49 : QObject(parent)
51 refreshDirList();
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);
61 FontInst::~FontInst()
65 int FontInst::disableFont(uint key, const QString &family, uint style, qulonglong writingSystems,
66 uint face, const QStringList &fileData)
68 BEGIN_COMMAND
70 status=CommandFailed;
71 if(fileData.count() && 0==fileData.count()%2)
73 CDisabledFonts::TFont font(family, style, writingSystems);
74 bool ok(true);
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]));
79 else
80 ok=false;
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;
90 END_COMMAND
93 int FontInst::enableFont(uint key, const QString &family, uint style)
95 BEGIN_COMMAND
97 status=itsDisabledFonts.enable(family, style) ? CommandOk : CommandFailed;
99 END_COMMAND
102 int FontInst::deleteDisabledFont(uint key, const QString &family, uint style)
104 BEGIN_COMMAND
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();
122 status=CommandOk;
125 else
126 status=CommandFailed;
128 END_COMMAND
131 int FontInst::reloadDisabledList(uint key)
133 BEGIN_COMMAND
135 status=itsDisabledFonts.save() ? CommandOk : CommandFailed;
136 itsDisabledFonts.load();
138 END_COMMAND
141 int FontInst::addToFc(uint key, const QString &dir)
143 BEGIN_COMMAND
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);
150 xft.addDir(dir);
151 status=xft.apply() ? CommandOk : CommandFailed;
153 if(CommandOk==status)
154 refreshDirList();
156 else
157 status=CommandFailed;
159 END_COMMAND
162 int FontInst::configureX(uint key, const QString &dir)
164 BEGIN_COMMAND
166 status=pathIsOk(dir) && Misc::configureForX11(dir) ? CommandOk : CommandFailed;
168 END_COMMAND
171 int FontInst::copyFont(uint key, const QString &src, const QString &dest)
173 BEGIN_COMMAND
175 if(pathIsOk(dest) && QFile::copy(src, dest))
177 Misc::setFilePerms(dest);
178 status=CommandOk;
180 else
181 status=CommandFailed;
183 END_COMMAND
186 int FontInst::moveFont(uint key, const QString &src, const QString &dest, uint uid, uint gid)
188 BEGIN_COMMAND
190 if(pathIsOk(0==uid ? dest : src) && QFile::rename(src, dest))
192 Misc::setFilePerms(dest);
193 ::chown(QFile::encodeName(dest).constData(), uid, gid);
194 status=CommandOk;
196 else
197 status=CommandFailed;
199 END_COMMAND
202 int FontInst::deleteFont(uint key, const QString &font)
204 BEGIN_COMMAND
206 status=pathIsOk(font) && QFile::remove(font) ? CommandOk : CommandFailed;
208 END_COMMAND
211 int FontInst::createDir(uint key, const QString &dir)
213 BEGIN_COMMAND
215 status=pathIsOk(dir) && KFI::Misc::createDir(dir) ? CommandOk : CommandFailed;
217 END_COMMAND
220 int FontInst::createAfm(uint key, const QString &font)
222 BEGIN_COMMAND
224 status=pathIsOk(font) && Misc::doCmd("pf2afm", QFile::encodeName(font)) ? CommandOk : CommandFailed;
226 END_COMMAND
229 int FontInst::fcCache(uint key)
231 BEGIN_COMMAND
233 status=Misc::doCmd("fc-cache") ? CommandOk : CommandFailed;
235 END_COMMAND
238 void FontInst::timeout()
240 QCoreApplication::quit();
243 void FontInst::refreshDirList()
245 FcStrList *list=FcConfigGetFontDirs(FcInitLoadConfig());
246 FcChar8 *dir;
247 QString main;
249 itsFcDirs.clear();
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)
256 main=d;
257 else
258 itsFcDirs.append(d);
261 // Place main folder at the top of the list - to speed things up!
262 if(!main.isEmpty())
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());
272 for(; it!=end; ++it)
273 if(0==path.indexOf(*it))
274 return true;
276 // Perhaps dir was added? Refresh fontconfig list and try again...
277 refreshDirList();
279 it=itsFcDirs.begin();
280 end=itsFcDirs.end();
282 for(; it!=end; ++it)
283 if(0==path.indexOf(*it))
284 return true;
286 return false;
289 int main(int argc, char **argv)
291 QCoreApplication app(argc, argv);
293 if(QDBusConnection::systemBus().registerService(KFI_IFACE))
295 new FontInst(0L);
296 return app.exec();
298 else
299 return -1;
302 #include "FontInst.moc"