1 /*
2  *   File name: QDirStatApp.h
3  *   Summary:	QDirStat application class for key objects
4  *   License:	GPL V2 - See file LICENSE for details.
5  *
6  *   Author:	Stefan Hundhammer <Stefan.Hundhammer@gmx.de>
7  */
8 
9 #ifndef QDirStatApp_h
10 #define QDirStatApp_h
11 
12 
13 class QWidget;
14 
15 
16 namespace QDirStat
17 {
18     class DirTreeModel;
19     class DirTree;
20     class SelectionModel;
21     class CleanupCollection;
22     class QDirStatApp;
23     class FileInfo;
24 
25 
26     /**
27      * Access the singleton instance of the QDirStatApp class. If no instance
28      * exists yet, this will create it and also the key objects that it manages
29      * (see below).
30      **/
31     QDirStatApp * app();
32 
33 
34     /**
35      * This is the application object for the QDirStat application with a
36      * similar approach to Qt's QCoreApplication. It does not create any window
37      * or widget, so it is meant to be instantiated before any widgets or
38      * anything GUI related.
39      *
40      * This class holds key objects and initializes them in the correct order,
41      * and it allows access to those key objects from other classes without
42      * having to pass every single one of them to each of the other classes
43      * that needs them.
44      *
45      * This is a singleton class.
46      **/
47     class QDirStatApp
48     {
49     public:
50 
51         /**
52          * Explicitly create the singleton instance of this class if it isn't
53          * created yet. Do nothing if it already exists.
54          **/
55         static void createInstance();
56 
57         /**
58          * Delete the singleton instance of this class and all the key objects
59          * that it manages. It is important that the widgets that need any of
60          * them are deleted BEFORE deleting this app instance.
61          **/
62         static void deleteInstance();
63 
64         /**
65          * Access the singleton instance of the QDirStatApp class. If no
66          * instance exists yet, this will create it and also the key objects
67          * that it manages.
68          *
69          * Typically, you will want to use the global app() function instead.
70          **/
71         static QDirStatApp * instance();
72 
73 
74         //
75         // Access to key objects
76         //
77 
78         /**
79          * Return the directory tree model. This is the model part of Qt
80          * model/view widgets such as the DirTreeView (QAbstractItemView) or
81          * the TreemapView.
82          *
83          * It has a DirTree that actually holds the in-memory tree of FileInfo
84          * / DirInfo nodes.
85          **/
dirTreeModel()86         DirTreeModel * dirTreeModel() const { return _dirTreeModel; }
87 
88         /**
89          * Return the DirTree that is owned by the DirTreeModel.
90          *
91          * A DirTree is the in-memory representation of a directory tree
92          * consisting of FileInfo nodes or more specialized classes derived
93          * from FileInfo such as DirInfo, DotEntry, Attic, or even PkgInfo.
94          *
95          * A DirTree may start with PkgInfo nodes that each represent one
96          * installed software package. A PkgInfo node typically has DirInfo /
97          * FileInfo child nodes each representing a directory with files that
98          * belong to that software package.
99          **/
100         DirTree * dirTree() const;
101 
102         /**
103          * Return the SelectionModel that keeps track of what items are marked
104          * as selected across the different connected views, i.e. the DirTreeView
105          * and the TreemapView.
106          **/
selectionModel()107         SelectionModel * selectionModel() const { return _selectionModel; }
108 
109         /**
110          * Return the CleanupCollection, i.e. the collection of actions that
111          * the user can start to clean up after files or directories are found
112          * that should be deleted or compressed or in general cleaned up.
113          *
114          * That includes actions that are purely inspecting files or
115          * directories, such as starting an interactive shell in that directory
116          * or showing the directory in a file manager window. Most cleanup
117          * actions are started as external commands, and they can be configured
118          * to the user's liking with the configuration dialog.
119          **/
cleanupCollection()120         CleanupCollection * cleanupCollection() const { return _cleanupCollection; }
121 
122 
123         //
124         // Convenience methods
125         //
126 
127 
128 
129         /**
130          * Return the (first) MainWindow instance of the running program that
131          * is suitable as a widget parent for subwindows to maintain the
132          * correct window stacking order (and avoid having subwindows disappear
133          * behind the main window). Return 0 if there is no MainWindow (yet).
134          **/
135         QWidget * findMainWindow() const;
136 
137         /**
138          * Return the first selected directory from the SelectionModel or, if
139          * none is selected, the DirTree's root directory.
140          *
141          * Notice that this might still return 0 if the tree is completely
142          * empty.
143          **/
144         FileInfo * selectedDirOrRoot() const;
145 
146 
147     protected:
148 
149         /**
150          * Constructor
151          **/
152         QDirStatApp();
153 
154         /**
155          * Destructor.
156          **/
157         virtual ~QDirStatApp();
158 
159 
160         //
161         // Data members
162         //
163 
164         DirTreeModel            * _dirTreeModel;
165         SelectionModel          * _selectionModel;
166         CleanupCollection       * _cleanupCollection;
167 
168         static QDirStatApp      * _instance;
169 
170     };  // class QDirStatApp
171 
172 }       // namespace QDirStat
173 
174 #endif  // class QDirStatApp_h
175