1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
|
/*
SPDX-FileCopyrightText: 2009 Niko Sams <niko.sams@gmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "pathmappings.h"
#include <debug.h>
#include <QAbstractTableModel>
#include <QVBoxLayout>
#include <QTableView>
#include <QHeaderView>
#include <QAction>
#include <QFile>
#include <QIcon>
#include <KLocalizedString>
#include <KConfigGroup>
namespace {
static QUrl rebaseMatchingUrl(const QUrl& toRebase, const KConfigGroup& config, const QString& baseEntry, const QString& rebaseEntry)
{
const QUrl::UrlFormattingOption matchOpts = QUrl::NormalizePathSegments;
const auto configGroups = config.groupList();
for (const QString& group : configGroups) {
KConfigGroup pathCfg = config.group(group);
const QString baseStr = pathCfg.readEntry(baseEntry, QUrl()).url(matchOpts);
const QString searchStr = toRebase.url(matchOpts);
if (searchStr.contains(baseStr)) {
const QUrl rebase = pathCfg.readEntry(rebaseEntry, QUrl());
return rebase.resolved(QUrl(searchStr.mid(baseStr.length())));
}
}
//No mapping found
return toRebase;
}
}
namespace KDevelop {
const QString PathMappings::pathMappingsEntry(QStringLiteral("Path Mappings"));
const QString PathMappings::pathMappingRemoteEntry(QStringLiteral("Remote"));
const QString PathMappings::pathMappingLocalEntry(QStringLiteral("Local"));
QUrl PathMappings::convertToLocalUrl(const KConfigGroup& config, const QUrl& remoteUrl)
{
if (remoteUrl.isLocalFile() && QFile::exists(remoteUrl.toLocalFile())) {
return remoteUrl;
}
KConfigGroup cfg = config.group(pathMappingsEntry);
return rebaseMatchingUrl(remoteUrl, cfg, pathMappingRemoteEntry, pathMappingLocalEntry);
}
QUrl PathMappings::convertToRemoteUrl(const KConfigGroup& config, const QUrl& localUrl)
{
KConfigGroup cfg = config.group(pathMappingsEntry);
return rebaseMatchingUrl(localUrl, cfg, pathMappingLocalEntry, pathMappingRemoteEntry);
}
class PathMappingModel : public QAbstractTableModel
{
Q_OBJECT
public:
int columnCount(const QModelIndex& parent = QModelIndex()) const override
{
if (parent.isValid()) return 0;
return 2;
}
int rowCount(const QModelIndex& parent = QModelIndex()) const override
{
if (parent.isValid()) return 0;
return m_paths.count() + 1;
}
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
if (section == 0) {
return i18n("Remote Path");
} else if (section == 1) {
return i18n("Local Path");
}
}
return QAbstractTableModel::headerData(section, orientation, role);
}
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override
{
if (!index.isValid()) return QVariant();
if (index.parent().isValid()) return QVariant();
if (index.column() > 1) return QVariant();
if (index.row() > m_paths.count()) return QVariant();
if (role == Qt::DisplayRole || role == Qt::EditRole) {
if (index.row() == m_paths.count()) return QString();
if (index.column() == 0) {
return m_paths[index.row()].remote.toDisplayString(QUrl::PreferLocalFile);
} else if (index.column() == 1) {
return m_paths[index.row()].local.toDisplayString(QUrl::PreferLocalFile);
}
}
return QVariant();
}
Qt::ItemFlags flags(const QModelIndex& index) const override
{
if (index.parent().isValid()) return Qt::NoItemFlags;
if (!index.isValid()) return Qt::NoItemFlags;
return ( Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled );
}
bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole) override
{
if (!index.isValid()) return false;
if (index.parent().isValid()) return false;
if (index.column() > 1) return false;
if (index.row() > m_paths.count()) return false;
if (role == Qt::EditRole) {
if (index.row() == m_paths.count()) {
beginInsertRows(QModelIndex(), index.row()+1, index.row()+1);
m_paths.append(Path());
endInsertRows();
}
if (index.column() == 0) {
m_paths[index.row()].remote = QUrl::fromUserInput(value.toString());
} else if (index.column() == 1) {
m_paths[index.row()].local = QUrl::fromLocalFile(value.toString());
}
emit dataChanged(index, index);
return true;
}
return false;
}
bool removeRows(int row, int count, const QModelIndex& parent = QModelIndex()) override
{
if (parent.isValid()) return false;
if (row+count > m_paths.count()) return false;
beginRemoveRows(parent, row, row+count-1);
for (int i=0; i<count; ++i) {
qCDebug(DEBUGGER) << row + i;
m_paths.removeAt(row + i);
}
qCDebug(DEBUGGER) << m_paths.count();
endRemoveRows();
return true;
}
void loadFromConfiguration(const KConfigGroup &config)
{
beginResetModel();
m_paths.clear();
KConfigGroup cfg = config.group(PathMappings::pathMappingsEntry);
const auto pathCount = cfg.readEntry("Count", 0);
m_paths.reserve(pathCount);
for (int i = 0; i < pathCount; ++i) {
KConfigGroup pCfg = cfg.group(QString::number(i+1));
Path p;
p.remote = pCfg.readEntry(PathMappings::pathMappingRemoteEntry, QUrl());
p.local = pCfg.readEntry(PathMappings::pathMappingLocalEntry, QUrl());
m_paths << p;
}
endResetModel();
}
void saveToConfiguration(KConfigGroup config) const
{
qCDebug(DEBUGGER) << m_paths.count();
KConfigGroup cfg = config.group(PathMappings::pathMappingsEntry);
cfg.writeEntry("Count", m_paths.count());
int i=0;
for (const Path& p : m_paths) {
i++;
KConfigGroup pCfg = cfg.group(QString::number(i));
pCfg.writeEntry(PathMappings::pathMappingRemoteEntry, p.remote);
pCfg.writeEntry(PathMappings::pathMappingLocalEntry, p.local);
}
cfg.sync();
}
private:
struct Path {
QUrl remote;
QUrl local;
};
QVector<Path> m_paths;
};
PathMappingsWidget::PathMappingsWidget(QWidget* parent): QWidget(parent)
{
auto *verticalLayout = new QVBoxLayout(this);
m_pathMappingTable = new QTableView(this);
m_pathMappingTable->setSelectionBehavior(QAbstractItemView::SelectRows);
m_pathMappingTable->horizontalHeader()->setDefaultSectionSize(150);
m_pathMappingTable->horizontalHeader()->setStretchLastSection(true);
verticalLayout->addWidget(m_pathMappingTable);
m_pathMappingTable->setModel(new PathMappingModel());
connect(m_pathMappingTable->model(), &QAbstractItemModel::dataChanged, this, &PathMappingsWidget::changed);
connect(m_pathMappingTable->model(), &QAbstractItemModel::rowsRemoved, this, &PathMappingsWidget::changed);
connect(m_pathMappingTable->model(), &QAbstractItemModel::rowsInserted, this, &PathMappingsWidget::changed);
auto* deletePath = new QAction(
QIcon::fromTheme(QStringLiteral("edit-delete")),
i18n( "Delete" ),
this
);
connect(deletePath, &QAction::triggered, this, &PathMappingsWidget::deletePath);
deletePath->setShortcut(Qt::Key_Delete);
deletePath->setShortcutContext(Qt::WidgetWithChildrenShortcut);
m_pathMappingTable->addAction(deletePath);
}
void PathMappingsWidget::deletePath()
{
const auto selectedRows = m_pathMappingTable->selectionModel()->selectedRows();
for (const QModelIndex& i : selectedRows) {
m_pathMappingTable->model()->removeRow(i.row(), i.parent());
}
}
void PathMappingsWidget::loadFromConfiguration(const KConfigGroup& cfg)
{
static_cast<PathMappingModel*>(m_pathMappingTable->model())->loadFromConfiguration(cfg);
}
void PathMappingsWidget::saveToConfiguration(const KConfigGroup& cfg) const
{
static_cast<PathMappingModel*>(m_pathMappingTable->model())->saveToConfiguration(cfg);
}
}
#include "pathmappings.moc"
#include "moc_pathmappings.cpp"
|