OSDN Git Service

Use qMin() and qMax() instead of min() and max() macros. Also use qBound() where...
[lamexp/LameXP.git] / src / Thread_DiskObserver.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2011 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.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 //
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
21
22 #include "Thread_DiskObserver.h"
23
24 #include "Global.h"
25
26 #include <QDir>
27
28 #define MIN_DISKSPACE 104857600ui64 //100 MB
29
30 ////////////////////////////////////////////////////////////
31 // Constructor & Destructor
32 ////////////////////////////////////////////////////////////
33
34 DiskObserverThread::DiskObserverThread(const QString &path)
35 :
36         m_path(makeRootDir(path))
37 {
38         m_terminated = false;
39 }
40
41 DiskObserverThread::~DiskObserverThread(void)
42 {
43 }
44
45 ////////////////////////////////////////////////////////////
46 // Protected functions
47 ////////////////////////////////////////////////////////////
48
49 void DiskObserverThread::run(void)
50 {
51         qDebug("DiskSpace observer started!");
52         m_terminated = false;
53
54         try
55         {
56                 observe();
57         }
58         catch(...)
59         {
60                 fflush(stdout);
61                 fflush(stderr);
62                 fprintf(stderr, "\nGURU MEDITATION !!!\n");
63                 FatalAppExit(0, L"Unhandeled exception error, application will exit!");
64                 TerminateProcess(GetCurrentProcess(), -1);
65         }
66 }
67
68 void DiskObserverThread::observe(void)
69 {
70         unsigned __int64 minimumSpace = MIN_DISKSPACE;
71         unsigned __int64 freeSpace, previousSpace = 0ui64;
72         bool ok = false;
73
74         while(!m_terminated)
75         {
76                 freeSpace = lamexp_free_diskspace(m_path, &ok);
77                 if(ok)
78                 {
79                         if(freeSpace < minimumSpace)
80                         {
81                                 qWarning("Free diskspace on '%s' dropped below %s MB, only %s MB free!", m_path.toUtf8().constData(), QString::number(minimumSpace / 1048576ui64).toUtf8().constData(), QString::number(freeSpace / 1048576ui64).toUtf8().constData());
82                                 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)), true);
83                                 minimumSpace = qMin(freeSpace, (minimumSpace >> 1));
84                         }
85                         if(freeSpace != previousSpace)
86                         {
87                                 emit freeSpaceChanged(freeSpace);
88                                 previousSpace = freeSpace;
89                         }
90                 }
91                 msleep(1000);
92         }
93 }
94
95 QString DiskObserverThread::makeRootDir(const QString &baseDir)
96 {
97         QDir dir(baseDir);
98         
99         if(!dir.exists())
100         {
101                 return baseDir;
102         }
103         
104         bool success = true;
105         
106         while(success)
107         {
108                 success = dir.cdUp();
109         }
110
111         return dir.canonicalPath();
112 }
113
114 ////////////////////////////////////////////////////////////
115 // SLOTS
116 ////////////////////////////////////////////////////////////
117
118 /*NONE*/
119
120 ////////////////////////////////////////////////////////////
121 // EVENTS
122 ////////////////////////////////////////////////////////////
123
124 /*NONE*/