diff --git a/src/app/images/icons8-sync-16.png b/src/app/images/icons8-sync-16.png new file mode 100644 index 000000000..50313ee1e Binary files /dev/null and b/src/app/images/icons8-sync-16.png differ diff --git a/src/ui/src/mainwindow.cpp b/src/ui/src/mainwindow.cpp index 9006ebeb2..3a2b3e9fa 100644 --- a/src/ui/src/mainwindow.cpp +++ b/src/ui/src/mainwindow.cpp @@ -46,7 +46,7 @@ #ifdef Q_OS_WIN #define WIN32_LEAN_AND_MEAN #include -#endif // _WIN32 +#endif // Q_OS_WIN #include @@ -58,18 +58,16 @@ #include #include #include -#include #include #include #include #include -#include #include "log.h" +#include "openfilehelper.h" #include "mainwindow.h" -#include "sessioninfo.h" #include "recentfiles.h" #include "crawlerwidget.h" #include "highlightersdialog.h" @@ -652,16 +650,12 @@ void MainWindow::copyFullPath() void MainWindow::openContainingFolder() { - const auto& current_file = session_.getFilename( currentCrawlerWidget() ); - const auto& dir = QFileInfo( current_file ).absolutePath(); - QDesktopServices::openUrl( QUrl( QDir::toNativeSeparators( dir ) ) ); + showPathInFileExplorer( session_.getFilename( currentCrawlerWidget() ) ); } - void MainWindow::openInEditor() { - const auto& current_file = session_.getFilename( currentCrawlerWidget() ); - QDesktopServices::openUrl( QUrl( QDir::toNativeSeparators( current_file ) ) ); + openFileInDefaultApplication( session_.getFilename( currentCrawlerWidget() ) ); } void MainWindow::onClipboardDataChanged() diff --git a/src/ui/src/tabbedcrawlerwidget.cpp b/src/ui/src/tabbedcrawlerwidget.cpp index 0a379f5e5..2031ff554 100644 --- a/src/ui/src/tabbedcrawlerwidget.cpp +++ b/src/ui/src/tabbedcrawlerwidget.cpp @@ -19,18 +19,18 @@ #include "tabbedcrawlerwidget.h" +#include +#include +#include +#include #include #include #include -#include -#include -#include -#include -#include #include "crawlerwidget.h" #include "log.h" +#include "openfilehelper.h" TabbedCrawlerWidget::TabbedCrawlerWidget() : QTabWidget() @@ -68,7 +68,7 @@ TabbedCrawlerWidget::TabbedCrawlerWidget() &TabbedCrawlerWidget::showContextMenu ); } -void TabbedCrawlerWidget::addTabBarItem(int index, const QString &file_name) +void TabbedCrawlerWidget::addTabBarItem( int index, const QString& file_name ) { const auto tab_label = QFileInfo( file_name ).fileName(); @@ -163,14 +163,11 @@ void TabbedCrawlerWidget::showContextMenu( const QPoint& point ) closeRight->setDisabled( true ); } - connect( copyFullPath, &QAction::triggered, [this, tab] { - QApplication::clipboard()->setText( tabToolTip( tab ) ); - } ); + connect( copyFullPath, &QAction::triggered, + [this, tab] { QApplication::clipboard()->setText( tabToolTip( tab ) ); } ); - connect( openContainingFolder, &QAction::triggered, [this, tab] { - const auto& dir = QFileInfo( tabToolTip( tab ) ).absolutePath(); - QDesktopServices::openUrl( QUrl( dir ) ); - } ); + connect( openContainingFolder, &QAction::triggered, + [this, tab] { showPathInFileExplorer( tabToolTip( tab ) ); } ); menu.exec( myTabBar_.mapToGlobal( point ) ); } diff --git a/src/utils/CMakeLists.txt b/src/utils/CMakeLists.txt index 11b428255..1c3f5841c 100644 --- a/src/utils/CMakeLists.txt +++ b/src/utils/CMakeLists.txt @@ -7,7 +7,7 @@ add_library(utils STATIC ${CMAKE_CURRENT_SOURCE_DIR}/include/perfcounter.h ${CMAKE_CURRENT_SOURCE_DIR}/include/persistable.h ${CMAKE_CURRENT_SOURCE_DIR}/include/persistentinfo.h -) + ${CMAKE_CURRENT_SOURCE_DIR}/include/openfilehelper.h) target_include_directories(utils PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include") target_link_libraries(utils PUBLIC Qt5::Gui absl::optional plog) diff --git a/src/utils/include/openfilehelper.h b/src/utils/include/openfilehelper.h new file mode 100644 index 000000000..1ecc6270c --- /dev/null +++ b/src/utils/include/openfilehelper.h @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2016 -- 2019 Anton Filimonov + * + * This file is part of klogg. + * + * klogg is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * klogg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with klogg. If not, see . + * + * showPathInFileExplorer code is derived from Qt Creator sources + */ + +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt Creator. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +****************************************************************************/ + +#pragma once + +#include +#include +#include +#include +#include +#include + +#include "log.h" + +inline void showPathInFileExplorer( const QString& file_path ) +{ + const QFileInfo file_info( file_path ); + + LOG( logINFO ) << "Show path in explorer: " << file_path; + +#if defined( Q_OS_WIN ) + const auto explorer = QString( "explorer.exe /select \"%1\"" ) + .arg( QDir::toNativeSeparators( file_info.canonicalFilePath() ) ); + QProcess::startDetached( explorer ); +#elif defined( Q_OS_MAC ) + QStringList scriptArgs; + scriptArgs << QLatin1String( "-e" ) + << QString::fromLatin1( "tell application \"Finder\" to reveal POSIX file \"%1\"" ) + .arg( file_info.canonicalFilePath() ); + QProcess::execute( QLatin1String( "/usr/bin/osascript" ), scriptArgs ); + scriptArgs.clear(); + scriptArgs << QLatin1String( "-e" ) + << QLatin1String( "tell application \"Finder\" to activate" ); + QProcess::execute( QLatin1String( "/usr/bin/osascript" ), scriptArgs ); +#else + QDesktopServices::openUrl( QUrl::fromLocalFile( file_info.canonicalPath() ) ); +#endif +} + +inline void openFileInDefaultApplication( const QString& file_path ) +{ + LOG( logINFO ) << "Open file in default app: " << file_path; + + const QFileInfo file_info( file_path ); + QDesktopServices::openUrl( QUrl::fromLocalFile( file_info.canonicalFilePath() ) ); +}