2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2021 Vladimir Golovnev <glassez@yandex.ru>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
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 General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
29 #include "watchedfoldersmodel.h"
33 #include "base/exceptions.h"
34 #include "base/global.h"
35 #include "base/utils/fs.h"
37 WatchedFoldersModel::WatchedFoldersModel(TorrentFilesWatcher
*fsWatcher
, QObject
*parent
)
38 : QAbstractListModel
{parent
}
39 , m_fsWatcher
{fsWatcher
}
40 , m_watchedFolders
{m_fsWatcher
->folders().keys()}
41 , m_watchedFoldersOptions
{m_fsWatcher
->folders()}
43 connect(m_fsWatcher
, &TorrentFilesWatcher::watchedFolderSet
, this, &WatchedFoldersModel::onFolderSet
);
44 connect(m_fsWatcher
, &TorrentFilesWatcher::watchedFolderRemoved
, this, &WatchedFoldersModel::onFolderRemoved
);
47 int WatchedFoldersModel::rowCount(const QModelIndex
&parent
) const
49 return parent
.isValid() ? 0 : m_watchedFolders
.count();
52 int WatchedFoldersModel::columnCount(const QModelIndex
&parent
) const
58 QVariant
WatchedFoldersModel::data(const QModelIndex
&index
, const int role
) const
60 if (!index
.isValid() || (index
.row() >= rowCount()) || (index
.column() >= columnCount()))
63 if (role
== Qt::DisplayRole
)
64 return m_watchedFolders
.at(index
.row()).toString();
69 QVariant
WatchedFoldersModel::headerData(const int section
, const Qt::Orientation orientation
, const int role
) const
71 if ((orientation
!= Qt::Horizontal
) || (role
!= Qt::DisplayRole
)
72 || (section
< 0) || (section
>= columnCount()))
77 return tr("Watched Folder");
80 bool WatchedFoldersModel::removeRows(const int row
, const int count
, const QModelIndex
&parent
)
82 if (parent
.isValid() || (row
< 0) || (row
>= rowCount())
83 || (count
<= 0) || ((row
+ count
) > rowCount()))
88 const int firstRow
= row
;
89 const int lastRow
= row
+ (count
- 1);
91 beginRemoveRows(parent
, firstRow
, lastRow
);
92 for (int i
= firstRow
; i
<= lastRow
; ++i
)
94 const Path folderPath
= m_watchedFolders
.takeAt(i
);
95 m_watchedFoldersOptions
.remove(folderPath
);
96 m_deletedFolders
.insert(folderPath
);
103 void WatchedFoldersModel::addFolder(const Path
&path
, const TorrentFilesWatcher::WatchedFolderOptions
&options
)
106 throw InvalidArgument(tr("Watched folder path cannot be empty."));
108 if (path
.isRelative())
109 throw InvalidArgument(tr("Watched folder path cannot be relative."));
111 if (m_watchedFoldersOptions
.contains(path
))
112 throw RuntimeError(tr("Folder '%1' is already in watch list.").arg(path
.toString()));
114 const QDir watchDir
{path
.data()};
115 if (!watchDir
.exists())
116 throw RuntimeError(tr("Folder '%1' doesn't exist.").arg(path
.toString()));
117 if (!watchDir
.isReadable())
118 throw RuntimeError(tr("Folder '%1' isn't readable.").arg(path
.toString()));
120 m_deletedFolders
.remove(path
);
122 beginInsertRows(QModelIndex(), rowCount(), rowCount());
123 m_watchedFolders
.append(path
);
124 m_watchedFoldersOptions
[path
] = options
;
128 TorrentFilesWatcher::WatchedFolderOptions
WatchedFoldersModel::folderOptions(const int row
) const
130 Q_ASSERT((row
>= 0) && (row
< rowCount()));
132 const Path folderPath
= m_watchedFolders
.at(row
);
133 return m_watchedFoldersOptions
[folderPath
];
136 void WatchedFoldersModel::setFolderOptions(const int row
, const TorrentFilesWatcher::WatchedFolderOptions
&options
)
138 Q_ASSERT((row
>= 0) && (row
< rowCount()));
140 const Path folderPath
= m_watchedFolders
.at(row
);
141 m_watchedFoldersOptions
[folderPath
] = options
;
144 void WatchedFoldersModel::apply()
146 const QSet
<Path
> deletedFolders
{m_deletedFolders
};
147 // We have to clear `m_deletedFolders` for optimization reason, otherwise
148 // it will be cleared one element at a time in `onFolderRemoved()` handler
149 m_deletedFolders
.clear();
150 for (const Path
&path
: deletedFolders
)
151 m_fsWatcher
->removeWatchedFolder(path
);
153 for (const Path
&path
: asConst(m_watchedFolders
))
154 m_fsWatcher
->setWatchedFolder(path
, m_watchedFoldersOptions
.value(path
));
157 void WatchedFoldersModel::onFolderSet(const Path
&path
, const TorrentFilesWatcher::WatchedFolderOptions
&options
)
159 if (!m_watchedFoldersOptions
.contains(path
))
161 m_deletedFolders
.remove(path
);
163 beginInsertRows(QModelIndex(), rowCount(), rowCount());
164 m_watchedFolders
.append(path
);
165 m_watchedFoldersOptions
[path
] = options
;
170 m_watchedFoldersOptions
[path
] = options
;
174 void WatchedFoldersModel::onFolderRemoved(const Path
&path
)
176 const int row
= m_watchedFolders
.indexOf(path
);
180 m_deletedFolders
.remove(path
);