moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kiten / kloader.cpp
blob6193e0bfffa9efe3531bfacd3faf2e5e98da69c7
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:
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
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.
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).
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
30 #include <klocale.h>
31 #include <kio/netaccess.h>
33 #include "kloader.h"
35 class KLoader::KLoaderPrivate
37 public:
38 KLoaderPrivate() : isLocal(true), file(0), textStream(0), dataStream(0) {};
39 ~KLoaderPrivate()
41 delete file;
42 delete textStream;
43 delete dataStream;
45 bool isLocal;
46 QString tempFile;
47 QFile *file;
48 KURL url;
49 QString error;
50 QTextStream *textStream;
51 QDataStream *dataStream;
54 KLoader::KLoader(const KURL &target)
56 d = new KLoaderPrivate;
57 d->url = target;
59 if(d->url.protocol() == "file")
61 d->isLocal = true;
62 d->file = new QFile(d->url.path());
64 else
66 d->isLocal = false;
70 KLoader::~KLoader()
72 close();
73 delete d;
76 bool KLoader::open(void)
78 if(d->isLocal)
80 if(!d->file->open(IO_ReadOnly))
82 d->error = i18n("Could not read from %1.").arg(d->url.prettyURL());
83 return false;
86 else
88 if(!KIO::NetAccess::download(d->url, d->tempFile))
90 d->error = i18n("Could not read from %1.").arg(d->url.prettyURL());
91 return false;
94 d->file = new QFile(d->tempFile);
95 if(!d->file->open(IO_ReadOnly))
97 d->error = i18n("Could not read from %1.").arg(d->tempFile);
98 return false;
101 return true;
104 void KLoader::close(void)
106 d->textStream = 0;
107 d->dataStream = 0;
109 delete d->file;
110 d->file = 0;
112 if(!d->isLocal)
113 KIO::NetAccess::removeTempFile(d->tempFile);
116 QString KLoader::error(void)
118 return d->error;
121 QFile &KLoader::file(void)
123 if(d->file)
124 return *d->file;
125 else
126 return *static_cast<QFile *>(0);
129 QTextStream &KLoader::textStream()
131 if(d->textStream)
133 return *d->textStream;
135 else if(d->file)
137 d->textStream = new QTextStream(d->file);
138 return *d->textStream;
140 else
142 return *static_cast<QTextStream *>(0);
146 QDataStream &KLoader::dataStream()
148 if(d->dataStream)
150 return *d->dataStream;
152 else if(d->file)
154 d->dataStream = new QDataStream(d->file);
155 return *d->dataStream;
157 else
159 return *static_cast<QDataStream *>(0);