~ubuntu-filemanager-dev/ubuntu-filemanager-app/trunk

« back to all changes in this revision

Viewing changes to src/plugin/folderlistmodel/disk/disklocation.cpp

  • Committer: Bileto Bot
  • Date: 2017-04-04 17:06:41 UTC
  • mfrom: (588.1.19 fix-desktop-file)
  • Revision ID: ci-train-bot@canonical.com-20170404170641-1p15lmx8wodlx2ut
* Rename binary file to ubuntu-filemanager-app
* Join plugin packages into the main package 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**************************************************************************
 
2
 *
 
3
 * Copyright 2014 Canonical Ltd.
 
4
 * Copyright 2014 Carlos J Mazieri <carlos.mazieri@gmail.com>
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU Lesser General Public License as published by
 
8
 * the Free Software Foundation; version 3.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU Lesser General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU Lesser General Public License
 
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
 *
 
18
 * File: disklocation.cpp
 
19
 * Date: 08/03/2014
 
20
 */
 
21
 
 
22
#include "disklocation.h"
 
23
#include "disklocationitemdiriterator.h"
 
24
#include "iorequest.h"
 
25
#include "ioworkerthread.h"
 
26
#include "externalfswatcher.h"
 
27
#include "locationurl.h"
 
28
#include "disklocationitemfile.h"
 
29
#include "disklocationitemdir.h"
 
30
 
 
31
 
 
32
#if defined(Q_OS_UNIX)
 
33
#include <sys/statvfs.h>
 
34
#endif
 
35
 
 
36
#include <QDebug>
 
37
 
 
38
#if defined(DEBUG_EXT_FS_WATCHER)
 
39
# define DEBUG_WATCHER()  qDebug() << "[extFsWatcher]" << QDateTime::currentDateTime().toString("hh:mm:ss.zzz") \
 
40
                                   << Q_FUNC_INFO << this
 
41
#else
 
42
# define DEBUG_WATCHER() /**/
 
43
#endif
 
44
 
 
45
DiskLocation::DiskLocation(int type, QObject *parent)
 
46
   : Location(type, parent)
 
47
   , m_extWatcher(0)
 
48
{
 
49
}
 
50
 
 
51
 
 
52
DiskLocation::~ DiskLocation()
 
53
{
 
54
    stopExternalFsWatcher();
 
55
}
 
56
 
 
57
 
 
58
/*!
 
59
 * \brief DiskLocation::stopExternalFsWatcher() stops the External File System Watcher
 
60
 */
 
61
void DiskLocation::stopExternalFsWatcher()
 
62
{
 
63
        if (m_extWatcher)
 
64
        {
 
65
            DEBUG_WATCHER();
 
66
            delete m_extWatcher;
 
67
            m_extWatcher = 0;
 
68
        }
 
69
}
 
70
 
 
71
 
 
72
/*!
 
73
 * \brief DiskLocation::startExternalFsWatcher() starts the External File System Watcher
 
74
 */
 
75
void DiskLocation::startExternalFsWatcher()
 
76
{
 
77
    if (m_extWatcher == 0)
 
78
    {
 
79
        DEBUG_WATCHER();
 
80
        m_extWatcher = new ExternalFSWatcher(this);
 
81
        m_extWatcher->setIntervalToNotifyChanges(EX_FS_WATCHER_TIMER_INTERVAL);
 
82
 
 
83
        connect(m_extWatcher, SIGNAL(pathModified(QString)),
 
84
                this,         SIGNAL(extWatcherPathChanged(QString)));            
 
85
    }
 
86
    if (m_extWatcher && m_info)
 
87
    {
 
88
          //setCurrentPath() checks for empty paths
 
89
           m_extWatcher->setCurrentPath(m_info->absoluteFilePath());
 
90
    }
 
91
}
 
92
 
 
93
 
 
94
void DiskLocation::onItemsFetched()
 
95
{
 
96
    if (m_extWatcher)
 
97
    {
 
98
         m_extWatcher->setCurrentPath(m_info->absoluteFilePath());
 
99
    }
 
100
    emit itemsFetched();
 
101
}
 
102
 
 
103
 
 
104
void DiskLocation::startWorking()
 
105
{
 
106
    if (m_usingExternalWatcher)
 
107
    {
 
108
        startExternalFsWatcher();
 
109
    }
 
110
}
 
111
 
 
112
 
 
113
void DiskLocation::stopWorking()
 
114
{
 
115
    stopExternalFsWatcher();
 
116
}
 
117
 
 
118
 
 
119
void DiskLocation::fetchExternalChanges(const QString &path,
 
120
                                        const DirItemInfoList &list,
 
121
                                        QDir::Filters dirFilter)
 
