a?adido el codigo del tree model

modificada la funci?n de creaci?n de la base de datos (se crea la estructura de directorios, los c?mics y comic_info)

falta determinar qu? informaci?n habr? finalmente en la BD y tratar adecuadamente los errores
adem?s habr?a que mover el c?digo relacionado con la base de datos a la clase DataBaseManagement.
This commit is contained in:
Luis Ángel San Martín 2012-05-24 17:10:28 +02:00
parent 51a0f1b458
commit 3533a9ec64
10 changed files with 1013 additions and 352 deletions

View File

@ -7,7 +7,8 @@ TARGET =
DEPENDPATH += . DEPENDPATH += .
INCLUDEPATH += . INCLUDEPATH += .
INCLUDEPATH += ../common \ INCLUDEPATH += ../common \
./server ./server \
./db
CONFIG += release CONFIG += release
CONFIG -= flat CONFIG -= flat
QT += sql network QT += sql network
@ -27,8 +28,10 @@ HEADERS += comic_flow.h \
import_library_dialog.h \ import_library_dialog.h \
package_manager.h \ package_manager.h \
../common/qnaturalsorting.h \ ../common/qnaturalsorting.h \
data_base_management.h \ bundle_creator.h \
bundle_creator.h ./db/data_base_management.h \
./db/treeitem.h \
./db/treemodel.h
SOURCES += comic_flow.cpp \ SOURCES += comic_flow.cpp \
create_library_dialog.cpp \ create_library_dialog.cpp \
library_creator.cpp \ library_creator.cpp \
@ -44,8 +47,10 @@ SOURCES += comic_flow.cpp \
import_library_dialog.cpp \ import_library_dialog.cpp \
package_manager.cpp \ package_manager.cpp \
../common/qnaturalsorting.cpp \ ../common/qnaturalsorting.cpp \
data_base_management.cpp \ bundle_creator.cpp \
bundle_creator.cpp ./db/data_base_management.cpp \
./db/treeitem.cpp \
./db/treemodel.cpp
include(./server/server.pri) include(./server/server.pri)

View File

@ -1,22 +0,0 @@
#include "data_base_management.h"
#include <QtCore>
DataBaseManagement::DataBaseManagement()
:QObject(),dataBasesList()
{
}
bool DataBaseManagement::createDataBase(QString name, QString path)
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(QDir::cleanPath(path) + "/" + name + ".ydb");
if (!db.open())
qDebug() << db.lastError();
else {
qDebug() << db.tables();
db.close();
}
return true;
}

View File

@ -0,0 +1,52 @@
#include "data_base_management.h"
#include <QtCore>
DataBaseManagement::DataBaseManagement()
:QObject(),dataBasesList()
{
}
TreeModel * DataBaseManagement::newTreeModel(QString path)
{
//la consulta se ejecuta...
QSqlQuery selectQuery(loadDatabase(path));
selectQuery.setForwardOnly(true);
selectQuery.exec("select * from folder order by parentId,name");
return new TreeModel(selectQuery);
}
QSqlDatabase DataBaseManagement::createDatabase(QString name, QString path)
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(QDir::cleanPath(path) + "/" + name + ".ydb");
if (!db.open())
qDebug() << db.lastError();
else {
qDebug() << db.tables();
db.close();
}
return db;
}
QSqlDatabase DataBaseManagement::loadDatabase(QString path)
{
//TODO check path
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(path);
if (!db.open()) {
/*QMessageBox::critical( 0, QObject::tr("Cannot open database"),
QObject::tr("Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it.\n\n"
"Click Cancel to exit."), QMessageBox::Cancel);*/
//se devuelve una base de datos vacía e inválida
return QSqlDatabase();
}
//devuelve la base de datos
return db;
}

View File

@ -3,6 +3,9 @@
#include <QtCore> #include <QtCore>
#include <QtSql> #include <QtSql>
#include <QSqlDatabase>
#include "treemodel.h"
class DataBaseManagement : public QObject class DataBaseManagement : public QObject
{ {
@ -11,8 +14,9 @@ private:
QList<QString> dataBasesList; QList<QString> dataBasesList;
public: public:
DataBaseManagement(); DataBaseManagement();
bool createDataBase(QString name, QString path); TreeModel * newTreeModel(QString path);
QSqlDatabase createDatabase(QString name, QString path);
QSqlDatabase loadDatabase(QString path);
}; };
#endif #endif

View File

