• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDECore

  • kdecore
  • io
kautosavefile.cpp
Go to the documentation of this file.
1/* This file is part of the KDE libraries
2 Copyright (c) 2006 Jacob R Rideout <kde@jacobrideout.net>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19
20#include "kautosavefile.h"
21
22#include <stdio.h> // for FILENAME_MAX
23
24#include <QtCore/QLatin1Char>
25#include <QtCore/QCoreApplication>
26#include "klockfile.h"
27#include "krandom.h"
28#include "kglobal.h"
29#include "kstandarddirs.h"
30
31class KAutoSaveFilePrivate
32{
33public:
34 KAutoSaveFilePrivate()
35 : lock(0),
36 managedFileNameChanged(false)
37 {}
38
39 QString tempFileName();
40 KUrl managedFile;
41 KLockFile::Ptr lock;
42 static const int padding;
43 bool managedFileNameChanged;
44};
45
46const int KAutoSaveFilePrivate::padding = 8;
47
48QString KAutoSaveFilePrivate::tempFileName()
49{
50 static const int maxNameLength = FILENAME_MAX;
51
52 // Note: we drop any query string and user/pass info
53 QString protocol(managedFile.protocol());
54 QString path(managedFile.directory());
55 QString name(managedFile.fileName());
56
57 // Remove any part of the path to the right if it is longer than the max file size and
58 // ensure that the max filesize takes into account the other parts of the tempFileName
59 // Subtract 1 for the _ char, 3 for the padding sepperator, 5 is for the .lock
60 path = path.left(maxNameLength - padding - name.size() - protocol.size() - 9);
61
62 QString junk = KRandom::randomString(padding);
63 // tempName = fileName + junk.trunicated + protocol + _ + path.trunicated + junk
64 // This is done so that the separation between the filename and path can be determined
65 name += junk.right(3) + protocol + QLatin1Char('_');
66 name += path + junk;
67
68 return QString::fromLatin1(KUrl::toPercentEncoding(name));
69}
70
71KAutoSaveFile::KAutoSaveFile(const KUrl &filename, QObject *parent)
72 : QFile(parent),
73 d(new KAutoSaveFilePrivate)
74{
75 setManagedFile(filename);
76 KGlobal::dirs()->addResourceType("stale", 0, QString::fromLatin1("data/stalefiles"));
77}
78
79KAutoSaveFile::KAutoSaveFile(QObject *parent)
80 : QFile(parent),
81 d(new KAutoSaveFilePrivate)
82{
83 KGlobal::dirs()->addResourceType("stale", 0, QString::fromLatin1("data/stalefiles"));
84}
85
86KAutoSaveFile::~KAutoSaveFile()
87{
88 releaseLock();
89 delete d;
90}
91
92KUrl KAutoSaveFile::managedFile() const
93{
94 return d->managedFile;
95}
96
97void KAutoSaveFile::setManagedFile(const KUrl &filename)
98{
99 releaseLock();
100
101 d->managedFile = filename;
102 d->managedFileNameChanged = true;
103}
104
105void KAutoSaveFile::releaseLock()
106{
107 if (d->lock && d->lock->isLocked()) {
108 d->lock.clear();
109 if (!fileName().isEmpty()) {
110 remove();
111 }
112 }
113}
114
115bool KAutoSaveFile::open(OpenMode openmode)
116{
117 if (d->managedFile == KUrl()) {
118 return false;
119 }
120
121 QString tempFile;
122 if (d->managedFileNameChanged) {
123 tempFile = KStandardDirs::locateLocal("stale",
124 QCoreApplication::instance()->applicationName()
125 + QChar::fromLatin1('/')
126 + d->tempFileName()
127 );
128 } else {
129 tempFile = fileName();
130 }
131
132 d->managedFileNameChanged = false;
133
134 setFileName(tempFile);
135
136 if (QFile::open(openmode)) {
137
138 d->lock = new KLockFile(tempFile + QString::fromLatin1(".lock"));
139 d->lock->setStaleTime(60); // HARDCODE, 1 minute
140
141 if (d->lock->lock(KLockFile::ForceFlag|KLockFile::NoBlockFlag) == KLockFile::LockOK) {
142 return true;
143 } else {
144 close();
145 }
146 }
147
148 return false;
149}
150
151QList<KAutoSaveFile *> KAutoSaveFile::staleFiles(const KUrl &filename, const QString &applicationName)
152{
153 KGlobal::dirs()->addResourceType("stale", 0, QString::fromLatin1("data/stalefiles"));
154
155 QString appName(applicationName);
156 if (appName.isEmpty()) {
157 appName = QCoreApplication::instance()->applicationName();
158 }
159
160 QString url = filename.fileName();
161
162 if (url.isEmpty()) {
163 return QList<KAutoSaveFile *>();
164 }
165
166 // get stale files
167 const QStringList files = KGlobal::dirs()->findAllResources("stale",
168 appName + QChar::fromLatin1('/') +
169 url + QChar::fromLatin1('*'),
170 KStandardDirs::Recursive);
171
172 QList<KAutoSaveFile *> list;
173 KAutoSaveFile * asFile;
174
175 // contruct a KAutoSaveFile for each stale file
176 foreach(const QString &file, files) {
177 if (file.endsWith(QLatin1String(".lock")))
178 continue;
179 // sets managedFile
180 asFile = new KAutoSaveFile(filename);
181 asFile->setFileName(file);
182 // flags the name, so it isn't regenerated
183 asFile->d->managedFileNameChanged = false;
184 list.append(asFile);
185 }
186
187 return list;
188}
189
190QList<KAutoSaveFile *> KAutoSaveFile::allStaleFiles(const QString &applicationName)
191{
192 KGlobal::dirs()->addResourceType("stale", 0, QString::fromLatin1("data/stalefiles"));
193
194 QString appName(applicationName);
195 if (appName.isEmpty()) {
196 appName = QCoreApplication::instance()->applicationName();
197 }
198
199 // get stale files
200 const QStringList files = KGlobal::dirs()->findAllResources("stale", appName + QLatin1String("/*"));
201
202 QList<KAutoSaveFile *> list;
203
204 // contruct a KAutoSaveFile for each stale file
205 foreach(QString file, files) { // krazy:exclude=foreach (no const& because modified below)
206 if (file.endsWith(QLatin1String(".lock")))
207 continue;
208 const QString sep = file.right(3);
209 file.chop(KAutoSaveFilePrivate::padding);
210
211 int sepPos = file.indexOf(sep);
212 int pathPos = file.indexOf(QChar::fromLatin1('_'), sepPos);
213 KUrl name;
214 name.setProtocol(file.mid(sepPos + 3, pathPos - sep.size() - 3));
215 name.setPath(KUrl::fromPercentEncoding(file.right(pathPos - 1).toLatin1()));
216 name.addPath(KUrl::fromPercentEncoding(file.left(sepPos).toLatin1()));
217
218 // sets managedFile
219 KAutoSaveFile* asFile = new KAutoSaveFile(name);
220 asFile->setFileName(file);
221 // flags the name, so it isn't regenerated
222 asFile->d->managedFileNameChanged = false;
223 list.append(asFile);
224 }
225
226 return list;
227}
228
229#include "kautosavefile.moc"
KAutoSaveFile
Creates and manages a temporary "auto-save" file.
Definition: kautosavefile.h:130
KAutoSaveFile::KAutoSaveFile
KAutoSaveFile(const KUrl &filename, QObject *parent=0)
Constructs a KAutoSaveFile for file filename.
Definition: kautosavefile.cpp:71
KAutoSaveFile::managedFile
KUrl managedFile() const
Retrieves the URL of the file managed by KAutoSaveFile.
Definition: kautosavefile.cpp:92
KAutoSaveFile::releaseLock
virtual void releaseLock()
Closes the autosave file resource and removes the lock file.
Definition: kautosavefile.cpp:105
KAutoSaveFile::open
virtual bool open(OpenMode openmode)
Opens the autosave file and locks it if it wasn't already locked.
Definition: kautosavefile.cpp:115
KAutoSaveFile::setManagedFile
void setManagedFile(const KUrl &filename)
Sets the URL of the file managed by KAutoSaveFile.
Definition: kautosavefile.cpp:97
KAutoSaveFile::~KAutoSaveFile
~KAutoSaveFile()
Destroys the KAutoSaveFile object, removes the autosave file and drops the lock being held (if any).
Definition: kautosavefile.cpp:86
KAutoSaveFile::allStaleFiles
static QList< KAutoSaveFile * > allStaleFiles(const QString &applicationName=QString())
Returns all stale autosave files left behind by crashed or otherwise gone instances of this applicati...
Definition: kautosavefile.cpp:190
KAutoSaveFile::staleFiles
static QList< KAutoSaveFile * > staleFiles(const KUrl &filename, const QString &applicationName=QString())
Checks for stale autosave files for filename.
Definition: kautosavefile.cpp:151
KLockFile
The KLockFile class provides NFS safe lockfiles.
Definition: klockfile.h:37
KLockFile::LockOK
@ LockOK
Lock was acquired successfully.
Definition: klockfile.h:55
KLockFile::NoBlockFlag
@ NoBlockFlag
Return immediately, do not wait for the lock to become available.
Definition: klockfile.h:77
KLockFile::ForceFlag
@ ForceFlag
Automatically remove a lock when a lock is detected that is stale for more than staleTime() seconds,...
Definition: klockfile.h:84
KSharedPtr< KLockFile >
KStandardDirs::Recursive
@ Recursive
Definition: kstandarddirs.h:191
KStandardDirs::findAllResources
QStringList findAllResources(const char *type, const QString &filter=QString(), SearchOptions options=NoSearchOptions) const
Tries to find all resources with the specified type.
Definition: kstandarddirs.cpp:900
KStandardDirs::locateLocal
static QString locateLocal(const char *type, const QString &filename, const KComponentData &cData=KGlobal::mainComponent())
This function is much like locate.
Definition: kstandarddirs.cpp:2097
KStandardDirs::addResourceType
bool addResourceType(const char *type, const QString &relativename, bool priority=true)
Adds suffixes for types.
Definition: kstandarddirs.cpp:393
KUrl
Represents and parses a URL.
Definition: kurl.h:112
KUrl::setProtocol
void setProtocol(const QString &proto)
Sets the protocol for the URL (i.e., file, http, etc.)
Definition: kurl.cpp:677
KUrl::setPath
void setPath(const QString &path)
Definition: kurl.cpp:1772
KUrl::fileName
QString fileName(const DirectoryOptions &options=IgnoreTrailingSlash) const
Returns the filename of the path.
Definition: kurl.cpp:1283
KUrl::addPath
void addPath(const QString &txt)
Adds to the current path.
Definition: kurl.cpp:1344
QList
Definition: kaboutdata.h:33
QObject
QStringList
QString
kautosavefile.h
kglobal.h
klockfile.h
krandom.h
kstandarddirs.h
KGlobal::dirs
KStandardDirs * dirs()
Returns the application standard dirs object.
KRandom::randomString
QString randomString(int length)
Generates a random string.
Definition: krandom.cpp:52
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

Skip menu "KDECore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal