gdrivehelper.cpp 5.65 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
 * Copyright (C) 2014 Daniel Vrátil <dvratil@redhat.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#include "gdrivehelper.h"

22
#include <KIO/Job>
Elvis Angelaccio's avatar
Elvis Angelaccio committed
23
#include <KGAPI/Drive/File>
24
#include <KLocalizedString>
25
26
27

using namespace KGAPI2::Drive;

28
29
30
31
32
33
34
35
36
37
38
#define VND_GOOGLE_APPS_DOCUMENT        QStringLiteral("application/vnd.google-apps.document")
#define VND_GOOGLE_APPS_DRAWING         QStringLiteral("application/vnd.google-apps.drawing")
#define VND_GOOGLE_APPS_FILE            QStringLiteral("application/vnd.google-apps.file")
#define VND_GOOGLE_APPS_FORM            QStringLiteral("application/vnd.google-apps.form")
#define VND_GOOGLE_APPS_PRESENTATION    QStringLiteral("application/vnd.google-apps.presentation")
#define VND_GOOGLE_APPS_SPREADSHEET     QStringLiteral("application/vnd.google-apps.spreadsheet")
#define VND_GOOGLE_APPS_FOLDER          QStringLiteral("application/vnd.google-apps.folder")

#define VND_OASIS_OPENDOCUMENT_TEXT     QStringLiteral("application/vnd.oasis.opendocument.text")
#define VND_OASIS_OPENDOCUMENT_PRESENTATION QStringLiteral("application/vnd.oasis.opendocument.presentation")
#define VND_OASIS_OPENDOCUMENT_SPREADSHEED QStringLiteral("application/x-vnd.oasis.opendocument.spreadsheet")
39
40

#define VND_OPENXMLFORMATS_OFFICEDOCUMENT_WORDPROCESSINGML_DOCUMENT \
41
            QStringLiteral("application/vnd.openxmlformats-officedocument.wordprocessingml.document")
42
#define VND_OPENXMLFORMATS_OFFICEDOCUMENT_PRESENTATIONML_PRESENTATION \
43
            QStringLiteral("application/vnd.openxmlformats-officedocument.presentationml.presentation")
44
#define VND_OPENXMLFORMATS_OFFICEDOCUMENT_SPREADSHEETML_SHEET \
45
            QStringLiteral("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
46

47
48
49
#define IMAGE_PNG                       QStringLiteral("image/png")
#define IMAGE_JPEG                      QStringLiteral("image/jpeg")
#define APPLICATION_PDF                 QStringLiteral("application/pdf")
50
51
52
53
54

namespace GDriveHelper {

static const QMap<QString /* mimetype */, QString /* .ext */> ExtensionsMap = []() {
    QMap<QString, QString> ext;
55
56
57
58
59
60
61
62
63
    ext[VND_OASIS_OPENDOCUMENT_TEXT] = QStringLiteral(".odt");
    ext[VND_OASIS_OPENDOCUMENT_SPREADSHEED] = QStringLiteral(".ods");
    ext[VND_OASIS_OPENDOCUMENT_PRESENTATION] = QStringLiteral(".odp");
    ext[VND_OPENXMLFORMATS_OFFICEDOCUMENT_WORDPROCESSINGML_DOCUMENT] = QStringLiteral(".docx");
    ext[VND_OPENXMLFORMATS_OFFICEDOCUMENT_SPREADSHEETML_SHEET] = QStringLiteral(".xlsx");
    ext[VND_OPENXMLFORMATS_OFFICEDOCUMENT_PRESENTATIONML_PRESENTATION] = QStringLiteral(".pptx");
    ext[IMAGE_PNG] = QStringLiteral(".png");
    ext[IMAGE_JPEG] = QStringLiteral(".jpg");
    ext[APPLICATION_PDF] = QStringLiteral(".pdf");
64
65
66
67
68
69
    return ext;
}();

static const QMap<QString /* mimetype */, QStringList /* target mimetypes */ > ConversionMap = []() {
    QMap<QString, QStringList> map;

Elvis Angelaccio's avatar
Elvis Angelaccio committed
70
    map[VND_GOOGLE_APPS_DOCUMENT] = QStringList {
71
72
73
74
75
            VND_OASIS_OPENDOCUMENT_TEXT,
            VND_OPENXMLFORMATS_OFFICEDOCUMENT_WORDPROCESSINGML_DOCUMENT,
            APPLICATION_PDF
    };

Elvis Angelaccio's avatar
Elvis Angelaccio committed
76
    map[VND_GOOGLE_APPS_DRAWING] = QStringList {
77
78
79
80
81
            IMAGE_PNG,
            IMAGE_JPEG,
            APPLICATION_PDF
    };

Elvis Angelaccio's avatar
Elvis Angelaccio committed
82
    map[VND_GOOGLE_APPS_PRESENTATION] = QStringList {
83
84
85
86
87
            VND_OASIS_OPENDOCUMENT_PRESENTATION,
            VND_OPENXMLFORMATS_OFFICEDOCUMENT_PRESENTATIONML_PRESENTATION,
            APPLICATION_PDF
    };

Elvis Angelaccio's avatar
Elvis Angelaccio committed
88
    map[VND_GOOGLE_APPS_SPREADSHEET] = QStringList {
89
90
91
92
93
94
95
96
97
98
            VND_OASIS_OPENDOCUMENT_SPREADSHEED,
            VND_OPENXMLFORMATS_OFFICEDOCUMENT_SPREADSHEETML_SHEET,
            APPLICATION_PDF
    };

    return map;
}();

}

99
QString GDriveHelper::folderMimeType()
100
101
102
103
{
    return VND_GOOGLE_APPS_FOLDER;
}

104
105
106
107
108
109
110
111
112
113
114
115
116
bool GDriveHelper::isGDocsDocument(const KGAPI2::Drive::FilePtr &file)
{
    return GDriveHelper::ConversionMap.contains(file->mimeType());
}

QUrl GDriveHelper::convertFromGDocs(KGAPI2::Drive::FilePtr &file)
{
    const QString originalMimeType = file->mimeType();
    if (!GDriveHelper::ConversionMap.contains(originalMimeType)) {
        return file->downloadUrl();
    }

    const auto exportLinks = file->exportLinks();
117
    Q_FOREACH (const QString &targetMimeType, GDriveHelper::ConversionMap[originalMimeType]) {
118
119
120
121
122
123
124
125
126
127
128
129
        if (exportLinks.contains(targetMimeType)) {
            file->setMimeType(targetMimeType);
            file->setTitle(file->title() + GDriveHelper::ExtensionsMap[targetMimeType]);
            return exportLinks[targetMimeType];
        }
    }

    // If we got here that means that the file cannot be converted to any meaningful
    // type, so we do nothing
    return file->downloadUrl();
}

Elvis Angelaccio's avatar
Elvis Angelaccio committed
130
131
// Currently unused, see https://phabricator.kde.org/T3443
/*
132
133
134
KIO::UDSEntry GDriveHelper::trash()
{
    KIO::UDSEntry trashEntry;
135
    trashEntry.insert(KIO::UDSEntry::UDS_NAME, QStringLiteral("trash"));
136
137
    trashEntry.insert(KIO::UDSEntry::UDS_DISPLAY_NAME, i18n("Trash"));
    trashEntry.insert(KIO::UDSEntry::UDS_FILE_TYPE, S_IFDIR);
138
    trashEntry.insert(KIO::UDSEntry::UDS_ICON_NAME, QStringLiteral("user-trash"));
139
140
    return trashEntry;
}
Elvis Angelaccio's avatar
Elvis Angelaccio committed
141
*/
142