ctkCmdLineModuleCache.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. #include <QHash>
  22. #if (QT_VERSION < QT_VERSION_CHECK(4,7,0))
  23. #include "ctkCommandLineModulesCoreExport.h"
  24. CTK_CMDLINEMODULECORE_EXPORT int qHash(const QUrl& url)
  25. {
  26. return qHash(url.toString());
  27. }
  28. #endif
  29. struct ctkCmdLineModuleCachePrivate
  30. {
  31. QString CacheDir;
  32. QHash<QUrl, qint64> LocationToTimeStamp;
  33. QHash<QUrl, QByteArray> LocationToXmlDescription;
  34. QMutex Mutex;
  35. void LoadTimeStamps()
  36. {
  37. QDirIterator dirIter(this->CacheDir, QStringList() << "*.timestamp", QDir::Files | QDir::Readable);
  38. while(dirIter.hasNext())
  39. {
  40. QFile timestampFile(dirIter.next());
  41. timestampFile.open(QIODevice::ReadOnly);
  42. QUrl url = QUrl(timestampFile.readLine().trimmed().data());
  43. QByteArray timestamp = timestampFile.readLine();
  44. bool ok = false;
  45. qint64 ts = timestamp.toLongLong(&ok);
  46. if (ok && !url.isEmpty())
  47. {
  48. this->LocationToTimeStamp[url] = ts;
  49. }
  50. }
  51. }
  52. QString timeStampFileName(const QUrl& moduleLocation) const
  53. {
  54. return this->CacheDir + "/" + QString::number(qHash(moduleLocation)) + ".timestamp";
  55. }
  56. QString xmlFileName(const QUrl& moduleLocation) const
  57. {
  58. return this->CacheDir + "/" + QString::number(qHash(moduleLocation)) + ".xml";
  59. }
  60. };
  61. ctkCmdLineModuleCache::ctkCmdLineModuleCache(const QString& cacheDir)
  62. : d(new ctkCmdLineModuleCachePrivate)
  63. {
  64. d->CacheDir = cacheDir;
  65. d->LoadTimeStamps();
  66. }
  67. ctkCmdLineModuleCache::~ctkCmdLineModuleCache()
  68. {
  69. }
  70. QString ctkCmdLineModuleCache::cacheDir() const
  71. {
  72. QMutexLocker lock(&d->Mutex);
  73. return d->CacheDir;
  74. }
  75. QByteArray ctkCmdLineModuleCache::rawXmlDescription(const QUrl& moduleLocation) const
  76. {
  77. QMutexLocker lock(&d->Mutex);
  78. if (d->LocationToXmlDescription.contains(moduleLocation))
  79. {
  80. return d->LocationToXmlDescription[moduleLocation];
  81. }
  82. // lazily load the XML description from the file system
  83. QByteArray xml;
  84. QString a = moduleLocation.toString();
  85. QString fn = d->xmlFileName(moduleLocation);
  86. QFile xmlFile(d->xmlFileName(moduleLocation));
  87. if (xmlFile.exists() && xmlFile.open(QIODevice::ReadOnly))
  88. {
  89. xml = xmlFile.readAll();
  90. xmlFile.close();
  91. }
  92. d->LocationToXmlDescription[moduleLocation] = xml;
  93. return xml;
  94. }
  95. qint64 ctkCmdLineModuleCache::timeStamp(const QUrl& moduleLocation) const
  96. {
  97. QMutexLocker lock(&d->Mutex);
  98. if (d->LocationToTimeStamp.contains(moduleLocation))
  99. {
  100. return d->LocationToTimeStamp[moduleLocation];
  101. }
  102. return -1;
  103. }
  104. void ctkCmdLineModuleCache::cacheXmlDescription(const QUrl& moduleLocation, qint64 timestamp, const QByteArray& xmlDescription)
  105. {
  106. QFile timestampFile(d->timeStampFileName(moduleLocation));
  107. QFile xmlFile(d->xmlFileName(moduleLocation));
  108. timestampFile.remove();
  109. timestampFile.open(QIODevice::WriteOnly);
  110. QByteArray ba;
  111. QTextStream str(&ba);
  112. str << moduleLocation.toString() << '\n' << timestamp;
  113. str.flush();
  114. if (timestampFile.write(ba) == -1)
  115. {
  116. timestampFile.close();
  117. timestampFile.remove();
  118. return;
  119. }
  120. timestampFile.close();
  121. xmlFile.remove();
  122. if (!xmlDescription.isEmpty())
  123. {
  124. xmlFile.open(QIODevice::WriteOnly);
  125. if (xmlFile.write(xmlDescription) == -1)
  126. {
  127. timestampFile.remove();
  128. xmlFile.close();
  129. xmlFile.remove();
  130. return;
  131. }
  132. }
  133. {
  134. QMutexLocker lock(&d->Mutex);
  135. d->LocationToXmlDescription[moduleLocation] = xmlDescription;
  136. d->LocationToTimeStamp[moduleLocation] = timestamp;
  137. }
  138. }
  139. void ctkCmdLineModuleCache::removeCacheEntry(const QUrl& moduleLocation)
  140. {
  141. {
  142. QMutexLocker lock(&d->Mutex);
  143. d->LocationToTimeStamp.remove(moduleLocation);
  144. d->LocationToXmlDescription.remove(moduleLocation);
  145. }
  146. QFile timestampFile(d->timeStampFileName(moduleLocation));
  147. if (timestampFile.exists())
  148. {
  149. timestampFile.remove();
  150. }
  151. QFile xmlFile(d->xmlFileName(moduleLocation));
  152. if (xmlFile.exists())
  153. {
  154. xmlFile.remove();
  155. }
  156. }
  157. void ctkCmdLineModuleCache::clearCache()
  158. {
  159. foreach(const QUrl &url, d->LocationToXmlDescription.keys())
  160. {
  161. removeCacheEntry(url);
  162. }
  163. }