delay a few things on startup, such as setting the visibility mode, which ensures...
[personal-kdebase.git] / runtime / nepomuk / services / strigi / statuswidget.cpp
blobd26cc4a5e66277a42d45548e04d2450e766780c9
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>
23 #include <KIcon>
24 #include <KLocale>
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 )
38 : KDialog( parent ),
39 m_model( model ),
40 m_indexScheduler( scheduler ),
41 m_connected( false ),
42 m_updating( false ),
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();
75 if ( suspended )
76 m_labelStrigiState->setText( i18n( "File indexer is suspended" ) );
77 else if ( indexing )
78 m_labelStrigiState->setText( i18n( "Strigi is currently indexing files in folder %1", folder ) );
79 else
80 m_labelStrigiState->setText( i18n( "File indexer is idle" ) );
84 void Nepomuk::StatusWidget::slotUpdateStoreStatus()
86 if ( !m_updating && !m_updateTimer.isActive() ) {
87 m_updating = true;
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() ) );
95 else
96 m_labelStoreSize->setText( i18n( "Calculation failed" ) );
99 // update file count
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 );
104 int cnt = 0;
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();
110 ++cnt;
112 m_labelFileCount->setText( i18np( "1 file in index", "%1 files in index", cnt ) );
114 m_updating = false;
116 // start the timer to avoid too many updates
117 m_updateTimer.start();
119 else {
120 m_updateRequested = true;
125 void Nepomuk::StatusWidget::slotUpdateTimeout()
127 if ( m_updateRequested ) {
128 m_updateRequested = false;
129 slotUpdateStoreStatus();
134 void Nepomuk::StatusWidget::slotConfigure()
136 KCMultiDialog dlg;
137 dlg.addModule( "kcm_nepomuk" );
138 dlg.exec();
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() ) {
155 if ( screen == -1 )
156 screen = desktop->primaryScreen();
157 else if ( screen == -3 )
158 screen = desktop->screenNumber( QCursor::pos() );
159 else
160 screen = desktop->screenNumber( widget );
163 return desktop->availableGeometry( screen );
164 } else
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() ) );
183 m_connected = true;
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 )
200 if ( m_connected ) {
201 m_indexScheduler->disconnect( this );
202 m_model->disconnect( this );
203 m_connected = false;
206 KDialog::hideEvent( event );
209 #include "statuswidget.moc"