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 Utils::Fs::toNativePath(m_watchedFolders
.at(index
.row()));
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 QString folderPath
= m_watchedFolders
.takeAt(i
);
95 m_watchedFoldersOptions
.remove(folderPath
);
96 m_deletedFolders
.insert(folderPath
);
103 void WatchedFoldersModel::addFolder(const QString
&path
, const TorrentFilesWatcher::WatchedFolderOptions
&options
)
105 const QString cleanWatchPath
= m_fsWatcher
->makeCleanPath(path
);
106 if (m_watchedFoldersOptions
.contains(cleanWatchPath
))
107 throw RuntimeError(tr("Folder '%1' is already in watch list.").arg(path
));
109 const QDir watchDir
{cleanWatchPath
};
110 if (!watchDir
.exists())
111 throw RuntimeError(tr("Folder '%1' doesn't exist.").arg(path
));
112 if (!watchDir
.isReadable())
113 throw RuntimeError(tr("Folder '%1' isn't readable.").arg(path
));
115 m_deletedFolders
.remove(cleanWatchPath
);
117 beginInsertRows(QModelIndex(), rowCount(), rowCount());
118 m_watchedFolders
.append(cleanWatchPath
);
119 m_watchedFoldersOptions
[cleanWatchPath
] = options
;
123 TorrentFilesWatcher::WatchedFolderOptions
WatchedFoldersModel::folderOptions(const int row
) const
125 Q_ASSERT((row
>= 0) && (row
< rowCount()));
127 const QString folderPath
= m_watchedFolders
.at(row
);
128 return m_watchedFoldersOptions
[folderPath
];
131 void WatchedFoldersModel::setFolderOptions(const int row
, const TorrentFilesWatcher::WatchedFolderOptions
&options
)
133 Q_ASSERT((row
>= 0) && (row
< rowCount()));
135 const QString folderPath
= m_watchedFolders
.at(row
);
136 m_watchedFoldersOptions
[folderPath
] = options
;
139 void WatchedFoldersModel::apply()
141 const QSet
<QString
> deletedFolders
{m_deletedFolders
};
142 // We have to clear `m_deletedFolders` for optimization reason, otherwise
143 // it will be cleared one element at a time in `onFolderRemoved()` handler
144 m_deletedFolders
.clear();
145 for (const QString
&path
: deletedFolders
)
146 m_fsWatcher
->removeWatchedFolder(path
);
148 for (const QString
&path
: asConst(m_watchedFolders
))
149 m_fsWatcher
->setWatchedFolder(path
, m_watchedFoldersOptions
.value(path
));
152 void WatchedFoldersModel::onFolderSet(const QString
&path
, const TorrentFilesWatcher::WatchedFolderOptions
&options
)
154 if (!m_watchedFoldersOptions
.contains(path
))
156 m_deletedFolders
.remove(path
);
158 beginInsertRows(QModelIndex(), rowCount(), rowCount());
159 m_watchedFolders
.append(path
);
160 m_watchedFoldersOptions
[path
] = options
;
165 m_watchedFoldersOptions
[path
] = options
;
169 void WatchedFoldersModel::onFolderRemoved(const QString
&path
)
171 const int row
= m_watchedFolders
.indexOf(path
);
175 m_deletedFolders
.remove(path
);