122
{
 
123
     ExternalFileSystemChangesWorker *extFsWorker =
 
124
          new ExternalFileSystemChangesWorker(list,
 
125
                                              path,
 
126
                                              dirFilter, false);
 
127
     addExternalFsWorkerRequest(extFsWorker);
 
128
 
 
129
 
 
130
}
 
131
 
 
132
void DiskLocation::addExternalFsWorkerRequest(ExternalFileSystemChangesWorker *extFsWorker)
 
133
{
 
134
    connect(extFsWorker,    SIGNAL(added(DirItemInfo)),
 
135
            this,           SIGNAL(extWatcherItemAdded(DirItemInfo)));
 
136
 
 
137
    connect(extFsWorker,    SIGNAL(removed(DirItemInfo)),
 
138
            this,           SIGNAL(extWatcherItemRemoved(DirItemInfo)));
 
139
 
 
140
    connect(extFsWorker,    SIGNAL(changed(DirItemInfo)),
 
141
            this,           SIGNAL(extWatcherItemChanged(DirItemInfo)));
 
142
 
 
143
    connect(extFsWorker,    SIGNAL(finished(int)),
 
144
            this,           SIGNAL(extWatcherChangesFetched(int)));
 
145
 
 
146
        workerThread()->addRequest(extFsWorker);
 
147
}
 
148
 
 
149
 
 
150
ExternalFSWatcher  * DiskLocation::getExternalFSWatcher() const
 
151
{   
 
152
    return m_extWatcher;
 
153
}
 
154
 
 
155
 
 
156
void DiskLocation::setUsingExternalWatcher(bool use)
 
157
{   
 
158
    if ((m_usingExternalWatcher = use))
 
159
    {
 
160
        startExternalFsWatcher();
 
161
    }
 
162
    else
 
163
    {
 
164
        stopExternalFsWatcher();
 
165
    }
 
166
}
 
167
 
 
168
 
 
169
DirItemInfo * DiskLocation::newItemInfo(const QString &urlPath)
 
170
{
 
171
    return new DirItemInfo(urlPath);
 
172
}
 
173
 
 
174
 
 
175
DirListWorker * DiskLocation::newListWorker(const QString &urlPath, QDir::Filters filter, const bool isRecursive)
 
176
{
 
177
    return new DirListWorker(urlPath,filter,isRecursive);
 
178
}
 
179
 
 
180
 
 
181
QString DiskLocation::urlBelongsToLocation(const QString &urlPath, int indexOfColonAndSlash)
 
182
{
 
183
    QString ret;
 
184
    if (urlPath.startsWith(LocationUrl::DiskRootURL.midRef(0,5)))
 
185
    {
 
186
        ret  = QDir::rootPath() + DirItemInfo::removeExtraSlashes(urlPath, indexOfColonAndSlash+1);
 
187
    }
 
188
    return ret;
 
189
}
 
190
 
 
191
 
 
192
LocationItemDirIterator *
 
193
DiskLocation::newDirIterator(const QString &path,
 
194
                             QDir::Filters filters,
 
195
                             QDirIterator::IteratorFlags flags,
 
196
                             LocationItemDirIterator::LoadMode loadmode)
 
197
{
 
198
    Q_UNUSED(loadmode);
 
199
    return  new DiskLocationItemDirIterator(path, filters, flags);
 
200
}
 
201
 
 
202
 
 
203
LocationItemFile *
 
204
DiskLocation::newFile(const QString &path)
 
205
{
 
206
    return new DiskLocationItemFile(path, this);
 
207
}
 
208
 
 
209
 
 
210
LocationItemDir *
 
211
DiskLocation::newDir(const QString &dir)
 
212
{
 
213
    return new DiskLocationItemDir(dir);
 
214
}
 
215
 
 
216
 
 
217
bool DiskLocation::isThereDiskSpace(const QString &pathname, qint64 requiredSize)
 
218
{
 
219
    bool ret = true;
 
220
#if defined(Q_OS_UNIX)
 
221
    QFileInfo info(pathname);    
 
222
    while (!info.exists() && info.absoluteFilePath() != QDir::rootPath())
 
223
    {
 
224
        info.setFile(info.absolutePath());       
 
225
    }
 
226
    struct statvfs  vfs;
 
227
    if ( ::statvfs( QFile::encodeName(info.absoluteFilePath()).constData(), &vfs) == 0 )
 
228
    {
 
229
        qint64 free =  vfs.f_bsize * vfs.f_bfree;
 
230
        ret = free > requiredSize;
 
231
    }
 
232
#endif
 
233
   return ret;
 
234
}