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