1/*
2 * Copyright (C) 2002 David Faure <faure@kde.org>
3 * Copyright (C) 2008 Pino Toscano <pino@kde.org>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License version 2 as published by the Free Software Foundation;
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include <QFile>
21
22#include <kmimetype.h>
23#include <kcmdlineargs.h>
24#include <QCoreApplication>
25#include <kdeversion.h>
26#include <klocale.h>
27#include <kcomponentdata.h>
28
29#include <stdio.h>
30
31int main(int argc, char *argv[])
32{
33 KCmdLineArgs::init( argc, argv, "kmimetypefinder", 0, ki18n("MimeType Finder"), KDE_VERSION_STRING , ki18n("Gives the mimetype for a given file"));
34
35
36 KCmdLineOptions options;
37
38 options.add("c").add("content", ki18n("Use only the file content for determining the mimetype."));
39 options.add("f").add("filename-only", ki18n("Whether use the file name only for determining the mimetype. Not used if -c is specified."));
40 options.add("+filename", ki18n("The filename to test. '-' to read from stdin."));
41
42 KCmdLineArgs::addCmdLineOptions( options );
43
44 QCoreApplication app(argc, argv); // in case KSycoca needs a QEventLoop
45 KComponentData instance("kmimetypefinder");
46
47 KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
48 if( args->count() < 1 ) {
49 printf( "No filename specified\n" );
50 return 1;
51 }
52 const QString fileName = args->arg( 0 );
53 int accuracy;
54 KMimeType::Ptr mime;
55 if (fileName == QLatin1String("-")) {
56 QFile qstdin;
57 qstdin.open(stdin, QIODevice::ReadOnly);
58 const QByteArray data = qstdin.readAll();
59 mime = KMimeType::findByContent(data, &accuracy);
60 } else if (args->isSet("c")) {
61 mime = KMimeType::findByFileContent(fileName, &accuracy);
62 } else {
63 mime = KMimeType::findByPath(fileName, 0, args->isSet("f"), &accuracy);
64 }
65 if ( mime && mime->name() != KMimeType::defaultMimeType() ) {
66 printf("%s\n", mime->name().toLatin1().constData());
67 printf("(accuracy %d)\n", accuracy);
68 } else {
69 return 1; // error
70 }
71
72 return 0;
73}
74