Commit 12647ef8f1955596c68a9b7ca02ca5fd713b40a0
Merge pull request #290 from biometrics/defer_open
Address issues #242, #192
Showing
2 changed files
with
76 additions
and
10 deletions
openbr/plugins/gallery.cpp
| ... | ... | @@ -124,23 +124,51 @@ class BinaryGallery : public Gallery |
| 124 | 124 | |
| 125 | 125 | gallery.open(stderr, QFile::WriteOnly); |
| 126 | 126 | } else { |
| 127 | + // Defer opening the file, in the general case we don't know if we | |
| 128 | + // need read or write mode yet | |
| 129 | + return; | |
| 130 | + } | |
| 131 | + stream.setDevice(&gallery); | |
| 132 | + } | |
| 133 | + | |
| 134 | + void readOpen() | |
| 135 | + { | |
| 136 | + if (!gallery.isOpen()) { | |
| 127 | 137 | gallery.setFileName(file); |
| 138 | + if (!gallery.exists()) | |
| 139 | + qFatal("File %s does not exist", qPrintable(gallery.fileName())); | |
| 140 | + | |
| 141 | + QFile::OpenMode mode = QFile::ReadOnly; | |
| 142 | + if (!gallery.open(mode)) | |
| 143 | + qFatal("Can't open gallery: %s for reading", qPrintable(gallery.fileName())); | |
| 144 | + stream.setDevice(&gallery); | |
| 145 | + } | |
| 146 | + } | |
| 147 | + | |
| 148 | + void writeOpen() | |
| 149 | + { | |
| 150 | + if (!gallery.isOpen()) { | |
| 151 | + gallery.setFileName(file); | |
| 152 | + | |
| 153 | + // Do we remove the pre-existing gallery? | |
| 128 | 154 | if (file.get<bool>("remove")) |
| 129 | 155 | gallery.remove(); |
| 130 | 156 | QtUtils::touchDir(gallery); |
| 131 | - QFile::OpenMode mode = QFile::ReadWrite; | |
| 157 | + QFile::OpenMode mode = QFile::WriteOnly; | |
| 132 | 158 | |
| 159 | + // Do we append? | |
| 133 | 160 | if (file.get<bool>("append")) |
| 134 | 161 | mode |= QFile::Append; |
| 135 | 162 | |
| 136 | 163 | if (!gallery.open(mode)) |
| 137 | - qFatal("Can't open gallery: %s", qPrintable(gallery.fileName())); | |
| 164 | + qFatal("Can't open gallery: %s for writing", qPrintable(gallery.fileName())); | |
| 165 | + stream.setDevice(&gallery); | |
| 138 | 166 | } |
| 139 | - stream.setDevice(&gallery); | |
| 140 | 167 | } |
| 141 | 168 | |
| 142 | 169 | TemplateList readBlock(bool *done) |
| 143 | 170 | { |
| 171 | + readOpen(); | |
| 144 | 172 | if (gallery.atEnd()) |
| 145 | 173 | gallery.seek(0); |
| 146 | 174 | |
| ... | ... | @@ -163,6 +191,7 @@ class BinaryGallery : public Gallery |
| 163 | 191 | |
| 164 | 192 | void write(const Template &t) |
| 165 | 193 | { |
| 194 | + writeOpen(); | |
| 166 | 195 | writeTemplate(t); |
| 167 | 196 | if (gallery.isSequential()) |
| 168 | 197 | gallery.flush(); |
| ... | ... | @@ -174,6 +203,7 @@ protected: |
| 174 | 203 | |
| 175 | 204 | qint64 totalSize() |
| 176 | 205 | { |
| 206 | + readOpen(); | |
| 177 | 207 | return gallery.size(); |
| 178 | 208 | } |
| 179 | 209 | |
| ... | ... | @@ -879,6 +909,7 @@ class csvGallery : public FileGallery |
| 879 | 909 | |
| 880 | 910 | TemplateList readBlock(bool *done) |
| 881 | 911 | { |
| 912 | + readOpen(); | |
| 882 | 913 | *done = false; |
| 883 | 914 | TemplateList templates; |
| 884 | 915 | if (!file.exists()) { |
| ... | ... | @@ -971,6 +1002,7 @@ class txtGallery : public FileGallery |
| 971 | 1002 | |
| 972 | 1003 | TemplateList readBlock(bool *done) |
| 973 | 1004 | { |
| 1005 | + readOpen(); | |
| 974 | 1006 | *done = false; |
| 975 | 1007 | if (f.atEnd()) |
| 976 | 1008 | f.seek(0); |
| ... | ... | @@ -1000,6 +1032,7 @@ class txtGallery : public FileGallery |
| 1000 | 1032 | |
| 1001 | 1033 | void write(const Template &t) |
| 1002 | 1034 | { |
| 1035 | + writeOpen(); | |
| 1003 | 1036 | QString line = t.file.name; |
| 1004 | 1037 | if (!label.isEmpty()) |
| 1005 | 1038 | line += " " + t.file.get<QString>(label); |
| ... | ... | @@ -1021,6 +1054,7 @@ class flatGallery : public FileGallery |
| 1021 | 1054 | |
| 1022 | 1055 | TemplateList readBlock(bool *done) |
| 1023 | 1056 | { |
| 1057 | + readOpen(); | |
| 1024 | 1058 | *done = false; |
| 1025 | 1059 | if (f.atEnd()) |
| 1026 | 1060 | f.seek(0); |
| ... | ... | @@ -1047,6 +1081,7 @@ class flatGallery : public FileGallery |
| 1047 | 1081 | |
| 1048 | 1082 | void write(const Template &t) |
| 1049 | 1083 | { |
| 1084 | + writeOpen(); | |
| 1050 | 1085 | f.write((t.file.flat()+"\n").toLocal8Bit() ); |
| 1051 | 1086 | } |
| 1052 | 1087 | }; |
| ... | ... | @@ -1079,6 +1114,9 @@ class xmlGallery : public FileGallery |
| 1079 | 1114 | |
| 1080 | 1115 | TemplateList readBlock(bool *done) |
| 1081 | 1116 | { |
| 1117 | + if (readOpen()) | |
| 1118 | + reader.setDevice(&f); | |
| 1119 | + | |
| 1082 | 1120 | if (reader.atEnd()) |
| 1083 | 1121 | f.seek(0); |
| 1084 | 1122 | |
| ... | ... | @@ -1202,7 +1240,6 @@ class xmlGallery : public FileGallery |
| 1202 | 1240 | void init() |
| 1203 | 1241 | { |
| 1204 | 1242 | FileGallery::init(); |
| 1205 | - reader.setDevice(&f); | |
| 1206 | 1243 | } |
| 1207 | 1244 | }; |
| 1208 | 1245 | |
| ... | ... | @@ -1674,14 +1711,40 @@ BR_REGISTER(Gallery, vbbGallery) |
| 1674 | 1711 | void FileGallery::init() |
| 1675 | 1712 | { |
| 1676 | 1713 | f.setFileName(file); |
| 1677 | - QtUtils::touchDir(f); | |
| 1678 | - if (!f.open(QFile::ReadWrite)) | |
| 1679 | - qFatal("Failed to open %s for read/write.", qPrintable(file)); | |
| 1680 | - fileSize = f.size(); | |
| 1681 | 1714 | |
| 1682 | 1715 | Gallery::init(); |
| 1683 | 1716 | } |
| 1684 | 1717 | |
| 1718 | +void FileGallery::writeOpen() | |
| 1719 | +{ | |
| 1720 | + if (!f.isOpen() ) { | |
| 1721 | + QtUtils::touchDir(f); | |
| 1722 | + if (!f.open(QFile::WriteOnly)) | |
| 1723 | + qFatal("Failed to open %s for writing.", qPrintable(file)); | |
| 1724 | + } | |
| 1725 | +} | |
| 1726 | + | |
| 1727 | +bool FileGallery::readOpen() | |
| 1728 | +{ | |
| 1729 | + if (!f.isOpen() ) { | |
| 1730 | + if (!f.exists() ) { | |
| 1731 | + qFatal("File %s does not exist.", qPrintable(file)); | |
| 1732 | + } | |
| 1733 | + | |
| 1734 | + if (!f.open(QFile::ReadOnly)) | |
| 1735 | + qFatal("Failed to open %s for reading.", qPrintable(file)); | |
| 1736 | + return true; | |
| 1737 | + } | |
| 1738 | + return false; | |
| 1739 | +} | |
| 1740 | + | |
| 1741 | +qint64 FileGallery::totalSize() | |
| 1742 | +{ | |
| 1743 | + readOpen(); | |
| 1744 | + return f.size(); | |
| 1745 | +} | |
| 1746 | + | |
| 1747 | + | |
| 1685 | 1748 | } // namespace br |
| 1686 | 1749 | |
| 1687 | 1750 | #include "gallery.moc" | ... | ... |
openbr/plugins/openbr_internal.h
| ... | ... | @@ -464,14 +464,17 @@ class FileGallery : public Gallery |
| 464 | 464 | Q_OBJECT |
| 465 | 465 | public: |
| 466 | 466 | QFile f; |
| 467 | - qint64 fileSize; | |
| 468 | 467 | |
| 469 | 468 | virtual ~FileGallery() { f.close(); } |
| 470 | 469 | |
| 471 | 470 | void init(); |
| 472 | 471 | |
| 473 | - qint64 totalSize() { return fileSize; } | |
| 472 | + qint64 totalSize(); | |
| 474 | 473 | qint64 position() { return f.pos(); } |
| 474 | + | |
| 475 | + bool readOpen(); | |
| 476 | + void writeOpen(); | |
| 477 | + | |
| 475 | 478 | }; |
| 476 | 479 | |
| 477 | 480 | ... | ... |