Line data Source code
1 : // DUNE DAQ modification notice:
2 : // This file has been modified from the original ATLAS dbe source for the DUNE DAQ project.
3 : // Fork baseline commit: dbe-02-12-17 (2022-05-12).
4 : // Renamed since fork: no.
5 :
6 : /// Including QT Headers
7 : #include "dbe/StyleUtility.hpp"
8 : #include "dbe/confaccessor.hpp"
9 : #include "dbe/ui_constants.hpp"
10 :
11 : #include <QDir>
12 : #include <QFileInfo>
13 : /// Including DBE Headers
14 : #include "dbe/FileModel.hpp"
15 : #include "dbe/MainWindow.hpp"
16 : #include "dbe/config_api_get.hpp"
17 :
18 0 : dbe::FileModel::FileModel(QObject* parent)
19 : : QAbstractTableModel(parent)
20 0 : , Headers{ tr("Name"), tr("Folder"), tr("Access"), tr("Status") }
21 : {
22 0 : initpaths();
23 0 : initconnections();
24 0 : initmodel();
25 0 : }
26 :
27 0 : dbe::FileModel::FileModel(QList<QStringList> const& FileList, QObject* parent)
28 : : QAbstractTableModel(parent)
29 0 : , IncludedFiles(FileList)
30 0 : , Headers{ tr("Name"), tr("Folder"), tr("Access"), tr("Status") }
31 : {
32 0 : initconnections();
33 0 : }
34 :
35 0 : dbe::FileModel::~FileModel() {}
36 :
37 : int
38 0 : dbe::FileModel::rowCount(const QModelIndex& parent) const
39 : {
40 0 : if (!parent.isValid()) {
41 0 : return IncludedFiles.size();
42 : }
43 :
44 : return 0;
45 : }
46 :
47 : int
48 0 : dbe::FileModel::columnCount(const QModelIndex& parent) const
49 : {
50 0 : Q_UNUSED(parent)
51 0 : return Headers.size();
52 : }
53 :
54 : QVariant
55 0 : dbe::FileModel::data(const QModelIndex& index, int role) const
56 : {
57 0 : if (index.isValid()) {
58 0 : if (role == Qt::DisplayRole) {
59 0 : return QVariant(IncludedFiles.at(index.row()).at(index.column()));
60 : }
61 0 : if (role == Qt::ForegroundRole) {
62 0 : if (IncludedFiles.at(index.row()).at(static_cast<int>(tablepositions::filepermission)) == "RO") {
63 0 : return QBrush(StyleUtility::FileReadOnlyForeground);
64 : }
65 : }
66 0 : if (role == Qt::BackgroundRole) {
67 0 : if (IncludedFiles.at(index.row()).at(static_cast<int>(tablepositions::filepermission)) == "RO") {
68 0 : return QBrush(StyleUtility::FileReadOnlyBackground);
69 : }
70 : }
71 : }
72 :
73 0 : return QVariant();
74 : }
75 :
76 : QVariant
77 0 : dbe::FileModel::headerData(int section, Qt::Orientation orientation, int role) const
78 : {
79 0 : if (role == Qt::DisplayRole) {
80 0 : if (orientation == Qt::Horizontal) {
81 0 : return Headers.at(section);
82 : }
83 :
84 0 : if (orientation == Qt::Vertical) {
85 0 : return section + 1;
86 : }
87 : }
88 :
89 0 : return QVariant();
90 : }
91 :
92 : Qt::ItemFlags
93 0 : dbe::FileModel::flags(const QModelIndex& /*index*/) const
94 : {
95 : // if ( IncludedFiles.at ( index.row() ).at ( static_cast<int>
96 : // ( tablepositions::filepermission ) ) == "RW" )
97 : // {
98 0 : return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
99 : // }
100 :
101 : // return Qt::ItemIsSelectable;
102 : }
103 :
104 : void
105 0 : dbe::FileModel::initpaths()
106 : {
107 0 : QString TDAQ_DB_REPOSITORY = getenv("TDAQ_DB_REPOSITORY");
108 0 : if (TDAQ_DB_REPOSITORY.isEmpty() == false) {
109 0 : QString TDAQ_DB_USER_REPOSITORY = getenv("TDAQ_DB_USER_REPOSITORY");
110 0 : FolderPathList = TDAQ_DB_USER_REPOSITORY.split(":", Qt::SkipEmptyParts);
111 :
112 0 : FolderPathList << dbe::MainWindow::findthis()->find_db_repository_dir();
113 0 : } else {
114 0 : QString DUNEDAQ_DB_PATH = getenv("DUNEDAQ_DB_PATH");
115 0 : FolderPathList = DUNEDAQ_DB_PATH.split(":", Qt::SkipEmptyParts);
116 0 : }
117 :
118 0 : for (QString& PathName : FolderPathList) {
119 0 : if (!PathName.endsWith("/")) {
120 0 : PathName.append("/");
121 : }
122 : }
123 0 : }
124 :
125 : void
126 0 : dbe::FileModel::initconnections()
127 : {
128 0 : connect(
129 0 : this, SIGNAL(FileCacheReady(QList<QStringList>&)), &confaccessor::ref(), SLOT(GetFileCache(QList<QStringList>&)));
130 0 : }
131 :
132 : QString
133 0 : dbe::FileModel::GetFullFileName(QString& FileName)
134 : {
135 0 : QFileInfo FileInfo(FileName);
136 :
137 0 : if (FileInfo.isRelative()) {
138 0 : bool found = false;
139 0 : for (const QString& Folder : FolderPathList) {
140 0 : QDir TdaqFolder(Folder);
141 :
142 0 : if (TdaqFolder.exists(FileName)) {
143 0 : FileName = TdaqFolder.path() + "/" + FileName;
144 0 : char* rpath = realpath(FileName.toStdString().c_str(), NULL);
145 0 : FileName = QString(rpath);
146 0 : free(rpath);
147 0 : found = true;
148 0 : break;
149 : }
150 0 : }
151 0 : if (!found) {
152 0 : FileName = FileInfo.absoluteFilePath();
153 : }
154 : }
155 :
156 0 : return FileName;
157 0 : }
158 :
159 : QList<QStringList>
160 0 : dbe::FileModel::GetFilesInfo() const
161 : {
162 0 : return IncludedFiles;
163 : }
164 :
165 : void
166 0 : dbe::FileModel::initmodel()
167 : {
168 0 : QStringList sources = dbe::config::api::get::file::inclusions({ confaccessor::dbfullname() });
169 :
170 0 : std::list<std::string> updated = confaccessor::uncommitted_files();
171 0 : for (QString const& source : sources) {
172 0 : QFileInfo srcinfo(source);
173 0 : QString fn = srcinfo.filePath();
174 0 : QString dn = GetFullFileName(fn).remove(QRegularExpression("/[a-zA-Z0-9_-]*.data.xml"));
175 :
176 0 : QString modified{};
177 0 : auto file = GetFullFileName(fn).toStdString();
178 0 : for (auto ufile : updated) {
179 0 : if (file == ufile) {
180 0 : modified = "Modified";
181 0 : break;
182 : }
183 0 : }
184 :
185 0 : IncludedFiles.append(
186 0 : QStringList{ srcinfo.fileName(), dn, confaccessor::check_file_rw(source) ? "RW" : "RO", modified });
187 0 : }
188 :
189 0 : if (IncludedFiles.size()) {
190 0 : emit FileCacheReady(IncludedFiles);
191 : }
192 0 : }
|