File: freespacenotifier.cpp

package info (click to toggle)
kde-workspace 4%3A4.8.4-6
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 89,920 kB
  • sloc: cpp: 373,584; ansic: 35,020; xml: 7,435; perl: 1,550; sh: 1,329; ruby: 1,135; python: 646; asm: 566; makefile: 38
file content (182 lines) | stat: -rw-r--r-- 6,644 bytes parent folder | download | duplicates (2)
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
/* 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>
   Copyright (c) 2009 Ivo Anjo <knuckles@gmail.com>

   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 <QtCore/QDir>
#include <QtCore/QFile>
#include <QtGui/QLabel>
#include <QtGui/QSpinBox>

#include <QtDBus/QtDBus>

#include <KDebug>
#include <KLocale>
#include <KRun>
#include <KConfigDialog>
#include <KDiskFreeSpaceInfo>

#include "settings.h"
#include "ui_freespacenotifier_prefs_base.h"

FreeSpaceNotifier::FreeSpaceNotifier( QObject* parent )
    : QObject( parent )
    , lastAvailTimer( NULL )
    , notification( NULL )
    , lastAvail( -1 )
{
    // If we are running, notifications are enabled
    FreeSpaceNotifierSettings::setEnableNotification( true );

    connect( &timer, SIGNAL(timeout()), SLOT(checkFreeDiskSpace()) );
    timer.start( 1000 * 60 /* 1 minute */ );
}

FreeSpaceNotifier::~FreeSpaceNotifier()
{
    // The notification is automatically destroyed when it goes away, so we only need to do this if
    // it is still being shown
    if ( notification ) notification->deref();
}

void FreeSpaceNotifier::checkFreeDiskSpace()
{
    if ( notification || !FreeSpaceNotifierSettings::enableNotification() )
        return;
    KDiskFreeSpaceInfo fsInfo = KDiskFreeSpaceInfo::freeSpaceInfo( QDir::homePath() );
    if ( fsInfo.isValid() )
    {
        int limit = FreeSpaceNotifierSettings::minimumSpace(); // MiB
        int availpct = int( 100 * fsInfo.available() / fsInfo.size() );
        qint64 avail = fsInfo.available() / ( 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 / 2 ) // 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 )
        {
            notification = new KNotification( "freespacenotif", 0, KNotification::Persistent );

            notification->setText( i18nc( "Warns the user that the system is running low on space on his home folder, indicating the percentage and absolute MiB size remaining, and asks if the user wants to do something about it", "You are running low on disk space on your home folder (currently %2%, %1 MiB free).\nWould you like to run a file manager to free some disk space?", avail, availpct ) );
            notification->setActions( QStringList() << i18nc( "Opens a file manager like dolphin", "Open File Manager" ) << i18nc( "Closes the notification", "Do Nothing" ) << i18nc( "Allows the user to configure the warning notification being shown", "Configure Warning" ) );
            //notification->setPixmap( ... ); // TODO: Maybe add a picture here?

            connect( notification, SIGNAL(action1Activated()), SLOT(openFileManager()) );
            connect( notification, SIGNAL(action2Activated()), SLOT(cleanupNotification()) );
            connect( notification, SIGNAL(action3Activated()), SLOT(showConfiguration()) );
            connect( notification, SIGNAL(closed()),           SLOT(cleanupNotification()) );

            notification->setComponentData( KComponentData( "freespacenotifier" ) );
            notification->sendEvent();
        }
    }
}

void FreeSpaceNotifier::openFileManager()
{
    cleanupNotification();
    new KRun( KUrl( QDir::homePath() ), 0 );
}

void FreeSpaceNotifier::showConfiguration()
{
    cleanupNotification();

    if ( KConfigDialog::showDialog( "settings" ) )  {
        return;
    }

    KConfigDialog *dialog = new KConfigDialog( 0, "settings", FreeSpaceNotifierSettings::self() );
    QWidget *generalSettingsDlg = new QWidget();

    Ui::freespacenotifier_prefs_base preferences;
    preferences.setupUi( generalSettingsDlg );

    dialog->addPage( generalSettingsDlg, i18nc( "The settings dialog main page name, as in 'general settings'", "General" ), "system-run" );
    connect( dialog, SIGNAL(finished()), this, SLOT(configDialogClosed()) );
    dialog->setAttribute( Qt::WA_DeleteOnClose );
    dialog->show();
}

void FreeSpaceNotifier::cleanupNotification()
{
    notification = NULL;

    // warn again if constantly 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;
}

void FreeSpaceNotifier::configDialogClosed()
{
    if ( !FreeSpaceNotifierSettings::enableNotification() )
        disableFSNotifier();
}

/* The idea here is to disable ourselves by telling kded to stop autostarting us, and
 * to kill the current running instance.
 */
void FreeSpaceNotifier::disableFSNotifier()
{
    QDBusInterface iface( "org.kde.kded", "/kded", "org.kde.kded" );
    if ( dbusError( iface ) ) return;

    // Disable current module autoload
    iface.call( "setModuleAutoloading", "freespacenotifier", false );
    if ( dbusError( iface ) ) return;

    // Unload current module
    iface.call( "unloadModule", "freespacenotifier" );
    if ( dbusError( iface ) ) return;
}

bool FreeSpaceNotifier::dbusError( QDBusInterface &iface )
{
    QDBusError err = iface.lastError();
    if ( err.isValid() )
    {
        kError() << "Failed to perform operation on kded [" << err.name() << "]:" << err.message();
        return true;
    }
    return false;
}