Skip to content

Commit

Permalink
Use QIODevice instead of QFile
Browse files Browse the repository at this point in the history
  • Loading branch information
hluk committed Feb 19, 2017
1 parent e6db951 commit 80f20eb
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 41 deletions.
8 changes: 4 additions & 4 deletions plugins/itemencrypted/itemencrypted.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#include "item/serialize.h"

#include <QDir>
#include <QFile>
#include <QIODevice>
#include <QLabel>
#include <QTextEdit>
#include <QtPlugin>
Expand Down Expand Up @@ -310,7 +310,7 @@ QWidget *ItemEncryptedLoader::createSettingsWidget(QWidget *parent)
return w;
}

bool ItemEncryptedLoader::canLoadItems(QFile *file) const
bool ItemEncryptedLoader::canLoadItems(QIODevice *file) const
{
QDataStream stream(file);

Expand Down Expand Up @@ -348,7 +348,7 @@ bool ItemEncryptedLoader::canSaveItems(const QAbstractItemModel &model) const
return false;
}

bool ItemEncryptedLoader::loadItems(QAbstractItemModel *model, QFile *file)
bool ItemEncryptedLoader::loadItems(QAbstractItemModel *model, QIODevice *file)
{
// This is needed to skip header.
if ( !canLoadItems(file) )
Expand Down Expand Up @@ -420,7 +420,7 @@ bool ItemEncryptedLoader::loadItems(QAbstractItemModel *model, QFile *file)
return true;
}

