<commit_before>/* This file is part of the KDE libraries
   Copyright (C) 2001-2003 <NAME> <<EMAIL>>

   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.
*/

//BEGIN Includes
#include "katefiletype.h"
#include "katefiletype.moc"

#include "katedocument.h"
#include "kateconfig.h"
#include "kateview.h"
#include "kateglobal.h"

#include "ui_filetypeconfigwidget.h"

#include <kconfig.h>
#include <kmimemagic.h>
#include <kmimetype.h>
#include <kmimetypechooser.h>
#include <kdebug.h>
#include <kiconloader.h>
#include <knuminput.h>
#include <klocale.h>
#include <kmenu.h>

#include <qregexp.h>
#include <qcheckbox.h>
#include <qcombobox.h>
#include <QGroupBox>

#include <qlabel.h>
#include <qlayout.h>
#include <qlineedit.h>
#include <qpushbutton.h>
#include <qtoolbutton.h>
#include <kvbox.h>

#define KATE_FT_HOWMANY 1024
//END Includes

//BEGIN KateFileTypeManager
KateFileTypeManager::KateFileTypeManager ()
{
  update ();
}

KateFileTypeManager::~KateFileTypeManager ()
{
}

//
// read the types from config file and update the internal list
//
void KateFileTypeManager::update ()
{
  KConfig config ("katefiletyperc", false, false);

  QStringList g (config.groupList());
  g.sort ();

  m_types.clear ();
  for (int z=0; z < g.count(); z++)
  {
    config.setGroup (g[z]);

    KateFileType type;
    type.number = z;
    type.name = g[z];
    type.section = config.readEntry ("Section");
    type.wildcards = config.readEntry ("Wildcards", QStringList(), ';');
    type.mimetypes = config.readEntry ("Mimetypes", QStringList(), ';');
    type.priority = config.readEntry ("Priority", 0);
    type.varLine = config.readEntry ("Variables");

    m_types.append(type);
  }
}

//
// save the given list to config file + update
//
void KateFileTypeManager::save (const QList<KateFileType>& v)
{
  KConfig config ("katefiletyperc", false, false);

  QStringList newg;
  foreach (const KateFileType& type, v)
  {
    config.setGroup(type.name);

    config.writeEntry ("Section", type.section);
    config.writeEntry ("Wildcards", type.wildcards, ';');
    config.writeEntry ("Mimetypes", type.mimetypes, ';');
    config.writeEntry ("Priority", type.priority);

    QString varLine = type.varLine;
    if (QRegExp("kate:(.*)").indexIn(varLine) < 0)
      varLine.prepend ("kate: ");

    config.writeEntry ("Variables", varLine);

    newg << type.name;
  }

  QStringList g (config.groupList());

  for (int z=0; z < g.count(); z++)
  {
    if (newg.indexOf (g[z]) == -1)
      config.deleteGroup (g[z]);
  }

  config.sync ();

  update ();
}

