comparison data/fileio/CachedFile.cpp @ 467:c9b055f84326

* make use of CachedFile (untested)
author Chris Cannam
date Mon, 27 Oct 2008 18:15:20 +0000
parents f35bfa88f0b5
children 70b333085952
comparison
equal deleted inserted replaced
466:f35bfa88f0b5 467:c9b055f84326
59 } 59 }
60 60
61 return fi.filePath(); 61 return fi.filePath();
62 } 62 }
63 63
64 CachedFile::CachedFile(QString url, ProgressReporter *reporter) :
65 m_url(url),
66 m_localFilename(getLocalFilenameFor(m_url)),
67 m_reporter(reporter),
68 m_ok(false)
69 {
70 std::cerr << "CachedFile::CachedFile: url is \""
71 << url.toStdString() << "\"" << std::endl;
72 check();
73 }
74
64 CachedFile::CachedFile(QUrl url, ProgressReporter *reporter) : 75 CachedFile::CachedFile(QUrl url, ProgressReporter *reporter) :
65 m_url(url), 76 m_url(url),
66 m_localFilename(getLocalFilenameFor(url)), 77 m_localFilename(getLocalFilenameFor(m_url)),
67 m_reporter(reporter), 78 m_reporter(reporter),
68 m_ok(false) 79 m_ok(false)
69 { 80 {
70 refresh(); 81 std::cerr << "CachedFile::CachedFile: url is \""
82 << url.toString().toStdString() << "\"" << std::endl;
83 check();
84 }
85
86 CachedFile::~CachedFile()
87 {
71 } 88 }
72 89
73 bool 90 bool
74 CachedFile::isOK() const 91 CachedFile::isOK() const
75 { 92 {
81 { 98 {
82 return m_localFilename; 99 return m_localFilename;
83 } 100 }
84 101
85 void 102 void
86 CachedFile::refresh() 103 CachedFile::check()
87 { 104 {
88 //!!! n.b. obvious race condition here if different CachedFile 105 //!!! n.b. obvious race condition here if different CachedFile
89 // objects for same url used in more than one thread -- need to 106 // objects for same url used in more than one thread -- need to
90 // lock appropriately. also consider race condition between 107 // lock appropriately. also consider race condition between
91 // separate instances of the program 108 // separate instances of the program
92 109
93 if (!QFileInfo(m_localFilename).exists()) { 110 if (!QFileInfo(m_localFilename).exists()) {
111 std::cerr << "CachedFile::check: Local file does not exist, making a note that it hasn't been retrieved" << std::endl;
94 updateLastRetrieval(false); // empirically! 112 updateLastRetrieval(false); // empirically!
95 } 113 }
96 114
97 QDateTime lastRetrieval = getLastRetrieval(); 115 QDateTime lastRetrieval = getLastRetrieval();
98 116
99 if (lastRetrieval.isValid()) { 117 if (lastRetrieval.isValid()) {
118 std::cerr << "CachedFile::check: Valid last retrieval at "
119 << lastRetrieval.toString().toStdString() << std::endl;
100 // this will not be the case if the file is missing, after 120 // this will not be the case if the file is missing, after
101 // updateLastRetrieval(false) was called above 121 // updateLastRetrieval(false) was called above
102 m_ok = true; 122 m_ok = true;
103 if (lastRetrieval.addDays(2) < QDateTime::currentDateTime()) { //!!! 123 if (lastRetrieval.addDays(2) < QDateTime::currentDateTime()) { //!!!
124 std::cerr << "CachedFile::check: Out of date; trying to retrieve again" << std::endl;
104 // doesn't matter if retrieval fails -- we just don't 125 // doesn't matter if retrieval fails -- we just don't
105 // update the last retrieval time 126 // update the last retrieval time
106 127
107 //!!! but we do want an additional last-attempted 128 //!!! but we do want an additional last-attempted
108 // timestamp so as to ensure we aren't retrying the 129 // timestamp so as to ensure we aren't retrying the
109 // retrieval every single time if it isn't working 130 // retrieval every single time if it isn't working
110 131
111 if (retrieve()) { 132 if (retrieve()) {
133 std::cerr << "CachedFile::check: Retrieval succeeded" << std::endl;
112 updateLastRetrieval(true); 134 updateLastRetrieval(true);
113 } 135 } else {
136 std::cerr << "CachedFile::check: Retrieval failed, will try again later (using existing file for now)" << std::endl;
137 }
114 } 138 }
115 } else { 139 } else {
140 std::cerr << "CachedFile::check: No valid last retrieval" << std::endl;
116 // there is no acceptable file 141 // there is no acceptable file
117 if (retrieve()) { 142 if (retrieve()) {
143 std::cerr << "CachedFile::check: Retrieval succeeded" << std::endl;
118 m_ok = true; 144 m_ok = true;
119 updateLastRetrieval(true); 145 updateLastRetrieval(true);
120 } else { 146 } else {
147 std::cerr << "CachedFile::check: Retrieval failed, remaining in invalid state" << std::endl;
121 // again, we don't need to do anything here -- the last 148 // again, we don't need to do anything here -- the last
122 // retrieval timestamp is already invalid 149 // retrieval timestamp is already invalid
123 } 150 }
124 } 151 }
125 } 152 }