Commit 8f742c2c1c4d0bb96c4cdbf73451780f4408a419

Authored by Charles Otto
1 parent e095c645

Defer opening gallery files until read/write are first called

Don't open gallery files in init, instead defer opening until they are
first attempted to be read from/written to.

This instead of introducing read/write modes.
openbr/plugins/gallery.cpp
@@ -124,23 +124,51 @@ class BinaryGallery : public Gallery @@ -124,23 +124,51 @@ class BinaryGallery : public Gallery
124 124
125 gallery.open(stderr, QFile::WriteOnly); 125 gallery.open(stderr, QFile::WriteOnly);
126 } else { 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 gallery.setFileName(file); 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 if (file.get<bool>("remove")) 154 if (file.get<bool>("remove"))
129 gallery.remove(); 155 gallery.remove();
130 QtUtils::touchDir(gallery); 156 QtUtils::touchDir(gallery);
131 - QFile::OpenMode mode = QFile::ReadWrite; 157 + QFile::OpenMode mode = QFile::WriteOnly;
132 158
  159 + // Do we append?
133 if (file.get<bool>("append")) 160 if (file.get<bool>("append"))
134 mode |= QFile::Append; 161 mode |= QFile::Append;
135 162
136 if (!gallery.open(mode)) 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 TemplateList readBlock(bool *done) 169 TemplateList readBlock(bool *done)
143 { 170 {
  171 + readOpen();
144 if (gallery.atEnd()) 172 if (gallery.atEnd())
145 gallery.seek(0); 173 gallery.seek(0);
146 174
@@ -163,6 +191,7 @@ class BinaryGallery : public Gallery @@ -163,6 +191,7 @@ class BinaryGallery : public Gallery
163 191
164 void write(const Template &t) 192 void write(const Template &t)
165 { 193 {
  194 + writeOpen();
166 writeTemplate(t); 195 writeTemplate(t);
167 if (gallery.isSequential()) 196 if (gallery.isSequential())
168 gallery.flush(); 197 gallery.flush();
@@ -174,6 +203,7 @@ protected: @@ -174,6 +203,7 @@ protected:
174 203
175 qint64 totalSize() 204 qint64 totalSize()
176 { 205 {
  206 + readOpen();
177 return gallery.size(); 207 return gallery.size();
178 } 208 }
179 209
@@ -879,6 +909,7 @@ class csvGallery : public FileGallery @@ -879,6 +909,7 @@ class csvGallery : public FileGallery
879 909
880 TemplateList readBlock(bool *done) 910 TemplateList readBlock(bool *done)
881 { 911 {
  912 + readOpen();
882 *done = false; 913 *done = false;
883 TemplateList templates; 914 TemplateList templates;
884 if (!file.exists()) { 915 if (!file.exists()) {
@@ -971,6 +1002,7 @@ class txtGallery : public FileGallery @@ -971,6 +1002,7 @@ class txtGallery : public FileGallery
971 1002
972 TemplateList readBlock(bool *done) 1003 TemplateList readBlock(bool *done)
973 { 1004 {
  1005 + readOpen();
974 *done = false; 1006 *done = false;
975 if (f.atEnd()) 1007 if (f.atEnd())
976 f.seek(0); 1008 f.seek(0);
@@ -1000,6 +1032,7 @@ class txtGallery : public FileGallery @@ -1000,6 +1032,7 @@ class txtGallery : public FileGallery
1000 1032
1001 void write(const Template &t) 1033 void write(const Template &t)
1002 { 1034 {
  1035 + writeOpen();
1003 QString line = t.file.name; 1036 QString line = t.file.name;
1004 if (!label.isEmpty()) 1037 if (!label.isEmpty())
1005 line += " " + t.file.get<QString>(label); 1038 line += " " + t.file.get<QString>(label);
@@ -1021,6 +1054,7 @@ class flatGallery : public FileGallery @@ -1021,6 +1054,7 @@ class flatGallery : public FileGallery
1021 1054
1022 TemplateList readBlock(bool *done) 1055 TemplateList readBlock(bool *done)
1023 { 1056 {
  1057 + readOpen();
1024 *done = false; 1058 *done = false;
1025 if (f.atEnd()) 1059 if (f.atEnd())
1026 f.seek(0); 1060 f.seek(0);
@@ -1047,6 +1081,7 @@ class flatGallery : public FileGallery @@ -1047,6 +1081,7 @@ class flatGallery : public FileGallery
1047 1081
1048 void write(const Template &t) 1082 void write(const Template &t)
1049 { 1083 {
  1084 + writeOpen();
1050 f.write((t.file.flat()+"\n").toLocal8Bit() ); 1085 f.write((t.file.flat()+"\n").toLocal8Bit() );
1051 } 1086 }
1052 }; 1087 };
@@ -1079,6 +1114,9 @@ class xmlGallery : public FileGallery @@ -1079,6 +1114,9 @@ class xmlGallery : public FileGallery
1079 1114
1080 TemplateList readBlock(bool *done) 1115 TemplateList readBlock(bool *done)
1081 { 1116 {
  1117 + if (readOpen())
  1118 + reader.setDevice(&f);
  1119 +
1082 if (reader.atEnd()) 1120 if (reader.atEnd())
1083 f.seek(0); 1121 f.seek(0);
1084 1122
@@ -1202,7 +1240,6 @@ class xmlGallery : public FileGallery @@ -1202,7 +1240,6 @@ class xmlGallery : public FileGallery
1202 void init() 1240 void init()
1203 { 1241 {
1204 FileGallery::init(); 1242 FileGallery::init();
1205 - reader.setDevice(&f);  
1206 } 1243 }
1207 }; 1244 };
1208 1245
@@ -1674,14 +1711,40 @@ BR_REGISTER(Gallery, vbbGallery) @@ -1674,14 +1711,40 @@ BR_REGISTER(Gallery, vbbGallery)
1674 void FileGallery::init() 1711 void FileGallery::init()
1675 { 1712 {
1676 f.setFileName(file); 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 Gallery::init(); 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 } // namespace br 1748 } // namespace br
1686 1749
1687 #include "gallery.moc" 1750 #include "gallery.moc"
openbr/plugins/openbr_internal.h
@@ -464,14 +464,17 @@ class FileGallery : public Gallery @@ -464,14 +464,17 @@ class FileGallery : public Gallery
464 Q_OBJECT 464 Q_OBJECT
465 public: 465 public:
466 QFile f; 466 QFile f;
467 - qint64 fileSize;  
468 467
469 virtual ~FileGallery() { f.close(); } 468 virtual ~FileGallery() { f.close(); }
470 469
471 void init(); 470 void init();
472 471
473 - qint64 totalSize() { return fileSize; } 472 + qint64 totalSize();
474 qint64 position() { return f.pos(); } 473 qint64 position() { return f.pos(); }
  474 +
  475 + bool readOpen();
  476 + void writeOpen();
  477 +
475 }; 478 };
476 479
477 480