1 /* ============================================================
2 *
3 * This file is a part of KDE project
4 *
5 *
6 * Date : 2013-11-18
7 * Description : a kipi plugin to export images to Google-Drive web service
8 *
9 * Copyright (C) 2013 by Pankaj Kumar <me at panks dot me>
10 *
11 * This program is free software; you can redistribute it
12 * and/or modify it under the terms of the GNU General
13 * Public License as published by the Free Software Foundation;
14 * either version 2, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * ============================================================ */
22
23 #include "mpform_gdrive.h"
24
25 // Qt includes
26
27 #include <QJsonDocument>
28 #include <QJsonParseError>
29 #include <QJsonObject>
30 #include <QJsonValue>
31 #include <QJsonArray>
32 #include <QUrl>
33 #include <QFile>
34 #include <QMimeDatabase>
35 #include <QMimeType>
36 #include <QTime>
37
38 // local includes
39
40 #include "kipiplugins_debug.h"
41 #include "kputil.h"
42
43 namespace KIPIGoogleServicesPlugin
44 {
45
MPForm_GDrive()46 MPForm_GDrive::MPForm_GDrive()
47 : m_boundary(KIPIPlugins::KPRandomGenerator::randomString(42 + 13).toLatin1())
48 {
49 reset();
50 }
51
~MPForm_GDrive()52 MPForm_GDrive::~MPForm_GDrive()
53 {
54 }
55
reset()56 void MPForm_GDrive::reset()
57 {
58 m_buffer.resize(0);
59 }
60
finish()61 void MPForm_GDrive::finish()
62 {
63 qCDebug(KIPIPLUGINS_LOG) << "in finish";
64 QByteArray str;
65 str += "--";
66 str += m_boundary;
67 str += "--";
68 m_buffer.append(str);
69 qCDebug(KIPIPLUGINS_LOG) << "finish:" << m_buffer;
70 }
71
addPair(const QString & name,const QString & description,const QString & path,const QString & id)72 void MPForm_GDrive::addPair(const QString& name, const QString& description, const QString& path,const QString& id)
73 {
74 QMimeDatabase db;
75 QMimeType ptr = db.mimeTypeForUrl(QUrl::fromLocalFile(path));
76 QString mime = ptr.name();
77 qCDebug(KIPIPLUGINS_LOG) << "in add pair:" << name << " " << description << " " << path << " " << id << " " << mime;
78
79 // Generate JSON
80 QJsonObject photoInfo;
81 photoInfo.insert(QString::fromLatin1("title"), QJsonValue(name));
82 photoInfo.insert(QString::fromLatin1("description"), QJsonValue(description));
83 photoInfo.insert(QString::fromLatin1("mimeType"), QJsonValue(mime));
84
85 QVariantMap parentId;
86 parentId.insert(QString::fromLatin1("id"), id);
87 QVariantList parents;
88 parents << parentId;
89 photoInfo.insert(QString::fromLatin1("parents"), QJsonValue(QJsonArray::fromVariantList(parents)));
90
91 QJsonDocument doc(photoInfo);
92 QByteArray json = doc.toJson();
93
94 // Append to the multipart
95 QByteArray str;
96 str += "--";
97 str += m_boundary;
98 str += "\r\n";
99 str += "Content-Type:application/json; charset=UTF-8\r\n\r\n";
100 str += json;
101 str += "\r\n";
102 m_buffer.append(str);
103 }
104
addFile(const QString & path)105 bool MPForm_GDrive::addFile(const QString& path)
106 {
107 QByteArray str;
108 qCDebug(KIPIPLUGINS_LOG) << "in addfile" << path;
109
110 QMimeDatabase db;
111 QMimeType ptr = db.mimeTypeForUrl(QUrl::fromLocalFile(path));
112 QString mime = ptr.name();
113 str += "--";
114 str += m_boundary;
115 str += "\r\n";
116 str += "Content-Type: ";
117 str += mime.toLatin1();
118 str += "\r\n\r\n";
119
120 QFile imageFile(path);
121
122 if (!imageFile.open(QIODevice::ReadOnly))
123 {
124 return false;
125 }
126
127 QByteArray imageData = imageFile.readAll();
128 m_file_size = QString::number(imageFile.size());
129
130 imageFile.close();
131
132 m_buffer.append(str);
133 m_buffer.append(imageData);
134 m_buffer.append("\r\n");
135
136 return true;
137 }
138
formData() const139 QByteArray MPForm_GDrive::formData() const
140 {
141 return m_buffer;
142 }
143
boundary() const144 QString MPForm_GDrive::boundary() const
145 {
146 return QString::fromLatin1(m_boundary);
147 }
148
contentType() const149 QString MPForm_GDrive::contentType() const
150 {
151 return QString::fromLatin1("multipart/related;boundary=") + QString::fromLatin1(m_boundary);
152 }
153
getFileSize() const154 QString MPForm_GDrive::getFileSize() const
155 {
156 return m_file_size;
157 }
158
159 } // namespace KIPIGoogleServicesPlugin
160