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_CONVERTFILETASK_H_
23 #define _U2_CONVERTFILETASK_H_
24 
25 #include <U2Core/GUrl.h>
26 #include <U2Core/Task.h>
27 
28 namespace U2 {
29 
30 class LoadDocumentTask;
31 class SaveDocumentTask;
32 
33 //
34 class U2FORMATS_EXPORT ConvertFileTask : public Task {
35     Q_OBJECT
36 public:
37     ConvertFileTask(const GUrl &sourceURL, const QString &detectedFormat, const QString &targetFormat, const QString &dir);
38 
39     GUrl getSourceURL() const;
40     QString getResult() const;
41 
42     void run();
43 
44 protected:
45     GUrl sourceURL;
46     QString detectedFormat;
47     QString targetFormat;
48     QString workingDir;
49     QString targetUrl;
50 };  // ConvertFileTask
51 
52 // use this task for default conversions
53 class U2FORMATS_EXPORT DefaultConvertFileTask : public ConvertFileTask {
54     Q_OBJECT
55 public:
56     DefaultConvertFileTask(const GUrl &sourceUrl, const QString &detectedFormat, const QString &targetFormat, const QString &dir);
57     DefaultConvertFileTask(const GUrl &sourceUrl, const QString &detectedFormat, const QString &targetUrl, const QString &targetFormat, const QString &dir);
58 
59 private:
60     void prepare();
61     QList<Task *> onSubTaskFinished(Task *subTask);
62 
63     LoadDocumentTask *loadTask;
64     SaveDocumentTask *saveTask;
65 
66 };  // DefaultConvertFileTask
67 
68 // SAM->BAM creates a sorted and indexed BAM
69 class U2FORMATS_EXPORT BamSamConversionTask : public ConvertFileTask {
70 public:
71     BamSamConversionTask(const GUrl &sourceURL, const QString &detectedFormat, const QString &targetFormat, const QString &dir);
72 
73 protected:
74     void prepare();
75     void run();
76 
77 private:
78     bool samToBam;
79 };  // BamSamConversionTask
80 
81 //////////////////////////////////////////////////////////////////////////
82 // Factories and registries
83 class U2FORMATS_EXPORT ConvertFileFactory : public QObject {
84 public:
85     // return true if it is a custom conversion for given formats
86     virtual bool isCustomFormatTask(const QString &detectedFormat, const QString &targetFormat);
getTask(const GUrl & sourceURL,const QString & detectedFormat,const QString & targetFormat,const QString & dir)87     virtual ConvertFileTask *getTask(const GUrl &sourceURL, const QString &detectedFormat, const QString &targetFormat, const QString &dir) {
88         return new DefaultConvertFileTask(sourceURL, detectedFormat, targetFormat, dir);
89     }
90 };
91 
92 class U2FORMATS_EXPORT BAMConvertFactory : public ConvertFileFactory {
93 public:
94     virtual bool isCustomFormatTask(const QString &detectedFormat, const QString &targetFormat);
getTask(const GUrl & sourceURL,const QString & detectedFormat,const QString & targetFormat,const QString & dir)95     virtual ConvertFileTask *getTask(const GUrl &sourceURL, const QString &detectedFormat, const QString &targetFormat, const QString &dir) {
96         return new BamSamConversionTask(sourceURL, detectedFormat, targetFormat, dir);
97     }
98 };
99 
100 class U2FORMATS_EXPORT ConvertFactoryRegistry : public QObject {
101 public:
102     ConvertFactoryRegistry(QObject *o = 0);
103     ~ConvertFactoryRegistry();
104     bool registerConvertFactory(ConvertFileFactory *f);
105     void unregisterConvertFactory(ConvertFileFactory *f);
106     ConvertFileFactory *getFactoryByFormats(const QString &detectedFormat, const QString &targetFormat);
107 
108 private:
109     QList<ConvertFileFactory *> factories;
110 };
111 
112 }  // namespace U2
113 
114 #endif  // _U2_CONVERTFILETASK_H_
115