File indexing completed on 2024-04-28 17:06:14

0001 /*
0002     SPDX-FileCopyrightText: 2000 Shie Erlich <krusader@users.sourceforge.net>
0003     SPDX-FileCopyrightText: 2000 Rafi Yanai <krusader@users.sourceforge.net>
0004     SPDX-FileCopyrightText: 2004-2022 Krusader Krew <https://krusader.org>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #ifndef KMOUNTMANGUI_H
0010 #define KMOUNTMANGUI_H
0011 
0012 // QtCore
0013 #include <QDateTime>
0014 #include <QList>
0015 #include <QTimer>
0016 // QtWidgets
0017 #include <QDialog>
0018 #include <QFrame>
0019 
0020 #include <KIOCore/KMountPoint>
0021 #include <utility>
0022 
0023 #include "../GUI/krtreewidget.h"
0024 #include "kmountman.h"
0025 
0026 #include <math.h>
0027 
0028 #define WATCHER_DELAY 500
0029 
0030 class QCheckBox;
0031 class KrFSDisplay;
0032 
0033 // forward definitions
0034 class fsData;
0035 
0036 class KMountManGUI : public QDialog
0037 {
0038     Q_OBJECT
0039 
0040     enum Pages { Filesystems = 0 };
0041 
0042 public:
0043     explicit KMountManGUI(KMountMan *mntMan);
0044     ~KMountManGUI() override;
0045 
0046 protected:
0047     void resizeEvent(QResizeEvent *e) override;
0048 
0049 protected slots:
0050     void doubleClicked(QTreeWidgetItem *);
0051     void clicked(QTreeWidgetItem *, const QPoint &);
0052     void slotToggleMount();
0053     void slotEject();
0054     void changeActive();
0055     void changeActive(QTreeWidgetItem *);
0056     void checkMountChange(); // check whether the mount table was changed
0057 
0058     void updateList(); // fill-up the filesystems list
0059     void getSpaceData();
0060 
0061 protected:
0062     QLayout *createMainPage(); // creator of the main page - filesystems
0063     void addItemToMountList(KrTreeWidget *lst, fsData &fs);
0064     fsData *getFsData(QTreeWidgetItem *item);
0065     QString getMntPoint(QTreeWidgetItem *item);
0066     void addNonMounted();
0067 
0068 private:
0069     KMountMan *mountMan;
0070     KrFSDisplay *info;
0071     KrTreeWidget *mountList;
0072     QCheckBox *cbShowOnlyRemovable;
0073     QPushButton *mountButton;
0074     QPushButton *ejectButton;
0075     QTimer *watcher;
0076     QDateTime lastMtab;
0077     // used for the getSpace - gotSpace functions
0078     KMountPoint::List possible, mounted;
0079     QList<fsData> fileSystems;
0080 
0081     int sizeX;
0082     int sizeY;
0083 };
0084 
0085 // Data container for a single-filesystem data
0086 // maximum size supported is 2GB of 1kb blocks == 2048GB, enough.
0087 // not really needed, but kept for backward compatibility
0088 class fsData
0089 {
0090 public:
0091     fsData()
0092         : Name()
0093         , Type()
0094         , MntPoint()
0095         , TotalBlks(0)
0096         , FreeBlks(0)
0097         , Mounted(false)
0098     {
0099     }
0100 
0101     // get information
0102     inline QString name()
0103     {
0104         return Name;
0105     }
0106     inline QString shortName()
0107     {
0108         return Name.right(Name.length() - Name.indexOf("/", 1) - 1);
0109     }
0110     inline QString type()
0111     {
0112         return Type;
0113     }
0114     inline QString mntPoint()
0115     {
0116         return MntPoint;
0117     }
0118     inline unsigned long totalBlks()
0119     {
0120         return TotalBlks;
0121     }
0122     inline unsigned long freeBlks()
0123     {
0124         return FreeBlks;
0125     }
0126     inline KIO::filesize_t totalBytes()
0127     {
0128         return TotalBlks * 1024;
0129     }
0130     inline KIO::filesize_t freeBytes()
0131     {
0132         return FreeBlks * 1024;
0133     }
0134     int usedPerct()
0135     {
0136         if (TotalBlks == 0)
0137             return 0;
0138 
0139         return static_cast<int>(roundl((static_cast<long double>(TotalBlks - FreeBlks) * 100) / TotalBlks));
0140     }
0141     inline bool mounted()
0142     {
0143         return Mounted;
0144     }
0145 
0146     // set information
0147     inline void setName(QString n_)
0148     {
0149         Name = std::move(n_);
0150     }
0151     inline void setType(QString t_)
0152     {
0153         Type = std::move(t_);
0154     }
0155     inline void setMntPoint(QString m_)
0156     {
0157         MntPoint = std::move(m_);
0158     }
0159     inline void setTotalBlks(unsigned long t_)
0160     {
0161         TotalBlks = t_;
0162     }
0163     inline void setFreeBlks(unsigned long f_)
0164     {
0165         FreeBlks = f_;
0166     }
0167     inline void setMounted(bool m_)
0168     {
0169         Mounted = m_;
0170     }
0171 
0172 private:
0173     QString Name; // i.e: /dev/cdrom
0174     QString Type; // i.e: iso9600
0175     QString MntPoint; // i.e: /mnt/cdrom
0176     unsigned long TotalBlks; // measured in 1024 bytes per block
0177     unsigned long FreeBlks;
0178     bool Mounted; // true if filesystem is mounted
0179 
0180     // additional attributes of a filesystem, parsed from fstab
0181 public:
0182     QString options; // additional fstab options
0183 };
0184 
0185 class KrMountDetector
0186 {
0187     QString checksum;
0188 #ifndef BSD
0189     QDateTime lastMtab;
0190 #endif
0191 public:
0192     KrMountDetector();
0193     static KrMountDetector *getInstance();
0194     bool hasMountsChanged();
0195 };
0196 
0197 #endif