/src/utils/include/openfilehelper.h

https://github.com/variar/klogg · C Header · 92 lines · 38 code · 10 blank · 44 comment · 0 complexity · 569e7cba0b763be8c8f511abe5657b96 MD5 · raw file

  1. /*
  2. * Copyright (C) 2016 -- 2019 Anton Filimonov
  3. *
  4. * This file is part of klogg.
  5. *
  6. * klogg 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. * klogg 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 klogg. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. * showPathInFileExplorer code is derived from Qt Creator sources
  20. */
  21. /****************************************************************************
  22. **
  23. ** Copyright (C) 2016 The Qt Company Ltd.
  24. ** Contact: https://www.qt.io/licensing/
  25. **
  26. ** This file is part of Qt Creator.
  27. **
  28. ** Commercial License Usage
  29. ** Licensees holding valid commercial Qt licenses may use this file in
  30. ** accordance with the commercial license agreement provided with the
  31. ** Software or, alternatively, in accordance with the terms contained in
  32. ** a written agreement between you and The Qt Company. For licensing terms
  33. ** and conditions see https://www.qt.io/terms-conditions. For further
  34. ** information use the contact form at https://www.qt.io/contact-us.
  35. **
  36. ** GNU General Public License Usage
  37. ** Alternatively, this file may be used under the terms of the GNU
  38. ** General Public License version 3 as published by the Free Software
  39. ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
  40. ** included in the packaging of this file. Please review the following
  41. ** information to ensure the GNU General Public License requirements will
  42. ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
  43. **
  44. ****************************************************************************/
  45. #ifndef KLOGG_OPEN_FILE_HELPER_H
  46. #define KLOGG_OPEN_FILE_HELPER_H
  47. #include <QApplication>
  48. #include <QDir>
  49. #include <QFileInfo>
  50. #include <QUrl>
  51. #include <QtCore/QProcess>
  52. #include <QtGui/QDesktopServices>
  53. #include "log.h"
  54. inline void showPathInFileExplorer( const QString& filePath )
  55. {
  56. const QFileInfo fileInfo( filePath );
  57. LOG_INFO << "Show path in explorer: " << filePath;
  58. #if defined( Q_OS_WIN )
  59. const auto explorer = QString( "explorer.exe /select,%1" )
  60. .arg( QDir::toNativeSeparators( fileInfo.canonicalFilePath() ) );
  61. QProcess::startDetached( explorer );
  62. #elif defined( Q_OS_MAC )
  63. QStringList scriptArgs;
  64. scriptArgs << QLatin1String( "-e" )
  65. << QString::fromLatin1( "tell application \"Finder\" to reveal POSIX file \"%1\"" )
  66. .arg( fileInfo.canonicalFilePath() );
  67. QProcess::execute( QLatin1String( "/usr/bin/osascript" ), scriptArgs );
  68. scriptArgs.clear();
  69. scriptArgs << QLatin1String( "-e" )
  70. << QLatin1String( "tell application \"Finder\" to activate" );
  71. QProcess::execute( QLatin1String( "/usr/bin/osascript" ), scriptArgs );
  72. #else
  73. QDesktopServices::openUrl( QUrl::fromLocalFile( fileInfo.canonicalPath() ) );
  74. #endif
  75. }
  76. inline void openFileInDefaultApplication( const QString& filePath )
  77. {
  78. LOG_INFO << "Open file in default app: " << filePath;
  79. const QFileInfo fileInfo( filePath );
  80. QDesktopServices::openUrl( QUrl::fromLocalFile( fileInfo.canonicalFilePath() ) );
  81. }
  82. #endif