bool ItemEncryptedLoader::saveItems(const QAbstractItemModel &model, QFile *file)
bool ItemEncryptedLoader::saveItems(const QAbstractItemModel &model, QIODevice *file)
{
if (m_gpgProcessStatus == GpgNotInstalled)
return false;
Expand Down
8 changes: 4 additions & 4 deletions plugins/itemencrypted/itemencrypted.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Ui {
class ItemEncryptedSettings;
}

class QFile;
class QIODevice;

class ItemEncrypted : public QWidget, public ItemWidget
{
Expand Down Expand Up @@ -75,13 +75,13 @@ class ItemEncryptedLoader : public QObject, public ItemLoaderInterface

virtual QWidget *createSettingsWidget(QWidget *parent);

virtual bool canLoadItems(QFile *file) const;
virtual bool canLoadItems(QIODevice *file) const;

virtual bool canSaveItems(const QAbstractItemModel &model) const;

virtual bool loadItems(QAbstractItemModel *model, QFile *file);
virtual bool loadItems(QAbstractItemModel *model, QIODevice *file);

virtual bool saveItems(const QAbstractItemModel &model, QFile *file);
virtual bool saveItems(const QAbstractItemModel &model, QIODevice *file);

virtual bool initializeTab(QAbstractItemModel *model);

Expand Down
10 changes: 5 additions & 5 deletions plugins/itemsync/itemsync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ bool readConfigHeader(QDataStream *stream)
return header == dataFileHeader;
}

bool readConfig(QFile *file, QVariantMap *config)
bool readConfig(QIODevice *file, QVariantMap *config)
{
QDataStream stream(file);
if ( !readConfigHeader(&stream) )
Expand All @@ -131,7 +131,7 @@ bool readConfig(QFile *file, QVariantMap *config)
&& config->value(configVersion, 0).toInt() == currentVersion;
}

void writeConfiguration(QFile *file, const QStringList &savedFiles)
void writeConfiguration(QIODevice *file, const QStringList &savedFiles)
{
QVariantMap config;
config.insert(configVersion, currentVersion);
Expand Down Expand Up @@ -1365,7 +1365,7 @@ QWidget *ItemSyncLoader::createSettingsWidget(QWidget *parent)
return w;
}

bool ItemSyncLoader::canLoadItems(QFile *file) const
bool ItemSyncLoader::canLoadItems(QIODevice *file) const
{
QDataStream stream(file);
return readConfigHeader(&stream);
Expand All @@ -1376,7 +1376,7 @@ bool ItemSyncLoader::canSaveItems(const QAbstractItemModel &model) const
return m_tabPaths.contains(model.property("tabName").toString());
}

bool ItemSyncLoader::loadItems(QAbstractItemModel *model, QFile *file)
bool ItemSyncLoader::loadItems(QAbstractItemModel *model, QIODevice *file)
{
QVariantMap config;
if ( !readConfig(file, &config) )
Expand All @@ -1386,7 +1386,7 @@ bool ItemSyncLoader::loadItems(QAbstractItemModel *model, QFile *file)
return loadItems(model, files);
}

bool ItemSyncLoader::saveItems(const QAbstractItemModel &model, QFile *file)
bool ItemSyncLoader::saveItems(const QAbstractItemModel &model, QIODevice *file)
{
FileWatcher *watcher = m_watchers.value(&model, nullptr);

Expand Down
6 changes: 3 additions & 3 deletions plugins/itemsync/itemsync.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,13 @@ class ItemSyncLoader : public QObject, public ItemLoaderInterface

virtual QWidget *createSettingsWidget(QWidget *parent);

virtual bool canLoadItems(QFile *file) const;
virtual bool canLoadItems(QIODevice *file) const;

virtual bool canSaveItems(const QAbstractItemModel &model) const;

virtual bool loadItems(QAbstractItemModel *model, QFile *file);
virtual bool loadItems(QAbstractItemModel *model, QIODevice *file);

virtual bool saveItems(const QAbstractItemModel &model, QFile *file);
virtual bool saveItems(const QAbstractItemModel &model, QIODevice *file);

virtual bool initializeTab(QAbstractItemModel *model);

Expand Down
14 changes: 5 additions & 9 deletions src/item/itemfactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

#include <QCoreApplication>
#include <QDir>
#include <QFile>
#include <QIODevice>
#include <QLabel>
#include <QModelIndex>
#include <QPluginLoader>
Expand Down Expand Up @@ -176,27 +176,23 @@ class DummyLoader : public ItemLoaderInterface
return new DummyItem(index, parent, preview);
}

bool canLoadItems(QFile *) const override { return true; }
bool canLoadItems(QIODevice *) const override { return true; }

bool canSaveItems(const QAbstractItemModel &) const override { return true; }

bool loadItems(QAbstractItemModel *model, QFile *file) override
bool loadItems(QAbstractItemModel *model, QIODevice *file) override
{
if ( file->size() > 0 ) {
if ( !deserializeData(model, file) ) {
model->removeRows(0, model->rowCount());
const QString errorString =
QObject::tr("Item file %1 is corrupted or some CopyQ plugins are missing!")
.arg(quoteString(file->fileName()) );
m_factory->emitError(errorString);
return false;
}
}

return true;
}

bool saveItems(const QAbstractItemModel &model, QFile *file) override
bool saveItems(const QAbstractItemModel &model, QIODevice *file) override
{
return serializeData(model, file);
}
Expand Down Expand Up @@ -324,7 +320,7 @@ bool ItemFactory::isLoaderEnabled(const ItemLoaderInterface *loader) const
return !m_disabledLoaders.contains(loader);
}

