File indexing completed on 2024-03-24 16:41:40

0001 /****************************************************************************************
0002   Copyright (C) 2003 by Jeroen Wijnhout (Jeroen.Wijnhout@kdemail.net)
0003             (C) 2011-2022 by Michel Ludwig (michel.ludwig@kdemail.net)
0004  ****************************************************************************************/
0005 
0006 /***************************************************************************
0007  *                                                                         *
0008  *   This program is free software; you can redistribute it and/or modify  *
0009  *   it under the terms of the GNU General Public License as published by  *
0010  *   the Free Software Foundation; either version 2 of the License, or     *
0011  *   (at your option) any later version.                                   *
0012  *                                                                         *
0013  ***************************************************************************/
0014 
0015 #ifndef KILETOOL_H
0016 #define KILETOOL_H
0017 
0018 #include <QHash>
0019 #include <QMap>
0020 #include <QObject>
0021 #include <QString>
0022 #include <QStringList>
0023 
0024 #include <KLocalizedString>
0025 
0026 #include "kilelauncher.h"
0027 #include "outputinfo.h"
0028 
0029 class KConfig;
0030 class KileInfo;
0031 class KileProject;
0032 
0033 namespace KileTool
0034 {
0035 typedef QMap<QString, QString> Config;
0036 
0037 class Factory;
0038 class Manager;
0039 
0040 /**
0041 * A class that defines a general tool (latex, dvips etc.) to be launched from
0042 * within Kile.
0043 *
0044 * @author Jeroen Wijnhout
0045 **/
0046 
0047 class Base : public QObject
0048 {
0049     Q_OBJECT
0050     friend class KileTool::Factory;
0051 
0052     // only the factory can create tools
0053 protected:
0054     Base(const QString &name, Manager *manager, bool prepare = true);
0055 public:
0056     ~Base();
0057 
0058     /**
0059      * Sets the KileInfo object, this is already taken care of by the Manager.
0060      **/
0061     void setInfo(KileInfo *ki) {
0062         m_ki = ki;
0063     }
0064 
0065     /**
0066      * Sets the KConfig object, this is already taken care of by the Manager.
0067      **/
0068     void setConfig(KConfig *config) {
0069         m_config = config;
0070     }
0071 
0072     /**
0073      * @returns the Manager object for this tool.
0074      **/
0075     Manager* manager() const {
0076         return m_manager;
0077     }
0078 
0079     /**
0080      * @returns a short descriptive name for this tool.
0081      **/
0082     const QString& name() const {
0083         return m_name;
0084     }
0085 
0086     /**
0087      * Allows you to set the source file and working directory explicitly (absolute path).
0088      **/
0089     virtual void setSource(const QString& source, const QString& workingDir = "");
0090 
0091     /**
0092      * @returns the source file that is used to run the tool on.
0093      **/
0094     QString source(bool absolute = true) const;
0095 
0096     QString S() const {
0097         return m_S;
0098     }
0099     QString baseDir() const {
0100         return m_basedir;
0101     }
0102     QString relativeDir() const {
0103         return m_relativedir;
0104     }
0105     QString targetDir() const {
0106         return m_targetdir;
0107     }
0108     inline QString from() const {
0109         return readEntry("from");
0110     }
0111     inline QString to() const {
0112         return readEntry("to");
0113     }
0114     QString target() const {
0115         return m_target;
0116     }
0117     QString options() const {
0118         return m_options;
0119     }
0120 
0121     QString toolConfig() const {
0122         return m_toolConfig;
0123     }
0124 
0125     void setOptions(const QString& opt) {
0126         m_options = opt;
0127     }
0128 
0129     virtual bool isViewer() {
0130         return false;
0131     }
0132 
0133     void setQuickie() {
0134         m_quickie = true;
0135     }
0136     bool isQuickie() {
0137         return m_quickie;
0138     }
0139 
0140     /**
0141      * Returns true iff all documents must be saved before the tool can be launched
0142      **/
0143     virtual bool requestSaveAll();
0144 
0145     void setPartOfLivePreview() {
0146         m_isPartOfLivePreview = true;
0147     }
0148     bool isPartOfLivePreview() const {
0149         return m_isPartOfLivePreview;
0150     }
0151 
0152     void setTeXInputPaths(const QString& s);
0153     QString teXInputPaths() const;
0154     void setBibInputPaths(const QString& s);
0155     QString bibInputPaths() const;
0156     void setBstInputPaths(const QString& s);
0157     QString bstInputPaths() const;
0158     void copyPaths(Base* tool);
0159 
0160     /**
0161      * Allows you to set the target file explicitly (filename only).
0162      **/
0163     virtual void setTarget(const QString & target);
0164     virtual void setTargetDir(const QString & target);
0165     virtual void setTargetPath(const QString & target);
0166 
0167     /**
0168      * Sets the target directory relative to the source directory.
0169      **/
0170     void setRelativeBaseDir(const QString & dir) {
0171         m_relativedir = dir;
0172     }
0173 
0174     /**
0175      * Installs a launcher object that will be responsible for actually starting the tool. The
0176      * tool can be a command-line tool or a kpart, the KileTool class doesn't need to know
0177      * about the specifics of the launcher.
0178      **/
0179     void installLauncher(Launcher *lr );
0180 
0181     /**
0182      * Installs a launcher as indicated by the tool type. This creates a launcher object.
0183      **/
0184     bool installLauncher();
0185 
0186     /**
0187      * @returns a pointer to the launcher object, returns 0 if no launcher is installed.
0188      **/
0189     Launcher *launcher() {
0190         return m_launcher;
0191     }
0192 
0193     /**
0194      * @returns the working dir for this tool.
0195      **/
0196     const QString &workingDir() const {
0197         return m_workingDir;
0198     }
0199 
0200     void setWorkingDir(const QString& s) {
0201         m_workingDir = s;
0202     }
0203 
0204     /**
0205      * @returns the dictionary that translates the following keys
0206     Example docu:
0207     Consider a file which is called myBestBook.tex which resides in /home/thomas/latex and you compile it with pdflatex to myBestBook.pdf.
0208 
0209     The variables have the following meanings:
0210     %source ->  filename with suffix but without path <-> myBestBook.tex
0211     %S ->  filename without suffix but without path <-> myBestBook
0212     %dir_base  -> path of the source file without filename  <-> /home/thomas/latex
0213     %dir_target -> path of the target file without filename, same as %dir_base if no relative path has been set <-> /home/thomas/latex
0214     %target -> target filename without path <-> without filename
0215 
0216     And these are special variables:
0217     %res <-> resolution of the quickpreview action set in configure kile->tools->preview
0218 
0219     %AFL <-> List of all files in a project marked for archiving. You can set the archive flag in the "Files and projects" sidebar using the context menu.
0220 
0221     %absolute_target -> Used in conjunction with Okular to inform it about the cursor position for
0222                         the ForwardDVI/PDF feature
0223 
0224     %sourceFileName <-> Source file name (for synchronizing the cursor location with the viewer)
0225     %sourceLine <-> Line in the source file on which the cursor is located (for synchronizing the cursor location with the viewer)
0226     */
0227     QHash<QString,QString>& paramDict() {
0228         return m_dictParams;
0229     }
0230 
0231     bool addDict(const QString& key, const QString& value);
0232 
0233     void translate(QString &str, bool quoteForShell = false);
0234 
0235     void setFlags(uint flags) {
0236         m_flags = flags;
0237     }
0238     uint flags() {
0239         return m_flags;
0240     }
0241     void removeFlag(uint flag);
0242 
0243     void setMsg(long n, const KLocalizedString& msg);
0244     KLocalizedString msg(long n) const {
0245         return m_messages[n];
0246     }
0247 
0248     virtual void setupAsChildTool(KileTool::Base *child);
0249 
0250 public Q_SLOTS:
0251     void sendMessage(int, const QString &);
0252     virtual void filterOutput(const QString &);
0253 
0254     /**
0255      * Starts the tool. First it performs basic checks (checkPrereqs()),
0256      * if all is well it launches the tool (launch()). After the process has
0257      * exited it calls finish().
0258      * @return the exit code of the tool (if available)
0259      **/
0260     virtual int run();
0261 
0262     /**
0263      * Terminates the running process.
0264      **/
0265     virtual void stop();
0266 
0267     /**
0268      * Clean up after the process/lib has finished.
0269      **/
0270     virtual bool finish(int);
0271 
0272     void installLaTeXOutputParserResult(int nErrors, int nWarnings, int nBadBoxes, const LatexOutputInfoArray& outputList,
0273                                         const QString& logFile);
0274 
0275 Q_SIGNALS:
0276     void message(int, const QString &, const QString &);
0277     void output(const QString &);
0278 
0279     void start(KileTool::Base*);
0280     void done(KileTool::Base*, int, bool childToolSpawned);
0281     void failedToRun(KileTool::Base*, int);
0282 
0283     void aboutToBeDestroyed(KileTool::Base*);
0284 
0285 public:
0286     void setEntryMap(Config map) {
0287         m_entryMap = map;
0288     }
0289     void setEntry(const QString& key, const QString& value);
0290     const QString readEntry(const QString& key) const {
0291         return m_entryMap[key];
0292     }
0293 
0294     virtual void prepareToRun();
0295     bool isPrepared() {
0296         return m_bPrepared;
0297     }
0298     bool needsToBePrepared() {
0299         return m_bPrepareToRun;
0300     }
0301 
0302 protected:
0303     Launcher    *m_launcher;
0304     bool        m_quickie;
0305     bool        m_isPartOfLivePreview;
0306 
0307     bool needsUpdate(const QString &target, const QString &source);
0308 
0309     /**
0310      * Checks if the prerequisites are in order.
0311      * @returns true if everything is ok, false otherwise.
0312      **/
0313     virtual bool checkPrereqs();
0314 
0315     /**
0316      * Determines on which file to run the tool.
0317      **/
0318     virtual bool determineSource();
0319 
0320     /**
0321      * Determines the target of the tool (i.e. a DVI for latex, PS for dvips) and
0322      * checks if the target file can be written to the specified location.
0323      **/
0324     virtual bool determineTarget();
0325 
0326     /**
0327      * Check if the target dir and file have the correct permissions (according to the flags set).
0328      **/
0329     virtual bool checkTarget();
0330 
0331     virtual bool checkSource();
0332 
0333     void runChildNext(Base *tool, bool block = false);
0334 
0335     void setToolConfig(const QString& config) {
0336         m_toolConfig = config;
0337     }
0338 
0339     virtual void latexOutputParserResultInstalled();
0340 
0341 protected:
0342     Manager         *m_manager;
0343     KileInfo        *m_ki;
0344     KConfig         *m_config;
0345 
0346 private:
0347     QString         m_name;
0348     QString         m_target, m_basedir, m_relativedir, m_targetdir, m_source, m_S, m_workingDir;
0349     QString         m_options;
0350     QString         m_resolution;
0351 
0352     QString         m_message;
0353 
0354     bool            m_buildPrereqs;
0355 
0356     QHash<QString,QString> m_dictParams;
0357     Config          m_entryMap;
0358 
0359     uint                m_flags;
0360     int         m_nPreparationResult;
0361     bool            m_bPrepared;
0362     bool            m_bPrepareToRun;
0363     QString         m_toolConfig;
0364 
0365     QString m_texInputs, m_bibInputs, m_bstInputs;
0366 
0367     //messages
0368     QMap<long, KLocalizedString>    m_messages;
0369 
0370 protected:
0371     int         m_nErrors, m_nWarnings, m_nBadBoxes;
0372     LatexOutputInfoArray    m_latexOutputInfoList;
0373     QString         m_logFile;
0374     bool            m_childToolSpawned;
0375     int         m_toolResult;
0376 };
0377 
0378 /**
0379  * A class that represents a compile tool (such as latex, pdflatex).
0380  **/
0381 class Compile : public Base
0382 {
0383     friend class KileTool::Factory;
0384 protected:
0385     Compile(const QString &name, Manager * manager, bool prepare = true);
0386 public:
0387     ~Compile();
0388 
0389 protected:
0390     virtual bool checkSource() override;
0391 };
0392 
0393 /**
0394  * A class that represents a view tool (such as KDVI, gv, etc.).
0395  **/
0396 class View : public Base
0397 {
0398     friend class KileTool::Factory;
0399 protected:
0400     View(const QString &name, Manager * manager, bool prepare = true);
0401 public:
0402     ~View();
0403 
0404     virtual bool isViewer() override {
0405         return true;
0406     }
0407 };
0408 
0409 /**
0410  * A class that represents a conversion tool (such as dvips).
0411  **/
0412 class Convert : public Base
0413 {
0414     friend class KileTool::Factory;
0415 protected:
0416     Convert(const QString &name, Manager * manager, bool prepare = true);
0417 public:
0418     ~Convert();
0419 
0420     virtual bool determineSource() override;
0421 };
0422 
0423 /**
0424  * A class that represents a tool like tar, from multiple files to one file
0425  **/
0426 class Archive: public Base
0427 {
0428     Q_OBJECT
0429     friend class KileTool::Factory;
0430 
0431 protected:
0432     Archive(const QString &name, Manager * manager, bool prepare = true);
0433 public:
0434     ~Archive();
0435     virtual bool checkPrereqs() override;
0436     virtual void setSource(const QString & source, const QString& workingDir = "") override;
0437 private:
0438     KileProject *m_project;
0439     QString m_fileList;
0440 };
0441 
0442 class Sequence : public Base
0443 {
0444     Q_OBJECT
0445     friend class KileTool::Factory;
0446 
0447     virtual bool requestSaveAll() override;
0448 
0449     void setupSequenceTools();
0450 
0451     LaTeXOutputHandler* latexOutputHandler();
0452     void setLaTeXOutputHandler(LaTeXOutputHandler *h);
0453 
0454 public Q_SLOTS:
0455     virtual int run() override;
0456 
0457 protected:
0458     Sequence(const QString &name, Manager *manager, bool prepare = true);
0459     ~Sequence();
0460 
0461     // will also determine the current LaTeXOutputHandler
0462     virtual bool determineSource() override;
0463 
0464     std::list<Base*> m_tools;
0465     QString m_unknownToolSpec;
0466     LaTeXOutputHandler *m_latexOutputHandler;
0467 };
0468 }
0469 
0470 #endif