Sync translations from Transifex and run lupdate
[qBittorrent.git] / src / gui / torrentcontenttreeview.cpp
blob0e4ab4329ebd6421676bcd65158d7695e8c1237e
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2014 Ivan Sorokin <vanyacpp@gmail.com>
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 "torrentcontenttreeview.h"
31 #include <QDir>
32 #include <QHeaderView>
33 #include <QKeyEvent>
34 #include <QLineEdit>
35 #include <QMessageBox>
36 #include <QModelIndexList>
37 #include <QTableView>
38 #include <QThread>
40 #include "base/bittorrent/abstractfilestorage.h"
41 #include "base/bittorrent/common.h"
42 #include "base/bittorrent/session.h"
43 #include "base/bittorrent/torrent.h"
44 #include "base/bittorrent/torrentinfo.h"
45 #include "base/exceptions.h"
46 #include "base/global.h"
47 #include "base/utils/fs.h"
48 #include "autoexpandabledialog.h"
49 #include "raisedmessagebox.h"
50 #include "torrentcontentfiltermodel.h"
51 #include "torrentcontentmodelitem.h"
53 namespace
55 QString getFullPath(const QModelIndex &idx)
57 QStringList paths;
58 for (QModelIndex i = idx; i.isValid(); i = i.parent())
59 paths.prepend(i.data().toString());
60 return paths.join(QLatin1Char {'/'});
64 TorrentContentTreeView::TorrentContentTreeView(QWidget *parent)
65 : QTreeView(parent)
67 setExpandsOnDoubleClick(false);
69 // This hack fixes reordering of first column with Qt5.
70 // https://github.com/qtproject/qtbase/commit/e0fc088c0c8bc61dbcaf5928b24986cd61a22777
71 QTableView unused;
72 unused.setVerticalHeader(header());
73 header()->setParent(this);
74 header()->setStretchLastSection(false);
75 unused.setVerticalHeader(new QHeaderView(Qt::Horizontal));
78 void TorrentContentTreeView::keyPressEvent(QKeyEvent *event)
80 if ((event->key() != Qt::Key_Space) && (event->key() != Qt::Key_Select))
82 QTreeView::keyPressEvent(event);
83 return;
86 event->accept();
88 QModelIndex current = currentNameCell();
90 QVariant value = current.data(Qt::CheckStateRole);
91 if (!value.isValid())
93 Q_ASSERT(false);
94 return;
97 Qt::CheckState state = (static_cast<Qt::CheckState>(value.toInt()) == Qt::Checked
98 ? Qt::Unchecked : Qt::Checked);
100 const QModelIndexList selection = selectionModel()->selectedRows(TorrentContentModelItem::COL_NAME);
102 for (const QModelIndex &index : selection)
104 Q_ASSERT(index.column() == TorrentContentModelItem::COL_NAME);
105 model()->setData(index, state, Qt::CheckStateRole);
109 void TorrentContentTreeView::renameSelectedFile(BitTorrent::AbstractFileStorage &fileStorage)
111 const QModelIndexList selectedIndexes = selectionModel()->selectedRows(0);
112 if (selectedIndexes.size() != 1) return;
114 const QPersistentModelIndex modelIndex = selectedIndexes.first();
115 if (!modelIndex.isValid()) return;
117 auto model = dynamic_cast<TorrentContentFilterModel *>(TorrentContentTreeView::model());
118 if (!model) return;
120 const bool isFile = (model->itemType(modelIndex) == TorrentContentModelItem::FileType);
122 // Ask for new name
123 bool ok = false;
124 QString newName = AutoExpandableDialog::getText(this, tr("Renaming"), tr("New name:"), QLineEdit::Normal
125 , modelIndex.data().toString(), &ok, isFile).trimmed();
126 if (!ok || !modelIndex.isValid()) return;
128 const QString oldName = modelIndex.data().toString();
129 if (newName == oldName)
130 return; // Name did not change
132 const QString parentPath = getFullPath(modelIndex.parent());
133 const QString oldPath {parentPath.isEmpty() ? oldName : parentPath + QLatin1Char {'/'} + oldName};
134 const QString newPath {parentPath.isEmpty() ? newName : parentPath + QLatin1Char {'/'} + newName};
138 if (isFile)
139 fileStorage.renameFile(oldPath, newPath);
140 else
141 fileStorage.renameFolder(oldPath, newPath);
143 model->setData(modelIndex, newName);
145 catch (const RuntimeError &error)
147 RaisedMessageBox::warning(this, tr("Rename error"), error.message(), QMessageBox::Ok);
151 QModelIndex TorrentContentTreeView::currentNameCell()
153 QModelIndex current = currentIndex();
154 if (!current.isValid())
156 Q_ASSERT(false);
157 return {};
160 return model()->index(current.row(), TorrentContentModelItem::COL_NAME, current.parent());