Debian Codegraph @

src/gui/operations/HandleUploadResultOperation.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2019 Damir Porobic <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #include "HandleUploadResultOperation.h"
21
22 HandleUploadResultOperation::HandleUploadResultOperation(
23 const UploadResult &result,
24 const QSharedPointer<INotificationService> &notificationService,
25 const QSharedPointer<IClipboard> &clipboard,
26 const QSharedPointer<IDesktopService> &desktopService,
27 const QSharedPointer<IConfig> &config) :
28 mUploadResult(result),
29 mNotificationService(notificationService),
30 mConfig(config),
31 mClipboardService(clipboard),
32 mDesktopService(desktopService)
33 {
34 }
35
36 bool HandleUploadResultOperation::execute()
37 {
38 switch (mUploadResult.type) {
39 case UploaderType::Imgur:
40 handleImgurResult();
41 break;
42 case UploaderType::Script:
43 handleScriptResult();
44 break;
45 case UploaderType::Ftp:
46 handleFtpResult();
47 break;
48 }
49
50 return mUploadResult.status == UploadStatus::NoError;
51 }
52
53 void HandleUploadResultOperation::handleImgurResult()
54 {
55 if(mUploadResult.status == UploadStatus::NoError) {
56 if (mConfig->imgurOpenLinkInBrowser()) {
57 OpenUrl(mUploadResult.content);
58 }
59
60 if (mConfig->imgurAlwaysCopyToClipboard()) {
61 copyToClipboard(mUploadResult.content);
62 }
63
64 notifyImgurSuccessfulUpload(mUploadResult.content);
65 } else {
66 handleUploadError();
67 }
68 }
69
70 void HandleUploadResultOperation::handleScriptResult()
71 {
72 if(mUploadResult.status == UploadStatus::NoError) {
73 if (mConfig->uploadScriptCopyOutputToClipboard()) {
74 copyToClipboard(mUploadResult.content);
75 }
76
77 notifyScriptSuccessfulUpload();
78 } else {
79 handleUploadError();
80 }
81 }
82
83 void HandleUploadResultOperation::handleFtpResult()
84 {
85 if(mUploadResult.status == UploadStatus::NoError) {
86 notifyFtpSuccessfulUpload();
87 } else {
88 handleUploadError();
89 }
90 }
91
92 void HandleUploadResultOperation::notifyFtpSuccessfulUpload() const
93 {
94 NotifyOperation operation(tr("Upload Successful"), tr("FTP Upload finished successfully."), NotificationTypes::Information, mNotificationService, mConfig);
95 operation.execute();
96 }
97
98 void HandleUploadResultOperation::notifyScriptSuccessfulUpload() const
99 {
100 NotifyOperation operation(tr("Upload Successful"), tr("Upload script %1 finished successfully.").arg(mConfig->uploadScriptPath()),
101 NotificationTypes::Information, mNotificationService, mConfig);
102 operation.execute();
103 }
104
105 void HandleUploadResultOperation::notifyImgurSuccessfulUpload(const QString &url) const
106 {
107 NotifyOperation operation(tr("Upload Successful"), tr("Uploaded to %1").arg(url), url, NotificationTypes::Information,
108 mNotificationService, mConfig);
109 operation.execute();
110 }
111
112 void HandleUploadResultOperation::copyToClipboard(const QString &url) const
113 {
114 mClipboardService->setText(url);
115 }
116
117 void HandleUploadResultOperation::OpenUrl(const QString &url) const
118 {
119 mDesktopService->openUrl(url);
120 }
121
122 void HandleUploadResultOperation::handleUploadError()
123 {
124 switch (mUploadResult.status) {
125 case UploadStatus::NoError:
126 // Nothing to report all good
127 break;
128 case UploadStatus::UnableToSaveTemporaryImage:
129 notifyFailedUpload(tr("Unable to save temporary image for upload."));
130 break;
131 case UploadStatus::FailedToStart:
132 notifyFailedUpload(tr("Unable to start process, check path and permissions."));
133 break;
134 case UploadStatus::Crashed:
135 notifyFailedUpload(tr("Process crashed"));
136 break;
137 case UploadStatus::TimedOut:
138 notifyFailedUpload(tr("Process timed out."));
139 break;
140 case UploadStatus::ReadError:
141 notifyFailedUpload(tr("Process read error."));
142 break;
143 case UploadStatus::WriteError:
144 notifyFailedUpload(tr("Process write error."));
145 break;
146 case UploadStatus::WebError:
147 notifyFailedUpload(tr("Web error, check console output."));
148 break;
149 case UploadStatus::UnknownError:
150 notifyFailedUpload(tr("Unknown error."));
151 break;
152 case UploadStatus::ScriptWroteToStdErr:
153 notifyFailedUpload(tr("Script wrote to StdErr."));
154 break;
155 case UploadStatus::ConnectionError:
156 notifyFailedUpload(tr("Connection Error."));
157 break;
158 case UploadStatus::PermissionError:
159 notifyFailedUpload(tr("Permission Error."));
160 break;
161 }
162 }
163
164 void HandleUploadResultOperation::notifyFailedUpload(const QString &message) const
165 {
166 NotifyOperation operation(tr("Upload Failed"), message, NotificationTypes::Warning, mNotificationService, mConfig);
167 operation.execute();
168 }
169