You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
831 lines
20 KiB
831 lines
20 KiB
15 years ago
|
/*
|
||
|
* This file is part of the KDE libraries
|
||
13 years ago
|
* Copyright (c) 2001 Michael Goffioul <tdeprint@swing.be>
|
||
15 years ago
|
*
|
||
|
* This library is free software; you can redistribute it and/or
|
||
|
* modify it under the terms of the GNU Library General Public
|
||
|
* License version 2 as published by the Free Software Foundation.
|
||
|
*
|
||
|
* This library is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||
|
* Library General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU Library General Public License
|
||
|
* along with this library; see the file COPYING.LIB. If not, write to
|
||
|
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||
|
* Boston, MA 02110-1301, USA.
|
||
|
**/
|
||
|
|
||
|
#include "kxmlcommand.h"
|
||
|
#include "driver.h"
|
||
|
#include "kmfactory.h"
|
||
13 years ago
|
#include "tdeprintcheck.h"
|
||
15 years ago
|
#include "driverview.h"
|
||
|
|
||
15 years ago
|
#include <tqfile.h>
|
||
|
#include <tqregexp.h>
|
||
|
#include <tqdir.h>
|
||
|
#include <tqinputdialog.h>
|
||
|
#include <tqmap.h>
|
||
|
#include <tqvaluelist.h>
|
||
15 years ago
|
#include <kstandarddirs.h>
|
||
|
#include <klocale.h>
|
||
|
#include <ksimpleconfig.h>
|
||
|
#include <kdialogbase.h>
|
||
|
#include <kdebug.h>
|
||
|
#include <kprocess.h>
|
||
|
#include <klibloader.h>
|
||
|
#include <kmessagebox.h>
|
||
|
|
||
15 years ago
|
static void setOptionText(DrBase *opt, const TQString& s)
|
||
15 years ago
|
{
|
||
|
if (s.isEmpty())
|
||
|
opt->set("text", opt->name());
|
||
|
else
|
||
|
opt->set("text", i18n(s.utf8()));
|
||
|
}
|
||
|
|
||
|
class KXmlCommand::KXmlCommandPrivate
|
||
|
{
|
||
|
public:
|
||
14 years ago
|
TQString m_name;
|
||
|
TQString m_command;
|
||
15 years ago
|
DrMain *m_driver;
|
||
|
struct
|
||
|
{
|
||
14 years ago
|
TQString m_format[2]; // 0 -> file, 1 -> pipe
|
||
15 years ago
|
} m_io[2]; // 0 -> input, 1 -> output
|
||
14 years ago
|
TQString m_description;
|
||
|
TQString m_outputMime;
|
||
|
TQStringList m_inputMime;
|
||
|
TQStringList m_requirements;
|
||
15 years ago
|
bool m_loaded[2]; // 0 -> Desktop, 1 -> XML
|
||
15 years ago
|
TQString m_comment;
|
||
15 years ago
|
};
|
||
|
|
||
15 years ago
|
KXmlCommand::KXmlCommand(const TQString& xmlId)
|
||
|
: TQObject(KXmlCommandManager::self(), "XmlCommand")
|
||
15 years ago
|
{
|
||
|
init();
|
||
|
d->m_name = xmlId;
|
||
|
}
|
||
|
|
||
|
KXmlCommand::~KXmlCommand()
|
||
|
{
|
||
|
//kdDebug(500) << "deleting driver" << endl;
|
||
|
delete d->m_driver;
|
||
|
//kdDebug(500) << "deleting private data" << endl;
|
||
|
delete d;
|
||
|
//kdDebug(500) << "finished" << endl;
|
||
|
}
|
||
|
|
||
|
void KXmlCommand::init()
|
||
|
{
|
||
|
d = new KXmlCommandPrivate;
|
||
|
d->m_driver = 0;
|
||
|
d->m_loaded[0] = d->m_loaded[1] = false;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString KXmlCommand::name() const
|
||
15 years ago
|
{ return d->m_name; }
|
||
|
|
||
15 years ago
|
void KXmlCommand::setName(const TQString& s)
|
||
15 years ago
|
{ d->m_name = s; }
|
||
|
|
||
15 years ago
|
TQString KXmlCommand::command()
|
||
15 years ago
|
{
|
||
|
check(true);
|
||
|
return d->m_command;
|
||
|
}
|
||
|
|
||
15 years ago
|
void KXmlCommand::setCommand(const TQString& s)
|
||
15 years ago
|
{
|
||
|
d->m_command = s;
|
||
|
}
|
||
|
|
||
|
DrMain* KXmlCommand::driver()
|
||
|
{
|
||
|
check(true);
|
||
|
return d->m_driver;
|
||
|
}
|
||
|
|
||
|
DrMain* KXmlCommand::takeDriver()
|
||
|
{
|
||
|
check(true);
|
||
|
DrMain *dr = d->m_driver;
|
||
|
d->m_driver = 0;
|
||
|
d->m_loaded[1] = false;
|
||
|
return dr;
|
||
|
}
|
||
|
|
||
|
void KXmlCommand::setDriver(DrMain *driver)
|
||
|
{
|
||
|
delete d->m_driver;
|
||
|
d->m_driver = driver;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString KXmlCommand::io(bool io_input, bool io_pipe)
|
||
15 years ago
|
{
|
||
|
check(true);
|
||
|
return d->m_io[(io_input?0:1)].m_format[(io_pipe?1:0)];
|
||
|
}
|
||
|
|
||
15 years ago
|
void KXmlCommand::setIo(const TQString& s, bool io_input, bool io_pipe)
|
||
15 years ago
|
{
|
||
|
d->m_io[(io_input?0:1)].m_format[(io_pipe?1:0)] = s;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString KXmlCommand::description()
|
||
15 years ago
|
{
|
||
|
check();
|
||
|
return d->m_description;
|
||
|
}
|
||
|
|
||
15 years ago
|
void KXmlCommand::setDescription(const TQString& s)
|
||
15 years ago
|
{
|
||
|
d->m_description = s;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString KXmlCommand::mimeType()
|
||
15 years ago
|
{
|
||
|
check();
|
||
|
return d->m_outputMime;
|
||
|
}
|
||
|
|
||
15 years ago
|
void KXmlCommand::setMimeType(const TQString& s)
|
||
15 years ago
|
{
|
||
|
d->m_outputMime = s;
|
||
|
}
|
||
|
|
||
15 years ago
|
bool KXmlCommand::acceptMimeType(const TQString& s)
|
||
15 years ago
|
{
|
||
|
check();
|
||
13 years ago
|
return (d->m_inputMime.find(s) != d->m_inputMime.end());
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
TQStringList KXmlCommand::inputMimeTypes()
|
||
15 years ago
|
{
|
||
|
check();
|
||
|
return d->m_inputMime;
|
||
|
}
|
||
|
|
||
15 years ago
|
void KXmlCommand::setInputMimeTypes(const TQStringList& l)
|
||
15 years ago
|
{
|
||
|
d->m_inputMime = l;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQStringList KXmlCommand::requirements()
|
||
15 years ago
|
{
|
||
|
check();
|
||
|
return d->m_requirements;
|
||
|
}
|
||
|
|
||
15 years ago
|
void KXmlCommand::setRequirements(const TQStringList& l)
|
||
15 years ago
|
{
|
||
|
d->m_requirements = l;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString KXmlCommand::comment()
|
||
15 years ago
|
{
|
||
|
check();
|
||
|
return d->m_comment;
|
||
|
}
|
||
|
|
||
15 years ago
|
void KXmlCommand::setComment( const TQString& s )
|
||
15 years ago
|
{
|
||
|
d->m_comment = s;
|
||
|
}
|
||
|
|
||
|
bool KXmlCommand::isValid()
|
||
|
{
|
||
13 years ago
|
return (!locate("data", "tdeprint/filters/"+name()+".desktop").isEmpty());
|
||
15 years ago
|
}
|
||
|
|
||
|
void KXmlCommand::check(bool use_xml)
|
||
|
{
|
||
|
if (!d->m_loaded[0])
|
||
|
{
|
||
|
loadDesktop();
|
||
|
d->m_loaded[0] = true;
|
||
|
}
|
||
|
if (use_xml && !d->m_loaded[1])
|
||
|
{
|
||
|
loadXml();
|
||
|
d->m_loaded[1] = true;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void KXmlCommand::loadDesktop()
|
||
|
{
|
||
13 years ago
|
KSimpleConfig conf(locate("data", "tdeprint/filters/"+name()+".desktop"));
|
||
15 years ago
|
conf.setGroup("KDE Print Filter Entry");
|
||
|
d->m_description = conf.readEntry("Comment");
|
||
|
d->m_outputMime = conf.readEntry("MimeTypeOut");
|
||
|
d->m_inputMime = conf.readListEntry("MimeTypeIn");
|
||
|
d->m_requirements = conf.readListEntry("Require");
|
||
|
d->m_comment = conf.readEntry( "Description" );
|
||
|
}
|
||
|
|
||
|
void KXmlCommand::saveDesktop()
|
||
|
{
|
||
13 years ago
|
KSimpleConfig conf(locateLocal("data", "tdeprint/filters/"+name()+".desktop"));
|
||
15 years ago
|
conf.setGroup("KDE Print Filter Entry");
|
||
|
conf.writeEntry("Comment", d->m_description);
|
||
|
conf.writeEntry("MimeTypeIn", d->m_inputMime);
|
||
|
conf.writeEntry("MimeTypeOut", d->m_outputMime);
|
||
|
conf.writeEntry("Require", d->m_requirements);
|
||
|
conf.writeEntry( "Description", d->m_comment );
|
||
|
}
|
||
|
|
||
|
void KXmlCommand::loadXml()
|
||
|
{
|
||
13 years ago
|
TQFile f(locate("data", "tdeprint/filters/"+name()+".xml"));
|
||
14 years ago
|
TQDomDocument doc;
|
||
15 years ago
|
if (f.open(IO_ReadOnly) && doc.setContent(&f) && doc.documentElement().tagName() == "kprintfilter")
|
||
|
{
|
||
14 years ago
|
TQDomElement e, docElem = doc.documentElement();
|
||
15 years ago
|
d->m_name = docElem.attribute("name");
|
||
|
|
||
|
// command
|
||
|
e = docElem.namedItem("filtercommand").toElement();
|
||
|
if (!e.isNull())
|
||
|
d->m_command = e.attribute("data");
|
||
|
|
||
|
// arguments
|
||
|
e = docElem.namedItem("filterargs").toElement();
|
||
|
if (!e.isNull())
|
||
|
{
|
||
|
d->m_driver = new DrMain;
|
||
|
d->m_driver->setName(d->m_name);
|
||
|
parseGroup(e, d->m_driver);
|
||
|
setOptionText(d->m_driver, d->m_description);
|
||
|
}
|
||
|
|
||
|
// input/output
|
||
|
e = docElem.namedItem("filterinput").toElement();
|
||
|
if (!e.isNull())
|
||
|
parseIO(e, 0);
|
||
|
e = docElem.namedItem("filteroutput").toElement();
|
||
|
if (!e.isNull())
|
||
|
parseIO(e, 1);
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
void KXmlCommand::parseIO(const TQDomElement& e, int n)
|
||
15 years ago
|
{
|
||
14 years ago
|
TQDomElement elem = e.firstChild().toElement();
|
||
15 years ago
|
while (!elem.isNull())
|
||
|
{
|
||
|
if (elem.tagName() == "filterarg")
|
||
|
{
|
||
|
int format = (elem.attribute("name") == "file" ? 0 : 1);
|
||
|
d->m_io[n].m_format[format] = elem.attribute("format");
|
||
|
}
|
||
|
elem = elem.nextSibling().toElement();
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
DrGroup* KXmlCommand::parseGroup(const TQDomElement& e, DrGroup *grp)
|
||
15 years ago
|
{
|
||
|
if (!grp)
|
||
|
grp = new DrGroup;
|
||
|
grp->setName(e.attribute("name"));
|
||
|
setOptionText(grp, e.attribute("description"));
|
||
|
|
||
14 years ago
|
TQDomElement elem = e.firstChild().toElement();
|
||
15 years ago
|
while (!elem.isNull())
|
||
|
{
|
||
|
if (elem.tagName() == "filterarg")
|
||
|
{
|
||
|
DrBase *opt = parseArgument(elem);
|
||
|
if (opt)
|
||
|
grp->addOption(opt);
|
||
|
}
|
||
|
else if (elem.tagName() == "filtergroup")
|
||
|
{
|
||
|
DrGroup *group = parseGroup(elem, 0);
|
||
|
if (group)
|
||
|
grp->addGroup(group);
|
||
|
}
|
||
|
elem = elem.nextSibling().toElement();
|
||
|
}
|
||
|
|
||
|
return grp;
|
||
|
}
|
||
|
|
||
15 years ago
|
DrBase* KXmlCommand::parseArgument(const TQDomElement& e)
|
||
15 years ago
|
{
|
||
|
DrBase *opt(0);
|
||
14 years ago
|
TQString type = e.attribute("type");
|
||
15 years ago
|
|
||
|
if (type == "int" || type == "float")
|
||
|
{
|
||
|
if (type == "int")
|
||
|
opt = new DrIntegerOption;
|
||
|
else
|
||
|
opt = new DrFloatOption;
|
||
|
opt->set("minval", e.attribute("min"));
|
||
|
opt->set("maxval", e.attribute("max"));
|
||
|
}
|
||
|
else if (type == "string")
|
||
|
opt = new DrStringOption;
|
||
|
else if (type == "list" || type == "bool")
|
||
|
{
|
||
|
if (type == "list")
|
||
|
opt = new DrListOption;
|
||
|
else
|
||
|
opt = new DrBooleanOption;
|
||
|
DrListOption *lopt = static_cast<DrListOption*>(opt);
|
||
14 years ago
|
TQDomElement elem = e.firstChild().toElement();
|
||
15 years ago
|
while (!elem.isNull())
|
||
|
{
|
||
|
if (elem.tagName() == "value")
|
||
|
{
|
||
|
DrBase *choice = new DrBase;
|
||
|
choice->setName(elem.attribute("name"));
|
||
|
setOptionText(choice, elem.attribute("description"));
|
||
|
lopt->addChoice(choice);
|
||
|
}
|
||
|
elem = elem.nextSibling().toElement();
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
return 0;
|
||
|
|
||
|
opt->setName("_kde-" + d->m_name + "-" + e.attribute("name"));
|
||
|
setOptionText(opt, e.attribute("description"));
|
||
|
opt->set("format", e.attribute("format"));
|
||
|
opt->set("default", e.attribute("default"));
|
||
|
opt->set( "persistent", e.attribute( "persistent" ) );
|
||
|
opt->setValueText(opt->get("default"));
|
||
|
|
||
|
return opt;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString KXmlCommand::buildCommand(const TQMap<TQString,TQString>& opts, bool pipein, bool pipeout)
|
||
15 years ago
|
{
|
||
|
check(true);
|
||
|
|
||
14 years ago
|
TQString str, cmd = d->m_command;
|
||
15 years ago
|
TQString re( "%value" ), quotedRe( "'%value'" );
|
||
15 years ago
|
|
||
|
if (d->m_driver)
|
||
|
{
|
||
15 years ago
|
TQMap<TQString,TQString> fopts;
|
||
15 years ago
|
|
||
|
d->m_driver->setOptions(opts);
|
||
|
d->m_driver->getOptions(fopts, false);
|
||
15 years ago
|
for (TQMap<TQString,TQString>::ConstIterator it=fopts.begin(); it!=fopts.end(); ++it)
|
||
15 years ago
|
{
|
||
14 years ago
|
DrBase *dopt = d->m_driver->findOption(it.key());
|
||
15 years ago
|
if (dopt)
|
||
|
{
|
||
14 years ago
|
TQString format = dopt->get("format");
|
||
15 years ago
|
TQString value = dopt->valueText();
|
||
13 years ago
|
if ( format.find( quotedRe ) != -1 )
|
||
15 years ago
|
{
|
||
|
if ( ( value.right( 1 ) == "'" && value.left( 1 ) == "'" ) ||
|
||
|
( value.right( 1 ) == "\"" && value.left( 1 ) == "\"" ) )
|
||
14 years ago
|
format.replace( quotedRe, value );
|
||
15 years ago
|
else
|
||
14 years ago
|
format.replace( re, value );
|
||
15 years ago
|
}
|
||
|
else
|
||
|
{
|
||
14 years ago
|
format.replace( re, KProcess::quote( dopt->valueText() ) );
|
||
15 years ago
|
}
|
||
|
str.append(format).append(" ");
|
||
|
}
|
||
|
}
|
||
14 years ago
|
cmd.replace("%filterargs", str);
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
cmd.replace("%filterinput", d->m_io[0].m_format[(pipein?1:0)]);
|
||
|
cmd.replace("%filteroutput", d->m_io[1].m_format[(pipeout?1:0)]);
|
||
15 years ago
|
|
||
|
return cmd;
|
||
|
}
|
||
|
|
||
15 years ago
|
void KXmlCommand::setOptions(const TQMap<TQString,TQString>& opts)
|
||
15 years ago
|
{
|
||
|
if (opts.count() == 0)
|
||
|
return;
|
||
|
// force loading the driver if needed
|
||
|
if (driver())
|
||
|
d->m_driver->setOptions(opts);
|
||
|
}
|
||
|
|
||
15 years ago
|
void KXmlCommand::getOptions(TQMap<TQString,TQString>& opts, bool incldef)
|
||
15 years ago
|
{
|
||
|
// force loading the driver
|
||
|
if (driver())
|
||
|
d->m_driver->getOptions(opts, incldef);
|
||
|
}
|
||
|
|
||
|
void KXmlCommand::saveXml()
|
||
|
{
|
||
13 years ago
|
TQFile f(locateLocal("data", "tdeprint/filters/"+name()+".xml"));
|
||
15 years ago
|
if (!f.open(IO_WriteOnly))
|
||
|
return;
|
||
|
|
||
14 years ago
|
TQDomDocument doc("kprintfilter");
|
||
|
TQDomElement root = doc.createElement("kprintfilter"), elem;
|
||
15 years ago
|
|
||
|
root.setAttribute("name", d->m_name);
|
||
|
doc.appendChild(root);
|
||
|
|
||
|
// command
|
||
|
elem = doc.createElement("filtercommand");
|
||
|
elem.setAttribute("data", d->m_command);
|
||
|
root.appendChild(elem);
|
||
|
|
||
|
// options
|
||
|
if (d->m_driver)
|
||
|
{
|
||
|
elem = createGroup(doc, d->m_driver);
|
||
|
elem.setTagName("filterargs");
|
||
|
root.appendChild(elem);
|
||
|
}
|
||
|
|
||
|
// IO
|
||
|
if (!(elem=createIO(doc, 0, "filterinput")).isNull())
|
||
|
root.appendChild(elem);
|
||
|
if (!(elem=createIO(doc, 1, "filteroutput")).isNull())
|
||
|
root.appendChild(elem);
|
||
|
|
||
|
// save to file (and close it)
|
||
14 years ago
|
TQTextStream t(&f);
|
||
15 years ago
|
t << doc.toString();
|
||
|
f.close();
|
||
|
}
|
||
|
|
||
15 years ago
|
TQDomElement KXmlCommand::createIO(TQDomDocument& doc, int n, const TQString& tag)
|
||
15 years ago
|
{
|
||
14 years ago
|
TQDomElement elem = doc.createElement(tag);
|
||
13 years ago
|
if (d->m_command.find("%"+tag) != -1)
|
||
15 years ago
|
{
|
||
|
for (int i=0; i<2; i++)
|
||
|
{
|
||
14 years ago
|
TQDomElement io = doc.createElement("filterarg");
|
||
15 years ago
|
io.setAttribute("name", (i ? "pipe" : "file"));
|
||
|
io.setAttribute("format", d->m_io[n].m_format[i]);
|
||
|
elem.appendChild(io);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return elem;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQDomElement KXmlCommand::createGroup(TQDomDocument& doc, DrGroup *group)
|
||
15 years ago
|
{
|
||
14 years ago
|
TQDomElement elem = doc.createElement("filtergroup");
|
||
15 years ago
|
elem.setAttribute("name", group->name());
|
||
|
elem.setAttribute("description", group->get("text"));
|
||
|
|
||
15 years ago
|
TQPtrListIterator<DrGroup> git(group->groups());
|
||
15 years ago
|
for (; git.current(); ++git)
|
||
|
elem.appendChild(createGroup(doc, git.current()));
|
||
|
|
||
15 years ago
|
TQPtrListIterator<DrBase> oit(group->options());
|
||
15 years ago
|
for (; oit.current(); ++oit)
|
||
|
elem.appendChild(createElement(doc, oit.current()));
|
||
|
|
||
|
return elem;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQDomElement KXmlCommand::createElement(TQDomDocument& doc, DrBase *opt)
|
||
15 years ago
|
{
|
||
14 years ago
|
TQDomElement elem = doc.createElement("filterarg");
|
||
|
TQString elemName = opt->name();
|
||
15 years ago
|
if (elemName.startsWith("_kde-"))
|
||
14 years ago
|
elemName.replace(0, name().length()+6, "");
|
||
15 years ago
|
elem.setAttribute("name", elemName);
|
||
|
elem.setAttribute("format", opt->get("format"));
|
||
|
elem.setAttribute("description", opt->get("text"));
|
||
|
elem.setAttribute("default", opt->get("default"));
|
||
|
elem.setAttribute( "persistent", opt->get( "persistent" ) == "1" ? "1" : "0");
|
||
|
|
||
|
switch (opt->type())
|
||
|
{
|
||
|
case DrBase::String:
|
||
|
elem.setAttribute("type", "string");
|
||
|
break;
|
||
|
case DrBase::Integer:
|
||
|
case DrBase::Float:
|
||
|
elem.setAttribute("type", (opt->type() == DrBase::Integer ? "int" : "float"));
|
||
|
elem.setAttribute("min", opt->get("minval"));
|
||
|
elem.setAttribute("max", opt->get("maxval"));
|
||
|
break;
|
||
|
case DrBase::Boolean:
|
||
|
case DrBase::List:
|
||
|
elem.setAttribute("type", (opt->type() == DrBase::List ? "list" : "bool"));
|
||
|
{
|
||
15 years ago
|
TQPtrListIterator<DrBase> it(*(static_cast<DrListOption*>(opt)->choices()));
|
||
15 years ago
|
for (; it.current(); ++it)
|
||
|
{
|
||
14 years ago
|
TQDomElement chElem = doc.createElement("value");
|
||
15 years ago
|
chElem.setAttribute("name", it.current()->name());
|
||
|
chElem.setAttribute("description", it.current()->get("text"));
|
||
|
elem.appendChild(chElem);
|
||
|
}
|
||
|
}
|
||
|
break;
|
||
|
default:
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
return elem;
|
||
|
}
|
||
|
|
||
|
//---------------------------------------------------------------------------------------------------
|
||
|
|
||
|
class KXmlCommandManager::KXmlCommandManagerPrivate
|
||
|
{
|
||
|
public:
|
||
14 years ago
|
TQStringList m_cmdlist;
|
||
15 years ago
|
TQMap<TQString, TQValueList<KXmlCommand*> > m_mimemap;
|
||
|
TQMap<TQString, KXmlCommand*> m_cmdmap;
|
||
15 years ago
|
};
|
||
|
|
||
|
KXmlCommandManager* KXmlCommandManager::m_self = 0;
|
||
|
|
||
|
KXmlCommandManager* KXmlCommandManager::self()
|
||
|
{
|
||
|
if (!m_self)
|
||
|
{
|
||
|
m_self = new KXmlCommandManager;
|
||
|
Q_CHECK_PTR(m_self);
|
||
|
}
|
||
|
return m_self;
|
||
|
}
|
||
|
|
||
|
KXmlCommandManager::KXmlCommandManager()
|
||
15 years ago
|
: TQObject(KMFactory::self(), "XmlCommandManager")
|
||
15 years ago
|
{
|
||
|
d = new KXmlCommandManagerPrivate;
|
||
|
}
|
||
|
|
||
|
KXmlCommandManager::~KXmlCommandManager()
|
||
|
{
|
||
|
cleanUp();
|
||
|
delete d;
|
||
|
}
|
||
|
|
||
15 years ago
|
KXmlCommand* KXmlCommandManager::loadCommand(const TQString& xmlId, bool check)
|
||
15 years ago
|
{
|
||
|
if (check)
|
||
|
{
|
||
13 years ago
|
TQString desktopFile = locate("data", "tdeprint/filters/"+xmlId+".desktop");
|
||
15 years ago
|
if (desktopFile.isEmpty())
|
||
|
return 0;
|
||
|
}
|
||
|
return new KXmlCommand(xmlId);
|
||
|
}
|
||
|
|
||
|
void KXmlCommandManager::saveCommand(KXmlCommand *xmlCmd)
|
||
|
{
|
||
|
xmlCmd->saveDesktop();
|
||
|
xmlCmd->saveXml();
|
||
|
|
||
|
cleanUp();
|
||
|
}
|
||
|
|
||
|
void KXmlCommandManager::cleanUp()
|
||
|
{
|
||
15 years ago
|
for (TQMap<TQString, KXmlCommand*>::ConstIterator it=d->m_cmdmap.begin(); it!=d->m_cmdmap.end(); ++it)
|
||
15 years ago
|
delete (*it);
|
||
|
d->m_cmdmap.clear();
|
||
|
d->m_mimemap.clear();
|
||
|
d->m_cmdlist.clear();
|
||
|
}
|
||
|
|
||
|
void KXmlCommandManager::preload()
|
||
|
{
|
||
|
if (d->m_cmdmap.count() == 0)
|
||
|
{
|
||
|
commandList();
|
||
15 years ago
|
for (TQStringList::Iterator it=d->m_cmdlist.begin(); it!=d->m_cmdlist.end(); ++it)
|
||
15 years ago
|
{
|
||
|
KXmlCommand *xmlCmd = loadCommand(*it);
|
||
|
if (!xmlCmd) continue; // Error!
|
||
|
|
||
14 years ago
|
TQStringList inputMime = xmlCmd->inputMimeTypes();
|
||
15 years ago
|
for (TQStringList::ConstIterator mime=inputMime.begin(); mime!=inputMime.end(); ++mime)
|
||
15 years ago
|
{
|
||
|
d->m_mimemap[*mime].append(xmlCmd);
|
||
|
d->m_cmdmap[*it] = xmlCmd;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
TQStringList KXmlCommandManager::commandList()
|
||
15 years ago
|
{
|
||
|
if (d->m_cmdlist.isEmpty())
|
||
|
{
|
||
13 years ago
|
TQStringList dirs = KGlobal::dirs()->findDirs("data", "tdeprint/filters/");
|
||
15 years ago
|
|
||
15 years ago
|
for (TQStringList::ConstIterator it=dirs.begin(); it!=dirs.end(); ++it)
|
||
15 years ago
|
{
|
||
14 years ago
|
TQStringList list = TQDir(*it).entryList("*.desktop", TQDir::Files, TQDir::Unsorted);
|
||
15 years ago
|
for (TQStringList::ConstIterator it2=list.begin(); it2!=list.end(); ++it2)
|
||
15 years ago
|
{
|
||
14 years ago
|
TQString cmdName = (*it2).left((*it2).length()-8);
|
||
13 years ago
|
if (d->m_cmdlist.find(cmdName) == d->m_cmdlist.end())
|
||
15 years ago
|
d->m_cmdlist.append(cmdName);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
d->m_cmdlist.sort();
|
||
|
}
|
||
|
|
||
|
return d->m_cmdlist;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQStringList KXmlCommandManager::commandListWithDescription()
|
||
15 years ago
|
{
|
||
|
preload();
|
||
14 years ago
|
TQStringList l;
|
||
15 years ago
|
for (TQMap<TQString,KXmlCommand*>::ConstIterator it=d->m_cmdmap.begin(); it!=d->m_cmdmap.end(); ++it)
|
||
15 years ago
|
l << (*it)->name() << (*it)->description();
|
||
|
|
||
|
return l;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString KXmlCommandManager::selectCommand(TQWidget *parent)
|
||
15 years ago
|
{
|
||
13 years ago
|
KLibrary *lib = KLibLoader::self()->library( "libtdeprint_management_module" );
|
||
15 years ago
|
if ( !lib )
|
||
|
{
|
||
|
KMessageBox::error( parent, i18n( "Unable to load KDE print management library: %1" ).arg( KLibLoader::self()->lastErrorMessage() ) );
|
||
15 years ago
|
return TQString::null;
|
||
15 years ago
|
}
|
||
|
else
|
||
|
{
|
||
15 years ago
|
TQString ( *func )( TQWidget* ) = ( TQString( * )( TQWidget* ) )lib->symbol( "select_command" );
|
||
15 years ago
|
if ( !func )
|
||
|
{
|
||
14 years ago
|
KMessageBox::error( parent, i18n( "Unable to find wizard object in management library." ) );
|
||
15 years ago
|
return TQString::null;
|
||
15 years ago
|
}
|
||
|
else
|
||
|
return func( parent );
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
KXmlCommand* KXmlCommandManager::command(const TQString& xmlId) const
|
||
15 years ago
|
{
|
||
13 years ago
|
return (d->m_cmdmap.contains(xmlId) ? d->m_cmdmap[xmlId] : 0);
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
int KXmlCommandManager::insertCommand(TQStringList& list, const TQString& filtername, bool defaultToStart)
|
||
15 years ago
|
{
|
||
|
preload();
|
||
|
|
||
|
int pos(0);
|
||
|
KXmlCommand *f1 = command(filtername), *f2 = 0;
|
||
|
if (f1 && f1->inputMimeTypes().count() > 0)
|
||
|
{
|
||
14 years ago
|
TQString mimetype = f1->inputMimeTypes()[0];
|
||
15 years ago
|
for (TQStringList::Iterator it=list.begin(); it!=list.end(); ++it, pos++)
|
||
15 years ago
|
{
|
||
|
f2 = command(*it);
|
||
|
if (!f2)
|
||
|
return -1; // Shouldn't happen
|
||
|
|
||
|
if (f2->acceptMimeType(f1->mimeType()) && f1->acceptMimeType(mimetype))
|
||
|
{
|
||
|
list.insert(it, filtername);
|
||
|
break;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
mimetype = f2->mimeType();
|
||
|
f2 = 0;
|
||
|
}
|
||
|
}
|
||
|
if (pos == (int)(list.count()))
|
||
|
{
|
||
|
if (list.count() == 0 || f1->acceptMimeType(mimetype))
|
||
|
list.append(filtername);
|
||
|
else if (defaultToStart)
|
||
|
{
|
||
|
pos = 0;
|
||
|
list.prepend(filtername);
|
||
|
}
|
||
|
else
|
||
|
pos = -1;
|
||
|
}
|
||
|
}
|
||
|
return pos;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQStringList KXmlCommandManager::autoConvert(const TQString& mimesrc, const TQString& mimedest)
|
||
15 years ago
|
{
|
||
14 years ago
|
TQStringList chain;
|
||
15 years ago
|
uint score(0);
|
||
|
|
||
|
preload();
|
||
|
|
||
13 years ago
|
if (d->m_mimemap.contains(mimesrc))
|
||
15 years ago
|
{
|
||
15 years ago
|
const TQValueList<KXmlCommand*> l = d->m_mimemap[mimesrc];
|
||
|
for (TQValueList<KXmlCommand*>::ConstIterator it=l.begin(); it!=l.end(); ++it)
|
||
15 years ago
|
{
|
||
|
// check filter availability
|
||
|
if (!KdeprintChecker::check((*it)->requirements()))
|
||
|
continue;
|
||
|
|
||
|
// direct filter: shortest path => return immediately
|
||
|
if ((*it)->mimeType() == mimedest)
|
||
|
{
|
||
15 years ago
|
chain = TQStringList((*it)->name());
|
||
15 years ago
|
break;
|
||
|
}
|
||
14 years ago
|
// non direct filter: find the shortest way between
|
||
15 years ago
|
// its output and mimedest (do not consider cyling filters)
|
||
|
else if ((*it)->mimeType() != mimesrc)
|
||
|
{
|
||
14 years ago
|
TQStringList subchain = autoConvert((*it)->mimeType(), mimedest);
|
||
15 years ago
|
// If chain length is 0, then there's no possible filter between those 2
|
||
14 years ago
|
// mime types. Just discard it. If the subchain contains also the current
|
||
15 years ago
|
// considered filter, then discard it: it denotes of a cycle in filter
|
||
|
// chain.
|
||
13 years ago
|
if (subchain.count() > 0 && subchain.findIndex((*it)->name()) == -1)
|
||
15 years ago
|
{
|
||
|
subchain.prepend((*it)->name());
|
||
|
if (subchain.count() < score || score == 0)
|
||
|
{
|
||
|
chain = subchain;
|
||
|
score = subchain.count();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
// At this point, either we have the shortest path, or empty
|
||
|
// list if nothing could be found
|
||
|
return chain;
|
||
|
}
|
||
|
|
||
15 years ago
|
bool KXmlCommandManager::checkCommand(const TQString& xmlId, int inputCheck, int outputCheck, TQString *msg)
|
||
15 years ago
|
{
|
||
|
KXmlCommand *xmlCmd = command(xmlId);
|
||
14 years ago
|
TQString errmsg;
|
||
15 years ago
|
bool needDestroy(false);
|
||
|
//kdDebug(500) << "checking command: " << xmlId << " (" << (xmlCmd != NULL) << ")" << endl;
|
||
|
if (!xmlCmd)
|
||
|
{
|
||
|
xmlCmd = loadCommand(xmlId, true);
|
||
|
needDestroy = (xmlCmd != 0);
|
||
|
}
|
||
|
|
||
|
bool status(true);
|
||
|
if (xmlCmd)
|
||
|
{
|
||
|
status = (xmlCmd->isValid() && KdeprintChecker::check(xmlCmd->requirements()));
|
||
|
if (!status)
|
||
|
errmsg = i18n("One of the command object's requirements is not met.");
|
||
|
}
|
||
14 years ago
|
TQString cmd = (xmlCmd ? xmlCmd->command() : xmlId);
|
||
15 years ago
|
if (status && !cmd.isEmpty() && (inputCheck > None || outputCheck > None))
|
||
|
{
|
||
13 years ago
|
if (inputCheck > None && (cmd.find("%in") == -1 || inputCheck == Advanced) && cmd.find("%filterinput") == -1)
|
||
15 years ago
|
{
|
||
|
status = false;
|
||
|
errmsg = i18n("The command does not contain the required tag %1.").arg(inputCheck == Advanced ? "%filterinput" : "{%in,%filterinput}");
|
||
|
}
|
||
13 years ago
|
if (status && outputCheck > None && (cmd.find("%out") == -1 || outputCheck == Advanced) && cmd.find("filteroutput") == -1)
|
||
15 years ago
|
{
|
||
|
status = false;
|
||
|
errmsg = i18n("The command does not contain the required tag %1.").arg(outputCheck == Advanced ? "%filteroutput" : "{%out,%filteroutput}");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (needDestroy)
|
||
|
delete xmlCmd;
|
||
|
|
||
|
if (msg)
|
||
|
*msg = errmsg;
|
||
|
|
||
|
return status;
|
||
|
}
|
||
|
|
||
15 years ago
|
bool KXmlCommandManager::configure(KXmlCommand *xmlCmd, TQWidget *parent)
|
||
15 years ago
|
{
|
||
|
if (xmlCmd->driver())
|
||
|
{
|
||
|
KDialogBase dlg(parent, 0, true, xmlCmd->description(), KDialogBase::Ok);
|
||
|
DriverView view(&dlg);
|
||
|
|
||
|
dlg.setMainWidget(&view);
|
||
|
view.setDriver(xmlCmd->driver());
|
||
|
dlg.resize(350,400);
|
||
|
dlg.exec();
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
return false;
|
||
|
}
|