File indexing completed on 2024-05-19 05:40:36
0001 /*************************************************************************** 0002 * Copyright (C) 2020 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 "controller/view_controller/notecontroller.h" 0021 0022 #include <QFile> 0023 #include <QFileInfo> 0024 #include <QStringDecoder> 0025 #include <QTextDocument> 0026 0027 NoteController::NoteController(const QString& id, QObject* parent) 0028 : MediaControllerBase(id, Core::ContentType::NOTES, parent) 0029 { 0030 0031 connect(this, &NoteController::urlChanged, this, &NoteController::loadText); 0032 0033 /*auto future= QtConcurrent::run(IOHelper::loadFile, m_path); 0034 auto watcher= new QFutureWatcher<QByteArray>(); 0035 connect(watcher, &QFutureWatcher<QByteArray>::finished, this, [this, watcher]() { 0036 auto result= watcher->result(); 0037 0038 if(!result.isEmpty()) 0039 setText(QString(result)); 0040 0041 watcher->deleteLater(); 0042 });*/ 0043 } 0044 0045 QString NoteController::text() const 0046 { 0047 return m_text; 0048 } 0049 0050 void NoteController::setText(const QString& text) 0051 { 0052 if(text == m_text) 0053 return; 0054 m_text= text; 0055 emit textChanged(m_text); 0056 } 0057 0058 bool NoteController::isHtml() const 0059 { 0060 return m_html; 0061 } 0062 0063 void NoteController::loadText() 0064 { 0065 QFileInfo fi(url().toLocalFile()); 0066 const QString ext= fi.completeSuffix().toLower(); 0067 if(!fi.exists()) 0068 { 0069 return; 0070 } 0071 0072 if(ext == "odt" || ext == "ott") 0073 { 0074 emit loadOdt(url().toLocalFile()); 0075 } 0076 else 0077 { 0078 // TODO use helper/worker 0079 QFile file(url().toLocalFile()); 0080 if(!file.open(QFile::ReadOnly)) 0081 return; 0082 0083 QByteArray data= file.readAll(); 0084 auto encoding= QStringConverter::encodingForHtml(data); 0085 auto toHtml= QStringDecoder(encoding.value()); 0086 QString str= toHtml.decode(data); 0087 0088 setHtml(Qt::mightBeRichText(str)); 0089 setText(isHtml() ? str : QString::fromLocal8Bit(QByteArrayView{data})); 0090 } 0091 } 0092 0093 void NoteController::setHtml(bool b) 0094 { 0095 if(m_html == b) 0096 return; 0097 m_html= b; 0098 emit htmlChanged(); 0099 }