1 /* This file is part of the KDE Project
2 Copyright (c) 2008 Sebastian Trueg <trueg@kde.org>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
19 #include "statuswidget.h"
20 #include "indexscheduler.h"
22 #include <KCMultiDialog>
25 #include <KTitleWidget>
26 #include <KStandardDirs>
27 #include <KIO/NetAccess>
28 #include <kio/directorysizejob.h>
30 #include <Soprano/Model>
31 #include <Soprano/QueryResultIterator>
32 #include <Soprano/Vocabulary/Xesam>
34 #include <QtCore/QTimer>
37 Nepomuk::StatusWidget::StatusWidget( Soprano::Model
* model
, IndexScheduler
* scheduler
, QWidget
* parent
)
40 m_indexScheduler( scheduler
),
43 m_updateRequested( false )
45 setupUi( mainWidget() );
47 setCaption( m_title
->text() );
48 setButtons( Ok
|User1
);
49 setDefaultButton( Ok
);
50 setButtonGuiItem( User1
, KGuiItem( i18n( "Configure" ), KIcon( "configure" ) ) );
52 m_title
->setPixmap( KIcon( "nepomuk" ).pixmap( 32, 32 ) );
54 m_updateTimer
.setSingleShot( true );
55 m_updateTimer
.setInterval( 10*1000 ); // do not update multiple times in 10 seconds
56 connect( &m_updateTimer
, SIGNAL( timeout() ),
57 this, SLOT( slotUpdateTimeout() ) );
59 connect( this, SIGNAL( user1Clicked() ),
60 this, SLOT( slotConfigure() ) );
64 Nepomuk::StatusWidget::~StatusWidget()
69 void Nepomuk::StatusWidget::slotUpdateStrigiStatus()
71 bool indexing
= m_indexScheduler
->isIndexing();
72 bool suspended
= m_indexScheduler
->isSuspended();
73 QString folder
= m_indexScheduler
->currentFolder();
76 m_labelStrigiState
->setText( i18n( "File indexer is suspended" ) );
78 m_labelStrigiState
->setText( i18n( "Strigi is currently indexing files in folder %1", folder
) );
80 m_labelStrigiState
->setText( i18n( "File indexer is idle" ) );
84 void Nepomuk::StatusWidget::slotUpdateStoreStatus()
86 if ( !m_updating
&& !m_updateTimer
.isActive() ) {
89 // update storage size
90 // ========================================
91 QString path
= KStandardDirs::locateLocal( "data", "nepomuk/repository/main/", false );
92 KIO::DirectorySizeJob
* job
= KIO::directorySize( path
);
93 if ( KIO::NetAccess::synchronousRun( job
, this ) )
94 m_labelStoreSize
->setText( KIO::convertSize( job
->totalSize() ) );
96 m_labelStoreSize
->setText( i18n( "Calculation failed" ) );
100 // ========================================
101 Soprano::QueryResultIterator it
= m_model
->executeQuery( QString( "select distinct ?r where { ?r a <%1> . }" )
102 .arg( Soprano::Vocabulary::Xesam::File().toString() ),
103 Soprano::Query::QueryLanguageSparql
);
105 while ( it
.next() ) {
106 // a bit of hacking to keep the GUI responsive
107 // TODO: if we don't get aggregate functions in SPARQL soon, use a thread
108 if ( cnt
% 100 == 0 )
109 QApplication::processEvents();
112 m_labelFileCount
->setText( i18np( "1 file in index", "%1 files in index", cnt
) );
116 // start the timer to avoid too many updates
117 m_updateTimer
.start();
120 m_updateRequested
= true;
125 void Nepomuk::StatusWidget::slotUpdateTimeout()
127 if ( m_updateRequested
) {
128 m_updateRequested
= false;
129 slotUpdateStoreStatus();
134 void Nepomuk::StatusWidget::slotConfigure()
137 dlg
.addModule( "kcm_nepomuk" );
141 #include <QApplication>
142 #include <QDesktopWidget>
144 // from kdialog.cpp since KDialog::centerOnScreen will simply do nothing on X11!
145 static QRect
screenRect( QWidget
*widget
, int screen
)
147 QDesktopWidget
*desktop
= QApplication::desktop();
148 KConfig
gc( "kdeglobals", KConfig::NoGlobals
);
149 KConfigGroup
cg(&gc
, "Windows" );
150 if ( desktop
->isVirtualDesktop() &&
151 cg
.readEntry( "XineramaEnabled", true ) &&
152 cg
.readEntry( "XineramaPlacementEnabled", true ) ) {
154 if ( screen
< 0 || screen
>= desktop
->numScreens() ) {
156 screen
= desktop
->primaryScreen();
157 else if ( screen
== -3 )
158 screen
= desktop
->screenNumber( QCursor::pos() );
160 screen
= desktop
->screenNumber( widget
);
163 return desktop
->availableGeometry( screen
);
165 return desktop
->geometry();
168 void Nepomuk::StatusWidget::showEvent( QShowEvent
* event
)
170 if ( !m_connected
) {
171 connect( m_indexScheduler
, SIGNAL( indexingStarted() ),
172 this, SLOT( slotUpdateStrigiStatus() ) );
173 connect( m_indexScheduler
, SIGNAL( indexingStopped() ),
174 this, SLOT( slotUpdateStrigiStatus() ) );
175 connect( m_indexScheduler
, SIGNAL( indexingFolder(QString
) ),
176 this, SLOT( slotUpdateStrigiStatus() ) );
178 connect( m_model
, SIGNAL( statementsAdded() ),
179 this, SLOT( slotUpdateStoreStatus() ) );
180 connect( m_model
, SIGNAL( statementsRemoved() ),
181 this, SLOT( slotUpdateStoreStatus() ) );
186 QTimer::singleShot( 0, this, SLOT( slotUpdateStoreStatus() ) );
187 QTimer::singleShot( 0, this, SLOT( slotUpdateStrigiStatus() ) );
189 KDialog::showEvent( event
);
191 QRect rect
= screenRect( this, -1 );
192 move( rect
.center().x() - width() / 2,
193 rect
.center().y() - height() / 2 );
194 //KDialog::centerOnScreen( this );
198 void Nepomuk::StatusWidget::hideEvent( QHideEvent
* event
)
201 m_indexScheduler
->disconnect( this );
202 m_model
->disconnect( this );
206 KDialog::hideEvent( event
);
209 #include "statuswidget.moc"