ctkCmdLineModuleCache.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #include "ctkCmdLineModuleCache_p.h"
  16. #include <QUrl>
  17. #include <QFile>
  18. #include <QDirIterator>
  19. #include <QTextStream>
  20. #include <QMutex>
  21. struct ctkCmdLineModuleCachePrivate
  22. {
  23. QString CacheDir;
  24. QHash<QUrl, qint64> LocationToTimeStamp;
  25. QHash<QUrl, QByteArray> LocationToXmlDescription;
  26. QMutex Mutex;
  27. void LoadTimeStamps()
  28. {
  29. QDirIterator dirIter(this->CacheDir, QStringList() << "*.timestamp", QDir::Files | QDir::Readable);
  30. while(dirIter.hasNext())
  31. {
  32. QFile timestampFile(dirIter.next());
  33. timestampFile.open(QIODevice::ReadOnly);
  34. QUrl url = QUrl(timestampFile.readLine().trimmed().data());
  35. QByteArray timestamp = timestampFile.readLine();
  36. bool ok = false;
  37. qint64 ts = timestamp.toLongLong(&ok);
  38. if (ok && !url.isEmpty())
  39. {
  40. this->LocationToTimeStamp[url] = ts;
  41. }
  42. }
  43. }
  44. QString timeStampFileName(const QUrl& moduleLocation) const
  45. {
  46. return this->CacheDir + "/" + QString::number(qHash(moduleLocation)) + ".timestamp";
  47. }
  48. QString xmlFileName(const QUrl& moduleLocation) const
  49. {
  50. return this->CacheDir + "/" + QString::number(qHash(moduleLocation)) + ".xml";
  51. }
  52. };
  53. ctkCmdLineModuleCache::ctkCmdLineModuleCache(const QString& cacheDir)
  54. : d(new ctkCmdLineModuleCachePrivate)
  55. {
  56. d->CacheDir = cacheDir;
  57. d->LoadTimeStamps();
  58. }
  59. ctkCmdLineModuleCache::~ctkCmdLineModuleCache()
  60. {
  61. }
  62. QString ctkCmdLineModuleCache::cacheDir() const
  63. {
  64. QMutexLocker lock(&d->Mutex);
  65. return d->CacheDir;
  66. }
  67. QByteArray ctkCmdLineModuleCache::rawXmlDescription(const QUrl& moduleLocation) const
  68. {
  69. QMutexLocker lock(&d->Mutex);
  70. if (d->LocationToXmlDescription.contains(moduleLocation))
  71. {
  72. return d->LocationToXmlDescription[moduleLocation];
  73. }
  74. // lazily load the XML description from the file system
  75. QByteArray xml;
  76. QString a = moduleLocation.toString();
  77. QString fn = d->xmlFileName(moduleLocation);
  78. QFile xmlFile(d->xmlFileName(moduleLocation));
  79. if (xmlFile.exists() && xmlFile.open(QIODevice::ReadOnly))
  80. {
  81. xml = xmlFile.readAll();
  82. xmlFile.close();
  83. }
  84. d->LocationToXmlDescription[moduleLocation] = xml;
  85. return xml;
  86. }
  87. qint64 ctkCmdLineModuleCache::timeStamp(const QUrl& moduleLocation) const
  88. {
  89. QMutexLocker lock(&d->Mutex);
  90. if (d->LocationToTimeStamp.contains(moduleLocation))
  91. {
  92. return d->LocationToTimeStamp[moduleLocation];
  93. }
  94. return -1;
  95. }
  96. void ctkCmdLineModuleCache::cacheXmlDescription(const QUrl& moduleLocation, qint64 timestamp, const QByteArray& xmlDescription)
  97. {
  98. QFile timestampFile(d->timeStampFileName(moduleLocation));
  99. QFile xmlFile(d->xmlFileName(moduleLocation));
  100. timestampFile.remove();
  101. timestampFile.open(QIODevice::WriteOnly);
  102. QByteArray ba;
  103. QTextStream str(&ba);
  104. str << moduleLocation.toString() << '\n' << timestamp;
  105. str.flush();
  106. if (timestampFile.write(ba) == -1)
  107. {
  108. timestampFile.close();
  109. timestampFile.remove();
  110. return;
  111. }
  112. timestampFile.close();
  113. xmlFile.remove();
  114. if (!xmlDescription.isEmpty())
  115. {
  116. xmlFile.open(QIODevice::WriteOnly);
  117. if (xmlFile.write(xmlDescription) == -1)
  118. {
  119. timestampFile.remove();
  120. xmlFile.close();
  121. xmlFile.remove();
  122. return;
  123. }
  124. }
  125. {
  126. QMutexLocker lock(&d->Mutex);
  127. d->LocationToXmlDescription[moduleLocation] = xmlDescription;
  128. d->LocationToTimeStamp[moduleLocation] = timestamp;
  129. }
  130. }
  131. void ctkCmdLineModuleCache::removeCacheEntry(const QUrl& moduleLocation)
  132. {
  133. {
  134. QMutexLocker lock(&d->Mutex);
  135. d->LocationToTimeStamp.remove(moduleLocation);
  136. d->LocationToXmlDescription.remove(moduleLocation);
  137. }
  138. QFile timestampFile(d->timeStampFileName(moduleLocation));
  139. if (timestampFile.exists())
  140. {
  141. timestampFile.remove();
  142. }
  143. QFile xmlFile(d->xmlFileName(moduleLocation));
  144. if (xmlFile.exists())
  145. {
  146. xmlFile.remove();
  147. }
  148. }