~ubuntu-branches/ubuntu/jaunty/psi/jaunty

« back to all changes in this revision

Viewing changes to src/tools/atomicxmlfile/atomicxmlfile.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2008-08-28 18:46:52 UTC
  • mfrom: (1.2.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20080828184652-iiik12dl91nq7cdi
Tags: 0.12-2
Uploading to unstable (Closes: Bug#494352)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * atomicxmlfile.cpp - atomic saving of QDomDocuments in files
 
3
 * Copyright (C) 2007  Michail Pishchagin
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU General Public License
 
7
 * as published by the Free Software Foundation; either version 2
 
8
 * of the License, or (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this library; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 *
 
19
 */
 
20
 
 
21
#include "atomicxmlfile.h"
 
22
 
 
23
#include <QDomDocument>
 
24
#include <QFile>
 
25
#include <QStringList>
 
26
#include <QTextStream>
 
27
 
 
28
/**
 
29
 * Creates new instance of AtomicXmlFile class that will be able to
 
30
 * atomically save config file, so if application is terminated while
 
31
 * saving config file, data is not lost.
 
32
 */
 
33
AtomicXmlFile::AtomicXmlFile(QString fileName)
 
34
        : fileName_(fileName)
 
35
{
 
36
}
 
37
 
 
38
/**
 
39
 * Atomically save \a doc to specified name. Prior to saving, back up
 
40
 * of old config data is created, and only then data is saved.
 
41
 */
 
42
bool AtomicXmlFile::saveDocument(const QDomDocument& doc) const
 
43
{
 
44
        if (!saveDocument(doc, tempFileName())) {
 
45
                qWarning("AtomicXmlFile::saveDocument(): Unable to save '%s'. Possibly drive is full.",
 
46
                         qPrintable(tempFileName()));
 
47
                return false;
 
48
        }
 
49
 
 
50
        if (QFile::exists(backupFileName()))
 
51
                QFile::remove(backupFileName());
 
52
 
 
53
        if (QFile::exists(fileName_)) {
 
54
                if (!QFile::rename(fileName_, backupFileName())) {
 
55
                        qWarning("AtomicXmlFile::saveDocument(): Unable to rename '%s' to '%s'.",
 
56
                                 qPrintable(fileName_), qPrintable(backupFileName()));
 
57
                        return false;
 
58
                }
 
59
        }
 
60
 
 
61
        if (!QFile::rename(tempFileName(), fileName_)) {
 
62
                qWarning("AtomicXmlFile::saveDocument(): Unable to rename '%s' to '%s'.",
 
63
                         qPrintable(tempFileName()), qPrintable(fileName_));
 
64
                return false;
 
65
        }
 
66
 
 
67
        return true;
 
68
}
 
69
 
 
70
/**
 
71
 * Tries to load \a doc from config file, or if that fails, from a back up.
 
72
 */
 
73
bool AtomicXmlFile::loadDocument(QDomDocument* doc) const
 
74
{
 
75
        Q_ASSERT(doc);
 
76
 
 
77
        QStringList fileNames;
 
78
        fileNames << fileName_
 
79
                  << tempFileName()
 
80
                  << backupFileName();
 
81
 
 
82
        foreach(QString fileName, fileNames)
 
83
                if (loadDocument(doc, fileName))
 
84
                        return true;
 
85
 
 
86
        return false;
 
87
}
 
88
 
 
89
/**
 
90
 * Returns name of the file the config is first written to.
 
91
 */
 
92
QString AtomicXmlFile::tempFileName() const
 
93
{
 
94
        return fileName_ + ".temp";
 
95
}
 
96
 
 
97
/**
 
98
 * Returns name of the back up file.
 
99
 */
 
100
QString AtomicXmlFile::backupFileName() const
 
101
{
 
102
        return fileName_ + ".backup";
 
103
}
 
104
 
 
105
bool AtomicXmlFile::saveDocument(const QDomDocument& doc, QString fileName) const
 
106
{
 
107
        bool result = false;
 
108
 
 
109
        QFile file(fileName);
 
110
        if (!file.open(QIODevice::WriteOnly)) {
 
111
                return result;
 
112
        }
 
113
 
 
114
        QTextStream text;
 
115
        text.setDevice(&file);
 
116
        text.setCodec("UTF-8");
 
117
        text << doc.toString();
 
118
 
 
119
        result = file.error() == QFile::NoError;
 
120
        file.close();
 
121
 
 
122
        // QFile error checking should be enough, but to be completely sure that
 
123
        // XML is well-formed we could try to parse data we just had written:
 
124
        // if (result) {
 
125
        //      QDomDocument temp;
 
126
        //      result = loadDocument(&temp, fileName);
 
127
        // }
 
128
 
 
129
        return result;
 
130
}
 
131
 
 
132
bool AtomicXmlFile::loadDocument(QDomDocument* doc, QString fileName) const
 
133
{
 
134
        QFile file(fileName);
 
135
        if (!file.open(QIODevice::ReadOnly)) {
 
136
                return false;
 
137
        }
 
138
 
 
139
        if (!doc->setContent(&file)) {
 
140
                return false;
 
141
        }
 
142
 
 
143
        file.close();
 
144
        return true;
 
145
}