<commit_before>/*******************************************************************************

Licensed to the OpenCOR team under one or more contributor license agreements.
See the NOTICE.txt file distributed with this work for additional information
regarding copyright ownership. The OpenCOR team licenses this file to you under
the Apache License, Version 2.0 (the "License"); you may not use this file
except in compliance with the License. You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.

*******************************************************************************/

//==============================================================================
// Core plugin
//==============================================================================

#include "cliutils.h"
#include "centralwidget.h"
#include "coreplugin.h"
#include "filetypeinterface.h"
#include "filemanager.h"
#include "guiutils.h"
#include "organisationwidget.h"
#include "plugin.h"

//==============================================================================

#include <QAction>
#include <QApplication>
#include <QEvent>
#include <QFile>
#include <QMainWindow>
#include <QMenu>
#include <QMessageBox>
#include <QPalette>
#include <QSettings>

//==============================================================================

namespace OpenCOR {
namespace Core {

//==============================================================================

PLUGININFO_FUNC CorePluginInfo()
{
    Descriptions descriptions;

    descriptions.insert("en", QString::fromUtf8("the core plugin."));
    descriptions.insert("fr", QString::fromUtf8("l'extension de base."));

    return new PluginInfo(PluginInfo::Miscellaneous, false, false,
                          QStringList(),
                          descriptions);
}

//==============================================================================

CorePlugin::CorePlugin() :
    mRecentFileNamesOrUrls(QStringList())
{
}

//==============================================================================
// Core interface
//==============================================================================

QWidget * CorePlugin::centralWidget()
{
    // Return our central widget

    return mCentralWidget;
}

//==============================================================================

void CorePlugin::settingsLoaded(const Plugins &pLoadedPlugins)
{
    // Let our central widget know that all the other plugins have now loaded
    // their settings

    mCentralWidget->settingsLoaded(pLoadedPlugins);
}

//==============================================================================

void CorePlugin::handleArguments(const QStringList &pArguments)
{
    // All the arguments are currently assumed to be files to open, so...

    foreach (const QString &argument, pArguments)
        if (!argument.isEmpty()) {
            bool isLocalFile;
            QString fileNameOrUrl;

            checkFileNameOrUrl(argument, isLocalFile, fileNameOrUrl);

            if (isLocalFile)
                mCentralWidget->openFile(fileNameOrUrl);
            else
                mCentralWidget->openRemoteFile(fileNameOrUrl);
        }
}

//==============================================================================

bool CorePlugin::canClose()
{
    // To determine whether we can close, we must ask our central widget

    return mCentralWidget->canClose();
}

//==============================================================================
// File handling interface
//==============================================================================

bool CorePlugin::saveFile(const QString &pOldFileName,
                          const QString &pNewFileName)
{
    Q_UNUSED(pOldFileName);
    Q_UNUSED(pNewFileName);

    // We don't handle this interface...

    return false;
}

//==============================================================================

void CorePlugin::fileOpened(const QString &pFileName)
{
    // Remove the file from our list of recent files and update our Reopen
    // sub-menu

    FileManager *fileManagerInstance = FileManager::instance();

    if (fileManagerInstance->isRemote(pFileName))
        mRecentFileNamesOrUrls.removeOne(fileManagerInstance->url(pFileName));
    else
        mRecentFileNamesOrUrls.removeOne(pFileName);

    updateFileReopenMenu();
}

//==============================================================================

void CorePlugin::filePermissionsChanged(const QString &pFileName)
{
    // Update the checked state of our Locked menu, if needed

    if (!pFileName.compare(mCentralWidget->currentFileName()))
        mFileLockedAction->setChecked(!FileManager::instance()->isReadableAndWritable(pFileName));
        // Note: we really want to call isReadableAndWritable() rather than
        //       isLocked() since from the GUI perspective a file should only be
        //       considered unlocked if it can be both readable and writable
        //       (see CentralWidget::updateFileTab())...
}

//==============================================================================

void CorePlugin::fileModified(const QString &pFileName)
{
    // Update our new/modified sensitive actions, if needed

    if (!pFileName.compare(mCentralWidget->currentFileName()))
        updateNewModifiedSensitiveActions();
}

//==============================================================================

void CorePlugin::fileReloaded(const QString &pFileName)
{
    Q_UNUSED(pFileName);

    // We don't handle this interface...
}

//==============================================================================

void CorePlugin::fileRenamed(const QString &pOldFileName,
                             const QString &pNewFileName)
{
    // Remove the new file from our list of recent files, should it be there
    // Note: it's fine if the new file isn't in our list since nothing will be
    //       done in that case (thus avoiding us having to test for its
    //       presence)...

    mRecentFileNamesOrUrls.removeOne(pNewFileName);

    // A file has been created or saved under a new name, so we want the old
    // file name to be added to our list of recent files, i.e. as if it had been
    // closed

    fileClosed(pOldFileName);
}

//==============================================================================

void CorePlugin::fileClosed(const QString &pFileName)
{
    // Add, if isn't new and it still exists (i.e. we are not here because the
    // file has been deleted), the file to our list of recent files (making sure
    // that we don't end up with more than 10 recent file names) and update our
    // Reopen sub-menu
    // Note: the most recent file is to be shown first...

    FileManager *fileManagerInstance = FileManager::instance();

    if (   !fileManagerInstance->isNew(pFileName)
        &&  QFileInfo(pFileName).exists()) {
        if (fileManagerInstance->isRemote(pFileName))
            mRecentFileNamesOrUrls.prepend(fileManagerInstance->url(pFileName));
        else
            mRecentFileNamesOrUrls.prepend(pFileName);

        while (mRecentFileNamesOrUrls.count() > 10)
            mRecentFileNamesOrUrls.removeLast();

        updateFileReopenMenu();
    }
}

//==============================================================================
// GUI interface
//==============================================================================

void CorePlugin::updateGui(Plugin *pViewPlugin, const QString &pFileName)
{
    Q_UNUSED(pViewPlugin);
    Q_UNUSED(pFileName);

    // Update our new/modified sensitive actions

    updateNewModifiedSensitiveActions();
}<commit_msg>Some minor cleaning up [ci skip].<commit_after>/*******************************************************************************

Licensed to the OpenCOR team under one or more contributor license agreements.
See the NOTICE.txt file distributed with this work for additional information
regarding copyright ownership. The OpenCOR team licenses this file to you under
the Apache License, Version 2.0 (the "License"); you may not use this file
except in compliance with the License. You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.

*******************************************************************************/

//==============================================================================
// Core plugin
//==============================================================================

#include "cliutils.h"
#include "centralwidget.h"
#include "coreplugin.h"
#include "filetypeinterface.h"
#include "filemanager.h"
#include "guiutils.h"
#include "organisationwidget.h"
#include "plugin.h"

//==============================================================================

#include <QAction>
#include <QApplication>
#include <QEvent>
#include <QFile>
#include <QMainWindow>
#include <QMenu>
#include <QMessageBox>
#include <QPalette>
#include <QSettings>

//==============================================================================

namespace OpenCOR {
namespace Core {

//==============================================================================

PLUGININFO_FUNC CorePluginInfo()
{
    Descriptions descriptions;

    descriptions.insert("en", QString::fromUtf8("the core plugin."));
    descriptions.insert("fr", QString::fromUtf8("l'extension de base."));

    return new PluginInfo(PluginInfo::Miscellaneous, false, false,
                          QStringList(),
                          descriptions);
}

//==============================================================================

CorePlugin::CorePlugin() :
    mRecentFileNamesOrUrls(QStringList())
{
}

//==============================================================================
// Core interface
//==============================================================================

QWidget * CorePlugin::centralWidget()
{
    // Return our central widget

    return mCentralWidget;
}

//==============================================================================

void CorePlugin::settingsLoaded(const Plugins &pLoadedPlugins)
{
    // Let our central widget know that all the other plugins have now loaded
    // their settings

    mCentralWidget->settingsLoaded(pLoadedPlugins);
}

//==============================================================================

void CorePlugin::handleArguments(const QStringList &pArguments)
{
    // All the arguments are currently assumed to be files to open, so...

    foreach (const QString &argument, pArguments)
        if (!argument.isEmpty()) {
            bool isLocalFile;
            QString fileNameOrUrl;

            checkFileNameOrUrl(argument, isLocalFile, fileNameOrUrl);

            if (isLocalFile)
                mCentralWidget->openFile(fileNameOrUrl);
            else
                mCentralWidget->openRemoteFile(fileNameOrUrl);
        }
}

//==============================================================================

bool CorePlugin::canClose()
{
    // To determine whether we can close, we must ask our central widget

    return mCentralWidget->canClose();
}

//==============================================================================
// File handling interface
//==============================================================================

bool CorePlugin::saveFile(const QString &pOldFileName,
                          const QString &pNewFileName)
{
    Q_UNUSED(pOldFileName);
    Q_UNUSED(pNewFileName);

    // We don't handle this interface...

    return false;
}

//==============================================================================

void CorePlugin::fileOpened(const QString &pFileName)
{
    // Remove the file from our list of recent files and update our Reopen
    // sub-menu

    FileManager *fileManagerInstance = FileManager::instance();

    if (fileManagerInstance->isRemote(pFileName))
        mRecentFileNamesOrUrls.removeOne(fileManagerInstance->url(pFileName));
    else
        mRecentFileNamesOrUrls.removeOne(pFileName);

    updateFileReopenMenu();
}

//==============================================================================

void CorePlugin::filePermissionsChanged(const QString &pFileName)
{
    // Update the checked state of our Locked menu, if needed

    if (!pFileName.compare(mCentralWidget->currentFileName()))
        mFileLockedAction->setChecked(!FileManager::instance()->isReadableAndWritable(pFileName));
        // Note: we really want to call isReadableAndWritable() rather than
        //       isLocked() since from the GUI perspective a file should only be
        //       considered unlocked if it can be both readable and writable
        //       (see CentralWidget::updateFileTab())...
}

//==============================================================================

void CorePlugin::fileModified(const QString &pFileName)
{
    // Update our new/modified sensitive actions, if needed

    if (!pFileName.compare(mCentralWidget->currentFileName()))
        updateNewModifiedSensitiveActions();
}

//==============================================================================

void CorePlugin::fileReloaded(const QString &pFileName)
{
    Q_UNUSED(pFileName);

    // We don't handle this interface...
}

//==============================================================================

void CorePlugin::fileRenamed(const QString &pOldFileName,
                             const QString &pNewFileName)
{
    // Remove the new file from our list of recent files, should it be there
    // Note: it's fine if the new file isn't in our list since nothing will be
    //       done in that case (thus avoiding us having to test for its
    //       presence)...

    mRecentFileNamesOrUrls.removeOne(pNewFileName);

    // A file has been created or saved under a new name, so we want the old
    // file name to be added to our list of recent files, i.e. as if it had been
    // closed

    fileClosed(pOldFileName);
}

//==============================================================================

void CorePlugin::fileClosed(const QString &pFileName)
{
    // Add, if isn't new and it still exists (i.e. we are not here because the
    // file has been deleted), the file to our list of recent files (making sure
    // that we don't end up with more than 10 recent file names) and update our
    // Reopen sub-menu
    // Note: the most recent file is to be shown first...

    FileManager *fileManagerInstance = FileManager::instance();

    if (   !fileManagerInstance->isNew(pFileName)
        &&  QFile::exists(pFileName)) {
        if (fileManagerInstance->isRemote(pFileName))
            mRecentFileNamesOrUrls.prepend(fileManagerInstance->url(pFileName));
        else
            mRecentFileNamesOrUrls.prepend(pFileName);

        while (mRecentFileNamesOrUrls.count() > 10)
            mRecentFileNamesOrUrls.removeLast();

        updateFileReopenMenu();
    }
}

//==============================================================================
// GUI interface
//==============================================================================

void CorePlugin::updateGui(Plugin *pViewPlugin, const QString &pFileName)
{
    Q_UNUSED(pViewPlugin);
    Q_UNUSED(pFileName);

    // Update our new/modified sensitive actions

    updateNewModifiedSensitiveActions();
}
{'fraction_non_alphanumeric': 0.29971760276121745, 'fraction_numerical': 0.0012550988390335738, 'mean_word_length': 4.344064386317908, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3394882', 'n_tokens_mistral': 3771, 'n_tokens_neox': 3479, 'n_words': 1105}