• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.10.5 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 
31 class KAutoSaveFilePrivate
32 {
33 public:
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 
46 const int KAutoSaveFilePrivate::padding = 8;
47 
48 QString 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 
71 KAutoSaveFile::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 
79 KAutoSaveFile::KAutoSaveFile(QObject *parent)
80  : QFile(parent),
81  d(new KAutoSaveFilePrivate)
82 {
83  KGlobal::dirs()->addResourceType("stale", 0, QString::fromLatin1("data/stalefiles"));
84 }
85 
86 KAutoSaveFile::~KAutoSaveFile()
87 {
88  releaseLock();
89  delete d;
90 }
91 
92 KUrl KAutoSaveFile::managedFile() const
93 {
94  return d->managedFile;
95 }
96 
97 void KAutoSaveFile::setManagedFile(const KUrl &filename)
98 {
99  releaseLock();
100 
101  d->managedFile = filename;
102  d->managedFileNameChanged = true;
103 }
104 
105 void KAutoSaveFile::releaseLock()
106 {
107  if (d->lock && d->lock->isLocked()) {
108  d->lock.clear();
109  if (!fileName().isEmpty()) {
110  remove();
111  }
112  }
113 }
114 
115 bool 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  if (d->lock->isLocked()) {
140  close();
141  return false;
142  }
143 
144  d->lock->setStaleTime(60); // HARDCODE, 1 minute
145 
146  if (d->lock->lock(KLockFile::ForceFlag|KLockFile::NoBlockFlag) == KLockFile::LockOK) {
147  return true;
148  } else {
149  close();
150  }
151  }
152 
153  return false;
154 }
155 
156 QList<KAutoSaveFile *> KAutoSaveFile::staleFiles(const KUrl &filename, const QString &applicationName)
157 {
158  KGlobal::dirs()->addResourceType("stale", 0, QString::fromLatin1("data/stalefiles"));
159 
160  QString appName(applicationName);
161  if (appName.isEmpty()) {
162  appName = QCoreApplication::instance()->applicationName();
163  }
164 
165  QString url = filename.fileName();
166 
167  if (url.isEmpty()) {
168  return QList<KAutoSaveFile *>();
169  }
170 
171  // get stale files
172  const QStringList files = KGlobal::dirs()->findAllResources("stale",
173  appName + QChar::fromLatin1('/') +
174  url + QChar::fromLatin1('*'),
175  KStandardDirs::Recursive);
176 
177  QList<KAutoSaveFile *> list;
178  KAutoSaveFile * asFile;
179 
180  // contruct a KAutoSaveFile for each stale file
181  foreach(const QString &file, files) {
182  if (file.endsWith(QLatin1String(".lock")))
183  continue;
184  // sets managedFile
185  asFile = new KAutoSaveFile(filename);
186  asFile->setFileName(file);
187  // flags the name, so it isn't regenerated
188  asFile->d->managedFileNameChanged = false;
189  list.append(asFile);
190  }
191 
192  return list;
193 }
194 
195 QList<KAutoSaveFile *> KAutoSaveFile::allStaleFiles(const QString &applicationName)
196 {
197  KGlobal::dirs()->addResourceType("stale", 0, QString::fromLatin1("data/stalefiles"));
198 
199  QString appName(applicationName);
200  if (appName.isEmpty()) {
201  appName = QCoreApplication::instance()->applicationName();
202  }
203 
204  // get stale files
205  const QStringList files = KGlobal::dirs()->findAllResources("stale", appName + QLatin1String("/*"));
206 
207  QList<KAutoSaveFile *> list;
208 
209  // contruct a KAutoSaveFile for each stale file
210  foreach(QString file, files) { // krazy:exclude=foreach (no const& because modified below)
211  if (file.endsWith(QLatin1String(".lock")))
212  continue;
213  const QString sep = file.right(3);
214  file.chop(KAutoSaveFilePrivate::padding);
215 
216  int sepPos = file.indexOf(sep);
217  int pathPos = file.indexOf(QChar::fromLatin1('_'), sepPos);
218  KUrl name;
219  name.setProtocol(file.mid(sepPos + 3, pathPos - sep.size() - 3));
220  name.setPath(KUrl::fromPercentEncoding(file.right(pathPos - 1).toLatin1()));
221  name.addPath(KUrl::fromPercentEncoding(file.left(sepPos).toLatin1()));
222 
223  // sets managedFile
224  KAutoSaveFile* asFile = new KAutoSaveFile(name);
225  asFile->setFileName(file);
226  // flags the name, so it isn't regenerated
227  asFile->d->managedFileNameChanged = false;
228  list.append(asFile);
229  }
230 
231  return list;
232 }
233 
234 #include "kautosavefile.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Fri Jul 12 2013 08:50:15 by doxygen 1.8.3.1 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.10.5 API Reference

Skip menu "kdelibs-4.10.5 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