@ -0,0 +1,118 @@
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
** the names of its contributors may be used to endorse or promote
** products derived from this software without specific prior written
** permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
** $QT_END_LICENSE$
**
****************************************************************************/
/*
treeitem.cpp
A container for items of data supplied by the simple tree model.
*/
#include <QStringList>
#include "treeitem.h"
//! [0]
TreeItem::TreeItem(const QList<QVariant> &data, TreeItem *parent)
{
parentItem = parent;
itemData = data;
}
//! [0]
//! [1]
TreeItem::~TreeItem()
{
qDeleteAll(childItems);
}
//! [1]
//! [2]
void TreeItem::appendChild(TreeItem *item)
{
item->parentItem = this;
//TODO insertar odernado
childItems.append(item);
}
//! [2]
//! [3]
TreeItem *TreeItem::child(int row)
{
return childItems.value(row);
}
//! [3]
//! [4]
int TreeItem::childCount() const
{
return childItems.count();
}
//! [4]
//! [5]
int TreeItem::columnCount() const
{
return itemData.count();
}
//! [5]
//! [6]
QVariant TreeItem::data(int column) const
{
return itemData.value(column);
}
//! [6]
//! [7]
TreeItem *TreeItem::parent()
{
return parentItem;
}
//! [7]
//! [8]
int TreeItem::row() const
{
if (parentItem)
return parentItem->childItems.indexOf(const_cast<TreeItem*>(this));
return 0;
}
//! [8]

View File

@ -0,0 +1,72 @@
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
** the names of its contributors may be used to endorse or promote
** products derived from this software without specific prior written
** permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef TREEITEM_H
#define TREEITEM_H
#include <QList>
#include <QVariant>
//! [0]
class TreeItem
{
public:
TreeItem(const QList<QVariant> &data, TreeItem *parent = 0);
~TreeItem();
void appendChild(TreeItem *child);
TreeItem *child(int row);
int childCount() const;
int columnCount() const;
QVariant data(int column) const;
int row() const;
TreeItem *parent();
TreeItem *parentItem;
unsigned long long int id;
private:
QList<TreeItem*> childItems;
QList<QVariant> itemData;
};
//! [0]
#endif

View File

@ -0,0 +1,192 @@
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
** the names of its contributors may be used to endorse or promote
** products derived from this software without specific prior written
** permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
** $QT_END_LICENSE$
**
****************************************************************************/
/*
treemodel.cpp
Provides a simple tree model to show how to create and use hierarchical
models.
*/
#include <QtGui>
#include "treeitem.h"
#include "treemodel.h"
//! [0]
TreeModel::TreeModel( QSqlQuery &sqlquery, QObject *parent)
: QAbstractItemModel(parent)
{
//lo más probable es que el nodo raíz no necesite tener información
QList<QVariant> rootData;
rootData << "root"; //id 0, padre 0, title "root" (el id, y el id del padre van a ir en la clase TreeItem)
rootItem = new TreeItem(rootData);
rootItem->id = 0;
setupModelData(sqlquery, rootItem);
}
//! [0]
//! [1]
TreeModel::~TreeModel()
{
delete rootItem;
}
//! [1]
//! [2]
int TreeModel::columnCount(const QModelIndex &parent) const
{
if (parent.isValid())
return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
else
return rootItem->columnCount();
}
//! [2]
//! [3]
QVariant TreeModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role != Qt::DisplayRole)
return QVariant();
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
return item->data(index.column());
}
//! [3]
//! [4]
Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return 0;
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
//! [4]
//! [5]
QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
return rootItem->data(section);
return QVariant();
}
//! [5]
//! [6]
QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
TreeItem *parentItem;
if (!parent.isValid())
parentItem = rootItem;
else
parentItem = static_cast<TreeItem*>(parent.internalPointer());
TreeItem *childItem = parentItem->child(row);
if (childItem)
return createIndex(row, column, childItem);
else
return QModelIndex();
}
//! [6]
//! [7]
QModelIndex TreeModel::parent(const QModelIndex &index) const
{
if (!index.isValid())
return QModelIndex();
TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
TreeItem *parentItem = childItem->parent();
if (parentItem == rootItem)
return QModelIndex();
return createIndex(parentItem->row(), 0, parentItem);
}
//! [7]
//! [8]
int TreeModel::rowCount(const QModelIndex &parent) const
{
TreeItem *parentItem;
if (parent.column() > 0)
return 0;
if (!parent.isValid())
parentItem = rootItem;
else
parentItem = static_cast<TreeItem*>(parent.internalPointer());
return parentItem->childCount();
}
//! [8]
void TreeModel::setupModelData(QSqlQuery &sqlquery, TreeItem *parent)
{
//64 bits para la primary key, es decir la misma precisión que soporta sqlit 2^64
//el diccionario permitirá encontrar cualquier nodo del árbol rápidamente, de forma que añadir un hijo a un padre sea O(1)
QMap<unsigned long long int, TreeItem *> items;
//se añade el nodo 0
items.insert(parent->id,parent);
while (sqlquery.next()) {
QList<QVariant> data;
data << sqlquery.value(2).toString();
TreeItem * item = new TreeItem(data);
item->id = sqlquery.value(0).toLongLong();
items.value(sqlquery.value(1).toLongLong())->appendChild(item);
//se añade el item al map, de forma que se pueda encontrar como padre en siguientes iteraciones
items.insert(item->id,item);
}
}

View File

