KDECore
kautosavefile.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "kautosavefile.h"
00021
00022 #include <stdio.h>
00023
00024 #include <QtCore/QLatin1Char>
00025 #include <QtCore/QCoreApplication>
00026 #include "klockfile.h"
00027 #include "krandom.h"
00028 #include "kglobal.h"
00029 #include "kstandarddirs.h"
00030
00031 class KAutoSaveFilePrivate
00032 {
00033 public:
00034 KAutoSaveFilePrivate()
00035 : lock(0),
00036 managedFileNameChanged(false)
00037 {}
00038
00039 QString tempFileName();
00040 KUrl managedFile;
00041 KLockFile::Ptr lock;
00042 static const int padding;
00043 bool managedFileNameChanged;
00044 };
00045
00046 const int KAutoSaveFilePrivate::padding = 8;
00047
00048 QString KAutoSaveFilePrivate::tempFileName()
00049 {
00050 static const int maxNameLength = FILENAME_MAX;
00051
00052
00053 QString protocol(managedFile.protocol());
00054 QString path(managedFile.directory());
00055 QString name(managedFile.fileName());
00056
00057
00058
00059
00060 path = path.left(maxNameLength - padding - name.size() - protocol.size() - 9);
00061
00062 QString junk = KRandom::randomString(padding);
00063
00064
00065 name += junk.right(3) + protocol + QLatin1Char('_');
00066 name += path + junk;
00067
00068 return QString::fromLatin1(KUrl::toPercentEncoding(name));
00069 }
00070
00071 KAutoSaveFile::KAutoSaveFile(const KUrl &filename, QObject *parent)
00072 : QFile(parent),
00073 d(new KAutoSaveFilePrivate)
00074 {
00075 setManagedFile(filename);
00076 KGlobal::dirs()->addResourceType("stale", 0, QString::fromLatin1("data/stalefiles"));
00077 }
00078
00079 KAutoSaveFile::KAutoSaveFile(QObject *parent)
00080 : QFile(parent),
00081 d(new KAutoSaveFilePrivate)
00082 {
00083 KGlobal::dirs()->addResourceType("stale", 0, QString::fromLatin1("data/stalefiles"));
00084 }
00085
00086 KAutoSaveFile::~KAutoSaveFile()
00087 {
00088 releaseLock();
00089 delete d;
00090 }
00091
00092 KUrl KAutoSaveFile::managedFile() const
00093 {
00094 return d->managedFile;
00095 }
00096
00097 void KAutoSaveFile::setManagedFile(const KUrl &filename)
00098 {
00099 releaseLock();
00100
00101 d->managedFile = filename;
00102 d->managedFileNameChanged = true;
00103 }
00104
00105 void KAutoSaveFile::releaseLock()
00106 {
00107 if (d->lock && d->lock->isLocked()) {
00108 d->lock.clear();
00109 if (!fileName().isEmpty()) {
00110 remove();
00111 }
00112 }
00113 }
00114
00115 bool KAutoSaveFile::open(OpenMode openmode)
00116 {
00117 if (d->managedFile == KUrl()) {
00118 return false;
00119 }
00120
00121 QString tempFile;
00122 if (d->managedFileNameChanged) {
00123 tempFile = KStandardDirs::locateLocal("stale",
00124 QCoreApplication::instance()->applicationName()
00125 + QChar::fromLatin1('/')
00126 + d->tempFileName()
00127 );
00128 } else {
00129 tempFile = fileName();
00130 }
00131
00132 d->managedFileNameChanged = false;
00133
00134 setFileName(tempFile);
00135
00136 if (QFile::open(openmode)) {
00137
00138 d->lock = new KLockFile(tempFile + QString::fromLatin1(".lock"));
00139 if (d->lock->isLocked()) {
00140 close();
00141 return false;
00142 }
00143
00144 d->lock->setStaleTime(60);
00145
00146 if (d->lock->lock(KLockFile::ForceFlag|KLockFile::NoBlockFlag) == KLockFile::LockOK) {
00147 return true;
00148 } else {
00149 close();
00150 }
00151 }
00152
00153 return false;
00154 }
00155
00156 QList<KAutoSaveFile *> KAutoSaveFile::staleFiles(const KUrl &filename, const QString &applicationName)
00157 {
00158 KGlobal::dirs()->addResourceType("stale", 0, QString::fromLatin1("data/stalefiles"));
00159
00160 QString appName(applicationName);
00161 if (appName.isEmpty()) {
00162 appName = QCoreApplication::instance()->applicationName();
00163 }
00164
00165 QString url = filename.fileName();
00166
00167 if (url.isEmpty()) {
00168 return QList<KAutoSaveFile *>();
00169 }
00170
00171
00172 QStringList files = KGlobal::dirs()->findAllResources("stale",
00173 appName + QChar::fromLatin1('/') +
00174 url + QChar::fromLatin1('*'),
00175 KStandardDirs::Recursive);
00176
00177 QList<KAutoSaveFile *> list;
00178 KAutoSaveFile * asFile;
00179
00180
00181 foreach(const QString &file, files) {
00182 if (file.endsWith(".lock"))
00183 continue;
00184
00185 asFile = new KAutoSaveFile(filename);
00186 asFile->setFileName(file);
00187
00188 asFile->d->managedFileNameChanged = false;
00189 list.append(asFile);
00190 }
00191
00192 return list;
00193 }
00194
00195 QList<KAutoSaveFile *> KAutoSaveFile::allStaleFiles(const QString &applicationName)
00196 {
00197 KGlobal::dirs()->addResourceType("stale", 0, QString::fromLatin1("data/stalefiles"));
00198
00199 QString appName(applicationName);
00200 if (appName.isEmpty()) {
00201 appName = QCoreApplication::instance()->applicationName();
00202 }
00203
00204
00205 QStringList files = KGlobal::dirs()->findAllResources("stale", appName + QLatin1String("/*"));
00206
00207 QList<KAutoSaveFile *> list;
00208 QString file, sep;
00209 KUrl name;
00210 KAutoSaveFile * asFile;
00211
00212
00213 foreach(file, files) {
00214 if (file.endsWith(".lock"))
00215 continue;
00216 sep = file.right(3);
00217 file.chop(KAutoSaveFilePrivate::padding);
00218
00219 int sepPos = file.indexOf(sep);
00220 int pathPos = file.indexOf(QChar::fromLatin1('_'), sepPos);
00221 name.setProtocol(file.mid(sepPos + 3, pathPos - sep.size() - 3));
00222 name.setPath(KUrl::fromPercentEncoding(file.right(pathPos - 1).toLatin1()));
00223 name.addPath(KUrl::fromPercentEncoding(file.left(sepPos).toLatin1()));
00224
00225
00226 asFile = new KAutoSaveFile(name);
00227 asFile->setFileName(file);
00228
00229 asFile->d->managedFileNameChanged = false;
00230 list.append(asFile);
00231 }
00232
00233 return list;
00234 }
00235
00236 #include "kautosavefile.moc"