add more spacing
[personal-kdebase.git] / workspace / kcontrol / kfontinst / kio / Helper.cpp
blob03d8c9445289ed11c399ed3e77b80a8f1277a958
1 /*
2 * KFontInst - KDE Font Installer
4 * Copyright 2003-2007 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 "Helper.h"
25 #include "Misc.h"
26 #include "DisabledFonts.h"
27 #include "KfiConstants.h"
28 #include "Socket.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>
35 #include <KDE/KDebug>
36 #include <stdio.h>
37 #include <stdlib.h>
39 namespace KFI
42 class CHelper : public CSocket
44 public:
46 CHelper(char *sock, int uid) : itsSock(sock), itsUid(uid) { }
48 int run();
50 private:
52 bool getFile(CDisabledFonts::TFont &font);
53 bool disableFont();
54 bool enableFont();
55 bool deleteDisabledFont();
56 bool addToFc();
57 bool configureX();
58 bool copyFile();
59 bool moveFile();
60 bool deleteFile();
61 bool createDir();
62 bool createAfm();
64 private:
66 const char *itsSock;
67 int itsUid;
68 CDisabledFonts itsDisabledFonts;
71 int CHelper::run()
73 if(connectToServer(itsSock, itsUid))
75 for(;;)
77 QVariant cmd;
78 bool res(false);
80 kDebug() << "Waiting for cmd...";
81 if(!read(cmd, -1))
82 break;
84 switch(cmd.type())
86 case QVariant::Int:
87 switch(cmd.toInt())
89 case KFI::CMD_ENABLE_FONT:
90 res=enableFont();
91 break;
92 case KFI::CMD_DISABLE_FONT:
93 res=disableFont();
94 break;
95 case KFI::CMD_DELETE_DISABLED_FONT:
96 res=deleteDisabledFont();
97 break;
98 case KFI::CMD_RELOAD_DISABLED_LIST:
99 kDebug() << "reloadDisabled";
100 res=itsDisabledFonts.save();
101 itsDisabledFonts.load();
102 break;
103 case KFI::CMD_COPY_FILE:
104 res=copyFile();
105 break;
106 case KFI::CMD_MOVE_FILE:
107 res=moveFile();
108 break;
109 case KFI::CMD_DELETE_FILE:
110 res=deleteFile();
111 break;
112 case KFI::CMD_CREATE_DIR:
113 res=createDir();
114 break;
115 case KFI::CMD_CREATE_AFM:
116 res=createAfm();
117 break;
118 case KFI::CMD_FC_CACHE:
119 kDebug() << "fc-cache";
120 Misc::doCmd("fc-cache");
121 res=true;
122 break;
123 case KFI::CMD_ADD_DIR_TO_FONTCONFIG:
124 res=addToFc();
125 break;
126 case KFI::CMD_CFG_DIR_FOR_X:
127 res=configureX();
128 break;
129 case KFI::CMD_QUIT:
130 write(true);
131 return 0;
132 break;
133 default:
134 kError() << "Unknown command:" << cmd.toInt() << endl;
135 return -1;
137 break;
138 case QVariant::Invalid:
139 kDebug() << "Finished";
140 return 0; // finished!
141 default:
142 kError() << "Invalid type received when expecting command int, " << cmd.type() << endl;
143 break;
146 if(!write(res))
148 kError() << " Could not write result to kio_fonts" << endl;
149 break;
153 else
154 kError() << " Could not connect to kio_fonts" << endl;
156 return -1;
159 bool CHelper::getFile(CDisabledFonts::TFont &font)
161 kDebug() << "getFile";
163 QString file,
164 foundry;
166 if(read(file) && read(foundry))
168 kDebug() << "file:" << file << " foundry:" << foundry;
169 font.files.append(CDisabledFonts::TFile(file, 0, foundry));
170 return true;
173 kError() << " Invalid parameters for getFile" << endl;
175 return false;
178 bool CHelper::disableFont()
180 kDebug() << "disableFont";
182 QString family;
183 int style,
184 face,
185 numFiles;
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)
196 if(!getFile(font))
197 return false;
199 if(font.files.count())
201 if(1==font.files.count())
202 (*(font.files.begin())).face=face;
203 return itsDisabledFonts.disable(font);
205 else
206 return false;
209 kError() << " Invalid parameters for disableFont" << endl;
210 return false;
213 bool CHelper::enableFont()
215 kDebug() << "enableFont";
217 QString family;
218 int style;
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;
228 return false;
231 bool CHelper::deleteDisabledFont()
233 kDebug() << "deleteDisabledFont";
235 QString family;
236 int style;
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()))
252 return false;
254 itsDisabledFonts.remove(it);
255 itsDisabledFonts.refresh();
256 return true;
258 else
259 return false;
262 kError() << " Invalid parameters for disableFont" << endl;
263 return false;
266 bool CHelper::addToFc()
268 kDebug() << "addToFc";
270 QString dir;
272 if(read(dir))
274 kDebug() << "dir:" << dir;
276 KXftConfig xft(KXftConfig::Dirs, KFI::Misc::root());
278 xft.addDir(dir);
279 return xft.apply();
282 kError() << " Invalid parameters for addToFc" << endl;
283 return false;
286 bool CHelper::configureX()
288 kDebug() << "configureX";
290 QString dir;
292 if(read(dir))
294 kDebug() << "dir:" << dir;
295 return Misc::configureForX11(dir);
298 kError() << " Invalid parameters for configureX" << endl;
299 return false;
302 bool CHelper::copyFile()
304 kDebug() << "copyFile";
306 QString from,
309 if(read(from) && read(to))
311 kDebug() << "from:" << from << " to:" << to;
313 bool rv=QFile::copy(from, to);
314 if(rv)
315 Misc::setFilePerms(to);
316 return rv;
319 kError() << " Invalid parameters for copyFile" << endl;
320 return false;
323 bool CHelper::moveFile()
325 kDebug() << "moveFile";
327 QString from,
329 int user,
330 group;
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()));
340 if(res)
342 Misc::setFilePerms(toC);
343 ::chown(toC.constData(), user, group);
346 return res;
349 kError() << " Invalid parameters for moveFile" << endl;
350 return false;
353 bool CHelper::deleteFile()
355 kDebug() << "deleteFile";
357 QString file;
359 if(read(file))
361 kDebug() << "file:" << file;
362 return 0==::unlink(QFile::encodeName(file).constData());
365 kError() << " Invalid parameters for deleteFile" << endl;
366 return false;
369 bool CHelper::createDir()
371 kDebug() << "createDir";
373 QString dir;
375 if(read(dir))
377 kDebug() << "dir:" << dir;
379 return KFI::Misc::createDir(dir);
382 kError() << " Invalid parameters for createDir" << endl;
383 return false;
386 bool CHelper::createAfm()
388 kDebug() << "createAfm";
390 QString font;
392 if(read(font))
394 kDebug() << "font:" << font;
396 return Misc::doCmd("pf2afm", QFile::encodeName(font));
399 kError() << " Invalid parameters for createAfm" << endl;
400 return false;
405 int main(int argc, char *argv[])
407 if(argc==3)
408 return KFI::CHelper(argv[1], atoi(argv[2])).run();
409 else
410 fprintf(stderr, "Usage: %s <socket> <uid>\n", argv[0]);
411 return -1;