1 //=============================================================================
2 //  MuseScore
3 //  Music Composition & Notation
4 //
5 //  Copyright (C) 2020 MuseScore BVBA
6 //
7 //  This program is free software; you can redistribute it and/or modify
8 //  it under the terms of the GNU General Public License version 2
9 //  as published by the Free Software Foundation and appearing in
10 //  the file LICENCE.GPL
11 //=============================================================================
12 
13 #include "openfilelocation.h"
14 
15 namespace Ms {
16 
17 //---------------------------------------------------------
18 //   platformText
19 ///   Returns the platform specific text for opening the file.
20 ///   There is no official standard, but there seems to be a general agreement
21 ///
22 ///   Windows: "Show in Explorer"
23 ///   MacOs:   "Reveal in Finder"
24 ///   Else:    "Open Containing Folder"
25 //---------------------------------------------------------
26 
platformText()27 const QString OpenFileLocation::platformText()
28       {
29 #if defined(Q_OS_WIN)
30       return tr("Show in Explorer");
31 #elif defined(Q_OS_MAC)
32       return tr("Reveal in Finder");
33 #else
34       return tr("Open Containing Folder");
35 #endif
36       }
37 
38 //---------------------------------------------------------
39 //   openFileLocation
40 ///   reveals the file in explorer (Windows) / finder (MacOs) or the default file manager in other cases.
41 ///   Note that opening the default file manager doesn't show the file, but simply opens the containing folder.
42 ///   Note also that all platforms will fallback to opening the containing folder if they can't show the file.
43 ///
44 ///   Returns true on success, false on failure.
45 ///   See MetaEditDialog::openFileLocation example usage
46 //---------------------------------------------------------
47 
openFileLocation(const QString & path)48 bool OpenFileLocation::openFileLocation(const QString& path)
49       {
50       const QFileInfo fileInfo(path);
51       if (!fileInfo.exists())
52             return false;
53       QStringList args;
54 #if defined(Q_OS_WIN)
55       if (!fileInfo.isDir())
56             args += QLatin1String("/select,");
57       args += QDir::toNativeSeparators(fileInfo.canonicalFilePath());
58       if (QProcess::startDetached("explorer.exe", args))
59             return true;
60 #elif defined(Q_OS_MAC)
61       args << QLatin1String("-e")
62            << QString::fromLatin1("tell application \"Finder\" to reveal POSIX file \"%1\"")
63               .arg(fileInfo.canonicalFilePath());
64       QProcess::execute(QLatin1String("/usr/bin/osascript"), args);
65       args.clear();
66       args << QLatin1String("-e")
67            << QLatin1String("tell application \"Finder\" to activate");
68       if (!QProcess::execute("/usr/bin/osascript", args))
69             return true;
70 #endif // not #else so that the following can be used as a fallback.
71       return QDesktopServices::openUrl(QUrl::fromLocalFile(fileInfo.isDir()? path : fileInfo.path()));
72       }
73 
74 } // namespace Ms
75