Don't overwrite stored layout of main window with incorrect one
[qBittorrent.git] / src / gui / watchedfoldersmodel.cpp
blob0bb4e4c75a550b43b5348abee1d845e84952ec51
1 /*
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"
31 #include <QDir>
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([[maybe_unused]] const QModelIndex &parent) const
54 return 1;
57 QVariant WatchedFoldersModel::data(const QModelIndex &index, const int role) const
59 if (!index.isValid() || (index.row() >= rowCount()) || (index.column() >= columnCount()))
60 return {};
62 if (role == Qt::DisplayRole)
63 return m_watchedFolders.at(index.row()).toString();
65 return {};
68 QVariant WatchedFoldersModel::headerData(const int section, const Qt::Orientation orientation, const int role) const
70 if ((orientation != Qt::Horizontal) || (role != Qt::DisplayRole)
71 || (section < 0) || (section >= columnCount()))
73 return {};
76 return tr("Watched Folder");
79 bool WatchedFoldersModel::removeRows(const int row, const int count, const QModelIndex &parent)
81 if (parent.isValid() || (row < 0) || (row >= rowCount())
82 || (count <= 0) || ((row + count) > rowCount()))
84 return false;
87 const int firstRow = row;
88 const int lastRow = row + (count - 1);
90 beginRemoveRows(parent, firstRow, lastRow);
91 for (int i = firstRow; i <= lastRow; ++i)
93 const Path folderPath = m_watchedFolders.takeAt(i);
94 m_watchedFoldersOptions.remove(folderPath);
95 m_deletedFolders.insert(folderPath);
97 endRemoveRows();
99 return true;
102 void WatchedFoldersModel::addFolder(const Path &path, const TorrentFilesWatcher::WatchedFolderOptions &options)
104 if (path.isEmpty())
105 throw InvalidArgument(tr("Watched folder path cannot be empty."));
107 if (path.isRelative())
108 throw InvalidArgument(tr("Watched folder path cannot be relative."));
110 if (m_watchedFoldersOptions.contains(path))
111 throw RuntimeError(tr("Folder '%1' is already in watch list.").arg(path.toString()));
113 const QDir watchDir {path.data()};
114 if (!watchDir.exists())
115 throw RuntimeError(tr("Folder '%1' doesn't exist.").arg(path.toString()));
116 if (!watchDir.isReadable())
117 throw RuntimeError(tr("Folder '%1' isn't readable.").arg(path.toString()));
119 m_deletedFolders.remove(path);
121 beginInsertRows(QModelIndex(), rowCount(), rowCount());
122 m_watchedFolders.append(path);
123 m_watchedFoldersOptions[path] = options;
124 endInsertRows();
127 TorrentFilesWatcher::WatchedFolderOptions WatchedFoldersModel::folderOptions(const int row) const
129 Q_ASSERT((row >= 0) && (row < rowCount()));
131 const Path folderPath = m_watchedFolders.at(row);
132 return m_watchedFoldersOptions[folderPath];
135 void WatchedFoldersModel::setFolderOptions(const int row, const TorrentFilesWatcher::WatchedFolderOptions &options)
137 Q_ASSERT((row >= 0) && (row < rowCount()));
139 const Path folderPath = m_watchedFolders.at(row);
140 m_watchedFoldersOptions[folderPath] = options;
143 void WatchedFoldersModel::apply()
145 const QSet<Path> deletedFolders {m_deletedFolders};
146 // We have to clear `m_deletedFolders` for optimization reason, otherwise
147 // it will be cleared one element at a time in `onFolderRemoved()` handler
148 m_deletedFolders.clear();
149 for (const Path &path : deletedFolders)
150 m_fsWatcher->removeWatchedFolder(path);
152 for (const Path &path : asConst(m_watchedFolders))
153 m_fsWatcher->setWatchedFolder(path, m_watchedFoldersOptions.value(path));
156 void WatchedFoldersModel::onFolderSet(const Path &path, const TorrentFilesWatcher::WatchedFolderOptions &options)
158 if (!m_watchedFoldersOptions.contains(path))
160 m_deletedFolders.remove(path);
162 beginInsertRows(QModelIndex(), rowCount(), rowCount());
163 m_watchedFolders.append(path);
164 m_watchedFoldersOptions[path] = options;
165 endInsertRows();
167 else
169 m_watchedFoldersOptions[path] = options;
173 void WatchedFoldersModel::onFolderRemoved(const Path &path)
175 const int row = m_watchedFolders.indexOf(path);
176 if (row >= 0)
177 removeRows(row, 1);
179 m_deletedFolders.remove(path);