<commit_before>/*
   This file is part of the Kate text editor of the KDE project.

   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.

   ---
   Copyright (C) 2004, <NAME> <<EMAIL>>
*/
// TODO
// Icons
// Direct shortcut setting
//BEGIN Includes
#include "kateexternaltools.h"
#include "kateexternaltools.moc"
#include <KTextEditor/View>
#include <KTextEditor/Document>
#include <KTextEditor/Editor>

#include <KActionCollection>
#include <KListWidget>
#include <KLocale>
#include <KIconLoader>
#include <KMessageBox>
#include <KMimeTypeChooser>
#include <KXmlGuiWindow>
#include <KConfig>
#include <KConfigGroup>
#include <KComboBox>
#include <KLineEdit>
#include <KRun>
#include <KIconDialog>
#include <KMenu>
#include <KSharedConfig>
#include <kdebug.h>

#include <QBitmap>
#include <QFile>
#include <QLabel>
#include <QPushButton>
#include <QRegExp>
#include <QToolButton>
#include <QGridLayout>
#include <QObject>
#include <QTextEdit>

#include <unistd.h>
//END Includes

KateExternalToolsCommand *KateExternalToolsCommand::s_self = 0;

//BEGIN KateExternalTool
KateExternalTool::KateExternalTool( const QString &name,
                                    const QString &command,
                                    const QString &icon,
                                    const QString &tryexec,
                                    const QStringList &mimetypes,
                                    const QString &acname,
                                    const QString &cmdname,
                                    int save )
    : name ( name ),
    command ( command ),
    icon ( icon ),
    tryexec ( tryexec ),
    mimetypes ( mimetypes ),
    acname ( acname ),
    cmdname ( cmdname ),
    save ( save )
{
  //if ( ! tryexec.isEmpty() )
  hasexec = checkExec();
}

bool KateExternalTool::checkExec()
{
  // if tryexec is empty, it is the first word of command
  if ( tryexec.isEmpty() )
    tryexec = command.section( " ", 0, 0, QString::SectionSkipEmpty );

  // NOTE this code is modified taken from kdesktopfile.cpp, from KDesktopFile::tryExec()
  if (!tryexec.isEmpty())
  {
    if (tryexec[0] == '/')
    {
      if (::access(QFile::encodeName(tryexec), R_OK | X_OK))
        return false;

      m_exec = tryexec;
    }
    else
    {
      // !!! <NAME> <<EMAIL>> !!!
      // Environment PATH may contain filenames in 8bit locale cpecified
      // encoding (Like a filenames).
      const QString path = QFile::decodeName(qgetenv("PATH"));
#ifdef Q_WS_WIN
      const QStringList dirs = path.split(';', QString::SkipEmptyParts);
#else
      const QStringList dirs = path.split(':', QString::SkipEmptyParts);
#endif
      QStringList::ConstIterator it(dirs.begin());
      bool match = false;
      for (; it != dirs.end(); ++it)
      {
        QString fName = *it + '/' + tryexec;
        if (::access(QFile::encodeName(fName), R_OK | X_OK) == 0)
        {
          match = true;
          m_exec = fName;
          break;
        }
      }
      // didn't match at all
      if (!match)
        return false;
    }
    return true;
  }
  return false;
}

bool KateExternalTool::valid( const QString &mt ) const
{
  return mimetypes.isEmpty() || mimetypes.contains( mt );
}
//END KateExternalTool

//BEGIN KateExternalToolsCommand
KateExternalToolsCommand::KateExternalToolsCommand() : KTextEditor::Command()
{<commit_msg>fix crash on win32 with msvc<commit_after>/*
   This file is part of the Kate text editor of the KDE project.

   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.

   ---
   Copyright (C) 2004, <NAME> <<EMAIL>>
*/
// TODO
// Icons
// Direct shortcut setting
//BEGIN Includes
#include "kateexternaltools.h"
#include "kateexternaltools.moc"
#include <KTextEditor/View>
#include <KTextEditor/Document>
#include <KTextEditor/Editor>

#include <KActionCollection>
#include <KListWidget>
#include <KLocale>
#include <KIconLoader>
#include <KMessageBox>
#include <KMimeTypeChooser>
#include <KXmlGuiWindow>
#include <KConfig>
#include <KConfigGroup>
#include <KComboBox>
#include <KLineEdit>
#include <KRun>
#include <KIconDialog>
#include <KMenu>
#include <KSharedConfig>
#include <kdebug.h>
#include <kde_file.h>

#include <QBitmap>
#include <QFile>
#include <QLabel>
#include <QPushButton>
#include <QRegExp>
#include <QToolButton>
#include <QGridLayout>
#include <QObject>
#include <QTextEdit>

#include <unistd.h>
//END Includes

KateExternalToolsCommand *KateExternalToolsCommand::s_self = 0;

//BEGIN KateExternalTool
KateExternalTool::KateExternalTool( const QString &name,
                                    const QString &command,
                                    const QString &icon,
                                    const QString &tryexec,
                                    const QStringList &mimetypes,
                                    const QString &acname,
                                    const QString &cmdname,
                                    int save )
    : name ( name ),
    command ( command ),
    icon ( icon ),
    tryexec ( tryexec ),
    mimetypes ( mimetypes ),
    acname ( acname ),
    cmdname ( cmdname ),
    save ( save )
{
  //if ( ! tryexec.isEmpty() )
  hasexec = checkExec();
}

bool KateExternalTool::checkExec()
{
  // if tryexec is empty, it is the first word of command
  if ( tryexec.isEmpty() )
    tryexec = command.section( ' ', 0, 0, QString::SectionSkipEmpty );

  // NOTE this code is modified taken from kdesktopfile.cpp, from KDesktopFile::tryExec()
  if (!tryexec.isEmpty())
  {
    if (tryexec[0] == '/')
    {
      if (KDE::access(tryexec, R_OK | X_OK))
        return false;

      m_exec = tryexec;
    }
    else
    {
      // !!! <NAME> <<EMAIL>> !!!
      // Environment PATH may contain filenames in 8bit locale cpecified
      // encoding (Like a filenames).
      const QString path = QFile::decodeName(qgetenv("PATH"));
      const QStringList dirs = path.split(KPATH_SEPARATOR, QString::SkipEmptyParts);
      QStringList::ConstIterator it(dirs.begin());
      bool match = false;
      for (; it != dirs.end(); ++it)
      {
        QString fName = *it + '/' + tryexec;
        if (KDE::access(fName, R_OK | X_OK) == 0)
        {
          match = true;
          m_exec = fName;
          break;
        }
      }
      // didn't match at all
      if (!match)
        return false;
    }
    return true;
  }
  return false;
}

bool KateExternalTool::valid( const QString &mt ) const
{
  return mimetypes.isEmpty() || mimetypes.contains( mt );
}
//END KateExternalTool

//BEGIN KateExternalToolsCommand
KateExternalToolsCommand::KateExternalToolsCommand() : KTextEditor::Command()
{
{'fraction_non_alphanumeric': 0.10600126342387871, 'fraction_numerical': 0.005811749842072015, 'mean_word_length': 2.6682113067655235, 'pattern_counts': {'":': 0, '<': 74, '<?xml version=': 0, '>': 74, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6533543', 'n_tokens_mistral': 2398, 'n_tokens_neox': 2308, 'n_words': 748}