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 #ifndef _U2_DOCUMENT_IMPORT_H_
23 #define _U2_DOCUMENT_IMPORT_H_
24 
25 #include <QDialog>
26 
27 #include <U2Core/DocumentModel.h>
28 
29 namespace U2 {
30 
31 class DocumentImporter;
32 class DocumentProviderTask;
33 class FormatDetectionResult;
34 class ImportWidget;
35 
36 class U2CORE_EXPORT ImportDialog : public QDialog {
37     Q_OBJECT
38 public:
ImportDialog(const QVariantMap & _settings)39     ImportDialog(const QVariantMap &_settings)
40         : settings(_settings) {
41     }
42 
getSettings()43     const QVariantMap &getSettings() const {
44         return settings;
45     }
46 
47 public slots:
48     virtual void accept();
49 
50 protected:
isValid()51     virtual bool isValid() {
52         return true;
53     }  // it is not const method: derived class can do something non-const
54     virtual void applySettings() = 0;
55 
56     QVariantMap settings;
57 };
58 
59 class ImportWidgetFactory {
60 public:
~ImportWidgetFactory()61     virtual ~ImportWidgetFactory() {
62     }
63 
64     virtual ImportWidget *getWidget(const GUrl &url, const QVariantMap &settings) const = 0;
65 };
66 
67 /** Registry for all DocumentImportHandlers */
68 class U2CORE_EXPORT DocumentImportersRegistry : public QObject {
69     Q_OBJECT
Q_DISABLE_COPY(DocumentImportersRegistry)70     Q_DISABLE_COPY(DocumentImportersRegistry)
71 public:
72     DocumentImportersRegistry(QObject *p = nullptr)
73         : QObject(p) {
74     }
75     ~DocumentImportersRegistry();
76 
77     /** returns handler by its id */
78     DocumentImporter *getDocumentImporter(const QString &importerId) const;
79 
80     /** registers new document import handler */
81     void addDocumentImporter(DocumentImporter *i);
82 
getImporters()83     const QList<DocumentImporter *> &getImporters() const {
84         return importers;
85     }
86 
87 private:
88     QList<DocumentImporter *> importers;
89 };
90 
91 class U2CORE_EXPORT DocumentImporter : public QObject {
92     Q_OBJECT
Q_DISABLE_COPY(DocumentImporter)93     Q_DISABLE_COPY(DocumentImporter)
94 public:
95     DocumentImporter(const QString &_id, const QString &_name, QObject *o = nullptr)
96         : QObject(o), id(_id), name(_name), widgetFactory(nullptr) {
97     }
98 
~DocumentImporter()99     virtual ~DocumentImporter() {
100         delete widgetFactory;
101     }
102 
103     virtual FormatCheckResult checkRawData(const QByteArray &rawData, const GUrl &url) = 0;
104 
105     virtual DocumentProviderTask *createImportTask(const FormatDetectionResult &res, bool showGui, const QVariantMap &hints) = 0;
106 
107     virtual QString getRadioButtonText() const;
108 
109     virtual ImportWidget *createImportWidget(const GUrl &url, const QVariantMap &settings) const;
110 
getImporterDescription()111     virtual QString getImporterDescription() const {
112         return importerDescription;
113     }
114 
getImporterName()115     const QString &getImporterName() const {
116         return name;
117     }
118 
getId()119     const QString &getId() const {
120         return id;
121     }
122 
getFormatIds()123     const QStringList &getFormatIds() const {
124         return formatIds;
125     }
126 
getSupportedFileExtensions()127     const QList<QString> &getSupportedFileExtensions() const {
128         return extensions;
129     }
130 
131     void setWidgetFactory(ImportWidgetFactory *factory);
132 
133     const QSet<GObjectType> &getSupportedObjectTypes() const;
134 
135     // NOTE: it is hint, it should be true by default
136     static const QString LOAD_RESULT_DOCUMENT;
137 
138 protected:
139     QString id;
140     QString name;
141     QStringList formatIds;
142     QList<QString> extensions;
143     QString importerDescription;
144     ImportWidgetFactory *widgetFactory;
145     QSet<GObjectType> supportedObjectTypes;
146 };
147 
148 }  // namespace U2
149 
150 #endif
151