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 connect( reloadAction, SIGNAL(triggered()), this, SLOT(reload()) ); 172 173 stopAction = new QAction( tr("&Stop"), this ); 174 stopAction->setIcon( QIcon(":/images/stop16.png") ); 175 stopAction->setEnabled( false ); 176 connect( stopAction, SIGNAL(triggered()), this, SLOT(stop()) ); 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 // Reload the current log file 311 void MainWindow::reload() 312 { 313 if ( !currentFile.isEmpty() ) 314 loadFile( currentFile ); 315 } 316 317 // Stop the loading operation 318 void MainWindow::stop() 319 { 320 session_->stopLoading( crawlerWidget ); 321 } 322 323 // Opens the 'Filters' dialog box 324 void MainWindow::filters() 325 { 326 FiltersDialog dialog(this); 327 connect(&dialog, SIGNAL( optionsChanged() ), crawlerWidget, SLOT( applyConfiguration() )); 328 dialog.exec(); 329 } 330 331 // Opens the 'Options' modal dialog box 332 void MainWindow::options() 333 { 334 OptionsDialog dialog(this); 335 connect(&dialog, SIGNAL( optionsChanged() ), crawlerWidget, SLOT( applyConfiguration() )); 336 dialog.exec(); 337 } 338 339 // Opens the 'About' dialog box. 340 void MainWindow::about() 341 { 342 QMessageBox::about(this, tr("About glogg"), 343 tr("<h2>glogg " GLOGG_VERSION "</h2>" 344 "<p>A fast, advanced log explorer." 345 #ifdef GLOGG_COMMIT 346 "<p>Built " GLOGG_DATE " from " GLOGG_COMMIT 347 #endif 348 "<p>Copyright © 2009, 2010, 2011, 2012, 2013 Nicolas Bonnefon and other contributors" 349 "<p>You may modify and redistribute the program under the terms of the GPL (version 3 or later)." ) ); 350 } 351 352 // Opens the 'About Qt' dialog box. 353 void MainWindow::aboutQt() 354 { 355 } 356 357 void MainWindow::toggleOverviewVisibility( bool isVisible ) 358 { 359 Configuration& config = Persistent<Configuration>( "settings" ); 360 config.setOverviewVisible( isVisible ); 361 emit optionsChanged(); 362 } 363 364 void MainWindow::toggleMainLineNumbersVisibility( bool isVisible ) 365 { 366 Configuration& config = Persistent<Configuration>( "settings" ); 367 config.setMainLineNumbersVisible( isVisible ); 368 emit optionsChanged(); 369 } 370 371 void MainWindow::toggleFilteredLineNumbersVisibility( bool isVisible ) 372 { 373 Configuration& config = Persistent<Configuration>( "settings" ); 374 config.setFilteredLineNumbersVisible( isVisible ); 375 emit optionsChanged(); 376 } 377 378 void MainWindow::disableFollow() 379 { 380 followAction->setChecked( false ); 381 } 382 383 void MainWindow::lineNumberHandler( int line ) 384 { 385 // The line number received is the internal (starts at 0) 386 lineNbField->setText( tr( "Line %1" ).arg( line + 1 ) ); 387 } 388 389 void MainWindow::updateLoadingProgress( int progress ) 390 { 391 LOG(logDEBUG) << "Loading progress: " << progress; 392 393 // We ignore 0% and 100% to avoid a flash when the file (or update) 394 // is very short. 395 if ( progress > 0 && progress < 100 ) { 396 infoLine->setText( loadingFileName + tr( " - Indexing lines... (%1 %)" ).arg( progress ) ); 397 infoLine->displayGauge( progress ); 398 399 stopAction->setEnabled( true ); 400 } 401 } 402 403 void MainWindow::displayNormalStatus( bool success ) 404 { 405 QLocale defaultLocale; 406 407 LOG(logDEBUG) << "displayNormalStatus"; 408 409 if ( success ) 410 setCurrentFile( loadingFileName ); 411 412 uint64_t fileSize; 413 uint32_t fileNbLine; 414 QDateTime lastModified; 415 416 session_->getFileInfo( crawlerWidget, 417 &fileSize, &fileNbLine, &lastModified ); 418 if ( lastModified.isValid() ) { 419 const QString date = 420 defaultLocale.toString( lastModified, QLocale::NarrowFormat ); 421 infoLine->setText( tr( "%1 (%2 - %3 lines - modified on %4)" ) 422 .arg(currentFile).arg(readableSize(fileSize)) 423 .arg(fileNbLine).arg( date ) ); 424 } 425 else { 426 infoLine->setText( tr( "%1 (%2 - %3 lines)" ) 427 .arg(currentFile).arg(readableSize(fileSize)) 428 .arg(fileNbLine) ); 429 } 430 431 infoLine->hideGauge(); 432 stopAction->setEnabled( false ); 433 434 // Now everything is ready, we can finally show the file! 435 crawlerWidget->show(); 436 } 437 438 // 439 // Events 440 // 441 442 // Closes the application 443 void MainWindow::closeEvent( QCloseEvent *event ) 444 { 445 writeSettings(); 446 event->accept(); 447 } 448 449 // Accepts the drag event if it looks like a filename 450 void MainWindow::dragEnterEvent( QDragEnterEvent* event ) 451 { 452 if ( event->mimeData()->hasFormat( "text/uri-list" ) ) 453 event->acceptProposedAction(); 454 } 455 456 // Tries and loads the file if the URL dropped is local 457 void MainWindow::dropEvent( QDropEvent* event ) 458 { 459 QList<QUrl> urls = event->mimeData()->urls(); 460 if ( urls.isEmpty() ) 461 return; 462 463 QString fileName = urls.first().toLocalFile(); 464 if ( fileName.isEmpty() ) 465 return; 466 467 loadFile( fileName ); 468 } 469 470 // 471 // Private functions 472 // 473 474 // Create a CrawlerWidget for the passed file, start its loading 475 // and update the title bar. 476 // The loading is done asynchronously. 477 bool MainWindow::loadFile( const QString& fileName ) 478 { 479 LOG(logDEBUG) << "loadFile ( " << fileName.toStdString() << " )"; 480 481 int topLine = 0; 482 483 // If we're loading the same file, put the same line on top. 484 if ( fileName == currentFile ) 485 topLine = crawlerWidget->getTopLine(); 486 487 // First get the global search history 488 savedSearches = &(Persistent<SavedSearches>( "savedSearches" )); 489 490 // Load the file 491 loadingFileName = fileName; 492 493 crawlerWidget = dynamic_cast<CrawlerWidget*>( session_->open( fileName.toStdString(), 494 [this]() { return new CrawlerWidget( savedSearches, this ); } ) ); 495 496 // We won't show the widget until the file is fully loaded 497 crawlerWidget->hide(); 498 499 signalMux_.setCurrentDocument( crawlerWidget ); 500 501 // Send actions to the crawlerwidget 502 signalMux_.connect( this, SIGNAL( followSet( bool ) ), 503 SIGNAL( followSet( bool ) ) ); 504 signalMux_.connect( this, SIGNAL( optionsChanged() ), 505 SLOT( applyConfiguration() ) ); 506 507 // Actions from the CrawlerWidget 508 signalMux_.connect( SIGNAL( followDisabled() ), 509 this, SLOT( disableFollow() ) ); 510 signalMux_.connect( SIGNAL( updateLineNumber( int ) ), 511 this, SLOT( lineNumberHandler( int ) ) ); 512 513 // FIXME: is it necessary? 514 emit optionsChanged(); 515 516 // We start with the empty file 517 setCurrentFile( "" ); 518 519 // Register for progress status bar 520 signalMux_.connect( SIGNAL( loadingProgressed( int ) ), 521 this, SLOT( updateLoadingProgress( int ) ) ); 522 signalMux_.connect( SIGNAL( loadingFinished( bool ) ), 523 this, SLOT( displayNormalStatus( bool ) ) ); 524 525 setCentralWidget(crawlerWidget); 526 527 LOG(logDEBUG) << "Success loading file " << fileName.toStdString(); 528 return true; 529 } 530 531 // Strips the passed filename from its directory part. 532 QString MainWindow::strippedName( const QString& fullFileName ) const 533 { 534 return QFileInfo( fullFileName ).fileName(); 535 } 536 537 // Add the filename to the recent files list and update the title bar. 538 void MainWindow::setCurrentFile( const QString& fileName ) 539 { 540 // Change the current file 541 currentFile = fileName; 542 QString shownName = tr( "Untitled" ); 543 if ( !currentFile.isEmpty() ) { 544 // (reload the list first in case another glogg changed it) 545 GetPersistentInfo().retrieve( "recentFiles" ); 546 recentFiles.addRecent( currentFile ); 547 GetPersistentInfo().save( "recentFiles" ); 548 updateRecentFileActions(); 549 shownName = strippedName( currentFile ); 550 } 551 552 setWindowTitle( 553 tr("%1 - %2").arg(shownName).arg(tr("glogg")) 554 #ifdef GLOGG_COMMIT 555 + " (dev build " GLOGG_VERSION ")" 556 #endif 557 ); 558 } 559 560 // Updates the actions for the recent files. 561 // Must be called after having added a new name to the list. 562 void MainWindow::updateRecentFileActions() 563 { 564 QStringList recent_files = recentFiles.recentFiles(); 565 566 for ( int j = 0; j < MaxRecentFiles; ++j ) { 567 if ( j < recent_files.count() ) { 568 QString text = tr("&%1 %2").arg(j + 1).arg(strippedName(recent_files[j])); 569 recentFileActions[j]->setText( text ); 570 recentFileActions[j]->setToolTip( recent_files[j] ); 571 recentFileActions[j]->setData( recent_files[j] ); 572 recentFileActions[j]->setVisible( true ); 573 } 574 else { 575 recentFileActions[j]->setVisible( false ); 576 } 577 } 578 579 // separatorAction->setVisible(!recentFiles.isEmpty()); 580 } 581 582 // Write settings to permanent storage 583 void MainWindow::writeSettings() 584 { 585 // Save the session 586 SessionInfo& session = Persistent<SessionInfo>( "session" ); 587 session.setGeometry( saveGeometry() ); 588 session.setCrawlerState( crawlerWidget->saveState() ); 589 session.setCurrentFile( currentFile ); 590 GetPersistentInfo().save( QString( "session" ) ); 591 592 // User settings 593 GetPersistentInfo().save( QString( "settings" ) ); 594 } 595 596 // Read settings from permanent storage 597 void MainWindow::readSettings() 598 { 599 // Get and restore the session 600 GetPersistentInfo().retrieve( QString( "session" ) ); 601 SessionInfo session = Persistent<SessionInfo>( "session" ); 602 restoreGeometry( session.geometry() ); 603 previousFile = session.currentFile(); 604 /* 605 * FIXME: should be in the session 606 crawlerWidget->restoreState( session.crawlerState() ); 607 */ 608 609 // History of recent files 610 GetPersistentInfo().retrieve( QString( "recentFiles" ) ); 611 updateRecentFileActions(); 612 613 GetPersistentInfo().retrieve( QString( "savedSearches" ) ); 614 GetPersistentInfo().retrieve( QString( "settings" ) ); 615 GetPersistentInfo().retrieve( QString( "filterSet" ) ); 616 } 617 618 // Returns the size in human readable format 619 static QString readableSize( qint64 size ) 620 { 621 static const QString sizeStrs[] = { 622 QObject::tr("B"), QObject::tr("KiB"), QObject::tr("MiB"), 623 QObject::tr("GiB"), QObject::tr("TiB") }; 624 625 QLocale defaultLocale; 626 unsigned int i; 627 double humanSize = size; 628 629 for ( i=0; i+1 < (sizeof(sizeStrs)/sizeof(QString)) && (humanSize/1024.0) >= 1024.0; i++ ) 630 humanSize /= 1024.0; 631 632 if ( humanSize >= 1024.0 ) { 633 humanSize /= 1024.0; 634 i++; 635 } 636 637 QString output; 638 if ( i == 0 ) 639 // No decimal part if we display straight bytes. 640 output = defaultLocale.toString( (int) humanSize ); 641 else 642 output = defaultLocale.toString( humanSize, 'f', 1 ); 643 644 output += QString(" ") + sizeStrs[i]; 645 646 return output; 647 } 648