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

0001 /***************************************************************************************
0002     begin                : Sat Apr 26 2003
0003     copyright            : (C) 2003 by Jeroen Wijnhout (wijnhout@science.uva.nl)
0004                                2007-2019 by Michel Ludwig (michel.ludwig@kdemail.net)
0005  ***************************************************************************************/
0006 
0007 /***************************************************************************
0008  *                                                                         *
0009  *   This program is free software; you can redistribute it and/or modify  *
0010  *   it under the terms of the GNU General Public License as published by  *
0011  *   the Free Software Foundation; either version 2 of the License, or     *
0012  *   (at your option) any later version.                                   *
0013  *                                                                         *
0014  ***************************************************************************/
0015 
0016 #ifndef TEMPLATES_H
0017 #define TEMPLATES_H
0018 
0019 #include <QListWidget>
0020 #include <QObject>
0021 
0022 #include <QProcess>
0023 
0024 #include <KLocalizedString>
0025 #include <QUrl>
0026 
0027 #include "kileconstants.h"
0028 
0029 #define DEFAULT_EMPTY_ICON "type_Empty"
0030 
0031 class KProcess;
0032 
0033 class KileInfo;
0034 
0035 namespace KileTemplate {
0036 
0037 struct Info {
0038 public:
0039     Info();
0040 
0041     QString name;
0042     QString path;
0043     QString icon;
0044     KileDocument::Type type;
0045 
0046     bool operator==(const Info &ti) const;
0047 };
0048 
0049 typedef QList<Info> TemplateList;
0050 typedef QList<Info>::iterator TemplateListIterator;
0051 typedef QList<Info>::const_iterator TemplateListConstIterator;
0052 
0053 class Manager : public QObject {
0054     Q_OBJECT
0055 
0056 public:
0057     explicit Manager(KileInfo *info, QObject* parent = Q_NULLPTR, const char* name = NULL);
0058     virtual ~Manager();
0059 
0060     void scanForTemplates();
0061 
0062     /**
0063     * Get all the templates.
0064     **/
0065     TemplateList getAllTemplates() const;
0066 
0067     /**
0068     * Get all the templates of a certain type.
0069     *
0070     * @param type The type of the templates that should be returned. You can pass "KileDocument::Undefined" to
0071     *             retrieve every template.
0072     **/
0073     TemplateList getTemplates(KileDocument::Type type) const;
0074 
0075     /**
0076      * Checks whether a template with a given name and type exists.
0077      *
0078      * @return true iff a template with the given name and type could be found
0079      **/
0080     bool searchForTemplate(const QString& name, KileDocument::Type& type) const;
0081 
0082     // Add a template in .kde/share/kile/templates/
0083     // This function will override any existing template.
0084     bool add(const QUrl &templateSourceURL, const QString &name, const QUrl &icon);
0085 
0086     // Remove a template from .kde/share/kile/templates/
0087     bool remove(KileTemplate::Info ti);
0088 
0089     // these have to be methods as we cannot use i18n calls in global objects
0090     static QString defaultEmptyTemplateCaption();
0091     static QString defaultEmptyLaTeXTemplateCaption();
0092     static QString defaultEmptyBibTeXTemplateCaption();
0093 
0094 protected:
0095     KileInfo* m_kileInfo;
0096 
0097 private:
0098     bool copyAppData(const QUrl &src, const QString& subdir, const QString& fileName);
0099     bool removeAppData(const QString &file);
0100 
0101     /**
0102      * Adds a new template. This method differs from the other add method in that it does not try to determine
0103      * the type of the template from the passed source URL.
0104      **/
0105     bool add(const QUrl &templateSourceURL, KileDocument::Type type, const QString& name, const QUrl& icon);
0106 
0107 
0108 private:
0109     TemplateList m_TemplateList;
0110     QString m_TempFilePath;
0111 };
0112 
0113 }
0114 
0115 
0116 class TemplateItem : public QListWidgetItem
0117 {
0118 public:
0119     TemplateItem( QListWidget * parent, const KileTemplate::Info & info);
0120     ~TemplateItem() {}
0121 
0122     bool operator<(const QListWidgetItem &other) const override;
0123 
0124     QString name() {
0125         return m_info.name;
0126     }
0127     QString path() {
0128         return m_info.path;
0129     }
0130     QString icon() {
0131         return m_info.icon;
0132     }
0133     KileDocument::Type type() {
0134         return m_info.type;
0135     }
0136 
0137 private:
0138     KileTemplate::Info m_info;
0139 };
0140 
0141 class TemplateIconView : public QListWidget {
0142     Q_OBJECT
0143 
0144 public:
0145     explicit TemplateIconView(QWidget *parent = Q_NULLPTR);
0146     virtual ~TemplateIconView();
0147 
0148     void setTemplateManager(KileTemplate::Manager *templateManager);
0149 
0150     void fillWithTemplates(KileDocument::Type type);
0151 
0152 Q_SIGNALS:
0153     void classFileSearchFinished();
0154 
0155 protected:
0156     KileTemplate::Manager *m_templateManager;
0157     QString m_output;
0158     KProcess *m_proc;
0159 
0160     void addTemplateIcons(KileDocument::Type type);
0161     void searchLaTeXClassFiles();
0162 
0163 protected Q_SLOTS:
0164     void slotProcessError();
0165     void slotProcessOutput();
0166     void slotProcessExited(int exitCode, QProcess::ExitStatus exitStatus);
0167 };
0168 
0169 #endif