summaryrefslogtreecommitdiffstats
path: root/opensuse/core/tdebase/lowdiskspace.patch
blob: a2ee05f549bbe0eac3152ee873ea4a06f06d7c95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
Subject: Dialog notifying about running low on disk space
From: Lubos Lunak
Feature: bnc#199054
Patch-upstream: no

Index: kioslave/media/medianotifier/Makefile.am
===================================================================
--- kioslave/media/medianotifier/Makefile.am.orig
+++ kioslave/media/medianotifier/Makefile.am
@@ -5,7 +5,8 @@ kded_medianotifier_la_LDFLAGS = -module
 kded_medianotifier_la_LIBADD = ../libmediacommon/libmediacommon.la $(LIB_KDECORE) \
                                $(LIB_KDEUI) $(LIB_KIO)
 kded_medianotifier_la_SOURCES = medianotifier.cpp medianotifier.skel \
-                                notificationdialog.cpp notificationdialogview.ui
+                                notificationdialog.cpp notificationdialogview.ui \
+                                freespacenotifier.cpp freespacewidget.ui
 
 noinst_HEADERS = medianotifier.h notificationdialog.h
 
Index: kioslave/media/medianotifier/medianotifier.h
===================================================================
--- kioslave/media/medianotifier/medianotifier.h.orig
+++ kioslave/media/medianotifier/medianotifier.h
@@ -27,6 +27,8 @@
 #include <qstring.h>
 #include <qmap.h>
 
+class FreeSpaceNotifier;
+
 class MediaNotifier:  public KDEDModule
 {
 	Q_OBJECT
@@ -52,6 +54,8 @@ private:
 	                   const QString &autoopenFile );
 
 	QMap<KIO::Job*,bool> m_allowNotificationMap;
+        FreeSpaceNotifier* m_freeSpaceNotifier;
 };
+
 #endif
 
Index: kioslave/media/medianotifier/medianotifier.cpp
===================================================================
--- kioslave/media/medianotifier/medianotifier.cpp.orig
+++ kioslave/media/medianotifier/medianotifier.cpp
@@ -36,6 +36,7 @@
 #include "notifiersettings.h"
 #include "notifieraction.h"
 #include "mediamanagersettings.h"
+#include "freespacenotifier.h"
 
 MediaNotifier::MediaNotifier(const QCString &name) : KDEDModule(name)
 {
@@ -44,6 +45,8 @@ MediaNotifier::MediaNotifier(const QCStr
 	
 	connectDCOPSignal( "kded", "mediamanager", "mediumChanged(QString, bool)",
 	                   "onMediumChange(QString, bool)", true );
+
+        m_freeSpaceNotifier = new FreeSpaceNotifier( this );
 }
 
 MediaNotifier::~MediaNotifier()
@@ -53,6 +56,7 @@ MediaNotifier::~MediaNotifier()
 	
 	disconnectDCOPSignal( "kded", "mediamanager", "mediumChanged(QString, bool)",
 	                      "onMediumChange(QString, bool)" );
+        delete m_freeSpaceNotifier;
 }
 
 void MediaNotifier::onMediumChange( const QString &name, bool allowNotification )
