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