Commit 7bb3c841 authored by Victor Lafon's avatar Victor Lafon
Browse files

Refactoring and fixing resource manager code bugs about creation, install and uninstall

parent 02fb85d7
......@@ -33,6 +33,8 @@
#include <iostream>
using namespace std;
//TODO Voir s'il ne vaut pas mieux faire un constructeur avec un xmlmeta plutot qu'un setmeta (cf control createPack)
KoResourceBundle::KoResourceBundle(QString const& bundlePath):KoResource(bundlePath)
{
manager=new KoResourceBundleManager(bundlePath.section('/',0,bundlePath.count('/')-2));
......@@ -84,7 +86,7 @@ bool KoResourceBundle::save()
meta->checkSort();
if (manager->bad()) {
meta->addTags(manifest->getTagList());
//meta->addTags(manifest->getTagsList());
manager->createPack(manifest,meta,thumbnail,true);
}
else {
......@@ -145,11 +147,21 @@ void KoResourceBundle::setMeta(KoXmlResourceBundleMeta* newMeta)
meta=newMeta;
}
void KoResourceBundle::addFile(QString fileType,QString filePath)
//TODO Voir s'il faut aussi rajouter les tags dans le meta
void KoResourceBundle::addFile(QString fileType,QString filePath, QStringList fileTagList)
{
manifest->addTag(fileType,filePath);
manifest->addManiTag(fileType,filePath,fileTagList);
meta->addTags(fileTagList);
}
//On rappelle que les tags d'un bundle ne sont stockés que dans le meta
//Les tags du manifest sont ajoutés au fur et à mesure de l'ajout des fichiers
QList<QString> KoResourceBundle::getTagsList()
{
return meta->getTagsList();
}
void KoResourceBundle::removeFile(QString fileName)
{
QList<QString> list=manifest->removeFile(fileName);
......@@ -177,17 +189,16 @@ void KoResourceBundle::rename(QString filename,QString name)
QString oldName=meta->getPackName();
QString shortName=name.section('.',0,0);
setFilename(filename);
setName(name);
addMeta("filename",filename);
addMeta("name",shortName);
manifest->rename(shortName);
if (isInstalled()) {
QList<QString> directoryList = manifest->getDirList();
QString dirPath;
QDir dir;
for (int i = 0; i < directoryList.size(); i++) {
dirPath = this->manager->getKritaPath();
dirPath = manager->getKritaPath();
dirPath.append(directoryList.at(i)).append("/");
dir.rename(dirPath+oldName,dirPath+shortName);
}
......@@ -195,6 +206,11 @@ void KoResourceBundle::rename(QString filename,QString name)
save();
}
void KoResourceBundle::removeTag(QString tagName)
{
meta->removeFirstTag("tag",tagName);
}
void KoResourceBundle::setThumbnail(QString filename)
{
thumbnail=QImage(filename);
......
......@@ -98,7 +98,9 @@ public:
* @param fileType type of the resource file
* @param filePath path of the resource file
*/
void addFile(QString fileType,QString filePath);
void addFile(QString fileType,QString filePath,QStringList fileTagList);
QList<QString> getTagsList();
/**
* @brief removeFile : Remove a file from the bundle
......@@ -106,7 +108,6 @@ public:
*/
void removeFile(QString fileName);
/**
* @brief addResourceDirs : Link the directories containing the resources of the bundle to the resource types
*/
......@@ -154,6 +155,7 @@ public:
bool isInstalled();
void setThumbnail(QString);
void removeTag(QString tagName);
protected:
......
......@@ -127,6 +127,8 @@ void KoResourceBundleManager::extractKFiles(QMap<QString,QString> pathList)
dirPath = targetPath.section('/',0,targetPath.count('/')-1);
mkdir(dirPath.toUtf8().constData(),S_IRWXU|S_IRGRP|S_IXGRP);
if(!resourcePack->extractFile(currentPath,targetPath)){
cout<<qPrintable(currentPath)<<endl;
cout<<qPrintable(targetPath)<<endl;
//TODO Supprimer le dossier créé
exit(1);
}
......
......@@ -97,6 +97,7 @@ void KoResourceManagerControl::toStatus(QString text,int timeout)
emit status(text,timeout);
}
//TODO Design Decision : Un paquet peut il contenir un paquet
bool KoResourceManagerControl::createPack(int type)
{
KoResourceTableModel *currentModel=getModel(type);
......@@ -120,25 +121,48 @@ bool KoResourceManagerControl::createPack(int type)
else {
QString bundlePath=root+"share/apps/krita/bundles/"+newMeta->getPackName()+".zip";
KoResourceBundle* newBundle=new KoResourceBundle(bundlePath);
bool isEmpty = true;
newBundle->load();
newBundle->setMeta(newMeta);
for (int i=0; i<selected.size(); i++) {
newBundle->addFile(selected.at(i).section('/',selected.count("/")-2,selected.count("/")-2),selected.at(i));
QString currentFileName = selected.at(i);
KoResource* currentResource = currentModel->getResourceFromFilename(selected.at(i));
KoResourceBundle* currentBundle = dynamic_cast<KoResourceBundle*>(currentResource);
if (currentFileName.contains('/') && !currentBundle) {
isEmpty=false;
newBundle->addFile(currentFileName.section('/',currentFileName.count("/")-1,currentFileName.count("/")-1),currentFileName,
currentModel->assignedTagsList(currentResource));
}
}
if (isEmpty) {
delete newBundle;
emit status("No valid content to be added to a new bundle...Creation Cancelled",3000);
return false;
}
newBundle->addMeta("fileName",bundlePath);
newBundle->addMeta("created",QDate::currentDate().toString("dd/MM/yyyy"));
newBundle->addMeta("created",QDate::currentDate().toString("dd/MM/yyyy"));
bundleServer->addResource(newBundle);
QStringList tagsList = newBundle->getTagsList();
for (int i=0;i<tagsList.size();i++) {
bundleServer->addTag(newBundle,tagsList.at(i));
}
currentModel->tagCategoryMembersChanged();
currentModel->clearSelected();
emit status("New bundle created successfully");
return true;
}
}
}
//TODO Trouver une solution pour que les ressources installées soient visibles
bool KoResourceManagerControl::install(int type)
{
KoResourceTableModel* currentModel=getModel(type);
......@@ -171,8 +195,6 @@ bool KoResourceManagerControl::install(int type)
return false;
}
else {
emit status("Bundle(s) installed successfully",3000);
currentModel->clearSelected();
for (int i=0;i<nbModels;i++) {
modelList.at(i)->refreshBundles();
......@@ -213,8 +235,6 @@ bool KoResourceManagerControl::uninstall(int type)
return false;
}
else {
emit status("Bundle(s) uninstalled successfully",3000);
currentModel->clearSelected();
for (int i=0;i<nbModels;i++) {
modelList.at(i)->refreshBundles();
......@@ -225,45 +245,61 @@ bool KoResourceManagerControl::uninstall(int type)
}
}
void KoResourceManagerControl::remove(int type)
bool KoResourceManagerControl::remove(int type)
{
KoResourceTableModel* currentModel=getModel(type);
QList<QString> selected=currentModel->getSelectedResource();
if (selected.isEmpty()) {
emit status("No resource selected to be removed...",3000);
return;
return false;
}
else {
QString currentFileName;
KoResource* currentResource;
KoResourceBundle* currentBundle;
bool modified=false;
bool bundleModified=false;
for (int i=0;i<selected.size();i++) {
currentFileName=selected.at(i);
currentResource=currentModel->getResourceFromFilename(currentFileName);
currentBundle = dynamic_cast<KoResourceBundle*>(currentResource);
if (currentBundle) {
if (currentResource) {
modified=true;
if (currentBundle->isInstalled()) {
currentBundle->uninstall();
QStringList tagsList = currentModel->assignedTagsList(currentResource);
for (int j=0;j<tagsList.size();j++) {
currentModel->deleteTag(currentResource,tagsList.at(j));
}
}
currentModel->removeResourceFile(currentResource,currentFileName);
QString delFilename = root+"share/apps/krita/temp/"+currentFileName.section('/',currentFileName.count('/'));
QFileInfo fileInfo(delFilename);
if (fileInfo.exists()) {
QTemporaryFile file(fileInfo.path() + "/" + fileInfo.baseName() + "XXXXXX" + "." + fileInfo.suffix());
if (file.open()) {
delFilename=file.fileName();
file.close();
if (currentBundle) {
bundleModified=true;
if (currentBundle->isInstalled()) {
currentBundle->uninstall();
}
}
currentModel->removeResourceFile(currentResource,currentFileName);
QString delFilename = root+"share/apps/krita/temp/"+currentFileName.section('/',currentFileName.count('/'));
QFileInfo fileInfo(delFilename);
if (fileInfo.exists()) {
QTemporaryFile file(fileInfo.path() + "/" + fileInfo.baseName() + "XXXXXX" + "." + fileInfo.suffix());
if (file.open()) {
delFilename=file.fileName();
file.close();
}
}
QFile::rename(currentFileName,delFilename);
}
}
QFile::rename(currentFileName,delFilename);
if (!modified) {
emit status("No target available to be removed...",3000);
return false;
}
currentModel->clearSelected();
......@@ -272,11 +308,11 @@ void KoResourceManagerControl::remove(int type)
for (int j=0;j<selected.size();j++) {
modelList.at(i)->removeOneSelected(selected.at(j));
}
if (modified) {
if (bundleModified) {
modelList.at(i)->refreshBundles();
}
}
emit status("Resource(s) removed successfully",3000);
return true;
}
}
......@@ -310,11 +346,6 @@ void KoResourceManagerControl::thumbnail(QModelIndex index,QString fileName,int
}
}
void KoResourceManagerControl::setMeta(QModelIndex index,QString metaType,QString metaValue,int type)
{
KoResourceBundle *currentBundle = dynamic_cast<KoResourceBundle*>(getModel(type)->getResourceFromIndex(index));
......@@ -324,13 +355,6 @@ void KoResourceManagerControl::setMeta(QModelIndex index,QString metaType,QStrin
}
}
void KoResourceManagerControl::setMeta(KoResourceBundle *bundle, QString metaType,QString metaValue)
{
if (bundle) {
bundle->addMeta(metaType,metaValue);
}
}
void KoResourceManagerControl::saveMeta(QModelIndex index,int type)
{
KoResourceBundle *currentBundle = dynamic_cast<KoResourceBundle*>(getModel(type)->getResourceFromIndex(index));
......@@ -341,44 +365,68 @@ void KoResourceManagerControl::saveMeta(QModelIndex index,int type)
}
//TODO Bug Foreground to Background
//TODO Bug rename bouton/label
//TODO Rafraichir réellement un bundle renommé
//TODO Trouver pourquoi les tags sont préservés pour les fichiers mais pas pour les paquets
//TODO Trouver un moyen pour bien différencier et renommer fichier et nom de ressource
//TODO Bug pour les noms qui n'ont pas l'extension du fichier de base
//TODO A tester pr tous les cas possibles
//TODO Rajouter le cas où le nom est déjà utilisé
//TODO Vérifier comment on supprime un tag d'un bundle <=> est ce qu'on supprime la méta donnée dans le xml
//<=>est-ce qu'on vérifie avant de modif le xml si un fichier qu'il contient n'a pas ce tag...
bool KoResourceManagerControl::rename(QModelIndex index,QString newName,int type)
{
KoResourceTableModel* currentModel=getModel(type);
KoResource* currentResource=currentModel->getResourceFromIndex(index);
if (currentResource!=0 && currentResource->valid()) {
KoResourceBundle* currentBundle= dynamic_cast<KoResourceBundle*>(currentResource);
if (currentResource) {
QString oldFilename=currentResource->filename();
QString newFilename=oldFilename.section('/',0,oldFilename.count('/')-1)+"/"+newName;
if (oldFilename!=newFilename) {
KoResourceBundle* currentBundle;
QStringList tagList = currentModel->assignedTagsList(currentResource);
if (!currentResource->name().contains('.')) {
newName=newName.section('.',0,0);
}
for (int i=0;i<tagList.size();i++) {
currentModel->deleteTag(currentResource,tagList.at(i));
}
QFile::rename(oldFilename,newFilename);
currentResource->setFilename(newFilename);
currentResource->setName(newName);
currentBundle = dynamic_cast<KoResourceBundle*>(currentResource);
if (currentBundle) {
currentBundle->rename(newFilename,newName);
}
else if (newFilename.count('/')==root.count()+5) {
QString bundleName=newFilename.section('/',newFilename.count('/')-1,newFilename.count('/')-1);
QString fileType=newFilename.section('/',newFilename.count('/')-2,newFilename.count('/')-2);
currentBundle= dynamic_cast<KoResourceBundle*>
(currentModel->getResourceFromFilename(bundleName.append(".zip")));
if (currentBundle) {
currentBundle->removeFile(oldFilename);
currentBundle->addFile(fileType,newFilename);
else {
if (newFilename.count('/')==root.count()+5) {
QString bundleName=newFilename.section('/',newFilename.count('/')-1,newFilename.count('/')-1);
QString fileType=newFilename.section('/',newFilename.count('/')-2,newFilename.count('/')-2);
currentBundle= dynamic_cast<KoResourceBundle*>
(currentModel->getResourceFromFilename(bundleName.append(".zip")));
if (currentBundle) {
currentBundle->removeFile(oldFilename);
currentBundle->addFile(fileType,newFilename,tagList);
}
}
}
for (int i=0;i<tagList.size();i++) {
currentModel->addTag(currentResource,tagList.at(i));
}
currentModel->removeOneSelected(oldFilename);
return true;
}
}
return true;
//model->refresh();
//TODO Définir dans quels cas le renommage échoue
return false;
}
//TODO Voir s'il est intéressant de garder la sélection
......@@ -449,7 +497,7 @@ void KoResourceManagerControl::filterResourceTypes(int index)
//TODO Rajouter la mise a jour des tags
//TODO Voir si ce traitement ne peut pas etre généraliser ou fait ailleurs
//TODO Lors de l'ajout d'un tag à un paquet, rajouter le tag aussi dans le meta
void KoResourceManagerControl::addFiles(QString bundleName,int type)
{
KoResourceTableModel* currentModel=getModel(type);
......@@ -464,7 +512,7 @@ void KoResourceManagerControl::addFiles(QString bundleName,int type)
QString currentSelect=selected.at(i);
QString resourceType=currentSelect.section('/',currentSelect.count("/")-2,currentSelect.count("/")-2);
currentBundle->addFile(resourceType,currentSelect);
currentBundle->addFile(resourceType,currentSelect,currentModel->assignedTagsList(currentModel->getResourceFromFilename(currentSelect)));
QFile::copy(currentSelect,path+resourceType+QString("/")+bundleName+QString("/")
+currentSelect.section('/',currentSelect.count("/")));
}
......
......@@ -50,13 +50,12 @@ public:
bool createPack(int type);
bool install(int type);
bool uninstall(int type);
void remove(int type);
bool remove(int type);
void configureFilters(int filterType, bool enable);
bool rename(QModelIndex index,QString,int type);
void setMeta(QModelIndex index,QString metaType,QString metaValue, int type);
void setMeta(KoResourceBundle *bundle, QString metaType,QString metaValue);
void saveMeta(QModelIndex index,int type);
void thumbnail(QModelIndex index,QString fileName,int type);
void exportBundle(int type);
......
......@@ -31,7 +31,7 @@
#include <iostream>
using namespace std;
//TODO Paramètre de ManagerControl à modifier si on veut rajouter des onglets
//TODO KoResourceManagerControl constructor parameter is the number of tabs of the Resource Manager
KoResourceManagerWidget::KoResourceManagerWidget(QWidget *parent) :
QMainWindow(parent),ui(new Ui::KoResourceManagerWidget),control(new KoResourceManagerControl(2)),tagMan(0),firstRefresh(true)
{
......@@ -56,21 +56,6 @@ KoResourceManagerWidget::KoResourceManagerWidget(QWidget *parent) :
ui->tabWidget->removeTab(2);
ui->statusbar->showMessage("Welcome back ! Resource Manager is ready to use...",1500);
/*this->model2=new MyTableModel(0);
m_filter = new QSortFilterProxyModel(this);
m_filter->setSourceModel(model2);
connect(ui->lineEdit,SIGNAL(textChanged(QString)),
m_filter,SLOT(setFilterFixedString(QString)));
m_filter->setFilterKeyColumn(1);
m_filter->setFilterCaseSensitivity(Qt::CaseInsensitive);
ui->tableView->setModel(m_filter);
ui->tableView_2->setModel(m_filter);
ui->tableView_3->setModel(m_filter);
ui->tableView_4->setModel(m_filter);
ui->tableView_5->setModel(m_filter);*/
}
KoResourceManagerWidget::~KoResourceManagerWidget()
......@@ -81,6 +66,8 @@ KoResourceManagerWidget::~KoResourceManagerWidget()
delete tagMan;
}
/*Initialize*/
void KoResourceManagerWidget::initializeFilterMenu()
{
QList<QAction*> liste;
......@@ -182,6 +169,8 @@ void KoResourceManagerWidget::initializeConnect()
connect(ui->tabWidget,SIGNAL(currentChanged(int)),this,SLOT(tableViewChanged(int)));
}
/*Tools*/
void KoResourceManagerWidget::showHide()
{
ui->widget_2->setVisible(!ui->widget_2->isVisible());
......@@ -197,8 +186,20 @@ QTableView* KoResourceManagerWidget::tableView(int index)
return dynamic_cast<QTableView*>(ui->tabWidget->widget(index)->layout()->itemAt(0)->widget());
}
//TODO Régler le pb de chgt de taille de la thumbnail
/*Slots*/
void KoResourceManagerWidget::toBundleView(int installTab) {
if(ui->tabWidget->currentIndex()!=installTab) {
ui->tabWidget->setCurrentIndex(installTab);
}
if (ui->comboBox->currentIndex()!=1) {
ui->comboBox->setCurrentIndex(1);
}
else {
refresh();
}
}
/*Functionalities*/
void KoResourceManagerWidget::about()
{
......@@ -211,6 +212,7 @@ void KoResourceManagerWidget::createPack()
{
if (control->createPack(ui->tabWidget->currentIndex())) {
toBundleView(0);
status("New bundle created successfully",3000);
}
}
......@@ -218,34 +220,27 @@ void KoResourceManagerWidget::installPack()
{
if (control->install(ui->tabWidget->currentIndex())) {
toBundleView(1);
status("Bundle(s) installed successfully",3000);
}
}
void KoResourceManagerWidget::deletePack()
{
control->remove(ui->tabWidget->currentIndex());
if(control->remove(ui->tabWidget->currentIndex())) {
refreshDetails(tableView(ui->tabWidget->currentIndex())->currentIndex());
emit status("Resource(s) removed successfully",3000);
}
}
void KoResourceManagerWidget::uninstallPack()
{
if (control->uninstall(ui->tabWidget->currentIndex())) {
toBundleView(0);
status("Bundle(s) uninstalled successfully",3000);
}
}
void KoResourceManagerWidget::toBundleView(int installTab) {
if(ui->tabWidget->currentIndex()!=installTab) {
ui->tabWidget->setCurrentIndex(installTab);
}
if (ui->comboBox->currentIndex()!=1) {
ui->comboBox->setCurrentIndex(1);
}
else {
refresh();
}
}
//TODO Régler le pb de chgt de taille de la thumbnail
void KoResourceManagerWidget::thumbnail()
{
QTableView* currentTableView = tableView(ui->tabWidget->currentIndex());
......@@ -258,7 +253,6 @@ void KoResourceManagerWidget::thumbnail()
currentTableView->setCurrentIndex(currentIndex);
}
void KoResourceManagerWidget::setMeta()
{
QObject *emetteur = sender();
......@@ -267,6 +261,7 @@ void KoResourceManagerWidget::setMeta()
int currentIndex=ui->tabWidget->currentIndex();
QTableView* currentTableView=tableView(currentIndex);
QLineEdit* sender=(QLineEdit*)emetteur;
if (emetteur==ui->lineEdit_2) {
control->setMeta(currentTableView->currentIndex(),"Author",sender->text(),currentIndex);
ui->lineEdit_2->blockSignals(true);
......@@ -318,16 +313,17 @@ void KoResourceManagerWidget::endRenaming()
if (control->rename(currentIndex,newFileName,ui->tabWidget->currentIndex())) {
resourceNameLabel->setText(newFileName);
currentTableView->reset();
ui->statusbar->showMessage("Resource renamed successfully...",3000);
}
else {
ui->lineEdit_5->setText(resourceNameLabel->text().section('.',0,0));
ui->statusbar->showMessage("Rename cancelled...",3000);
}
currentTableView->setCurrentIndex(currentIndex);
resourceNameLabel->setVisible(true);
ui->toolButton->setVisible(true);
ui->lineEdit_5->setVisible(false);
ui->statusbar->showMessage("Resource renamed successfully...",3000);
}
void KoResourceManagerWidget::rename(QString newName)
......@@ -344,6 +340,9 @@ void KoResourceManagerWidget::rename(QString newName)
currentTableView->reset();
ui->statusbar->showMessage("Resource renamed successfully...",3000);
}
else {
ui->statusbar->showMessage("Rename cancelled...",3000);
}
}
void KoResourceManagerWidget::removeTag(){
......@@ -448,8 +447,6 @@ void KoResourceManagerWidget::filterResourceTypes(int index)
ui->statusbar->showMessage("Resource lists updated",3000);
}