<commit_before>#include <QDebug>
#include <QFileSystemWatcher>

#include "previewwindowcontroller.h"
#include "previewwindow.h"
#include "markdowndoc.h"

////////////////////////////////////////////////////////////////////////////////////////////////////

PreviewWindowController::PreviewWindowController(QObject *parent)
    : QObject(parent)
    , _window(new PreviewWindow())
    , _doc(0)
    , _watcher(new QFileSystemWatcher(this))
{
    connect(_window, SIGNAL(urlsAccepted(QList<QUrl>)),
            this, SLOT(onUrlsDropped(QList<QUrl>)));

    connect(this, SIGNAL(documentChanged(MarkdownDoc*)),
            _window, SLOT(setDocument(MarkdownDoc*)));

    // Wire up the watcher

    connect(_watcher, SIGNAL(fileChanged(QString)), this, SLOT(onDocumentPathChanged(QString)));

}

/**
    Start the application and show the window.
*/
void PreviewWindowController::start()
{
    _doc = 0;
    _window->show();
}

/**
    Handle and URL being dropped inside the application window.

    @param urls
        The list of dropped URLs. The current version ignores all URLs
        after the first.
*/
void PreviewWindowController::onUrlsDropped(QList<QUrl> urls)
{
    QUrl url = urls.takeFirst();
    if (url.scheme() != "file") {
        return;
    }

    if (_doc) {
        // Avoid unnecessary updates and notifications.
        _watcher->removePath(_doc->inputUrl().toLocalFile());

        // Get rid of the current document.
        _doc->deleteLater();
        _doc = 0; // neat freak.
    }

    _doc = new MarkdownDoc(url, this);

    // Make the document update itself when the file changes.

    _watcher->addPath(_doc->inputUrl().toLocalFile());
    emit documentChanged(_doc);
}

/**
    Update the document when the original file is changed.

    @param path
        The path to the changed file. Used to test if the filesystem watcher still
        contains the file.
*/
void PreviewWindowController::onDocumentPathChanged(const QString& path)
{
    _doc->update();

    // Lots of applications perform a save by first writing to a temp file
    // and then moving the temp file over the original file. It seems that
    // the filesystem watcher stops watching the original file in this
    // case. For this reason, we'll add it again. According to the Qt docs
    // there's no problem if we add the same path more than once, but we
    // like to be extra-neat.

    QStringList paths = _watcher->files();
    if (!paths.contains(path)) {
        _watcher->addPath(_doc->inputUrl().toLocalFile());
    }
}
<commit_msg>Read an URL from the command line, too.<commit_after>#include <QApplication>
#include <QDebug>
#include <QFileSystemWatcher>

#include "previewwindowcontroller.h"
#include "previewwindow.h"
#include "markdowndoc.h"

////////////////////////////////////////////////////////////////////////////////////////////////////

PreviewWindowController::PreviewWindowController(QObject *parent)
    : QObject(parent)
    , _window(new PreviewWindow())
    , _doc(0)
    , _watcher(new QFileSystemWatcher(this))
{
    connect(_window, SIGNAL(urlsAccepted(QList<QUrl>)),
            this, SLOT(onUrlsDropped(QList<QUrl>)));

    connect(this, SIGNAL(documentChanged(MarkdownDoc*)),
            _window, SLOT(setDocument(MarkdownDoc*)));

    // Wire up the watcher

    connect(_watcher, SIGNAL(fileChanged(QString)), this, SLOT(onDocumentPathChanged(QString)));

}

/**
    Start the application and show the window.
*/
void PreviewWindowController::start()
{
    _doc = 0;
    _window->show();

    // Check if an URL was passed via arguments.

    QStringList args = QApplication::instance()->arguments();
    if (args.size() > 1) {
        QList<QUrl> urls;
        urls << QUrl::fromLocalFile(args.at(1));
        onUrlsDropped(urls);
    }
}

/**
    Handle and URL being dropped inside the application window.

    @param urls
        The list of dropped URLs. The current version ignores all URLs
        after the first.
*/
void PreviewWindowController::onUrlsDropped(QList<QUrl> urls)
{
    QUrl url = urls.takeFirst();
    if (url.scheme() != "file") {
        return;
    }

    if (_doc) {
        // Avoid unnecessary updates and notifications.
        _watcher->removePath(_doc->inputUrl().toLocalFile());

        // Get rid of the current document.
        _doc->deleteLater();
        _doc = 0; // neat freak.
    }

    _doc = new MarkdownDoc(url, this);

    // Make the document update itself when the file changes.

    _watcher->addPath(_doc->inputUrl().toLocalFile());
    emit documentChanged(_doc);
}

/**
    Update the document when the original file is changed.

    @param path
        The path to the changed file. Used to test if the filesystem watcher still
        contains the file.
*/
void PreviewWindowController::onDocumentPathChanged(const QString& path)
{
    _doc->update();

    // Lots of applications perform a save by first writing to a temp file
    // and then moving the temp file over the original file. It seems that
    // the filesystem watcher stops watching the original file in this
    // case. For this reason, we'll add it again. According to the Qt docs
    // there's no problem if we add the same path more than once, but we
    // like to be extra-neat.

    QStringList paths = _watcher->files();
    if (!paths.contains(path)) {
        _watcher->addPath(_doc->inputUrl().toLocalFile());
    }
}

{'fraction_non_alphanumeric': 0.1449758991472006, 'fraction_numerical': 0.0014831294030404152, 'mean_word_length': 2.6650815217391304, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 37, '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': '7636609', 'n_tokens_mistral': 1579, 'n_tokens_neox': 1519, 'n_words': 472}