1 /* This file is part of the KDE project
2 Copyright (C) 2007, 2008 David Faure <faure@kde.org>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License or ( at
7 your option ) version 3 or, at the discretion of KDE e.V. ( which shall
8 act as a proxy as in section 14 of the GPLv3 ), any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; see the file COPYING. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 #include "mimetypewriter.h"
24 #include <kstandarddirs.h>
26 #include <kstandarddirs.h>
28 #include <QXmlStreamWriter>
31 class MimeTypeWriterPrivate
34 QString
localFilePath() const;
38 QStringList m_patterns
;
42 MimeTypeWriter::MimeTypeWriter(const QString
& mimeType
)
43 : d(new MimeTypeWriterPrivate
)
45 d
->m_mimeType
= mimeType
;
46 Q_ASSERT(!mimeType
.isEmpty());
49 MimeTypeWriter::~MimeTypeWriter()
54 void MimeTypeWriter::setComment(const QString
& comment
)
56 d
->m_comment
= comment
;
59 void MimeTypeWriter::setPatterns(const QStringList
& patterns
)
61 d
->m_patterns
= patterns
;
64 void MimeTypeWriter::setMarker(const QString
& marker
)
69 bool MimeTypeWriter::write()
71 const QString packageFileName
= d
->localFilePath();
72 kDebug() << "writing" << packageFileName
;
73 QFile
packageFile(packageFileName
);
74 if (!packageFile
.open(QIODevice::WriteOnly
)) {
75 kError() << "Couldn't open" << packageFileName
<< "for writing";
78 QXmlStreamWriter
writer(&packageFile
);
79 writer
.setAutoFormatting(true);
80 writer
.writeStartDocument();
81 if (!d
->m_marker
.isEmpty()) {
82 writer
.writeComment(d
->m_marker
);
84 const QString nsUri
= "http://www.freedesktop.org/standards/shared-mime-info";
85 writer
.writeDefaultNamespace(nsUri
);
86 writer
.writeStartElement("mime-info");
87 writer
.writeStartElement(nsUri
, "mime-type");
88 writer
.writeAttribute("type", d
->m_mimeType
);
90 writer
.writeStartElement(nsUri
, "comment");
91 writer
.writeCharacters(d
->m_comment
);
92 writer
.writeEndElement(); // comment
94 foreach(const QString
& pattern
, d
->m_patterns
) {
95 writer
.writeStartElement(nsUri
, "glob");
96 writer
.writeAttribute("pattern", pattern
);
97 writer
.writeEndElement(); // glob
100 writer
.writeEndElement(); // mime-info
101 writer
.writeEndElement(); // mime-type
102 writer
.writeEndDocument();
106 void MimeTypeWriter::runUpdateMimeDatabase()
108 const QString localPackageDir
= KStandardDirs::locateLocal("xdgdata-mime", QString());
109 Q_ASSERT(!localPackageDir
.isEmpty());
111 proc
<< "update-mime-database";
112 proc
<< localPackageDir
;
113 const int exitCode
= proc
.execute();
115 kWarning() << proc
.program() << "exited with error code" << exitCode
;
119 QString
MimeTypeWriterPrivate::localFilePath() const
121 // XDG shared mime: we must write into a <kdehome>/share/mime/packages/ file...
122 // To simplify our job, let's use one "input" file per mimetype, in the user's dir.
123 // (this writes into $HOME/.local/share/mime by default)
125 // We could also use Override.xml, says the spec, but then we'd need to merge with other mimetypes,
126 // and in ~/.local we don't really expect other packages to be installed anyway...
127 // TODO this writes into $HOME/.local/share/mime which makes the unit test mess up the user's configuration...
128 // can we avoid that? is $KDEHOME/share/mime available too?
129 QString baseName
= m_mimeType
;
130 baseName
.replace('/', '-');
131 return KStandardDirs::locateLocal( "xdgdata-mime", "packages/" + baseName
+ ".xml" );
135 QString
MimeTypeWriter::existingDefinitionFile() const
137 QString baseName
= d
->m_mimeType
;
138 baseName
.replace('/', '-');
139 return KGlobal::dirs()->findResource( "xdgdata-mime", "packages/" + baseName
+ ".xml" );