File indexing completed on 2024-04-21 15:55:39

0001 /*****************************************************************************
0002  *   Copyright (C) 2007 by Holger Danielsson (holger.danielsson@versanet.de) *
0003  *             (C) 2016-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 #include "kileextensions.h"
0016 
0017 #include <QFileInfo>
0018 
0019 #include <KLocalizedString>
0020 #include "kiledebug.h"
0021 
0022 namespace KileDocument
0023 {
0024 
0025 //////////////////// Extensions ////////////////////
0026 
0027 Extensions::Extensions()
0028 {
0029     m_documents = ".tex .ltx .latex .dtx .ins";
0030     m_packages = ".cls .sty .bbx .cbx .lbx .dbx";
0031     m_bibtex = ".bib";
0032     m_metapost = ".mp";
0033     m_script = ".js";
0034     m_project = ".kilepr";
0035     //m_images = ".eps .pdf .dvi .ps .fig .gif .jpg .jpeg .png";
0036     m_images = ".eps .jpg .jpeg .png .pdf .ps .fig .gif";
0037 
0038     m_latexDefault = ".tex";
0039     m_bibtexDefault = ".bib";
0040     m_metapostDefault = ".mp";
0041     m_scriptDefault = ".js";
0042     m_projectDefault = ".kilepr";
0043 }
0044 
0045 //////////////////// file filter ////////////////////
0046 
0047 QString Extensions::fileFilterKDEStyle(ExtensionType type) const
0048 {
0049     QString ext, text;
0050     fileFilterRaw(type, ext, text);
0051 
0052     ext.replace('.', "*.");
0053     return ext + '|' + text;
0054 }
0055 
0056 QString Extensions::fileFilterQtStyle(ExtensionType type) const
0057 {
0058     QString ext, text;
0059     fileFilterRaw(type, ext, text);
0060 
0061     ext.replace('.', "*.");
0062     return text + QStringLiteral(" (") + ext + ')';
0063 }
0064 
0065 void Extensions::fileFilterRaw(ExtensionType type, QString& ext, QString& text) const
0066 {
0067     switch(type) {
0068     case TEX:
0069         ext = m_documents;
0070         text = i18n("(La)TeX Source Files");
0071         return;
0072     case PACKAGES:
0073         ext = m_packages;
0074         text = i18n("(La)TeX Packages");
0075         return;
0076     case BIB:
0077         ext = m_bibtex;
0078         text = i18n("BibTeX Files");
0079         return;
0080     case IMG:
0081         ext = m_images;
0082         text = i18n("Image Files");
0083         return;
0084     case METAPOST:
0085         ext = m_metapost;
0086         text = i18n("Metapost Files");
0087         return;
0088     case JS:
0089         ext = m_script;
0090         text = i18n("Kile Script Files");
0091         return;
0092     case KILE_PROJECT:
0093         ext = m_project;
0094         text = i18n("Kile Project Files");
0095         return;
0096     }
0097 }
0098 
0099 QString Extensions::fileFilterKDEStyle(bool includeAllFiles, const std::list<ExtensionType>& extensions) const
0100 {
0101     QString toReturn;
0102 
0103     for(ExtensionType extension : extensions) {
0104         toReturn += fileFilterKDEStyle(extension) + '\n';
0105     }
0106 
0107     if(includeAllFiles) {
0108         toReturn += i18n("* |All Files");
0109     }
0110 
0111     return toReturn;
0112 }
0113 
0114 QString Extensions::fileFilterQtStyle(bool includeAllFiles, const std::list<ExtensionType>& extensions) const
0115 {
0116     QString toReturn;
0117 
0118     for(ExtensionType extension : extensions) {
0119         toReturn += fileFilterQtStyle(extension) + QStringLiteral(";;");
0120     }
0121 
0122     if(includeAllFiles) {
0123         toReturn += i18n("All Files (*)");
0124     }
0125 
0126     return toReturn;
0127 }
0128 
0129 //////////////////// document type ////////////////////
0130 
0131 bool Extensions::isTexFile(const QString &fileName) const
0132 {
0133     //TODO use mimetype
0134     QString ext = '.' + QFileInfo(fileName).suffix();
0135     return isLatexDocument(ext) || isLatexPackage(ext);
0136 }
0137 
0138 bool Extensions::isBibFile(const QString &fileName) const
0139 {
0140     QString ext = '.' + QFileInfo(fileName).suffix();
0141     return isBibtex(ext);
0142 }
0143 
0144 bool Extensions::isScriptFile(const QString &fileName) const
0145 {
0146     QString ext = '.' + QFileInfo(fileName).suffix();
0147     return isScript(ext);
0148 }
0149 
0150 bool Extensions::isProjectFile(const QString &fileName) const
0151 {
0152     QString ext = '.' + QFileInfo(fileName).suffix();
0153     return isProject(ext);
0154 }
0155 
0156 bool Extensions::validExtension(const QString &ext, const QString &extensions) const
0157 {
0158     const QStringList extlist = extensions.split(' ');
0159     for(QStringList::ConstIterator it = extlist.constBegin(); it != extlist.constEnd(); ++it) {
0160         if((*it) == ext) {
0161             return true;
0162         }
0163     }
0164 
0165     return false;
0166 }
0167 
0168 Type Extensions::determineDocumentType(const QUrl &url) const
0169 {
0170     if ( isTexFile(url) )
0171     {
0172         return KileDocument::LaTeX;
0173     }
0174     else if ( isBibFile(url) )
0175     {
0176         return KileDocument::BibTeX;
0177     }
0178     else if ( isScriptFile(url) )
0179     {
0180         return KileDocument::Script;
0181     }
0182     else
0183     {
0184         return KileDocument::Text;
0185     }
0186 }
0187 
0188 QString Extensions::defaultExtensionForDocumentType(KileDocument::Type type) const
0189 {
0190     switch(type) {
0191     case KileDocument::LaTeX:
0192         return m_latexDefault;
0193 
0194     case KileDocument::BibTeX:
0195         return m_bibtexDefault;
0196 
0197     case KileDocument::Script:
0198         return m_scriptDefault;
0199 
0200     case KileDocument::Text:
0201     /* fall through */
0202     case KileDocument::Undefined:
0203         /* do nothing */
0204         break;
0205     }
0206     return QString();
0207 }
0208 
0209 }