add more spacing
[personal-kdebase.git] / workspace / kcontrol / kfontinst / kcmfontinst / FcQuery.cpp
blob032bd8c1039c39c86a54cce0e60c8053159fbb60
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 "FcQuery.h"
25 #include "Fc.h"
26 #include <QtCore/QProcess>
27 #include <stdio.h>
29 namespace KFI
32 // key: 0(i)(s)
33 static int getInt(const QString &str)
35 int rv=KFI_NULL_SETTING,
36 start=str.lastIndexOf(':')+1,
37 end=str.lastIndexOf("(i)(s)");
39 if(end>start)
40 rv=str.mid(start, end-start).trimmed().toInt();
42 return rv;
45 CFcQuery::~CFcQuery()
49 void CFcQuery::run(const QString &query)
51 QStringList args;
53 itsFile=itsFont=QString();
54 itsBuffer=QByteArray();
56 if(itsProc)
57 itsProc->kill();
58 else
59 itsProc=new QProcess(this);
61 args << "-v" << query;
63 connect(itsProc, SIGNAL(finished(int, QProcess::ExitStatus)), SLOT(procExited()));
64 connect(itsProc, SIGNAL(readyReadStandardOutput()), SLOT(data()));
66 itsProc->start("fc-match", args);
69 void CFcQuery::procExited()
71 QString family;
72 int weight(KFI_NULL_SETTING), slant(KFI_NULL_SETTING), width(KFI_NULL_SETTING);
73 QStringList results(QString::fromUtf8(itsBuffer, itsBuffer.length()).split('\n'));
75 if(results.size())
77 QStringList::ConstIterator it(results.begin()),
78 end(results.end());
80 for(; it!=end; ++it)
82 QString line((*it).trimmed());
84 if(0==line.indexOf("file:")) // file: "Wibble"(s)
86 int endPos=line.indexOf("\"(s)");
88 if(-1!=endPos)
89 itsFile=line.mid(7, endPos-7);
91 else if(0==line.indexOf("family:")) // family: "Wibble"(s)
93 int endPos=line.indexOf("\"(s)");
95 if(-1!=endPos)
96 family=line.mid(9, endPos-9);
98 else if(0==line.indexOf("slant:")) // slant: 0(i)(s)
99 slant=getInt(line);
100 else if(0==line.indexOf("weight:")) // weight: 0(i)(s)
101 weight=getInt(line);
102 else if(0==line.indexOf("width:")) // width: 0(i)(s)
103 width=getInt(line);
107 if(!family.isEmpty())
108 itsFont=FC::createName(family, weight, width, slant);
110 emit finished();
113 void CFcQuery::data()
115 itsBuffer+=itsProc->readAllStandardOutput();
120 #include "FcQuery.moc"