• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdeutils
  • Sitemap
  • Contact Us
 

kdf

kwikdisk.cpp

Go to the documentation of this file.
00001 /*
00002   kwikdisk.cpp - KDiskFree
00003 
00004   Copyright (C) 1999 by Michael Kropfberger <michael.kropfberger@gmx.net>
00005 
00006   This program is free software; you can redistribute it and/or modify
00007   it under the terms of the GNU General Public License as published by
00008   the Free Software Foundation; either version 2 of the License, or
00009   (at your option) any later version.
00010 
00011   This program is distributed in the hope that it will be useful,
00012   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014   GNU General Public License for more details.
00015 
00016   You should have received a copy of the GNU General Public License
00017   along with this program; if not, write to the Free Software
00018   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00019 
00020   */
00021 
00022 //
00023 // 1999-12-03 Espen Sand
00024 // Cleanups, improvements and fixes for KDE-2
00025 //
00026 // 2004-07-15 Stanislav Karchebny
00027 // Rewrite for KDE 3
00028 //
00029 
00030 #include "kwikdisk.h"
00031 
00032 #include <stdlib.h>
00033 #include <unistd.h>
00034 
00035 #include <QtGui/QPen>
00036 #include <QtGui/QBitmap>
00037 #include <QtGui/QPainter>
00038 #include <QtGui/QMouseEvent>
00039 #include <QtGui/QPixmap>
00040 #include <QtCore/QAbstractEventDispatcher>
00041 #include <kxmlguiwindow.h>
00042 #include <klocale.h>
00043 #include <kapplication.h>
00044 #include <kaboutdata.h>
00045 #include <kcmdlineargs.h>
00046 #include <kmessagebox.h>
00047 #include <kmenu.h>
00048 #include <krun.h>
00049 #include <ktoolinvocation.h>
00050 #include <kshell.h>
00051 
00052 static const char description[] =
00053    I18N_NOOP("KDE Free disk space utility");
00054 
00055 static const char version[] = "0.4";
00056 
00057 /*****************************************************************************/
00058 
00059 KwikDisk::KwikDisk()
00060    : KSystemTrayIcon()
00061    , m_readingDF(false)
00062    , m_dirty(true)
00063    , m_inside(false)
00064    , m_optionDialog(0)
00065 {
00066    kDebug() ;
00067 
00068    setIcon(KSystemTrayIcon::loadIcon("kdf"));
00069    show();
00070 
00071    connect( &m_diskList, SIGNAL(readDFDone()), this, SLOT(updateDFDone()) );
00072    connect( &m_diskList, SIGNAL(criticallyFull(DiskEntry*)),
00073             this, SLOT(criticallyFull(DiskEntry*)) );
00074 
00075    connect( contextMenu(), SIGNAL(aboutToHide()), this, SLOT(aboutToHide()) );
00076 
00077    loadSettings();
00078    updateDF();
00079    connect( this, SIGNAL( activated(QSystemTrayIcon::ActivationReason)),
00080             SLOT(slotActivated(QSystemTrayIcon::ActivationReason) ));
00081 }
00082 
00083 void KwikDisk::enterEvent(QEvent *)
00084 {
00085    kDebug() ;
00086    m_inside = true;
00087 }
00088 
00089 void KwikDisk::leaveEvent(QEvent *)
00090 {
00091    kDebug() ;
00092    m_inside = false;
00093 }
00094 
00095 void KwikDisk::slotActivated(QSystemTrayIcon::ActivationReason)
00096 {
00097    kDebug() ;
00098 
00099    if( m_dirty )
00100       updateDF();
00101 }
00102 
00103 void KwikDisk::loadSettings()
00104 {
00105    kDebug() ;
00106 
00107    m_options.updateConfiguration();
00108    setUpdateFrequency( m_options.updateFrequency() );
00109 }
00110 
00111 void KwikDisk::setUpdateFrequency(int frequency)
00112 {
00113    kDebug() ;
00114 
00115    //
00116    // Kill current timer and restart it if the frequency is
00117    // larger than zero.
00118    //
00119    QAbstractEventDispatcher::instance()->unregisterTimers(this);
00120    if( frequency > 0 )
00121    {
00122       startTimer(frequency * 1000);
00123    }
00124 }
00125 
00131 void KwikDisk::timerEvent(QTimerEvent *)
00132 {
00133    kDebug() ;
00134    m_dirty = true;
00135 }
00136 
00137 void KwikDisk::updateDF()
00138 {
00139    kDebug() ;
00140 
00141    m_readingDF = true;
00142    m_diskList.readFSTAB();
00143    m_diskList.readDF();
00144 }
00145 
00146 void KwikDisk::updateDFDone()
00147 {
00148    kDebug() ;
00149 
00150    m_readingDF = false;
00151    m_dirty     = false;
00152 
00153    contextMenu()->clear();
00154    contextMenu()->setTitle(i18n("KwikDisk"));
00155 
00156    int itemNo = 0;
00157    for( DiskEntry *disk = m_diskList.first(); disk != 0; disk = m_diskList.next() )
00158    {
00159       // FIXME: tool tips are unused atm
00160       QString toolTipText = i18n("%1 (%2) %3 on %4",
00161            disk->mounted() ? i18nc("Unmount the storage device", "Unmount") : i18nc("Mount the storage device", "Mount"),
00162           disk->fsType(), disk->deviceName(), disk->mountPoint());
00163 
00164       QString entryName = disk->mountPoint();
00165       if( disk->mounted() )
00166       {
00167          entryName += QString("\t\t\t[%1]").arg(disk->prettyKBAvail());
00168       }
00169       int id = contextMenu()->insertItem("", this, SLOT(toggleMount(int)) );
00170       contextMenu()->setItemParameter(id, itemNo);
00171       itemNo++;
00172 
00173       QPixmap pix(KSystemTrayIcon::loadIcon(disk->iconName()).pixmap());
00174 
00175       if( getuid() !=0 && !disk->mountOptions().contains("user",Qt::CaseInsensitive) == -1 )
00176       {
00177          //
00178          // Special root icon, normal user cant mount.
00179          //
00180          // 2000-01-23 Espen Sand
00181          // Careful here: If the mask has not been defined we can
00182          // not use QPixmap::mask() because it returns 0 => segfault
00183          //
00184          if( !pix.mask().isNull() )
00185          {
00186             QBitmap *bm = new QBitmap((pix.mask()));
00187             if( bm != 0 )
00188             {
00189                QPainter qp( bm );
00190                qp.setPen(QPen(Qt::white,1));
00191                qp.drawRect(0,0,bm->width(),bm->height());
00192                qp.end();
00193                pix.setMask(*bm);
00194             }
00195             QPainter qp( &pix );
00196             qp.setPen(QPen(Qt::red,1));
00197             qp.drawRect(0,0,pix.width(),pix.height());
00198             qp.end();
00199          }
00200          contextMenu()->disconnectItem(id,disk,SLOT(toggleMount()));
00201          toolTipText = i18n("You must login as root to mount this disk");
00202       }
00203 
00204       contextMenu()->changeItem(id,pix,entryName);
00205    }
00206 
00207    contextMenu()->addSeparator();
00208 
00209    contextMenu()->insertItem(
00210       KSystemTrayIcon::loadIcon("kdf"),
00211       i18n("&Start KDiskFree"), this, SLOT(startKDF()),0);
00212 
00213    contextMenu()->insertItem(
00214       KSystemTrayIcon::loadIcon("configure"),
00215       i18n("&Configure KwikDisk..."), this, SLOT(changeSettings()),0);
00216 
00217    contextMenu()->insertItem(
00218       KSystemTrayIcon::loadIcon("help-contents"),
00219       KStandardGuiItem::help().text(), this, SLOT(invokeHelp()),0);
00220 
00221 }
00222 
00223 void KwikDisk::toggleMount(int item)
00224 {
00225    kDebug() ;
00226 
00227    DiskEntry *disk = m_diskList.at(item);
00228    if( disk == 0 )
00229    {
00230       return;
00231    }
00232 
00233    int val = disk->toggleMount();
00234    if( val != 0 )
00235    {
00236       KMessageBox::error(0, disk->lastSysError());
00237    }
00238    else if( (m_options.openFileManager() == true) && (disk->mounted() == true ) )
00239    {
00240       kDebug() << "opening filemanager" ;
00241       if( m_options.fileManager().isEmpty() == false )
00242       {
00243          QString cmd = m_options.fileManager();
00244          int pos = cmd.indexOf("%m");
00245          if( pos > 0 )
00246          {
00247             cmd = cmd.replace( pos, 2, KShell::quoteArg(disk->mountPoint()) ) + " &";
00248          }
00249          else
00250          {
00251             cmd += ' ' + KShell::quoteArg(disk->mountPoint()) +" &";
00252          }
00253          system( QFile::encodeName(cmd) );
00254       }
00255    }
00256    m_dirty = true;
00257 }
00258 
00259 void KwikDisk::criticallyFull(DiskEntry *disk)
00260 {
00261    kDebug() ;
00262 
00263    if( m_options.popupIfFull() == true )
00264    {
00265       QString msg = i18n("Device [%1] on [%2] is critically full.",
00266                      disk->deviceName(), disk->mountPoint());
00267       KMessageBox::sorry( 0, msg, i18nc("Device is getting critically full", "Warning"));
00268    }
00269 }
00270 
00271 void KwikDisk::changeSettings()
00272 {
00273    if( m_optionDialog == 0 )
00274    {
00275       m_optionDialog = new COptionDialog(0);
00276       if( !m_optionDialog ) return;
00277       connect(m_optionDialog, SIGNAL(valueChanged()),
00278                         this, SLOT(loadSettings()));
00279    }
00280    m_optionDialog->show();
00281 }
00282 
00283 void KwikDisk::startKDF()
00284 {
00285    kDebug() ;
00286 
00287    KRun::runCommand("kdf",NULL);
00288 }
00289 
00290 void KwikDisk::invokeHelp()
00291 {
00292    KToolInvocation::invokeHelp("", "kdf");
00293 }
00294 
00295 /*****************************************************************************/
00296 
00297 int main(int argc, char **argv)
00298 {
00299    KLocale::setMainCatalog( "kdf" );
00300 
00301    KAboutData about("kwikdisk", 0, ki18n("KwikDisk"), version, ki18n(description),
00302                   KAboutData::License_GPL, ki18n("(C) 2004 Stanislav Karchebny"),
00303                   KLocalizedString(),  "http://utils.kde.org/projects/kdf",
00304                   "Stanislav.Karchebny@kdemail.net");
00305    about.addAuthor( ki18n("Michael Kropfberger"), ki18n("Original author"),
00306                     "michael.kropfberger@gmx.net" );
00307    about.addAuthor( ki18n("Espen Sand"), ki18n("KDE 2 changes"));
00308    about.addAuthor( ki18n("Stanislav Karchebny"), ki18n("KDE 3 changes"),
00309                     "Stanislav.Karchebny@kdemail.net" );
00310    KCmdLineArgs::init(argc, argv, &about);
00311 
00312    KCmdLineOptions options;
00313    KCmdLineArgs::addCmdLineOptions( options );
00314    KApplication app;
00315    KwikDisk *mainWin = 0;
00316 
00317    mainWin = new KwikDisk;
00318    QObject::connect(mainWin, SIGNAL(quitSelected()), &app, SLOT(quit()));
00319 
00320    // mainWin has WDestructiveClose flag by default, so it will delete itself.
00321    return app.exec();
00322 }
00323 
00324 /*****************************************************************************/
00325 
00326 #include "kwikdisk.moc"

kdf

Skip menu "kdf"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members

kdeutils

Skip menu "kdeutils"
  • ark
  • kcalc
  • kcharselect
  • kdessh
  • kdf
  • kfloppy
  • kgpg
  • ktimer
  • kwallet
  • okteta
  • printer-applet
  • superkaramba
  • sweeper
Generated for kdeutils by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal