Salome HOME
a20e93f4d4ffde448acc62114a5e825a2235ec77
[modules/gui.git] / src / HelpBrowser / HelpBrowser_Application.cxx
1 // Copyright (C) 2007-2023  CEA, EDF, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // File   : HelpBrowser_Application.cpp
24 // Author : Maxim GLIBIN, OpenCASCADE S.A.S. (maxim.glibin@opencascade.com)
25
26 #include "HelpBrowser_Application.h"
27
28 #include <QFile>
29 #include <QFileSystemWatcher>
30 #include <QTextStream>
31 #include <QWidget>
32
33 /*!
34   \brief Constructor
35  */
36 HelpBrowser_Application::HelpBrowser_Application(const QString& appId, int& argc, char** argv)
37     : QtSingleApplication(appId, argc, argv)
38 {
39   watcher = new QFileSystemWatcher();
40 }
41
42 /*!
43   \brief Returns the file system watcher for monitoring files for modifications.
44 */
45 QFileSystemWatcher* HelpBrowser_Application::fileSysWatcher() const
46 {
47   return watcher;
48 }
49
50 /*!
51   \brief Removes the specified paths from the file system watcher and corresponding
52   files if \a isDeleteFile flag is set. By default, this flag is \c true.
53 */
54 void HelpBrowser_Application::clearWatcher(bool isDeleteFile)
55 {
56   QStringList aFiles = watcher->files();
57   foreach ( QString aFilePath, aFiles ) 
58   {
59     removeWatchPath( aFilePath );
60     if ( isDeleteFile )
61       QFile::remove( aFilePath );
62   }
63 }
64
65 /*!
66   Adds the path to the file system watcher if the file exists.
67 */
68 void HelpBrowser_Application::addWatchPath(const QString& thePath)
69 {
70   if ( thePath.isEmpty() )
71   {
72     qWarning("HelpBrowser_Application::addWatchPath: path is empty.");
73     return;
74   }
75   watcher->addPath( thePath );
76 }
77
78 /*!
79   Removes the path to the file system watcher.
80 */
81 void HelpBrowser_Application::removeWatchPath(const QString& thePath)
82 {
83   if (thePath.isEmpty())
84   {
85     qWarning("HelpBrowser_Application::removeWatchPath: path is empty.");
86     return;
87   }
88   watcher->removePath( thePath );
89 }
90
91 /*!
92   Updates the statement of watcher. If file becomes empty it is removed and
93   calls quit() function for application.
94 */
95 void HelpBrowser_Application::updateWatchStatement(const QString& thePath)
96 {
97   if ( thePath.isEmpty() )
98   {
99     qWarning("HelpBrowser_Application::updateWatchStatement: path is empty.");
100     return;
101   }
102
103   if ( !QFile::exists( thePath ) )
104     removeWatchPath( thePath );
105
106   // Get list of paths to files that are being watched
107   bool isClose = false;
108   if ( watcher->files().contains( thePath ) )
109   {
110     QFile aFile( thePath );
111     if ( aFile.open( QIODevice::ReadOnly | QIODevice::Text ) )
112     {
113       QTextStream anInSteam( &aFile );
114       if ( anInSteam.readAll().isEmpty() )
115         isClose = true;
116       aFile.close();
117     }
118   }
119
120   if ( isClose )
121   {
122     QFile::remove( thePath );
123     this->quit();
124   }
125 }