LogoopenSUSE Build Service > Projects
Sign Up | Log In

View File lowdiskspace.patch of Package kdebase3 (Project openSUSE:Factory)

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 <ltinkl@suse.cz>
+   Copyright (c) 2008 Lubos Lunak <l.lunak@suse.cz>
+
+   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 <ltinkl@suse.cz>
+   Copyright (c) 2008 Lubos Lunak <l.lunak@suse.cz>
+
+   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>