int KateFileTypeManager::fileType (KateDocument *doc)
{
  kDebug(13020)<<k_funcinfo<<endl;
  if (!doc)
    return -1;

  if (m_types.isEmpty())
    return -1;

  QString fileName = doc->url().prettyUrl();
  int length = doc->url().prettyUrl().length();

  int result;

  // Try wildcards
  if ( ! fileName.isEmpty() )
  {
    static QStringList commonSuffixes = QString(".orig;.new;~;.bak;.BAK").split (";");

    if ((result = wildcardsFind(fileName)) != -1)
      return result;

    QString backupSuffix = KateDocumentConfig::global()->backupSuffix();
    if (fileName.endsWith(backupSuffix)) {
      if ((result = wildcardsFind(fileName.left(length - backupSuffix.length()))) != -1)
        return result;
    }

    for (QStringList::Iterator it = commonSuffixes.begin(); it != commonSuffixes.end(); ++it) {
      if (*it != backupSuffix && fileName.endsWith(*it)) {
        if ((result = wildcardsFind(fileName.left(length - (*it).length()))) != -1)
          return result;
      }
    }
  }

  // Even try the document name, if the URL is empty
  // This is usefull if the document name is set for example by a plugin which
  // created the document
  else if ( (result = wildcardsFind(doc->documentName())) != -1)
  {
    kDebug(13020)<<"KateFiletype::filetype(): got type "<<result<<" using docName '"<<doc->documentName()<<"'"<<endl;
    return result;
  }

  // Try content-based mimetype
  KMimeType::Ptr mt = doc->mimeTypeForContent();

  QList<KateFileType> types;

  foreach (const KateFileType& type, m_types)
  {
    if (type.mimetypes.indexOf (mt->name()) > -1)
      types.append (type);
  }

  if ( !types.isEmpty() )
  {
    int pri = -1;
    int hl = -1;

    foreach (const KateFileType& type, types)
    {
      if (type.priority > pri)
      {
        pri = type.priority;
        hl = type.number;
      }
    }

    return hl;<commit_msg>do not crash if mime type magic failed to return the mimetype<commit_after>/* This file is part of the KDE libraries
   Copyright (C) 2001-2003 <NAME> <<EMAIL>>

   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.
*/

//BEGIN Includes
#include "katefiletype.h"
#include "katefiletype.moc"

#include "katedocument.h"
#include "kateconfig.h"
#include "kateview.h"
#include "kateglobal.h"

#include "ui_filetypeconfigwidget.h"

#include <kconfig.h>
#include <kmimemagic.h>
#include <kmimetype.h>
#include <kmimetypechooser.h>
#include <kdebug.h>
#include <kiconloader.h>
#include <knuminput.h>
#include <klocale.h>
#include <kmenu.h>

#include <qregexp.h>
#include <qcheckbox.h>
#include <qcombobox.h>
#include <QGroupBox>

#include <qlabel.h>
#include <qlayout.h>
#include <qlineedit.h>
#include <qpushbutton.h>
#include <qtoolbutton.h>
#include <kvbox.h>

#define KATE_FT_HOWMANY 1024
//END Includes

//BEGIN KateFileTypeManager
KateFileTypeManager::KateFileTypeManager ()
{
  update ();
}

KateFileTypeManager::~KateFileTypeManager ()
{
}

//
// read the types from config file and update the internal list
//
void KateFileTypeManager::update ()
{
  KConfig config ("katefiletyperc", false, false);

  QStringList g (config.groupList());
  g.sort ();

  m_types.clear ();
  for (int z=0; z < g.count(); z++)
  {
    config.setGroup (g[z]);

    KateFileType type;
    type.number = z;
    type.name = g[z];
    type.section = config.readEntry ("Section");
    type.wildcards = config.readEntry ("Wildcards", QStringList(), ';');
    type.mimetypes = config.readEntry ("Mimetypes", QStringList(), ';');
    type.priority = config.readEntry ("Priority", 0);
    type.varLine = config.readEntry ("Variables");

    m_types.append(type);
  }
}

//
// save the given list to config file + update
//
void KateFileTypeManager::save (const QList<KateFileType>& v)
{
  KConfig config ("katefiletyperc", false, false);

  QStringList newg;
  foreach (const KateFileType& type, v)
  {
    config.setGroup(type.name);

    config.writeEntry ("Section", type.section);
    config.writeEntry ("Wildcards", type.wildcards, ';');
    config.writeEntry ("Mimetypes", type.mimetypes, ';');
    config.writeEntry ("Priority", type.priority);

    QString varLine = type.varLine;
    if (QRegExp("kate:(.*)").indexIn(varLine) < 0)
      varLine.prepend ("kate: ");

    config.writeEntry ("Variables", varLine);

    newg << type.name;
  }

  QStringList g (config.groupList());

  for (int z=0; z < g.count(); z++)
  {
    if (newg.indexOf (g[z]) == -1)
      config.deleteGroup (g[z]);
  }

  config.sync ();

  update ();
}

int KateFileTypeManager::fileType (KateDocument *doc)
{
  kDebug(13020)<<k_funcinfo<<endl;
  if (!doc)
    return -1;

  if (m_types.isEmpty())
    return -1;

  QString fileName = doc->url().prettyUrl();
  int length = doc->url().prettyUrl().length();

  int result;

  // Try wildcards
  if ( ! fileName.isEmpty() )
  {
    static QStringList commonSuffixes = QString(".orig;.new;~;.bak;.BAK").split (";");

    if ((result = wildcardsFind(fileName)) != -1)
      return result;

    QString backupSuffix = KateDocumentConfig::global()->backupSuffix();
    if (fileName.endsWith(backupSuffix)) {
      if ((result = wildcardsFind(fileName.left(length - backupSuffix.length()))) != -1)
        return result;
    }

    for (QStringList::Iterator it = commonSuffixes.begin(); it != commonSuffixes.end(); ++it) {
      if (*it != backupSuffix && fileName.endsWith(*it)) {
        if ((result = wildcardsFind(fileName.left(length - (*it).length()))) != -1)
          return result;
      }
    }
  }

  // Even try the document name, if the URL is empty
  // This is usefull if the document name is set for example by a plugin which
  // created the document
  else if ( (result = wildcardsFind(doc->documentName())) != -1)
  {
    kDebug(13020)<<"KateFiletype::filetype(): got type "<<result<<" using docName '"<<doc->documentName()<<"'"<<endl;
    return result;
  }

  // Try content-based mimetype
  KMimeType::Ptr mt = doc->mimeTypeForContent();
  
  if (!mt)
    return;

  QList<KateFileType> types;

  foreach (const KateFileType& type, m_types)
  {
    if (type.mimetypes.indexOf (mt->name()) > -1)
      types.append (type);
  }

  if ( !types.isEmpty() )
  {
    int pri = -1;
    int hl = -1;

    foreach (const KateFileType& type, types)
    {
      if (type.priority > pri)
      {
        pri = type.priority;
        hl = type.number;
      }
    }

    return hl;
{'fraction_non_alphanumeric': 0.14464915719325755, 'fraction_numerical': 0.009408075264602116, 'mean_word_length': 3.4680385288966726, 'pattern_counts': {'":': 0, '<': 93, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6115964', 'n_tokens_mistral': 3491, 'n_tokens_neox': 3342, 'n_words': 931}