xref: /glogg/src/mainwindow.cpp (revision b423cd88e57c52bc4c23c685b57ee676012b10f4)
1 /*
2  * Copyright (C) 2009, 2010, 2011, 2013, 2014 Nicolas Bonnefon and other contributors
3  *
4  * This file is part of glogg.
5  *
6  * glogg is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * glogg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with glogg.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 // This file implements MainWindow. It is responsible for creating and
21 // managing the menus, the toolbar, and the CrawlerWidget. It also
22 // load/save the settings on opening/closing of the app
23 
24 #include <iostream>
25 #include <cassert>
26 
27 #include <QAction>
28 #include <QDesktopWidget>
29 #include <QMenuBar>
30 #include <QToolBar>
31 #include <QFileInfo>
32 #include <QFileDialog>
33 #include <QClipboard>
34 #include <QMessageBox>
35 #include <QCloseEvent>
36 #include <QDragEnterEvent>
37 #include <QMimeData>
38 #include <QUrl>
39 
40 #include "log.h"
41 
42 #include "mainwindow.h"
43 
44 #include "sessioninfo.h"
45 #include "recentfiles.h"
46 #include "crawlerwidget.h"
47 #include "filtersdialog.h"
48 #include "optionsdialog.h"
49 #include "persistentinfo.h"
50 #include "menuactiontooltipbehavior.h"
51 #include "tabbedcrawlerwidget.h"
52 
53 // Returns the size in human readable format
54 static QString readableSize( qint64 size );
55 
56 MainWindow::MainWindow( std::unique_ptr<Session> session ) :
57     session_( std::move( session )  ),
58     recentFiles( Persistent<RecentFiles>( "recentFiles" ) ),
59     mainIcon_(),
60     signalMux_(),
61     quickFindMux_( session_->getQuickFindPattern() ),
62     mainTabWidget_()
63 {
64     createActions();
65     createMenus();
66     // createContextMenu();
67     createToolBars();
68     // createStatusBar();
69 
70     setAcceptDrops( true );
71 
72     // Default geometry
73     const QRect geometry = QApplication::desktop()->availableGeometry( this );
74     setGeometry( geometry.x() + 20, geometry.y() + 40,
75             geometry.width() - 140, geometry.height() - 140 );
76 
77     mainIcon_.addFile( ":/images/hicolor/16x16/glogg.png" );
78     mainIcon_.addFile( ":/images/hicolor/24x24/glogg.png" );
79     mainIcon_.addFile( ":/images/hicolor/32x32/glogg.png" );
80     mainIcon_.addFile( ":/images/hicolor/48x48/glogg.png" );
81 
82     setWindowIcon( mainIcon_ );
83 
84     readSettings();
85 
86     // Connect the signals to the mux (they will be forwarded to the
87     // "current" crawlerwidget
88 
89     // Send actions to the crawlerwidget
90     signalMux_.connect( this, SIGNAL( followSet( bool ) ),
91             SIGNAL( followSet( bool ) ) );
92     signalMux_.connect( this, SIGNAL( optionsChanged() ),
93             SLOT( applyConfiguration() ) );
94 
95     // Actions from the CrawlerWidget
96     signalMux_.connect( SIGNAL( followDisabled() ),
97             this, SLOT( disableFollow() ) );
98     signalMux_.connect( SIGNAL( updateLineNumber( int ) ),
99             this, SLOT( lineNumberHandler( int ) ) );
100 
101     // Register for progress status bar
102     signalMux_.connect( SIGNAL( loadingProgressed( int ) ),
103             this, SLOT( updateLoadingProgress( int ) ) );
104     signalMux_.connect( SIGNAL( loadingFinished( bool ) ),
105             this, SLOT( displayNormalStatus( bool ) ) );
106 
107     // Configure the main tabbed widget
108     mainTabWidget_.setDocumentMode( true );
109     mainTabWidget_.setMovable( true );
110     //mainTabWidget_.setTabShape( QTabWidget::Triangular );
111     mainTabWidget_.setTabsClosable( true );
112 
113     connect( &mainTabWidget_, SIGNAL( tabCloseRequested ( int ) ),
114             this, SLOT( closeTab( int ) ) );
115 
116     // Establish the QuickFindWidget and mux ( to send requests from the
117     // QFWidget to the right window )
118     /*
119     connect( quickFindWidget_, SIGNAL( close() ),
120              this, SLOT( hideQuickFindBar() ) );
121              */
122     connect( &quickFindWidget_, SIGNAL( patternConfirmed( const QString&, bool ) ),
123              &quickFindMux_, SLOT( confirmPattern( const QString&, bool ) ) );
124     connect( &quickFindWidget_, SIGNAL( patternUpdated( const QString&, bool ) ),
125              &quickFindMux_, SLOT( setNewPattern( const QString&, bool ) ) );
126     connect( &quickFindWidget_, SIGNAL( cancelSearch() ),
127              &quickFindMux_, SLOT( cancelSearch() ) );
128     connect( &quickFindWidget_, SIGNAL( searchForward() ),
129              &quickFindMux_, SLOT( searchForward() ) );
130     connect( &quickFindWidget_, SIGNAL( searchBackward() ),
131              &quickFindMux_, SLOT( searchBackward() ) );
132     connect( &quickFindWidget_, SIGNAL( searchNext() ),
133              &quickFindMux_, SLOT( searchNext() ) );
134 
135     // QuickFind changes coming from the views
136     /*
137     connect( quickFindMux_, SIGNAL( patternChanged( const QString& ) ),
138              this, SLOT( changeQFPattern( const QString& ) ) );
139              */
140     connect( &quickFindMux_, SIGNAL( notify( const QFNotification& ) ),
141              &quickFindWidget_, SLOT( notify( const QFNotification& ) ) );
142     connect( &quickFindMux_, SIGNAL( clearNotification() ),
143              &quickFindWidget_, SLOT( clearNotification() ) );
144 
145     // Construct the QuickFind bar
146     quickFindWidget_.hide();
147 
148     QWidget* central_widget = new QWidget();
149     QVBoxLayout* main_layout = new QVBoxLayout();
150     main_layout->addWidget( &mainTabWidget_ );
151     main_layout->addWidget( &quickFindWidget_ );
152     central_widget->setLayout( main_layout );
153 
154     setCentralWidget( central_widget );
155 }
156 
157 void MainWindow::loadInitialFile( QString fileName )
158 {
159     LOG(logDEBUG) << "loadInitialFile";
160 
161     // Is there a file passed as argument?
162     if ( !fileName.isEmpty() )
163         loadFile( fileName );
164     else if ( !previousFile.isEmpty() )
165         loadFile( previousFile );
166 }
167 
168 //
169 // Private functions
170 //
171 
172 // Menu actions
173 void MainWindow::createActions()
174 {
175     Configuration& config = Persistent<Configuration>( "settings" );
176 
177     openAction = new QAction(tr("&Open..."), this);
178     openAction->setShortcut(QKeySequence::Open);
179     openAction->setIcon( QIcon(":/images/open16.png") );
180     openAction->setStatusTip(tr("Open a file"));
181     connect(openAction, SIGNAL(triggered()), this, SLOT(open()));
182 
183     // Recent files
184     for (int i = 0; i < MaxRecentFiles; ++i) {
185         recentFileActions[i] = new QAction(this);
186         recentFileActions[i]->setVisible(false);
187         connect(recentFileActions[i], SIGNAL(triggered()),
188                 this, SLOT(openRecentFile()));
189     }
190 
191     exitAction = new QAction(tr("E&xit"), this);
192     exitAction->setShortcut(tr("Ctrl+Q"));
193     exitAction->setStatusTip(tr("Exit the application"));
194     connect( exitAction, SIGNAL(triggered()), this, SLOT(close()) );
195 
196     copyAction = new QAction(tr("&Copy"), this);
197     copyAction->setShortcut(QKeySequence::Copy);
198     copyAction->setStatusTip(tr("Copy the selection"));
199     connect( copyAction, SIGNAL(triggered()), this, SLOT(copy()) );
200 
201     selectAllAction = new QAction(tr("Select &All"), this);
202     selectAllAction->setShortcut(tr("Ctrl+A"));
203     selectAllAction->setStatusTip(tr("Select all the text"));
204     connect( selectAllAction, SIGNAL(triggered()),
205              this, SLOT( selectAll() ) );
206 
207     findAction = new QAction(tr("&Find..."), this);
208     findAction->setShortcut(QKeySequence::Find);
209     findAction->setStatusTip(tr("Find the text"));
210     connect( findAction, SIGNAL(triggered()),
211             this, SLOT( find() ) );
212 
213     overviewVisibleAction = new QAction( tr("Matches &overview"), this );
214     overviewVisibleAction->setCheckable( true );
215     overviewVisibleAction->setChecked( config.isOverviewVisible() );
216     connect( overviewVisibleAction, SIGNAL( toggled( bool ) ),
217             this, SLOT( toggleOverviewVisibility( bool )) );
218 
219     lineNumbersVisibleInMainAction =
220         new QAction( tr("Line &numbers in main view"), this );
221     lineNumbersVisibleInMainAction->setCheckable( true );
222     lineNumbersVisibleInMainAction->setChecked( config.mainLineNumbersVisible() );
223     connect( lineNumbersVisibleInMainAction, SIGNAL( toggled( bool ) ),
224             this, SLOT( toggleMainLineNumbersVisibility( bool )) );
225 
226     lineNumbersVisibleInFilteredAction =
227         new QAction( tr("Line &numbers in filtered view"), this );
228     lineNumbersVisibleInFilteredAction->setCheckable( true );
229     lineNumbersVisibleInFilteredAction->setChecked( config.filteredLineNumbersVisible() );
230     connect( lineNumbersVisibleInFilteredAction, SIGNAL( toggled( bool ) ),
231             this, SLOT( toggleFilteredLineNumbersVisibility( bool )) );
232 
233     followAction = new QAction( tr("&Follow File"), this );
234     followAction->setShortcut(Qt::Key_F);
235     followAction->setCheckable(true);
236     connect( followAction, SIGNAL(toggled( bool )),
237             this, SIGNAL(followSet( bool )) );
238 
239     reloadAction = new QAction( tr("&Reload"), this );
240     reloadAction->setShortcut(QKeySequence::Refresh);
241     reloadAction->setIcon( QIcon(":/images/reload16.png") );
242     signalMux_.connect( reloadAction, SIGNAL(triggered()), SLOT(reload()) );
243 
244     stopAction = new QAction( tr("&Stop"), this );
245     stopAction->setIcon( QIcon(":/images/stop16.png") );
246     stopAction->setEnabled( false );
247     signalMux_.connect( stopAction, SIGNAL(triggered()), SLOT(stopLoading()) );
248 
249     filtersAction = new QAction(tr("&Filters..."), this);
250     filtersAction->setStatusTip(tr("Show the Filters box"));
251     connect( filtersAction, SIGNAL(triggered()), this, SLOT(filters()) );
252 
253     optionsAction = new QAction(tr("&Options..."), this);
254     optionsAction->setStatusTip(tr("Show the Options box"));
255     connect( optionsAction, SIGNAL(triggered()), this, SLOT(options()) );
256 
257     aboutAction = new QAction(tr("&About"), this);
258     aboutAction->setStatusTip(tr("Show the About box"));
259     connect( aboutAction, SIGNAL(triggered()), this, SLOT(about()) );
260 
261     aboutQtAction = new QAction(tr("About &Qt"), this);
262     aboutAction->setStatusTip(tr("Show the Qt library's About box"));
263     connect( aboutQtAction, SIGNAL(triggered()), this, SLOT(aboutQt()) );
264 }
265 
266 void MainWindow::createMenus()
267 {
268     fileMenu = menuBar()->addMenu( tr("&File") );
269     fileMenu->addAction( openAction );
270     fileMenu->addSeparator();
271     for (int i = 0; i < MaxRecentFiles; ++i) {
272         fileMenu->addAction( recentFileActions[i] );
273         recentFileActionBehaviors[i] =
274             new MenuActionToolTipBehavior(recentFileActions[i], fileMenu, this);
275     }
276     fileMenu->addSeparator();
277     fileMenu->addAction( exitAction );
278 
279     editMenu = menuBar()->addMenu( tr("&Edit") );
280     editMenu->addAction( copyAction );
281     editMenu->addAction( selectAllAction );
282     editMenu->addSeparator();
283     editMenu->addAction( findAction );
284 
285     viewMenu = menuBar()->addMenu( tr("&View") );
286     viewMenu->addAction( overviewVisibleAction );
287     viewMenu->addSeparator();
288     viewMenu->addAction( lineNumbersVisibleInMainAction );
289     viewMenu->addAction( lineNumbersVisibleInFilteredAction );
290     viewMenu->addSeparator();
291     viewMenu->addAction( followAction );
292     viewMenu->addSeparator();
293     viewMenu->addAction( reloadAction );
294 
295     toolsMenu = menuBar()->addMenu( tr("&Tools") );
296     toolsMenu->addAction( filtersAction );
297     toolsMenu->addSeparator();
298     toolsMenu->addAction( optionsAction );
299 
300     menuBar()->addSeparator();
301 
302     helpMenu = menuBar()->addMenu( tr("&Help") );
303     helpMenu->addAction( aboutAction );
304 }
305 
306 void MainWindow::createToolBars()
307 {
308     infoLine = new InfoLine();
309     infoLine->setFrameStyle( QFrame::WinPanel | QFrame::Sunken );
310     infoLine->setLineWidth( 0 );
311 
312     lineNbField = new QLabel( );
313     lineNbField->setText( "Line 0" );
314     lineNbField->setAlignment( Qt::AlignLeft | Qt::AlignVCenter );
315     lineNbField->setMinimumSize(
316             lineNbField->fontMetrics().size( 0, "Line 0000000") );
317 
318     toolBar = addToolBar( tr("&Toolbar") );
319     toolBar->setIconSize( QSize( 16, 16 ) );
320     toolBar->setMovable( false );
321     toolBar->addAction( openAction );
322     toolBar->addAction( reloadAction );
323     toolBar->addWidget( infoLine );
324     toolBar->addAction( stopAction );
325     toolBar->addWidget( lineNbField );
326 }
327 
328 //
329 // Slots
330 //
331 
332 // Opens the file selection dialog to select a new log file
333 void MainWindow::open()
334 {
335     QString defaultDir = ".";
336 
337     // Default to the path of the current file if there is one
338     if ( !currentFile.isEmpty() ) {
339         QFileInfo fileInfo = QFileInfo( currentFile );
340         defaultDir = fileInfo.path();
341     }
342 
343     QString fileName = QFileDialog::getOpenFileName(this,
344             tr("Open file"), defaultDir, tr("All files (*)"));
345     if (!fileName.isEmpty())
346         loadFile(fileName);
347 }
348 
349 // Opens a log file from the recent files list
350 void MainWindow::openRecentFile()
351 {
352     QAction* action = qobject_cast<QAction*>(sender());
353     if (action)
354         loadFile(action->data().toString());
355 }
356 
357 // Select all the text in the currently selected view
358 void MainWindow::selectAll()
359 {
360     CrawlerWidget* current = currentCrawlerWidget();
361 
362     if ( current )
363         current->selectAll();
364 }
365 
366 // Copy the currently selected line into the clipboard
367 void MainWindow::copy()
368 {
369     static QClipboard* clipboard = QApplication::clipboard();
370     CrawlerWidget* current = currentCrawlerWidget();
371 
372     if ( current ) {
373         clipboard->setText( current->getSelectedText() );
374 
375         // Put it in the global selection as well (X11 only)
376         clipboard->setText( current->getSelectedText(),
377                 QClipboard::Selection );
378     }
379 }
380 
381 // Display the QuickFind bar
382 void MainWindow::find()
383 {
384     displayQuickFindBar( QuickFindMux::Forward );
385 }
386 
387 // Opens the 'Filters' dialog box
388 void MainWindow::filters()
389 {
390     FiltersDialog dialog(this);
391     signalMux_.connect(&dialog, SIGNAL( optionsChanged() ), SLOT( applyConfiguration() ));
392     dialog.exec();
393     signalMux_.disconnect(&dialog, SIGNAL( optionsChanged() ), SLOT( applyConfiguration() ));
394 }
395 
396 // Opens the 'Options' modal dialog box
397 void MainWindow::options()
398 {
399     OptionsDialog dialog(this);
400     signalMux_.connect(&dialog, SIGNAL( optionsChanged() ), SLOT( applyConfiguration() ));
401     dialog.exec();
402     signalMux_.disconnect(&dialog, SIGNAL( optionsChanged() ), SLOT( applyConfiguration() ));
403 }
404 
405 // Opens the 'About' dialog box.
406 void MainWindow::about()
407 {
408     QMessageBox::about(this, tr("About glogg"),
409             tr("<h2>glogg " GLOGG_VERSION "</h2>"
410                 "<p>A fast, advanced log explorer."
411 #ifdef GLOGG_COMMIT
412                 "<p>Built " GLOGG_DATE " from " GLOGG_COMMIT
413 #endif
414                 "<p>Copyright &copy; 2009, 2010, 2011, 2012, 2013 Nicolas Bonnefon and other contributors"
415                 "<p>You may modify and redistribute the program under the terms of the GPL (version 3 or later)." ) );
416 }
417 
418 // Opens the 'About Qt' dialog box.
419 void MainWindow::aboutQt()
420 {
421 }
422 
423 void MainWindow::toggleOverviewVisibility( bool isVisible )
424 {
425     Configuration& config = Persistent<Configuration>( "settings" );
426     config.setOverviewVisible( isVisible );
427     emit optionsChanged();
428 }
429 
430 void MainWindow::toggleMainLineNumbersVisibility( bool isVisible )
431 {
432     Configuration& config = Persistent<Configuration>( "settings" );
433     config.setMainLineNumbersVisible( isVisible );
434     emit optionsChanged();
435 }
436 
437 void MainWindow::toggleFilteredLineNumbersVisibility( bool isVisible )
438 {
439     Configuration& config = Persistent<Configuration>( "settings" );
440     config.setFilteredLineNumbersVisible( isVisible );
441     emit optionsChanged();
442 }
443 
444 void MainWindow::disableFollow()
445 {
446     followAction->setChecked( false );
447 }
448 
449 void MainWindow::lineNumberHandler( int line )
450 {
451     // The line number received is the internal (starts at 0)
452     lineNbField->setText( tr( "Line %1" ).arg( line + 1 ) );
453 }
454 
455 void MainWindow::updateLoadingProgress( int progress )
456 {
457     LOG(logDEBUG) << "Loading progress: " << progress;
458 
459     // We ignore 0% and 100% to avoid a flash when the file (or update)
460     // is very short.
461     if ( progress > 0 && progress < 100 ) {
462         infoLine->setText( loadingFileName + tr( " - Indexing lines... (%1 %)" ).arg( progress ) );
463         infoLine->displayGauge( progress );
464 
465         stopAction->setEnabled( true );
466     }
467 }
468 
469 void MainWindow::displayNormalStatus( bool success )
470 {
471     QLocale defaultLocale;
472 
473     LOG(logDEBUG) << "displayNormalStatus";
474 
475     if ( success )
476         setCurrentFile( loadingFileName );
477 
478     uint64_t fileSize;
479     uint32_t fileNbLine;
480     QDateTime lastModified;
481 
482     session_->getFileInfo( currentCrawlerWidget(),
483             &fileSize, &fileNbLine, &lastModified );
484     if ( lastModified.isValid() ) {
485         const QString date =
486             defaultLocale.toString( lastModified, QLocale::NarrowFormat );
487         infoLine->setText( tr( "%1 (%2 - %3 lines - modified on %4)" )
488                 .arg(currentFile).arg(readableSize(fileSize))
489                 .arg(fileNbLine).arg( date ) );
490     }
491     else {
492         infoLine->setText( tr( "%1 (%2 - %3 lines)" )
493                 .arg(currentFile).arg(readableSize(fileSize))
494                 .arg(fileNbLine) );
495     }
496 
497     infoLine->hideGauge();
498     stopAction->setEnabled( false );
499 
500     // Now everything is ready, we can finally show the file!
501     currentCrawlerWidget()->show();
502     mainTabWidget_.setEnabled( true );
503 }
504 
505 void MainWindow::closeTab( int index )
506 {
507     auto widget = dynamic_cast<CrawlerWidget*>(
508             mainTabWidget_.widget( index ) );
509 
510     assert( widget );
511 
512     mainTabWidget_.removeTab( index );
513     session_->close( widget );
514     delete widget;
515 }
516 
517 //
518 // Events
519 //
520 
521 // Closes the application
522 void MainWindow::closeEvent( QCloseEvent *event )
523 {
524     writeSettings();
525     event->accept();
526 }
527 
528 // Accepts the drag event if it looks like a filename
529 void MainWindow::dragEnterEvent( QDragEnterEvent* event )
530 {
531     if ( event->mimeData()->hasFormat( "text/uri-list" ) )
532         event->acceptProposedAction();
533 }
534 
535 // Tries and loads the file if the URL dropped is local
536 void MainWindow::dropEvent( QDropEvent* event )
537 {
538     QList<QUrl> urls = event->mimeData()->urls();
539     if ( urls.isEmpty() )
540         return;
541 
542     QString fileName = urls.first().toLocalFile();
543     if ( fileName.isEmpty() )
544         return;
545 
546     loadFile( fileName );
547 }
548 
549 //
550 // Private functions
551 //
552 
553 // Create a CrawlerWidget for the passed file, start its loading
554 // and update the title bar.
555 // The loading is done asynchronously.
556 bool MainWindow::loadFile( const QString& fileName )
557 {
558     LOG(logDEBUG) << "loadFile ( " << fileName.toStdString() << " )";
559 
560     // Load the file
561     loadingFileName = fileName;
562 
563     CrawlerWidget* crawler_widget = dynamic_cast<CrawlerWidget*>(
564             session_->open( fileName.toStdString(),
565                 []() { return new CrawlerWidget(); } ) );
566     assert( crawler_widget );
567 
568     // We won't show the widget until the file is fully loaded
569     crawler_widget->hide();
570 
571     // We disable the tab widget to avoid having someone switch
572     // tab during loading. (maybe FIXME)
573     mainTabWidget_.setEnabled( false );
574 
575     int index = mainTabWidget_.addTab( crawler_widget, strippedName( fileName ) );
576 
577     // Setting the new tab, the user will see a blank page for the duration
578     // of the loading, with no way to switch to another tab
579     mainTabWidget_.setCurrentIndex( index );
580 
581     signalMux_.setCurrentDocument( crawler_widget );
582     quickFindMux_.registerSelector( crawler_widget );
583 
584     // FIXME: is it necessary?
585     emit optionsChanged();
586 
587     // We start with the empty file
588     setCurrentFile( "" );
589 
590     LOG(logDEBUG) << "Success loading file " << fileName.toStdString();
591     return true;
592 }
593 
594 // Strips the passed filename from its directory part.
595 QString MainWindow::strippedName( const QString& fullFileName ) const
596 {
597     return QFileInfo( fullFileName ).fileName();
598 }
599 
600 // Return the currently active CrawlerWidget, or NULL if none
601 CrawlerWidget* MainWindow::currentCrawlerWidget() const
602 {
603     auto current = dynamic_cast<CrawlerWidget*>(
604             mainTabWidget_.currentWidget() );
605 
606     return current;
607 }
608 
609 // Add the filename to the recent files list and update the title bar.
610 void MainWindow::setCurrentFile( const QString& fileName )
611 {
612     if ( fileName != currentFile )
613     {
614         // Change the current file
615         currentFile = fileName;
616         QString shownName = tr( "Untitled" );
617         if ( !currentFile.isEmpty() ) {
618             // (reload the list first in case another glogg changed it)
619             GetPersistentInfo().retrieve( "recentFiles" );
620             recentFiles.addRecent( currentFile );
621             GetPersistentInfo().save( "recentFiles" );
622             updateRecentFileActions();
623             shownName = strippedName( currentFile );
624         }
625 
626         setWindowTitle(
627                 tr("%1 - %2").arg(shownName).arg(tr("glogg"))
628 #ifdef GLOGG_COMMIT
629                 + " (dev build " GLOGG_VERSION ")"
630 #endif
631                 );
632     }
633     else
634     {
635         // Nothing, happens when e.g., the file is reloaded
636     }
637 }
638 
639 // Updates the actions for the recent files.
640 // Must be called after having added a new name to the list.
641 void MainWindow::updateRecentFileActions()
642 {
643     QStringList recent_files = recentFiles.recentFiles();
644 
645     for ( int j = 0; j < MaxRecentFiles; ++j ) {
646         if ( j < recent_files.count() ) {
647             QString text = tr("&%1 %2").arg(j + 1).arg(strippedName(recent_files[j]));
648             recentFileActions[j]->setText( text );
649             recentFileActions[j]->setToolTip( recent_files[j] );
650             recentFileActions[j]->setData( recent_files[j] );
651             recentFileActions[j]->setVisible( true );
652         }
653         else {
654             recentFileActions[j]->setVisible( false );
655         }
656     }
657 
658     // separatorAction->setVisible(!recentFiles.isEmpty());
659 }
660 
661 // Write settings to permanent storage
662 void MainWindow::writeSettings()
663 {
664     // Save the session
665     SessionInfo& session = Persistent<SessionInfo>( "session" );
666     session.setGeometry( saveGeometry() );
667     //session.setCrawlerState( crawlerWidget->saveState() );
668     session.setCurrentFile( currentFile );
669     GetPersistentInfo().save( QString( "session" ) );
670 
671     // User settings
672     GetPersistentInfo().save( QString( "settings" ) );
673 }
674 
675 // Read settings from permanent storage
676 void MainWindow::readSettings()
677 {
678     // Get and restore the session
679     GetPersistentInfo().retrieve( QString( "session" ) );
680     SessionInfo session = Persistent<SessionInfo>( "session" );
681     restoreGeometry( session.geometry() );
682     previousFile = session.currentFile();
683     /*
684      * FIXME: should be in the session
685     crawlerWidget->restoreState( session.crawlerState() );
686     */
687 
688     // History of recent files
689     GetPersistentInfo().retrieve( QString( "recentFiles" ) );
690     updateRecentFileActions();
691 
692     GetPersistentInfo().retrieve( QString( "settings" ) );
693     GetPersistentInfo().retrieve( QString( "filterSet" ) );
694 }
695 
696 void MainWindow::displayQuickFindBar( QuickFindMux::QFDirection direction )
697 {
698     LOG(logDEBUG) << "MainWindow::displayQuickFindBar";
699 
700     // Remember who had the focus
701     //qfSavedFocus_ = QApplication::focusWidget();
702 
703     quickFindMux_.setDirection( direction );
704     quickFindWidget_.userActivate();
705 }
706 
707 // Returns the size in human readable format
708 static QString readableSize( qint64 size )
709 {
710     static const QString sizeStrs[] = {
711         QObject::tr("B"), QObject::tr("KiB"), QObject::tr("MiB"),
712         QObject::tr("GiB"), QObject::tr("TiB") };
713 
714     QLocale defaultLocale;
715     unsigned int i;
716     double humanSize = size;
717 
718     for ( i=0; i+1 < (sizeof(sizeStrs)/sizeof(QString)) && (humanSize/1024.0) >= 1024.0; i++ )
719         humanSize /= 1024.0;
720 
721     if ( humanSize >= 1024.0 ) {
722         humanSize /= 1024.0;
723         i++;
724     }
725 
726     QString output;
727     if ( i == 0 )
728         // No decimal part if we display straight bytes.
729         output = defaultLocale.toString( (int) humanSize );
730     else
731         output = defaultLocale.toString( humanSize, 'f', 1 );
732 
733     output += QString(" ") + sizeStrs[i];
734 
735     return output;
736 }
737