1 /*****************************************************************************
2  * Copyright (C) 2000 Shie Erlich <krusader@users.sourceforge.net>           *
3  * Copyright (C) 2000 Rafi Yanai <krusader@users.sourceforge.net>            *
4  * Copyright (C) 2004-2019 Krusader Krew [https://krusader.org]              *
5  *                                                                           *
6  * This file is part of Krusader [https://krusader.org].                     *
7  *                                                                           *
8  * Krusader is free software: you can redistribute it and/or modify          *
9  * it under the terms of the GNU General Public License as published by      *
10  * the Free Software Foundation, either version 2 of the License, or         *
11  * (at your option) any later version.                                       *
12  *                                                                           *
13  * Krusader is distributed in the hope that it will be useful,               *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
16  * GNU General Public License for more details.                              *
17  *                                                                           *
18  * You should have received a copy of the GNU General Public License         *
19  * along with Krusader.  If not, see [http://www.gnu.org/licenses/].         *
20  *****************************************************************************/
21 
22 
23 #ifndef KMOUNTMANGUI_H
24 #define KMOUNTMANGUI_H
25 
26 // QtCore
27 #include <QTimer>
28 #include <QList>
29 #include <QDateTime>
30 // QtWidgets
31 #include <QDialog>
32 #include <QFrame>
33 
34 #include <KIOCore/KMountPoint>
35 
36 #include "../GUI/krtreewidget.h"
37 #include "kmountman.h"
38 
39 #define  WATCHER_DELAY    500
40 
41 class QCheckBox;
42 class KRFSDisplay;
43 
44 // forward definitions
45 class fsData;
46 
47 class KMountManGUI : public QDialog
48 {
49     Q_OBJECT
50 
51     enum Pages {
52         Filesystems = 0
53     };
54 
55 public:
56     explicit KMountManGUI(KMountMan *mntMan);
57     ~KMountManGUI();
58 
59 protected:
60     virtual void resizeEvent(QResizeEvent *e) Q_DECL_OVERRIDE;
61 
62 protected slots:
63     void doubleClicked(QTreeWidgetItem *);
64     void clicked(QTreeWidgetItem *, const QPoint &);
65     void slotToggleMount();
66     void slotEject();
67     void changeActive();
68     void changeActive(QTreeWidgetItem *);
69     void checkMountChange(); // check whether the mount table was changed
70 
71     void updateList();     // fill-up the filesystems list
72     void getSpaceData();
73 
74 protected:
75     QLayout *createMainPage(); // creator of the main page - filesystems
76     void addItemToMountList(KrTreeWidget *lst, fsData &fs);
77     fsData* getFsData(QTreeWidgetItem *item);
78     QString getMntPoint(QTreeWidgetItem *item);
79     void addNonMounted();
80 
81 private:
82     KMountMan *mountMan;
83     KRFSDisplay *info;
84     KrTreeWidget *mountList;
85     QCheckBox *cbShowOnlyRemovable;
86     QPushButton *mountButton;
87     QPushButton *ejectButton;
88     QTimer *watcher;
89     QDateTime lastMtab;
90     // used for the getSpace - gotSpace functions
91     KMountPoint::List possible, mounted;
92     QList<fsData> fileSystems;
93 
94     int sizeX;
95     int sizeY;
96 };
97 
98 // Data container for a single-filesystem data
99 // maximum size supported is 2GB of 1kb blocks == 2048GB, enough.
100 // not really needed, but kept for backward compatibility
101 class fsData
102 {
103 public:
fsData()104     fsData() : Name(), Type(), MntPoint(), TotalBlks(0),
105             FreeBlks(0), Mounted(false) {}
106 
107     // get information
name()108     inline QString name() {
109         return Name;
110     }
shortName()111     inline QString shortName() {
112         return Name.right(Name.length() - Name.indexOf("/", 1) - 1);
113     }
type()114     inline QString type() {
115         return Type;
116     }
mntPoint()117     inline QString mntPoint() {
118         return MntPoint;
119     }
totalBlks()120     inline long totalBlks() {
121         return TotalBlks;
122     }
freeBlks()123     inline long freeBlks() {
124         return FreeBlks;
125     }
totalBytes()126     inline KIO::filesize_t totalBytes() {
127         return TotalBlks * 1024;
128     }
freeBytes()129     inline KIO::filesize_t freeBytes() {
130         return FreeBlks * 1024;
131     }
132     //////////////////// insert a good round function here /////////////////
usedPerct()133     int usedPerct() {
134         if (TotalBlks == 0)
135             return 0;
136         float res = ((float)(TotalBlks - FreeBlks)) / ((float) TotalBlks) * 100;
137         if ((res - (int) res) > 0.5)
138             return (int) res + 1;
139         else
140             return (int) res;
141     }
mounted()142     inline bool mounted() {
143         return Mounted;
144     }
145 
146     // set information
setName(QString n_)147     inline void setName(QString n_) {
148         Name = n_;
149     }
setType(QString t_)150     inline void setType(QString t_) {
151         Type = t_;
152     }
setMntPoint(QString m_)153     inline void setMntPoint(QString m_) {
154         MntPoint = m_;
155     }
setTotalBlks(long t_)156     inline void setTotalBlks(long t_) {
157         TotalBlks = t_;
158     }
setFreeBlks(long f_)159     inline void setFreeBlks(long f_) {
160         FreeBlks = f_;
161     }
setMounted(bool m_)162     inline void setMounted(bool m_) {
163         Mounted = m_;
164     }
165 
166 private:
167     QString Name;       // i.e: /dev/cdrom
168     QString Type;       // i.e: iso9600
169     QString MntPoint;   // i.e: /mnt/cdrom
170     long TotalBlks;  // measured in 1024bytes per block
171     long FreeBlks;
172     bool Mounted;    // true if filesystem is mounted
173 
174     // additional attributes of a filesystem, parsed from fstab
175 public:
176     QString options;    // additional fstab options
177 };
178 
179 class KrMountDetector
180 {
181     QString checksum;
182 #ifndef BSD
183     QDateTime lastMtab;
184 #endif
185 public:
186     KrMountDetector();
187     static KrMountDetector * getInstance();
188     bool hasMountsChanged();
189 };
190 
191 
192 
193 #endif
194