Skip to content

Commit

Permalink
Merge branch 'master' into 'master'
Browse files Browse the repository at this point in the history
*Support for newer xdg-utils:

Changed defaults.list into mimeapps.list
NOTE : needs further testing, has been tested on my own device so far with ok results

See merge request !1
  • Loading branch information
pvuorela committed Apr 22, 2016
2 parents 281dc1f + c4a1161 commit 2180fe9
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 33 deletions.
2 changes: 1 addition & 1 deletion data/data.pro
Expand Up @@ -16,7 +16,7 @@ testdata.files = \
INSTALLS += testdata

applications.path = /usr/share/applications
applications.files = defaults.list
applications.files = mimeapps.list
INSTALLS += applications

XML_FILES = highlight1.xml.in
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions rpm/libcontentaction-qt5.spec
@@ -1,6 +1,6 @@
Name: libcontentaction-qt5
Summary: Library for associating content with actions
Version: 0.2.3
Version: 0.2.9
Release: 1
Group: System/Desktop
License: LGPLv2.1
Expand Down Expand Up @@ -79,7 +79,7 @@ rm -rf %{buildroot}
%{_datadir}/contentaction/highlight1.xml
%{_datadir}/contentaction/tracker1.xml
%{_libdir}/libcontentaction5.so.*
%exclude %{_datadir}/applications/defaults.list
%exclude %{_datadir}/applications/mimeapps.list

%files devel
%defattr(-,root,root,-)
Expand Down
69 changes: 42 additions & 27 deletions src/mime.cpp
Expand Up @@ -80,7 +80,7 @@ void readKeyValues(QFile& file, QHash<QString, QString>& dict)


QHash<QString, QString> readChangedKeyValueFiles(const QStringList& dirs,
const QString& suffix)
const QStringList& suffixes)
{
// Read each file if it has changed since the last time we read it.

Expand All @@ -106,19 +106,24 @@ QHash<QString, QString> readChangedKeyValueFiles(const QStringList& dirs,
// Read the files in such a order that the first ones override the later
// ones.
for (int i = dirs.size()-1; i >= 0; --i) {
long lm = 0;
QString filename = dirs[i] + suffix;
if (!readLastModifiedTime(filename, lm)) {
// Most probably the file doesn't exist.
continue;
}
if (!rereadAllThatFollows && lm == lastModified.value(filename)) {
continue;
for (int suffix = 0; suffix < suffixes.size(); suffix++) {
QString filename = dirs[i] + suffixes[suffix];
if (QFileInfo::exists(filename)) {
long lm = 0;
if (!readLastModifiedTime(filename, lm)) {
// Most probably the file doesn't exist.
continue;
}
if (!rereadAllThatFollows && lm == lastModified.value(filename)) {
continue;
}
QFile f(filename);
readKeyValues(f, temp);
lastModified[filename] = lm;
rereadAllThatFollows = true;
break;
}
}
QFile f(filename);
readKeyValues(f, temp);
lastModified[filename] = lm;
rereadAllThatFollows = true;
}
return temp;
}
Expand Down Expand Up @@ -172,12 +177,11 @@ QString Internal::mimeForFile(const QUrl& uri)
char *type = g_content_type_guess (fileUri.toLocalFile().toLocal8Bit().constData(),
NULL, 0, NULL);
QString res;
if (type)
{
if (type) {
gchar *temp = g_content_type_get_mime_type (type);
res = temp;
g_free (temp);
}
}
g_free (type);
return res;
}
Expand Down Expand Up @@ -215,9 +219,17 @@ static const QStringList& xdgDataDirs()
return dirs;
}

static const QString getMimeFile(QString rootPath)
{
QString mimeFileDB = rootPath + "/applications/mimeapps.list";
if (!QFileInfo::exists(mimeFileDB))
mimeFileDB = rootPath + "/applications/defaults.list";
return mimeFileDB;
}

