File indexing completed on 2024-05-05 04:59:18
0001 /* 0002 This file is part of the KDE project 0003 Copyright (C) 2011 Ernesto Rodriguez Ortiz <eortiz@uci.cu> 0004 0005 This program 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 3 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, see <http://www.gnu.org/licenses/>. 0017 0018 */ 0019 0020 #include "mmsthread.h" 0021 #include <vector> 0022 0023 MmsThread::MmsThread(const QString &url, const QString &name, int begin, int end) 0024 : QThread() 0025 , m_sourceUrl(url) 0026 , m_fileName(name) 0027 , m_begin(begin) 0028 , m_end(end) 0029 , m_download(true) 0030 { 0031 } 0032 0033 void MmsThread::run() 0034 { 0035 /** Seems that some times mmsx_read not read well.*/ 0036 int readed; 0037 mmsx_t *mms; 0038 QFile file(m_fileName); 0039 /** Opening the file for write the information.*/ 0040 file.open(QIODevice::ReadWrite); 0041 file.seek(m_begin); 0042 0043 /** Connecting to the url*/ 0044 mms = mmsx_connect(NULL, NULL, qstrdup(m_sourceUrl.toLatin1()), 1e6); 0045 if (mms) { 0046 m_locker.lock(); 0047 Q_EMIT signIsConnected(true); 0048 m_locker.unlock(); 0049 /** If the connections result successful it start the download.*/ 0050 mmsx_seek(nullptr, mms, m_begin, 0); 0051 while ((m_begin < m_end) && m_download) { 0052 if ((m_begin + 1024) > m_end) { 0053 const int var = m_end - m_begin; 0054 std::vector<char> data(var); 0055 readed = mmsx_read(nullptr, mms, data.data(), var); 0056 m_locker.lock(); 0057 Q_EMIT signReading(var, m_end, m_begin = m_end); 0058 /** Writing the readed to the file */ 0059 if (readed) { 0060 file.write(data.data(), readed); 0061 } 0062 m_locker.unlock(); 0063 } else { 0064 std::vector<char> data(1024); 0065 readed = mmsx_read(nullptr, mms, data.data(), 1024); 0066 m_locker.lock(); 0067 Q_EMIT signReading(1024, m_end, m_begin += 1024); 0068 /** Writing the readed to the file */ 0069 if (readed) { 0070 file.write(data.data(), readed); 0071 } 0072 m_locker.unlock(); 0073 } 0074 } 0075 file.close(); 0076 mmsx_close(mms); 0077 quit(); // NOTE: Keep "quit()" here, if not then the thread never Q_EMIT the signal finish. 0078 } else { 0079 /** If the connections not result successfully then stop all the download*/ 0080 m_locker.lock(); 0081 Q_EMIT signIsConnected(false); 0082 m_locker.unlock(); 0083 quit(); // NOTE: Keep "quit()" here, if not then the thread never Q_EMIT the signal finish. 0084 } 0085 exec(); 0086 } 0087 0088 void MmsThread::stop() 0089 { 0090 m_download = false; 0091 } 0092 0093 #include "moc_mmsthread.cpp"