1 /**
2 * UGENE - Integrated Bioinformatics Tools.
3 * Copyright (C) 2008-2021 UniPro <ugene@unipro.ru>
4 * http://ugene.net
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301, USA.
20 */
21
22 #include "DataPathRegistry.h"
23
24 #include <QFile>
25
26 #include <U2Core/AppContext.h>
27 #include <U2Core/Settings.h>
28 #include <U2Core/U2SafePoints.h>
29
30 namespace U2 {
31
32 ////////////////////////////////////////
33 // U2DataPath
U2DataPath(const QString & _name,const QString & _path,const QString & _descr,Options _options)34 U2DataPath::U2DataPath(const QString &_name, const QString &_path, const QString &_descr, Options _options)
35 : name(_name),
36 path(_path),
37 description(_descr),
38 options(_options),
39 valid(false) {
40 init();
41 }
42
getPathByName(const QString & name) const43 QString U2DataPath::getPathByName(const QString &name) const {
44 QString res = "";
45
46 if (dataItems.contains(name)) {
47 res = dataItems.value(name, "");
48 }
49
50 return res;
51 }
52
operator ==(const U2DataPath & other) const53 bool U2DataPath::operator==(const U2DataPath &other) const {
54 return (name == other.name) && (options == other.options);
55 }
56
operator !=(const U2DataPath & other) const57 bool U2DataPath::operator!=(const U2DataPath &other) const {
58 return !(*this == other);
59 }
60
init()61 void U2DataPath::init() {
62 if (path.isEmpty() || !QFile::exists(path)) {
63 valid = false;
64 return;
65 }
66
67 QFileInfo fi(path);
68 QString filePath = fi.absoluteFilePath();
69 path = filePath;
70
71 if (fi.isDir()) {
72 if (options.testFlag(AddTopLevelFolder)) {
73 dataItems.insertMulti(fi.fileName(), filePath);
74 }
75 fillDataItems(fi.absoluteFilePath(), options.testFlag(AddRecursively));
76
77 } else if (fi.isFile()) {
78 if (!options.testFlag(AddOnlyFolders)) {
79 QString fileName = chopExtention(fi.fileName());
80 dataItems.insertMulti(fileName, filePath);
81 }
82 }
83
84 valid = true;
85 }
86
fillDataItems(const QDir & dir,bool recursive)87 void U2DataPath::fillDataItems(const QDir &dir, bool recursive) {
88 QFileInfoList infoList = dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot | QDir::Files);
89
90 foreach (const QFileInfo &fi, infoList) {
91 if (fi.isFile()) {
92 if (!options.testFlag(AddOnlyFolders)) {
93 QString fileName = chopExtention(fi.fileName());
94 QString filePath = fi.absoluteFilePath();
95
96 dataItems.insertMulti(fileName, filePath);
97 }
98 } else if (fi.isDir()) {
99 if (options.testFlag(AddOnlyFolders)) {
100 QString fileName = fi.fileName();
101 QString filePath = fi.absoluteFilePath();
102
103 dataItems.insertMulti(fileName, filePath);
104 }
105
106 if (recursive) {
107 fillDataItems(fi.absoluteFilePath(), recursive);
108 }
109 }
110 }
111 }
112
getName() const113 const QString &U2DataPath::getName() const {
114 return name;
115 }
116
getPath() const117 const QString &U2DataPath::getPath() const {
118 return path;
119 }
120
getDescription() const121 const QString &U2DataPath::getDescription() const {
122 return description;
123 }
124
getDataItems() const125 const QMap<QString, QString> &U2DataPath::getDataItems() const {
126 return dataItems;
127 }
128
getDataNames() const129 QList<QString> U2DataPath::getDataNames() const {
130 return dataItems.keys();
131 }
132
isValid() const133 bool U2DataPath::isValid() const {
134 return valid;
135 }
136
isFolders() const137 bool U2DataPath::isFolders() const {
138 return options.testFlag(AddOnlyFolders);
139 }
140
getDataItemsVariantMap() const141 QVariantMap U2DataPath::getDataItemsVariantMap() const {
142 QVariantMap vm;
143
144 foreach (const QString &key, dataItems.keys()) {
145 vm.insert(key, dataItems[key]);
146 }
147
148 return vm;
149 }
150
chopExtention(QString name)151 QString U2DataPath::chopExtention(QString name) {
152 CHECK(options.testFlag(CutFileExtension), name);
153 if (name.endsWith(".gz")) {
154 name.chop(3);
155 }
156 int dot = name.lastIndexOf('.');
157 if (dot > 0) {
158 name.chop(name.size() - dot);
159 }
160
161 return name;
162 }
163
164 ////////////////////////////////////////
165 // U2DataPathRegistry
~U2DataPathRegistry()166 U2DataPathRegistry::~U2DataPathRegistry() {
167 qDeleteAll(registry.values());
168 }
169
getDataPathByName(const QString & name)170 U2DataPath *U2DataPathRegistry::getDataPathByName(const QString &name) {
171 return registry.value(name, nullptr);
172 }
173
registerEntry(U2DataPath * dp)174 bool U2DataPathRegistry::registerEntry(U2DataPath *dp) {
175 if (registry.contains(dp->getName()) || !dp->isValid()) {
176 return false;
177 } else {
178 registry.insert(dp->getName(), dp);
179 }
180 return true;
181 }
182
unregisterEntry(const QString & name)183 void U2DataPathRegistry::unregisterEntry(const QString &name) {
184 CHECK(registry.contains(name), );
185 delete registry.take(name);
186 }
187
getAllEntries() const188 QList<U2DataPath *> U2DataPathRegistry::getAllEntries() const {
189 return registry.values();
190 }
191 } // namespace U2
192