~ubuntu-branches/ubuntu/hoary/kdemultimedia/hoary

« back to all changes in this revision

Viewing changes to noatun/noatun/modules/tron/ksaver.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Martin Schulze
  • Date: 2003-01-22 15:00:51 UTC
  • Revision ID: james.westby@ubuntu.com-20030122150051-uihwkdoxf15mi1tn
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// ksaver.cpp
 
2
//
 
3
// Copyright (C) 2001 Neil Stevens <multivac@fcmail.com>
 
4
//
 
5
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
6
// of this software and associated documentation files (the "Software"), to deal
 
7
// in the Software without restriction, including without limitation the rights
 
8
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
9
// copies of the Software, and to permit persons to whom the Software is
 
10
// furnished to do so, subject to the following conditions:
 
11
// 
 
12
// The above copyright notice and this permission notice shall be included in
 
13
// all copies or substantial portions of the Software.
 
14
// 
 
15
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
16
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
17
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 
18
// THE AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 
19
// AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 
20
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
21
// 
 
22
// Except as contained in this notice, the name(s) of the author(s) shall not be
 
23
// used in advertising or otherwise to promote the sale, use or other dealings
 
24
// in this Software without prior written authorization from the author(s).
 
25
 
 
26
#ifdef HAVE_CONFIG_H
 
27
#include "config.h"
 
28
#endif
 
29
 
 
30
#include <klocale.h>
 
31
#include <ktempfile.h>
 
32
#include <kio/netaccess.h>
 
33
 
 
34
#include "ksaver.h"
 
35
 
 
36
class KSaver::KSaverPrivate
 
37
{
 
38
public:
 
39
        KSaverPrivate() : isLocal(true), tempFile(0), file(0), textStream(0), dataStream(0) {};
 
40
        bool isLocal;
 
41
        KTempFile *tempFile;
 
42
        QFile *file;
 
43
        KURL url;
 
44
        QString error;
 
45
        QTextStream *textStream;
 
46
        QDataStream *dataStream;
 
47
};
 
48
 
 
49
KSaver::KSaver(const KURL &_target)
 
50
{
 
51
        d = new KSaverPrivate;
 
52
        d->url = _target;
 
53
 
 
54
        if(d->url.protocol() == "file")
 
55
        {
 
56
                d->isLocal = true;
 
57
                d->file = new QFile(d->url.path());
 
58
        }
 
59
        else
 
60
        {
 
61
                d->isLocal = false;
 
62
        }
 
63
}
 
64
 
 
65
KSaver::~KSaver()
 
66
{
 
67
        close();
 
68
        delete d;
 
69
}
 
70
 
 
71
bool KSaver::open(void)
 
72
{
 
73
        if(d->isLocal)
 
74
        {
 
75
                if(d->file->open(IO_WriteOnly))
 
76
                {
 
77
                        return true;
 
78
                }
 
79
                else
 
80
                {
 
81
                        d->error = i18n("Could not write to %1.").arg(d->url.prettyURL());
 
82
                        return false;
 
83
                }
 
84
        }
 
85
        else
 
86
        {
 
87
                d->tempFile = new KTempFile;
 
88
                return true;
 
89
        }
 
90
}
 
91
 
 
92
bool KSaver::close(void)
 
93
{
 
94
        if(d->textStream)
 
95
                d->textStream = 0;
 
96
 
 
97
        if(d->dataStream)
 
98
                d->dataStream = 0;
 
99
 
 
100
        if(d->isLocal)
 
101
        {
 
102
                if(!d->file) return true;
 
103
 
 
104
                delete d->file;
 
105
                d->file = 0;
 
106
                return true;
 
107
        }
 
108
        else
 
109
        {
 
110
                if(!d->tempFile) return true;
 
111
 
 
112
                d->tempFile->close();
 
113
 
 
114
                bool retval = KIO::NetAccess::upload(d->tempFile->name(), d->url);
 
115
 
 
116
                delete d->tempFile;
 
117
                d->tempFile = 0;
 
118
 
 
119
                return retval;
 
120
        }
 
121
}
 
122
 
 
123
QString KSaver::error(void)
 
124
{
 
125
        return d->error;
 
126
}
 
127
 
 
128
QFile &KSaver::file(void)
 
129
{
 
130
        if(d->isLocal && d->file)
 
131
                return *d->file;
 
132
        else if(!d->isLocal && d->tempFile)
 
133
                return *d->tempFile->file();
 
134
        else
 
135
                return *static_cast<QFile *>(0);
 
136
}
 
137
 
 
138
QTextStream &KSaver::textStream()
 
139
{
 
140
        if(d->textStream)
 
141
        {
 
142
                return *d->textStream;
 
143
        }
 
144
        else if(d->isLocal && d->file)
 
145
        {
 
146
                d->textStream = new QTextStream(d->file);
 
147
                return *d->textStream;
 
148
        }
 
149
        else if(!d->isLocal && d->tempFile)
 
150
        {
 
151
                d->textStream = d->tempFile->textStream();
 
152
                return *d->textStream;
 
153
        }
 
154
        else
 
155
        {
 
156
                return *static_cast<QTextStream *>(0);
 
157
        }
 
158
}
 
159
 
 
160
QDataStream &KSaver::dataStream()
 
161
{
 
162
        if(d->dataStream)
 
163
        {
 
164
                return *d->dataStream;
 
165
        }
 
166
        else if(d->isLocal && d->file)
 
167
        {
 
168
                d->dataStream = new QDataStream(d->file);
 
169
                return *d->dataStream;
 
170
        }
 
171
        else if(!d->isLocal && d->tempFile)
 
172
        {
 
173
                d->dataStream = d->tempFile->dataStream();
 
174
                return *d->dataStream;
 
175
        }
 
176
        else
 
177
        {
 
178
                return *static_cast<QDataStream *>(0);
 
179
        }
 
180
}