File indexing completed on 2024-05-05 05:40:26
0001 /*************************************************************************** 0002 * Copyright (C) 2009 by Renaud Guezennec * 0003 * https://rolisteam.org/contact * 0004 * * 0005 * Rolisteam 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 "data/cleveruri.h" 0021 #include <QDebug> 0022 #include <QString> 0023 0024 #include <QDataStream> 0025 #include <QFileInfo> 0026 #include <set> 0027 0028 #include "media/mediatype.h" 0029 #include "preferences/preferencesmanager.h" 0030 #include "worker/utilshelper.h" 0031 0032 ///////////////// 0033 // CleverUri {Core::SONG, ":/resources/images/audiofile.svg"},{Core::CHAT, 0034 // ":/resources/images/scenario.png"},{CleverURI::SONGLIST, ":/resources/images/playlist.svg"} 0035 ///////////////// 0036 0037 QStringList CleverURI::m_typeToPreferenceDirectory 0038 = QStringList() << QString("SessionDirectory") << QString("MapDirectory") << QString("ChatDirectory") 0039 << QString("ImageDirectory") << QString("ImageDirectory") << QString("MinutesDirectory") 0040 << QString("CharacterSheetDirectory") << QString("MusicDirectoryPlayer") 0041 << QString("MusicDirectoryPlayer") << QString("MinutesDirectory"); 0042 0043 CleverURI::CleverURI(Core::ContentType type) 0044 : ResourcesNode(TypeResource::Cleveruri), m_type(type), m_state(Core::State::Unloaded) 0045 { 0046 init(); 0047 } 0048 0049 QIcon CleverURI::icon() const 0050 { 0051 return QIcon::fromTheme(helper::utils::typeToIconPath(m_type)); 0052 } 0053 0054 CleverURI::CleverURI(const QString& name, const QString& path, Core::ContentType type) 0055 : ResourcesNode(TypeResource::Cleveruri), m_path(path), m_type(type), m_state(Core::State::Displayed) 0056 { 0057 setName(name); 0058 init(); 0059 } 0060 0061 CleverURI::~CleverURI()= default; 0062 0063 bool CleverURI::operator==(const CleverURI& uri) const 0064 { 0065 if((uri.path() == path()) && (uri.contentType() == contentType())) 0066 return true; 0067 return false; 0068 } 0069 0070 void CleverURI::setPath(const QString& path) 0071 { 0072 if(path == m_path) 0073 return; 0074 m_path= path; 0075 emit pathChanged(); 0076 } 0077 0078 Core::State CleverURI::state() const 0079 { 0080 return m_state; 0081 } 0082 0083 void CleverURI::setContentType(Core::ContentType type) 0084 { 0085 if(type == m_type) 0086 return; 0087 m_type= type; 0088 emit contentTypeChanged(); 0089 } 0090 0091 QString CleverURI::path() const 0092 { 0093 return m_path; 0094 } 0095 0096 Core::ContentType CleverURI::contentType() const 0097 { 0098 return m_type; 0099 } 0100 bool CleverURI::hasChildren() const 0101 { 0102 return false; 0103 } 0104 0105 void CleverURI::loadData() 0106 { 0107 if(loadingMode() == Core::LoadingMode::Internal) 0108 { 0109 loadFileFromUri(m_data); 0110 } 0111 } 0112 0113 void CleverURI::init() 0114 { 0115 if(m_path.isEmpty()) 0116 m_loadingMode= Core::LoadingMode::Internal; 0117 else 0118 m_loadingMode= Core::LoadingMode::Linked; 0119 } 0120 0121 void CleverURI::setState(const Core::State& state) 0122 { 0123 if(state == m_state) 0124 return; 0125 m_state= state; 0126 emit stateChanged(); 0127 } 0128 0129 bool CleverURI::hasData() const 0130 { 0131 return !m_data.isEmpty(); 0132 } 0133 0134 QByteArray CleverURI::data() const 0135 { 0136 return m_data; 0137 } 0138 0139 void CleverURI::setData(const QByteArray& data) 0140 { 0141 if(m_data == data) 0142 return; 0143 m_data= data; 0144 emit dataChanged(); 0145 } 0146 0147 bool CleverURI::isDisplayed() const 0148 { 0149 return (m_state == Core::State::Displayed); 0150 } 0151 0152 Core::LoadingMode CleverURI::loadingMode() const 0153 { 0154 return m_loadingMode; 0155 } 0156 0157 void CleverURI::setLoadingMode(const Core::LoadingMode& currentMode) 0158 { 0159 if(currentMode == m_loadingMode) 0160 return; 0161 m_loadingMode= currentMode; 0162 emit loadingModeChanged(); 0163 } 0164 bool CleverURI::exists() 0165 { 0166 if(m_path.isEmpty()) 0167 return false; 0168 0169 return QFileInfo::exists(m_path); 0170 } 0171 const QString CleverURI::getAbsolueDir() const 0172 { 0173 QFileInfo info(m_path); 0174 return info.absolutePath(); 0175 } 0176 0177 void CleverURI::write(QDataStream& out, bool tag, bool saveData) const 0178 { 0179 if(tag) 0180 { 0181 out << QStringLiteral("CleverUri"); 0182 } 0183 0184 QByteArray data; 0185 if(saveData) 0186 { 0187 if(m_data.isEmpty()) 0188 { 0189 loadFileFromUri(data); 0190 } 0191 else 0192 { 0193 data= m_data; 0194 } 0195 } 0196 out << static_cast<int>(m_type) << m_path << m_name << static_cast<int>(m_loadingMode) << m_data 0197 << static_cast<int>(m_state); 0198 } 0199 0200 void CleverURI::read(QDataStream& in) 0201 { 0202 int type; 0203 int mode; 0204 int state; 0205 in >> type >> m_path >> m_name >> mode >> m_data >> state; 0206 m_type= static_cast<Core::ContentType>(type); 0207 m_loadingMode= static_cast<Core::LoadingMode>(mode); 0208 m_state= static_cast<Core::State>(state); 0209 // updateListener(CleverURI::NAME); 0210 } 0211 0212 void CleverURI::loadFileFromUri(QByteArray& array) const 0213 { 0214 QFile file(m_path); 0215 if(file.open(QIODevice::ReadOnly)) 0216 { 0217 array= file.readAll(); 0218 } 0219 } 0220 0221 QVariant CleverURI::getData(ResourcesNode::DataValue i) const 0222 { 0223 QVariant var; 0224 switch(i) 0225 { 0226 case ResourcesNode::NAME: 0227 var= m_name; 0228 break; 0229 case ResourcesNode::MODE: 0230 var= m_loadingMode == Core::LoadingMode::Internal ? QObject::tr("Internal") : QObject::tr("Linked"); 0231 break; 0232 case ResourcesNode::DISPLAYED: 0233 { 0234 static const std::vector<QString> list( 0235 {QObject::tr("Closed"), QObject::tr("Hidden"), QObject::tr("Displayed")}); 0236 var= list[m_state]; 0237 break; 0238 } 0239 case ResourcesNode::URI: 0240 var= m_path; 0241 break; 0242 } 0243 return var; 0244 }