1 /*
2 resourcebrowser.cpp
3
4 This file is part of GammaRay, the Qt application inspection and
5 manipulation tool.
6
7 Copyright (C) 2010-2021 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
8 Author: Stephen Kelly <stephen.kelly@kdab.com>
9
10 Licensees holding valid commercial KDAB GammaRay licenses may use this file in
11 accordance with GammaRay Commercial License Agreement provided with the Software.
12
13 Contact info@kdab.com if any conditions of this licensing are not clear to you.
14
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation, either version 2 of the License, or
18 (at your option) any later version.
19
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 */
28
29 #include "resourcebrowser.h"
30 #include "resourcefiltermodel.h"
31
32 #include "qt/resourcemodel.h"
33 #include "common/objectbroker.h"
34
35 #include <core/remote/serverproxymodel.h>
36
37 #include <QDebug>
38 #include <QItemSelectionModel>
39 #include <QUrl>
40
41 using namespace GammaRay;
42
ResourceBrowser(Probe * probe,QObject * parent)43 ResourceBrowser::ResourceBrowser(Probe *probe, QObject *parent)
44 : ResourceBrowserInterface(parent)
45 {
46 auto *resourceModel = new ResourceModel(this);
47 auto proxy = new ServerProxyModel<ResourceFilterModel>(this);
48 proxy->setSourceModel(resourceModel);
49 probe->registerModel(QStringLiteral("com.kdab.GammaRay.ResourceModel"), proxy);
50 QItemSelectionModel *selectionModel = ObjectBroker::selectionModel(proxy);
51 connect(selectionModel, &QItemSelectionModel::currentChanged,
52 this, [this](const QModelIndex &index) { currentChanged(index); });
53 }
54
downloadResource(const QString & sourceFilePath,const QString & targetFilePath)55 void ResourceBrowser::downloadResource(const QString &sourceFilePath, const QString &targetFilePath)
56 {
57 const QFileInfo fi(sourceFilePath);
58
59 if (fi.isFile()) {
60 QFile f(fi.absoluteFilePath());
61 if (f.open(QFile::ReadOnly))
62 emit resourceDownloaded(targetFilePath, f.readAll());
63 else
64 qWarning() << "Failed to open" << fi.absoluteFilePath();
65 }
66 }
67
selectResource(const QString & sourceFilePath,int line,int column)68 void ResourceBrowser::selectResource(const QString &sourceFilePath, int line, int column)
69 {
70 const bool locked = blockSignals(true);
71 const QItemSelectionModel::SelectionFlags selectionFlags = QItemSelectionModel::ClearAndSelect
72 |QItemSelectionModel::Rows
73 | QItemSelectionModel::Current;
74 const Qt::MatchFlags matchFlags = Qt::MatchExactly | Qt::MatchRecursive | Qt::MatchWrap;
75 auto model = ObjectBroker::model(QStringLiteral("com.kdab.GammaRay.ResourceModel"));
76 auto selectionModel = ObjectBroker::selectionModel(model);
77 const QString filePath = QLatin1Char(':') + QUrl(sourceFilePath).path();
78 const QModelIndex index = model->match(model->index(0,
79 0), ResourceModel::FilePathRole, filePath, 1,
80 matchFlags).value(0);
81 selectionModel->setCurrentIndex(index, selectionFlags);
82 blockSignals(locked);
83 currentChanged(index, line, column);
84 }
85
currentChanged(const QModelIndex & current,int line,int column)86 void ResourceBrowser::currentChanged(const QModelIndex ¤t, int line, int column)
87 {
88 if (!current.isValid())
89 return;
90 const auto idx = current.sibling(current.row(), 0);
91 const QFileInfo fi(idx.data(ResourceModel::FilePathRole).toString());
92 if (!fi.isFile()) {
93 emit resourceDeselected();
94 return;
95 }
96
97 QFile f(fi.absoluteFilePath());
98 if (f.open(QFile::ReadOnly)) {
99 emit resourceSelected(f.readAll(), line, column);
100 } else {
101 qWarning() << "Failed to open" << fi.absoluteFilePath();
102 emit resourceDeselected();
103 }
104 }
105