ItemLoaderInterface *ItemFactory::loadItems(QAbstractItemModel *model, QFile *file)
ItemLoaderInterface *ItemFactory::loadItems(QAbstractItemModel *model, QIODevice *file)
{
for ( auto loader : enabledLoaders() ) {
file->seek(0);
Expand Down
4 changes: 2 additions & 2 deletions src/item/itemfactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
class ItemLoaderInterface;
class ItemWidget;
class QAbstractItemModel;
class QFile;
class QIODevice;
class QModelIndex;
class QWidget;
struct Command;
Expand Down Expand Up @@ -116,7 +116,7 @@ class ItemFactory : public QObject
* Load items using a plugin.
* @return true only if any plugin (ItemLoaderInterface::loadItems()) returned true
*/
ItemLoaderInterface *loadItems(QAbstractItemModel *model, QFile *file);
ItemLoaderInterface *loadItems(QAbstractItemModel *model, QIODevice *file);

/**
* Initialize tab.
Expand Down
5 changes: 4 additions & 1 deletion src/item/itemstore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ ItemLoaderInterface *loadItems(

ItemLoaderInterface *loader = itemFactory->loadItems(&model, &tabFile);
if (!loader) {
log( QString("Tab \"%1\": Failed to load items"), LogError );
const QString errorString =
QObject::tr("Item file %1 is corrupted or some CopyQ plugins are missing!")
.arg( quoteString(tabFileName) );
itemFactory->emitError(errorString);
return nullptr;
}

Expand Down
6 changes: 3 additions & 3 deletions src/item/itemwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ ItemWidget *ItemLoaderInterface::create(const QModelIndex &, QWidget *, bool) co
return nullptr;
}

bool ItemLoaderInterface::canLoadItems(QFile *) const
bool ItemLoaderInterface::canLoadItems(QIODevice *) const
{
return false;
}
Expand All @@ -274,12 +274,12 @@ bool ItemLoaderInterface::canSaveItems(const QAbstractItemModel &) const
return false;
}

bool ItemLoaderInterface::loadItems(QAbstractItemModel *, QFile *)
bool ItemLoaderInterface::loadItems(QAbstractItemModel *, QIODevice *)
{
return false;
}

bool ItemLoaderInterface::saveItems(const QAbstractItemModel &, QFile *)
bool ItemLoaderInterface::saveItems(const QAbstractItemModel &, QIODevice *)
{
return false;
}
Expand Down
8 changes: 4 additions & 4 deletions src/item/itemwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

class QAbstractItemModel;
class QTextEdit;
class QFile;
class QIODevice;
class QFont;
class QModelIndex;
class QPalette;
Expand Down Expand Up @@ -225,7 +225,7 @@ class ItemLoaderInterface
/**
* @return true only if items can be loaded
*/
virtual bool canLoadItems(QFile *file) const;
virtual bool canLoadItems(QIODevice *file) const;

/**
* @return true only if items can be saved
Expand All @@ -236,13 +236,13 @@ class ItemLoaderInterface
* Load items.
* @return true only if items were saved by this plugin (or just not to load them any further)
*/
virtual bool loadItems(QAbstractItemModel *model, QFile *file);
virtual bool loadItems(QAbstractItemModel *model, QIODevice *file);

/**
* Save items.
* @return true only if items were saved
*/
virtual bool saveItems(const QAbstractItemModel &model, QFile *file);
virtual bool saveItems(const QAbstractItemModel &model, QIODevice *file);

/**
* Initialize tab (tab was not yet saved or loaded).
Expand Down
6 changes: 3 additions & 3 deletions src/item/serialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include <QAbstractItemModel>
#include <QByteArray>
#include <QDataStream>
#include <QFile>
#include <QIODevice>
#include <QList>
#include <QObject>
#include <QPair>
Expand Down Expand Up @@ -232,14 +232,14 @@ bool deserializeData(QAbstractItemModel *model, QDataStream *stream)
return stream->status() == QDataStream::Ok;
}

bool serializeData(const QAbstractItemModel &model, QFile *file)
bool serializeData(const QAbstractItemModel &model, QIODevice *file)
{
QDataStream stream(file);
stream.setVersion(QDataStream::Qt_4_7);
return serializeData(model, &stream);
}

bool deserializeData(QAbstractItemModel *model, QFile *file)
bool deserializeData(QAbstractItemModel *model, QIODevice *file)
{
QDataStream stream(file);
return deserializeData(model, &stream);
Expand Down
6 changes: 3 additions & 3 deletions src/item/serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
class QAbstractItemModel;
class QByteArray;
class QDataStream;
class QFile;
class QIODevice;

void serializeData(QDataStream *out, const QVariantMap &data);
void deserializeData(QDataStream *stream, QVariantMap *data);
Expand All @@ -34,7 +34,7 @@ bool deserializeData(QVariantMap *data, const QByteArray &bytes);

bool serializeData(const QAbstractItemModel &model, QDataStream *stream);
bool deserializeData(QAbstractItemModel *model, QDataStream *stream);
bool serializeData(const QAbstractItemModel &model, QFile *file);
bool deserializeData(QAbstractItemModel *model, QFile *file);
bool serializeData(const QAbstractItemModel &model, QIODevice *file);
bool deserializeData(QAbstractItemModel *model, QIODevice *file);

#endif // SERIALIZE_H

0 comments on commit 80f20eb

Please sign in to comment.