summaryrefslogtreecommitdiffstats
path: root/src/common/gui/pfile_ext.cpp
blob: 080421fc6194a7a39e09eb8892c5cb2cf7a82356 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/***************************************************************************
 *   Copyright (C) 2005-2006 Nicolas Hadacek <hadacek@kde.org>             *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/
#include "pfile_ext.h"

#include <tqfile.h>
#include <kio/netaccess.h>
#include <ktempfile.h>
#include "common/gui/misc_gui.h"

//-----------------------------------------------------------------------------
bool PURL::File::openForWrite()
{
  close();
  if (_tmp) delete _tmp;
  _tmp = new KTempFile(TQString(), _extension);
  _tmp->setAutoDelete(true);
  if ( _tmp->status()!=0 ) {
    _error = i18n("Could not create temporary file.");
    _log.sorry(_error, i18n("File: %1").arg(_tmp->name()));
    return false;
  }
  return true;
}

bool PURL::File::close()
{
  if (_tmp) _tmp->close();
  else _file->close();
  bool ok = (_tmp ? _tmp->status() : _file->status())==IO_Ok;
  if ( !_file->name().isEmpty() ) {
    KIO::NetAccess::removeTempFile(_file->name());
    _file->setName(TQString());
  }
  delete _stream;
  _stream = 0;
  if ( ok && _tmp && !_url.isEmpty() && !KIO::NetAccess::upload(_tmp->name(), _url.kurl(), tqApp->mainWidget()) ) {
    _error = KIO::NetAccess::lastErrorString();
    ok = false;
    _log.sorry(i18n("Could not save file."), errorString());
  }
  delete _tmp;
  _tmp = 0;
  return ok;
}

bool PURL::File::openForRead()
{
  close();
  TQString tmp;
  if ( !KIO::NetAccess::download(_url.kurl(), tmp, tqApp->mainWidget()) ) {
    _error = KIO::NetAccess::lastErrorString();
    _log.sorry(i18n("Could not open file for reading."), errorString());
    return false;
  }
  _file->setName(tmp);
  if ( !_file->open(IO_ReadOnly) ) {
    _error = i18n("Could not open temporary file.");
    _log.sorry(_error, i18n("File: %1").arg(_file->name()));
    return false;
  }
  return true;
}

bool PURL::File::remove()
{
  close();
  if ( !_url.isEmpty() ) return _url.del(_log);
  return false;
}

//-----------------------------------------------------------------------------
PURL::TempFile::TempFile(Log::Generic &log, const TQString &extension)
  : FileBase(log, extension)
{}

PURL::Url PURL::TempFile::url() const
{
  return (_tmp ? Url::fromPathOrUrl(_tmp->name()) : Url());
}

bool PURL::TempFile::close()
{
  delete _stream;
  _stream = 0;
  if (_tmp) {
    _tmp->close();
    if ( _tmp->status()!=IO_Ok ) {
      _error = i18n("Could not write to temporary file.");
      _log.sorry(_error, i18n("File: %1").arg(_tmp->name()));
      return false;
    }
  }
  return true;
}

bool PURL::TempFile::openForWrite()
{
  close();
  if (_tmp) delete _tmp;
  _tmp = new KTempFile(TQString(), _extension);
  _tmp->setAutoDelete(true);
  if ( _tmp->status()!=0 ) {
    _error = i18n("Could not create temporary file.");
    _log.sorry(_error, i18n("File: %1").arg(_tmp->name()));
    return false;
  }
  return true;
}