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 createToolBars(); 67 // createStatusBar(); 68 69 setAcceptDrops( true ); 70 71 // Default geometry 72 const QRect geometry = QApplication::desktop()->availableGeometry( this ); 73 setGeometry( geometry.x() + 20, geometry.y() + 40, 74 geometry.width() - 140, geometry.height() - 140 ); 75 76 mainIcon_.addFile( ":/images/hicolor/16x16/glogg.png" ); 77 mainIcon_.addFile( ":/images/hicolor/24x24/glogg.png" ); 78 mainIcon_.addFile( ":/images/hicolor/32x32/glogg.png" ); 79 mainIcon_.addFile( ":/images/hicolor/48x48/glogg.png" ); 80 81 setWindowIcon( mainIcon_ ); 82 83 readSettings(); 84 85 // Connect the signals to the mux (they will be forwarded to the 86 // "current" crawlerwidget 87 88 // Send actions to the crawlerwidget 89 signalMux_.connect( this, SIGNAL( followSet( bool ) ), 90 SIGNAL( followSet( bool ) ) ); 91 signalMux_.connect( this, SIGNAL( optionsChanged() ), 92 SLOT( applyConfiguration() ) ); 93 signalMux_.connect( this, SIGNAL( enteringQuickFind() ), 94 SLOT( enteringQuickFind() ) ); 95 signalMux_.connect( &quickFindWidget_, SIGNAL( close() ), 96 SLOT( exitingQuickFind() ) ); 97 98 // Actions from the CrawlerWidget 99 signalMux_.connect( SIGNAL( followDisabled() ), 100 this, SLOT( disableFollow() ) ); 101 signalMux_.connect( SIGNAL( updateLineNumber( int ) ), 102 this, SLOT( lineNumberHandler( int ) ) ); 103 104 // Register for progress status bar 105 signalMux_.connect( SIGNAL( loadingProgressed( int ) ), 106 this, SLOT( updateLoadingProgress( int ) ) ); 107 signalMux_.connect( SIGNAL( loadingFinished( bool ) ), 108 this, SLOT( displayNormalStatus( bool ) ) ); 109 110 // Configure the main tabbed widget 111 mainTabWidget_.setDocumentMode( true ); 112 mainTabWidget_.setMovable( true ); 113 //mainTabWidget_.setTabShape( QTabWidget::Triangular ); 114 mainTabWidget_.setTabsClosable( true ); 115 116 connect( &mainTabWidget_, SIGNAL( tabCloseRequested( int ) ), 117 this, SLOT( closeTab( int ) ) ); 118 connect( &mainTabWidget_, SIGNAL( currentChanged( int ) ), 119 this, SLOT( currentTabChanged( int ) ) ); 120 121 // Establish the QuickFindWidget and mux ( to send requests from the 122 // QFWidget to the right window ) 123 connect( &quickFindWidget_, SIGNAL( patternConfirmed( const QString&, bool ) ), 124 &quickFindMux_, SLOT( confirmPattern( const QString&, bool ) ) ); 125 connect( &quickFindWidget_, SIGNAL( patternUpdated( const QString&, bool ) ), 126 &quickFindMux_, SLOT( setNewPattern( const QString&, bool ) ) ); 127 connect( &quickFindWidget_, SIGNAL( cancelSearch() ), 128 &quickFindMux_, SLOT( cancelSearch() ) ); 129 connect( &quickFindWidget_, SIGNAL( searchForward() ), 130 &quickFindMux_, SLOT( searchForward() ) ); 131 connect( &quickFindWidget_, SIGNAL( searchBackward() ), 132 &quickFindMux_, SLOT( searchBackward() ) ); 133 connect( &quickFindWidget_, SIGNAL( searchNext() ), 134 &quickFindMux_, SLOT( searchNext() ) ); 135 136 // QuickFind changes coming from the views 137 connect( &quickFindMux_, SIGNAL( patternChanged( const QString& ) ), 138 this, SLOT( changeQFPattern( const QString& ) ) ); 139 connect( &quickFindMux_, SIGNAL( notify( const QFNotification& ) ), 140 &quickFindWidget_, SLOT( notify( const QFNotification& ) ) ); 141 connect( &quickFindMux_, SIGNAL( clearNotification() ), 142 &quickFindWidget_, SLOT( clearNotification() ) ); 143 144 // Construct the QuickFind bar 145 quickFindWidget_.hide(); 146 147 QWidget* central_widget = new QWidget(); 148 QVBoxLayout* main_layout = new QVBoxLayout(); 149 main_layout->setContentsMargins( 0, 0, 0, 0 ); 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::reloadSession() 158 { 159 int current_file_index = -1; 160 161 for ( auto open_file: session_->restore( 162 []() { return new CrawlerWidget(); }, 163 ¤t_file_index ) ) 164 { 165 QString file_name = { open_file.first.c_str() }; 166 CrawlerWidget* crawler_widget = dynamic_cast<CrawlerWidget*>( 167 open_file.second ); 168 169 assert( crawler_widget ); 170 171 mainTabWidget_.addTab( crawler_widget, strippedName( file_name ) ); 172 } 173 174 if ( current_file_index >= 0 ) 175 mainTabWidget_.setCurrentIndex( current_file_index ); 176 } 177 178 void MainWindow::loadInitialFile( QString fileName ) 179 { 180 LOG(logDEBUG) << "loadInitialFile"; 181 182 // Is there a file passed as argument? 183 if ( !fileName.isEmpty() ) 184 loadFile( fileName ); 185 } 186 187 // 188 // Private functions 189 // 190 191 // Menu actions 192 void MainWindow::createActions() 193 { 194 std::shared_ptr<Configuration> config = 195 Persistent<Configuration>( "settings" ); 196 197 openAction = new QAction(tr("&Open..."), this); 198 openAction->setShortcut(QKeySequence::Open); 199 openAction->setIcon( QIcon(":/images/open16.png") ); 200 openAction->setStatusTip(tr("Open a file")); 201 connect(openAction, SIGNAL(triggered()), this, SLOT(open())); 202 203 // Recent files 204 for (int i = 0; i < MaxRecentFiles; ++i) { 205 recentFileActions[i] = new QAction(this); 206 recentFileActions[i]->setVisible(false); 207 connect(recentFileActions[i], SIGNAL(triggered()), 208 this, SLOT(openRecentFile())); 209 } 210 211 exitAction = new QAction(tr("E&xit"), this); 212 exitAction->setShortcut(tr("Ctrl+Q")); 213 exitAction->setStatusTip(tr("Exit the application")); 214 connect( exitAction, SIGNAL(triggered()), this, SLOT(close()) ); 215 216 copyAction = new QAction(tr("&Copy"), this); 217 copyAction->setShortcut(QKeySequence::Copy); 218 copyAction->setStatusTip(tr("Copy the selection")); 219 connect( copyAction, SIGNAL(triggered()), this, SLOT(copy()) ); 220 221 selectAllAction = new QAction(tr("Select &All"), this); 222 selectAllAction->setShortcut(tr("Ctrl+A")); 223 selectAllAction->setStatusTip(tr("Select all the text")); 224 connect( selectAllAction, SIGNAL(triggered()), 225 this, SLOT( selectAll() ) ); 226 227 findAction = new QAction(tr("&Find..."), this); 228 findAction->setShortcut(QKeySequence::Find); 229 findAction->setStatusTip(tr("Find the text")); 230 connect( findAction, SIGNAL(triggered()), 231 this, SLOT( find() ) ); 232 233 overviewVisibleAction = new QAction( tr("Matches &overview"), this ); 234 overviewVisibleAction->setCheckable( true ); 235 overviewVisibleAction->setChecked( config->isOverviewVisible() ); 236 connect( overviewVisibleAction, SIGNAL( toggled( bool ) ), 237 this, SLOT( toggleOverviewVisibility( bool )) ); 238 239 lineNumbersVisibleInMainAction = 240 new QAction( tr("Line &numbers in main view"), this ); 241 lineNumbersVisibleInMainAction->setCheckable( true ); 242 lineNumbersVisibleInMainAction->setChecked( config->mainLineNumbersVisible() ); 243 connect( lineNumbersVisibleInMainAction, SIGNAL( toggled( bool ) ), 244 this, SLOT( toggleMainLineNumbersVisibility( bool )) ); 245 246 lineNumbersVisibleInFilteredAction = 247 new QAction( tr("Line &numbers in filtered view"), this ); 248 lineNumbersVisibleInFilteredAction->setCheckable( true ); 249 lineNumbersVisibleInFilteredAction->setChecked( config->filteredLineNumbersVisible() ); 250 connect( lineNumbersVisibleInFilteredAction, SIGNAL( toggled( bool ) ), 251 this, SLOT( toggleFilteredLineNumbersVisibility( bool )) ); 252 253 followAction = new QAction( tr("&Follow File"), this ); 254 followAction->setShortcut(Qt::Key_F); 255 followAction->setCheckable(true); 256 connect( followAction, SIGNAL(toggled( bool )), 257 this, SIGNAL(followSet( bool )) ); 258 259 reloadAction = new QAction( tr("&Reload"), this ); 260 reloadAction->setShortcut(QKeySequence::Refresh); 261 reloadAction->setIcon( QIcon(":/images/reload16.png") ); 262 signalMux_.connect( reloadAction, SIGNAL(triggered()), SLOT(reload()) ); 263 264 stopAction = new QAction( tr("&Stop"), this ); 265 stopAction->setIcon( QIcon(":/images/stop16.png") ); 266 stopAction->setEnabled( false ); 267 signalMux_.connect( stopAction, SIGNAL(triggered()), SLOT(stopLoading()) ); 268 269 filtersAction = new QAction(tr("&Filters..."), this); 270 filtersAction->setStatusTip(tr("Show the Filters box")); 271 connect( filtersAction, SIGNAL(triggered()), this, SLOT(filters()) ); 272 273 optionsAction = new QAction(tr("&Options..."), this); 274 optionsAction->setStatusTip(tr("Show the Options box")); 275 connect( optionsAction, SIGNAL(triggered()), this, SLOT(options()) ); 276 277 aboutAction = new QAction(tr("&About"), this); 278 aboutAction->setStatusTip(tr("Show the About box")); 279 connect( aboutAction, SIGNAL(triggered()), this, SLOT(about()) ); 280 281 aboutQtAction = new QAction(tr("About &Qt"), this); 282 aboutAction->setStatusTip(tr("Show the Qt library's About box")); 283 connect( aboutQtAction, SIGNAL(triggered()), this, SLOT(aboutQt()) ); 284 } 285 286 void MainWindow::createMenus() 287 { 288 fileMenu = menuBar()->addMenu( tr("&File") ); 289 fileMenu->addAction( openAction ); 290 fileMenu->addSeparator(); 291 for (int i = 0; i < MaxRecentFiles; ++i) { 292 fileMenu->addAction( recentFileActions[i] ); 293 recentFileActionBehaviors[i] = 294 new MenuActionToolTipBehavior(recentFileActions[i], fileMenu, this); 295 } 296 fileMenu->addSeparator(); 297 fileMenu->addAction( exitAction ); 298 299 editMenu = menuBar()->addMenu( tr("&Edit") ); 300 editMenu->addAction( copyAction ); 301 editMenu->addAction( selectAllAction ); 302 editMenu->addSeparator(); 303 editMenu->addAction( findAction ); 304 305 viewMenu = menuBar()->addMenu( tr("&View") ); 306 viewMenu->addAction( overviewVisibleAction ); 307 viewMenu->addSeparator(); 308 viewMenu->addAction( lineNumbersVisibleInMainAction ); 309 viewMenu->addAction( lineNumbersVisibleInFilteredAction ); 310 viewMenu->addSeparator(); 311 viewMenu->addAction( followAction ); 312 viewMenu->addSeparator(); 313 viewMenu->addAction( reloadAction ); 314 315 toolsMenu = menuBar()->addMenu( tr("&Tools") ); 316 toolsMenu->addAction( filtersAction ); 317 toolsMenu->addSeparator(); 318 toolsMenu->addAction( optionsAction ); 319 320 menuBar()->addSeparator(); 321 322 helpMenu = menuBar()->addMenu( tr("&Help") ); 323 helpMenu->addAction( aboutAction ); 324 } 325 326 void MainWindow::createToolBars() 327 { 328 infoLine = new InfoLine(); 329 infoLine->setFrameStyle( QFrame::WinPanel | QFrame::Sunken ); 330 infoLine->setLineWidth( 0 ); 331 332 lineNbField = new QLabel( ); 333 lineNbField->setText( "Line 0" ); 334 lineNbField->setAlignment( Qt::AlignLeft | Qt::AlignVCenter ); 335 lineNbField->setMinimumSize( 336 lineNbField->fontMetrics().size( 0, "Line 0000000") ); 337 338 toolBar = addToolBar( tr("&Toolbar") ); 339 toolBar->setIconSize( QSize( 16, 16 ) ); 340 toolBar->setMovable( false ); 341 toolBar->addAction( openAction ); 342 toolBar->addAction( reloadAction ); 343 toolBar->addWidget( infoLine ); 344 toolBar->addAction( stopAction ); 345 toolBar->addWidget( lineNbField ); 346 } 347 348 // 349 // Slots 350 // 351 352 // Opens the file selection dialog to select a new log file 353 void MainWindow::open() 354 { 355 QString defaultDir = "."; 356 357 // Default to the path of the current file if there is one 358 if ( auto current = currentCrawlerWidget() ) 359 { 360 std::string current_file = session_->getFilename( current ); 361 QFileInfo fileInfo = QFileInfo( QString( current_file.c_str() ) ); 362 defaultDir = fileInfo.path(); 363 } 364 365 QString fileName = QFileDialog::getOpenFileName(this, 366 tr("Open file"), defaultDir, tr("All files (*)")); 367 if (!fileName.isEmpty()) 368 loadFile(fileName); 369 } 370 371 // Opens a log file from the recent files list 372 void MainWindow::openRecentFile() 373 { 374 QAction* action = qobject_cast<QAction*>(sender()); 375 if (action) 376 loadFile(action->data().toString()); 377 } 378 379 // Select all the text in the currently selected view 380 void MainWindow::selectAll() 381 { 382 CrawlerWidget* current = currentCrawlerWidget(); 383 384 if ( current ) 385 current->selectAll(); 386 } 387 388 // Copy the currently selected line into the clipboard 389 void MainWindow::copy() 390 { 391 static QClipboard* clipboard = QApplication::clipboard(); 392 CrawlerWidget* current = currentCrawlerWidget(); 393 394 if ( current ) { 395 clipboard->setText( current->getSelectedText() ); 396 397 // Put it in the global selection as well (X11 only) 398 clipboard->setText( current->getSelectedText(), 399 QClipboard::Selection ); 400 } 401 } 402 403 // Display the QuickFind bar 404 void MainWindow::find() 405 { 406 displayQuickFindBar( QuickFindMux::Forward ); 407 } 408 409 // Opens the 'Filters' dialog box 410 void MainWindow::filters() 411 { 412 FiltersDialog dialog(this); 413 signalMux_.connect(&dialog, SIGNAL( optionsChanged() ), SLOT( applyConfiguration() )); 414 dialog.exec(); 415 signalMux_.disconnect(&dialog, SIGNAL( optionsChanged() ), SLOT( applyConfiguration() )); 416 } 417 418 // Opens the 'Options' modal dialog box 419 void MainWindow::options() 420 { 421 OptionsDialog dialog(this); 422 signalMux_.connect(&dialog, SIGNAL( optionsChanged() ), SLOT( applyConfiguration() )); 423 dialog.exec(); 424 signalMux_.disconnect(&dialog, SIGNAL( optionsChanged() ), SLOT( applyConfiguration() )); 425 } 426 427 // Opens the 'About' dialog box. 428 void MainWindow::about() 429 { 430 QMessageBox::about(this, tr("About glogg"), 431 tr("<h2>glogg " GLOGG_VERSION "</h2>" 432 "<p>A fast, advanced log explorer." 433 #ifdef GLOGG_COMMIT 434 "<p>Built " GLOGG_DATE " from " GLOGG_COMMIT 435 #endif 436 "<p>Copyright © 2009, 2010, 2011, 2012, 2013 Nicolas Bonnefon and other contributors" 437 "<p>You may modify and redistribute the program under the terms of the GPL (version 3 or later)." ) ); 438 } 439 440 // Opens the 'About Qt' dialog box. 441 void MainWindow::aboutQt() 442 { 443 } 444 445 void MainWindow::toggleOverviewVisibility( bool isVisible ) 446 { 447 std::shared_ptr<Configuration> config = 448 Persistent<Configuration>( "settings" ); 449 config->setOverviewVisible( isVisible ); 450 emit optionsChanged(); 451 } 452 453 void MainWindow::toggleMainLineNumbersVisibility( bool isVisible ) 454 { 455 std::shared_ptr<Configuration> config = 456 Persistent<Configuration>( "settings" ); 457 config->setMainLineNumbersVisible( isVisible ); 458 emit optionsChanged(); 459 } 460 461 void MainWindow::toggleFilteredLineNumbersVisibility( bool isVisible ) 462 { 463 std::shared_ptr<Configuration> config = 464 Persistent<Configuration>( "settings" ); 465 config->setFilteredLineNumbersVisible( isVisible ); 466 emit optionsChanged(); 467 } 468 469 void MainWindow::disableFollow() 470 { 471 followAction->setChecked( false ); 472 } 473 474 void MainWindow::lineNumberHandler( int line ) 475 { 476 // The line number received is the internal (starts at 0) 477 lineNbField->setText( tr( "Line %1" ).arg( line + 1 ) ); 478 } 479 480 void MainWindow::updateLoadingProgress( int progress ) 481 { 482 LOG(logDEBUG) << "Loading progress: " << progress; 483 484 // FIXME - should check and set loadingFileName 485 486 // We ignore 0% and 100% to avoid a flash when the file (or update) 487 // is very short. 488 if ( progress > 0 && progress < 100 ) { 489 infoLine->setText( loadingFileName + tr( " - Indexing lines... (%1 %)" ).arg( progress ) ); 490 infoLine->displayGauge( progress ); 491 492 stopAction->setEnabled( true ); 493 } 494 } 495 496 void MainWindow::displayNormalStatus( bool success ) 497 { 498 QLocale defaultLocale; 499 500 LOG(logDEBUG) << "displayNormalStatus"; 501 502 // No file is loading 503 loadingFileName.clear(); 504 505 // Following should always work as we will only receive enter 506 // this slot if there is a crawler connected. 507 QString current_file = 508 session_->getFilename( currentCrawlerWidget() ).c_str(); 509 510 uint64_t fileSize; 511 uint32_t fileNbLine; 512 QDateTime lastModified; 513 514 session_->getFileInfo( currentCrawlerWidget(), 515 &fileSize, &fileNbLine, &lastModified ); 516 if ( lastModified.isValid() ) { 517 const QString date = 518 defaultLocale.toString( lastModified, QLocale::NarrowFormat ); 519 infoLine->setText( tr( "%1 (%2 - %3 lines - modified on %4)" ) 520 .arg(current_file).arg(readableSize(fileSize)) 521 .arg(fileNbLine).arg( date ) ); 522 } 523 else { 524 infoLine->setText( tr( "%1 (%2 - %3 lines)" ) 525 .arg(current_file).arg(readableSize(fileSize)) 526 .arg(fileNbLine) ); 527 } 528 529 infoLine->hideGauge(); 530 stopAction->setEnabled( false ); 531 532 // Now everything is ready, we can finally show the file! 533 currentCrawlerWidget()->show(); 534 mainTabWidget_.setEnabled( true ); 535 } 536 537 void MainWindow::closeTab( int index ) 538 { 539 auto widget = dynamic_cast<CrawlerWidget*>( 540 mainTabWidget_.widget( index ) ); 541 542 assert( widget ); 543 544 mainTabWidget_.removeTab( index ); 545 session_->close( widget ); 546 delete widget; 547 } 548 549 void MainWindow::currentTabChanged( int index ) 550 { 551 LOG(logDEBUG) << "currentTabChanged"; 552 553 if ( index >= 0 ) 554 { 555 CrawlerWidget* crawler_widget = dynamic_cast<CrawlerWidget*>( 556 mainTabWidget_.widget( index ) ); 557 signalMux_.setCurrentDocument( crawler_widget ); 558 quickFindMux_.registerSelector( crawler_widget ); 559 560 // New tab is set up with fonts etc... 561 emit optionsChanged(); 562 563 // Update the title bar 564 updateTitleBar( QString( 565 session_->getFilename( crawler_widget ).c_str() ) ); 566 } 567 else 568 { 569 // FIXME 570 } 571 } 572 573 void MainWindow::changeQFPattern( const QString& newPattern ) 574 { 575 quickFindWidget_.changeDisplayedPattern( newPattern ); 576 } 577 578 // 579 // Events 580 // 581 582 // Closes the application 583 void MainWindow::closeEvent( QCloseEvent *event ) 584 { 585 writeSettings(); 586 event->accept(); 587 } 588 589 // Accepts the drag event if it looks like a filename 590 void MainWindow::dragEnterEvent( QDragEnterEvent* event ) 591 { 592 if ( event->mimeData()->hasFormat( "text/uri-list" ) ) 593 event->acceptProposedAction(); 594 } 595 596 // Tries and loads the file if the URL dropped is local 597 void MainWindow::dropEvent( QDropEvent* event ) 598 { 599 QList<QUrl> urls = event->mimeData()->urls(); 600 if ( urls.isEmpty() ) 601 return; 602 603 QString fileName = urls.first().toLocalFile(); 604 if ( fileName.isEmpty() ) 605 return; 606 607 loadFile( fileName ); 608 } 609 610 void MainWindow::keyPressEvent( QKeyEvent* keyEvent ) 611 { 612 LOG(logDEBUG4) << "keyPressEvent received"; 613 614 switch ( (keyEvent->text())[0].toLatin1() ) { 615 case '/': 616 displayQuickFindBar( QuickFindMux::Forward ); 617 break; 618 case '?': 619 displayQuickFindBar( QuickFindMux::Backward ); 620 break; 621 default: 622 keyEvent->ignore(); 623 } 624 625 if ( !keyEvent->isAccepted() ) 626 QMainWindow::keyPressEvent( keyEvent ); 627 } 628 629 // 630 // Private functions 631 // 632 633 // Create a CrawlerWidget for the passed file, start its loading 634 // and update the title bar. 635 // The loading is done asynchronously. 636 bool MainWindow::loadFile( const QString& fileName ) 637 { 638 LOG(logDEBUG) << "loadFile ( " << fileName.toStdString() << " )"; 639 640 // Load the file 641 loadingFileName = fileName; 642 643 try { 644 CrawlerWidget* crawler_widget = dynamic_cast<CrawlerWidget*>( 645 session_->open( fileName.toStdString(), 646 []() { return new CrawlerWidget(); } ) ); 647 assert( crawler_widget ); 648 649 // We won't show the widget until the file is fully loaded 650 crawler_widget->hide(); 651 652 // We disable the tab widget to avoid having someone switch 653 // tab during loading. (maybe FIXME) 654 mainTabWidget_.setEnabled( false ); 655 656 int index = mainTabWidget_.addTab( 657 crawler_widget, strippedName( fileName ) ); 658 659 // Setting the new tab, the user will see a blank page for the duration 660 // of the loading, with no way to switch to another tab 661 mainTabWidget_.setCurrentIndex( index ); 662 663 // Update the recent files list 664 // (reload the list first in case another glogg changed it) 665 GetPersistentInfo().retrieve( "recentFiles" ); 666 recentFiles_->addRecent( fileName ); 667 GetPersistentInfo().save( "recentFiles" ); 668 updateRecentFileActions(); 669 } 670 catch ( FileUnreadableErr ) { 671 LOG(logDEBUG) << "Can't open file " << fileName.toStdString(); 672 return false; 673 } 674 675 LOG(logDEBUG) << "Success loading file " << fileName.toStdString(); 676 return true; 677 678 } 679 680 // Strips the passed filename from its directory part. 681 QString MainWindow::strippedName( const QString& fullFileName ) const 682 { 683 return QFileInfo( fullFileName ).fileName(); 684 } 685 686 // Return the currently active CrawlerWidget, or NULL if none 687 CrawlerWidget* MainWindow::currentCrawlerWidget() const 688 { 689 auto current = dynamic_cast<CrawlerWidget*>( 690 mainTabWidget_.currentWidget() ); 691 692 return current; 693 } 694 695 // Update the title bar. 696 void MainWindow::updateTitleBar( const QString& file_name ) 697 { 698 QString shownName = tr( "Untitled" ); 699 if ( !file_name.isEmpty() ) 700 shownName = strippedName( file_name ); 701 702 setWindowTitle( 703 tr("%1 - %2").arg(shownName).arg(tr("glogg")) 704 #ifdef GLOGG_COMMIT 705 + " (dev build " GLOGG_VERSION ")" 706 #endif 707 ); 708 } 709 710 // Updates the actions for the recent files. 711 // Must be called after having added a new name to the list. 712 void MainWindow::updateRecentFileActions() 713 { 714 QStringList recent_files = recentFiles_->recentFiles(); 715 716 for ( int j = 0; j < MaxRecentFiles; ++j ) { 717 if ( j < recent_files.count() ) { 718 QString text = tr("&%1 %2").arg(j + 1).arg(strippedName(recent_files[j])); 719 recentFileActions[j]->setText( text ); 720 recentFileActions[j]->setToolTip( recent_files[j] ); 721 recentFileActions[j]->setData( recent_files[j] ); 722 recentFileActions[j]->setVisible( true ); 723 } 724 else { 725 recentFileActions[j]->setVisible( false ); 726 } 727 } 728 729 // separatorAction->setVisible(!recentFiles.isEmpty()); 730 } 731 732 // Write settings to permanent storage 733 void MainWindow::writeSettings() 734 { 735 // Save the session 736 // Generate the ordered list of widgets and their topLine 737 std::vector<std::pair<const ViewInterface*, uint64_t>> widget_list; 738 for ( int i = 0; i < mainTabWidget_.count(); ++i ) 739 widget_list.push_back( { 740 dynamic_cast<const ViewInterface*>( mainTabWidget_.widget( i ) ), 741 0 } ); 742 session_->save( widget_list ); 743 //SessionInfo& session = Persistent<SessionInfo>( "session" ); 744 //session.setGeometry( saveGeometry() ); 745 //session.setCrawlerState( crawlerWidget->saveState() ); 746 //GetPersistentInfo().save( QString( "session" ) ); 747 748 // User settings 749 GetPersistentInfo().save( QString( "settings" ) ); 750 } 751 752 // Read settings from permanent storage 753 void MainWindow::readSettings() 754 { 755 // Get and restore the session 756 // GetPersistentInfo().retrieve( QString( "session" ) ); 757 // SessionInfo session = Persistent<SessionInfo>( "session" ); 758 //restoreGeometry( session.geometry() ); 759 /* 760 * FIXME: should be in the session 761 crawlerWidget->restoreState( session.crawlerState() ); 762 */ 763 764 // History of recent files 765 GetPersistentInfo().retrieve( QString( "recentFiles" ) ); 766 updateRecentFileActions(); 767 768 // GetPersistentInfo().retrieve( QString( "settings" ) ); 769 GetPersistentInfo().retrieve( QString( "filterSet" ) ); 770 } 771 772 void MainWindow::displayQuickFindBar( QuickFindMux::QFDirection direction ) 773 { 774 LOG(logDEBUG) << "MainWindow::displayQuickFindBar"; 775 776 // Warn crawlers so they can save the position of the focus in order 777 // to do incremental search in the right view. 778 emit enteringQuickFind(); 779 780 quickFindMux_.setDirection( direction ); 781 quickFindWidget_.userActivate(); 782 } 783 784 // Returns the size in human readable format 785 static QString readableSize( qint64 size ) 786 { 787 static const QString sizeStrs[] = { 788 QObject::tr("B"), QObject::tr("KiB"), QObject::tr("MiB"), 789 QObject::tr("GiB"), QObject::tr("TiB") }; 790 791 QLocale defaultLocale; 792 unsigned int i; 793 double humanSize = size; 794 795 for ( i=0; i+1 < (sizeof(sizeStrs)/sizeof(QString)) && (humanSize/1024.0) >= 1024.0; i++ ) 796 humanSize /= 1024.0; 797 798 if ( humanSize >= 1024.0 ) { 799 humanSize /= 1024.0; 800 i++; 801 } 802 803 QString output; 804 if ( i == 0 ) 805 // No decimal part if we display straight bytes. 806 output = defaultLocale.toString( (int) humanSize ); 807 else 808 output = defaultLocale.toString( humanSize, 'f', 1 ); 809 810 output += QString(" ") + sizeStrs[i]; 811 812 return output; 813 } 814