diff --git a/src/libs/utils/stringutils.cpp b/src/libs/utils/stringutils.cpp index cf1b4b36cf9b04664dbd484405ba67b7f6132392..10a2d418391a28d6976f90c29a4348f982698f14 100644 --- a/src/libs/utils/stringutils.cpp +++ b/src/libs/utils/stringutils.cpp @@ -31,6 +31,8 @@ #include <QtCore/QString> #include <QtCore/QStringList> +#include <QtCore/QFileInfo> +#include <QtCore/QDir> #include <limits.h> @@ -100,4 +102,21 @@ QTCREATOR_UTILS_EXPORT QString commonPath(const QStringList &files) return common; } +QTCREATOR_UTILS_EXPORT QString withTildeHomePath(const QString &path) +{ +#ifdef Q_OS_WIN + QString outPath = path; +#else + static const QString homePath = QDir::homePath(); + + QFileInfo fi(QDir::cleanPath(path)); + QString outPath = fi.absoluteFilePath(); + if (outPath.startsWith(homePath)) + outPath = QLatin1Char('~') + outPath.mid(homePath.size()); + else + outPath = path; +#endif + return outPath; +} + } // namespace Utils diff --git a/src/libs/utils/stringutils.h b/src/libs/utils/stringutils.h index 16cac815ded1477fe6cad7cc37e9c7de5d4d20e0..a863a6177199f7c2b53217485d8c9f2765302d9d 100644 --- a/src/libs/utils/stringutils.h +++ b/src/libs/utils/stringutils.h @@ -50,6 +50,11 @@ QTCREATOR_UTILS_EXPORT QString commonPrefix(const QStringList &strings); // "C:\foo\bar1" "C:\foo\bar2" -> "C:\foo" QTCREATOR_UTILS_EXPORT QString commonPath(const QStringList &files); +// On Linux/Mac replace user's home path with ~ +// Uses cleaned path and tries to use absolute path of "path" if possible +// If path is not sub of home path, or when running on Windows, returns the input +QTCREATOR_UTILS_EXPORT QString withTildeHomePath(const QString &path); + } // namespace Utils #endif // SETTINGSTUTILS_H diff --git a/src/plugins/coreplugin/mainwindow.cpp b/src/plugins/coreplugin/mainwindow.cpp index 4982e0ec466479193c4336a5ad8ec4444cfdd2b0..e8dfa3af6550f5428c5694309ec9b8a4aafd2a9d 100644 --- a/src/plugins/coreplugin/mainwindow.cpp +++ b/src/plugins/coreplugin/mainwindow.cpp @@ -70,6 +70,7 @@ #include <coreplugin/settingsdatabase.h> #include <utils/pathchooser.h> #include <utils/stylehelper.h> +#include <utils/stringutils.h> #include <extensionsystem/pluginmanager.h> #include <QtCore/QDebug> @@ -1253,7 +1254,8 @@ void MainWindow::aboutToShowRecentFiles() bool hasRecentFiles = false; foreach (const QString &fileName, m_fileManager->recentFiles()) { hasRecentFiles = true; - QAction *action = aci->menu()->addAction(fileName); + QAction *action = aci->menu()->addAction( + Utils::withTildeHomePath(fileName)); action->setData(fileName); connect(action, SIGNAL(triggered()), this, SLOT(openRecentFile())); } diff --git a/src/plugins/projectexplorer/projectexplorer.cpp b/src/plugins/projectexplorer/projectexplorer.cpp index e2a925f3f1b3a7c954b98b20dca7808f23084822..aa6aa70e2b38277020fef2cc85dfce3df59661ef 100644 --- a/src/plugins/projectexplorer/projectexplorer.cpp +++ b/src/plugins/projectexplorer/projectexplorer.cpp @@ -97,6 +97,7 @@ #include <utils/consoleprocess.h> #include <utils/qtcassert.h> #include <utils/parameteraction.h> +#include <utils/stringutils.h> #include <QtCore/QtPlugin> #include <QtCore/QDateTime> @@ -1998,7 +1999,7 @@ void ProjectExplorerPlugin::updateRecentProjectMenu() const QPair<QString, QString> &s = *it; if (s.first.endsWith(QLatin1String(".qws"))) continue; - QAction *action = menu->addAction(s.first); + QAction *action = menu->addAction(Utils::withTildeHomePath(s.first)); action->setData(s.first); connect(action, SIGNAL(triggered()), this, SLOT(openRecentProject())); } diff --git a/src/plugins/projectexplorer/projectwelcomepagewidget.cpp b/src/plugins/projectexplorer/projectwelcomepagewidget.cpp index e75df35e9b7e655eebb8555b7b9c0414ea775363..9c30289eb4fbdce405b157e7a1227440c757f85b 100644 --- a/src/plugins/projectexplorer/projectwelcomepagewidget.cpp +++ b/src/plugins/projectexplorer/projectwelcomepagewidget.cpp @@ -39,6 +39,8 @@ #include <coreplugin/mainwindow.h> #include <coreplugin/filemanager.h> +#include <utils/stringutils.h> + #include <QtCore/QFileInfo> #include <QtCore/QDir> #include <QtCore/QPair> @@ -146,8 +148,9 @@ void ProjectWelcomePageWidget::updateWelcomePage(const WelcomePageData &welcomeP break; const QFileInfo fi(it.first); QString label = "<b>" + it.second + - "</b><br><font color=gray>" + - fm.elidedText(it.first, Qt::ElideMiddle, 250); + "</b><br><font color=gray>" + + fm.elidedText(QDir::toNativeSeparators(Utils::withTildeHomePath(it.first)), + Qt::ElideMiddle, 250); ui->projTreeWidget->addItem(label, it.first, QDir::toNativeSeparators(fi.absolutePath())); } diff --git a/tests/auto/auto.pro b/tests/auto/auto.pro index 9b51ff0f978167f5d36805ea7bb2f9987a2f495e..4e35b4e5c5f9420475bd3482a7bc4d2609485ce8 100644 --- a/tests/auto/auto.pro +++ b/tests/auto/auto.pro @@ -8,7 +8,8 @@ SUBDIRS += \ aggregation \ changeset \ # icheckbuild \ - generichighlighter + generichighlighter \ + utils_stringutils contains (QT_CONFIG, declarative) { SUBDIRS += qml diff --git a/tests/auto/utils_stringutils/tst_stringutils.cpp b/tests/auto/utils_stringutils/tst_stringutils.cpp new file mode 100644 index 0000000000000000000000000000000000000000..31990ae29b72a6087e4fcccbd3d120683ca619c6 --- /dev/null +++ b/tests/auto/utils_stringutils/tst_stringutils.cpp @@ -0,0 +1,76 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** Commercial Usage +** +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Nokia. +** +** GNU Lesser General Public License Usage +** +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://qt.nokia.com/contact. +** +**************************************************************************/ + +#include <stringutils.h> + +#include <QtTest/QtTest> + +class tst_StringUtils : public QObject +{ + Q_OBJECT + +private slots: + void testWithTildeHomePath(); + +}; + +void tst_StringUtils::testWithTildeHomePath() +{ +#ifndef Q_OS_WIN + // home path itself + QCOMPARE(Utils::withTildeHomePath(QDir::homePath()), QLatin1String("~")); + QCOMPARE(Utils::withTildeHomePath(QDir::homePath() + QLatin1Char('/')), + QLatin1String("~")); + QCOMPARE(Utils::withTildeHomePath(QLatin1String("/unclean/..") + QDir::homePath()), + QLatin1String("~")); + // sub of home path + QCOMPARE(Utils::withTildeHomePath(QDir::homePath() + QLatin1String("/foo")), + QLatin1String("~/foo")); + QCOMPARE(Utils::withTildeHomePath(QDir::homePath() + QLatin1String("/foo/")), + QLatin1String("~/foo")); + QCOMPARE(Utils::withTildeHomePath(QDir::homePath() + QLatin1String("/some/path/file.txt")), + QLatin1String("~/some/path/file.txt")); + QCOMPARE(Utils::withTildeHomePath(QDir::homePath() + QLatin1String("/some/unclean/../path/file.txt")), + QLatin1String("~/some/path/file.txt")); + // not sub of home path + QCOMPARE(Utils::withTildeHomePath(QDir::homePath() + QLatin1String("/../foo")), + QDir::homePath() + QLatin1String("/../foo")); +#else + // windows: should return same as input + QCOMPARE(Utils::withTildeHomePath(QDir::homePath()), QDir::homePath()); + QCOMPARE(Utils::withTildeHomePath(QDir::homePath() + QLatin1String("/foo")), + QDir::homePath() + QLatin1String("/foo")); + QCOMPARE(Utils::withTildeHomePath(QDir::homePath() + QLatin1String("/../foo")), + Utils::withTildeHomePath(QDir::homePath() + QLatin1String("/../foo"))); +#endif +} + +QTEST_MAIN(tst_StringUtils) + +#include "tst_stringutils.moc" diff --git a/tests/auto/utils_stringutils/utils_stringutils.pro b/tests/auto/utils_stringutils/utils_stringutils.pro new file mode 100644 index 0000000000000000000000000000000000000000..3c0d6552f43a659eea8ae9ae66c0616656e82a95 --- /dev/null +++ b/tests/auto/utils_stringutils/utils_stringutils.pro @@ -0,0 +1,15 @@ +CONFIG += qtestlib testcase +TEMPLATE = app +CONFIG -= app_bundle +DEFINES += QTCREATOR_UTILS_LIB + +UTILS_PATH = ../../../src/libs/utils + +INCLUDEPATH += $$UTILS_PATH +# Input +SOURCES += tst_stringutils.cpp \ + $$UTILS_PATH/stringutils.cpp +HEADERS += $$UTILS_PATH/stringutils.h \ + $$UTILS_PATH/utils_global.h + +TARGET=tst_$$TARGET