static void writeDefaultsList(const QHash<QString, QString>& defaults)
{
QString targetFileName = xdgDataHome() + "/applications/defaults.list";
QString targetFileName = xdgDataHome() + "/applications/mimeapps.list";
QFile outFile(targetFileName + ".temp");
if (!outFile.open(QIODevice::WriteOnly)) {
LCA_WARNING << "cannot write" << outFile.fileName();
Expand Down Expand Up @@ -251,9 +263,9 @@ void setMimeDefault(const QString& mimeType, const Action& action)
void setMimeDefault(const QString& mimeType, const QString& app)
{
QHash<QString, QString> defaults;
// Read the contents of $XDG_DATA_HOME/applications/defaults.list (if
// Read the contents of $XDG_DATA_HOME/applications/mimeapps.list (if
// it exists)
QFile file(xdgDataHome() + "/applications/defaults.list");
QFile file(getMimeFile(xdgDataHome()));
readKeyValues(file, defaults);

defaults[mimeType] = app + ".desktop";
Expand All @@ -267,9 +279,9 @@ void setMimeDefault(const QString& mimeType, const QString& app)
void resetMimeDefault(const QString& mimeType)
{
QHash<QString, QString> defaults;
// Read the contents of $XDG_DATA_HOME/applications/defaults.list (if
// Read the contents of $XDG_DATA_HOME/applications/mimeapps.list (if
// it exists)
QFile file(xdgDataHome() + "/applications/defaults.list");
QFile file(getMimeFile(xdgDataHome()));
readKeyValues(file, defaults);

defaults.remove(mimeType);
Expand All @@ -294,18 +306,20 @@ QString Internal::findDesktopFile(const QString& id)
}

// Returns the default application for handling the given \a contentType. The
// default application is read from the defaults.list. If there is no default
// default application is read from the mimeapps.list. If there is no default
// application, returns an empty string.
QString Internal::defaultAppForContentType(const QString& contentType)
{
static QHash<QString, QString> defaultApps;

QStringList dirs = xdgDataDirs();

// Read the defaults.lists which have changed since we read them the last
// time.
QHash<QString, QString> temp = readChangedKeyValueFiles(dirs,
"/applications/defaults.list");
// Read the mimeapps.lists / defaults.lists which have changed since we
// read them the last time.
QStringList files;
files << "/applications/mimeapps.list";
files << "/applications/defaults.list";
QHash<QString, QString> temp = readChangedKeyValueFiles(dirs, files);

// Merge the results into our cache
QHashIterator<QString, QString> it(temp);
Expand All @@ -330,10 +344,11 @@ const QHash<QString, QStringList>& Internal::mimeApps()

QStringList dirs = xdgDataDirs();


// Read the mimeinfo.caches which have changed since we read them the last
// time.
QHash<QString, QString> temp = readChangedKeyValueFiles(dirs,
"/applications/mimeinfo.cache");
QStringList("/applications/mimeinfo.cache"));

// Merge the changes into our cache
QHashIterator<QString, QString> it(temp);
Expand Down
1 change: 1 addition & 0 deletions src/src.pro
Expand Up @@ -8,6 +8,7 @@ include(../common.pri)
# day?
VERSION = 0.0.75


QT = core xml dbus
CONFIG += link_pkgconfig hide_symbols
CONFIG -= link_prl
Expand Down
2 changes: 1 addition & 1 deletion tests/applications/applications.pro
Expand Up @@ -5,7 +5,7 @@ include(../../common.pri)
desktop_tests.path = $$CONTENTACTION_TESTDIR/applications
desktop_tests.files = \
mimeinfo.cache \
defaults.list \
mimeapps.list \
uberexec.desktop \
unterexec.desktop \
ubermimeopen.desktop \
Expand Down
2 changes: 1 addition & 1 deletion tests/test-invalid-defaults.py
Expand Up @@ -38,7 +38,7 @@
class InvalidDefaults(unittest.TestCase):
def testInvalidDefaultForUri(self):
# this uri is a ncal:Event, and x-maemo-nepomuk/calendar-event has a
# defaults.list entry pointing to a nonexistent application.
# mimeapps.list entry pointing to a nonexistent application.
(status, output) = getstatusoutput("lca-tool --tracker --printdefault urn:test:calendarevent")
self.assert_(status == 0)
self.assert_(output.find("Invalid action") != -1)
Expand Down
2 changes: 1 addition & 1 deletion tests/test-mimedefaults.cpp
Expand Up @@ -57,7 +57,7 @@ void TestMimeDefaults::initTestCase()
void TestMimeDefaults::cleanupTestCase()
{
// Unfortunately, no rmtree in Qt/C++.
QFile file(tempApplications + "/defaults.list");
QFile file(tempApplications + "/mimeapps.list");
file.remove();
QDir(".").rmpath(QString(tempApplications));
}
Expand Down
1 change: 1 addition & 0 deletions tests/testcase.pri
@@ -1,5 +1,6 @@
QT += testlib
INCLUDEPATH += ../src

LIBS += -L../src
LIBS += -lcontentaction5

Expand Down
1 change: 1 addition & 0 deletions tests/tests.pro
@@ -1,4 +1,5 @@
TEMPLATE = subdirs

SUBDIRS = \
applications \
test-l10n-data \
Expand Down

0 comments on commit 2180fe9

Please sign in to comment.