File indexing completed on 2024-05-05 05:40:35

0001 /***************************************************************************
0002  *  Copyright (C) 2019 by Renaud Guezennec                               *
0003  *   http://www.rolisteam.org/contact                                      *
0004  *                                                                         *
0005  *   This software is free software; you can redistribute it and/or modify *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0019  ***************************************************************************/
0020 #include "undoCmd/newmediacontroller.h"
0021 
0022 #include <QDebug>
0023 #include <QFileInfo>
0024 #include <QUuid>
0025 
0026 #include "controller/contentcontroller.h"
0027 #include "data/campaign.h"
0028 #include "data/campaigneditor.h"
0029 #include "media/mediafactory.h"
0030 #include "model/contentmodel.h"
0031 #include "worker/iohelper.h"
0032 #include "worker/utilshelper.h"
0033 
0034 NewMediaController::NewMediaController(ContentModel* model, const std::map<QString, QVariant>& map, QColor color,
0035                                        bool localIsGM, campaign::CampaignEditor* editor, QUndoCommand* parent)
0036     : QUndoCommand(parent), m_color(color), m_args(map), m_model(model), m_localGM(localIsGM), m_editor(editor)
0037 {
0038     m_contentType= m_args[Core::keys::KEY_TYPE].value<Core::ContentType>();
0039     m_title= m_args[Core::keys::KEY_NAME].toString();
0040     m_uuidUri= m_args[Core::keys::KEY_UUID].toString();
0041     m_fullPath= m_args[Core::keys::KEY_URL].toUrl();
0042 
0043     if(m_uuidUri.isEmpty())
0044         m_uuidUri= QUuid::createUuid().toString(QUuid::WithoutBraces);
0045 
0046     if(m_title.isEmpty())
0047     {
0048         m_title= QString("%1_%2")
0049                      .arg(helper::utils::typeToString(m_contentType))
0050                      .arg(model->mediaCount(m_contentType) + 1);
0051     }
0052 
0053     if(m_fullPath.isEmpty())
0054     {
0055         m_fullPath
0056             = QUrl::fromUserInput(m_editor ? m_editor->mediaFullPathWithExtension(m_title, m_contentType) : m_title);
0057 
0058         if(QFileInfo::exists(m_fullPath.toLocalFile()))
0059         {
0060             m_fullPath= QUrl::fromUserInput(m_editor ? m_editor->mediaFullPathWithExtension(m_uuidUri, m_contentType) :
0061                                                        m_title);
0062         }
0063     }
0064 
0065     QString media(QObject::tr("Create new %1 %2"));
0066 
0067     setText(media.arg(helper::utils::typeToString(m_contentType), m_title));
0068 }
0069 
0070 void NewMediaController::redo()
0071 {
0072     if(m_model.isNull())
0073         return;
0074 
0075     qInfo() << QStringLiteral("Redo command newmediacontroller: %1 ").arg(text());
0076     auto media= Media::MediaFactory::createLocalMedia(m_uuidUri, m_contentType, m_args, m_color, m_localGM);
0077     media->setUrl(m_fullPath);
0078     if(m_editor)
0079     {
0080         m_editor->addMedia(m_uuidUri, m_fullPath.toLocalFile(), IOHelper::saveController(media));
0081     }
0082     m_model->appendMedia(media);
0083 }
0084 
0085 void NewMediaController::undo()
0086 {
0087     if(m_model.isNull())
0088         return;
0089     qInfo() << QStringLiteral("Undo command newmediacontroller: %1 ").arg(text());
0090     m_model->removeMedia(m_uuidUri);
0091     if(m_editor)
0092     {
0093         m_editor->removeMedia(m_fullPath.toLocalFile());
0094     }
0095 }