[KDE/Mac] QStandardPaths possible solution

Jeremy Whiting jpwhiting at kde.org
Thu Jan 8 14:01:49 UTC 2015


Rene,

Sorry I forgot to upload my changes to my patch for 5.4. I've attached it
here the includes at the top aren't needed (and maybe I should remove the
whitespace change also before pushing to gerrit?). What would you put in
place of "/opt/local/share" with your idea of configure time prefix?

David,

I appreciate your feedback here, despite what Ian suggested. Please stay in
the loop.

BR,
Jeremy

On Thu, Jan 8, 2015 at 4:38 AM, René J.V. <rjvbertin at gmail.com> wrote:

> On Thursday January 08 2015 16:51:47 Ian Wadham wrote:
>
> Hi Ian
>
> > > I think the hardcoded string should be set at configure time. If you
> do it properly (#define FAKE_XDG_STANDARDPATH "/where/ever"), you can even
> provide a default configure set-up where the code behaves exactly as it
> does now:
> ...
> > > This adds a bit of complexity, but having a default configuration that
> just has some additional lines without effect should make acceptance
> easier, I guess.
> >
> > I agree entirely, especially re prepending the XDG path, if it is
> defined, and the
> > gaining of acceptance.
>
> Reckoned you would :)
>
> Since I am grappling with Qt 5.3.2 anyway, here's (I hope) how Jeremy's
> proposed patch would look for that version - to be applied through MacPorts
> (or HB, or Fink, or ...) of course.
> I actually caught myself being surprised that this work had apparently
> been done for Qt4 (port:qt4-mac) but never for port:qt5-mac ...
>
> R.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.kde.org/pipermail/kde-mac/attachments/20150108/288fbe73/attachment.html>
-------------- next part --------------
diff --git a/src/corelib/io/qstandardpaths_mac.mm b/src/corelib/io/qstandardpaths_mac.mm
index 01d1c01..a2d56f3 100644
--- a/src/corelib/io/qstandardpaths_mac.mm
+++ b/src/corelib/io/qstandardpaths_mac.mm
@@ -127,7 +127,7 @@ static QString macLocation(QStandardPaths::StandardLocation type, short domain)
     if (err)
        return QString();
 
-   QString path = getFullPath(ref);
+    QString path = getFullPath(ref);
 
     if (type == QStandardPaths::AppDataLocation || type == QStandardPaths::AppLocalDataLocation || type == QStandardPaths::CacheLocation)
         appendOrganizationAndApp(path);
@@ -177,6 +177,38 @@ QString QStandardPaths::writableLocation(StandardLocation type)
     }
 }
 
+static QStringList xdgDataDirs()
+{
+    QStringList dirs;
+    // http://standards.freedesktop.org/basedir-spec/latest/
+    QString xdgDataDirsEnv = QFile::decodeName(qgetenv("XDG_DATA_DIRS"));
+    if (xdgDataDirsEnv.isEmpty()) {
+        dirs.append(QString::fromLatin1("/opt/local/share"));
+        dirs.append(QString::fromLatin1("/Library/Application Support"));
+    } else {
+        dirs = xdgDataDirsEnv.split(QLatin1Char(':'), QString::SkipEmptyParts);
+
+        // Normalize paths, skip relative paths
+        QMutableListIterator<QString> it(dirs);
+        while (it.hasNext()) {
+            const QString dir = it.next();
+            if (!dir.startsWith(QLatin1Char('/')))
+                it.remove();
+            else
+                it.setValue(QDir::cleanPath(dir));
+        }
+
+        // Remove duplicates from the list, there's no use for duplicated
+        // paths in XDG_DATA_DIRS - if it's not found in the given
+        // directory the first time, it won't be there the second time.
+        // Plus duplicate paths causes problems for example for mimetypes,
+        // where duplicate paths here lead to duplicated mime types returned
+        // for a file, eg "text/plain,text/plain" instead of "text/plain"
+        dirs.removeDuplicates();
+    }
+    return dirs;
+}
+
 QStringList QStandardPaths::standardLocations(StandardLocation type)
 {
     QStringList dirs;
@@ -187,7 +219,15 @@ QStringList QStandardPaths::standardLocations(StandardLocation type)
             dirs.append(path);
     }
 
+    if (type == GenericDataLocation)
+        dirs.append(xdgDataDirs());
+
     if (type == AppDataLocation || type == AppLocalDataLocation) {
+        QStringList xdgDirs = xdgDataDirs();
+        for (int i = 0; i < xdgDirs.count(); ++i)
+            appendOrganizationAndApp(xdgDirs[i]);
+        dirs.append(xdgDirs);
+
         CFBundleRef mainBundle = CFBundleGetMainBundle();
         if (mainBundle) {
             CFURLRef bundleUrl = CFBundleCopyBundleURL(mainBundle);


More information about the kde-mac mailing list