1/*
2 This file is part of KDE.
3
4 Copyright (c) 2005 Cornelius Schumacher <schumacher@kde.org>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
20*/
21/**
22 @file
23 This file is part of the API for handling calendar data and
24 defines the CalendarLocal class.
25
26 @brief
27 Classes to represents the result of an operation. It's meant to be used
28 as return value of functions for returning status and especially error
29 information.
30
31 @author Cornelius Schumacher \<schumacher@kde.org\>
32*/
33
34#include "kresult.h"
35
36#include <klocalizedstring.h>
37#include <kdebug.h>
38
39using namespace KCal;
40
41/**
42 Private class that helps to provide binary compatibility between releases.
43 @internal
44*/
45//@cond PRIVATE
46class KCal::KResult::Private
47{
48 public:
49 Private()
50 : mType( Ok ),
51 mErrorType( NotAnError ),
52 mChainedResult( 0 )
53 {
54 }
55
56 Private( Type type )
57 : mType( type ),
58 mChainedResult( 0 )
59 {
60 if ( mType == Error ) {
61 mErrorType = Undefined;
62 } else {
63 mErrorType = NotAnError;
64 }
65 }
66
67 Private( ErrorType error, const QString &details )
68 : mType( Error ),
69 mErrorType( error ),
70 mDetails( details ),
71 mChainedResult( 0 )
72 {
73 }
74
75 Type mType;
76 ErrorType mErrorType;
77 QString mDetails;
78 KResult *mChainedResult;
79};
80//@endcond
81
82KResult::KResult()
83 : d( new KCal::KResult::Private )
84{
85}
86
87KResult::KResult( Type type )
88 : d( new KCal::KResult::Private( type ) )
89{
90}
91
92KResult::KResult( ErrorType error, const QString &details )
93 : d( new KCal::KResult::Private( error, details ) )
94{
95}
96
97KResult::~KResult()
98{
99 delete d->mChainedResult;
100 delete d;
101}
102
103KResult::KResult( const KResult &o ) : d( new KCal::KResult::Private )
104{
105 d->mType = o.d->mType;
106 d->mErrorType = o.d->mErrorType;
107 d->mDetails = o.d->mDetails;
108 if ( o.d->mChainedResult ) {
109 d->mChainedResult = new KResult( *o.d->mChainedResult );
110 } else {
111 d->mChainedResult = 0;
112 }
113}
114
115KResult::operator bool() const
116{
117 return !isError();
118}
119
120bool KResult::isOk() const
121{
122 return d->mType == Ok;
123}
124
125bool KResult::isInProgress() const
126{
127 return d->mType == InProgress;
128}
129
130bool KResult::isError() const
131{
132 return d->mType == Error;
133}
134
135KResult::ErrorType KResult::error() const
136{
137 return d->mErrorType;
138}
139
140QString KResult::message() const
141{
142 switch ( d->mType ) {
143 case Ok:
144 return i18n( "OK" );
145 case InProgress:
146 return i18n( "In progress" );
147 case Error:
148 switch ( d->mErrorType ) {
149 case NotAnError:
150 return i18n( "Not an error" );
151 case Undefined:
152 return i18n( "Error" );
153 case InvalidUrl:
154 return i18n( "Invalid URL" );
155 case ConnectionFailed:
156 return i18n( "Connection failed" );
157 case WriteError:
158 return i18n( "Write error" );
159 case ReadError:
160 return i18n( "Read error" );
161 case WrongParameter:
162 return i18n( "Wrong Parameter" );
163 case ParseError:
164 return i18n( "Parse Error" );
165 case WrongSchemaRevision:
166 return i18n( "Wrong revision of schema" );
167 }
168 }
169
170 kError() << "Unhandled case";
171 return QString();
172}
173
174void KResult::setDetails( const QString &details )
175{
176 d->mDetails = details;
177}
178
179QString KResult::details() const
180{
181 return d->mDetails;
182}
183
184KResult &KResult::chain( const KResult &result )
185{
186 d->mChainedResult = new KResult( result );
187 return *this;
188}
189
190bool KResult::hasChainedResult() const
191{
192 return d->mChainedResult;
193}
194
195KResult KResult::chainedResult() const
196{
197 return *d->mChainedResult;
198}
199
200QString KResult::fullMessage() const
201{
202 QString msg = message();
203 if ( !details().isEmpty() ) {
204 msg += ": " + details();
205 }
206 return msg;
207}
208
209QString KResult::chainedMessage() const
210{
211 QString msg = fullMessage();
212 if ( hasChainedResult() ) {
213 msg += '\n' + chainedResult().chainedMessage();
214 }
215 return msg;
216}
217