@ -0,0 +1,77 @@
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
** the names of its contributors may be used to endorse or promote
** products derived from this software without specific prior written
** permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef TREEMODEL_H
#define TREEMODEL_H
#include <QAbstractItemModel>
#include <QModelIndex>
#include <QVariant>
#include <QSqlQuery>
class TreeItem;
//! [0]
class TreeModel : public QAbstractItemModel
{
Q_OBJECT
public:
TreeModel( QSqlQuery &sqlquery, QObject *parent = 0);
~TreeModel();
QVariant data(const QModelIndex &index, int role) const;
Qt::ItemFlags flags(const QModelIndex &index) const;
QVariant headerData(int section, Qt::Orientation orientation,
int role = Qt::DisplayRole) const;
QModelIndex index(int row, int column,
const QModelIndex &parent = QModelIndex()) const;
QModelIndex parent(const QModelIndex &index) const;
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
private:
void setupModelData( QSqlQuery &sqlquery, TreeItem *parent);
TreeItem *rootItem; //el árbol
};
//! [0]
#endif

View File

@ -1,323 +1,475 @@
#include "library_creator.h" #include "library_creator.h"
#include "custom_widgets.h" #include "custom_widgets.h"
#include <QMutex> #include <QMutex>
#include <QDebug> #include <QDebug>
#include <QSqlQuery>
//QMutex mutex; //QMutex mutex;
/*int numThreads = 0; /*int numThreads = 0;
QWaitCondition waitCondition; QWaitCondition waitCondition;
QMutex mutex; QMutex mutex;
*/ */
LibraryCreator::LibraryCreator()
{ class Folder
_nameFilter << "*.cbr" << "*.cbz" << "*.rar" << "*.zip" << "*.tar"; {
} public:
bool knownParent;
void LibraryCreator::createLibrary(const QString &source, const QString &target) bool knownId;
{ unsigned long long int id;
_source = source; unsigned long long int parentId;
_target = target; QString name;
if(!QDir(target).exists()) QString path;
_mode = CREATOR;
else Folder():knownParent(false), knownId(false){};
_mode = UPDATER; Folder(unsigned long long int sid, unsigned long long int pid,QString fn, QString fp):id(sid), parentId(pid),name(fn),path(fp),knownParent(true), knownId(true){};
} Folder(QString fn, QString fp):name(fn),path(fp),knownParent(false), knownId(false){};
void setId(unsigned long long int sid){id = sid;knownId = true;};
void LibraryCreator::updateLibrary(const QString &source, const QString &target) void setFather(unsigned long long int pid){parentId = pid;knownParent = true;};
{ };
_source = source;
_target = target; class Comic
_mode = UPDATER; {
} public:
unsigned long long int comicInfoId;
void LibraryCreator::run() unsigned long long int parentId;
{ QString name;
stopRunning = false; QString path;
if(_mode == CREATOR) QString hash;
create(QDir(_source));
else Comic(){};
update(QDir(_source),QDir(_target)); Comic(unsigned long long int cparentId, unsigned long long int ccomicInfoId, QString cname, QString cpath, QString chash)
emit(finished()); :parentId(cparentId),comicInfoId(ccomicInfoId),name(cname),path(cpath),hash(chash){};
} //Comic(QString fn, QString fp):name(fn),path(fp),knownParent(false), knownId(false){};
};
void LibraryCreator::stop()
{ LibraryCreator::LibraryCreator()
stopRunning = true; {
} _nameFilter << "*.cbr" << "*.cbz" << "*.rar" << "*.zip" << "*.tar";
}
void LibraryCreator::create(QDir dir)
{ void LibraryCreator::createLibrary(const QString &source, const QString &target)
dir.setNameFilters(_nameFilter); {
dir.setFilter(QDir::AllDirs|QDir::Files|QDir::NoDotAndDotDot); _source = source;
QFileInfoList list = dir.entryInfoList(); _target = target;
for (int i = 0; i < list.size(); ++i) if(!QDir(target+"/library.ydb").exists())
{ _mode = CREATOR;
if(stopRunning) else
return; _mode = UPDATER;
QFileInfo fileInfo = list.at(i); }
if(fileInfo.isDir())
{ void LibraryCreator::updateLibrary(const QString &source, const QString &target)
create(QDir(fileInfo.absoluteFilePath())); {
} _source = source;
else _target = target;
{ _mode = UPDATER;
dir.mkpath(_target+(QDir::cleanPath(fileInfo.absolutePath()).remove(_source))); }
emit(coverExtracted(QDir::cleanPath(fileInfo.absoluteFilePath()).remove(_source)));
bool LibraryCreator::createTables()
ThumbnailCreator tc(QDir::cleanPath(fileInfo.absoluteFilePath()),_target+(QDir::cleanPath(fileInfo.absoluteFilePath()).remove(_source))+".jpg"); {
tc.create(); bool success = true;
}
} //FOLDER (representa una carpeta en disco)
} QSqlQuery queryFolder(_database);
queryFolder.prepare("CREATE TABLE folder (id INTEGER PRIMARY KEY, parentId INTEGER NOT NULL, name TEXT NOT NULL, path TEXT NOT NULL, FOREIGN KEY(parentId) REFERENCES folder(id))");
void LibraryCreator::update(QDir dirS,QDir dirD) success = success && queryFolder.exec();
{
dirS.setNameFilters(_nameFilter); //COMIC INFO (representa la información de un cómic, cada cómic tendrá un idéntificador único formado por un hash sha1'de los primeros 512kb' + su tamaño en bytes)
dirS.setFilter(QDir::AllDirs|QDir::Files|QDir::NoDotAndDotDot); QSqlQuery queryComicInfo(_database);
dirS.setSorting(QDir::Name|QDir::IgnoreCase|QDir::LocaleAware|QDir::DirsFirst); queryComicInfo.prepare("CREATE TABLE comic_info (id INTEGER PRIMARY KEY, hash TEXT NOT NULL, name TEXT)");
QFileInfoList listS = dirS.entryInfoList(); success = success && queryComicInfo.exec();
dirD.setNameFilters(QStringList()<<"*.jpg"); //COMIC (representa un cómic en disco, contiene el nombre de fichero)
dirD.setFilter(QDir::AllDirs|QDir::Files|QDir::NoDotAndDotDot); QSqlQuery queryComic(_database);
dirD.setSorting(QDir::Name|QDir::IgnoreCase|QDir::LocaleAware|QDir::DirsFirst); queryComic.prepare("CREATE TABLE comic (id INTEGER PRIMARY KEY, parentId INTEGER NOT NULL, comicInfoId INTEGER NOT NULL, fileName TEXT NOT NULL, path TEXT, FOREIGN KEY(parentId) REFERENCES folder(id), FOREIGN KEY(comicInfoId) REFERENCES comic_info(id))");
QFileInfoList listD = dirD.entryInfoList(); success = success && queryComic.exec();
int lenghtS = listS.size(); return success;
int lenghtD = listD.size(); }
int maxLenght = qMax(lenghtS,lenghtD); //
void LibraryCreator::run()
bool updated; {
int i,j; stopRunning = false;
for (i=0,j=0; (i < lenghtS)||(j < lenghtD);)
{ if(_mode == CREATOR)
if(stopRunning) {
return; QDir dir;
updated = false; dir.mkpath(_target+"/covers");
if(i>=lenghtS) //finished source files/dirs _database = QSqlDatabase::addDatabase("QSQLITE");
{ _database.setDatabaseName(_target+"/library.ydb");
//delete listD //from j if(!_database.open())
for(;j<lenghtD;j++) return; //TODO avisar del problema
{ _currentPathFolders.clear();
if(stopRunning) _currentPathFolders.append(Folder(0,0,"root","/"));
return; if(!createTables())
QFileInfo fileInfoD = listD.at(j); return;
if(fileInfoD.isDir()) create(QDir(_source));
{ _database.commit();
delTree(QDir(fileInfoD.absoluteFilePath())); _database.close();
dirD.rmdir(fileInfoD.absoluteFilePath()); }
} else
else {
dirD.remove(fileInfoD.absoluteFilePath()); _currentPathFolders.clear();
} _currentPathFolders.append(Folder(0,0,"root","/"));
updated = true; _database = QSqlDatabase::addDatabase("QSQLITE");
} _database.setDatabaseName(_target+"/library.ydb");
if(j>=lenghtD) //finished library files/dirs if(!_database.open())
{ return; //TODO avisar del problema
//create listS //from i update(QDir(_source),QDir(_target));
for(;i<lenghtS;i++) }
{ emit(finished());
if(stopRunning) }
return;
QFileInfo fileInfoS = listS.at(i); void LibraryCreator::stop()
if(fileInfoS.isDir()) {
create(QDir(fileInfoS.absoluteFilePath())); stopRunning = true;
else }
{
dirD.mkpath(_target+(QDir::cleanPath(fileInfoS.absolutePath()).remove(_source))); //retorna el id del ultimo de los folders
emit(coverExtracted(QDir::cleanPath(fileInfoS.absoluteFilePath()).remove(_source))); unsigned long long int LibraryCreator::insertFolders()
ThumbnailCreator tc(QDir::cleanPath(fileInfoS.absoluteFilePath()),_target+(QDir::cleanPath(fileInfoS.absoluteFilePath()).remove(_source))+".jpg"); {
tc.create(); QList<Folder>::iterator i;
} int currentId = 0;
} for (i = _currentPathFolders.begin(); i != _currentPathFolders.end(); ++i)
updated = true; {
} if(!(i->knownId))
if(!updated) {
{ i->setFather(currentId);
QFileInfo fileInfoS = listS.at(i); currentId = insertFolder(currentId,*i);
QFileInfo fileInfoD = listD.at(j); i->setId(currentId);
QString nameS = QDir::cleanPath(fileInfoS.absoluteFilePath()).remove(QDir::cleanPath(fileInfoS.absolutePath())); //remove source }
QString nameD = QDir::cleanPath(fileInfoD.absoluteFilePath()).remove(QDir::cleanPath(fileInfoD.absolutePath())); //remove target else
{
int comparation = QString::localeAwareCompare(nameS,nameD); currentId = i->id;
if(fileInfoS.isDir()&&fileInfoD.isDir()) }
if(comparation == 0)//same folder, update }
{ return 0;
update(QDir(fileInfoS.absoluteFilePath()),QDir(fileInfoD.absoluteFilePath())); }
i++;
j++; unsigned long long int LibraryCreator::insertFolder(unsigned long long int parentId,const Folder & folder)
} {
else QSqlQuery query(_database);
if(comparation < 0) //nameS doesn't exist on Target folder... query.prepare("INSERT INTO folder (parentId, name, path) "
{ "VALUES (:parentId, :name, :path)");
if(nameS!="/.yacreaderlibrary") query.bindValue(":parentId", parentId);
create(QDir(fileInfoS.absoluteFilePath())); query.bindValue(":name", folder.name);
i++; query.bindValue(":path", folder.path);
} query.exec();
else //nameD no longer avaliable on Source folder... return query.lastInsertId().toLongLong();
{ }
if(nameS!="/.yacreaderlibrary")
{ unsigned long long int LibraryCreator::insertComic(const Comic & comic)
delTree(QDir(fileInfoD.absoluteFilePath())); {
dirD.rmdir(fileInfoD.absoluteFilePath()); //TODO comprobar si ya hay comic info con ese hash
j++; QSqlQuery comicInfoInsert(_database);
} comicInfoInsert.prepare("INSERT INTO comic_info (hash) "
else "VALUES (:hash)");
i++; //skip library directory comicInfoInsert.bindValue(":hash", comic.hash);
} comicInfoInsert.exec();
else // one of them(or both) is a file unsigned long long int comicInfoId =comicInfoInsert.lastInsertId().toLongLong();
if(fileInfoS.isDir()) //this folder doesn't exist on library
{ QSqlQuery query(_database);
if(nameS!="/.yacreaderlibrary") //skip .yacreaderlibrary folder query.prepare("INSERT INTO comic (parentId, comicInfoId, fileName, path) "
create(QDir(fileInfoS.absoluteFilePath())); "VALUES (:parentId,:comicInfoId,:name, :path)");
i++; query.bindValue(":parentId", comic.parentId);
} query.bindValue(":comicInfoId", comicInfoId);
else query.bindValue(":name", comic.name);
if(fileInfoD.isDir()) //delete this folder from library query.bindValue(":path", comic.path);
{ query.exec();
delTree(QDir(fileInfoD.absoluteFilePath())); return query.lastInsertId().toLongLong();
dirD.rmdir(fileInfoD.absoluteFilePath()); }
j++;
} void LibraryCreator::create(QDir dir)
else //both are files //BUG on windows (no case sensitive) {
{ dir.setNameFilters(_nameFilter);
nameD.remove(nameD.size()-4,4); dir.setFilter(QDir::AllDirs|QDir::Files|QDir::NoDotAndDotDot);
int comparation = QString::localeAwareCompare(nameS,nameD); QFileInfoList list = dir.entryInfoList();
if(comparation < 0) //create new thumbnail for (int i = 0; i < list.size(); ++i)
{ {
dirD.mkpath(_target+(QDir::cleanPath(fileInfoS.absolutePath()).remove(_source))); if(stopRunning)
emit(coverExtracted(QDir::cleanPath(fileInfoS.absoluteFilePath()).remove(_source))); return;
ThumbnailCreator tc(QDir::cleanPath(fileInfoS.absoluteFilePath()),_target+(QDir::cleanPath(fileInfoS.absoluteFilePath()).remove(_source))+".jpg"); QFileInfo fileInfo = list.at(i);
tc.create(); QString fileName = fileInfo.fileName();
i++; QString relativePath = QDir::cleanPath(fileInfo.absoluteFilePath()).remove(_source);
} if(fileInfo.isDir())
else {
{ //se añade al path actual el folder, aún no se sabe si habrá que añadirlo a la base de datos
if(comparation > 0) //delete thumbnail _currentPathFolders.append(Folder(fileInfo.fileName(),relativePath));
{ create(QDir(fileInfo.absoluteFilePath()));
dirD.remove(fileInfoD.absoluteFilePath()); //una vez importada la información del folder, se retira del path actual ya que no volverá a ser visitado
QString tick = fileInfoD.absoluteFilePath(); _currentPathFolders.pop_back();
dirD.remove(tick.remove(tick.size()-3,3)); }
dirD.remove(tick+"r"); else
j++; {
} //dir.mkpath(_target+(QDir::cleanPath(fileInfo.absolutePath()).remove(_source)));
else //same file
{ //en este punto sabemos que todos los folders que hay en _currentPath, deberían estar añadidos a la base de datos
if(fileInfoS.isFile() && fileInfoD.isFile()) insertFolders();
{ emit(coverExtracted(relativePath));
if(fileInfoS.lastModified()>fileInfoD.lastModified())
{ //Se calcula el hash del cómic
dirD.mkpath(_target+(QDir::cleanPath(fileInfoS.absolutePath()).remove(_source)));
emit(coverExtracted(QDir::cleanPath(fileInfoS.absoluteFilePath()).remove(_source))); QCryptographicHash crypto(QCryptographicHash::Sha1);
ThumbnailCreator tc(QDir::cleanPath(fileInfoS.absoluteFilePath()),_target+(QDir::cleanPath(fileInfoS.absoluteFilePath()).remove(_source))+".jpg"); QFile file(fileInfo.absoluteFilePath());
tc.create(); file.open(QFile::ReadOnly);
} crypto.addData(file.read(524288));
} //hash Sha1 del primer 0.5MB + filesize
i++;j++; QString hash = QString(crypto.result().toHex().constData()) + QString::number(fileInfo.size());
} insertComic(Comic(_currentPathFolders.last().id,0,fileName,relativePath,hash));
} ThumbnailCreator tc(QDir::cleanPath(fileInfo.absoluteFilePath()),_target+"/covers/"+hash+".jpg");
} //ThumbnailCreator tc(QDir::cleanPath(fileInfo.absoluteFilePath()),_target+"/covers/"+fileInfo.fileName()+".jpg");
} tc.create();
}
} }
ThumbnailCreator::ThumbnailCreator(QString fileSource, QString target="") }
:_fileSource(fileSource),_target(target),_numPages(0) }
{
} void LibraryCreator::update(QDir dirS,QDir dirD)
{
void ThumbnailCreator::create() dirS.setNameFilters(_nameFilter);
{ dirS.setFilter(QDir::AllDirs|QDir::Files|QDir::NoDotAndDotDot);
_7z = new QProcess(); dirS.setSorting(QDir::Name|QDir::IgnoreCase|QDir::LocaleAware|QDir::DirsFirst);
QStringList attributes; QFileInfoList listS = dirS.entryInfoList();
attributes << "l" << "-ssc-" << "-r" << _fileSource << "*.jpg" << "*.jpeg" << "*.png" << "*.gif" << "*.tiff" << "*.tif" << "*.bmp";
//connect(_7z,SIGNAL(finished(int,QProcess::ExitStatus)),this,SLOT(loadFirstImage(void))); dirD.setNameFilters(QStringList()<<"*.jpg");
connect(_7z,SIGNAL(error(QProcess::ProcessError)),this,SIGNAL(openingError(QProcess::ProcessError))); dirD.setFilter(QDir::AllDirs|QDir::Files|QDir::NoDotAndDotDot);
_7z->start(QCoreApplication::applicationDirPath()+"/utils/7z",attributes); dirD.setSorting(QDir::Name|QDir::IgnoreCase|QDir::LocaleAware|QDir::DirsFirst);
_7z->waitForFinished(60000); QFileInfoList listD = dirD.entryInfoList();
QRegExp rx("[0-9]{4}-[0-9]{2}-[0-9]{2}[ ]+[0-9]{2}:[0-9]{2}:[0-9]{2}[ ]+.{5}[ ]+([0-9]+)[ ]+([0-9]+)[ ]+(.+)"); int lenghtS = listS.size();
int lenghtD = listD.size();
QString ba = QString::fromUtf8(_7z->readAllStandardOutput()); int maxLenght = qMax(lenghtS,lenghtD);
QList<QString> lines = ba.split('\n');
QString line; bool updated;
_currentName = "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"; //TODO int i,j;
QString name; for (i=0,j=0; (i < lenghtS)||(j < lenghtD);)
foreach(line,lines) {
{ if(stopRunning)
if(rx.indexIn(line)!=-1) return;
{ updated = false;
name = rx.cap(3).trimmed(); if(i>=lenghtS) //finished source files/dirs
if(0 > QString::localeAwareCompare(name,_currentName)) {
_currentName = name; //delete listD //from j
_numPages++; for(;j<lenghtD;j++)
} {
} if(stopRunning)
delete _7z; return;
attributes.clear(); QFileInfo fileInfoD = listD.at(j);
_currentName = QDir::fromNativeSeparators(_currentName).split('/').last(); //separator fixed. if(fileInfoD.isDir())
#ifdef Q_WS_WIN {
attributes << "e" << "-so" << "-r" << _fileSource << QString(_currentName.toLocal8Bit().constData()); //TODO platform dependency?? OEM 437 delTree(QDir(fileInfoD.absoluteFilePath()));
#else dirD.rmdir(fileInfoD.absoluteFilePath());
attributes << "e" << "-so" << "-r" << _fileSource << _currentName; //TODO platform dependency?? OEM 437 }
#endif else
_7z = new QProcess(); dirD.remove(fileInfoD.absoluteFilePath());
connect(_7z,SIGNAL(error(QProcess::ProcessError)),this,SIGNAL(openingError(QProcess::ProcessError))); }
//connect(_7z,SIGNAL(finished(int,QProcess::ExitStatus)),this,SLOT(writeThumbnail(void))); updated = true;
_7z->start(QCoreApplication::applicationDirPath()+"/utils/7z",attributes); }
_7z->waitForFinished(60000); if(j>=lenghtD) //finished library files/dirs
{
QByteArray image = _7z->readAllStandardOutput(); //create listS //from i
QString error = _7z->readAllStandardError(); for(;i<lenghtS;i++)
QImage p; {
if(_target=="") if(stopRunning)
{ return;
if(!_cover.loadFromData(image)) QFileInfo fileInfoS = listS.at(i);
_cover.load(":/images/notCover.png"); if(fileInfoS.isDir())
} create(QDir(fileInfoS.absoluteFilePath()));
else else
{ {
if(p.loadFromData(image)) dirD.mkpath(_target+(QDir::cleanPath(fileInfoS.absolutePath()).remove(_source)));
{ emit(coverExtracted(QDir::cleanPath(fileInfoS.absoluteFilePath()).remove(_source)));
//TODO calculate aspect ratio ThumbnailCreator tc(QDir::cleanPath(fileInfoS.absoluteFilePath()),_target+(QDir::cleanPath(fileInfoS.absoluteFilePath()).remove(_source))+".jpg");
QImage scaled; tc.create();
if(p.width()>p.height()) //landscape?? }
scaled = p.scaledToWidth(640,Qt::SmoothTransformation); }
else updated = true;
scaled = p.scaledToWidth(480,Qt::SmoothTransformation); }
scaled.save(_target,0,75); if(!updated)
} {
else QFileInfo fileInfoS = listS.at(i);
{ QFileInfo fileInfoD = listD.at(j);
p.load(":/images/notCover.png"); QString nameS = QDir::cleanPath(fileInfoS.absoluteFilePath()).remove(QDir::cleanPath(fileInfoS.absolutePath())); //remove source
p.save(_target); QString nameD = QDir::cleanPath(fileInfoD.absoluteFilePath()).remove(QDir::cleanPath(fileInfoD.absolutePath())); //remove target
//TODO save a default image.
} int comparation = QString::localeAwareCompare(nameS,nameD);
} if(fileInfoS.isDir()&&fileInfoD.isDir())
delete _7z; if(comparation == 0)//same folder, update
} {
update(QDir(fileInfoS.absoluteFilePath()),QDir(fileInfoD.absoluteFilePath()));
/*void ThumbnailCreator::openingError(QProcess::ProcessError error) i++;
{ j++;
//TODO : move to the gui thread }
switch(error) else
{ if(comparation < 0) //nameS doesn't exist on Target folder...
case QProcess::FailedToStart: {
QMessageBox::critical(NULL,tr("7z not found"),tr("7z wasn't found in your PATH.")); if(nameS!="/.yacreaderlibrary")
break; create(QDir(fileInfoS.absoluteFilePath()));
case QProcess::Crashed: i++;
QMessageBox::critical(NULL,tr("7z crashed"),tr("7z crashed.")); }
break; else //nameD no longer avaliable on Source folder...
case QProcess::ReadError: {
QMessageBox::critical(NULL,tr("7z reading"),tr("problem reading from 7z")); if(nameS!="/.yacreaderlibrary")
break; {
case QProcess::UnknownError: delTree(QDir(fileInfoD.absoluteFilePath()));
QMessageBox::critical(NULL,tr("7z problem"),tr("Unknown error 7z")); dirD.rmdir(fileInfoD.absoluteFilePath());
break; j++;
default: }
//TODO else
break; i++; //skip library directory
} }
}*/ else // one of them(or both) is a file
if(fileInfoS.isDir()) //this folder doesn't exist on library
{
if(nameS!="/.yacreaderlibrary") //skip .yacreaderlibrary folder
create(QDir(fileInfoS.absoluteFilePath()));
i++;
}
else
if(fileInfoD.isDir()) //delete this folder from library
{
delTree(QDir(fileInfoD.absoluteFilePath()));
dirD.rmdir(fileInfoD.absoluteFilePath());
j++;
}
else //both are files //BUG on windows (no case sensitive)
{
nameD.remove(nameD.size()-4,4);
int comparation = QString::localeAwareCompare(nameS,nameD);
if(comparation < 0) //create new thumbnail
{
dirD.mkpath(_target+(QDir::cleanPath(fileInfoS.absolutePath()).remove(_source)));
emit(coverExtracted(QDir::cleanPath(fileInfoS.absoluteFilePath()).remove(_source)));
ThumbnailCreator tc(QDir::cleanPath(fileInfoS.absoluteFilePath()),_target+(QDir::cleanPath(fileInfoS.absoluteFilePath()).remove(_source))+".jpg");
tc.create();
i++;
}
else
{
if(comparation > 0) //delete thumbnail
{
dirD.remove(fileInfoD.absoluteFilePath());
QString tick = fileInfoD.absoluteFilePath();
dirD.remove(tick.remove(tick.size()-3,3));
dirD.remove(tick+"r");
j++;
}
else //same file
{
if(fileInfoS.isFile() && fileInfoD.isFile())
{
if(fileInfoS.lastModified()>fileInfoD.lastModified())
{
dirD.mkpath(_target+(QDir::cleanPath(fileInfoS.absolutePath()).remove(_source)));
emit(coverExtracted(QDir::cleanPath(fileInfoS.absoluteFilePath()).remove(_source)));
ThumbnailCreator tc(QDir::cleanPath(fileInfoS.absoluteFilePath()),_target+(QDir::cleanPath(fileInfoS.absoluteFilePath()).remove(_source))+".jpg");
tc.create();
}
}
i++;j++;
}
}
}
}
}
}
ThumbnailCreator::ThumbnailCreator(QString fileSource, QString target="")
:_fileSource(fileSource),_target(target),_numPages(0)
{
}
void ThumbnailCreator::create()
{
_7z = new QProcess();
QStringList attributes;
attributes << "l" << "-ssc-" << "-r" << _fileSource << "*.jpg" << "*.jpeg" << "*.png" << "*.gif" << "*.tiff" << "*.tif" << "*.bmp";
//connect(_7z,SIGNAL(finished(int,QProcess::ExitStatus)),this,SLOT(loadFirstImage(void)));
connect(_7z,SIGNAL(error(QProcess::ProcessError)),this,SIGNAL(openingError(QProcess::ProcessError)));
_7z->start(QCoreApplication::applicationDirPath()+"/utils/7z",attributes);
_7z->waitForFinished(60000);
QRegExp rx("[0-9]{4}-[0-9]{2}-[0-9]{2}[ ]+[0-9]{2}:[0-9]{2}:[0-9]{2}[ ]+.{5}[ ]+([0-9]+)[ ]+([0-9]+)[ ]+(.+)");
QString ba = QString::fromUtf8(_7z->readAllStandardOutput());
QList<QString> lines = ba.split('\n');
QString line;
_currentName = "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"; //TODO
QString name;
foreach(line,lines)
{
if(rx.indexIn(line)!=-1)
{
name = rx.cap(3).trimmed();
if(0 > QString::localeAwareCompare(name,_currentName))
_currentName = name;
_numPages++;
}
}
delete _7z;
attributes.clear();
_currentName = QDir::fromNativeSeparators(_currentName).split('/').last(); //separator fixed.
#ifdef Q_WS_WIN
attributes << "e" << "-so" << "-r" << _fileSource << QString(_currentName.toLocal8Bit().constData()); //TODO platform dependency?? OEM 437
#else
attributes << "e" << "-so" << "-r" << _fileSource << _currentName; //TODO platform dependency?? OEM 437
#endif
_7z = new QProcess();
connect(_7z,SIGNAL(error(QProcess::ProcessError)),this,SIGNAL(openingError(QProcess::ProcessError)));
//connect(_7z,SIGNAL(finished(int,QProcess::ExitStatus)),this,SLOT(writeThumbnail(void)));
_7z->start(QCoreApplication::applicationDirPath()+"/utils/7z",attributes);
_7z->waitForFinished(60000);
QByteArray image = _7z->readAllStandardOutput();
QString error = _7z->readAllStandardError();
QImage p;
if(_target=="")
{
if(!_cover.loadFromData(image))
_cover.load(":/images/notCover.png");
}
else
{
if(p.loadFromData(image))
{
//TODO calculate aspect ratio
QImage scaled;
if(p.width()>p.height()) //landscape??
scaled = p.scaledToWidth(640,Qt::SmoothTransformation);
else
scaled = p.scaledToWidth(480,Qt::SmoothTransformation);
scaled.save(_target,0,75);
}
else
{
p.load(":/images/notCover.png");
p.save(_target);
//TODO save a default image.
}
}
delete _7z;
}
/*void ThumbnailCreator::openingError(QProcess::ProcessError error)
{
//TODO : move to the gui thread
switch(error)
{
case QProcess::FailedToStart:
QMessageBox::critical(NULL,tr("7z not found"),tr("7z wasn't found in your PATH."));
break;
case QProcess::Crashed:
QMessageBox::critical(NULL,tr("7z crashed"),tr("7z crashed."));
break;
case QProcess::ReadError:
QMessageBox::critical(NULL,tr("7z reading"),tr("problem reading from 7z"));
break;
case QProcess::UnknownError:
QMessageBox::critical(NULL,tr("7z problem"),tr("Unknown error 7z"));
break;
default:
//TODO
break;
}
}*/

View File

@ -12,6 +12,10 @@
#include <QtGui> #include <QtGui>
#include <QMutex> #include <QMutex>
#include <QThread> #include <QThread>
#include <QSqlDatabase>
class Folder;
class Comic;
class LibraryCreator : public QThread class LibraryCreator : public QThread
{ {
@ -23,14 +27,21 @@
void stop(); void stop();
private: private:
enum Mode {CREATOR,UPDATER}; enum Mode {CREATOR,UPDATER};
//atributos "globales" durante el proceso de creación y actualización
enum Mode _mode; enum Mode _mode;
QString _source; QString _source;
QString _target; QString _target;
QStringList _nameFilter; QStringList _nameFilter;
QSqlDatabase _database;
QList<Folder> _currentPathFolders; //lista de folders en el orden en el que están siendo explorados, el último es el folder actual
//recursive method //recursive method
void create(QDir currentDirectory); void create(QDir currentDirectory);
void update(QDir currentDirectory,QDir libraryCurrentDirectory); void update(QDir currentDirectory,QDir libraryCurrentDirectory);
void run(); void run();
bool createTables();
unsigned long long int insertFolders();
unsigned long long int insertFolder(unsigned long long int parentId,const Folder & folder);
unsigned long long int insertComic(const Comic & comic);
bool stopRunning; bool stopRunning;
signals: signals:
void finished(); void finished();