File indexing completed on 2024-05-12 16:18:06
0001 /**************************************************************************************** 0002 * Copyright (C) 2003-2010 Mark Kretschmann <kretschmann@kde.org> * 0003 * Copyright (C) 2008 Dan Meltzer <parallelgrapefruit@gmail.com> * 0004 * Copyright (C) 2008-2009 Jeff Mitchell <mitchell@kde.org> * 0005 * Copyright (c) 2012 Matěj Laitl <matej@laitl.cz> * 0006 * * 0007 * This program is free software; you can redistribute it and/or modify it under * 0008 * the terms of the GNU General Public License as published by the Free Software * 0009 * Foundation; either version 2 of the License, or (at your option) any later * 0010 * version. * 0011 * * 0012 * This program is distributed in the hope that it will be useful, but WITHOUT ANY * 0013 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * 0014 * PARTICULAR PURPOSE. See the GNU General Public License for more details. * 0015 * * 0016 * You should have received a copy of the GNU General Public License along with * 0017 * this program. If not, see <http://www.gnu.org/licenses/>. * 0018 ****************************************************************************************/ 0019 0020 #include "ScanningState.h" 0021 0022 #include <QBuffer> 0023 #include <QDataStream> 0024 #include <QtDebug> 0025 0026 #ifdef Q_CC_MSVC 0027 #define __PRETTY_FUNCTION__ __FUNCSIG__ 0028 #endif 0029 0030 using namespace CollectionScanner; 0031 0032 ScanningState::ScanningState() 0033 : m_sharedMemory( nullptr ) 0034 , m_lastFilePos( 0 ) 0035 { 0036 } 0037 0038 ScanningState::~ScanningState() 0039 { 0040 delete m_sharedMemory; 0041 } 0042 0043 void 0044 ScanningState::setKey( const QString &key ) 0045 { 0046 delete m_sharedMemory; 0047 m_sharedMemory = new QSharedMemory( key ); 0048 m_sharedMemory->attach(); 0049 } 0050 0051 bool 0052 ScanningState::isValid() const 0053 { 0054 return m_sharedMemory && m_sharedMemory->isAttached(); 0055 } 0056 0057 QString 0058 ScanningState::lastDirectory() const 0059 { 0060 return m_lastDirectory; 0061 } 0062 0063 void 0064 ScanningState::setLastDirectory( const QString &dir ) 0065 { 0066 if( dir == m_lastDirectory ) 0067 return; 0068 0069 m_lastDirectory = dir; 0070 writeFull(); 0071 } 0072 0073 QStringList 0074 ScanningState::badFiles() const 0075 { 0076 return m_badFiles; 0077 } 0078 0079 void 0080 ScanningState::setBadFiles( const QStringList &badFiles ) 0081 { 0082 if( badFiles == m_badFiles ) 0083 return; 0084 0085 m_badFiles = badFiles; 0086 writeFull(); 0087 } 0088 0089 QString 0090 ScanningState::lastFile() const 0091 { 0092 return m_lastFile; 0093 } 0094 0095 void 0096 ScanningState::setLastFile( const QString &file ) 0097 { 0098 if( file == m_lastFile ) 0099 return; 0100 0101 m_lastFile = file; 0102 0103 if( !isValid() ) 0104 return; 0105 0106 QBuffer buffer; 0107 QDataStream out(&buffer); 0108 0109 buffer.open(QBuffer::WriteOnly); 0110 0111 out << m_lastFile; 0112 int size = buffer.size(); 0113 0114 if( size + m_lastFilePos < m_sharedMemory->size() ) 0115 { 0116 char *to = (char*)m_sharedMemory->data(); 0117 const char *from = buffer.data().data(); 0118 memcpy(to + m_lastFilePos, from, size); 0119 } 0120 else 0121 { 0122 qDebug() << __PRETTY_FUNCTION__ << "QSharedMemory is too small to hold the data."; 0123 qDebug() << "It is of size" << m_sharedMemory->size() << "bytes but we need more than" 0124 << size + m_lastFilePos << "bytes."; 0125 } 0126 0127 m_sharedMemory->unlock(); 0128 } 0129 0130 void 0131 ScanningState::readFull() 0132 { 0133 if( !isValid() ) 0134 return; 0135 0136 QBuffer buffer; 0137 QDataStream in(&buffer); 0138 0139 m_sharedMemory->lock(); 0140 buffer.setData((char*)m_sharedMemory->constData(), m_sharedMemory->size()); 0141 buffer.open(QBuffer::ReadOnly); 0142 0143 in >> m_lastDirectory; 0144 in >> m_badFiles; 0145 m_lastFilePos = buffer.pos(); 0146 in >> m_lastFile; 0147 0148 m_sharedMemory->unlock(); 0149 } 0150 0151 void 0152 ScanningState::writeFull() 0153 { 0154 if( !isValid() ) 0155 return; 0156 0157 QBuffer buffer; 0158 QDataStream out(&buffer); 0159 buffer.open(QBuffer::WriteOnly); 0160 0161 out << m_lastDirectory; 0162 out << m_badFiles; 0163 m_lastFilePos = buffer.pos(); 0164 out << m_lastFile; 0165 int size = buffer.size(); 0166 0167 m_sharedMemory->lock(); 0168 if( size < m_sharedMemory->size() ) 0169 { 0170 char *to = (char*)m_sharedMemory->data(); 0171 const char *from = buffer.data().data(); 0172 memcpy(to, from, size); 0173 } 0174 else 0175 { 0176 qDebug() << __PRETTY_FUNCTION__ << "QSharedMemory is too small to hold the data."; 0177 qDebug() << "It is of size" << m_sharedMemory->size() << "bytes but we need more than" 0178 << size << "bytes."; 0179 } 0180 0181 m_sharedMemory->unlock(); 0182 }