1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25
26 #include "fileshareprotocol.h"
27 #include "fileshareprotocolsettingspage.h"
28
29 #include <coreplugin/icore.h>
30 #include <coreplugin/messagemanager.h>
31
32 #include <QXmlStreamReader>
33 #include <QXmlStreamAttribute>
34 #include <QTemporaryFile>
35 #include <QDir>
36 #include <QFileInfo>
37 #include <QDebug>
38
39 enum { debug = 0 };
40
41 static const char tempPatternC[] = "pasterXXXXXX.xml";
42 static const char tempGlobPatternC[] = "paster*.xml";
43 static const char pasterElementC[] = "paster";
44 static const char userElementC[] = "user";
45 static const char descriptionElementC[] = "description";
46 static const char textElementC[] = "text";
47
48 namespace CodePaster {
49
FileShareProtocol()50 FileShareProtocol::FileShareProtocol() :
51 m_settingsPage(new FileShareProtocolSettingsPage(&m_settings))
52 {
53 m_settings.readSettings(Core::ICore::settings());
54 }
55
~FileShareProtocol()56 FileShareProtocol::~FileShareProtocol()
57 {
58 delete m_settingsPage;
59 }
60
name() const61 QString FileShareProtocol::name() const
62 {
63 return m_settingsPage->displayName();
64 }
65
capabilities() const66 unsigned FileShareProtocol::capabilities() const
67 {
68 return ListCapability | PostDescriptionCapability | PostUserNameCapability;
69 }
70
hasSettings() const71 bool FileShareProtocol::hasSettings() const
72 {
73 return true;
74 }
75
settingsPage() const76 Core::IOptionsPage *FileShareProtocol::settingsPage() const
77 {
78 return m_settingsPage;
79 }
80
parse(const QString & fileName,QString * errorMessage,QString * user=nullptr,QString * description=nullptr,QString * text=nullptr)81 static bool parse(const QString &fileName,
82 QString *errorMessage,
83 QString *user = nullptr, QString *description = nullptr, QString *text = nullptr)
84 {
85 unsigned elementCount = 0;
86
87 errorMessage->clear();
88 if (user)
89 user->clear();
90 if (description)
91 description->clear();
92 if (text)
93 text->clear();
94
95 QFile file(fileName);
96 if (!file.open(QIODevice::ReadOnly|QIODevice::Text)) {
97 *errorMessage = FileShareProtocol::tr("Cannot open %1: %2").arg(fileName, file.errorString());
98 return false;
99 }
100 QXmlStreamReader reader(&file);
101 while (!reader.atEnd()) {
102 if (reader.readNext() == QXmlStreamReader::StartElement) {
103 const auto elementName = reader.name();
104 // Check start element
105 if (elementCount == 0 && elementName != QLatin1String(pasterElementC)) {
106 *errorMessage = FileShareProtocol::tr("%1 does not appear to be a paster file.").arg(fileName);
107 return false;
108 }
109 // Parse elements
110 elementCount++;
111 if (user && elementName == QLatin1String(userElementC))
112 *user = reader.readElementText();
113 else if (description && elementName == QLatin1String(descriptionElementC))
114 *description = reader.readElementText();
115 else if (text && elementName == QLatin1String(textElementC))
116 *text = reader.readElementText();
117 }
118 }
119 if (reader.hasError()) {
120 *errorMessage = FileShareProtocol::tr("Error in %1 at %2: %3")
121 .arg(fileName).arg(reader.lineNumber()).arg(reader.errorString());
122 return false;
123 }
124 return true;
125 }
126
checkConfiguration(QString * errorMessage)127 bool FileShareProtocol::checkConfiguration(QString *errorMessage)
128 {
129 if (m_settings.path.value().isEmpty()) {
130 if (errorMessage)
131 *errorMessage = tr("Please configure a path.");
132 return false;
133 }
134 return true;
135 }
136
fetch(const QString & id)137 void FileShareProtocol::fetch(const QString &id)
138 {
139 // Absolute or relative path name.
140 QFileInfo fi(id);
141 if (fi.isRelative())
142 fi = QFileInfo(m_settings.path.value() + '/' + id);
143 QString errorMessage;
144 QString text;
145 if (parse(fi.absoluteFilePath(), &errorMessage, nullptr, nullptr, &text))
146 emit fetchDone(id, text, false);
147 else
148 emit fetchDone(id, errorMessage, true);
149 }
150
list()151 void FileShareProtocol::list()
152 {
153 // Read out directory, display by date (latest first)
154 QDir dir(m_settings.path.value(), tempGlobPatternC,
155 QDir::Time, QDir::Files|QDir::NoDotAndDotDot|QDir::Readable);
156 QStringList entries;
157 QString user;
158 QString description;
159 QString errorMessage;
160 const QChar blank = QLatin1Char(' ');
161 const QFileInfoList entryInfoList = dir.entryInfoList();
162 const int count = qMin(int(m_settings.displayCount.value()), entryInfoList.size());
163 for (int i = 0; i < count; i++) {
164 const QFileInfo& entryFi = entryInfoList.at(i);
165 if (parse(entryFi.absoluteFilePath(), &errorMessage, &user, &description)) {
166 QString entry = entryFi.fileName();
167 entry += blank;
168 entry += user;
169 entry += blank;
170 entry += description;
171 entries.push_back(entry);
172 }
173 if (debug)
174 qDebug() << entryFi.absoluteFilePath() << errorMessage;
175 }
176 emit listDone(name(), entries);
177 }
178
paste(const QString & text,ContentType,int,bool,const QString & username,const QString &,const QString & description)179 void FileShareProtocol::paste(
180 const QString &text,
181 ContentType /* ct */,
182 int /* expiryDays */,
183 bool /* publicPaste */,
184 const QString &username,
185 const QString & /* comment */,
186 const QString &description
187 )
188 {
189 // Write out temp XML file
190 Utils::TempFileSaver saver(m_settings.path.value() + '/' + tempPatternC);
191 saver.setAutoRemove(false);
192 if (!saver.hasError()) {
193 // Flat text sections embedded into pasterElement
194 QXmlStreamWriter writer(saver.file());
195 writer.writeStartDocument();
196 writer.writeStartElement(QLatin1String(pasterElementC));
197
198 writer.writeTextElement(QLatin1String(userElementC), username);
199 writer.writeTextElement(QLatin1String(descriptionElementC), description);
200 writer.writeTextElement(QLatin1String(textElementC), text);
201
202 writer.writeEndElement();
203 writer.writeEndDocument();
204
205 saver.setResult(&writer);
206 }
207 if (!saver.finalize()) {
208 Core::MessageManager::writeDisrupting(saver.errorString());
209 return;
210 }
211
212 Core::MessageManager::writeSilently(tr("Pasted: %1").arg(saver.filePath().toUserOutput()));
213 }
214 } // namespace CodePaster
215