xref: /glogg/src/platformfilewatcher.cpp (revision 96bde7d5e1c8c71a3ad8ee8e2641c176dc312047)
184b2179eSNicolas Bonnefon /*
284b2179eSNicolas Bonnefon  * Copyright (C) 2010, 2014 Nicolas Bonnefon and other contributors
384b2179eSNicolas Bonnefon  *
484b2179eSNicolas Bonnefon  * This file is part of glogg.
584b2179eSNicolas Bonnefon  *
684b2179eSNicolas Bonnefon  * glogg is free software: you can redistribute it and/or modify
784b2179eSNicolas Bonnefon  * it under the terms of the GNU General Public License as published by
884b2179eSNicolas Bonnefon  * the Free Software Foundation, either version 3 of the License, or
984b2179eSNicolas Bonnefon  * (at your option) any later version.
1084b2179eSNicolas Bonnefon  *
1184b2179eSNicolas Bonnefon  * glogg is distributed in the hope that it will be useful,
1284b2179eSNicolas Bonnefon  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1384b2179eSNicolas Bonnefon  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1484b2179eSNicolas Bonnefon  * GNU General Public License for more details.
1584b2179eSNicolas Bonnefon  *
1684b2179eSNicolas Bonnefon  * You should have received a copy of the GNU General Public License
1784b2179eSNicolas Bonnefon  * along with glogg.  If not, see <http://www.gnu.org/licenses/>.
1884b2179eSNicolas Bonnefon  */
1984b2179eSNicolas Bonnefon 
2084b2179eSNicolas Bonnefon #include "platformfilewatcher.h"
2184b2179eSNicolas Bonnefon 
2284b2179eSNicolas Bonnefon #ifdef _WIN32
2384b2179eSNicolas Bonnefon #include "winwatchtower.h"
2484b2179eSNicolas Bonnefon #else
25b278d183SNicolas Bonnefon #include "inotifywatchtowerdriver.h"
2684b2179eSNicolas Bonnefon #endif
2784b2179eSNicolas Bonnefon 
2884b2179eSNicolas Bonnefon #include "log.h"
2984b2179eSNicolas Bonnefon 
30*96bde7d5SNicolas Bonnefon using PlatformWatchTower = WatchTower<INotifyWatchTowerDriver>;
31*96bde7d5SNicolas Bonnefon 
32*96bde7d5SNicolas Bonnefon std::shared_ptr<PlatformWatchTower> PlatformFileWatcher::watch_tower_;
3384b2179eSNicolas Bonnefon 
3484b2179eSNicolas Bonnefon PlatformFileWatcher::PlatformFileWatcher() : FileWatcher()
3584b2179eSNicolas Bonnefon {
3684b2179eSNicolas Bonnefon     // Caution, this is NOT thread-safe or re-entrant!
3784b2179eSNicolas Bonnefon     if ( !watch_tower_ )
3884b2179eSNicolas Bonnefon     {
3984b2179eSNicolas Bonnefon #ifdef _WIN32
4084b2179eSNicolas Bonnefon         watch_tower_ = std::make_shared<WinWatchTower>();
4184b2179eSNicolas Bonnefon #else
42*96bde7d5SNicolas Bonnefon         watch_tower_ = std::make_shared<PlatformWatchTower>();
4384b2179eSNicolas Bonnefon #endif
4484b2179eSNicolas Bonnefon     }
4584b2179eSNicolas Bonnefon }
4684b2179eSNicolas Bonnefon 
4784b2179eSNicolas Bonnefon PlatformFileWatcher::~PlatformFileWatcher()
4884b2179eSNicolas Bonnefon {
4984b2179eSNicolas Bonnefon }
5084b2179eSNicolas Bonnefon 
5184b2179eSNicolas Bonnefon void PlatformFileWatcher::addFile( const QString& fileName )
5284b2179eSNicolas Bonnefon {
5384b2179eSNicolas Bonnefon     LOG(logDEBUG) << "FileWatcher::addFile " << fileName.toStdString();
5484b2179eSNicolas Bonnefon 
5584b2179eSNicolas Bonnefon     watched_file_name_ = fileName;
5684b2179eSNicolas Bonnefon 
57*96bde7d5SNicolas Bonnefon     notification_ = std::make_shared<Registration>(
5884b2179eSNicolas Bonnefon             watch_tower_->addFile( fileName.toStdString(), [this, fileName] {
5984b2179eSNicolas Bonnefon                 emit fileChanged( fileName ); } ) );
6084b2179eSNicolas Bonnefon }
6184b2179eSNicolas Bonnefon 
6284b2179eSNicolas Bonnefon void PlatformFileWatcher::removeFile( const QString& fileName )
6384b2179eSNicolas Bonnefon {
6484b2179eSNicolas Bonnefon     LOG(logDEBUG) << "FileWatcher::removeFile " << fileName.toStdString();
6584b2179eSNicolas Bonnefon 
6684b2179eSNicolas Bonnefon     notification_ = nullptr;
6784b2179eSNicolas Bonnefon }
68