2 * KFontInst - KDE Font Installer
4 * Copyright 2003-2007 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.
26 #include "DisabledFonts.h"
27 #include "KfiConstants.h"
29 #include "kxftconfig.h"
30 #include <QtCore/QFile>
31 #include <QtCore/QDataStream>
32 #include <QtCore/QVariant>
33 #include <KDE/KGlobal>
34 #include <KDE/KComponentData>
42 class CHelper
: public CSocket
46 CHelper(char *sock
, int uid
) : itsSock(sock
), itsUid(uid
) { }
52 bool getFile(CDisabledFonts::TFont
&font
);
55 bool deleteDisabledFont();
68 CDisabledFonts itsDisabledFonts
;
73 if(connectToServer(itsSock
, itsUid
))
80 kDebug() << "Waiting for cmd...";
89 case KFI::CMD_ENABLE_FONT
:
92 case KFI::CMD_DISABLE_FONT
:
95 case KFI::CMD_DELETE_DISABLED_FONT
:
96 res
=deleteDisabledFont();
98 case KFI::CMD_RELOAD_DISABLED_LIST
:
99 kDebug() << "reloadDisabled";
100 res
=itsDisabledFonts
.save();
101 itsDisabledFonts
.load();
103 case KFI::CMD_COPY_FILE
:
106 case KFI::CMD_MOVE_FILE
:
109 case KFI::CMD_DELETE_FILE
:
112 case KFI::CMD_CREATE_DIR
:
115 case KFI::CMD_CREATE_AFM
:
118 case KFI::CMD_FC_CACHE
:
119 kDebug() << "fc-cache";
120 Misc::doCmd("fc-cache");
123 case KFI::CMD_ADD_DIR_TO_FONTCONFIG
:
126 case KFI::CMD_CFG_DIR_FOR_X
:
134 kError() << "Unknown command:" << cmd
.toInt() << endl
;
138 case QVariant::Invalid
:
139 kDebug() << "Finished";
140 return 0; // finished!
142 kError() << "Invalid type received when expecting command int, " << cmd
.type() << endl
;
148 kError() << " Could not write result to kio_fonts" << endl
;
154 kError() << " Could not connect to kio_fonts" << endl
;
159 bool CHelper::getFile(CDisabledFonts::TFont
&font
)
161 kDebug() << "getFile";
166 if(read(file
) && read(foundry
))
168 kDebug() << "file:" << file
<< " foundry:" << foundry
;
169 font
.files
.append(CDisabledFonts::TFile(file
, 0, foundry
));
173 kError() << " Invalid parameters for getFile" << endl
;
178 bool CHelper::disableFont()
180 kDebug() << "disableFont";
186 qulonglong writingSystems
;
188 if(read(family
) && read(style
) && read(writingSystems
) && read(face
) && read(numFiles
))
190 kDebug() << "family:" << family
<< " style:" << style
<< " face:" << face
191 << " numFiles:" << numFiles
<< endl
;
193 CDisabledFonts::TFont
font(family
, style
, writingSystems
);
195 for(int i
=0; i
<numFiles
; ++i
)
199 if(font
.files
.count())
201 if(1==font
.files
.count())
202 (*(font
.files
.begin())).face
=face
;
203 return itsDisabledFonts
.disable(font
);
209 kError() << " Invalid parameters for disableFont" << endl
;
213 bool CHelper::enableFont()
215 kDebug() << "enableFont";
220 if(read(family
) && read(style
))
222 kDebug() << "family:" << family
<< " style:" << style
;
224 return itsDisabledFonts
.enable(family
, style
);
227 kError() << " Invalid parameters for enableFont" << endl
;
231 bool CHelper::deleteDisabledFont()
233 kDebug() << "deleteDisabledFont";
238 if(read(family
) && read(style
))
240 kDebug() << "family:" << family
<< " style:" << style
;
242 CDisabledFonts::TFont
font(family
, style
);
243 CDisabledFonts::TFontList::Iterator
it(itsDisabledFonts
.items().find(font
));
245 if(itsDisabledFonts
.modifiable() && itsDisabledFonts
.items().end()!=it
)
247 CDisabledFonts::TFileList::ConstIterator
fIt((*it
).files
.begin()),
248 fEnd((*it
).files
.end());
250 for(; fIt
!=fEnd
; ++fIt
)
251 if(::unlink(QFile::encodeName((*fIt
).path
).constData()))
254 itsDisabledFonts
.remove(it
);
255 itsDisabledFonts
.refresh();
262 kError() << " Invalid parameters for disableFont" << endl
;
266 bool CHelper::addToFc()
268 kDebug() << "addToFc";
274 kDebug() << "dir:" << dir
;
276 KXftConfig
xft(KXftConfig::Dirs
, KFI::Misc::root());
282 kError() << " Invalid parameters for addToFc" << endl
;
286 bool CHelper::configureX()
288 kDebug() << "configureX";
294 kDebug() << "dir:" << dir
;
295 return Misc::configureForX11(dir
);
298 kError() << " Invalid parameters for configureX" << endl
;
302 bool CHelper::copyFile()
304 kDebug() << "copyFile";
309 if(read(from
) && read(to
))
311 kDebug() << "from:" << from
<< " to:" << to
;
313 bool rv
=QFile::copy(from
, to
);
315 Misc::setFilePerms(to
);
319 kError() << " Invalid parameters for copyFile" << endl
;
323 bool CHelper::moveFile()
325 kDebug() << "moveFile";
332 if(read(from
) && read(to
) && read(user
) && read(group
))
334 kDebug() << "from:" << from
<< " to:" << to
<< " user:" << user
<< " group:" << group
;
336 QByteArray
toC(QFile::encodeName(to
));
338 bool res(0==::rename(QFile::encodeName(from
).constData(), toC
.constData()));
342 Misc::setFilePerms(toC
);
343 ::chown(toC
.constData(), user
, group
);
349 kError() << " Invalid parameters for moveFile" << endl
;
353 bool CHelper::deleteFile()
355 kDebug() << "deleteFile";
361 kDebug() << "file:" << file
;
362 return 0==::unlink(QFile::encodeName(file
).constData());
365 kError() << " Invalid parameters for deleteFile" << endl
;
369 bool CHelper::createDir()
371 kDebug() << "createDir";
377 kDebug() << "dir:" << dir
;
379 return KFI::Misc::createDir(dir
);
382 kError() << " Invalid parameters for createDir" << endl
;
386 bool CHelper::createAfm()
388 kDebug() << "createAfm";
394 kDebug() << "font:" << font
;
396 return Misc::doCmd("pf2afm", QFile::encodeName(font
));
399 kError() << " Invalid parameters for createAfm" << endl
;
405 int main(int argc
, char *argv
[])
408 return KFI::CHelper(argv
[1], atoi(argv
[2])).run();
410 fprintf(stderr
, "Usage: %s <socket> <uid>\n", argv
[0]);