File: storagemonitor.cpp

package info (click to toggle)
lomiri-camera-app 4.0.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,092 kB
  • sloc: cpp: 1,671; javascript: 27; makefile: 16; sh: 12
file content (119 lines) | stat: -rw-r--r-- 3,248 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * Copyright (C) 2015 Canonical Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "storagemonitor.h"
#include "storagelocations.h"

StorageMonitor::StorageMonitor(QObject *parent) :
    QObject(parent), m_low(false), m_criticallyLow(false), m_writeable(true)
{
    m_timer.setInterval(POLL_INTERVAL);
    m_timer.setSingleShot(false);
    connect(&m_timer, SIGNAL(timeout()), SLOT(refresh()));
}

void StorageMonitor::refresh()
{
    m_storage.refresh();
    checkDiskSpace();
}

void StorageMonitor::checkDiskSpace() {
    bool currentLow;
    bool currentCriticallyLow;

    if (m_storage.isValid() && m_storage.isReady()) {
        qint64 freeSpace = m_storage.bytesAvailable();
        currentLow = (freeSpace <= LOW_SPACE_THRESHOLD);
        currentCriticallyLow = (freeSpace <= CRITICALLY_LOW_SPACE_THRESHOLD);
    } else {
        currentLow = false;
        currentCriticallyLow = false;
    }

    if (currentLow != m_low) {
        m_low = currentLow;
        Q_EMIT diskSpaceLowChanged();
    }

    if (currentCriticallyLow != m_criticallyLow) {
        m_criticallyLow = currentCriticallyLow;
        Q_EMIT diskSpaceCriticallyLowChanged();
    }
}

void StorageMonitor::checkWriteable()
{
    bool writeable = true;

    QString mediaRoot("/media/" + qgetenv("USER"));
    if (m_storage.rootPath().startsWith(mediaRoot)) {
        // check only for external media, assume internal media is always writeable
        if (m_storage.isReadOnly()) {
            writeable = false;
        } else {
            StorageLocations locations;
            QDir storageRoot(locations.removableStoragePicturesLocation());
            QFile testFile(storageRoot.absoluteFilePath(".write_test"));
            bool opened = testFile.open(QIODevice::WriteOnly | QIODevice::Unbuffered);
            if (!opened || testFile.write("x", 1) != 1) writeable = false;
            testFile.close();
            testFile.remove();
        }
    }

    if (m_writeable != writeable) {
        m_writeable = writeable;
        Q_EMIT isWriteableChanged();
    }
}

void StorageMonitor::setLocation(QString location)
{
    if (location != m_location) {
        m_timer.stop();
        m_location = location;

        m_storage.setPath(m_location);
        checkDiskSpace();
        checkWriteable();
        if (m_storage.isValid()) {
            m_timer.start();
        }

        Q_EMIT locationChanged();
    }
}

QString StorageMonitor::location() const
{
    return m_location;
}

bool StorageMonitor::diskSpaceLow() const
{
    return m_low;
}

bool StorageMonitor::diskSpaceCriticallyLow() const
{
    return m_criticallyLow;
}

bool StorageMonitor::isWriteable() const
{
    return m_writeable;
}