1/*
2 SPDX-FileCopyrightText: 2014 Gregory Oestreicher <greg@kamago.net>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "davjobbase.h"
8#include "davjobbase_p.h"
9
10#include "daverror.h"
11
12using namespace KDAV;
13
14DavJobBase::DavJobBase(QObject *parent)
15 : KJob(parent)
16 , d_ptr(new DavJobBasePrivate())
17{
18 d_ptr->q_ptr = this;
19}
20
21DavJobBase::DavJobBase(DavJobBasePrivate *dd, QObject *parent)
22 : KJob(parent)
23 , d_ptr(dd)
24{
25 d_ptr->q_ptr = this;
26}
27
28DavJobBase::~DavJobBase() = default;
29
30int DavJobBase::latestResponseCode() const
31{
32 return d_ptr->mLatestResponseCode;
33}
34
35bool DavJobBase::canRetryLater() const
36{
37 bool ret = false;
38
39 // Be explicit and readable by splitting the if/else if clauses
40
41 if (latestResponseCode() == 0 && error()) {
42 // Likely a timeout or a connection failure.
43 ret = true;
44 } else if (latestResponseCode() == 401) {
45 // Authentication required
46 ret = true;
47 } else if (latestResponseCode() == 402) {
48 // Payment required
49 ret = true;
50 } else if (latestResponseCode() == 407) {
51 // Proxy authentication required
52 ret = true;
53 } else if (latestResponseCode() == 408) {
54 // Request timeout
55 ret = true;
56 } else if (latestResponseCode() == 423) {
57 // Locked
58 ret = true;
59 } else if (latestResponseCode() == 429) {
60 // Too many requests
61 ret = true;
62 } else if (latestResponseCode() >= 501 && latestResponseCode() <= 504) {
63 // Various server-side errors
64 ret = true;
65 } else if (latestResponseCode() == 507) {
66 // Insufficient storage
67 ret = true;
68 } else if (latestResponseCode() == 511) {
69 // Network authentication required
70 ret = true;
71 }
72
73 return ret;
74}
75
76bool DavJobBase::hasConflict() const
77{
78 return latestResponseCode() == 412;
79}
80
81void DavJobBasePrivate::setLatestResponseCode(int code)
82{
83 mLatestResponseCode = code;
84}
85
86Error DavJobBase::davError() const
87{
88 return Error(static_cast<KDAV::ErrorNumber>(error()), d_ptr->mLatestResponseCode, d_ptr->mInternalErrorText, d_ptr->mJobErrorCode);
89}
90
91void DavJobBasePrivate::setJobErrorText(const QString &errorText)
92{
93 mInternalErrorText = errorText;
94}
95
96void DavJobBasePrivate::setJobError(int jobErrorCode)
97{
98 mJobErrorCode = jobErrorCode;
99}
100
101void DavJobBasePrivate::setErrorTextFromDavError()
102{
103 q_ptr->setErrorText(q_ptr->davError().errorText());
104}
105
106void DavJobBasePrivate::setDavError(const Error &error)
107{
108 q_ptr->setError(error.errorNumber());
109 setLatestResponseCode(error.responseCode());
110 setJobErrorText(error.internalErrorText());
111 setJobError(error.jobErrorCode());
112}
113
114void DavJobBasePrivate::setError(int errorCode)
115{
116 q_ptr->setError(errorCode);
117}
118
119void DavJobBasePrivate::setErrorText(const QString &errorText)
120{
121 q_ptr->setErrorText(errorText);
122}
123
124void DavJobBasePrivate::emitResult()
125{
126 q_ptr->emitResult();
127}
128
129#include "moc_davjobbase.cpp"
130

source code of kdav/src/common/davjobbase.cpp