Adapt for latest MUtils changes.
[LameXP.git] / src / Thread_DiskObserver.cpp
blob49b62e36abe4813d39f84419b17e6b5841e7c9ce
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2016 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
23 #include "Thread_DiskObserver.h"
25 //Internal
26 #include "Global.h"
27 #include "Model_Progress.h"
29 //MUtils
30 #include <MUtils/Global.h>
31 #include <MUtils/OSSupport.h>
32 #include <MUtils/Exception.h>
34 //Qt
35 #include <QDir>
37 #define MIN_DISKSPACE 104857600ui64 //100 MB
39 ////////////////////////////////////////////////////////////
40 // Constructor & Destructor
41 ////////////////////////////////////////////////////////////
43 DiskObserverThread::DiskObserverThread(const QString &path)
45 m_path(makeRootDir(path))
49 DiskObserverThread::~DiskObserverThread(void)
53 ////////////////////////////////////////////////////////////
54 // Protected functions
55 ////////////////////////////////////////////////////////////
57 void DiskObserverThread::run(void)
59 qDebug("DiskSpace observer started!");
61 try
63 observe();
65 catch(const std::exception &error)
67 MUTILS_PRINT_ERROR("\nGURU MEDITATION !!!\n\nException error:\n%s\n", error.what());
68 MUtils::OS::fatal_exit(L"Unhandeled C++ exception error, application will exit!");
70 catch(...)
72 MUTILS_PRINT_ERROR("\nGURU MEDITATION !!!\n\nUnknown exception error!\n");
73 MUtils::OS::fatal_exit(L"Unhandeled C++ exception error, application will exit!");
76 while(m_semaphore.available()) m_semaphore.tryAcquire();
79 void DiskObserverThread::observe(void)
81 quint64 minimumSpace = MIN_DISKSPACE;
82 quint64 previousSpace = quint64(-1);
84 forever
86 quint64 freeSpace = 0ui64;
87 if(MUtils::OS::free_diskspace(m_path, freeSpace))
89 if(freeSpace < minimumSpace)
91 qWarning("Free diskspace on '%s' dropped below %s MB, only %s MB free!", MUTILS_UTF8(m_path), MUTILS_UTF8(QString::number(minimumSpace / 1048576ui64)), MUTILS_UTF8(QString::number(freeSpace / 1048576ui64)));
92 emit messageLogged(tr("Low diskspace on drive '%1' detected (only %2 MB are free), problems can occur!").arg(QDir::toNativeSeparators(m_path), QString::number(freeSpace / 1048576ui64)), ProgressModel::SysMsg_Warning);
93 minimumSpace = qMin(freeSpace, (minimumSpace >> 1));
95 if(freeSpace != previousSpace)
97 emit freeSpaceChanged(freeSpace);
98 previousSpace = freeSpace;
101 if(m_semaphore.tryAcquire(1, 2000)) break;
105 QString DiskObserverThread::makeRootDir(const QString &baseDir)
107 QDir dir(baseDir);
109 if(!dir.exists())
111 return baseDir;
114 bool success = true;
116 while(success)
118 success = dir.cdUp();
121 return dir.canonicalPath();
124 ////////////////////////////////////////////////////////////
125 // SLOTS
126 ////////////////////////////////////////////////////////////
128 /*NONE*/
130 ////////////////////////////////////////////////////////////
131 // EVENTS
132 ////////////////////////////////////////////////////////////
134 /*NONE*/