Index: kioslave/media/medianotifier/freespacenotifier.cpp
===================================================================
--- /dev/null
+++ kioslave/media/medianotifier/freespacenotifier.cpp
@@ -0,0 +1,159 @@
+/* This file is part of the KDE Project
+   Copyright (c) 2006 Lukas Tinkl <[email protected]>
+   Copyright (c) 2008 Lubos Lunak <[email protected]>
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "freespacenotifier.h"
+
+#include <sys/vfs.h>
+#include <unistd.h>
+
+#include <qdir.h>
+#include <qfile.h>
+#include <qlabel.h>
+#include <qspinbox.h>
+
+#include <kconfig.h>
+#include <kdebug.h>
+#include <klocale.h>
+#include <krun.h>
+
+#include "freespacewidget.h"
+
+
+FreeSpaceNotifier::FreeSpaceNotifier( QObject* parent )
+    : QObject( parent )
+    , lastAvailTimer( NULL )
+    , dialog( NULL )
+    , lastAvail( -1 )
+{
+    connect( &timer, SIGNAL( timeout() ), SLOT( checkFreeDiskSpace() ) );
+    KConfig cfg( "lowspacesuse", true ); // read only
+    KConfigGroup group( &cfg, "General" );
+    limit = group.readNumEntry( "WarnMinimumFreeSpace", 200 ); // MiB
+    if( limit != 0 )
+        timer.start( 1000 * 60 /* 1 minute */ );
+}
+
+FreeSpaceNotifier::~FreeSpaceNotifier()
+{
+    delete dialog;
+}
+
+void FreeSpaceNotifier::checkFreeDiskSpace()
+{
+    if ( dialog )
+        return;
+    struct statfs sfs;
+    if ( statfs( QFile::encodeName( QDir::homeDirPath() ), &sfs ) == 0 )
+    {
+        long avail = ( getuid() ? sfs.f_bavail : sfs.f_bfree );
+
+        if (avail < 0 || sfs.f_blocks <= 0)
+            return; // we better do not say anything about it
+        
+        int availpct = int( 100 * avail / sfs.f_blocks );
+        avail = ((long long)avail) * sfs.f_bsize / ( 1024 * 1024 ); // to MiB
+        bool warn = false;
+        if( avail < limit ) // avail disk space dropped under a limit
+        {
+            if( lastAvail < 0 ) // always warn the first time
+            {
+                lastAvail = avail;
+                warn = true;
+            }
+            else if( avail > lastAvail ) // the user freed some space
+                lastAvail = avail;       // so warn if it goes low again
+            else if( avail < lastAvail * 0.5 ) // available dropped to a half of previous one, warn again
+            {
+                warn = true;
+                lastAvail = avail;
+            }
+            // do not change lastAvail otherwise, to handle free space slowly going down
+        }
+        if ( warn )
+        {
+            dialog = new KDialogBase(
+                i18n( "Low Disk Space" ),
+                KDialogBase::Yes | KDialogBase::No | KDialogBase::Cancel,
+                KDialogBase::Yes, KDialogBase::No,
+                0, "lowdiskspacedialog", false, true,
+                i18n( "Open File Manager" ), i18n( "Do Nothing" ), i18n( "Disable Warning" ));
+            widget = new FreeSpaceWidget( dialog );
+            dialog->setMainWidget( widget );
+
+            QString text = i18n( "You are running low on disk space on your home partition (currently %2%, %1 MiB free)." )
+                .arg( avail ).arg( availpct );
+            widget->warningLabel->setText( text );
+            widget->spinbox->setMinValue( 0 );
+            widget->spinbox->setMaxValue( 100000 );
+            widget->spinbox->setValue( limit );
+            connect( dialog, SIGNAL( yesClicked() ), SLOT( slotYes() ) );
+            connect( dialog, SIGNAL( noClicked() ), SLOT( slotNo() ) );
+            connect( dialog, SIGNAL( cancelClicked() ), SLOT( slotCancel() ) );
+            dialog->show();
+        }
+    }
+}
+
+void FreeSpaceNotifier::slotYes()
+{
+    ( void ) new KRun( KURL::fromPathOrURL( QDir::homeDirPath() ) );
+    cleanupDialog( widget->spinbox->value());
+}
+
+void FreeSpaceNotifier::slotNo()
+{
+    cleanupDialog( widget->spinbox->value());
+}
+
+void FreeSpaceNotifier::slotCancel()
+{
+    cleanupDialog( 0 ); // set limit to zero
+}
+
+void FreeSpaceNotifier::cleanupDialog( long newLimit )
+{
+    dialog->deleteLater();
+    dialog = NULL;
+    if( limit != newLimit )
+    {
+        KConfig cfg( "lowspacesuse" );
+        KConfigGroup group( &cfg, "General" );
+        limit = newLimit;
+        group.writeEntry( "WarnMinimumFreeSpace", limit );
+        if( limit == 0 )
+            timer.stop();
+    }
+    if( limit != 0 )
+    { // warn again if constanly below limit for too long
+        if( lastAvailTimer == NULL )
+        { 
+            lastAvailTimer = new QTimer( this );
+            connect( lastAvailTimer, SIGNAL( timeout()), SLOT( resetLastAvailable()));
+        }
+        lastAvailTimer->start( 1000 * 60 * 60 /* 1 hour*/ ); 
+    }
+}
+
+void FreeSpaceNotifier::resetLastAvailable()
+{
+    lastAvail = -1;
+    lastAvailTimer->deleteLater();
+    lastAvailTimer = NULL;
+}
+
+#include "freespacenotifier.moc"
Index: kioslave/media/medianotifier/freespacenotifier.h
===================================================================
--- /dev/null
+++ kioslave/media/medianotifier/freespacenotifier.h
@@ -0,0 +1,51 @@
+/* This file is part of the KDE Project
+   Copyright (c) 2006 Lukas Tinkl <[email protected]>
+   Copyright (c) 2008 Lubos Lunak <[email protected]>
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef _FREESPACENOTIFIER_H_
+#define _FREESPACENOTIFIER_H_
+
+#include <qtimer.h>
+
+#include <kdialogbase.h>
+
+class FreeSpaceWidget;
+
+class FreeSpaceNotifier
+: public QObject
+{
+    Q_OBJECT
+    public:
+        FreeSpaceNotifier( QObject* parent = NULL );
+        virtual ~FreeSpaceNotifier();
+    private slots:
+        void checkFreeDiskSpace();
+        void resetLastAvailable();
+        void slotYes();
+        void slotNo();
+        void slotCancel();
+    private:
+        void cleanupDialog( long newLimit );
+        QTimer timer;
+        QTimer* lastAvailTimer;
+        KDialogBase* dialog;
+        FreeSpaceWidget* widget;
+        long limit;
+        long lastAvail; // used to supress repeated warnings when available space hasn't changed
+};
+
+#endif
Index: kioslave/media/medianotifier/freespacewidget.ui
===================================================================
--- /dev/null
+++ kioslave/media/medianotifier/freespacewidget.ui
@@ -0,0 +1,118 @@
+<!DOCTYPE UI><UI version="3.3" stdsetdef="1">
+<class>FreeSpaceWidget</class>
+<widget class="QWidget">
+    <property name="name">
+        <cstring>Form1</cstring>
+    </property>
+    <property name="geometry">
+        <rect>
+            <x>0</x>
+            <y>0</y>
+            <width>489</width>
+            <height>108</height>
+        </rect>
+    </property>
+    <property name="caption">
+        <string>Form1</string>
+    </property>
+    <vbox>
+        <property name="name">
+            <cstring>unnamed</cstring>
+        </property>
+        <widget class="QLabel">
+            <property name="name">
+                <cstring>warningLabel</cstring>
+            </property>
+            <property name="text">
+                <string></string>
+            </property>
+        </widget>
+        <widget class="QLabel">
+            <property name="name">
+                <cstring>textLabel2</cstring>
+            </property>
+            <property name="text">
+                <string>Would you like to run a file manager to free some disk space and fix the problem?</string>
+            </property>
+        </widget>
+        <spacer>
+            <property name="name">
+                <cstring>spacer3</cstring>
+            </property>
+            <property name="orientation">
+                <enum>Vertical</enum>
+            </property>
+            <property name="sizeType">
+                <enum>Expanding</enum>
+            </property>
+            <property name="sizeHint">
+                <size>
+                    <width>20</width>
+                    <height>40</height>
+                </size>
+            </property>
+        </spacer>
+        <widget class="QLayoutWidget">
+            <property name="name">
+                <cstring>layout3</cstring>
+            </property>
+            <hbox>
+                <property name="name">
+                    <cstring>unnamed</cstring>
+                </property>
+                <widget class="QLabel">
+                    <property name="name">
+                        <cstring>textLabel3</cstring>
+                    </property>
+                    <property name="text">
+                        <string>Warn again when the free space is below</string>
+                    </property>
+                </widget>
+                <widget class="QSpinBox">
+                    <property name="name">
+                        <cstring>spinbox</cstring>
+                    </property>
+                    <property name="suffix">
+                        <string> MiB</string>
+                    </property>
+                </widget>
+                <spacer>
+                    <property name="name">
+                        <cstring>spacer1</cstring>
+                    </property>
+                    <property name="orientation">
+                        <enum>Horizontal</enum>
+                    </property>
+                    <property name="sizeType">
+                        <enum>Expanding</enum>
+                    </property>
+                    <property name="sizeHint">
+                        <size>
+                            <width>30</width>
+                            <height>20</height>
+                        </size>
+                    </property>
+                </spacer>
+            </hbox>
+        </widget>
+        <spacer>
+            <property name="name">
+                <cstring>spacer2</cstring>
+            </property>
+            <property name="orientation">
+                <enum>Vertical</enum>
+            </property>
+            <property name="sizeType">
+                <enum>Expanding</enum>
+            </property>
+            <property name="sizeHint">
+                <size>
+                    <width>20</width>
+                    <height>16</height>
+                </size>
+            </property>
+        </spacer>
+    </vbox>
+</widget>
+<layoutdefaults spacing="6" margin="11"/>
+</UI>