Salome HOME
Redesign SALOME documentation: update link to youtube channel
[modules/gui.git] / src / HelpBrowser / HelpBrowser_Application.cxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D, 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 // File   : HelpBrowser_Application.cpp
23 // Author : Maxim GLIBIN, OpenCASCADE S.A.S. (maxim.glibin@opencascade.com)
24
25 #include "HelpBrowser_Application.h"
26
27 #include <QFile>
28 #include <QFileSystemWatcher>
29 #include <QTextStream>
30 #include <QWidget>
31
32 /*!
33   \brief Constructor
34  */
35 HelpBrowser_Application::HelpBrowser_Application(const QString& appId, int& argc, char** argv)
36     : QtSingleApplication(appId, argc, argv)
37 {
38   watcher = new QFileSystemWatcher();
39 }
40
41 /*!
42   \brief Returns the file system watcher for monitoring files for modifications.
43 */
44 QFileSystemWatcher* HelpBrowser_Application::fileSysWatcher() const
45 {
46   return watcher;
47 }
48
49 /*!
50   \brief Removes the specified paths from the file system watcher and corresponding
51   files if \a isDeleteFile flag is set. By default, this flag is \c true.
52 */
53 void HelpBrowser_Application::clearWatcher(bool isDeleteFile)
54 {
55   QStringList aFiles = watcher->files();
56   foreach ( QString aFilePath, aFiles ) 
57   {
58     removeWatchPath( aFilePath );
59     if ( isDeleteFile )
60       QFile::remove( aFilePath );
61   }
62 }
63
64 /*!
65   Adds the path to the file system watcher if the file exists.
66 */
67 void HelpBrowser_Application::addWatchPath(const QString& thePath)
68 {
69   if ( thePath.isEmpty() )
70   {
71     qWarning("HelpBrowser_Application::addWatchPath: path is empty.");
72     return;
73   }
74   watcher->addPath( thePath );
75 }
76
77 /*!
78   Removes the path to the file system watcher.
79 */
80 void HelpBrowser_Application::removeWatchPath(const QString& thePath)
81 {
82   if (thePath.isEmpty())
83   {
84     qWarning("HelpBrowser_Application::removeWatchPath: path is empty.");
85     return;
86   }
87   watcher->removePath( thePath );
88 }
89
90 /*!
91   Updates the statement of watcher. If file becomes empty it is removed and
92   calls quit() function for application.
93 */
94 void HelpBrowser_Application::updateWatchStatement(const QString& thePath)
95 {
96   if ( thePath.isEmpty() )
97   {
98     qWarning("HelpBrowser_Application::updateWatchStatement: path is empty.");
99     return;
100   }
101
102   if ( !QFile::exists( thePath ) )
103     removeWatchPath( thePath );
104
105   // Get list of paths to files that are being watched
106   bool isClose = false;
107   if ( watcher->files().contains( thePath ) )
108   {
109     QFile aFile( thePath );
110     if ( aFile.open( QIODevice::ReadOnly | QIODevice::Text ) )
111     {
112       QTextStream anInSteam( &aFile );
113       if ( anInSteam.readAll().isEmpty() )
114         isClose = true;
115       aFile.close();
116     }
117   }
118
119   if ( isClose )
120   {
121     QFile::remove( thePath );
122     this->quit();
123   }
124 }