File indexing completed on 2024-05-12 05:53:55

0001 /*
0002  * Copyright (c) 2018 Sune Vuorela <sune@vuorela.dk>
0003  *
0004  * Permission is hereby granted, free of charge, to any person
0005  * obtaining a copy of this software and associated documentation
0006  * files (the "Software"), to deal in the Software without
0007  * restriction, including without limitation the rights to use,
0008  * copy, modify, merge, publish, distribute, sublicense, and/or sell
0009  * copies of the Software, and to permit persons to whom the
0010  * Software is furnished to do so, subject to the following
0011  * conditions:
0012  *
0013  * The above copyright notice and this permission notice shall be
0014  * included in all copies or substantial portions of the Software.
0015  *
0016  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0017  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
0018  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0019  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
0020  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
0021  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
0022  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0023  * OTHER DEALINGS IN THE SOFTWARE.
0024  */
0025 #include "activedocument.h"
0026 #include "activedocumentlistener.h"
0027 #include <QFileSystemWatcher>
0028 #include <QDebug>
0029 
0030 ActiveDocument::ActiveDocument(QObject* parent) : QObject(parent)
0031 {
0032     m_watcher = std::make_unique<QFileSystemWatcher>();
0033     connect(m_watcher.get(), &QFileSystemWatcher::fileChanged, this, &ActiveDocument::checkedReload);
0034 }
0035 
0036 ActiveDocument::~ActiveDocument()
0037 {
0038     // for smart pointers
0039 }
0040 
0041 void ActiveDocument::registerListener(ActiveDocumentListener* pane)
0042 {
0043     m_panes.append(pane);
0044 }
0045 
0046 void ActiveDocument::openPath(const QString& file)
0047 {
0048     if (!m_currentPath.isEmpty()) {
0049         m_watcher->removePath(m_currentPath);
0050     }
0051 
0052     m_currentPath = file;
0053 
0054     if (!m_currentPath.isEmpty()) {
0055         m_watcher->addPath(m_currentPath);
0056     }
0057     reload();
0058 }
0059 
0060 void ActiveDocument::reload()
0061 {
0062     for(auto listener : qAsConst(m_panes)) {
0063         listener->openPath(m_currentPath);
0064     }
0065 }
0066 
0067 void ActiveDocument::checkedReload(const QString& path)
0068 {
0069     if (path == m_currentPath) {
0070         reload();
0071     }
0072 }
0073 
0074 QString ActiveDocument::currentPath() const
0075 {
0076     return m_currentPath;
0077 }
0078 
0079 
0080 
0081